wlcReadVer.c 50.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
/**CFile****************************************************************

  FileName    [wlcReadVer.c]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [Verilog parser.]

  Synopsis    [Parses several flavors of word-level Verilog.]

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

  Date        [Ver. 1.0. Started - August 22, 2014.]

  Revision    [$Id: wlcReadVer.c,v 1.00 2014/09/12 00:00:00 alanmi Exp $]

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

#include "wlc.h"

ABC_NAMESPACE_IMPL_START


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

// Word-level Verilog file parser
#define WLV_PRS_MAX_LINE   1000

typedef struct Wlc_Prs_t_  Wlc_Prs_t;
struct Wlc_Prs_t_ 
{
    int                    nFileSize;
    char *                 pFileName;
    char *                 pBuffer;
    Vec_Int_t *            vLines;
    Vec_Int_t *            vStarts;
    Vec_Int_t *            vFanins;
    Wlc_Ntk_t *            pNtk;
43 44 45
    Mem_Flex_t *           pMemTable;
    Vec_Ptr_t *            vTables;
    int                    nConsts;
46 47 48
    int                    nNonZero[4];
    int                    nNegative[4];
    int                    nReverse[4];
49 50 51 52 53 54 55
    char                   sError[WLV_PRS_MAX_LINE];
};

static inline int          Wlc_PrsOffset( Wlc_Prs_t * p, char * pStr )  { return pStr - p->pBuffer;                     }
static inline char *       Wlc_PrsStr( Wlc_Prs_t * p, int iOffset )     { return p->pBuffer + iOffset;                  }
static inline int          Wlc_PrsStrCmp( char * pStr, char * pWhat )   { return !strncmp( pStr, pWhat, strlen(pWhat)); }

56
#define Wlc_PrsForEachLine( p, pLine, i )             \
57
    for ( i = 0; (i < Vec_IntSize((p)->vStarts)) && ((pLine) = Wlc_PrsStr(p, Vec_IntEntry((p)->vStarts, i))); i++ )
58 59
#define Wlc_PrsForEachLineStart( p, pLine, i, Start ) \
    for ( i = Start; (i < Vec_IntSize((p)->vStarts)) && ((pLine) = Wlc_PrsStr(p, Vec_IntEntry((p)->vStarts, i))); i++ )
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77


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


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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
78
Wlc_Prs_t * Wlc_PrsStart( char * pFileName, char * pStr )
79 80
{
    Wlc_Prs_t * p;
81
    if ( pFileName && !Extra_FileCheck( pFileName ) )
82 83 84
        return NULL;
    p = ABC_CALLOC( Wlc_Prs_t, 1 );
    p->pFileName = pFileName;
85
    p->pBuffer   = pStr ? Abc_UtilStrsav(pStr) : Extra_FileReadContents( pFileName );
86 87 88 89
    p->nFileSize = strlen(p->pBuffer);  assert( p->nFileSize > 0 );
    p->vLines    = Vec_IntAlloc( p->nFileSize / 50 );
    p->vStarts   = Vec_IntAlloc( p->nFileSize / 50 );
    p->vFanins   = Vec_IntAlloc( 100 );
90 91
    p->vTables   = Vec_PtrAlloc( 1000 );
    p->pMemTable = Mem_FlexStart();
92 93 94 95 96 97
    return p;
}
void Wlc_PrsStop( Wlc_Prs_t * p )
{
    if ( p->pNtk )
        Wlc_NtkFree( p->pNtk );
98 99 100
    if ( p->pMemTable )
        Mem_FlexStop( p->pMemTable, 0 );
    Vec_PtrFreeP( &p->vTables );
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
    Vec_IntFree( p->vLines );
    Vec_IntFree( p->vStarts );
    Vec_IntFree( p->vFanins );
    ABC_FREE( p->pBuffer );
    ABC_FREE( p );
}

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

  Synopsis    [Prints the error message including the file name and line number.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
119 120 121 122 123 124 125 126
int Wlc_PrsFindLine( Wlc_Prs_t * p, char * pCur )
{
    int Entry, iLine = 0;
    Vec_IntForEachEntry( p->vLines, Entry, iLine )
        if ( Entry > pCur - p->pBuffer )
            return iLine + 1;
    return -1;
}
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
int Wlc_PrsWriteErrorMessage( Wlc_Prs_t * p, char * pCur, const char * format, ... )
{
    char * pMessage;
    // derive message
    va_list args;
    va_start( args, format );
    pMessage = vnsprintf( format, args );
    va_end( args );
    // print messsage
    assert( strlen(pMessage) < WLV_PRS_MAX_LINE - 100 );
    assert( p->sError[0] == 0 );
    if ( pCur == NULL ) // the line number is not given
        sprintf( p->sError, "%s: %s\n", p->pFileName, pMessage );
    else                // print the error message with the line number
    {
142 143
        int iLine = Wlc_PrsFindLine( p, pCur );
        sprintf( p->sError, "%s (line %d): %s\n", p->pFileName, iLine, pMessage );
144
    }
145
    ABC_FREE( pMessage );
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
    return 0;
}
void Wlc_PrsPrintErrorMessage( Wlc_Prs_t * p )
{
    if ( p->sError[0] == 0 )
        return;
    fprintf( stdout, "%s", p->sError );
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline int Wlc_PrsIsDigit( char * pStr )
{
    return (pStr[0] >= '0' && pStr[0] <= '9');
}
static inline int Wlc_PrsIsChar( char * pStr )
{
    return (pStr[0] >= 'a' && pStr[0] <= 'z') || 
           (pStr[0] >= 'A' && pStr[0] <= 'Z') || 
           (pStr[0] >= '0' && pStr[0] <= '9') || 
175
            pStr[0] == '_' || pStr[0] == '$' || pStr[0] == '\\';
176 177 178 179 180 181 182 183 184
}
static inline char * Wlc_PrsSkipSpaces( char * pStr )
{
    while ( *pStr && *pStr == ' ' )
        pStr++;
    return pStr;
}
static inline char * Wlc_PrsFindSymbol( char * pStr, char Symb )
{
185
    int fNotName = 1;
186
    for ( ; *pStr; pStr++ )
187 188
    {
        if ( fNotName && *pStr == Symb )
189
            return pStr;
190 191 192 193 194
        if ( pStr[0] == '\\' )
            fNotName = 0;
        else if ( !fNotName && *pStr == ' ' )
            fNotName = 1;
    }
195 196 197 198 199 200 201 202 203
    return NULL;
}
static inline char * Wlc_PrsFindSymbolTwo( char * pStr, char Symb, char Symb2 )
{
    for ( ; pStr[1]; pStr++ )
        if ( pStr[0] == Symb  && pStr[1] == Symb2 )
            return pStr;
    return NULL;
}
204
static inline char * Wlc_PrsFindClosingParenthesis( char * pStr, char Open, char Close )
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 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
{
    int Counter = 0;
    int fNotName = 1;
    assert( *pStr == Open );
    for ( ; *pStr; pStr++ )
    {
        if ( fNotName )
        {
            if ( *pStr == Open )
                Counter++;
            if ( *pStr == Close )
                Counter--;
            if ( Counter == 0 )
                return pStr;
        }
        if ( *pStr == '\\' )
            fNotName = 0;
        else if ( !fNotName && *pStr == ' ' )
            fNotName = 1;
    }
    return NULL;
}
int Wlc_PrsRemoveComments( Wlc_Prs_t * p )
{
    int fSpecifyFound = 0;
    char * pCur, * pNext, * pEnd = p->pBuffer + p->nFileSize;
    for ( pCur = p->pBuffer; pCur < pEnd; pCur++ )
    {
        // regular comment (//)
        if ( *pCur == '/' && pCur[1] == '/' )
        {
            if ( pCur + 5 < pEnd && pCur[2] == 'a' && pCur[3] == 'b' && pCur[4] == 'c' && pCur[5] == '2' )
                pCur[0] = pCur[1] = pCur[2] = pCur[3] = pCur[4] = pCur[5] = ' ';
            else
            {
                pNext = Wlc_PrsFindSymbol( pCur, '\n' );
                if ( pNext == NULL )
                    return Wlc_PrsWriteErrorMessage( p, pCur, "Cannot find end-of-line after symbols \"//\"." );
                for ( ; pCur < pNext; pCur++ )
                    *pCur = ' ';
            }
        }
        // skip preprocessor directive (`timescale, `celldefine, etc)
        else if ( *pCur == '`' )
        {
            pNext = Wlc_PrsFindSymbol( pCur, '\n' );
            if ( pNext == NULL )
                return Wlc_PrsWriteErrorMessage( p, pCur, "Cannot find end-of-line after symbols \"`\"." );
            for ( ; pCur < pNext; pCur++ )
                *pCur = ' ';
        }
        // regular comment (/* ... */)
        else if ( *pCur == '/' && pCur[1] == '*' )
        {
            pNext = Wlc_PrsFindSymbolTwo( pCur, '*', '/' );
            if ( pNext == NULL )
                return Wlc_PrsWriteErrorMessage( p, pCur, "Cannot find symbols \"*/\" after symbols \"/*\"." );
            // overwrite comment
            for ( ; pCur < pNext + 2; pCur++ )
                *pCur = ' ';
        }
        // 'specify' treated as comments
        else if ( *pCur == 's' && pCur[1] == 'p' && pCur[2] == 'e' && !strncmp(pCur, "specify", 7) )
        {
            for ( pNext = pCur; pNext < pEnd - 10; pNext++ )
                if ( *pNext == 'e' && pNext[1] == 'n' && pNext[2] == 'd' && !strncmp(pNext, "endspecify", 10) )
                {
                    // overwrite comment
                    for ( ; pCur < pNext + 10; pCur++ )
                        *pCur = ' ';
                    if ( fSpecifyFound == 0 )
                        Abc_Print( 0, "Ignoring specify/endspecify directives.\n" );
                    fSpecifyFound = 1;
                    break;
                }
        }
        // insert semicolons
        else if ( *pCur == 'e' && pCur[1] == 'n' && pCur[2] == 'd' && !strncmp(pCur, "endmodule", 9) )
            pCur[strlen("endmodule")] = ';';
        // overwrite end-of-lines with spaces (less checking to do later on)
        if ( *pCur == '\n' || *pCur == '\r'  || *pCur == '\t' )
            *pCur = ' ';
    }
    return 1;
}
int Wlc_PrsPrepare( Wlc_Prs_t * p )
{
    int fPrettyPrint = 0;
    int fNotName = 1;
    char * pTemp, * pPrev, * pThis;
    // collect info about lines
    assert( Vec_IntSize(p->vLines) == 0 );
    for ( pTemp = p->pBuffer; *pTemp; pTemp++ )
        if ( *pTemp == '\n' )
            Vec_IntPush( p->vLines, pTemp - p->pBuffer );
    // delete comments and insert breaks
    if ( !Wlc_PrsRemoveComments( p ) )
        return 0;
    // collect info about breaks
    assert( Vec_IntSize(p->vStarts) == 0 );
    for ( pPrev = pThis = p->pBuffer; *pThis; pThis++ )
    {
        if ( fNotName && *pThis == ';' )
        {
            *pThis = 0;
            Vec_IntPush( p->vStarts, Wlc_PrsOffset(p, Wlc_PrsSkipSpaces(pPrev)) );
            pPrev = pThis + 1;
        }
        if ( *pThis == '\\' )
            fNotName = 0;
        else if ( !fNotName && *pThis == ' ' )
            fNotName = 1;
    }

    if ( fPrettyPrint )
    {
        int i, k;
        // print the line types
        Wlc_PrsForEachLine( p, pTemp, i )
        {
            if ( Wlc_PrsStrCmp( pTemp, "module" ) )
                printf( "\n" );
            if ( !Wlc_PrsStrCmp( pTemp, "module" ) && !Wlc_PrsStrCmp( pTemp, "endmodule" ) )
                printf( "    " );
            printf( "%c", pTemp[0] );
            for ( k = 1; pTemp[k]; k++ )
                if ( pTemp[k] != ' ' || pTemp[k-1] != ' ' )
                    printf( "%c", pTemp[k] );
            printf( ";\n" );
        }
/*    
        // print the line types
        Wlc_PrsForEachLine( p, pTemp, i )
        {
            int k;  
            if ( !Wlc_PrsStrCmp( pTemp, "module" ) )
                continue;
            printf( "%3d : ", i );
            for ( k = 0; k < 40; k++ )
                printf( "%c", pTemp[k] ? pTemp[k] : ' ' );
            printf( "\n" );
        }
*/
    }
    return 1;
}

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

