amapParse.c 17.6 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
/**CFile****************************************************************

  FileName    [amapParse.c]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [Technology mapper for standard cells.]

  Synopsis    [Parses representations of gates.]

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

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

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

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

#include "amapInt.h"
22 23
#include "aig/hop/hop.h"
#include "bool/kit/kit.h"
24 25 26

ABC_NAMESPACE_IMPL_START

Alan Mishchenko committed
27 28 29 30 31 32

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

// the list of operation symbols to be used in expressions
33 34
#define AMAP_EQN_SYM_OPEN    '('   // opening parenthesis
#define AMAP_EQN_SYM_CLOSE   ')'   // closing parenthesis
Alan Mishchenko committed
35 36 37 38 39
#define AMAP_EQN_SYM_CONST0  '0'   // constant 0
#define AMAP_EQN_SYM_CONST1  '1'   // constant 1
#define AMAP_EQN_SYM_NEG     '!'   // negation before the variable
#define AMAP_EQN_SYM_NEGAFT  '\''  // negation after the variable
#define AMAP_EQN_SYM_AND     '*'   // logic AND
40
#define AMAP_EQN_SYM_AND2    '&'   // logic AND
Alan Mishchenko committed
41 42
#define AMAP_EQN_SYM_XOR     '^'   // logic XOR
#define AMAP_EQN_SYM_OR      '+'   // logic OR
Alan Mishchenko committed
43
#define AMAP_EQN_SYM_OR2     '|'   // logic OR
Alan Mishchenko committed
44 45 46 47 48 49

// the list of opcodes (also specifying operation precedence)
#define AMAP_EQN_OPER_NEG    10    // negation
#define AMAP_EQN_OPER_AND     9    // logic AND
#define AMAP_EQN_OPER_XOR     8    // logic XOR
#define AMAP_EQN_OPER_OR      7    // logic OR
50
#define AMAP_EQN_OPER_MARK    1    // OpStack token standing for an opening parenthesis
Alan Mishchenko committed
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

// these are values of the internal Flag
#define AMAP_EQN_FLAG_START   1    // after the opening parenthesis 
#define AMAP_EQN_FLAG_VAR     2    // after operation is received
#define AMAP_EQN_FLAG_OPER    3    // after operation symbol is received
#define AMAP_EQN_FLAG_ERROR   4    // when error is detected

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

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

  Synopsis    [Performs the operation on the top entries in the stack.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Hop_Obj_t * Amap_ParseFormulaOper( Hop_Man_t * pMan, Vec_Ptr_t * pStackFn, int Oper )
{
    Hop_Obj_t * gArg1, * gArg2, * gFunc;
    // perform the given operation
77 78
    gArg2 = (Hop_Obj_t *)Vec_PtrPop( pStackFn );
    gArg1 = (Hop_Obj_t *)Vec_PtrPop( pStackFn );
Alan Mishchenko committed
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
    if ( Oper == AMAP_EQN_OPER_AND )
        gFunc = Hop_And( pMan, gArg1, gArg2 );
    else if ( Oper == AMAP_EQN_OPER_OR )
        gFunc = Hop_Or( pMan, gArg1, gArg2 );
    else if ( Oper == AMAP_EQN_OPER_XOR )
        gFunc = Hop_Exor( pMan, gArg1, gArg2 );
    else
        return NULL;
//    Cudd_Ref( gFunc );
//    Cudd_RecursiveDeref( dd, gArg1 );
//    Cudd_RecursiveDeref( dd, gArg2 );
    Vec_PtrPush( pStackFn,  gFunc );
    return gFunc;
}

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

  Synopsis    [Derives the AIG corresponding to the equation.]

  Description [Takes the stream to output messages, the formula, the vector
  of variable names and the AIG manager.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
106
Hop_Obj_t * Amap_ParseFormula( FILE * pOutput, char * pFormInit, Vec_Ptr_t * vVarNames, Hop_Man_t * pMan, char * pGateName )
Alan Mishchenko committed
107 108 109 110 111 112 113 114 115 116
{
    char * pFormula;
    Vec_Ptr_t * pStackFn;
    Vec_Int_t * pStackOp;
    Hop_Obj_t * gFunc;
    char * pTemp, * pName;
    int nParans, fFound, Flag;
    int Oper, Oper1, Oper2;
    int i, v;

117
    // make sure that the number of opening and closing parentheses is the same
Alan Mishchenko committed
118 119 120 121 122 123 124 125
    nParans = 0;
    for ( pTemp = pFormInit; *pTemp; pTemp++ )
        if ( *pTemp == '(' )
            nParans++;
        else if ( *pTemp == ')' )
            nParans--;
    if ( nParans != 0 )
    {
126
        fprintf( pOutput, "Amap_ParseFormula(): Different number of opening and closing parentheses ().\n" );
Alan Mishchenko committed
127 128 129 130
        return NULL;
    }

    // copy the formula
Alan Mishchenko committed
131
    pFormula = ABC_ALLOC( char, strlen(pFormInit) + 3 );
Alan Mishchenko committed
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
    sprintf( pFormula, "(%s)", pFormInit );

    // start the stacks
    pStackFn = Vec_PtrAlloc( 100 );
    pStackOp = Vec_IntAlloc( 100 );

    Flag = AMAP_EQN_FLAG_START;
    for ( pTemp = pFormula; *pTemp; pTemp++ )
    {
        switch ( *pTemp )
        {
        // skip all spaces, tabs, and end-of-lines
        case ' ':
        case '\t':
        case '\r':
        case '\n':
            continue;
        case AMAP_EQN_SYM_CONST0:
            Vec_PtrPush( pStackFn, Hop_ManConst0(pMan) );  // Cudd_Ref( b0 );
            if ( Flag == AMAP_EQN_FLAG_VAR )
            {
                fprintf( pOutput, "Amap_ParseFormula(): No operation symbol before constant 0.\n" );
                Flag = AMAP_EQN_FLAG_ERROR; 
                break;
            }
            Flag = AMAP_EQN_FLAG_VAR; 
            break;
        case AMAP_EQN_SYM_CONST1:
            Vec_PtrPush( pStackFn, Hop_ManConst1(pMan) );  //  Cudd_Ref( b1 );
            if ( Flag == AMAP_EQN_FLAG_VAR )
            {
                fprintf( pOutput, "Amap_ParseFormula(): No operation symbol before constant 1.\n" );
                Flag = AMAP_EQN_FLAG_ERROR; 
                break;
            }
            Flag = AMAP_EQN_FLAG_VAR; 
            break;
        case AMAP_EQN_SYM_NEG:
            if ( Flag == AMAP_EQN_FLAG_VAR )
            {// if NEGBEF follows a variable, AND is assumed
                Vec_IntPush( pStackOp, AMAP_EQN_OPER_AND );
                Flag = AMAP_EQN_FLAG_OPER;
            }
            Vec_IntPush( pStackOp, AMAP_EQN_OPER_NEG );
            break;
        case AMAP_EQN_SYM_NEGAFT:
            if ( Flag != AMAP_EQN_FLAG_VAR )
            {// if there is no variable before NEGAFT, it is an error
                fprintf( pOutput, "Amap_ParseFormula(): No variable is specified before the negation suffix.\n" );
                Flag = AMAP_EQN_FLAG_ERROR; 
                break;
            }
            else // if ( Flag == PARSE_FLAG_VAR )
185
                Vec_PtrPush( pStackFn, Hop_Not( (Hop_Obj_t *)Vec_PtrPop(pStackFn) ) );
Alan Mishchenko committed
186 187
            break;
        case AMAP_EQN_SYM_AND:
188
        case AMAP_EQN_SYM_AND2:
Alan Mishchenko committed
189
        case AMAP_EQN_SYM_OR:
Alan Mishchenko committed
190
        case AMAP_EQN_SYM_OR2:
Alan Mishchenko committed
191 192 193 194 195 196 197
        case AMAP_EQN_SYM_XOR:
            if ( Flag != AMAP_EQN_FLAG_VAR )
            {
                fprintf( pOutput, "Amap_ParseFormula(): There is no variable before AND, EXOR, or OR.\n" );
                Flag = AMAP_EQN_FLAG_ERROR; 
                break;
            }
198
            if ( *pTemp == AMAP_EQN_SYM_AND || *pTemp == AMAP_EQN_SYM_AND2 )
Alan Mishchenko committed
199
                Vec_IntPush( pStackOp, AMAP_EQN_OPER_AND );
Alan Mishchenko committed
200
            else if ( *pTemp == AMAP_EQN_SYM_OR || *pTemp == AMAP_EQN_SYM_OR2 )
Alan Mishchenko committed
201 202 203 204 205 206 207 208
                Vec_IntPush( pStackOp, AMAP_EQN_OPER_OR );
            else //if ( *pTemp == AMAP_EQN_SYM_XOR )
                Vec_IntPush( pStackOp, AMAP_EQN_OPER_XOR );
            Flag = AMAP_EQN_FLAG_OPER; 
            break;
        case AMAP_EQN_SYM_OPEN:
            if ( Flag == AMAP_EQN_FLAG_VAR )
            {
Alan Mishchenko committed
209
                Vec_IntPush( pStackOp, AMAP_EQN_OPER_AND );
210
//                fprintf( pOutput, "Amap_ParseFormula(): An opening parenthesis follows a var without operation sign.\n" ); 
Alan Mishchenko committed
211 212
//                Flag = AMAP_EQN_FLAG_ERROR; 
//              break; 
Alan Mishchenko committed
213 214 215 216 217 218 219 220 221 222 223 224
            }
            Vec_IntPush( pStackOp, AMAP_EQN_OPER_MARK );
            // after an opening bracket, it feels like starting over again
            Flag = AMAP_EQN_FLAG_START; 
            break;
        case AMAP_EQN_SYM_CLOSE:
            if ( Vec_IntSize( pStackOp ) != 0 )
            {
                while ( 1 )
                {
                    if ( Vec_IntSize( pStackOp ) == 0 )
                    {
225
                        fprintf( pOutput, "Amap_ParseFormula(): There is no opening parenthesis\n" );
Alan Mishchenko committed
226 227 228 229 230 231 232 233 234 235 236
                        Flag = AMAP_EQN_FLAG_ERROR; 
                        break;
                    }
                    Oper = Vec_IntPop( pStackOp );
                    if ( Oper == AMAP_EQN_OPER_MARK )
                        break;

                    // perform the given operation
                    if ( Amap_ParseFormulaOper( pMan, pStackFn, Oper ) == NULL )
                    {
                        fprintf( pOutput, "Amap_ParseFormula(): Unknown operation\n" );
Alan Mishchenko committed
237
                        ABC_FREE( pFormula );
238 239
                        Vec_PtrFreeP( &pStackFn );
                        Vec_IntFreeP( &pStackOp );
Alan Mishchenko committed
240 241 242 243 244 245
                        return NULL;
                    }
                }
            }
            else
            {
246
                fprintf( pOutput, "Amap_ParseFormula(): There is no opening parenthesis\n" );
Alan Mishchenko committed
247 248 249 250 251 252 253 254 255 256 257 258
                Flag = AMAP_EQN_FLAG_ERROR; 
                break;
            }
            if ( Flag != AMAP_EQN_FLAG_ERROR )
                Flag = AMAP_EQN_FLAG_VAR; 
            break;


        default:
            // scan the next name
            for ( i = 0; pTemp[i] && 
                         pTemp[i] != ' ' && pTemp[i] != '\t' && pTemp[i] != '\r' && pTemp[i] != '\n' &&
259
                         pTemp[i] != AMAP_EQN_SYM_AND && pTemp[i] != AMAP_EQN_SYM_AND2 && pTemp[i] != AMAP_EQN_SYM_OR && pTemp[i] != AMAP_EQN_SYM_OR2 && 
Alan Mishchenko committed
260 261
                         pTemp[i] != AMAP_EQN_SYM_XOR && pTemp[i] != AMAP_EQN_SYM_NEGAFT && pTemp[i] != AMAP_EQN_SYM_CLOSE; 
                  i++ )
Alan Mishchenko committed
262 263 264
              {
                    if ( pTemp[i] == AMAP_EQN_SYM_NEG || pTemp[i] == AMAP_EQN_SYM_OPEN )
                    {
265
                        fprintf( pOutput, "Amap_ParseFormula(): The negation sign or an opening parenthesis inside the variable name.\n" );
Alan Mishchenko committed
266 267 268 269 270 271
                        Flag = AMAP_EQN_FLAG_ERROR; 
                        break;
                    }
              }
            // variable name is found
            fFound = 0;
272
            Vec_PtrForEachEntry( char *, vVarNames, pName, v )
Alan Mishchenko committed
273 274 275 276 277 278 279 280
                if ( strncmp(pTemp, pName, i) == 0 && strlen(pName) == (unsigned)i )
                {
                    pTemp += i-1;
                    fFound = 1;
                    break;
                }
            if ( !fFound )
            { 
281
                fprintf( pOutput, "Amap_ParseFormula(): The parser cannot find var \"%s\" in the input var list of gate \"%s\".\n", pTemp, pGateName ); 
Alan Mishchenko committed
282 283 284
                Flag = AMAP_EQN_FLAG_ERROR; 
                break; 
            }
Alan Mishchenko committed
285
/*
Alan Mishchenko committed
286 287 288 289 290 291
            if ( Flag == AMAP_EQN_FLAG_VAR )
            {
                fprintf( pOutput, "Amap_ParseFormula(): The variable name \"%s\" follows another var without operation sign.\n", pTemp ); 
                Flag = AMAP_EQN_FLAG_ERROR; 
                break; 
            }
Alan Mishchenko committed
292 293 294 295
*/
            if ( Flag == AMAP_EQN_FLAG_VAR )
                Vec_IntPush( pStackOp, AMAP_EQN_OPER_AND );

