ioReadEqn.c 7.54 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
/**CFile****************************************************************

  FileName    [ioReadEqn.c]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [Command processing package.]

  Synopsis    [Procedures to read equation format files.]

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

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

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

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

Alan Mishchenko committed
21
#include "ioAbc.h"
Alan Mishchenko committed
22

23 24 25
ABC_NAMESPACE_IMPL_START


Alan Mishchenko committed
26 27 28 29 30 31 32 33 34 35
////////////////////////////////////////////////////////////////////////
///                        DECLARATIONS                              ///
////////////////////////////////////////////////////////////////////////

static Abc_Ntk_t * Io_ReadEqnNetwork( Extra_FileReader_t * p );
static void        Io_ReadEqnStrCompact( char * pStr );
static int         Io_ReadEqnStrFind( Vec_Ptr_t * vTokens, char * pName );
static void        Io_ReadEqnStrCutAt( char * pStr, char * pStop, int fUniqueOnly, Vec_Ptr_t * vTokens );

////////////////////////////////////////////////////////////////////////
Alan Mishchenko committed
36
///                     FUNCTION DEFINITIONS                         ///
Alan Mishchenko committed
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
////////////////////////////////////////////////////////////////////////

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

  Synopsis    [Reads the network from a BENCH file.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Abc_Ntk_t * Io_ReadEqn( char * pFileName, int fCheck )
{
    Extra_FileReader_t * p;
    Abc_Ntk_t * pNtk;

    // start the file
    p = Extra_FileReaderAlloc( pFileName, "#", ";", "=" );
    if ( p == NULL )
        return NULL;

    // read the network
    pNtk = Io_ReadEqnNetwork( p );
    Extra_FileReaderFree( p );
    if ( pNtk == NULL )
        return NULL;

    // make sure that everything is okay with the network structure
Alan Mishchenko committed
67
    if ( fCheck && !Abc_NtkCheckRead( pNtk ) )
Alan Mishchenko committed
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
    {
        printf( "Io_ReadEqn: The network check has failed.\n" );
        Abc_NtkDelete( pNtk );
        return NULL;
    }
    return pNtk;
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Abc_Ntk_t * Io_ReadEqnNetwork( Extra_FileReader_t * p )
{
    ProgressBar * pProgress;
    Vec_Ptr_t * vTokens;
Alan Mishchenko committed
91
    Vec_Ptr_t * vVars;
Alan Mishchenko committed
92 93
    Abc_Ntk_t * pNtk;
    Abc_Obj_t * pNode;
Alan Mishchenko committed
94 95
    char * pNodeName, * pFormula, * pFormulaCopy, * pVarName;
    int iLine, i;
Alan Mishchenko committed
96 97
    
    // allocate the empty network
Alan Mishchenko committed
98 99 100 101
    pNtk = Abc_NtkAlloc( ABC_NTK_NETLIST, ABC_FUNC_AIG, 1 );
    // set the specs
    pNtk->pName = Extra_FileNameGeneric(Extra_FileReaderGetFileName(p));
    pNtk->pSpec = Extra_UtilStrsav(Extra_FileReaderGetFileName(p));
Alan Mishchenko committed
102 103 104 105

    // go through the lines of the file
    vVars  = Vec_PtrAlloc( 100 );
    pProgress = Extra_ProgressBarStart( stdout, Extra_FileReaderGetFileSize(p) );
106
    for ( iLine = 0; (vTokens = (Vec_Ptr_t *)Extra_FileReaderGetTokens(p)); iLine++ )
Alan Mishchenko committed
107 108 109 110
    {
        Extra_ProgressBarUpdate( pProgress, Extra_FileReaderGetCurPosition(p), NULL );

        // check if the first token contains anything
111 112
        Io_ReadEqnStrCompact( (char *)vTokens->pArray[0] );
        if ( strlen((char *)vTokens->pArray[0]) == 0 )
Alan Mishchenko committed
113 114 115 116 117 118 119 120 121 122 123
            break;

        // if the number of tokens is different from two, error
        if ( vTokens->nSize != 2 )
        {
            printf( "%s: Wrong input file format.\n", Extra_FileReaderGetFileName(p) );
            Abc_NtkDelete( pNtk );
            return NULL;
        }

        // get the type of the line
124
        if ( strncmp( (char *)vTokens->pArray[0], "INORDER", 7 ) == 0 )
Alan Mishchenko committed
125
        {
126 127
            Io_ReadEqnStrCutAt( (char *)vTokens->pArray[1], " \n\r\t", 0, vVars );
            Vec_PtrForEachEntry( char *, vVars, pVarName, i )
Alan Mishchenko committed
128 129
                Io_ReadCreatePi( pNtk, pVarName );
        }
130
        else if ( strncmp( (char *)vTokens->pArray[0], "OUTORDER", 8 ) == 0 )
Alan Mishchenko committed
131
        {
132 133
            Io_ReadEqnStrCutAt( (char *)vTokens->pArray[1], " \n\r\t", 0, vVars );
            Vec_PtrForEachEntry( char *, vVars, pVarName, i )
Alan Mishchenko committed
134 135 136 137
                Io_ReadCreatePo( pNtk, pVarName );
        }
        else 
        {
Alan Mishchenko committed
138 139 140
            extern Hop_Obj_t * Parse_FormulaParserEqn( FILE * pOutput, char * pFormInit, Vec_Ptr_t * vVarNames, Hop_Man_t * pMan );

            // get hold of the node name and its formula
141 142
            pNodeName = (char *)vTokens->pArray[0];
            pFormula  = (char *)vTokens->pArray[1];
Alan Mishchenko committed
143 144 145
            // compact the formula 
            Io_ReadEqnStrCompact( pFormula );

Alan Mishchenko committed
146
            // consider the case of the constant node
Alan Mishchenko committed
147
            if ( pFormula[1] == 0 && (pFormula[0] == '0' || pFormula[0] == '1') )
Alan Mishchenko committed
148
            {
Alan Mishchenko committed
149 150
                pFormulaCopy = NULL;
                Vec_PtrClear( vVars );
Alan Mishchenko committed
151
            }
Alan Mishchenko committed
152
            else
Alan Mishchenko committed
153
            {
Alan Mishchenko committed
154 155 156 157
                // make a copy of formula for names
                pFormulaCopy = Extra_UtilStrsav( pFormula );
                // find the names of the fanins of this node
                Io_ReadEqnStrCutAt( pFormulaCopy, "!*+()", 1, vVars );
Alan Mishchenko committed
158
            }
Alan Mishchenko committed
159 160 161
            // create the node
            pNode = Io_ReadCreateNode( pNtk, pNodeName, (char **)Vec_PtrArray(vVars), Vec_PtrSize(vVars) );
            // derive the function
162
            pNode->pData = Parse_FormulaParserEqn( stdout, pFormula, vVars, (Hop_Man_t *)pNtk->pManFunc );
Alan Mishchenko committed
163
            // remove the cubes
Alan Mishchenko committed
164
            ABC_FREE( pFormulaCopy );
Alan Mishchenko committed
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 196 197 198 199 200 201 202 203 204 205 206 207 208 209
        }
    }
    Extra_ProgressBarStop( pProgress );
    Vec_PtrFree( vVars );
    Abc_NtkFinalizeRead( pNtk );
    return pNtk;
}



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

  Synopsis    [Compacts the string by throwing away space-like chars.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Io_ReadEqnStrCompact( char * pStr )
{
    char * pCur, * pNew;
    for ( pNew = pCur = pStr; *pCur; pCur++ )
        if ( !(*pCur == ' ' || *pCur == '\n' || *pCur == '\r' || *pCur == '\t') )
            *pNew++ = *pCur;
    *pNew = 0;
}

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

  Synopsis    [Determines unique variables in the string.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Io_ReadEqnStrFind( Vec_Ptr_t * vTokens, char * pName )
{
    char * pToken;
    int i;
210
    Vec_PtrForEachEntry( char *, vTokens, pToken, i )
Alan Mishchenko committed
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
        if ( strcmp( pToken, pName ) == 0 )
            return i;
    return -1;
}

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

  Synopsis    [Cuts the string into pieces using stop chars.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Io_ReadEqnStrCutAt( char * pStr, char * pStop, int fUniqueOnly, Vec_Ptr_t * vTokens )
{
    char * pToken;
    Vec_PtrClear( vTokens );
    for ( pToken = strtok( pStr, pStop ); pToken; pToken = strtok( NULL, pStop ) )
        if ( !fUniqueOnly || Io_ReadEqnStrFind( vTokens, pToken ) == -1 )
            Vec_PtrPush( vTokens, pToken );
}


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



243 244
ABC_NAMESPACE_IMPL_END