354 355 356 357 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 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414
  Synopsis    [Modified version of strtok().]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
char * Wlc_PrsStrtok( char * s, const char * delim )
{
  const char *spanp;
  int c, sc;
  char *tok;
  static char *last;
  if (s == NULL && (s = last) == NULL)
      return NULL;
  // skip leading delimiters
cont:
  c = *s++;
  for (spanp = delim; (sc = *spanp++) != 0;) 
      if (c == sc)
          goto cont;
  if (c == 0)    // no non-delimiter characters 
      return (last = NULL);
//  tok = s - 1;
  if ( c != '\\' )
      tok = s - 1;
  else
      tok = s - 1;
  // go back to the first non-delimiter character
  s--;
  // find the token
  for (;;) 
  {
      c = *s++;
      if ( c == '\\' )  // skip blind characters
      {
          while ( c != ' ' )
              c = *s++;
          c = *s++;
      }
      spanp = delim;
      do {
          if ((sc = *spanp++) == c) 
          {
              if (c == 0)
                  s = NULL;
              else
                  s[-1] = 0;
              last = s;
              return (tok);
          }
      } while (sc != 0);
  }
  // not reached
  return NULL;
}

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

415 416 417 418 419 420 421 422 423
  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
424 425 426 427 428 429
char * Wlc_PrsConvertInitValues( Wlc_Ntk_t * p )
{
    Wlc_Obj_t * pObj; 
    int i, k, Value, * pInits;
    char * pResult;
    Vec_Str_t * vStr = Vec_StrAlloc( 1000 );
430
    Vec_IntForEachEntry( p->vInits, Value, i )
431 432 433 434 435 436 437 438
    {
        if ( Value < 0 )
        {
            for ( k = 0; k < -Value; k++ )
                Vec_StrPush( vStr, '0' );
            continue;
        }
        pObj = Wlc_NtkObj( p, Value );
439
        Value = Wlc_ObjRange(pObj);
440 441
        while ( pObj->Type == WLC_OBJ_BUF )
            pObj = Wlc_NtkObj( p, Wlc_ObjFaninId0(pObj) );
442
        pInits = (pObj->Type == WLC_OBJ_CONST && !pObj->fXConst) ? Wlc_ObjConstValue(pObj) : NULL;
443
        for ( k = 0; k < Abc_MinInt(Value, Wlc_ObjRange(pObj)); k++ )
444
            Vec_StrPush( vStr, (char)(pInits ? '0' + Abc_InfoHasBit((unsigned *)pInits, k) : 'X') );
445 446 447 448
        // extend values with zero, in case the init value signal has different range compared to constant used
        for ( ; k < Value; k++ )
            Vec_StrPush( vStr, '0' );
        // update vInits to contain either number of values or PI index
449
        Vec_IntWriteEntry( p->vInits, i, (pInits || pObj->fXConst) ? -Value : Wlc_ObjCiId(pObj) );
450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467
    }
    Vec_StrPush( vStr, '\0' );
    pResult = Vec_StrReleaseArray( vStr );
    Vec_StrFree( vStr );
    return pResult;
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
468 469 470 471 472 473 474
static inline char * Wlc_PrsFindRange( char * pStr, int * End, int * Beg )
{
    *End = *Beg = 0;
    pStr = Wlc_PrsSkipSpaces( pStr );
    if ( pStr[0] != '[' )
        return pStr;
    pStr = Wlc_PrsSkipSpaces( pStr+1 );
475
    if ( !Wlc_PrsIsDigit(pStr) && pStr[0] != '-' )
476 477 478 479 480 481 482 483 484 485 486 487
        return NULL;
    *End = *Beg = atoi( pStr );
    if ( Wlc_PrsFindSymbol( pStr, ':' ) == NULL )
    {
        pStr = Wlc_PrsFindSymbol( pStr, ']' );
        if ( pStr == NULL )
            return NULL;
    }
    else
    {
        pStr = Wlc_PrsFindSymbol( pStr, ':' );
        pStr = Wlc_PrsSkipSpaces( pStr+1 );
488
        if ( !Wlc_PrsIsDigit(pStr) && pStr[0] != '-' )
489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509
            return NULL;
        *Beg = atoi( pStr );
        pStr = Wlc_PrsFindSymbol( pStr, ']' );
        if ( pStr == NULL )
            return NULL;
    }
    return pStr + 1;
}
static inline char * Wlc_PrsFindWord( char * pStr, char * pWord, int * fFound )
{
    *fFound = 0;
    pStr = Wlc_PrsSkipSpaces( pStr );
    if ( !Wlc_PrsStrCmp(pStr, pWord) )
        return pStr;
    *fFound = 1;
    return pStr + strlen(pWord);
}
static inline char * Wlc_PrsFindName( char * pStr, char ** ppPlace )
{
    static char Buffer[WLV_PRS_MAX_LINE];
    char * pThis = *ppPlace = Buffer;
510
    int fNotName = 1;
511 512 513
    pStr = Wlc_PrsSkipSpaces( pStr );
    if ( !Wlc_PrsIsChar(pStr) )
        return NULL;
514 515 516 517 518 519 520 521 522 523
//    while ( Wlc_PrsIsChar(pStr) )
//        *pThis++ = *pStr++;
    while ( *pStr )
    {
        if ( fNotName && !Wlc_PrsIsChar(pStr) )
            break;
        if ( *pStr == '\\' )
            fNotName = 0;
        else if ( !fNotName && *pStr == ' ' )
            fNotName = 1;
524
        *pThis++ = *pStr++;
525
    }
526 527 528
    *pThis = 0;
    return pStr;
}
529
static inline char * Wlc_PrsReadConstant( Wlc_Prs_t * p, char * pStr, Vec_Int_t * vFanins, int * pRange, int * pSigned, int * pXValue )
530
{
531
    int i, nDigits, nBits = atoi( pStr );
532 533
    *pRange = -1;
    *pSigned = 0;
534
    *pXValue = 0;
535 536 537 538 539
    pStr = Wlc_PrsSkipSpaces( pStr );
    if ( Wlc_PrsFindSymbol( pStr, '\'' ) == NULL )
    {
        // handle decimal number
        int Number = atoi( pStr );
540 541
        *pRange = Abc_Base2Log( Number+1 );
        assert( *pRange < 32 );
542 543
        while ( Wlc_PrsIsDigit(pStr) )
            pStr++;
544
        Vec_IntFill( vFanins, 1, Number );
545 546 547 548 549 550 551 552
        return pStr;
    }
    pStr = Wlc_PrsFindSymbol( pStr, '\'' );
    if ( pStr[1] == 's' )
    {
        *pSigned = 1;
        pStr++;
    }
553 554 555 556 557
    if ( pStr[1] == 'b' )
    {
        Vec_IntFill( vFanins, Abc_BitWordNum(nBits), 0 );
        for ( i = 0; i < nBits; i++ )
            if ( pStr[2+i] == '1' )
558
                Abc_InfoSetBit( (unsigned *)Vec_IntArray(vFanins), nBits-1-i );
559 560 561 562 563 564
            else if ( pStr[2+i] != '0' )
                return (char *)(ABC_PTRINT_T)Wlc_PrsWriteErrorMessage( p, pStr, "Wrong digit in binary constant \"%c\".", pStr[2+i] );
        *pRange = nBits;
        pStr += 2 + nBits;
        return pStr;
    }
565 566
    if ( pStr[1] != 'h' )
        return (char *)(ABC_PTRINT_T)Wlc_PrsWriteErrorMessage( p, pStr, "Expecting hexadecimal constant and not \"%c\".", pStr[1] );
567
    *pXValue = (pStr[2] == 'x' || pStr[2] == 'X');
568 569 570 571 572
    Vec_IntFill( vFanins, Abc_BitWordNum(nBits), 0 );
    nDigits = Abc_TtReadHexNumber( (word *)Vec_IntArray(vFanins), pStr+2 );
    if ( nDigits != (nBits + 3)/4 )
    {
//        return (char *)(ABC_PTRINT_T)Wlc_PrsWriteErrorMessage( p, pStr, "The length of a constant does not match." );
573
//        printf( "Warning: The length of a constant (%d hex digits) does not match the number of bits (%d).\n", nDigits, nBits );
574 575 576 577 578 579 580
    }
    *pRange = nBits;
    pStr += 2;
    while ( Wlc_PrsIsChar(pStr) )
        pStr++;
    return pStr;
}
581 582
static inline char * Wlc_PrsReadName( Wlc_Prs_t * p, char * pStr, Vec_Int_t * vFanins )
{
583 584 585 586 587
    int NameId, fFound, iObj;
    pStr = Wlc_PrsSkipSpaces( pStr );
    if ( Wlc_PrsIsDigit(pStr) )
    {
        char Buffer[100];
588
        int Range, Signed, XValue = 0;
589
        Vec_Int_t * vFanins = Vec_IntAlloc(0);
590
        pStr = Wlc_PrsReadConstant( p, pStr, vFanins, &Range, &Signed, &XValue );
591 592 593 594 595 596 597 598
        if ( pStr == NULL )
        {
            Vec_IntFree( vFanins );
            return 0;
        }
        // create new node
        iObj = Wlc_ObjAlloc( p->pNtk, WLC_OBJ_CONST, Signed, Range-1, 0 );
        Wlc_ObjAddFanins( p->pNtk, Wlc_NtkObj(p->pNtk, iObj), vFanins );
599
        Wlc_NtkObj(p->pNtk, iObj)->fXConst = XValue;
600 601
        Vec_IntFree( vFanins );
        // add node's name
602
        sprintf( Buffer, "_c%d_", p->nConsts++ );
603 604 605 606 607 608 609 610 611 612
        NameId = Abc_NamStrFindOrAdd( p->pNtk->pManName, Buffer, &fFound );
        if ( fFound )
            return (char *)(ABC_PTRINT_T)Wlc_PrsWriteErrorMessage( p, pStr, "Name %s is already used.", Buffer );
        assert( iObj == NameId );
    }
    else
    {
        char * pName;
        pStr = Wlc_PrsFindName( pStr, &pName );
        if ( pStr == NULL )
613
            return (char *)(ABC_PTRINT_T)Wlc_PrsWriteErrorMessage( p, pStr, "Cannot read name in assign-statement." );
614 615 616 617
        NameId = Abc_NamStrFindOrAdd( p->pNtk->pManName, pName, &fFound );
        if ( !fFound )
            return (char *)(ABC_PTRINT_T)Wlc_PrsWriteErrorMessage( p, pStr, "Name %s is used but not declared.", pName );
    }
618 619 620
    Vec_IntPush( vFanins, NameId );
    return Wlc_PrsSkipSpaces( pStr );
}
621
static inline int Wlc_PrsFindDefinition( Wlc_Prs_t * p, char * pStr, Vec_Int_t * vFanins, int * pXValue )
622 623 624
{
    char * pName;
    int Type = WLC_OBJ_NONE;
625
    int fRotating = 0;
626 627 628 629 630 631 632
    Vec_IntClear( vFanins );
    pStr = Wlc_PrsSkipSpaces( pStr );
    if ( pStr[0] != '=' )
        return 0;
    pStr = Wlc_PrsSkipSpaces( pStr+1 );
    if ( pStr[0] == '(' )
    {
633 634 635 636
        // consider rotating shifter
        if ( Wlc_PrsFindSymbolTwo(pStr, '>', '>') && Wlc_PrsFindSymbolTwo(pStr, '<', '<') )
        {
            // THIS IS A HACK TO DETECT rotating shifters
637
            char * pClose = Wlc_PrsFindClosingParenthesis( pStr, '(', ')' );
638
            if ( pClose == NULL )
639
                return Wlc_PrsWriteErrorMessage( p, pStr, "Expecting closing parenthesis." );
640 641 642 643 644 645
            *pStr = ' '; *pClose = 0;
            pStr = Wlc_PrsSkipSpaces( pStr );
            fRotating = 1;
        }
        else
        {
646
            char * pClose = Wlc_PrsFindClosingParenthesis( pStr, '(', ')' );
647
            if ( pClose == NULL )
648
                return Wlc_PrsWriteErrorMessage( p, pStr, "Expecting closing parenthesis." );
649 650 651
            *pStr = *pClose = ' ';
            pStr = Wlc_PrsSkipSpaces( pStr );
        }
652 653 654
    }
    if ( Wlc_PrsIsDigit(pStr) )
    {
655
        int Range, Signed;
656
        pStr = Wlc_PrsReadConstant( p, pStr, vFanins, &Range, &Signed, pXValue );
657
        if ( pStr == NULL )
658
            return 0;
659 660
        Type = WLC_OBJ_CONST;
    }
661
    else if ( pStr[0] == '!' || (pStr[0] == '~' && pStr[1] != '&' && pStr[1] != '|' && pStr[1] != '^') || pStr[0] == '@' || pStr[0] == '#' )
662 663 664
    {
        if ( pStr[0] == '!' )
            Type = WLC_OBJ_LOGIC_NOT;
665
        else if ( pStr[0] == '~' )
666
            Type = WLC_OBJ_BIT_NOT;
667 668
        else if ( pStr[0] == '@' )
            Type = WLC_OBJ_ARI_SQRT;
669 670
        else if ( pStr[0] == '#' )
            Type = WLC_OBJ_ARI_SQUARE;
671
        else assert( 0 );
672
        // skip parentheses
673 674 675
        pStr = Wlc_PrsSkipSpaces( pStr+1 );
        if ( pStr[0] == '(' )
        {
676
            char * pClose = Wlc_PrsFindClosingParenthesis( pStr, '(', ')' );
677
            if ( pClose == NULL )
678
                return Wlc_PrsWriteErrorMessage( p, pStr, "Expecting closing parenthesis." );
679 680 681
            *pStr = *pClose = ' ';
        }
        if ( !(pStr = Wlc_PrsReadName(p, pStr, vFanins)) )
682 683
            return Wlc_PrsWriteErrorMessage( p, pStr, "Cannot read name after !." );
    }
684 685 686 687 688
    else if ( pStr[0] == '-' || 
              pStr[0] == '&' || pStr[0] == '|' || pStr[0] == '^' || 
              (pStr[0] == '~' && pStr[1] == '&') || 
              (pStr[0] == '~' && pStr[1] == '|') || 
              (pStr[0] == '~' && pStr[1] == '^') )
689
    {
690
        int shift = 1;
691 692 693
        if ( pStr[0] == '-' )
            Type = WLC_OBJ_ARI_MINUS;
        else if ( pStr[0] == '&' )
694 695 696 697 698
            Type = WLC_OBJ_REDUCT_AND;
        else if ( pStr[0] == '|' )
            Type = WLC_OBJ_REDUCT_OR;
        else if ( pStr[0] == '^' )
            Type = WLC_OBJ_REDUCT_XOR;
699
        else if ( pStr[0] == '~' && pStr[1] == '&' )
700
            {Type = WLC_OBJ_REDUCT_NAND; shift = 2;}
701
        else if ( pStr[0] == '~' && pStr[1] == '|' )
702
            {Type = WLC_OBJ_REDUCT_NOR; shift = 2;}
703
        else if ( pStr[0] == '~' && pStr[1] == '^' )
704
            {Type = WLC_OBJ_REDUCT_NXOR; shift = 2;}
705
        else assert( 0 );
706
        if ( !(pStr = Wlc_PrsReadName(p, pStr+shift, vFanins)) )
707
            return Wlc_PrsWriteErrorMessage( p, pStr, "Cannot read name after a unary operator." );
708 709 710
    }
    else if ( pStr[0] == '{' )
    {
711
        // THIS IS A HACK TO DETECT zero padding AND sign extension
712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730
        if ( Wlc_PrsFindSymbol(pStr+1, '{') )
        {
            if ( Wlc_PrsFindSymbol(pStr+1, '\'') )
                Type = WLC_OBJ_BIT_ZEROPAD;
            else
                Type = WLC_OBJ_BIT_SIGNEXT;
            pStr = Wlc_PrsFindSymbol(pStr+1, ',');
            if ( pStr == NULL )
                return Wlc_PrsWriteErrorMessage( p, pStr, "Expecting one comma in this line." );
            if ( !(pStr = Wlc_PrsReadName(p, pStr+1, vFanins)) )
                return Wlc_PrsWriteErrorMessage( p, pStr, "Cannot read name in sign-extension." );
            pStr = Wlc_PrsSkipSpaces( pStr );
            if ( pStr[0] != '}' )
                return Wlc_PrsWriteErrorMessage( p, pStr, "There is no closing brace (})." );
        }
        else // concatenation
        {
            while ( 1 )
            {
731 732
                pStr = Wlc_PrsSkipSpaces( pStr+1 );
                if ( !(pStr = Wlc_PrsReadName(p, pStr, vFanins)) )
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
                    return Wlc_PrsWriteErrorMessage( p, pStr, "Cannot read name in concatenation." );
                if ( pStr[0] == '}' )
                    break;
                if ( pStr[0] != ',' )
                    return Wlc_PrsWriteErrorMessage( p, pStr, "Expected comma (,) in this place." );
            }
            Type = WLC_OBJ_BIT_CONCAT;
        }
        assert( pStr[0] == '}' );
        pStr++;
    }
    else
    {
        if ( !(pStr = Wlc_PrsReadName(p, pStr, vFanins)) )
            return 0;
        // get the next symbol
        if ( pStr[0] == 0 )
            Type = WLC_OBJ_BUF;
        else if ( pStr[0] == '?' )
        {
            if ( !(pStr = Wlc_PrsReadName(p, pStr+1, vFanins)) )
                return Wlc_PrsWriteErrorMessage( p, pStr, "Cannot read name in MUX." );
            if ( pStr[0] != ':' )
                return Wlc_PrsWriteErrorMessage( p, pStr, "MUX lacks the colon symbol (:)." );
            if ( !(pStr = Wlc_PrsReadName(p, pStr+1, vFanins)) )
                return Wlc_PrsWriteErrorMessage( p, pStr, "Cannot read name in MUX." );
759 760
            assert( Vec_IntSize(vFanins) == 3 );
            ABC_SWAP( int, Vec_IntArray(vFanins)[1], Vec_IntArray(vFanins)[2] );
761 762 763 764
            Type = WLC_OBJ_MUX;
        }
        else if ( pStr[0] == '[' )
        {
765
            int End, Beg; char * pLine = pStr;
766
            pStr = Wlc_PrsFindRange( pStr, &End, &Beg );
767 768
            if ( pStr == NULL )
                return Wlc_PrsWriteErrorMessage( p, pLine, "Non-standard range." );
769
            Vec_IntPushTwo( vFanins, End, Beg );
770 771 772 773
            Type = WLC_OBJ_BIT_SELECT;
        }
        else 
        {
774
                 if ( pStr[0] == '>' && pStr[1] == '>' && pStr[2] != '>' ) pStr += 2, Type = fRotating ? WLC_OBJ_ROTATE_R : WLC_OBJ_SHIFT_R;
775
            else if ( pStr[0] == '>' && pStr[1] == '>' && pStr[2] == '>' ) pStr += 3, Type = WLC_OBJ_SHIFT_RA;      
776
            else if ( pStr[0] == '<' && pStr[1] == '<' && pStr[2] != '<' ) pStr += 2, Type = fRotating ? WLC_OBJ_ROTATE_L : WLC_OBJ_SHIFT_L;
777 778 779 780
            else if ( pStr[0] == '<' && pStr[1] == '<' && pStr[2] == '<' ) pStr += 3, Type = WLC_OBJ_SHIFT_LA;      
            else if ( pStr[0] == '&' && pStr[1] != '&'                   ) pStr += 1, Type = WLC_OBJ_BIT_AND;       
            else if ( pStr[0] == '|' && pStr[1] != '|'                   ) pStr += 1, Type = WLC_OBJ_BIT_OR;        
            else if ( pStr[0] == '^' && pStr[1] != '^'                   ) pStr += 1, Type = WLC_OBJ_BIT_XOR;       
781 782
            else if ( pStr[0] == '~' && pStr[1] == '&'                   ) pStr += 2, Type = WLC_OBJ_BIT_NAND;       
            else if ( pStr[0] == '~' && pStr[1] == '|'                   ) pStr += 2, Type = WLC_OBJ_BIT_NOR;       
783
            else if ( pStr[0] == '~' && pStr[1] == '^'                   ) pStr += 2, Type = WLC_OBJ_BIT_NXOR;       
784
            else if ( pStr[0] == '=' && pStr[1] == '>'                   ) pStr += 2, Type = WLC_OBJ_LOGIC_IMPL;     
785 786
            else if ( pStr[0] == '&' && pStr[1] == '&'                   ) pStr += 2, Type = WLC_OBJ_LOGIC_AND;     
            else if ( pStr[0] == '|' && pStr[1] == '|'                   ) pStr += 2, Type = WLC_OBJ_LOGIC_OR;      
787
            else if ( pStr[0] == '^' && pStr[1] == '^'                   ) pStr += 2, Type = WLC_OBJ_LOGIC_XOR;      
788
            else if ( pStr[0] == '=' && pStr[1] == '='                   ) pStr += 2, Type = WLC_OBJ_COMP_EQU;      
789
            else if ( pStr[0] == '!' && pStr[1] == '='                   ) pStr += 2, Type = WLC_OBJ_COMP_NOTEQU;      
790 791 792 793 794 795 796 797
            else if ( pStr[0] == '<' && pStr[1] != '='                   ) pStr += 1, Type = WLC_OBJ_COMP_LESS;     
            else if ( pStr[0] == '>' && pStr[1] != '='                   ) pStr += 1, Type = WLC_OBJ_COMP_MORE;     
            else if ( pStr[0] == '<' && pStr[1] == '='                   ) pStr += 2, Type = WLC_OBJ_COMP_LESSEQU;
            else if ( pStr[0] == '>' && pStr[1] == '='                   ) pStr += 2, Type = WLC_OBJ_COMP_MOREEQU;  
            else if ( pStr[0] == '+'                                     ) pStr += 1, Type = WLC_OBJ_ARI_ADD;       
            else if ( pStr[0] == '-'                                     ) pStr += 1, Type = WLC_OBJ_ARI_SUB;       
            else if ( pStr[0] == '*' && pStr[1] != '*'                   ) pStr += 1, Type = WLC_OBJ_ARI_MULTI;     
            else if ( pStr[0] == '/'                                     ) pStr += 1, Type = WLC_OBJ_ARI_DIVIDE;        
798
            else if ( pStr[0] == '%'                                     ) pStr += 1, Type = WLC_OBJ_ARI_REM;   
799 800 801 802
            else if ( pStr[0] == '*' && pStr[1] == '*'                   ) pStr += 2, Type = WLC_OBJ_ARI_POWER;
            else return Wlc_PrsWriteErrorMessage( p, pStr, "Unsupported operation (%c).", pStr[0] );
            if ( !(pStr = Wlc_PrsReadName(p, pStr+1, vFanins)) )
                return 0;
803 804 805
            pStr = Wlc_PrsSkipSpaces( pStr );
            if ( pStr[0] )
                printf( "Warning: Trailing symbols \"%s\" in line %d.\n", pStr, Wlc_PrsFindLine(p, pStr) );
806 807 808 809 810 811 812 813 814 815 816
        }
    }
    // make sure there is nothing left there
    if ( pStr )
    {
        pStr = Wlc_PrsFindName( pStr, &pName );
        if ( pStr != NULL )
            return Wlc_PrsWriteErrorMessage( p, pStr, "Name %s is left at the end of the line.", pName );   
    }
    return Type;
}
817 818
int Wlc_PrsReadDeclaration( Wlc_Prs_t * p, char * pStart )
{
819
    int fFound = 0, Type = WLC_OBJ_NONE, iObj; char * pLine;
820
    int Signed = 0, Beg = 0, End = 0, NameId, fIsPo = 0;
821
    if ( Wlc_PrsStrCmp( pStart, "input" ) )
822
        pStart += strlen("input"), Type = WLC_OBJ_PI;
823
    else if ( Wlc_PrsStrCmp( pStart, "output" ) )
824
        pStart += strlen("output"), fIsPo = 1;
825 826 827
    pStart = Wlc_PrsSkipSpaces( pStart );
    if ( Wlc_PrsStrCmp( pStart, "wire" ) )
        pStart += strlen("wire");
828 829
    else if ( Wlc_PrsStrCmp( pStart, "reg" ) )
        pStart += strlen("reg");
830 831 832
    // read 'signed'
    pStart = Wlc_PrsFindWord( pStart, "signed", &Signed );
    // read range
833
    pLine = pStart;
834 835
    pStart = Wlc_PrsFindRange( pStart, &End, &Beg );
    if ( pStart == NULL )
836
        return Wlc_PrsWriteErrorMessage( p, pLine, "Non-standard range." );
837 838 839 840 841 842 843 844 845 846
    if ( End != 0 && Beg != 0 )
    {
        if ( p->nNonZero[0]++ == 0 )
        {
            p->nNonZero[1] = End;
            p->nNonZero[2] = Beg;
            p->nNonZero[3] = Wlc_PrsFindLine(p, pStart);
        }
    }
    if ( End < 0 || Beg < 0  )
847
    {
848
        if ( p->nNegative[0]++ == 0 )
849
        {
850 851 852 853 854 855 856 857 858 859 860 861
            p->nNegative[1] = End;
            p->nNegative[2] = Beg;
            p->nNegative[3] = Wlc_PrsFindLine(p, pStart);
        }
    }
    if ( End < Beg )
    {
        if ( p->nReverse[0]++ == 0 )
        {
            p->nReverse[1] = End;
            p->nReverse[2] = Beg;
            p->nReverse[3] = Wlc_PrsFindLine(p, pStart);
862 863
        }
    }
864 865
    while ( 1 )
    {
866
        char * pName; int XValue;
867 868 869
        // read name
        pStart = Wlc_PrsFindName( pStart, &pName );
        if ( pStart == NULL )
870
            return Wlc_PrsWriteErrorMessage( p, pStart, "Cannot read name in declaration." );
871 872 873 874
        NameId = Abc_NamStrFindOrAdd( p->pNtk->pManName, pName, &fFound );
        if ( fFound )
            return Wlc_PrsWriteErrorMessage( p, pStart, "Name %s is declared more than once.", pName );
        iObj = Wlc_ObjAlloc( p->pNtk, Type, Signed, End, Beg );
875
        if ( fIsPo ) Wlc_ObjSetCo( p->pNtk, Wlc_NtkObj(p->pNtk, iObj), 0 );
876 877 878 879 880 881 882 883 884
        assert( iObj == NameId );
        // check next definition
        pStart = Wlc_PrsSkipSpaces( pStart );
        if ( pStart[0] == ',' )
        {
            pStart++;
            continue;
        }
        // check definition
885
        Type = Wlc_PrsFindDefinition( p, pStart, p->vFanins, &XValue );
886 887 888 889 890
        if ( Type )
        {
            Wlc_Obj_t * pObj = Wlc_NtkObj( p->pNtk, iObj );
            Wlc_ObjUpdateType( p->pNtk, pObj, Type );
            Wlc_ObjAddFanins( p->pNtk, pObj, p->vFanins );
891
            pObj->fXConst = XValue;
892 893 894 895 896
        }
        break;
    }
    return 1;
}
897 898
int Wlc_PrsDerive( Wlc_Prs_t * p )
{
899
    Wlc_Obj_t * pObj;
900 901 902 903 904
    char * pStart, * pName;
    int i;
    // go through the directives
    Wlc_PrsForEachLine( p, pStart, i )
    {
905
startword:
906 907 908
        if ( Wlc_PrsStrCmp( pStart, "module" ) )
        {
            // get module name
909
            pName = Wlc_PrsStrtok( pStart + strlen("module"), " \r\n\t(,)" );
910 911
            if ( pName == NULL )
                return Wlc_PrsWriteErrorMessage( p, pStart, "Cannot read model name." );
912 913 914 915 916 917 918 919 920 921 922 923
            // THIS IS A HACK to skip definitions of modules beginning with "CPL_"
            if ( Wlc_PrsStrCmp( pName, "CPL_" ) )
            {
                while ( ++i < Vec_IntSize(p->vStarts) )
                {
                    pStart = Wlc_PrsStr(p, Vec_IntEntry(p->vStarts, i));
                    pStart = strstr( pStart, "endmodule" );
                    if ( pStart != NULL )
                        break;
                }
                continue;
            }
924 925
            if ( Wlc_PrsStrCmp( pName, "table" ) )
            {
926
                // THIS IS A HACK to detect table module descriptions
927 928 929
                int Width1 = -1, Width2 = -1;
                int v, b, Value, nBits, nInts;
                unsigned * pTable;
930 931 932 933 934 935 936 937
                Vec_Int_t * vValues = Vec_IntAlloc( 256 );
                Wlc_PrsForEachLineStart( p, pStart, i, i+1 )
                {
                    if ( Wlc_PrsStrCmp( pStart, "endcase" ) )
                        break;
                    pStart = Wlc_PrsFindSymbol( pStart, '\'' );
                    if ( pStart == NULL )
                        continue;
938
                    Width1 = atoi(pStart-1);
939 940 941
                    pStart = Wlc_PrsFindSymbol( pStart+2, '\'' );
                    if ( pStart == NULL )
                        continue;
942
                    Width2 = atoi(pStart-1);
943 944 945 946 947 948 949 950 951 952 953
                    Value = 0;
                    Abc_TtReadHexNumber( (word *)&Value, pStart+2 );
                    Vec_IntPush( vValues, Value );
                }
                //Vec_IntPrint( vValues );
                nBits = Abc_Base2Log( Vec_IntSize(vValues) );
                if ( Vec_IntSize(vValues) != (1 << nBits) )
                {
                    Vec_IntFree( vValues );
                    return Wlc_PrsWriteErrorMessage( p, pStart, "Cannot read module \"%s\".", pName );
                }
954
                assert( Width1 == nBits );
955
                // create bitmap
956
                nInts = Abc_BitWordNum( Width2 * Vec_IntSize(vValues) );
957 958 959
                pTable = (unsigned *)Mem_FlexEntryFetch( p->pMemTable, nInts * sizeof(unsigned) );
                memset( pTable, 0, nInts * sizeof(unsigned) );
                Vec_IntForEachEntry( vValues, Value, v )
960
                    for ( b = 0; b < Width2; b++ )
961
                        if ( (Value >> b) & 1 )
962
                            Abc_InfoSetBit( pTable, v * Width2 + b );
963 964 965 966
                Vec_PtrPush( p->vTables, pTable );
                Vec_IntFree( vValues );
                continue;
            }
967 968 969 970
            if ( p->pNtk != NULL )
                return Wlc_PrsWriteErrorMessage( p, pStart, "Network is already defined." );
            p->pNtk = Wlc_NtkAlloc( pName, Vec_IntSize(p->vStarts) );
            p->pNtk->pManName = Abc_NamStart( Vec_IntSize(p->vStarts), 20 );
971 972 973
            p->pNtk->pMemTable = p->pMemTable; p->pMemTable = NULL;
            p->pNtk->vTables = p->vTables; p->vTables = NULL;
            // read the argument definitions
974
            while ( (pName = Wlc_PrsStrtok( NULL, "(,)" )) )
975 976 977 978 979 980 981 982
            {
                pName = Wlc_PrsSkipSpaces( pName );
                if ( Wlc_PrsStrCmp( pName, "input" ) || Wlc_PrsStrCmp( pName, "output" ) || Wlc_PrsStrCmp( pName, "wire" ) )
                {
                    if ( !Wlc_PrsReadDeclaration( p, pName ) )
                        return 0;
                }
            }
983 984 985 986 987 988
        }
        else if ( Wlc_PrsStrCmp( pStart, "endmodule" ) )
        {
            Vec_Int_t * vTemp = Vec_IntStartNatural( Wlc_NtkObjNumMax(p->pNtk) );
            Vec_IntAppend( &p->pNtk->vNameIds, vTemp );
            Vec_IntFree( vTemp );
989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004
            if ( p->pNtk->vInits )
            {
                // move FO/FI to be part of CI/CO
                assert( (Vec_IntSize(&p->pNtk->vFfs) & 1) == 0 );
                assert( Vec_IntSize(&p->pNtk->vFfs) == 2 * Vec_IntSize(p->pNtk->vInits) );
                Wlc_NtkForEachFf( p->pNtk, pObj, i )
                    if ( i & 1 )
                        Wlc_ObjSetCo( p->pNtk, pObj, 1 );
                    else
                        Wlc_ObjSetCi( p->pNtk, pObj );
                Vec_IntClear( &p->pNtk->vFfs );
                // convert init values into binary string
                //Vec_IntPrint( &p->pNtk->vInits );
                p->pNtk->pInits = Wlc_PrsConvertInitValues( p->pNtk );
                //printf( "%s", p->pNtk->pInits );
            }
1005 1006 1007
            break;
        }
        // these are read as part of the interface
1008
        else if ( Wlc_PrsStrCmp( pStart, "input" ) || Wlc_PrsStrCmp( pStart, "output" ) || Wlc_PrsStrCmp( pStart, "wire" ) || Wlc_PrsStrCmp( pStart, "reg" ) )
1009
        {
1010 1011
            if ( !Wlc_PrsReadDeclaration( p, pStart ) )
                return 0;
1012 1013 1014
        }
        else if ( Wlc_PrsStrCmp( pStart, "assign" ) )
        {
1015
            int Type, NameId, fFound, XValue = 0;
1016 1017 1018 1019 1020 1021 1022 1023 1024
            pStart += strlen("assign");
            // read name
            pStart = Wlc_PrsFindName( pStart, &pName );
            if ( pStart == NULL )
                return Wlc_PrsWriteErrorMessage( p, pStart, "Cannot read name after assign." );
            NameId = Abc_NamStrFindOrAdd( p->pNtk->pManName, pName, &fFound );
            if ( !fFound )
                return Wlc_PrsWriteErrorMessage( p, pStart, "Name %s is not declared.", pName );
            // read definition
1025
            Type = Wlc_PrsFindDefinition( p, pStart, p->vFanins, &XValue );
1026 1027
            if ( Type )
            {
1028
                pObj = Wlc_NtkObj( p->pNtk, NameId );
1029 1030
                Wlc_ObjUpdateType( p->pNtk, pObj, Type );
                Wlc_ObjAddFanins( p->pNtk, pObj, p->vFanins );
1031
                pObj->fXConst = XValue;
1032 1033 1034 1035
            }
            else
                return 0;
        }
1036 1037
        else if ( Wlc_PrsStrCmp( pStart, "table" ) )
        {
1038
            // THIS IS A HACK to detect tables
1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065
            int NameId, fFound, iTable = atoi( pStart + strlen("table") );
            // find opening
            pStart = Wlc_PrsFindSymbol( pStart, '(' );
            if ( pStart == NULL )
                return Wlc_PrsWriteErrorMessage( p, pStart, "Cannot read table." );
            // read input
            pStart = Wlc_PrsFindName( pStart+1, &pName );
            if ( pStart == NULL )
                return Wlc_PrsWriteErrorMessage( p, pStart, "Cannot read name after assign." );
            NameId = Abc_NamStrFindOrAdd( p->pNtk->pManName, pName, &fFound );
            if ( !fFound )
                return Wlc_PrsWriteErrorMessage( p, pStart, "Name %s is not declared.", pName );
            // save inputs
            Vec_IntClear( p->vFanins );
            Vec_IntPush( p->vFanins, NameId );
            Vec_IntPush( p->vFanins, iTable );
            // find comma
            pStart = Wlc_PrsFindSymbol( pStart, ',' );
            if ( pStart == NULL )
                return Wlc_PrsWriteErrorMessage( p, pStart, "Cannot read table." );
            // read output
            pStart = Wlc_PrsFindName( pStart+1, &pName );
            if ( pStart == NULL )
                return Wlc_PrsWriteErrorMessage( p, pStart, "Cannot read name after assign." );
            NameId = Abc_NamStrFindOrAdd( p->pNtk->pManName, pName, &fFound );
            if ( !fFound )
                return Wlc_PrsWriteErrorMessage( p, pStart, "Name %s is not declared.", pName );
1066 1067 1068
            pObj = Wlc_NtkObj( p->pNtk, NameId );
            Wlc_ObjUpdateType( p->pNtk, pObj, WLC_OBJ_TABLE );
            Wlc_ObjAddFanins( p->pNtk, pObj, p->vFanins );
1069
        }
1070 1071
        else if ( Wlc_PrsStrCmp( pStart, "always" ) )
        {
1072
            // THIS IS A HACK to detect always statement representing combinational MUX
1073
            int NameId, NameIdOut = -1, fFound, nValues, fDefaultFound = 0;
1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093
            // find control
            pStart = Wlc_PrsFindWord( pStart, "case", &fFound );
            if ( pStart == NULL )
                return Wlc_PrsWriteErrorMessage( p, pStart, "Cannot read case statement." );
            // read the name
            pStart = Wlc_PrsFindSymbol( pStart, '(' );
            if ( pStart == NULL )
                return Wlc_PrsWriteErrorMessage( p, pStart, "Cannot read table." );
            pStart = Wlc_PrsFindSymbol( pStart+1, '(' );
            if ( pStart == NULL )
                return Wlc_PrsWriteErrorMessage( p, pStart, "Cannot read table." );
            pStart = Wlc_PrsFindName( pStart+1, &pName );
            if ( pStart == NULL )
                return Wlc_PrsWriteErrorMessage( p, pStart, "Cannot read name after case." );
            NameId = Abc_NamStrFindOrAdd( p->pNtk->pManName, pName, &fFound );
            if ( !fFound )
                return Wlc_PrsWriteErrorMessage( p, pStart, "Name %s is not declared.", pName );
            Vec_IntClear( p->vFanins );
            Vec_IntPush( p->vFanins, NameId );
            // read data inputs
1094 1095 1096 1097 1098
            pObj = Wlc_NtkObj( p->pNtk, NameId );
            if ( pObj == NULL )
                return Wlc_PrsWriteErrorMessage( p, pStart, "Cannot find the object in case statement." );
            // remember the number of values
            nValues = (1 << Wlc_ObjRange(pObj));
1099
            while ( 1 )
1100
            {                
1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120
                // find opening
                pStart = Wlc_PrsFindSymbol( pStart, ':' );
                if ( pStart == NULL )
                    return Wlc_PrsWriteErrorMessage( p, pStart, "Cannot find colon in the case statement." );
                // find output name
                pStart = Wlc_PrsFindName( pStart+1, &pName );
                if ( pStart == NULL )
                    return Wlc_PrsWriteErrorMessage( p, pStart, "Cannot read name after case." );
                NameIdOut = Abc_NamStrFindOrAdd( p->pNtk->pManName, pName, &fFound );
                if ( !fFound )
                    return Wlc_PrsWriteErrorMessage( p, pStart, "Name %s is not declared.", pName );
                // find equality
                pStart = Wlc_PrsFindSymbol( pStart, '=' );
                if ( pStart == NULL )
                    return Wlc_PrsWriteErrorMessage( p, pStart, "Cannot find equality in the case statement." );
                // find input name
                pStart = Wlc_PrsSkipSpaces( pStart+1 );
                pStart = Wlc_PrsReadName( p, pStart, p->vFanins );
                if ( pStart == NULL )
                    return Wlc_PrsWriteErrorMessage( p, pStart, "Cannot read name inside case statement." );
1121 1122 1123 1124
                // consider default
                if ( fDefaultFound )
                {
                    int EntryLast = Vec_IntEntryLast( p->vFanins );
1125 1126 1127 1128
                    if (nValues != Vec_IntSize(p->vFanins)-2)
                        Vec_IntFillExtra( p->vFanins, nValues + 1, EntryLast );
                    else
                        Vec_IntPop(p->vFanins);
1129 1130 1131 1132 1133
                    // get next line and check its opening character
                    pStart = Wlc_PrsStr(p, Vec_IntEntry(p->vStarts, ++i));
                    pStart = Wlc_PrsSkipSpaces( pStart );
                }
                else
1134
                {
1135
                    // get next line and check its opening character
1136
                    pStart = Wlc_PrsStr(p, Vec_IntEntry(p->vStarts, ++i));
1137 1138 1139 1140 1141 1142 1143 1144
                    pStart = Wlc_PrsSkipSpaces( pStart );
                    if ( Wlc_PrsIsDigit(pStart) )
                        continue;
                    if ( Wlc_PrsStrCmp( pStart, "default" ) )
                    {
                        fDefaultFound = 1;
                        continue;
                    }
1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157
                }
                // find closing
                pStart = Wlc_PrsFindWord( pStart, "endcase", &fFound );
                if ( pStart == NULL )
                    return Wlc_PrsWriteErrorMessage( p, pStart, "Cannot read case statement." );
                // find closing
                pStart = Wlc_PrsFindWord( pStart, "end", &fFound );
                if ( pStart == NULL )
                    return Wlc_PrsWriteErrorMessage( p, pStart, "Cannot read case statement." );
                pStart = Wlc_PrsSkipSpaces( pStart );
                break;
            }
            // check range of the control
1158
            if ( nValues != Vec_IntSize(p->vFanins) - 1 )
1159
                return Wlc_PrsWriteErrorMessage( p, pStart, "The number of values in the case statement is wrong.", pName );
1160
            if ( Wlc_ObjRange(pObj) == 1 )
1161 1162 1163 1164
            {
//                return Wlc_PrsWriteErrorMessage( p, pStart, "Always-statement with 1-bit control is not bit-blasted correctly.", pName );
                printf( "Warning:  Case-statement with 1-bit control is treated as a 2:1 MUX (correct for unsigned signals only).\n" );
            }
1165 1166 1167 1168 1169 1170 1171
            pObj = Wlc_NtkObj( p->pNtk, NameIdOut );
            Wlc_ObjUpdateType( p->pNtk, pObj, WLC_OBJ_MUX );
            Wlc_ObjAddFanins( p->pNtk, pObj, p->vFanins );
            goto startword;
        }
        else if ( Wlc_PrsStrCmp( pStart, "CPL_FF" ) )
        {
1172
            int NameId = -1, NameIdIn = -1, NameIdOut = -1, fFound, nBits = 1, fFlopIn, fFlopOut;
1173 1174 1175 1176 1177
            pStart += strlen("CPL_FF");
            if ( pStart[0] == '#' )
                nBits = atoi(pStart+1);
            // read names
            while ( 1 )
1178
            {
1179 1180 1181 1182
                pStart = Wlc_PrsFindSymbol( pStart, '.' );
                if ( pStart == NULL )
                    break;
                pStart = Wlc_PrsSkipSpaces( pStart+1 );
1183
                if ( pStart[0] != 'd' && (pStart[0] != 'q' || pStart[1] == 'b') && strncmp(pStart, "arstval", 7) )
1184
                    continue;
1185 1186
                fFlopIn = (pStart[0] == 'd');
                fFlopOut = (pStart[0] == 'q');
1187 1188
                pStart = Wlc_PrsFindSymbol( pStart, '(' );
                if ( pStart == NULL )
1189
                    return Wlc_PrsWriteErrorMessage( p, pStart, "Cannot read opening parenthesis in the flop description." );
1190 1191 1192
                pStart = Wlc_PrsFindName( pStart+1, &pName );
                if ( pStart == NULL )
                    return Wlc_PrsWriteErrorMessage( p, pStart, "Cannot read name inside flop description." );
1193
                if ( fFlopIn )
1194
                    NameIdIn = Abc_NamStrFindOrAdd( p->pNtk->pManName, pName, &fFound );
1195 1196
                else if ( fFlopOut ) 
                    NameIdOut = Abc_NamStrFindOrAdd( p->pNtk->pManName, pName, &fFound );
1197
                else
1198 1199 1200
                    NameId = Abc_NamStrFindOrAdd( p->pNtk->pManName, pName, &fFound );
                if ( !fFound )
                    return Wlc_PrsWriteErrorMessage( p, pStart, "Name %s is not declared.", pName );
1201
            }
1202
            if ( NameIdIn == -1 || NameIdOut == -1 )
1203 1204
                return Wlc_PrsWriteErrorMessage( p, pStart, "Name of flop input or flop output is missing." );
            // create flop output
1205
            pObj = Wlc_NtkObj( p->pNtk, NameIdOut );
1206
            Wlc_ObjUpdateType( p->pNtk, pObj, WLC_OBJ_FO );
1207
            Vec_IntPush( &p->pNtk->vFfs, NameIdOut );
1208
            if ( nBits != Wlc_ObjRange(pObj) )
1209
                printf( "Warning!  Flop input has bit-width (%d) that differs from the flop declaration (%d)\n", Wlc_ObjRange(pObj), nBits );
1210
            // create flop input
1211 1212
            pObj = Wlc_NtkObj( p->pNtk, NameIdIn );
            Vec_IntPush( &p->pNtk->vFfs, NameIdIn );
1213
            if ( nBits != Wlc_ObjRange(pObj) )
1214
                printf( "Warning!  Flop output has bit-width (%d) that differs from the flop declaration (%d)\n", Wlc_ObjRange(pObj), nBits );
1215
            // save flop init value
1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226
            if ( NameId == -1 )
                printf( "Initial value of flop \"%s\" is not specified. Zero is assumed.\n", Abc_NamStr(p->pNtk->pManName, NameIdOut) );
            else
            {
                pObj = Wlc_NtkObj( p->pNtk, NameId );
                if ( nBits != Wlc_ObjRange(pObj) )
                    printf( "Warning!  Flop init signal bit-width (%d) is different from the flop declaration (%d)\n", Wlc_ObjRange(pObj), nBits );
            }
            if ( p->pNtk->vInits == NULL )
                p->pNtk->vInits = Vec_IntAlloc( 100 );
            Vec_IntPush( p->pNtk->vInits, NameId > 0 ? NameId : -nBits );
1227
        }
1228
        else if ( pStart[0] != '`' )
1229 1230 1231 1232 1233
        {
            pStart = Wlc_PrsFindName( pStart, &pName );
            return Wlc_PrsWriteErrorMessage( p, pStart, "Cannot read line beginning with %s.", pName );
        }
    }
1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244
    if ( p->nNonZero[0] )
    {
        printf( "Warning: Input file contains %d objects with non-zero-based ranges.\n", p->nNonZero[0] );
        printf( "For example, signal with range [%d:%d] is declared in line %d.\n", p->nNonZero[1], p->nNonZero[2], p->nNonZero[3] );
    }
    if ( p->nNegative[0] )
    {
        printf( "Warning: Input file contains %d objects with negative ranges.\n", p->nNegative[0] );
        printf( "For example, signal with range [%d:%d] is declared in line %d.\n", p->nNegative[1], p->nNegative[2], p->nNegative[3] );
    }
    if ( p->nReverse[0] )
1245
    {
1246 1247
        printf( "Warning: Input file contains %d objects with reversed ranges.\n", p->nReverse[0] );
        printf( "For example, signal with range [%d:%d] is declared in line %d.\n", p->nReverse[1], p->nReverse[2], p->nReverse[3] );
1248
    }
1249 1250
    return 1;
}
1251
Wlc_Ntk_t * Wlc_ReadVer( char * pFileName, char * pStr )
1252 1253 1254
{
    Wlc_Prs_t * p;
    Wlc_Ntk_t * pNtk = NULL;
1255 1256
    // either file name or string is given
    assert( (pFileName == NULL) != (pStr == NULL) );
1257
    // start the parser 
1258
    p = Wlc_PrsStart( pFileName, pStr );
1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269
    if ( p == NULL )
        return NULL;
    // detect lines
    if ( !Wlc_PrsPrepare( p ) )
        goto finish;
    // parse models
    if ( !Wlc_PrsDerive( p ) )
        goto finish;
    // derive topological order
    pNtk = Wlc_NtkDupDfs( p->pNtk );
    Wlc_NtkTransferNames( pNtk, p->pNtk );
1270
    pNtk->pSpec = Abc_UtilStrsav( pFileName );
1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290
finish:
    Wlc_PrsPrintErrorMessage( p );
    Wlc_PrsStop( p );
    return pNtk;
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Io_ReadWordTest( char * pFileName )
{
    Gia_Man_t * pNew;
1291
    Wlc_Ntk_t * pNtk = Wlc_ReadVer( pFileName, NULL );
1292 1293
    if ( pNtk == NULL )
        return;
1294
    Wlc_WriteVer( pNtk, "test.v", 0, 0 );
1295

1296
    pNew = Wlc_NtkBitBlast( pNtk, NULL, -1, 0, 0, 0, 0 );
1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309
    Gia_AigerWrite( pNew, "test.aig", 0, 0 );
    Gia_ManStop( pNew );

    Wlc_NtkFree( pNtk );
}

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


ABC_NAMESPACE_IMPL_END