Alan Mishchenko committed
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
            Vec_PtrPush( pStackFn, Hop_IthVar( pMan, v ) ); // Cudd_Ref( pbVars[v] );
            Flag = AMAP_EQN_FLAG_VAR; 
            break;
        }

        if ( Flag == AMAP_EQN_FLAG_ERROR )
            break;      // error exit
        else if ( Flag == AMAP_EQN_FLAG_START )
            continue;  //  go on parsing
        else if ( Flag == AMAP_EQN_FLAG_VAR )
            while ( 1 )
            {  // check if there are negations in the OpStack     
                if ( Vec_IntSize( pStackOp ) == 0 )
                    break;
                Oper = Vec_IntPop( pStackOp );
                if ( Oper != AMAP_EQN_OPER_NEG )
                {
                    Vec_IntPush( pStackOp, Oper );
                    break;
                }
                else
                {
318
                      Vec_PtrPush( pStackFn, Hop_Not((Hop_Obj_t *)Vec_PtrPop(pStackFn)) );
Alan Mishchenko committed
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
                }
            }
        else // if ( Flag == AMAP_EQN_FLAG_OPER )
            while ( 1 )
            {  // execute all the operations in the OpStack
               // with precedence higher or equal than the last one
                Oper1 = Vec_IntPop( pStackOp ); // the last operation
                if ( Vec_IntSize( pStackOp ) == 0 ) 
                {  // if it is the only operation, push it back
                    Vec_IntPush( pStackOp, Oper1 );
                    break;
                }
                Oper2 = Vec_IntPop( pStackOp ); // the operation before the last one
                if ( Oper2 >= Oper1 )  
                {  // if Oper2 precedence is higher or equal, execute it
                    if ( Amap_ParseFormulaOper( pMan, pStackFn, Oper2 ) == NULL )
                    {
                        fprintf( pOutput, "Amap_ParseFormula(): Unknown operation\n" );
Alan Mishchenko committed
337
                        ABC_FREE( pFormula );
338 339
                        Vec_PtrFreeP( &pStackFn );
                        Vec_IntFreeP( &pStackOp );
Alan Mishchenko committed
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
                        return NULL;
                    }
                    Vec_IntPush( pStackOp,  Oper1 );     // push the last operation back
                }
                else
                {  // if Oper2 precedence is lower, push them back and done
                    Vec_IntPush( pStackOp, Oper2 );
                    Vec_IntPush( pStackOp, Oper1 );
                    break;
                }
            }
    }

    if ( Flag != AMAP_EQN_FLAG_ERROR )
    {
        if ( Vec_PtrSize(pStackFn) != 0 )
        {    
357
            gFunc = (Hop_Obj_t *)Vec_PtrPop(pStackFn);
Alan Mishchenko committed
358 359 360 361
            if ( Vec_PtrSize(pStackFn) == 0 )
                if ( Vec_IntSize( pStackOp ) == 0 )
                {
//                    Cudd_Deref( gFunc );
Alan Mishchenko committed
362
                    ABC_FREE( pFormula );
363 364
                    Vec_PtrFreeP( &pStackFn );
                    Vec_IntFreeP( &pStackOp );
Alan Mishchenko committed
365 366 367 368 369 370 371 372 373 374
                    return gFunc;
                }
                else
                    fprintf( pOutput, "Amap_ParseFormula(): Something is left in the operation stack\n" );
            else
                fprintf( pOutput, "Amap_ParseFormula(): Something is left in the function stack\n" );
        }
        else
            fprintf( pOutput, "Amap_ParseFormula(): The input string is empty\n" );
    }
