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

  FileHead    [amapLiberty.c]

  SystemHead  [ABC: Logic synthesis and verification system.]

  PackageHead [Technology mapper for standard cells.]

  Synopsis    [Liberty parser.]

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

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

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

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

#include "amapInt.h"

23 24 25
ABC_NAMESPACE_IMPL_START


Alan Mishchenko committed
26 27 28 29
////////////////////////////////////////////////////////////////////////
///                        DECLARATIONS                              ///
////////////////////////////////////////////////////////////////////////

30 31
#define ABC_MAX_LIB_STR_LEN 5000

Alan Mishchenko committed
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 65 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 176 177 178 179 180 181 182 183
// entry types
typedef enum { 
    AMAP_LIBERTY_NONE = 0,        // 0:  unknown
    AMAP_LIBERTY_PROC,            // 1:  procedure :  key(head){body}
    AMAP_LIBERTY_EQUA,            // 2:  equation  :  key:head;
    AMAP_LIBERTY_LIST             // 3:  list      :  key(head) 
} Amap_LibertyType_t;

typedef struct Amap_Pair_t_ Amap_Pair_t;
struct Amap_Pair_t_
{
    int             Beg;          // item beginning
    int             End;          // item end
};

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

typedef struct Amap_Tree_t_ Amap_Tree_t;
struct Amap_Tree_t_
{
    char *          pFileName;    // input Liberty file name
    char *          pContents;    // file contents
    int             nContents;    // file size
    int             nLines;       // line counter
    int             nItems;       // number of items
    int             nItermAlloc;  // number of items allocated
    Amap_Item_t *   pItems;       // the items
    char *          pError;       // the error string
};

static inline Amap_Item_t *  Amap_LibertyRoot( Amap_Tree_t * p )                                       { return p->pItems;                                                 }
static inline Amap_Item_t *  Amap_LibertyItem( Amap_Tree_t * p, int v )                                { assert( v < p->nItems ); return v < 0 ? NULL : p->pItems + v;     }
static inline int            Amap_LibertyCompare( Amap_Tree_t * p, Amap_Pair_t Pair, char * pStr )     { return strncmp( p->pContents+Pair.Beg, pStr, Pair.End-Pair.Beg ); }
static inline void           Amap_PrintWord( FILE * pFile, Amap_Tree_t * p, Amap_Pair_t Pair )         { char * pBeg = p->pContents+Pair.Beg, * pEnd = p->pContents+Pair.End; while ( pBeg < pEnd ) fputc( *pBeg++, pFile ); }
static inline void           Amap_PrintSpace( FILE * pFile, int nOffset )                              { int i; for ( i = 0; i < nOffset; i++ ) fputc(' ', pFile);         }
static inline int            Amap_LibertyItemId( Amap_Tree_t * p, Amap_Item_t * pItem )                { return pItem - p->pItems;                                         }

#define Amap_ItemForEachChild( p, pItem, pChild ) \
    for ( pChild = Amap_LibertyItem(p, pItem->Child); pChild; pChild = Amap_LibertyItem(p, pChild->Next) )

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

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

  Synopsis    [Prints parse tree in Liberty format.]

  Description []

  SideEffects []

  SeeAlso     []

***********************************************************************/
void Amap_LibertyPrintLibertyItem( FILE * pFile, Amap_Tree_t * p, Amap_Item_t * pItem, int nOffset )
{
    if ( pItem->Type == AMAP_LIBERTY_PROC )
    {
        Amap_PrintSpace( pFile, nOffset );
        Amap_PrintWord( pFile, p, pItem->Key );
        fprintf( pFile, "(" );
        Amap_PrintWord( pFile, p, pItem->Head );
        fprintf( pFile, ") {\n" );
        if ( Amap_LibertyItem(p, pItem->Child) )
            Amap_LibertyPrintLibertyItem( pFile, p, Amap_LibertyItem(p, pItem->Child), nOffset + 1 );
        Amap_PrintSpace( pFile, nOffset );
        fprintf( pFile, "}\n" );
    }
    else if ( pItem->Type == AMAP_LIBERTY_EQUA )
    {
        Amap_PrintSpace( pFile, nOffset );
        Amap_PrintWord( pFile, p, pItem->Key );
        fprintf( pFile, " : " );
        Amap_PrintWord( pFile, p, pItem->Head );
        fprintf( pFile, ";\n" );
    }
    else if ( pItem->Type == AMAP_LIBERTY_LIST )
    {
        Amap_PrintSpace( pFile, nOffset );
        Amap_PrintWord( pFile, p, pItem->Key );
        fprintf( pFile, "(" );
        Amap_PrintWord( pFile, p, pItem->Head );
        fprintf( pFile, ");\n" );
    }
    else assert( 0 );
    if ( Amap_LibertyItem(p, pItem->Next) )
        Amap_LibertyPrintLibertyItem( pFile, p, Amap_LibertyItem(p, pItem->Next), nOffset );
}

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

  Synopsis    [Prints parse tree in Liberty format.]

  Description []

  SideEffects []

  SeeAlso     []

