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

  FileName    [mioRead.c]

  PackageName [MVSIS 1.3: Multi-valued logic synthesis system.]

  Synopsis    [File reading/writing for technology mapping.]

  Author      [MVSIS Group]
  
  Affiliation [UC Berkeley]

  Date        [Ver. 1.0. Started - September 8, 2003.]

  Revision    [$Id: mioRead.c,v 1.9 2004/10/19 06:40:16 satrajit Exp $]

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

Alan Mishchenko committed
19
#include <ctype.h>
Alan Mishchenko committed
20
#include "mioInt.h"
21
#include "base/io/ioAbc.h"
Alan Mishchenko committed
22

23 24 25
ABC_NAMESPACE_IMPL_START


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

////////////////////////////////////////////////////////////////////////
Alan Mishchenko committed
31
///                     FUNCTION DEFINITIONS                         ///
Alan Mishchenko committed
32 33
////////////////////////////////////////////////////////////////////////

34 35 36
static Mio_Library_t * Mio_LibraryReadOne( char * FileName, int fExtendedFormat, st__table * tExcludeGate, int fVerbose );
static Mio_Library_t * Mio_LibraryReadBuffer( char * pBuffer, int fExtendedFormat, st__table * tExcludeGate, int fVerbose );
static int             Mio_LibraryReadInternal( Mio_Library_t * pLib, char * pBuffer, int fExtendedFormat, st__table * tExcludeGate, int fVerbose );
37 38 39 40 41
static Mio_Gate_t *    Mio_LibraryReadGate( char ** ppToken, int fExtendedFormat );
static Mio_Pin_t *     Mio_LibraryReadPin( char ** ppToken, int fExtendedFormat );
static char *          chomp( char *s );
static void            Mio_LibraryDetectSpecialGates( Mio_Library_t * pLib );
static void            Io_ReadFileRemoveComments( char * pBuffer, int * pnDots, int * pnLines );
Alan Mishchenko committed
42 43 44 45 46 47 48 49 50 51 52 53

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

  Synopsis    [Read the genlib type of library.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
54
Mio_Library_t * Mio_LibraryRead( char * FileName, char * pBuffer, char * ExcludeFile, int fVerbose )
Alan Mishchenko committed
55 56 57 58
{
    Mio_Library_t * pLib;
    int num;

59
    st__table * tExcludeGate = 0;
Alan Mishchenko committed
60 61 62

    if ( ExcludeFile )
    {
63
        tExcludeGate = st__init_table(strcmp, st__strhash);
64
        if ( (num = Mio_LibraryReadExclude( ExcludeFile, tExcludeGate )) == -1 )
Alan Mishchenko committed
65
        {
66
            st__free_table( tExcludeGate );
Alan Mishchenko committed
67 68 69
            tExcludeGate = 0;
            return 0;
        }
70
        fprintf ( stdout, "Read %d gates from exclude file\n", num );
Alan Mishchenko committed
71 72
    }

73 74 75 76 77 78 79 80
    if ( pBuffer == NULL )
        pLib = Mio_LibraryReadOne( FileName, 0, tExcludeGate, fVerbose );       // try normal format first ..
    else
    {
        pLib = Mio_LibraryReadBuffer( pBuffer, 0, tExcludeGate, fVerbose );       // try normal format first ..
        if ( pLib )
            pLib->pName = Abc_UtilStrsav( Extra_FileNameGenericAppend(FileName, ".genlib") );
    }
Alan Mishchenko committed
81 82
    if ( pLib == NULL )
    {
83 84 85 86 87 88 89 90
        if ( pBuffer == NULL )
            pLib = Mio_LibraryReadOne( FileName, 1, tExcludeGate, fVerbose );       // try normal format first ..
        else
        {
            pLib = Mio_LibraryReadBuffer( pBuffer, 1, tExcludeGate, fVerbose );       // try normal format first ..
            if ( pLib )
                pLib->pName = Abc_UtilStrsav( Extra_FileNameGenericAppend(FileName, ".genlib") );
        }
Alan Mishchenko committed
91
        if ( pLib != NULL )
92
            printf ( "Warning: Read extended genlib format but ignoring extensions\n" );
Alan Mishchenko committed
93
    }
94
    if ( tExcludeGate )
95
        st__free_table( tExcludeGate );
Alan Mishchenko committed
96 97 98 99 100 101

    return pLib;
}

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

102 103 104 105 106 107 108 109 110
  Synopsis    [Read contents of the file.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
111
char * Mio_ReadFile( char * FileName, int fAddEnd )
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
{
    char * pBuffer;
    FILE * pFile;
    int nFileSize;
    int RetValue;

    // open the BLIF file for binary reading
    pFile = Io_FileOpen( FileName, "open_path", "rb", 1 );
//    pFile = fopen( FileName, "rb" );
    // if we got this far, file should be okay otherwise would
    // have been detected by caller
    assert ( pFile != NULL );
    // get the file size, in bytes
    fseek( pFile, 0, SEEK_END );  
    nFileSize = ftell( pFile );  
    // move the file current reading position to the beginning
    rewind( pFile ); 
    // load the contents of the file into memory
    pBuffer   = ABC_ALLOC( char, nFileSize + 10 );
    RetValue = fread( pBuffer, nFileSize, 1, pFile );
    // terminate the string with '\0'
    pBuffer[ nFileSize ] = '\0';
134 135
    if ( fAddEnd )
        strcat( pBuffer, "\n.end\n" );
136 137 138 139 140 141 142
    // close file
    fclose( pFile );
    return pBuffer;
}

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

Alan Mishchenko committed
143 144 145 146 147 148 149 150 151
  Synopsis    [Read the genlib type of library.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
152
Mio_Library_t * Mio_LibraryReadBuffer( char * pBuffer, int fExtendedFormat, st__table * tExcludeGate, int fVerbose )
Alan Mishchenko committed
153 154 155 156
{
    Mio_Library_t * pLib;

    // allocate the genlib structure
157
    pLib = ABC_CALLOC( Mio_Library_t, 1 );
158
    pLib->tName2Gate = st__init_table(strcmp, st__strhash);
159
    pLib->pMmFlex = Mem_FlexStart();
Alan Mishchenko committed
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 192 193 194 195
    pLib->vCube = Vec_StrAlloc( 100 );

    Io_ReadFileRemoveComments( pBuffer, NULL, NULL );

    // parse the contents of the file
    if ( Mio_LibraryReadInternal( pLib, pBuffer, fExtendedFormat, tExcludeGate, fVerbose ) )
    {
        Mio_LibraryDelete( pLib );
        return NULL;
    }

    // derive the functinality of gates
    if ( Mio_LibraryParseFormulas( pLib ) )
    {
        printf( "Mio_LibraryRead: Had problems parsing formulas.\n" );
        Mio_LibraryDelete( pLib );
        return NULL;
    }

    // detect INV and NAND2
    Mio_LibraryDetectSpecialGates( pLib );
//Mio_WriteLibrary( stdout, pLib );
    return pLib;
}

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

  Synopsis    [Read the genlib type of library.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
196
Mio_Library_t * Mio_LibraryReadOne( char * FileName, int fExtendedFormat, st__table * tExcludeGate, int fVerbose )
197 198 199 200 201 202 203
{
    Mio_Library_t * pLib;
    char * pBuffer;
    // read the file and clean comments
    // pBuffer = Io_ReadFileFileContents( FileName, NULL );
    // we don't use above function but actually do the same thing explicitly
    // to handle open_path expansion correctly
204
    pBuffer = Mio_ReadFile( FileName, 1 );
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
    if ( pBuffer == NULL )
        return NULL;
    pLib = Mio_LibraryReadBuffer( pBuffer, fExtendedFormat, tExcludeGate, fVerbose );
    ABC_FREE( pBuffer );
    if ( pLib )
        pLib->pName = Mio_UtilStrsav( FileName );
    return pLib;
}

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

  Synopsis    [Read the genlib type of library.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
225
int Mio_LibraryReadInternal( Mio_Library_t * pLib, char * pBuffer, int fExtendedFormat, st__table * tExcludeGate, int fVerbose )
Alan Mishchenko committed
226 227 228 229 230 231 232 233 234 235 236 237
{
    Mio_Gate_t * pGate, ** ppGate;
    char * pToken;
    int nGates = 0;
    int nDel = 0;

    // start the linked list of gates
    pLib->pGates = NULL;
    ppGate = &pLib->pGates;

    // read gates one by one
    pToken = strtok( pBuffer, " \t\r\n" );
238
    while ( pToken && (strcmp( pToken, MIO_STRING_GATE ) == 0 || strcmp( pToken, MIO_STRING_LATCH ) == 0) )
Alan Mishchenko committed
239
    {
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
        // skip latches
        if ( strcmp( pToken, MIO_STRING_LATCH ) == 0 )
        {
            while ( pToken && strcmp( pToken, MIO_STRING_GATE ) != 0 && strcmp( pToken, ".end" ) != 0 )
            {
                if ( strcmp( pToken, MIO_STRING_LATCH ) == 0 )
                {
                    pToken = strtok( NULL, " \t\r\n" );
                    printf( "Skipping latch \"%s\"...\n", pToken );
                    continue;
                }
                pToken = strtok( NULL, " \t\r\n" );
            }
            if ( !(pToken && strcmp( pToken, MIO_STRING_GATE ) == 0) )
                break;
        }

Alan Mishchenko committed
257 258 259 260
        // derive the next gate
        pGate = Mio_LibraryReadGate( &pToken, fExtendedFormat );
        if ( pGate == NULL )
            return 1;
261

262 263 264 265 266 267
        // skip the gate if its formula has problems
        if ( !Mio_ParseCheckFormula(pGate, pGate->pForm) )
        {
            Mio_GateDelete( pGate );
            continue;
        }
268
       
Alan Mishchenko committed
269 270 271 272 273
        // set the library
        pGate->pLib = pLib;

        // printf ("Processing: '%s'\n", pGate->pName);

274
        if ( tExcludeGate && st__is_member( tExcludeGate, pGate->pName ) )
Alan Mishchenko committed
275 276 277 278 279 280 281 282 283 284 285 286 287
        {
            //printf ("Excluding: '%s'\n", pGate->pName);
            Mio_GateDelete( pGate );
            nDel++;
        } 
        else
        {
            // add this gate to the list
            *ppGate = pGate;
            ppGate  = &pGate->pNext;
            nGates++;

            // remember this gate by name
288 289
            if ( ! st__is_member( pLib->tName2Gate, pGate->pName ) )
                st__insert( pLib->tName2Gate, pGate->pName, (char *)pGate );
Alan Mishchenko committed
290
            else
291
            {
292 293 294 295 296 297 298 299
                Mio_Gate_t * pBase = Mio_LibraryReadGateByName( pLib, pGate->pName, NULL );
                if ( pBase->pTwin != NULL )
                {
                    printf( "Gates with more than 2 outputs are not supported.\n" );
                    continue;
                }
                pBase->pTwin = pGate;
                pGate->pTwin = pBase;
300
//                printf( "Gate \"%s\" appears two times. Creating a 2-output gate.\n", pGate->pName );
301
            }
Alan Mishchenko committed
302 303 304
        }
    }

305 306 307 308 309 310
    if ( nGates == 0 )
    {
        printf( "The library contains no gates.\n" );
        return 1;
    }

Alan Mishchenko committed
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
    // check what is the last word read
    if ( pToken && strcmp( pToken, ".end" ) != 0 )
        return 1;

    if ( nDel != 0 ) 
        printf( "Actually excluded %d cells\n", nDel );

    return 0;
}

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

  Synopsis    [Read the genlib type of gate.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
332
Mio_Gate_t * Mio_LibraryReadGate( char ** ppToken, int fExtendedFormat )
Alan Mishchenko committed
333 334 335 336 337 338
{
    Mio_Gate_t * pGate;
    Mio_Pin_t * pPin, ** ppPin;
    char * pToken = *ppToken;

    // allocate the gate structure
339
    pGate = ABC_CALLOC( Mio_Gate_t, 1 );
Alan Mishchenko committed
340 341 342

    // read the name
    pToken = strtok( NULL, " \t\r\n" );
343
    pGate->pName = Mio_UtilStrsav( pToken );
Alan Mishchenko committed
344 345 346 347 348 349 350 351 352 353 354 355 356

    // read the area
    pToken = strtok( NULL, " \t\r\n" );
    pGate->dArea = atof( pToken );

    // read the formula

    // first the output name
    pToken = strtok( NULL, "=" );
    pGate->pOutName = chomp( pToken );

    // then rest of the expression 
    pToken = strtok( NULL, ";" );
Alan Mishchenko committed
357
    pGate->pForm = chomp( pToken );
Alan Mishchenko committed
358 359 360 361 362 363 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

    // read the pin info
    // start the linked list of pins
    pGate->pPins = NULL;
    ppPin = &pGate->pPins;

    // read gates one by one
    pToken = strtok( NULL, " \t\r\n" );
    while ( pToken && strcmp( pToken, MIO_STRING_PIN ) == 0 )
    {
        // derive the next gate
        pPin = Mio_LibraryReadPin( &pToken, fExtendedFormat );
        if ( pPin == NULL )
        {
            Mio_GateDelete( pGate );
            *ppToken = pToken;
            return NULL;
        }
        // add this pin to the list
        *ppPin = pPin;
        ppPin  = &pPin->pNext;
        // get the next token
        pToken = strtok( NULL, " \t\r\n" );
    }

    *ppToken = pToken;
    return pGate;
}



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

  Synopsis    [Read the genlib type of pin.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
400
Mio_Pin_t * Mio_LibraryReadPin( char ** ppToken, int fExtendedFormat )
Alan Mishchenko committed
401 402 403 404 405
{
    Mio_Pin_t * pPin;
    char * pToken = *ppToken;

    // allocate the gate structure
406
    pPin = ABC_CALLOC( Mio_Pin_t, 1 );
Alan Mishchenko committed
407 408 409

    // read the name
    pToken = strtok( NULL, " \t\r\n" );
410
    pPin->pName = Mio_UtilStrsav( pToken );
Alan Mishchenko committed
411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 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

    // read the pin phase
    pToken = strtok( NULL, " \t\r\n" );
    if ( strcmp( pToken, MIO_STRING_UNKNOWN ) == 0 )
        pPin->Phase = MIO_PHASE_UNKNOWN;
    else if ( strcmp( pToken, MIO_STRING_INV ) == 0 )
        pPin->Phase = MIO_PHASE_INV;
    else if ( strcmp( pToken, MIO_STRING_NONINV ) == 0 )
        pPin->Phase = MIO_PHASE_NONINV;
    else 
    {
        printf( "Cannot read pin phase specification\n" );
        Mio_PinDelete( pPin );
        *ppToken = pToken;
        return NULL;
    }

    pToken = strtok( NULL, " \t\r\n" );
    pPin->dLoadInput = atof( pToken );

    pToken = strtok( NULL, " \t\r\n" );
    pPin->dLoadMax = atof( pToken );

    pToken = strtok( NULL, " \t\r\n" );
    pPin->dDelayBlockRise = atof( pToken );

    pToken = strtok( NULL, " \t\r\n" );
    pPin->dDelayFanoutRise = atof( pToken );

    pToken = strtok( NULL, " \t\r\n" );
    pPin->dDelayBlockFall = atof( pToken );

    pToken = strtok( NULL, " \t\r\n" );
    pPin->dDelayFanoutFall = atof( pToken );

    if ( fExtendedFormat )
    {
        /* In extended format, the field after dDelayFanoutRise
         * is to be ignored
         **/

        pPin->dDelayBlockFall  = pPin->dDelayFanoutFall;

        pToken = strtok( NULL, " \t" );
        pPin->dDelayFanoutFall = atof( pToken );

        /* last field is ignored */
        pToken = strtok( NULL, " \t\r\n" );
    }

    if ( pPin->dDelayBlockRise > pPin->dDelayBlockFall )
        pPin->dDelayBlockMax = pPin->dDelayBlockRise;
    else
        pPin->dDelayBlockMax = pPin->dDelayBlockFall;

    *ppToken = pToken;
    return pPin;
}


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

  Synopsis    [Duplicates string and returns it with leading and 
               trailing spaces removed.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
483
char * chomp( char *s )
Alan Mishchenko committed
484
{
Alan Mishchenko committed
485 486 487 488 489 490
    char *a, *b, *c;
    // remove leading spaces
    for ( b = s; *b; b++ )
        if ( !isspace(*b) )
            break;
    // strsav the string
Alan Mishchenko committed
491
    a = strcpy( ABC_ALLOC(char, strlen(b)+1), b );
Alan Mishchenko committed
492 493 494 495 496 497 498
    // remove trailing spaces
    for ( c = a+strlen(a); c > a; c-- )
        if ( *c == 0 || isspace(*c) )
            *c = 0;
        else
            break;
    return a;
Alan Mishchenko committed
499 500 501 502
}   
        
/**Function*************************************************************

Alan Mishchenko committed
503
  Synopsis    []
Alan Mishchenko committed
504 505 506 507 508 509 510 511

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
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
int Mio_LibraryCompareGatesByArea( Mio_Gate_t ** pp1, Mio_Gate_t ** pp2 )
{
    double Diff = (*pp1)->dArea - (*pp2)->dArea;
    if ( Diff < 0.0 )
        return -1;
    if ( Diff > 0.0 ) 
        return 1;
    return 0; 
}
        
/**Function*************************************************************

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Mio_LibraryCompareGatesByName( Mio_Gate_t ** pp1, Mio_Gate_t ** pp2 )
{
    int Diff = strcmp( (*pp1)->pName, (*pp2)->pName );
    if ( Diff < 0.0 )
        return -1;
    if ( Diff > 0.0 ) 
        return 1;
    return 0; 
}
        
/**Function*************************************************************

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Mio_LibrarySortGates( Mio_Library_t * pLib )
{
    Mio_Gate_t ** ppGates, * pGate;
    int i = 0;
Alan Mishchenko committed
558
    ppGates = ABC_ALLOC( Mio_Gate_t *, pLib->nGates );
Alan Mishchenko committed
559 560 561 562
    Mio_LibraryForEachGate( pLib, pGate )
        ppGates[i++] = pGate;
    assert( i == pLib->nGates );
    // sort gates by area
Alan Mishchenko committed
563
    pLib->ppGates0 = ABC_ALLOC( Mio_Gate_t *, pLib->nGates );
Alan Mishchenko committed
564 565 566 567 568 569 570
    for ( i = 0; i < pLib->nGates; i++ )
        pLib->ppGates0[i] = ppGates[i];
    qsort( (void *)ppGates, pLib->nGates, sizeof(void *), 
            (int (*)(const void *, const void *)) Mio_LibraryCompareGatesByArea );
    for ( i = 0; i < pLib->nGates; i++ )
        ppGates[i]->pNext = (i < pLib->nGates-1)? ppGates[i+1] : NULL;
    pLib->pGates = ppGates[0];
Alan Mishchenko committed
571
    ABC_FREE( ppGates );
Alan Mishchenko committed
572
    // sort gates by name
Alan Mishchenko committed
573
    pLib->ppGatesName = ABC_ALLOC( Mio_Gate_t *, pLib->nGates );
Alan Mishchenko committed
574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590
    for ( i = 0; i < pLib->nGates; i++ )
        pLib->ppGatesName[i] = pLib->ppGates0[i];
    qsort( (void *)pLib->ppGatesName, pLib->nGates, sizeof(void *), 
            (int (*)(const void *, const void *)) Mio_LibraryCompareGatesByName );
}
        
/**Function*************************************************************

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
591 592 593 594 595 596 597 598 599 600
static inline Mio_Gate_t * Mio_GateCompare( Mio_Gate_t * pThis, Mio_Gate_t * pNew, word uTruth )
{
    if ( pNew->uTruth != uTruth )
        return pThis;
    if ( pThis == NULL )
        return pNew;
    if ( pThis->dArea > pNew->dArea || (pThis->dArea == pNew->dArea && strcmp(pThis->pName, pNew->pName) > 0) )
        return pNew;
    return pThis;
}
Alan Mishchenko committed
601 602 603
void Mio_LibraryDetectSpecialGates( Mio_Library_t * pLib )
{
    Mio_Gate_t * pGate;
604
    word uFuncBuf, uFuncInv, uFuncNand2, uFuncAnd2;
Alan Mishchenko committed
605

Alan Mishchenko committed
606 607
    Mio_LibrarySortGates( pLib );

608 609
    uFuncBuf   = ABC_CONST(0xAAAAAAAAAAAAAAAA);
    uFuncAnd2  = ABC_CONST(0xAAAAAAAAAAAAAAAA) & ABC_CONST(0xCCCCCCCCCCCCCCCC);
610 611
    uFuncInv   = ~uFuncBuf;
    uFuncNand2 = ~uFuncAnd2;
Alan Mishchenko committed
612

613
    // get smallest-area buffer
Alan Mishchenko committed
614
    Mio_LibraryForEachGate( pLib, pGate )
615
        pLib->pGateBuf = Mio_GateCompare( pLib->pGateBuf, pGate, uFuncBuf );
Alan Mishchenko committed
616 617
    if ( pLib->pGateBuf == NULL )
    {
618
        printf( "Warnings: genlib library reader cannot detect the buffer gate.\n" );
Alan Mishchenko committed
619 620
        printf( "Some parts of the supergate-based technology mapper may not work correctly.\n" );
    }
Alan Mishchenko committed
621
 
622
    // get smallest-area inverter
Alan Mishchenko committed
623
    Mio_LibraryForEachGate( pLib, pGate )
624
        pLib->pGateInv = Mio_GateCompare( pLib->pGateInv, pGate, uFuncInv );
Alan Mishchenko committed
625 626
    if ( pLib->pGateInv == NULL )
    {
627
        printf( "Warnings: genlib library reader cannot detect the invertor gate.\n" );
Alan Mishchenko committed
628 629 630
        printf( "Some parts of the supergate-based technology mapper may not work correctly.\n" );
    }

631
    // get smallest-area NAND2/AND2 gates
Alan Mishchenko committed
632
    Mio_LibraryForEachGate( pLib, pGate )
633
        pLib->pGateNand2 = Mio_GateCompare( pLib->pGateNand2, pGate, uFuncNand2 );
Alan Mishchenko committed
634
    Mio_LibraryForEachGate( pLib, pGate )
635
        pLib->pGateAnd2 = Mio_GateCompare( pLib->pGateAnd2, pGate, uFuncAnd2 );
Alan Mishchenko committed
636
    if ( pLib->pGateAnd2 == NULL && pLib->pGateNand2 == NULL )
Alan Mishchenko committed
637
    {
638
        printf( "Warnings: genlib library reader cannot detect the AND2 or NAND2 gate.\n" );
Alan Mishchenko committed
639 640 641 642 643 644 645 646 647 648 649 650 651 652 653
        printf( "Some parts of the supergate-based technology mapper may not work correctly.\n" );
    }
}

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

  Synopsis    [populate hash table of gates to be exlcuded from genlib]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
654
int Mio_LibraryReadExclude( char * ExcludeFile, st__table * tExcludeGate )
Alan Mishchenko committed
655 656 657 658 659 660 661 662 663 664 665 666 667
{
    int nDel = 0;
    FILE *pEx;
    char buffer[128];

    assert ( tExcludeGate );

    if ( ExcludeFile )
    {
        pEx = fopen( ExcludeFile, "r" );

        if ( pEx == NULL )
        {
668
            fprintf ( stdout, "Error: Could not open exclude file %s. Stop.\n", ExcludeFile );
Alan Mishchenko committed
669 670 671 672 673 674
            return -1;
        }

        while (1 == fscanf( pEx, "%127s", buffer ))
        {
            //printf ("Read: '%s'\n", buffer );
675
            st__insert( tExcludeGate, Mio_UtilStrsav( buffer ), (char *)0 );
Alan Mishchenko committed
676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747
            nDel++;
        }

        fclose( pEx );
    }

    return nDel;
}
    
/**Function*************************************************************

  Synopsis    [Eliminates comments from the input file.]

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

  SeeAlso     []

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

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


748 749
ABC_NAMESPACE_IMPL_END