Alan Mishchenko committed
375
    ABC_FREE( pFormula );
376 377
    Vec_PtrFreeP( &pStackFn );
    Vec_IntFreeP( &pStackOp );
Alan Mishchenko committed
378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393
    return NULL;
}

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

  Synopsis    [Parses equations for the gates.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Amap_LibParseEquations( Amap_Lib_t * p, int fVerbose )
{
394
//    extern int Kit_TruthSupportSize( unsigned * pTruth, int nVars );
Alan Mishchenko committed
395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
    Hop_Man_t * pMan;
    Hop_Obj_t * pObj;
    Vec_Ptr_t * vNames;
    Vec_Int_t * vTruth;
    Amap_Gat_t * pGate;
    Amap_Pin_t * pPin;
    unsigned * pTruth;
    int i, nPinMax;
    nPinMax = Amap_LibNumPinsMax(p);
    if ( nPinMax > AMAP_MAXINS )
        printf( "Gates with more than %d inputs will be ignored.\n", AMAP_MAXINS );
    vTruth = Vec_IntAlloc( 1 << 16 );
    vNames = Vec_PtrAlloc( 100 );
    pMan = Hop_ManStart();
    Hop_IthVar( pMan, nPinMax - 1 );
410
    Vec_PtrForEachEntry( Amap_Gat_t *, p->vGates, pGate, i )
Alan Mishchenko committed
411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430
    {
        if ( pGate->nPins == 0 )
        {
            pGate->pFunc = (unsigned *)Aig_MmFlexEntryFetch( p->pMemGates, 4 );
            if ( strcmp( pGate->pForm, AMAP_STRING_CONST0 ) == 0 )
                pGate->pFunc[0] = 0;
            else if ( strcmp( pGate->pForm, AMAP_STRING_CONST1 ) == 0 )
                pGate->pFunc[0] = ~0;
            else
            {
                printf( "Cannot parse formula \"%s\" of gate \"%s\" with no pins.\n", pGate->pForm, pGate->pName );
                break;
            }
            continue;
        }
        if ( pGate->nPins > AMAP_MAXINS )
            continue;
        Vec_PtrClear( vNames );
        Amap_GateForEachPin( pGate, pPin )
            Vec_PtrPush( vNames, pPin->pName );
431
        pObj = Amap_ParseFormula( stdout, pGate->pForm, vNames, pMan, pGate->pName );
Alan Mishchenko committed
432 433 434 435 436
        if ( pObj == NULL )
            break;
        pTruth = Hop_ManConvertAigToTruth( pMan, pObj, pGate->nPins, vTruth, 0 );
        if ( Kit_TruthSupportSize(pTruth, pGate->nPins) < (int)pGate->nPins )
        {
437 438
            if ( fVerbose )
                printf( "Skipping gate \"%s\" because its output \"%s\" does not depend on all input variables.\n", pGate->pName, pGate->pForm );
Alan Mishchenko committed
439 440
            continue;
        }
441 442
        pGate->pFunc = (unsigned *)Aig_MmFlexEntryFetch( p->pMemGates, sizeof(unsigned)*Abc_TruthWordNum(pGate->nPins) );
        memcpy( pGate->pFunc, pTruth, sizeof(unsigned)*Abc_TruthWordNum(pGate->nPins) );
Alan Mishchenko committed
443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462
    }
    Vec_PtrFree( vNames );
    Vec_IntFree( vTruth );
    Hop_ManStop( pMan );
    return i == Vec_PtrSize(p->vGates);
}

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

  Synopsis    [Parses equations for the gates.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Amap_LibParseTest( char * pFileName )
{
463
    int fVerbose = 0;
Alan Mishchenko committed
464
    Amap_Lib_t * p;
465
    abctime clk = Abc_Clock();
Alan Mishchenko committed
466 467 468 469 470
    p = Amap_LibReadFile( pFileName, fVerbose );
    if ( p == NULL )
        return;
    Amap_LibParseEquations( p, fVerbose );
    Amap_LibFree( p );
471
    ABC_PRT( "Total time", Abc_Clock() - clk );
Alan Mishchenko committed
472 473 474 475 476 477 478
}

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


479 480
ABC_NAMESPACE_IMPL_END