***********************************************************************/
int Amap_LibertyPrintLiberty( Amap_Tree_t * p, char * pFileName )
{
    FILE * pFile;
    if ( pFileName == NULL )
        pFile = stdout;
    else
    {
        pFile = fopen( pFileName, "w" );
        if ( pFile == NULL )
        {
            printf( "Amap_LibertyPrintLiberty(): The output file is unavailable (absent or open).\n" );
            return 0;
        }
    }
    Amap_LibertyPrintLibertyItem( pFile, p, Amap_LibertyRoot(p), 0 );
    if ( pFile != stdout )
        fclose( pFile );
    return 1;
}


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

  Synopsis    [Returns the time stamp.]

  Description [The file should be closed.]

  SideEffects []

  SeeAlso     []

***********************************************************************/
char * Amap_LibertyTimeStamp()
{
    static char Buffer[100];
    char * TimeStamp;
    time_t ltime;
    // get the current time
    time( &ltime );
    TimeStamp = asctime( localtime( &ltime ) );
    TimeStamp[ strlen(TimeStamp) - 1 ] = 0;
184
    assert( strlen(TimeStamp) < 100 );
Alan Mishchenko committed
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 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 231
    strcpy( Buffer, TimeStamp );
    return Buffer;
}

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

  Synopsis    [Returns cell's function.]

  Description []

  SideEffects []

  SeeAlso     []

***********************************************************************/
int Amap_LibertyCellIsFlop( Amap_Tree_t * p, Amap_Item_t * pCell )
{
    Amap_Item_t * pAttr;
    Amap_ItemForEachChild( p, pCell, pAttr )
        if ( !Amap_LibertyCompare(p, pAttr->Key, "ff") ||
             !Amap_LibertyCompare(p, pAttr->Key, "latch") )
            return 1;
    return 0;
}

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

  Synopsis    [Returns pin's function.]

  Description []

  SideEffects []

  SeeAlso     []

***********************************************************************/
Amap_Item_t * Amap_LibertyPinFunction( Amap_Tree_t * p, Amap_Item_t * pPin )
{
    Amap_Item_t * pFunc;
    Amap_ItemForEachChild( p, pPin, pFunc )
        if ( !Amap_LibertyCompare(p, pFunc->Key, "function") )
            return pFunc;
    return NULL;
}

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

232
  Synopsis    [Returns output pin(s).]
Alan Mishchenko committed
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252

  Description []

  SideEffects []

  SeeAlso     []

***********************************************************************/
Amap_Item_t * Amap_LibertyCellOutput( Amap_Tree_t * p, Amap_Item_t * pCell )
{
    Amap_Item_t * pPin;
    Amap_ItemForEachChild( p, pCell, pPin )
    {
        if ( Amap_LibertyCompare(p, pPin->Key, "pin") )
            continue;
        if ( Amap_LibertyPinFunction(p, pPin) )
            return pPin;
    }
    return NULL;
}
253 254 255 256 257 258 259 260 261 262 263 264 265 266
Vec_Ptr_t * Amap_LibertyCellOutputs( Amap_Tree_t * p, Amap_Item_t * pCell )
{
    Amap_Item_t * pPin;
    Vec_Ptr_t * vOutPins;
    vOutPins = Vec_PtrAlloc( 2 );
    Amap_ItemForEachChild( p, pCell, pPin )
    {
        if ( Amap_LibertyCompare(p, pPin->Key, "pin") )
            continue;
        if ( Amap_LibertyPinFunction(p, pPin) )
            Vec_PtrPush( vOutPins, pPin );
    }
    return vOutPins;
}
Alan Mishchenko committed
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

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

  Synopsis    [Returns cell's area.]

  Description []

  SideEffects []

  SeeAlso     []

***********************************************************************/
Amap_Item_t * Amap_LibertyCellArea( Amap_Tree_t * p, Amap_Item_t * pCell )
{
    Amap_Item_t * pArea;
    Amap_ItemForEachChild( p, pCell, pArea )
    {
        if ( Amap_LibertyCompare(p, pArea->Key, "area") )
            continue;
        return pArea;
    }
    return NULL;
}

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

  Synopsis    [Count cell's output pins (pins with a logic function).]

  Description []

  SideEffects []

  SeeAlso     []

***********************************************************************/
int Amap_LibertyCellCountOutputs( Amap_Tree_t * p, Amap_Item_t * pCell )
{
    Amap_Item_t * pPin;
    int Counter = 0;
    Amap_ItemForEachChild( p, pCell, pPin )
    {
        if ( Amap_LibertyCompare(p, pPin->Key, "pin") )
            continue;
        if ( Amap_LibertyPinFunction(p, pPin) )
            Counter++;
    }
    return Counter;
}

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

  Synopsis    [Gets the name to write.]

  Description []

  SideEffects []

  SeeAlso     []

***********************************************************************/
char * Amap_LibertyGetString( Amap_Tree_t * p, Amap_Pair_t Pair )   
{ 
329 330
    static char Buffer[ABC_MAX_LIB_STR_LEN]; 
    assert( Pair.End-Pair.Beg < ABC_MAX_LIB_STR_LEN );
Alan Mishchenko committed
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
    strncpy( Buffer, p->pContents+Pair.Beg, Pair.End-Pair.Beg ); 
    Buffer[Pair.End-Pair.Beg] = 0;
    return Buffer;
}

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

  Synopsis    [Gets the name to write.]

  Description []

  SideEffects []

  SeeAlso     []

***********************************************************************/
char * Amap_LibertyGetStringFormula( Amap_Tree_t * p, Amap_Pair_t Pair )   
{ 
349 350
    static char Buffer[ABC_MAX_LIB_STR_LEN]; 
    assert( Pair.End-Pair.Beg-2 < ABC_MAX_LIB_STR_LEN );
Alan Mishchenko committed
351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
    strncpy( Buffer, p->pContents+Pair.Beg+1, Pair.End-Pair.Beg-2 ); 
    Buffer[Pair.End-Pair.Beg-2] = 0;
    return Buffer;
}

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

  Synopsis    [Prints parse tree in Genlib format.]

  Description []

  SideEffects []

  SeeAlso     []

***********************************************************************/
367
int Amap_LibertyPrintGenlib( Amap_Tree_t * p, char * pFileName, int fVerbose )
Alan Mishchenko committed
368 369
{
    FILE * pFile;
370
    Vec_Ptr_t * vOutputs;
Alan Mishchenko committed
371
    Amap_Item_t * pCell, * pArea, * pFunc, * pPin, * pOutput;
Alan Mishchenko committed
372
    char * pForm;
373
    int i, Counter;
Alan Mishchenko committed
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
    if ( pFileName == NULL )
        pFile = stdout;
    else
    {
        pFile = fopen( pFileName, "w" );
        if ( pFile == NULL )
        {
            printf( "Amap_LibertyPrintGenlib(): The output file is unavailable (absent or open).\n" );
            return 0;
        }
    }
    fprintf( pFile, "# This Genlib file was generated by ABC on %s\n", Amap_LibertyTimeStamp() );
    fprintf( pFile, "# The standard cell library \"%s\" is from Liberty file \"%s\"\n", Amap_LibertyGetString(p, Amap_LibertyRoot(p)->Head), p->pFileName );
    fprintf( pFile, "# (To find out more about Genlib format, google for \"sis_paper.ps\")\n" );

    fprintf( pFile, "GATE  " );
    fprintf( pFile, "%16s  ", "_const0_" );
    fprintf( pFile, "%f  ",   0.0 );
    fprintf( pFile, "%s=",    "z" );
    fprintf( pFile, "%s;\n",  "CONST0" );

    fprintf( pFile, "GATE  " );
    fprintf( pFile, "%16s  ", "_const1_" );
    fprintf( pFile, "%f  ",   0.0 );
    fprintf( pFile, "%s=",    "z" );
    fprintf( pFile, "%s;\n",  "CONST1" );

    Amap_ItemForEachChild( p, Amap_LibertyRoot(p), pCell )
    {
/*
    if ( strcmp(Amap_LibertyGetString(p, pCell->Head), "HA1SVTX1") == 0 )
    {
        int s = 0;
    }
*/
        if ( Amap_LibertyCompare(p, pCell->Key, "cell") )
            continue;
        if ( Amap_LibertyCellIsFlop(p, pCell) )
        {
413 414
            if ( fVerbose )
                printf( "Amap_LibertyPrintGenlib() skipped sequential cell \"%s\".\n", Amap_LibertyGetString(p, pCell->Head) );
Alan Mishchenko committed
415 416 417 418 419
            continue;
        }
        Counter = Amap_LibertyCellCountOutputs( p, pCell );
        if ( Counter == 0 )
        {
420 421
            if ( fVerbose )
                printf( "Amap_LibertyPrintGenlib() skipped cell \"%s\" without logic function.\n", Amap_LibertyGetString(p, pCell->Head) );
Alan Mishchenko committed
422 423
            continue;
        }
424
/*
Alan Mishchenko committed
425 426
        if ( Counter > 1 )
        {
427 428
            if ( fVerbose )
                printf( "Amap_LibertyPrintGenlib() skipped multi-output cell \"%s\".\n", Amap_LibertyGetString(p, pCell->Head) );
Alan Mishchenko committed
429 430
            continue;
        }
431
*/
Alan Mishchenko committed
432 433 434
        pArea = Amap_LibertyCellArea( p, pCell );
        if ( pArea == NULL )
        {
435 436
            if ( fVerbose )
                printf( "Amap_LibertyPrintGenlib() skipped cell \"%s\" with unspecified area.\n", Amap_LibertyGetString(p, pCell->Head) );
Alan Mishchenko committed
437 438
            continue;
        }
439 440 441
//        pOutput = Amap_LibertyCellOutput( p, pCell );
        vOutputs = Amap_LibertyCellOutputs( p, pCell );
        Vec_PtrForEachEntry( Amap_Item_t *, vOutputs, pOutput, i )
Alan Mishchenko committed
442
        {
443 444 445 446 447 448 449 450 451 452 453 454 455 456
            pFunc   = Amap_LibertyPinFunction( p, pOutput );
            pForm   = Amap_LibertyGetStringFormula( p, pFunc->Head );
            if ( !strcmp(pForm, "0") || !strcmp(pForm, "1") )
            {
                if ( fVerbose )
                    printf( "Amap_LibertyPrintGenlib() skipped cell \"%s\" with constant formula \"%s\".\n", Amap_LibertyGetString(p, pCell->Head), pForm );
                continue;
            }
            fprintf( pFile, "GATE  " );
            fprintf( pFile, "%16s  ", Amap_LibertyGetString(p, pCell->Head) );
            fprintf( pFile, "%f  ",   atof(Amap_LibertyGetString(p, pArea->Head)) );
            fprintf( pFile, "%s=",    Amap_LibertyGetString(p, pOutput->Head) );
            fprintf( pFile, "%s;\n",  Amap_LibertyGetStringFormula(p, pFunc->Head) );
            Amap_ItemForEachChild( p, pCell, pPin )
457
                if ( Vec_PtrFind(vOutputs, pPin) == -1 && !Amap_LibertyCompare(p, pPin->Key, "pin") )
458
                    fprintf( pFile, "    PIN  %13s  UNKNOWN  1  999  1.00  0.00  1.00  0.00\n", Amap_LibertyGetString(p, pPin->Head) );
Alan Mishchenko committed
459
        }
460
        Vec_PtrFree( vOutputs );
Alan Mishchenko committed
461 462 463 464 465 466
    }
    if ( pFile != stdout )
        fclose( pFile );
    return 1;
}

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 500 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 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561
/**Function*************************************************************

  Synopsis    [Prints parse tree in Genlib format.]

  Description []

  SideEffects []

  SeeAlso     []

***********************************************************************/
Vec_Str_t * Amap_LibertyPrintGenlibStr( Amap_Tree_t * p, int fVerbose )
{
    Vec_Str_t * vStr;
    char Buffer[100];
    Vec_Ptr_t * vOutputs;
    Amap_Item_t * pCell, * pArea, * pFunc, * pPin, * pOutput;
    int i, Counter;
    char * pForm;

    vStr = Vec_StrAlloc( 1000 );

    Vec_StrPrintStr( vStr, "GATE          _const0_  0.000000  z=CONST0;\n" );
    Vec_StrPrintStr( vStr, "GATE          _const1_  0.000000  z=CONST1;\n" );
    Amap_ItemForEachChild( p, Amap_LibertyRoot(p), pCell )
    {
        if ( Amap_LibertyCompare(p, pCell->Key, "cell") )
            continue;
        if ( Amap_LibertyCellIsFlop(p, pCell) )
        {
            if ( fVerbose )
                printf( "Amap_LibertyPrintGenlib() skipped sequential cell \"%s\".\n", Amap_LibertyGetString(p, pCell->Head) );
            continue;
        }
        Counter = Amap_LibertyCellCountOutputs( p, pCell );
        if ( Counter == 0 )
        {
            if ( fVerbose )
                printf( "Amap_LibertyPrintGenlib() skipped cell \"%s\" without logic function.\n", Amap_LibertyGetString(p, pCell->Head) );
            continue;
        }
        pArea = Amap_LibertyCellArea( p, pCell );
        if ( pArea == NULL )
        {
            if ( fVerbose )
                printf( "Amap_LibertyPrintGenlib() skipped cell \"%s\" with unspecified area.\n", Amap_LibertyGetString(p, pCell->Head) );
            continue;
        }
        vOutputs = Amap_LibertyCellOutputs( p, pCell );
        Vec_PtrForEachEntry( Amap_Item_t *, vOutputs, pOutput, i )
        {
            pFunc   = Amap_LibertyPinFunction( p, pOutput );
            pForm   = Amap_LibertyGetStringFormula( p, pFunc->Head );
            if ( !strcmp(pForm, "0") || !strcmp(pForm, "1") )
            {
                if ( fVerbose )
                    printf( "Amap_LibertyPrintGenlib() skipped cell \"%s\" with constant formula \"%s\".\n", Amap_LibertyGetString(p, pCell->Head), pForm );
                continue;
            }
/*
            fprintf( pFile, "GATE  " );
            fprintf( pFile, "%16s  ", Amap_LibertyGetString(p, pCell->Head) );
            fprintf( pFile, "%f  ",   atof(Amap_LibertyGetString(p, pArea->Head)) );
            fprintf( pFile, "%s=",    Amap_LibertyGetString(p, pOutput->Head) );
            fprintf( pFile, "%s;\n",  Amap_LibertyGetStringFormula(p, pFunc->Head) );
            Amap_ItemForEachChild( p, pCell, pPin )
                if ( Vec_PtrFind(vOutputs, pPin) == -1 && !Amap_LibertyCompare(p, pPin->Key, "pin") )
                    fprintf( pFile, "    PIN  %13s  UNKNOWN  1  999  1.00  0.00  1.00  0.00\n", Amap_LibertyGetString(p, pPin->Head) );
*/
            Vec_StrPrintStr( vStr, "GATE " );
            Vec_StrPrintStr( vStr, Amap_LibertyGetString(p, pCell->Head) );
            Vec_StrPrintStr( vStr, " " );
            sprintf( Buffer, "%f", atof(Amap_LibertyGetString(p, pArea->Head)) );
            Vec_StrPrintStr( vStr, Buffer );
            Vec_StrPrintStr( vStr, " " );
            Vec_StrPrintStr( vStr, Amap_LibertyGetString(p, pOutput->Head) );
            Vec_StrPrintStr( vStr, "=" );
            Vec_StrPrintStr( vStr, Amap_LibertyGetStringFormula(p, pFunc->Head) );
            Vec_StrPrintStr( vStr, ";\n" );
            Amap_ItemForEachChild( p, pCell, pPin )
                if ( Vec_PtrFind(vOutputs, pPin) == -1 && !Amap_LibertyCompare(p, pPin->Key, "pin") )
                {
                    Vec_StrPrintStr( vStr, "  PIN " );
                    Vec_StrPrintStr( vStr, Amap_LibertyGetString(p, pPin->Head) );
                    Vec_StrPrintStr( vStr, " UNKNOWN  1  999  1.00  0.00  1.00  0.00\n" );
                }
        }
        Vec_PtrFree( vOutputs );
    }
    Vec_StrPrintStr( vStr, "\n.end\n" );
    Vec_StrPush( vStr, '\0' );
//    printf( "%s", Vec_StrArray(vStr) );
    return vStr;
}

Alan Mishchenko committed
562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 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 615 616 617 618 619 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 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679

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

  Synopsis    [Returns the file size.]

  Description [The file should be closed.]

  SideEffects []

  SeeAlso     []

***********************************************************************/
int Amap_LibertyFileSize( char * pFileName )
{
    FILE * pFile;
    int nFileSize;
    pFile = fopen( pFileName, "rb" );
    if ( pFile == NULL )
    {
        printf( "Amap_LibertyFileSize(): The input file is unavailable (absent or open).\n" );
        return 0;
    }
    fseek( pFile, 0, SEEK_END );  
    nFileSize = ftell( pFile ); 
    fclose( pFile );
    return nFileSize;
}

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

  Synopsis    []

  Description []

  SideEffects []

  SeeAlso     []

***********************************************************************/
void Amap_LibertyFixFileHead( char * pFileName )
{
    char * pHead;
    for ( pHead = pFileName; *pHead; pHead++ )
        if ( *pHead == '>' )
            *pHead = '\\';
}

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

  Synopsis    []

  Description []

  SideEffects []

  SeeAlso     []

***********************************************************************/
int Amap_LibertyCountItems( char * pBeg, char * pEnd )
{
    int Counter = 0;
    for ( ; pBeg < pEnd; pBeg++ )
        Counter += (*pBeg == '(' || *pBeg == ':');
    return Counter;
}

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

  Synopsis    [Removes C-style comments.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Amap_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;
        }
}

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

  Synopsis    [Returns 1 if the character is space.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline int Amap_LibertyCharIsSpace( char c )
{
    return c == ' ' || c == '\t' || c == '\r' || c == '\n' || c == '\\';
}

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

  Synopsis    [Skips spaces.]

  Description [Returns 1 if reached the end.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
680
static inline int Amap_LibertySkipSpaces( Amap_Tree_t * p, char ** ppPos, char * pEnd, int fStopAtNewLine )
Alan Mishchenko committed
681 682 683 684 685
{
    char * pPos = *ppPos;
    for ( ; pPos < pEnd; pPos++ )
    {
        if ( *pPos == '\n' )
Alan Mishchenko committed
686
        {
Alan Mishchenko committed
687
            p->nLines++;
Alan Mishchenko committed
688 689 690
            if ( fStopAtNewLine )
                break;
        }        
Alan Mishchenko committed
691 692 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
        if ( !Amap_LibertyCharIsSpace(*pPos) )
            break;
    }
    *ppPos = pPos;
    return pPos == pEnd;
}

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

  Synopsis    [Skips entry delimited by " :;(){}" ]

  Description [Returns 1 if reached the end.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline int Amap_LibertySkipEntry( char ** ppPos, char * pEnd )
{
    char * pPos = *ppPos;
    if ( *pPos == '\"' )
    {
        for ( pPos++; pPos < pEnd; pPos++ )
            if ( *pPos == '\"' )
            {
                pPos++;
                break;
            }
    }
    else
    {
        for ( ; pPos < pEnd; pPos++ )
724
            if ( *pPos == ' ' || *pPos == '\r' || *pPos == '\n' || *pPos == '\t' ||
Alan Mishchenko committed
725 726
                 *pPos == ':' || *pPos == ';'  || 
                 *pPos == '(' || *pPos == ')'  || 
Alan Mishchenko committed
727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 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 808 809 810 811 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
                 *pPos == '{' || *pPos == '}' )
                break;
    }
    *ppPos = pPos;
    return pPos == pEnd;
}

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

  Synopsis    [Find the matching closing symbol.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline char * Amap_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;
}

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

  Synopsis    [Find the matching closing symbol.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline Amap_Pair_t Amap_LibertyUpdateHead( Amap_Tree_t * p, Amap_Pair_t Head )
{
    Amap_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 ( Amap_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;
}

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

  Synopsis    [Returns free item.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline Amap_Item_t * Amap_LibertyNewItem( Amap_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    [Returns free item.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Amap_LibertyBuildItem( Amap_Tree_t * p, char ** ppPos, char * pEnd )
{
    Amap_Item_t * pItem;
    Amap_Pair_t Key, Head, Body;
    char * pNext, * pStop;
850
    Key.End = 0;
Alan Mishchenko committed
851
    if ( Amap_LibertySkipSpaces( p, ppPos, pEnd, 0 ) )
Alan Mishchenko committed
852 853 854 855 856
        return -2;
    Key.Beg = *ppPos - p->pContents;
    if ( Amap_LibertySkipEntry( ppPos, pEnd ) )
        goto exit;
    Key.End = *ppPos - p->pContents;
Alan Mishchenko committed
857
    if ( Amap_LibertySkipSpaces( p, ppPos, pEnd, 0 ) )
Alan Mishchenko committed
858 859 860 861 862
        goto exit;
    pNext = *ppPos;
    if ( *pNext == ':' )
    {
        *ppPos = pNext + 1;
Alan Mishchenko committed
863
        if ( Amap_LibertySkipSpaces( p, ppPos, pEnd, 0 ) )
Alan Mishchenko committed
864 865 866 867 868
            goto exit;
        Head.Beg = *ppPos - p->pContents;
        if ( Amap_LibertySkipEntry( ppPos, pEnd ) )
            goto exit;
        Head.End = *ppPos - p->pContents;
Alan Mishchenko committed
869
        if ( Amap_LibertySkipSpaces( p, ppPos, pEnd, 1 ) )
Alan Mishchenko committed
870 871
            goto exit;
        pNext = *ppPos;
Alan Mishchenko committed
872
        if ( *pNext != ';' && *pNext != '\n' )
Alan Mishchenko committed
873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889
            goto exit;
        *ppPos = pNext + 1;
        // end of equation
        pItem = Amap_LibertyNewItem( p, AMAP_LIBERTY_EQUA );
        pItem->Key  = Key;
        pItem->Head = Amap_LibertyUpdateHead( p, Head );
        pItem->Next = Amap_LibertyBuildItem( p, ppPos, pEnd );
        if ( pItem->Next == -1 )
            goto exit;
        return Amap_LibertyItemId( p, pItem );
    }
    if ( *pNext == '(' )
    {
        pStop = Amap_LibertyFindMatch( pNext, pEnd );
        Head.Beg = pNext - p->pContents + 1;
        Head.End = pStop - p->pContents;
        *ppPos = pStop + 1;
Alan Mishchenko committed
890
        if ( Amap_LibertySkipSpaces( p, ppPos, pEnd, 0 ) )
Alan Mishchenko committed
891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 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 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954
        {
            // end of list
            pItem = Amap_LibertyNewItem( p, AMAP_LIBERTY_LIST );
            pItem->Key  = Key;
            pItem->Head = Amap_LibertyUpdateHead( p, Head );
            return Amap_LibertyItemId( p, pItem );
        }
        pNext = *ppPos;
        if ( *pNext == '{' ) // beginning of body
        {
            pStop = Amap_LibertyFindMatch( pNext, pEnd );
            Body.Beg = pNext - p->pContents + 1;
            Body.End = pStop - p->pContents;
            // end of body
            pItem = Amap_LibertyNewItem( p, AMAP_LIBERTY_PROC );
            pItem->Key  = Key;
            pItem->Head = Amap_LibertyUpdateHead( p, Head );
            pItem->Body = Body;
            *ppPos = pNext + 1;
            pItem->Child = Amap_LibertyBuildItem( p, ppPos, pStop );
            if ( pItem->Child == -1 )
                goto exit;
            *ppPos = pStop + 1;
            pItem->Next = Amap_LibertyBuildItem( p, ppPos, pEnd );
            if ( pItem->Next == -1 )
                goto exit;
            return Amap_LibertyItemId( p, pItem );
        }
        // end of list
        if ( *pNext == ';' )
            *ppPos = pNext + 1;
        pItem = Amap_LibertyNewItem( p, AMAP_LIBERTY_LIST );
        pItem->Key  = Key;
        pItem->Head = Amap_LibertyUpdateHead( p, Head );
        pItem->Next = Amap_LibertyBuildItem( p, ppPos, pEnd );
        if ( pItem->Next == -1 )
            goto exit;
        return Amap_LibertyItemId( p, pItem );
    }
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, Amap_LibertyGetString(p, Key) );
    }
    return -1;
}

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

  Synopsis    [Starts the parsing manager.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Amap_Tree_t * Amap_LibertyStart( char * pFileName )
{
    FILE * pFile;
    Amap_Tree_t * p;
955
    int RetValue;
Alan Mishchenko committed
956 957 958 959 960 961 962 963 964 965 966 967 968
    // start the manager
    p = ABC_ALLOC( Amap_Tree_t, 1 );
    memset( p, 0, sizeof(Amap_Tree_t) );
    // read the file into the buffer
    Amap_LibertyFixFileHead( pFileName );
    p->nContents = Amap_LibertyFileSize( pFileName );
    if ( p->nContents == 0 )
    {
        ABC_FREE( p );
        return NULL;
    }
    pFile = fopen( pFileName, "rb" );
    p->pContents = ABC_ALLOC( char, p->nContents+1 );
969
    RetValue = fread( p->pContents, p->nContents, 1, pFile );
Alan Mishchenko committed
970 971 972
    fclose( pFile );
    p->pContents[p->nContents] = 0;
    // other 
973
    p->pFileName = Abc_UtilStrsav( pFileName );
Alan Mishchenko committed
974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011
    p->nItermAlloc = 10 + Amap_LibertyCountItems( p->pContents, p->pContents+p->nContents );
    p->pItems = ABC_CALLOC( Amap_Item_t, p->nItermAlloc );
    p->nItems = 0;
    p->nLines = 1;
    return p;
}

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

  Synopsis    [Stops the parsing manager.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Amap_LibertyStop( Amap_Tree_t * p )
{
    ABC_FREE( p->pFileName );
    ABC_FREE( p->pContents );
    ABC_FREE( p->pItems );
    ABC_FREE( p->pError );
    ABC_FREE( p );
}

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

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

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

  SeeAlso     []

***********************************************************************/
1012
int Amap_LibertyParse( char * pFileName, int fVerbose )
Alan Mishchenko committed
1013 1014 1015
{
    Amap_Tree_t * p;
    char * pPos;
1016
    abctime clk = Abc_Clock();
Alan Mishchenko committed
1017 1018 1019 1020 1021 1022 1023 1024 1025 1026
    int RetValue;
    p = Amap_LibertyStart( pFileName );
    if ( p == NULL )
        return 0;
    pPos = p->pContents;
    Amap_LibertyWipeOutComments( p->pContents, p->pContents+p->nContents );
    if ( Amap_LibertyBuildItem( p, &pPos, p->pContents + p->nContents ) == 0 )
    {
        if ( fVerbose )
        printf( "Parsing finished successfully.\n" );
Alan Mishchenko committed
1027
//        Amap_LibertyPrintLiberty( p, "temp_.lib" );
1028
        Amap_LibertyPrintGenlib( p, Extra_FileNameGenericAppend(pFileName, ".genlib"), fVerbose );
Alan Mishchenko committed
1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040
        RetValue = 1;
    }
    else
    {
        if ( p->pError )
            printf( "%s", p->pError );
        if ( fVerbose )
        printf( "Parsing failed.\n" );
        RetValue = 0;
    }
    if ( fVerbose )
    {
1041
    printf( "Memory = %7.2f MB. ", 1.0*(p->nContents+p->nItermAlloc*sizeof(Amap_Item_t))/(1<<20) );
1042
    ABC_PRT( "Time", Abc_Clock() - clk );
Alan Mishchenko committed
1043 1044 1045 1046 1047
    }
    Amap_LibertyStop( p );
    return RetValue;
}

1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063
/**Function*************************************************************

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

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

  SeeAlso     []

***********************************************************************/
Vec_Str_t * Amap_LibertyParseStr( char * pFileName, int fVerbose )
{
    Amap_Tree_t * p;
    Vec_Str_t * vStr = NULL;
    char * pPos;
1064
    abctime clk = Abc_Clock();
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
    int RetValue;
    p = Amap_LibertyStart( pFileName );
    if ( p == NULL )
        return 0;
    pPos = p->pContents;
    Amap_LibertyWipeOutComments( p->pContents, p->pContents+p->nContents );
    if ( Amap_LibertyBuildItem( p, &pPos, p->pContents + p->nContents ) == 0 )
    {
        if ( fVerbose )
        printf( "Parsing finished successfully.\n" );
//        Amap_LibertyPrintLiberty( p, "temp_.lib" );
        vStr = Amap_LibertyPrintGenlibStr( p, fVerbose );
        RetValue = 1;
    }
    else
    {
        if ( p->pError )
            printf( "%s", p->pError );
        if ( fVerbose )
        printf( "Parsing failed.\n" );
        RetValue = 0;
    }
    if ( fVerbose )
    {
    printf( "Memory = %7.2f MB. ", 1.0*(p->nContents+p->nItermAlloc*sizeof(Amap_Item_t))/(1<<20) );
1090
    ABC_PRT( "Time", Abc_Clock() - clk );
1091 1092 1093 1094 1095 1096
    }
    Amap_LibertyStop( p );
    return vStr;
}


Alan Mishchenko committed
1097 1098 1099 1100 1101
////////////////////////////////////////////////////////////////////////
///                       END OF FILE                                ///
////////////////////////////////////////////////////////////////////////


1102 1103
ABC_NAMESPACE_IMPL_END