sclLiberty.c 64.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/**CFile****************************************************************

  FileName    [sclLiberty.c]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [Standard-cell library representation.]

  Synopsis    [Liberty parser.]

  Author      [Alan Mishchenko, Niklas Een]
  
  Affiliation [UC Berkeley]

  Date        [Ver. 1.0. Started - August 24, 2012.]

  Revision    [$Id: sclLiberty.c,v 1.0 2012/08/24 00:00:00 alanmi Exp $]

***********************************************************************/
20
#include <string.h>
21 22 23 24
#ifdef _WIN32
#include <shlwapi.h>
#pragma comment(lib, "shlwapi.lib")
#else 
25
#include <fnmatch.h>
26
#endif
27 28

#include "sclLib.h"
29 30
#include "misc/st/st.h"
#include "map/mio/mio.h"
31 32 33 34 35 36 37 38

ABC_NAMESPACE_IMPL_START


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

39
// #define ABC_MAX_LIB_STR_LEN 5000
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

// entry types
typedef enum { 
    SCL_LIBERTY_NONE = 0,        // 0:  unknown
    SCL_LIBERTY_PROC,            // 1:  procedure :  key(head){body}
    SCL_LIBERTY_EQUA,            // 2:  equation  :  key:head;
    SCL_LIBERTY_LIST             // 3:  list      :  key(head) 
} Scl_LibertyType_t;

typedef struct Scl_Pair_t_ Scl_Pair_t;
struct Scl_Pair_t_
{
    int             Beg;          // item beginning
    int             End;          // item end
};

typedef struct Scl_Item_t_ Scl_Item_t;
struct Scl_Item_t_
{
    int             Type;         // Scl_LibertyType_t
    int             iLine;        // file line where the item's spec begins
    Scl_Pair_t      Key;          // key part
    Scl_Pair_t      Head;         // head part 
    Scl_Pair_t      Body;         // body part
    int             Next;         // next item in the list 
    int             Child;        // first child item 
};

typedef struct Scl_Tree_t_ Scl_Tree_t;
struct Scl_Tree_t_
{
    char *          pFileName;    // input Liberty file name
    char *          pContents;    // file contents
73
    long            nContents;    // file size
74 75 76 77 78 79
    int             nLines;       // line counter
    int             nItems;       // number of items
    int             nItermAlloc;  // number of items allocated
    Scl_Item_t *    pItems;       // the items
    char *          pError;       // the error string
    abctime         clkStart;     // beginning time
80
    Vec_Str_t *     vBuffer;      // temp string buffer
81 82
};

83 84 85 86 87 88 89 90

static inline int          Scl_LibertyGlobMatch(const char * pattern, const char * string) {
    #ifdef _WIN32
    return PathMatchSpec(string, pattern);
    #else
    return fnmatch(pattern, string, 0) == 0;
    #endif
}
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 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
static inline Scl_Item_t *  Scl_LibertyRoot( Scl_Tree_t * p )                                      { return p->pItems;                                                 }
static inline Scl_Item_t *  Scl_LibertyItem( Scl_Tree_t * p, int v )                               { assert( v < p->nItems ); return v < 0 ? NULL : p->pItems + v;     }
static inline int           Scl_LibertyCompare( Scl_Tree_t * p, Scl_Pair_t Pair, char * pStr )     { return strncmp( p->pContents+Pair.Beg, pStr, Pair.End-Pair.Beg ) || ((int)strlen(pStr) != Pair.End-Pair.Beg); }
static inline void          Scl_PrintWord( FILE * pFile, Scl_Tree_t * p, Scl_Pair_t Pair )         { char * pBeg = p->pContents+Pair.Beg, * pEnd = p->pContents+Pair.End; while ( pBeg < pEnd ) fputc( *pBeg++, pFile ); }
static inline void          Scl_PrintSpace( FILE * pFile, int nOffset )                            { int i; for ( i = 0; i < nOffset; i++ ) fputc(' ', pFile);         }
static inline int           Scl_LibertyItemId( Scl_Tree_t * p, Scl_Item_t * pItem )                { return pItem - p->pItems;                                         }

#define Scl_ItemForEachChild( p, pItem, pChild ) \
    for ( pChild = Scl_LibertyItem(p, pItem->Child); pChild; pChild = Scl_LibertyItem(p, pChild->Next) )
#define Scl_ItemForEachChildName( p, pItem, pChild, pName ) \
    for ( pChild = Scl_LibertyItem(p, pItem->Child); pChild; pChild = Scl_LibertyItem(p, pChild->Next) ) if ( Scl_LibertyCompare(p, pChild->Key, pName) ) {} else

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

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

  Synopsis    [Prints parse tree in Liberty format.]

  Description []

  SideEffects []

  SeeAlso     []

***********************************************************************/
void Scl_LibertyParseDumpItem( FILE * pFile, Scl_Tree_t * p, Scl_Item_t * pItem, int nOffset )
{
    if ( pItem->Type == SCL_LIBERTY_PROC )
    {
        Scl_PrintSpace( pFile, nOffset );
        Scl_PrintWord( pFile, p, pItem->Key );
        fprintf( pFile, "(" );
        Scl_PrintWord( pFile, p, pItem->Head );
        fprintf( pFile, ") {\n" );
        if ( Scl_LibertyItem(p, pItem->Child) )
            Scl_LibertyParseDumpItem( pFile, p, Scl_LibertyItem(p, pItem->Child), nOffset + 2 );
        Scl_PrintSpace( pFile, nOffset );
        fprintf( pFile, "}\n" );
    }
    else if ( pItem->Type == SCL_LIBERTY_EQUA )
    {
        Scl_PrintSpace( pFile, nOffset );
        Scl_PrintWord( pFile, p, pItem->Key );
        fprintf( pFile, " : " );
        Scl_PrintWord( pFile, p, pItem->Head );
        fprintf( pFile, ";\n" );
    }
    else if ( pItem->Type == SCL_LIBERTY_LIST )
    {
        Scl_PrintSpace( pFile, nOffset );
        Scl_PrintWord( pFile, p, pItem->Key );
        fprintf( pFile, "(" );
        Scl_PrintWord( pFile, p, pItem->Head );
        fprintf( pFile, ");\n" );
    }
    else assert( 0 );
    if ( Scl_LibertyItem(p, pItem->Next) )
        Scl_LibertyParseDumpItem( pFile, p, Scl_LibertyItem(p, pItem->Next), nOffset );
}
int Scl_LibertyParseDump( Scl_Tree_t * p, char * pFileName )
{
    FILE * pFile;
    if ( pFileName == NULL )
        pFile = stdout;
    else
    {
        pFile = fopen( pFileName, "w" );
        if ( pFile == NULL )
        {
            printf( "Scl_LibertyParseDump(): The output file is unavailable (absent or open).\n" );
            return 0;
        }
    }
    Scl_LibertyParseDumpItem( pFile, p, Scl_LibertyRoot(p), 0 );
    if ( pFile != stdout )
        fclose( pFile );
    return 1;
}


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

  Synopsis    [Gets the name to write.]

  Description []

  SideEffects []

  SeeAlso     []

***********************************************************************/
int Scl_LibertyCountItems( char * pBeg, char * pEnd )
{
    int Counter = 0;
    for ( ; pBeg < pEnd; pBeg++ )
        Counter += (*pBeg == '(' || *pBeg == ':');
    return Counter;
}
// removes C-style comments
192
/*
193 194 195 196 197 198 199 200 201 202 203 204 205
void Scl_LibertyWipeOutComments( char * pBeg, char * pEnd )
{
    char * pCur, * pStart;
    for ( pCur = pBeg; pCur < pEnd; pCur++ )
    if ( pCur[0] == '/' && pCur[1] == '*' )
        for ( pStart = pCur; pCur < pEnd; pCur++ )
        if ( pCur[0] == '*' && pCur[1] == '/' )
        {
            for ( ; pStart < pCur + 2; pStart++ )
            if ( *pStart != '\n' ) *pStart = ' ';
            break;
        }
}
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
*/
void Scl_LibertyWipeOutComments( char * pBeg, char * pEnd )
{
    char * pCur, * pStart;
    for ( pCur = pBeg; pCur < pEnd-1; pCur++ )
        if ( pCur[0] == '/' && pCur[1] == '*' )
        {
            for ( pStart = pCur; pCur < pEnd-1; pCur++ )
                if ( pCur[0] == '*' && pCur[1] == '/' )
                {
                    for ( ; pStart < pCur + 2; pStart++ )
                    if ( *pStart != '\n' ) *pStart = ' ';
                    break;
                }
        }
        else if ( pCur[0] == '/' && pCur[1] == '/' )
        {
            for ( pStart = pCur; pCur < pEnd; pCur++ )
                if ( pCur[0] == '\n' || pCur == pEnd-1 )
                {
                    for ( ; pStart < pCur; pStart++ ) *pStart = ' ';
                    break;
                }
        }
}
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 352 353 354 355 356 357 358
static inline int Scl_LibertyCharIsSpace( char c )
{
    return c == ' ' || c == '\t' || c == '\r' || c == '\n' || c == '\\';
}
static inline int Scl_LibertySkipSpaces( Scl_Tree_t * p, char ** ppPos, char * pEnd, int fStopAtNewLine )
{
    char * pPos = *ppPos;
    for ( ; pPos < pEnd; pPos++ )
    {
        if ( *pPos == '\n' )
        {
            p->nLines++;
            if ( fStopAtNewLine )
                break;
        }        
        if ( !Scl_LibertyCharIsSpace(*pPos) )
            break;
    }
    *ppPos = pPos;
    return pPos == pEnd;
}
// skips entry delimited by " :;(){}" and returns 1 if reached the end
static inline int Scl_LibertySkipEntry( char ** ppPos, char * pEnd )
{
    char * pPos = *ppPos;
    if ( *pPos == '\"' )
    {
        for ( pPos++; pPos < pEnd; pPos++ )
            if ( *pPos == '\"' )
            {
                pPos++;
                break;
            }
    }
    else
    {
        for ( ; pPos < pEnd; pPos++ )
            if ( *pPos == ' ' || *pPos == '\r' || *pPos == '\n' || *pPos == '\t' ||
                 *pPos == ':' || *pPos == ';'  || 
                 *pPos == '(' || *pPos == ')'  || 
                 *pPos == '{' || *pPos == '}' )
                break;
    }
    *ppPos = pPos;
    return pPos == pEnd;
}
// finds the matching closing symbol
static inline char * Scl_LibertyFindMatch( char * pPos, char * pEnd )
{
    int Counter = 0;
    assert( *pPos == '(' || *pPos == '{' );
    if ( *pPos == '(' )
    {
        for ( ; pPos < pEnd; pPos++ )
        {
            if ( *pPos == '(' )
                Counter++;
            if ( *pPos == ')' )
                Counter--;
            if ( Counter == 0 )
                break;
        }
    }
    else
    {
        for ( ; pPos < pEnd; pPos++ )
        {
            if ( *pPos == '{' )
                Counter++;
            if ( *pPos == '}' )
                Counter--;
            if ( Counter == 0 )
                break;
        }
    }
    assert( *pPos == ')' || *pPos == '}' );
    return pPos;
}
// trims spaces around the head
static inline Scl_Pair_t Scl_LibertyUpdateHead( Scl_Tree_t * p, Scl_Pair_t Head )
{
    Scl_Pair_t Res;
    char * pBeg = p->pContents + Head.Beg;
    char * pEnd = p->pContents + Head.End;
    char * pFirstNonSpace = NULL;
    char * pLastNonSpace = NULL;
    char * pChar;
    for ( pChar = pBeg; pChar < pEnd; pChar++ )
    {
        if ( *pChar == '\n' )
            p->nLines++;
        if ( Scl_LibertyCharIsSpace(*pChar) )
            continue;
        pLastNonSpace = pChar;
        if ( pFirstNonSpace == NULL )
            pFirstNonSpace = pChar;
    }
    if ( pFirstNonSpace == NULL || pLastNonSpace == NULL )
        return Head;
    assert( pFirstNonSpace && pLastNonSpace );
    Res.Beg = pFirstNonSpace - p->pContents;
    Res.End = pLastNonSpace  - p->pContents + 1;
    return Res;
}
// returns new item
static inline Scl_Item_t * Scl_LibertyNewItem( Scl_Tree_t * p, int Type )
{
    p->pItems[p->nItems].iLine = p->nLines;
    p->pItems[p->nItems].Type  = Type;
    p->pItems[p->nItems].Child = -1;
    p->pItems[p->nItems].Next  = -1;
    return p->pItems + p->nItems++;
}


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

  Synopsis    [Gets the name to write.]

  Description []

  SideEffects []

  SeeAlso     []

***********************************************************************/
char * Scl_LibertyReadString( Scl_Tree_t * p, Scl_Pair_t Pair )   
{ 
359 360 361 362 363
    // static char Buffer[ABC_MAX_LIB_STR_LEN]; 
    char * Buffer;
    if ( Pair.End - Pair.Beg + 2 > Vec_StrSize(p->vBuffer) )
        Vec_StrFill( p->vBuffer, Pair.End - Pair.Beg + 100, '\0' );
    Buffer = Vec_StrArray( p->vBuffer );
364 365 366 367 368 369 370 371 372 373 374 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
    strncpy( Buffer, p->pContents+Pair.Beg, Pair.End-Pair.Beg ); 
    if ( Pair.Beg < Pair.End && Buffer[0] == '\"' )
    {
        assert( Buffer[Pair.End-Pair.Beg-1] == '\"' );
        Buffer[Pair.End-Pair.Beg-1] = 0;
        return Buffer + 1;
    }
    Buffer[Pair.End-Pair.Beg] = 0;
    return Buffer;
}
int Scl_LibertyItemNum( Scl_Tree_t * p, Scl_Item_t * pRoot, char * pName )
{
    Scl_Item_t * pItem;
    int Counter = 0;
    Scl_ItemForEachChildName( p, pRoot, pItem, pName )
        Counter++;
    return Counter;
}

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

  Synopsis    [Returns free item.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Scl_LibertyBuildItem( Scl_Tree_t * p, char ** ppPos, char * pEnd )
{
    Scl_Item_t * pItem;
    Scl_Pair_t Key, Head, Body;
    char * pNext, * pStop;
    Key.End = 0;
    if ( Scl_LibertySkipSpaces( p, ppPos, pEnd, 0 ) )
        return -2;
    Key.Beg = *ppPos - p->pContents;
    if ( Scl_LibertySkipEntry( ppPos, pEnd ) )
        goto exit;
    Key.End = *ppPos - p->pContents;
    if ( Scl_LibertySkipSpaces( p, ppPos, pEnd, 0 ) )
        goto exit;
    pNext = *ppPos;
    if ( *pNext == ':' )
    {
        *ppPos = pNext + 1;
        if ( Scl_LibertySkipSpaces( p, ppPos, pEnd, 0 ) )
            goto exit;
        Head.Beg = *ppPos - p->pContents;
        if ( Scl_LibertySkipEntry( ppPos, pEnd ) )
            goto exit;
        Head.End = *ppPos - p->pContents;
        if ( Scl_LibertySkipSpaces( p, ppPos, pEnd, 1 ) )
            goto exit;
        pNext = *ppPos;
421 422 423 424 425 426 427 428 429 430 431 432
        while ( *pNext == '+' || *pNext == '-' || *pNext == '*' || *pNext == '/' )
        {
        (*ppPos) += 1;
            if ( Scl_LibertySkipSpaces( p, ppPos, pEnd, 0 ) )
                goto exit;
            if ( Scl_LibertySkipEntry( ppPos, pEnd ) )
                goto exit;
            Head.End = *ppPos - p->pContents;
            if ( Scl_LibertySkipSpaces( p, ppPos, pEnd, 1 ) )
                goto exit;
        pNext = *ppPos;
        }
433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 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
        if ( *pNext != ';' && *pNext != '\n' )
            goto exit;
        *ppPos = pNext + 1;
        // end of equation
        pItem = Scl_LibertyNewItem( p, SCL_LIBERTY_EQUA );
        pItem->Key  = Key;
        pItem->Head = Scl_LibertyUpdateHead( p, Head );
        pItem->Next = Scl_LibertyBuildItem( p, ppPos, pEnd );
        if ( pItem->Next == -1 )
            goto exit;
        return Scl_LibertyItemId( p, pItem );
    }
    if ( *pNext == '(' )
    {
        pStop = Scl_LibertyFindMatch( pNext, pEnd );
        Head.Beg = pNext - p->pContents + 1;
        Head.End = pStop - p->pContents;
        *ppPos = pStop + 1;
        if ( Scl_LibertySkipSpaces( p, ppPos, pEnd, 0 ) )
        {
            // end of list
            pItem = Scl_LibertyNewItem( p, SCL_LIBERTY_LIST );
            pItem->Key  = Key;
            pItem->Head = Scl_LibertyUpdateHead( p, Head );
            return Scl_LibertyItemId( p, pItem );
        }
        pNext = *ppPos;
        if ( *pNext == '{' ) // beginning of body
        {
            pStop = Scl_LibertyFindMatch( pNext, pEnd );
            Body.Beg = pNext - p->pContents + 1;
            Body.End = pStop - p->pContents;
            // end of body
            pItem = Scl_LibertyNewItem( p, SCL_LIBERTY_PROC );
            pItem->Key  = Key;
            pItem->Head = Scl_LibertyUpdateHead( p, Head );
            pItem->Body = Body;
            *ppPos = pNext + 1;
            pItem->Child = Scl_LibertyBuildItem( p, ppPos, pStop );
            if ( pItem->Child == -1 )
                goto exit;
            *ppPos = pStop + 1;
            pItem->Next = Scl_LibertyBuildItem( p, ppPos, pEnd );
            if ( pItem->Next == -1 )
                goto exit;
            return Scl_LibertyItemId( p, pItem );
        }
        // end of list
        if ( *pNext == ';' )
            *ppPos = pNext + 1;
        pItem = Scl_LibertyNewItem( p, SCL_LIBERTY_LIST );
        pItem->Key  = Key;
        pItem->Head = Scl_LibertyUpdateHead( p, Head );
        pItem->Next = Scl_LibertyBuildItem( p, ppPos, pEnd );
        if ( pItem->Next == -1 )
            goto exit;
        return Scl_LibertyItemId( p, pItem );
    }
491 492 493 494 495
    if ( *pNext == ';' )
    {
        *ppPos = pNext + 1;
        return Scl_LibertyBuildItem(p, ppPos, pEnd);
    }
496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523
exit:
    if ( p->pError == NULL )
    {
        p->pError = ABC_ALLOC( char, 1000 );
        sprintf( p->pError, "File \"%s\". Line %6d. Failed to parse entry \"%s\".\n", 
            p->pFileName, p->nLines, Scl_LibertyReadString(p, Key) );
    }
    return -1;
}

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

  Synopsis    [File management.]

  Description []

  SideEffects []

  SeeAlso     []

***********************************************************************/
void Scl_LibertyFixFileName( char * pFileName )
{
    char * pHead;
    for ( pHead = pFileName; *pHead; pHead++ )
        if ( *pHead == '>' )
            *pHead = '\\';
}
524
long Scl_LibertyFileSize( char * pFileName )
525 526
{
    FILE * pFile;
527
    long nFileSize;
528 529 530 531 532 533 534 535 536 537 538
    pFile = fopen( pFileName, "rb" );
    if ( pFile == NULL )
    {
        printf( "Scl_LibertyFileSize(): The input file is unavailable (absent or open).\n" );
        return 0;
    }
    fseek( pFile, 0, SEEK_END );  
    nFileSize = ftell( pFile ); 
    fclose( pFile );
    return nFileSize;
}
539
char * Scl_LibertyFileContents( char * pFileName, long nContents )
540 541 542
{
    FILE * pFile = fopen( pFileName, "rb" );
    char * pContents = ABC_ALLOC( char, nContents+1 );
Alan Mishchenko committed
543
    int RetValue = 0;
544
    RetValue = fread( pContents, nContents, 1, pFile );
545 546 547 548 549 550 551
    fclose( pFile );
    pContents[nContents] = 0;
    return pContents;
}
void Scl_LibertyStringDump( char * pFileName, Vec_Str_t * vStr )
{
    FILE * pFile = fopen( pFileName, "wb" );
Alan Mishchenko committed
552
    int RetValue = 0;
553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575
    if ( pFile == NULL )
    {
        printf( "Scl_LibertyStringDump(): The output file is unavailable.\n" );
        return;
    }
    RetValue = fwrite( Vec_StrArray(vStr), 1, Vec_StrSize(vStr), pFile );
    fclose( pFile );
}

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

  Synopsis    [Starts the parsing manager.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Scl_Tree_t * Scl_LibertyStart( char * pFileName )
{
    Scl_Tree_t * p;
576
    long RetValue;
577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593
    // read the file into the buffer
    Scl_LibertyFixFileName( pFileName );
    RetValue = Scl_LibertyFileSize( pFileName );
    if ( RetValue == 0 )
        return NULL;
    // start the manager
    p = ABC_ALLOC( Scl_Tree_t, 1 );
    memset( p, 0, sizeof(Scl_Tree_t) );
    p->clkStart  = Abc_Clock();
    p->nContents = RetValue;
    p->pContents = Scl_LibertyFileContents( pFileName, p->nContents );
    // other 
    p->pFileName = Abc_UtilStrsav( pFileName );
    p->nItermAlloc = 10 + Scl_LibertyCountItems( p->pContents, p->pContents+p->nContents );
    p->pItems = ABC_CALLOC( Scl_Item_t, p->nItermAlloc );
    p->nItems = 0;
    p->nLines = 1;
594
    p->vBuffer = Vec_StrStart( 10 );
595 596 597 598 599 600 601 602 603
    return p;
}
void Scl_LibertyStop( Scl_Tree_t * p, int fVerbose )
{
    if ( fVerbose )
    {
        printf( "Memory = %7.2f MB. ", 1.0 * (p->nContents + p->nItermAlloc * sizeof(Scl_Item_t))/(1<<20) );
        ABC_PRT( "Time", Abc_Clock() - p->clkStart );
    }
604
    Vec_StrFree( p->vBuffer );
605 606 607 608 609 610 611 612 613 614 615 616 617 618
    ABC_FREE( p->pFileName );
    ABC_FREE( p->pContents );
    ABC_FREE( p->pItems );
    ABC_FREE( p->pError );
    ABC_FREE( p );
}
Scl_Tree_t * Scl_LibertyParse( char * pFileName, int fVerbose )
{
    Scl_Tree_t * p;
    char * pPos;
    if ( (p = Scl_LibertyStart(pFileName)) == NULL )
        return NULL;
    pPos = p->pContents;
    Scl_LibertyWipeOutComments( p->pContents, p->pContents+p->nContents );
Alan Mishchenko committed
619
    if ( (!Scl_LibertyBuildItem( p, &pPos, p->pContents + p->nContents )) == 0 )
620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652
    {
        if ( p->pError ) printf( "%s", p->pError );
        printf( "Parsing failed.  " );
        Abc_PrintTime( 1, "Parsing time", Abc_Clock() - p->clkStart );
    }
    else if ( fVerbose )
    {
        printf( "Parsing finished successfully.  " );
        Abc_PrintTime( 1, "Parsing time", Abc_Clock() - p->clkStart );
    }
    return p;
}

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

  Synopsis    [Fetching attributes.]

  Description []

  SideEffects []

  SeeAlso     []

***********************************************************************/
int Scl_LibertyReadCellIsFlop( Scl_Tree_t * p, Scl_Item_t * pCell )
{
    Scl_Item_t * pAttr;
    Scl_ItemForEachChild( p, pCell, pAttr )
        if ( !Scl_LibertyCompare(p, pAttr->Key, "ff") ||
             !Scl_LibertyCompare(p, pAttr->Key, "latch") )
            return 1;
    return 0;
}
653
int Scl_LibertyReadCellIsDontUse( Scl_Tree_t * p, Scl_Item_t * pCell, SC_DontUse dont_use )
654 655 656
{
    Scl_Item_t * pAttr;
    Scl_ItemForEachChild( p, pCell, pAttr )
657
    {
658 659
        if ( !Scl_LibertyCompare(p, pAttr->Key, "dont_use") )
            return 1;
660 661
        const char * cell_name = Scl_LibertyReadString(p, pCell->Head);
        for (int i = 0; i < dont_use.size; i++) {
662
            if (Scl_LibertyGlobMatch(dont_use.dont_use_list[i], cell_name)) {
663 664 665 666
                return 1;
            }
        }
    }
667 668
    return 0;
}
669 670 671 672 673 674 675
char * Scl_LibertyReadCellArea( Scl_Tree_t * p, Scl_Item_t * pCell )
{
    Scl_Item_t * pArea;
    Scl_ItemForEachChildName( p, pCell, pArea, "area" )
        return Scl_LibertyReadString(p, pArea->Head);
    return 0;
}
676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692
char * Scl_LibertyReadCellLeakage( Scl_Tree_t * p, Scl_Item_t * pCell )
{
    Scl_Item_t * pItem, * pChild;
    Scl_ItemForEachChildName( p, pCell, pItem, "cell_leakage_power" )
        return Scl_LibertyReadString(p, pItem->Head);
    // look for another type
    Scl_ItemForEachChildName( p, pCell, pItem, "leakage_power" )
    {
        Scl_ItemForEachChildName( p, pItem, pChild, "when" )
            break;
        if ( pChild && !Scl_LibertyCompare(p, pChild->Key, "when") )
            continue;
        Scl_ItemForEachChildName( p, pItem, pChild, "value" )
            return Scl_LibertyReadString(p, pChild->Head);
    }
    return 0;
}
693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728
char * Scl_LibertyReadPinFormula( Scl_Tree_t * p, Scl_Item_t * pPin )
{
    Scl_Item_t * pFunc;
    Scl_ItemForEachChildName( p, pPin, pFunc, "function" )
        return Scl_LibertyReadString(p, pFunc->Head);
    return NULL;
}
int Scl_LibertyReadCellIsThreeState( Scl_Tree_t * p, Scl_Item_t * pCell )
{
    Scl_Item_t * pPin, * pItem;
    Scl_ItemForEachChildName( p, pCell, pPin, "pin" )
        Scl_ItemForEachChildName( p, pPin, pItem, "three_state" )
            return 1;
    return 0;
}
int Scl_LibertyReadCellOutputNum( Scl_Tree_t * p, Scl_Item_t * pCell )
{
    Scl_Item_t * pPin;
    int Counter = 0;
    Scl_ItemForEachChildName( p, pCell, pPin, "pin" )
        if ( Scl_LibertyReadPinFormula(p, pPin) )
            Counter++;
    return Counter;
}

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

  Synopsis    [Parses the standard cell library in Liberty format.]

  Description [Writes the resulting file in Genlib format.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
729
Vec_Str_t * Scl_LibertyReadGenlibStr( Scl_Tree_t * p, int fVerbose, SC_DontUse dont_use )
730 731 732 733 734 735 736 737 738 739 740 741 742 743
{
    Vec_Str_t * vStr;
    Scl_Item_t * pCell, * pOutput, * pInput;
    char * pFormula;
    vStr = Vec_StrAlloc( 1000 );
    Vec_StrPrintStr( vStr, "GATE          _const0_  0.000000  z=CONST0;\n" );
    Vec_StrPrintStr( vStr, "GATE          _const1_  0.000000  z=CONST1;\n" );
    Scl_ItemForEachChildName( p, Scl_LibertyRoot(p), pCell, "cell" )
    {
        if ( Scl_LibertyReadCellIsFlop(p, pCell) )
        {
            if ( fVerbose )  printf( "Scl_LibertyReadGenlib() skipped sequential cell \"%s\".\n", Scl_LibertyReadString(p, pCell->Head) );
            continue;
        }
744
        if ( Scl_LibertyReadCellIsDontUse(p, pCell, dont_use) )
745 746 747 748
        {
            if ( fVerbose )  printf( "Scl_LibertyReadGenlib() skipped cell \"%s\" due to dont_use attribute.\n", Scl_LibertyReadString(p, pCell->Head) );
            continue;
        }
749 750 751 752 753 754 755 756 757 758 759 760 761
        if ( Scl_LibertyReadCellIsThreeState(p, pCell) )
        {
            if ( fVerbose )  printf( "Scl_LibertyReadGenlib() skipped three-state cell \"%s\".\n", Scl_LibertyReadString(p, pCell->Head) );
            continue;
        }
        if ( Scl_LibertyReadCellOutputNum(p, pCell) == 0 )
        {
            if ( fVerbose )  printf( "Scl_LibertyReadGenlib() skipped cell \"%s\" without logic function.\n", Scl_LibertyReadString(p, pCell->Head) );
            continue;
        }
        // iterate through output pins
        Scl_ItemForEachChildName( p, pCell, pOutput, "pin" )
        {
762
            if ( !(pFormula = Scl_LibertyReadPinFormula(p, pOutput)) ) 
763 764 765 766 767 768 769 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
                continue;
            if ( !strcmp(pFormula, "0") || !strcmp(pFormula, "1") )
            {
                if ( fVerbose ) printf( "Scl_LibertyReadGenlib() skipped cell \"%s\" with constant formula \"%s\".\n", Scl_LibertyReadString(p, pCell->Head), pFormula );
                break;
            }
            Vec_StrPrintStr( vStr, "GATE " );
            Vec_StrPrintStr( vStr, Scl_LibertyReadString(p, pCell->Head) );
            Vec_StrPrintStr( vStr, " " );
            Vec_StrPrintStr( vStr, Scl_LibertyReadCellArea(p, pCell) );
            Vec_StrPrintStr( vStr, " " );
            Vec_StrPrintStr( vStr, Scl_LibertyReadString(p, pOutput->Head) );
            Vec_StrPrintStr( vStr, "=" );
            Vec_StrPrintStr( vStr, pFormula );
            Vec_StrPrintStr( vStr, ";\n" );
            // iterate through input pins
            Scl_ItemForEachChildName( p, pCell, pInput, "pin" )
            {
                if ( Scl_LibertyReadPinFormula(p, pInput) == NULL )
                    continue;
                Vec_StrPrintStr( vStr, "  PIN " );
                Vec_StrPrintStr( vStr, Scl_LibertyReadString(p, pInput->Head) );
                Vec_StrPrintStr( vStr, " UNKNOWN  1  999  1.00  0.00  1.00  0.00\n" );
            }
        }
    }
    Vec_StrPrintStr( vStr, "\n.end\n" );
    Vec_StrPush( vStr, '\0' );
//    printf( "%s", Vec_StrArray(vStr) );
    return vStr;
}

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

  Synopsis    [Enabling debug output.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
//#define SCL_DEBUG
#ifdef SCL_DEBUG
Alan Mishchenko committed
808 809 810 811
static inline void Vec_StrPutI_( Vec_Str_t * vOut, int Val )     {  printf( "%d ",  Val );        Vec_StrPutI( vOut, Val );  }
static inline void Vec_StrPutW_( Vec_Str_t * vOut, word Val )    {  printf( "%lu ", (long)Val );  Vec_StrPutW( vOut, Val );  }
static inline void Vec_StrPutF_( Vec_Str_t * vOut, float Val )   {  printf( "%f ",  Val );        Vec_StrPutF( vOut, Val );  }
static inline void Vec_StrPutS_( Vec_Str_t * vOut, char * Val )  {  printf( "%s ",  Val );        Vec_StrPutS( vOut, Val );  }
812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869
static inline void Vec_StrPut_( Vec_Str_t * vOut )               {  printf( "\n" ); }
#else
static inline void Vec_StrPutI_( Vec_Str_t * vOut, int Val )     { Vec_StrPutI( vOut, Val );  }
static inline void Vec_StrPutW_( Vec_Str_t * vOut, word Val )    { Vec_StrPutW( vOut, Val );  }
static inline void Vec_StrPutF_( Vec_Str_t * vOut, float Val )   { Vec_StrPutF( vOut, Val );  }
static inline void Vec_StrPutS_( Vec_Str_t * vOut, char * Val )  { Vec_StrPutS( vOut, Val );  }
static inline void Vec_StrPut_( Vec_Str_t * vOut )               { }
#endif

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

  Synopsis    [Parsing Liberty into internal data representation.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
char * Scl_LibertyReadDefaultWireLoad( Scl_Tree_t * p )
{
    Scl_Item_t * pItem;
    Scl_ItemForEachChildName( p, Scl_LibertyRoot(p), pItem, "default_wire_load" )
        return Scl_LibertyReadString(p, pItem->Head);
    return "";
}
char * Scl_LibertyReadDefaultWireLoadSel( Scl_Tree_t * p )
{
    Scl_Item_t * pItem;
    Scl_ItemForEachChildName( p, Scl_LibertyRoot(p), pItem, "default_wire_load_selection" )
        return Scl_LibertyReadString(p, pItem->Head);
    return "";
}
float Scl_LibertyReadDefaultMaxTrans( Scl_Tree_t * p )
{
    Scl_Item_t * pItem;
    Scl_ItemForEachChildName( p, Scl_LibertyRoot(p), pItem, "default_max_transition" )
        return atof(Scl_LibertyReadString(p, pItem->Head));
    return 0;
}
int Scl_LibertyReadTimeUnit( Scl_Tree_t * p )
{
    Scl_Item_t * pItem;
    Scl_ItemForEachChildName( p, Scl_LibertyRoot(p), pItem, "time_unit" )
    {
        char * pUnit = Scl_LibertyReadString(p, pItem->Head);
        // 9=1ns, 10=100ps, 11=10ps, 12=1ps
        if ( !strcmp(pUnit, "1ns") )
            return 9;
        if ( !strcmp(pUnit, "100ps") )
            return 10;
        if ( !strcmp(pUnit, "10ps") )
            return 11;
        if ( !strcmp(pUnit, "1ps") )
            return 12;
        break;
    }
Jeremy Kun committed
870
    printf( "Liberty parser cannot read \"time_unit\".  Assuming   time_unit : \"1ns\".\n" );
871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889
    return 9;
}
void Scl_LibertyReadLoadUnit( Scl_Tree_t * p, Vec_Str_t * vOut )
{
    Scl_Item_t * pItem;
    Scl_ItemForEachChildName( p, Scl_LibertyRoot(p), pItem, "capacitive_load_unit" )
    {
        // expecting (1.00,ff) or (1, pf) ... 12 or 15 for 'pf' or 'ff'
        char * pHead   = Scl_LibertyReadString(p, pItem->Head);
        float First    = atof(strtok(pHead, " \t\n\r\\\","));
        char * pSecond = strtok(NULL, " \t\n\r\\\",");
        Vec_StrPutF_( vOut, First );
        if ( pSecond && !strcmp(pSecond, "pf") )
            Vec_StrPutI_( vOut, 12 );
        else if ( pSecond && !strcmp(pSecond, "ff") )
            Vec_StrPutI_( vOut, 15 );
        else break;
        return;
    }
Jeremy Kun committed
890
    printf( "Liberty parser cannot read \"capacitive_load_unit\". Assuming   capacitive_load_unit(1, pf).\n" );
891 892 893 894 895 896 897 898 899 900 901 902 903
    Vec_StrPutF_( vOut, 1.0 );
    Vec_StrPutI_( vOut, 12 );
}
void Scl_LibertyReadWireLoad( Scl_Tree_t * p, Vec_Str_t * vOut )
{
    Scl_Item_t * pItem, * pChild;
    Vec_StrPutI_( vOut, Scl_LibertyItemNum(p, Scl_LibertyRoot(p), "wire_load") );
    Vec_StrPut_( vOut );
    Scl_ItemForEachChildName( p, Scl_LibertyRoot(p), pItem, "wire_load" )
    {
        Vec_StrPutS_( vOut, Scl_LibertyReadString(p, pItem->Head) );
        Scl_ItemForEachChildName( p, pItem, pChild, "capacitance" )
            Vec_StrPutF_( vOut, atof(Scl_LibertyReadString(p, pChild->Head)) );
904 905
        Scl_ItemForEachChildName( p, pItem, pChild, "slope" )
            Vec_StrPutF_( vOut, atof(Scl_LibertyReadString(p, pChild->Head)) );
906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938
        Vec_StrPut_( vOut );
        Vec_StrPutI_( vOut, Scl_LibertyItemNum(p, pItem, "fanout_length") );
        Vec_StrPut_( vOut );
        Scl_ItemForEachChildName( p, pItem, pChild, "fanout_length" )
        {
            char * pHead  = Scl_LibertyReadString(p, pChild->Head);
            int    First  = atoi( strtok(pHead, " ,") );
            float  Second = atof( strtok(NULL, " ") );
            Vec_StrPutI_( vOut, First );
            Vec_StrPutF_( vOut, Second );
            Vec_StrPut_( vOut );
        }
        Vec_StrPut_( vOut );
    }
    Vec_StrPut_( vOut );
}
void Scl_LibertyReadWireLoadSelect( Scl_Tree_t * p, Vec_Str_t * vOut )
{
    Scl_Item_t * pItem, * pChild;
    Vec_StrPutI_( vOut, Scl_LibertyItemNum(p, Scl_LibertyRoot(p), "wire_load_selection") );
    Vec_StrPut_( vOut );
    Scl_ItemForEachChildName( p, Scl_LibertyRoot(p), pItem, "wire_load_selection" )
    {
        Vec_StrPutS_( vOut, Scl_LibertyReadString(p, pItem->Head) );
        Vec_StrPut_( vOut );
        Vec_StrPutI_( vOut, Scl_LibertyItemNum(p, pItem, "wire_load_from_area") );
        Vec_StrPut_( vOut );
        Scl_ItemForEachChildName( p, pItem, pChild, "wire_load_from_area" )
        {
            char * pHead  = Scl_LibertyReadString(p, pChild->Head);
            float  First  = atof( strtok(pHead, " ,") );
            float  Second = atof( strtok(NULL, " ,") );
            char * pThird = strtok(NULL, " ");
939 940
            if ( pThird[0] == '\"' )
                assert(pThird[strlen(pThird)-1] == '\"'), pThird[strlen(pThird)-1] = 0, pThird++;
941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966
            Vec_StrPutF_( vOut, First );
            Vec_StrPutF_( vOut, Second );
            Vec_StrPutS_( vOut, pThird );
            Vec_StrPut_( vOut );
        }
        Vec_StrPut_( vOut );
    }
    Vec_StrPut_( vOut );
}
int Scl_LibertyReadDeriveStrength( Scl_Tree_t * p, Scl_Item_t * pCell )
{
    Scl_Item_t * pItem;
    Scl_ItemForEachChildName( p, pCell, pItem, "drive_strength" )
        return atoi(Scl_LibertyReadString(p, pItem->Head));
    return 0;
}
int Scl_LibertyReadPinDirection( Scl_Tree_t * p, Scl_Item_t * pPin )
{
    Scl_Item_t * pItem;
    Scl_ItemForEachChildName( p, pPin, pItem, "direction" )
    {
        char * pToken = Scl_LibertyReadString(p, pItem->Head);
        if ( !strcmp(pToken, "input") )
            return 0;
        if ( !strcmp(pToken, "output") )
            return 1;
967 968
        if ( !strcmp(pToken, "internal") )
            return 2;
969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988
        break;
    }
    return -1;
}
float Scl_LibertyReadPinCap( Scl_Tree_t * p, Scl_Item_t * pPin, char * pName )
{
    Scl_Item_t * pItem;
    Scl_ItemForEachChildName( p, pPin, pItem, pName )
        return atof(Scl_LibertyReadString(p, pItem->Head));
    return 0;
}
Scl_Item_t * Scl_LibertyReadPinTiming( Scl_Tree_t * p, Scl_Item_t * pPinOut, char * pNameIn )
{
    Scl_Item_t * pTiming, * pPinIn;
    Scl_ItemForEachChildName( p, pPinOut, pTiming, "timing" )
        Scl_ItemForEachChildName( p, pTiming, pPinIn, "related_pin" )
            if ( !strcmp(Scl_LibertyReadString(p, pPinIn->Head), pNameIn) )
                return pTiming;
    return NULL;
}
989 990 991 992 993 994 995 996 997 998 999
Vec_Ptr_t * Scl_LibertyReadPinTimingAll( Scl_Tree_t * p, Scl_Item_t * pPinOut, char * pNameIn )
{
    Vec_Ptr_t * vTimings;
    Scl_Item_t * pTiming, * pPinIn;
    vTimings = Vec_PtrAlloc( 16 );
    Scl_ItemForEachChildName( p, pPinOut, pTiming, "timing" )
        Scl_ItemForEachChildName( p, pTiming, pPinIn, "related_pin" )
            if ( !strcmp(Scl_LibertyReadString(p, pPinIn->Head), pNameIn) )
                Vec_PtrPush( vTimings, pTiming );
    return vTimings;
}
1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023
int Scl_LibertyReadTimingSense( Scl_Tree_t * p, Scl_Item_t * pPin )
{
    Scl_Item_t * pItem;
    Scl_ItemForEachChildName( p, pPin, pItem, "timing_sense" )
    {
        char * pToken = Scl_LibertyReadString(p, pItem->Head);
        if ( !strcmp(pToken, "positive_unate") )
            return sc_ts_Pos;
        if ( !strcmp(pToken, "negative_unate") )
            return sc_ts_Neg;
        if ( !strcmp(pToken, "non_unate") )
            return sc_ts_Non;
        break;
    }
    return sc_ts_Non;
}
Vec_Flt_t * Scl_LibertyReadFloatVec( char * pName )
{
    char * pToken;
    Vec_Flt_t * vValues = Vec_FltAlloc( 100 );
    for ( pToken = strtok(pName, " \t\n\r\\\","); pToken; pToken = strtok(NULL, " \t\n\r\\\",") )
        Vec_FltPush( vValues, atof(pToken) );
    return vValues;
}
1024 1025

void Scl_LibertyDumpTables( Vec_Str_t * vOut, Vec_Flt_t * vInd1, Vec_Flt_t * vInd2, Vec_Flt_t * vValues )
1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046
{
    int i; float Entry;
    // write entries
    Vec_StrPutI_( vOut, Vec_FltSize(vInd1) );
    Vec_FltForEachEntry( vInd1, Entry, i )
        Vec_StrPutF_( vOut, Entry );
    Vec_StrPut_( vOut );
    // write entries
    Vec_StrPutI_( vOut, Vec_FltSize(vInd2) );
    Vec_FltForEachEntry( vInd2, Entry, i )
        Vec_StrPutF_( vOut, Entry );
    Vec_StrPut_( vOut );
    Vec_StrPut_( vOut );
    // write entries
    assert( Vec_FltSize(vInd1) * Vec_FltSize(vInd2) == Vec_FltSize(vValues) );
    Vec_FltForEachEntry( vValues, Entry, i )
    {
        Vec_StrPutF_( vOut, Entry );
        if ( i % Vec_FltSize(vInd2) == Vec_FltSize(vInd2)-1 )
            Vec_StrPut_( vOut );
    }
1047
    // dump approximations
1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096
    Vec_StrPut_( vOut );
    for ( i = 0; i < 3; i++ ) 
        Vec_StrPutF_( vOut, 0 );
    for ( i = 0; i < 4; i++ ) 
        Vec_StrPutF_( vOut, 0 );
    for ( i = 0; i < 6; i++ ) 
        Vec_StrPutF_( vOut, 0 );
    Vec_StrPut_( vOut );
    Vec_StrPut_( vOut );
}
int Scl_LibertyScanTable( Scl_Tree_t * p, Vec_Ptr_t * vOut, Scl_Item_t * pTiming, char * pName, Vec_Ptr_t * vTemples )
{
    Vec_Flt_t * vIndex1 = NULL;
    Vec_Flt_t * vIndex2 = NULL;
    Vec_Flt_t * vValues = NULL;
    Vec_Flt_t * vInd1, * vInd2;
    Scl_Item_t * pItem, * pTable = NULL;
    char * pThis, * pTempl = NULL;
    int iPlace, i;
    float Entry;
    // find the table
    Scl_ItemForEachChildName( p, pTiming, pTable, pName )
        break;
    if ( pTable == NULL )
        return 0;
    // find the template
    pTempl = Scl_LibertyReadString(p, pTable->Head);
    if ( pTempl == NULL || pTempl[0] == 0 )
    {
        // read the numbers
        Scl_ItemForEachChild( p, pTable, pItem )
        {
            if ( !Scl_LibertyCompare(p, pItem->Key, "index_1") )
                assert(vIndex1 == NULL), vIndex1 = Scl_LibertyReadFloatVec( Scl_LibertyReadString(p, pItem->Head) );
            else if ( !Scl_LibertyCompare(p, pItem->Key, "index_2") )
                assert(vIndex2 == NULL), vIndex2 = Scl_LibertyReadFloatVec( Scl_LibertyReadString(p, pItem->Head) );
            else if ( !Scl_LibertyCompare(p, pItem->Key, "values") )
                assert(vValues == NULL), vValues = Scl_LibertyReadFloatVec( Scl_LibertyReadString(p, pItem->Head) );
        }
        if ( vIndex1 == NULL || vIndex2 == NULL || vValues == NULL )
            { printf( "Incomplete table specification\n" ); return 0; }
        // dump the table
        vInd1 = vIndex1;
        vInd2 = vIndex2;
        // write entries
        Vec_PtrPush( vOut, vInd1 );
        Vec_PtrPush( vOut, vInd2 );
        Vec_PtrPush( vOut, vValues );
    }
1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113
    else if ( !strcmp(pTempl, "scalar") )
    {
        Scl_ItemForEachChild( p, pTable, pItem )
            if ( !Scl_LibertyCompare(p, pItem->Key, "values") )
            {
                assert(vValues == NULL);
                vValues = Scl_LibertyReadFloatVec( Scl_LibertyReadString(p, pItem->Head) );
                assert( Vec_FltSize(vValues) == 1 );
                // write entries
                Vec_PtrPush( vOut, Vec_IntStart(1) );
                Vec_PtrPush( vOut, Vec_IntStart(1) );
                Vec_PtrPush( vOut, vValues );
                break;
            }
            else
            { printf( "Cannot read \"scalar\" template\n" ); return 0; }
    }
1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145
    else
    {
        // fetch the template
        iPlace = -1;
        Vec_PtrForEachEntry( char *, vTemples, pThis, i )
            if ( i % 4 == 0 && !strcmp(pTempl, pThis) )
            {  
                iPlace = i;
                break;
            }
        if ( iPlace == -1 )
            { printf( "Template cannot be found in the template library\n" ); return 0; }
        // read the numbers
        Scl_ItemForEachChild( p, pTable, pItem )
        {
            if ( !Scl_LibertyCompare(p, pItem->Key, "index_1") )
                assert(vIndex1 == NULL), vIndex1 = Scl_LibertyReadFloatVec( Scl_LibertyReadString(p, pItem->Head) );
            else if ( !Scl_LibertyCompare(p, pItem->Key, "index_2") )
                assert(vIndex2 == NULL), vIndex2 = Scl_LibertyReadFloatVec( Scl_LibertyReadString(p, pItem->Head) );
            else if ( !Scl_LibertyCompare(p, pItem->Key, "values") )
                assert(vValues == NULL), vValues = Scl_LibertyReadFloatVec( Scl_LibertyReadString(p, pItem->Head) );
        }
        // check the template style
        vInd1 = (Vec_Flt_t *)Vec_PtrEntry( vTemples, iPlace + 2 ); // slew
        vInd2 = (Vec_Flt_t *)Vec_PtrEntry( vTemples, iPlace + 3 ); // load
        if ( Vec_PtrEntry(vTemples, iPlace + 1) == NULL ) // normal order (vIndex1 is slew; vIndex2 is load)
        {
            assert( !vIndex1 || Vec_FltSize(vIndex1) == Vec_FltSize(vInd1) );
            assert( !vIndex2 || Vec_FltSize(vIndex2) == Vec_FltSize(vInd2) );
            vInd1 = vIndex1 ? vIndex1 : vInd1;
            vInd2 = vIndex2 ? vIndex2 : vInd2;
            // write entries
1146 1147 1148
            Vec_PtrPush( vOut, Vec_FltDup(vInd1) );
            Vec_PtrPush( vOut, Vec_FltDup(vInd2) );
            Vec_PtrPush( vOut, Vec_FltDup(vValues) );
1149 1150 1151
        }
        else  // reverse order (vIndex2 is slew; vIndex1 is load)
        {
1152
            Vec_Flt_t * vValues2 = Vec_FltAlloc( Vec_FltSize(vValues) );
1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163
            assert( !vIndex2 || Vec_FltSize(vIndex2) == Vec_FltSize(vInd1) );
            assert( !vIndex1 || Vec_FltSize(vIndex1) == Vec_FltSize(vInd2) );
            vInd1 = vIndex2 ? vIndex2 : vInd1;
            vInd2 = vIndex1 ? vIndex1 : vInd2;
            // write entries -- transpose
            assert( Vec_FltSize(vInd1) * Vec_FltSize(vInd2) == Vec_FltSize(vValues) );
            Vec_FltForEachEntry( vValues, Entry, i )
            {
                int x = i % Vec_FltSize(vInd2);
                int y = i / Vec_FltSize(vInd2);
                Entry = Vec_FltEntry( vValues, x * Vec_FltSize(vInd1) + y );
1164
                Vec_FltPush( vValues2, Entry );
1165
            }
1166 1167 1168 1169 1170
            assert( Vec_FltSize(vValues) == Vec_FltSize(vValues2) );
            // write entries
            Vec_PtrPush( vOut, Vec_FltDup(vInd1) );
            Vec_PtrPush( vOut, Vec_FltDup(vInd2) );
            Vec_PtrPush( vOut, vValues2 );
1171
        }
1172 1173 1174
        Vec_FltFreeP( &vIndex1 );
        Vec_FltFreeP( &vIndex2 );
        Vec_FltFreeP( &vValues );
1175 1176 1177
    }
    return 1;
}
1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202
int Scl_LibertyComputeWorstCase( Vec_Ptr_t * vTables, Vec_Flt_t ** pvInd0, Vec_Flt_t ** pvInd1, Vec_Flt_t ** pvValues )
{
    Vec_Flt_t * vInd0, * vInd1, * vValues;
    Vec_Flt_t * vind0, * vind1, * vvalues;
    int i, k, nTriples = Vec_PtrSize(vTables) / 3;
    float Entry;
    assert( Vec_PtrSize(vTables) > 0 && Vec_PtrSize(vTables) % 3 == 0 );
    if ( nTriples == 1 )
    {
        *pvInd0   = (Vec_Flt_t *)Vec_PtrEntry(vTables, 0);
        *pvInd1   = (Vec_Flt_t *)Vec_PtrEntry(vTables, 1);
        *pvValues = (Vec_Flt_t *)Vec_PtrEntry(vTables, 2);
        Vec_PtrShrink( vTables, 0 );
        return 1;
    }
    vInd0   = Vec_FltDup( (Vec_Flt_t *)Vec_PtrEntry(vTables, 0) );
    vInd1   = Vec_FltDup( (Vec_Flt_t *)Vec_PtrEntry(vTables, 1) );
    vValues = Vec_FltDup( (Vec_Flt_t *)Vec_PtrEntry(vTables, 2) );
    for ( i = 1; i < nTriples; i++ )
    {
        vind0   = (Vec_Flt_t *)Vec_PtrEntry(vTables, i*3+0);
        vind1   = (Vec_Flt_t *)Vec_PtrEntry(vTables, i*3+1);
        vvalues = (Vec_Flt_t *)Vec_PtrEntry(vTables, i*3+2);
        // check equality of indexes
        if ( !Vec_FltEqual(vind0, vInd0) )
1203
            continue;//return 0;
1204
        if ( !Vec_FltEqual(vind1, vInd1) )
1205
            continue;//return 0;
1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220
//        Vec_FltForEachEntry( vvalues, Entry, k )
//            Vec_FltAddToEntry( vValues, k, Entry );
        Vec_FltForEachEntry( vvalues, Entry, k )
            if ( Vec_FltEntry(vValues, k) < Entry )
                Vec_FltWriteEntry( vValues, k, Entry );
    }
//    Vec_FltForEachEntry( vValues, Entry, k )
//        Vec_FltWriteEntry( vValues, k, Entry/nTriples );
    // return the result
    *pvInd0 = vInd0;
    *pvInd1 = vInd1;
    *pvValues = vValues;
    return 1;
}

1221
int Scl_LibertyReadTable( Scl_Tree_t * p, Vec_Str_t * vOut, Scl_Item_t * pTiming, char * pName, Vec_Ptr_t * vTemples )
1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234
{
    Vec_Flt_t * vIndex1 = NULL;
    Vec_Flt_t * vIndex2 = NULL;
    Vec_Flt_t * vValues = NULL;
    Vec_Flt_t * vInd1, * vInd2;
    Scl_Item_t * pItem, * pTable = NULL;
    char * pThis, * pTempl = NULL;
    int iPlace, i;
    float Entry;
    // find the table
    Scl_ItemForEachChildName( p, pTiming, pTable, pName )
        break;
    if ( pTable == NULL )
1235
        return 0;
1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250
    // find the template
    pTempl = Scl_LibertyReadString(p, pTable->Head);
    if ( pTempl == NULL || pTempl[0] == 0 )
    {
        // read the numbers
        Scl_ItemForEachChild( p, pTable, pItem )
        {
            if ( !Scl_LibertyCompare(p, pItem->Key, "index_1") )
                assert(vIndex1 == NULL), vIndex1 = Scl_LibertyReadFloatVec( Scl_LibertyReadString(p, pItem->Head) );
            else if ( !Scl_LibertyCompare(p, pItem->Key, "index_2") )
                assert(vIndex2 == NULL), vIndex2 = Scl_LibertyReadFloatVec( Scl_LibertyReadString(p, pItem->Head) );
            else if ( !Scl_LibertyCompare(p, pItem->Key, "values") )
                assert(vValues == NULL), vValues = Scl_LibertyReadFloatVec( Scl_LibertyReadString(p, pItem->Head) );
        }
        if ( vIndex1 == NULL || vIndex2 == NULL || vValues == NULL )
1251
            { printf( "Incomplete table specification\n" ); return 0; }
1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285
        // dump the table
        vInd1 = vIndex1;
        vInd2 = vIndex2;
        // write entries
        Vec_StrPutI_( vOut, Vec_FltSize(vInd1) );
        Vec_FltForEachEntry( vInd1, Entry, i )
            Vec_StrPutF_( vOut, Entry );
        Vec_StrPut_( vOut );
        // write entries
        Vec_StrPutI_( vOut, Vec_FltSize(vInd2) );
        Vec_FltForEachEntry( vInd2, Entry, i )
            Vec_StrPutF_( vOut, Entry );
        Vec_StrPut_( vOut );
        Vec_StrPut_( vOut );
        // write entries
        assert( Vec_FltSize(vInd1) * Vec_FltSize(vInd2) == Vec_FltSize(vValues) );
        Vec_FltForEachEntry( vValues, Entry, i )
        {
            Vec_StrPutF_( vOut, Entry );
            if ( i % Vec_FltSize(vInd2) == Vec_FltSize(vInd2)-1 )
                Vec_StrPut_( vOut );
        }
    }
    else
    {
        // fetch the template
        iPlace = -1;
        Vec_PtrForEachEntry( char *, vTemples, pThis, i )
            if ( i % 4 == 0 && !strcmp(pTempl, pThis) )
            {  
                iPlace = i;
                break;
            }
        if ( iPlace == -1 )
1286
            { printf( "Template cannot be found in the template library\n" ); return 0; }
1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367
        // read the numbers
        Scl_ItemForEachChild( p, pTable, pItem )
        {
            if ( !Scl_LibertyCompare(p, pItem->Key, "index_1") )
                assert(vIndex1 == NULL), vIndex1 = Scl_LibertyReadFloatVec( Scl_LibertyReadString(p, pItem->Head) );
            else if ( !Scl_LibertyCompare(p, pItem->Key, "index_2") )
                assert(vIndex2 == NULL), vIndex2 = Scl_LibertyReadFloatVec( Scl_LibertyReadString(p, pItem->Head) );
            else if ( !Scl_LibertyCompare(p, pItem->Key, "values") )
                assert(vValues == NULL), vValues = Scl_LibertyReadFloatVec( Scl_LibertyReadString(p, pItem->Head) );
        }
        // check the template style
        vInd1 = (Vec_Flt_t *)Vec_PtrEntry( vTemples, iPlace + 2 ); // slew
        vInd2 = (Vec_Flt_t *)Vec_PtrEntry( vTemples, iPlace + 3 ); // load
        if ( Vec_PtrEntry(vTemples, iPlace + 1) == NULL ) // normal order (vIndex1 is slew; vIndex2 is load)
        {
            assert( !vIndex1 || Vec_FltSize(vIndex1) == Vec_FltSize(vInd1) );
            assert( !vIndex2 || Vec_FltSize(vIndex2) == Vec_FltSize(vInd2) );
            vInd1 = vIndex1 ? vIndex1 : vInd1;
            vInd2 = vIndex2 ? vIndex2 : vInd2;
            // write entries
            Vec_StrPutI_( vOut, Vec_FltSize(vInd1) );
            Vec_FltForEachEntry( vInd1, Entry, i )
                Vec_StrPutF_( vOut, Entry );
            Vec_StrPut_( vOut );
            // write entries
            Vec_StrPutI_( vOut, Vec_FltSize(vInd2) );
            Vec_FltForEachEntry( vInd2, Entry, i )
                Vec_StrPutF_( vOut, Entry );
            Vec_StrPut_( vOut );
            Vec_StrPut_( vOut );
            // write entries
            assert( Vec_FltSize(vInd1) * Vec_FltSize(vInd2) == Vec_FltSize(vValues) );
            Vec_FltForEachEntry( vValues, Entry, i )
            {
                Vec_StrPutF_( vOut, Entry );
                if ( i % Vec_FltSize(vInd2) == Vec_FltSize(vInd2)-1 )
                    Vec_StrPut_( vOut );
            }
        }
        else  // reverse order (vIndex2 is slew; vIndex1 is load)
        {
            assert( !vIndex2 || Vec_FltSize(vIndex2) == Vec_FltSize(vInd1) );
            assert( !vIndex1 || Vec_FltSize(vIndex1) == Vec_FltSize(vInd2) );
            vInd1 = vIndex2 ? vIndex2 : vInd1;
            vInd2 = vIndex1 ? vIndex1 : vInd2;
            // write entries
            Vec_StrPutI_( vOut, Vec_FltSize(vInd1) );
            Vec_FltForEachEntry( vInd1, Entry, i )
                Vec_StrPutF_( vOut, Entry );
            Vec_StrPut_( vOut );
            // write entries
            Vec_StrPutI_( vOut, Vec_FltSize(vInd2) );
            Vec_FltForEachEntry( vInd2, Entry, i )
                Vec_StrPutF_( vOut, Entry );
            Vec_StrPut_( vOut );
            Vec_StrPut_( vOut );
            // write entries -- transpose
            assert( Vec_FltSize(vInd1) * Vec_FltSize(vInd2) == Vec_FltSize(vValues) );
            Vec_FltForEachEntry( vValues, Entry, i )
            {
                int x = i % Vec_FltSize(vInd2);
                int y = i / Vec_FltSize(vInd2);
                Entry = Vec_FltEntry( vValues, x * Vec_FltSize(vInd1) + y );
                Vec_StrPutF_( vOut, Entry );
                if ( i % Vec_FltSize(vInd2) == Vec_FltSize(vInd2)-1 )
                    Vec_StrPut_( vOut );
            }
        }
    }
    Vec_StrPut_( vOut );
    for ( i = 0; i < 3; i++ ) 
        Vec_StrPutF_( vOut, 0 );
    for ( i = 0; i < 4; i++ ) 
        Vec_StrPutF_( vOut, 0 );
    for ( i = 0; i < 6; i++ ) 
        Vec_StrPutF_( vOut, 0 );
    Vec_FltFreeP( &vIndex1 );
    Vec_FltFreeP( &vIndex2 );
    Vec_FltFreeP( &vValues );
    Vec_StrPut_( vOut );
    Vec_StrPut_( vOut );
1368
    return 1;
1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440
}
void Scl_LibertyPrintTemplates( Vec_Ptr_t * vRes )
{
    Vec_Flt_t * vArray; int i;
    assert( Vec_PtrSize(vRes) % 4 == 0 );
    printf( "There are %d slew/load templates\n", Vec_PtrSize(vRes) % 4 );
    Vec_PtrForEachEntry( Vec_Flt_t *, vRes, vArray, i )
    {
        if ( i % 4 == 0 )
            printf( "%s\n", (char *)vArray );
        else if ( i % 4 == 1 )
            printf( "%d\n", (int)(vArray != NULL) );
        else if ( i % 4 == 2 || i % 4 == 3 )
            Vec_FltPrint( vArray );
        if ( i % 4 == 3 )
            printf( "\n" );
    }
}
Vec_Ptr_t * Scl_LibertyReadTemplates( Scl_Tree_t * p )
{
    Vec_Ptr_t * vRes = NULL;
    Vec_Flt_t * vIndex1, * vIndex2;
    Scl_Item_t * pTempl, * pItem;
    char * pVar1, * pVar2;
    int fFlag0, fFlag1;
    vRes = Vec_PtrAlloc( 100 );
    Scl_ItemForEachChildName( p, Scl_LibertyRoot(p), pTempl, "lu_table_template" )
    {
        pVar1 = pVar2 = NULL;
        vIndex1 = vIndex2 = NULL;
        Scl_ItemForEachChild( p, pTempl, pItem )
        {
            if ( !Scl_LibertyCompare(p, pItem->Key, "index_1") )
                assert(vIndex1 == NULL), vIndex1 = Scl_LibertyReadFloatVec( Scl_LibertyReadString(p, pItem->Head) );
            else if ( !Scl_LibertyCompare(p, pItem->Key, "index_2") )
                assert(vIndex2 == NULL), vIndex2 = Scl_LibertyReadFloatVec( Scl_LibertyReadString(p, pItem->Head) );
            else if ( !Scl_LibertyCompare(p, pItem->Key, "variable_1") )
                assert(pVar1 == NULL), pVar1 = Abc_UtilStrsav( Scl_LibertyReadString(p, pItem->Head) );
            else if ( !Scl_LibertyCompare(p, pItem->Key, "variable_2") )
                assert(pVar2 == NULL), pVar2 = Abc_UtilStrsav( Scl_LibertyReadString(p, pItem->Head) );
        }
        if ( pVar1 == NULL || pVar2 == NULL )
        {
            ABC_FREE( pVar1 );  
            ABC_FREE( pVar2 );
            Vec_FltFreeP( &vIndex1 );
            Vec_FltFreeP( &vIndex2 );
            continue;
        }
        assert( pVar1 != NULL && pVar2 != NULL );
        fFlag0 = (!strcmp(pVar1, "input_net_transition") && !strcmp(pVar2, "total_output_net_capacitance"));
        fFlag1 = (!strcmp(pVar2, "input_net_transition") && !strcmp(pVar1, "total_output_net_capacitance"));
        ABC_FREE( pVar1 );  
        ABC_FREE( pVar2 );
        if ( !fFlag0 && !fFlag1 )
        {
            Vec_FltFreeP( &vIndex1 );
            Vec_FltFreeP( &vIndex2 );
            continue;
        }
        Vec_PtrPush( vRes, Abc_UtilStrsav( Scl_LibertyReadString(p, pTempl->Head) ) );
        Vec_PtrPush( vRes, fFlag0 ? NULL : (void *)(ABC_PTRINT_T)1 );
        Vec_PtrPush( vRes, fFlag0 ? vIndex1 : vIndex2 );
        Vec_PtrPush( vRes, fFlag0 ? vIndex2 : vIndex1 );
    }
    if ( Vec_PtrSize(vRes) == 0 )
        Abc_Print( 0, "Templates are not defined.\n" );
    // print templates
//    printf( "Found %d templates\n", Vec_PtrSize(vRes)/4 );
//    Scl_LibertyPrintTemplates( vRes );
    return vRes;
}
1441
Vec_Str_t * Scl_LibertyReadSclStr( Scl_Tree_t * p, int fVerbose, int fVeryVerbose, SC_DontUse dont_use )
1442
{
1443
    int fUseFirstTable = 0;
1444
    Vec_Str_t * vOut;
1445
    Vec_Ptr_t * vNameIns, * vTemples = NULL;
1446 1447 1448
    Scl_Item_t * pCell, * pPin, * pTiming;
    Vec_Wrd_t * vTruth;
    char * pFormula, * pName;
1449
    int i, k, Counter, nOutputs, nCells;
1450
    int nSkipped[4] = {0};
1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483

    // read delay-table templates
    vTemples = Scl_LibertyReadTemplates( p );

    // start the library
    vOut = Vec_StrAlloc( 10000 );
    Vec_StrPutI_( vOut, ABC_SCL_CUR_VERSION );

    // top level information
    Vec_StrPut_( vOut );
    Vec_StrPutS_( vOut, Scl_LibertyReadString(p, Scl_LibertyRoot(p)->Head) );
    Vec_StrPutS_( vOut, Scl_LibertyReadDefaultWireLoad(p) );
    Vec_StrPutS_( vOut, Scl_LibertyReadDefaultWireLoadSel(p) );
    Vec_StrPutF_( vOut, Scl_LibertyReadDefaultMaxTrans(p) );
    Vec_StrPutI_( vOut, Scl_LibertyReadTimeUnit(p) );
    Scl_LibertyReadLoadUnit( p, vOut );
    Vec_StrPut_( vOut );
    Vec_StrPut_( vOut );

    // read wire loads
    Scl_LibertyReadWireLoad( p, vOut );
    Scl_LibertyReadWireLoadSelect( p, vOut );

    // count cells
    nCells = 0;
    Scl_ItemForEachChildName( p, Scl_LibertyRoot(p), pCell, "cell" )
    {
        if ( Scl_LibertyReadCellIsFlop(p, pCell) )
        {
            if ( fVeryVerbose )  printf( "Scl_LibertyReadGenlib() skipped sequential cell \"%s\".\n", Scl_LibertyReadString(p, pCell->Head) );
            nSkipped[0]++;
            continue;
        }
1484
        if ( Scl_LibertyReadCellIsDontUse(p, pCell, dont_use) )
1485 1486 1487 1488 1489
        {
            if ( fVeryVerbose )  printf( "Scl_LibertyReadGenlib() skipped cell \"%s\" due to dont_use attribute.\n", Scl_LibertyReadString(p, pCell->Head) );
            nSkipped[3]++;
            continue;
        }
1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511
        if ( Scl_LibertyReadCellIsThreeState(p, pCell) )
        {
            if ( fVeryVerbose )  printf( "Scl_LibertyReadGenlib() skipped three-state cell \"%s\".\n", Scl_LibertyReadString(p, pCell->Head) );
            nSkipped[1]++;
            continue;
        }
        if ( (Counter = Scl_LibertyReadCellOutputNum(p, pCell)) == 0 )
        {
            if ( fVeryVerbose )  printf( "Scl_LibertyReadGenlib() skipped cell \"%s\" without logic function.\n", Scl_LibertyReadString(p, pCell->Head) );
            nSkipped[2]++;
            continue;
        }
        nCells++;
    }
    // read cells
    Vec_StrPutI_( vOut, nCells );
    Vec_StrPut_( vOut );
    Vec_StrPut_( vOut );
    Scl_ItemForEachChildName( p, Scl_LibertyRoot(p), pCell, "cell" )
    {
        if ( Scl_LibertyReadCellIsFlop(p, pCell) )
            continue;
1512
        if ( Scl_LibertyReadCellIsDontUse(p, pCell, dont_use) )
1513
            continue;
1514 1515 1516 1517 1518 1519
        if ( Scl_LibertyReadCellIsThreeState(p, pCell) )
            continue;
        if ( (Counter = Scl_LibertyReadCellOutputNum(p, pCell)) == 0 )
            continue;
        // top level information
        Vec_StrPutS_( vOut, Scl_LibertyReadString(p, pCell->Head) );
1520 1521
        pName = Scl_LibertyReadCellArea(p, pCell);
        Vec_StrPutF_( vOut, pName ? atof(pName) : 1 );
1522 1523
        pName = Scl_LibertyReadCellLeakage(p, pCell);
        Vec_StrPutF_( vOut, pName ? atof(pName) : 0 );
1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538
        Vec_StrPutI_( vOut, Scl_LibertyReadDeriveStrength(p, pCell) );
        // pin count
        nOutputs = Scl_LibertyReadCellOutputNum( p, pCell );
        Vec_StrPutI_( vOut, Scl_LibertyItemNum(p, pCell, "pin") - nOutputs );
        Vec_StrPutI_( vOut, nOutputs );
        Vec_StrPut_( vOut );
        Vec_StrPut_( vOut );

        // input pins
        vNameIns = Vec_PtrAlloc( 16 );
        Scl_ItemForEachChildName( p, pCell, pPin, "pin" )
        {
            float CapOne, CapRise, CapFall;
            if ( Scl_LibertyReadPinFormula(p, pPin) != NULL ) // skip output pin
                continue;
1539
            assert( Scl_LibertyReadPinDirection(p, pPin) == 0 || Scl_LibertyReadPinDirection(p, pPin) == 2);
1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559
            pName = Scl_LibertyReadString(p, pPin->Head);
            Vec_PtrPush( vNameIns, Abc_UtilStrsav(pName) );
            Vec_StrPutS_( vOut, pName );
            CapOne  = Scl_LibertyReadPinCap( p, pPin, "capacitance" );
            CapRise = Scl_LibertyReadPinCap( p, pPin, "rise_capacitance" );
            CapFall = Scl_LibertyReadPinCap( p, pPin, "fall_capacitance" );
            if ( CapRise == 0 )
                CapRise = CapOne;
            if ( CapFall == 0 )
                CapFall = CapOne;
            Vec_StrPutF_( vOut, CapRise );
            Vec_StrPutF_( vOut, CapFall );
            Vec_StrPut_( vOut );
        }
        Vec_StrPut_( vOut );
        // output pins
        Scl_ItemForEachChildName( p, pCell, pPin, "pin" )
        {
            if ( !Scl_LibertyReadPinFormula(p, pPin) ) // skip input pin
                continue;
1560 1561
            if (Scl_LibertyReadPinDirection(p, pPin) == 2) // skip internal pin
                continue;
1562 1563 1564 1565 1566 1567 1568 1569 1570 1571
            assert( Scl_LibertyReadPinDirection(p, pPin) == 1 );
            pName = Scl_LibertyReadString(p, pPin->Head);
            Vec_StrPutS_( vOut, pName );
            Vec_StrPutF_( vOut, Scl_LibertyReadPinCap( p, pPin, "max_capacitance" ) );
            Vec_StrPutF_( vOut, Scl_LibertyReadPinCap( p, pPin, "max_transition" ) );
            Vec_StrPutI_( vOut, Vec_PtrSize(vNameIns) );
            pFormula = Scl_LibertyReadPinFormula(p, pPin);
            Vec_StrPutS_( vOut, pFormula );
            // write truth table
            vTruth = Mio_ParseFormulaTruth( pFormula, (char **)Vec_PtrArray(vNameIns), Vec_PtrSize(vNameIns) );
1572 1573
            if ( vTruth == NULL )
                return NULL;
1574 1575 1576 1577 1578 1579 1580
            for ( i = 0; i < Abc_Truth6WordNum(Vec_PtrSize(vNameIns)); i++ )
                Vec_StrPutW_( vOut, Vec_WrdEntry(vTruth, i) );
            Vec_WrdFree( vTruth );
            Vec_StrPut_( vOut );
            Vec_StrPut_( vOut );

            // write the delay tables
1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610
            if ( fUseFirstTable )
            {
                Vec_PtrForEachEntry( char *, vNameIns, pName, i )
                {
                    pTiming = Scl_LibertyReadPinTiming( p, pPin, pName );
                    Vec_StrPutS_( vOut, pName );
                    Vec_StrPutI_( vOut, (int)(pTiming != NULL) );
                    if ( pTiming == NULL ) // output does not depend on input
                        continue;
                    Vec_StrPutI_( vOut, Scl_LibertyReadTimingSense(p, pTiming) );
                    Vec_StrPut_( vOut );
                    Vec_StrPut_( vOut );
                    // some cells only have 'rise' or 'fall' but not both - here we work around this
                    if ( !Scl_LibertyReadTable( p, vOut, pTiming, "cell_rise",           vTemples ) )
                        if ( !Scl_LibertyReadTable( p, vOut, pTiming, "cell_fall",       vTemples ) )
                                { printf( "Table cannot be found\n" ); return NULL; }                              
                    if ( !Scl_LibertyReadTable( p, vOut, pTiming, "cell_fall",           vTemples ) )
                        if ( !Scl_LibertyReadTable( p, vOut, pTiming, "cell_rise",       vTemples ) )
                                { printf( "Table cannot be found\n" ); return NULL; }                              
                    if ( !Scl_LibertyReadTable( p, vOut, pTiming, "rise_transition",     vTemples ) )
                        if ( !Scl_LibertyReadTable( p, vOut, pTiming, "fall_transition", vTemples ) )
                                { printf( "Table cannot be found\n" ); return NULL; }                              
                    if ( !Scl_LibertyReadTable( p, vOut, pTiming, "fall_transition",     vTemples ) )
                        if ( !Scl_LibertyReadTable( p, vOut, pTiming, "rise_transition", vTemples ) )
                                { printf( "Table cannot be found\n" ); return NULL; }  
                }
                continue;
            }

            // write the timing tables
1611 1612
            Vec_PtrForEachEntry( char *, vNameIns, pName, i )
            {
1613 1614
                Vec_Ptr_t * vTables[4];
                Vec_Ptr_t * vTimings;
1615
                vTimings = Scl_LibertyReadPinTimingAll( p, pPin, pName );
1616
                Vec_StrPutS_( vOut, pName );
1617 1618 1619 1620
                Vec_StrPutI_( vOut, (int)(Vec_PtrSize(vTimings) != 0) );
                if ( Vec_PtrSize(vTimings) == 0 ) // output does not depend on input
                {
                    Vec_PtrFree( vTimings );
1621
                    continue;
1622 1623
                }
                Vec_StrPutI_( vOut, Scl_LibertyReadTimingSense(p, (Scl_Item_t *)Vec_PtrEntry(vTimings, 0)) );
1624 1625
                Vec_StrPut_( vOut );
                Vec_StrPut_( vOut );
1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657
                // collect the timing tables
                for ( k = 0; k < 4; k++ )
                    vTables[k] = Vec_PtrAlloc( 16 );
                Vec_PtrForEachEntry( Scl_Item_t *, vTimings, pTiming, k )
                {
                    // some cells only have 'rise' or 'fall' but not both - here we work around this
                    if ( !Scl_LibertyScanTable( p, vTables[0], pTiming, "cell_rise",           vTemples ) )
                        if ( !Scl_LibertyScanTable( p, vTables[0], pTiming, "cell_fall",       vTemples ) )
                                { printf( "Table cannot be found\n" ); return NULL; }                              
                    if ( !Scl_LibertyScanTable( p, vTables[1], pTiming, "cell_fall",           vTemples ) )
                        if ( !Scl_LibertyScanTable( p, vTables[1], pTiming, "cell_rise",       vTemples ) )
                                { printf( "Table cannot be found\n" ); return NULL; }                              
                    if ( !Scl_LibertyScanTable( p, vTables[2], pTiming, "rise_transition",     vTemples ) )
                        if ( !Scl_LibertyScanTable( p, vTables[2], pTiming, "fall_transition", vTemples ) )
                                { printf( "Table cannot be found\n" ); return NULL; }                              
                    if ( !Scl_LibertyScanTable( p, vTables[3], pTiming, "fall_transition",     vTemples ) )
                        if ( !Scl_LibertyScanTable( p, vTables[3], pTiming, "rise_transition", vTemples ) )
                                { printf( "Table cannot be found\n" ); return NULL; }  
                }
                Vec_PtrFree( vTimings );
                // compute worse case of the tables
                for ( k = 0; k < 4; k++ )
                {
                    Vec_Flt_t * vInd0, * vInd1, * vValues;
                    if ( !Scl_LibertyComputeWorstCase( vTables[k], &vInd0, &vInd1, &vValues ) )
                        { printf( "Table indexes have different values\n" ); return NULL; }  
                    Vec_VecFree( (Vec_Vec_t *)vTables[k] );
                    Scl_LibertyDumpTables( vOut, vInd0, vInd1, vValues );
                    Vec_FltFree( vInd0 );
                    Vec_FltFree( vInd1 );
                    Vec_FltFree( vValues );
                }
1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668
            }
        }
        Vec_StrPut_( vOut );
        Vec_PtrFreeFree( vNameIns );
    }
    // free templates
    if ( vTemples )
    {
        Vec_Flt_t * vArray;
        assert( Vec_PtrSize(vTemples) % 4 == 0 );
        Vec_PtrForEachEntry( Vec_Flt_t *, vTemples, vArray, i )
1669 1670 1671
        {
            if ( vArray == NULL )
                continue;
1672 1673 1674 1675
            if ( i % 4 == 0 )
                ABC_FREE( vArray );
            else if ( i % 4 == 2 || i % 4 == 3 )
                Vec_FltFree( vArray );
1676
        }
1677 1678 1679 1680 1681 1682
        Vec_PtrFree( vTemples );
    }
    if ( fVerbose )
    {
        printf( "Library \"%s\" from \"%s\" has %d cells ", 
            Scl_LibertyReadString(p, Scl_LibertyRoot(p)->Head), p->pFileName, nCells );
1683 1684
        printf( "(%d skipped: %d seq; %d tri-state; %d no func; %d dont_use).  ", 
            nSkipped[0]+nSkipped[1]+nSkipped[2], nSkipped[0], nSkipped[1], nSkipped[2], nSkipped[3] );
1685 1686 1687 1688
        Abc_PrintTime( 1, "Time", Abc_Clock() - p->clkStart );
    }
    return vOut;
}
1689
SC_Lib * Abc_SclReadLiberty( char * pFileName, int fVerbose, int fVeryVerbose, SC_DontUse dont_use )
1690 1691 1692 1693 1694 1695 1696 1697 1698
{
    SC_Lib * pLib;
    Scl_Tree_t * p;
    Vec_Str_t * vStr;
    p = Scl_LibertyParse( pFileName, fVeryVerbose );
    if ( p == NULL )
        return NULL;
//    Scl_LibertyParseDump( p, "temp_.lib" );
    // collect relevant data
1699
    vStr = Scl_LibertyReadSclStr( p, fVerbose, fVeryVerbose, dont_use );
1700
    Scl_LibertyStop( p, fVeryVerbose );
1701 1702
    if ( vStr == NULL )
        return NULL;
1703 1704
    // construct SCL data-structure
    pLib = Abc_SclReadFromStr( vStr );
1705 1706
    if ( pLib == NULL )
        return NULL;
1707
    pLib->pFileName = Abc_UtilStrsav( pFileName );
1708
    Abc_SclLibNormalize( pLib );
1709
    Vec_StrFree( vStr );
1710
//    printf( "Average slew = %.2f ps\n", Abc_SclComputeAverageSlew(pLib) );
1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736
    return pLib;
}

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

  Synopsis    [Experiments with Liberty parsing.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Scl_LibertyTest()
{
    char * pFileName = "bwrc.lib";
    int fVerbose = 1;
    int fVeryVerbose = 0;
    Scl_Tree_t * p;
    Vec_Str_t * vStr;
//    return;
    p = Scl_LibertyParse( pFileName, fVeryVerbose );
    if ( p == NULL )
        return;
//    Scl_LibertyParseDump( p, "temp_.lib" );
1737 1738
    SC_DontUse dont_use = {0};
    vStr = Scl_LibertyReadSclStr( p, fVerbose, fVeryVerbose, dont_use);
1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750
    Scl_LibertyStringDump( "test_scl.lib", vStr );
    Vec_StrFree( vStr );
    Scl_LibertyStop( p, fVerbose );
}

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


ABC_NAMESPACE_IMPL_END