00001
00019 #include "mioInt.h"
00020 #include "parse.h"
00021
00025
00026
00027 #define MIO_SYMB_AND '*'
00028 #define MIO_SYMB_OR '+'
00029 #define MIO_SYMB_NOT '!'
00030 #define MIO_SYMB_AFTNOT '\''
00031 #define MIO_SYMB_OPEN '('
00032 #define MIO_SYMB_CLOSE ')'
00033
00034 static int Mio_GateParseFormula( Mio_Gate_t * pGate );
00035 static int Mio_GateCollectNames( char * pFormula, char * pPinNames[] );
00036
00040
00052 int Mio_LibraryParseFormulas( Mio_Library_t * pLib )
00053 {
00054 Mio_Gate_t * pGate;
00055
00056
00057 pLib->nGates = 0;
00058 Mio_LibraryForEachGate( pLib, pGate )
00059 pLib->nGates++;
00060
00061
00062 pLib->dd = Cudd_Init( 20, 0, CUDD_UNIQUE_SLOTS, CUDD_CACHE_SLOTS, 0 );
00063
00064 Cudd_zddVarsFromBddVars( pLib->dd, 2 );
00065
00066
00067 Mio_LibraryForEachGate( pLib, pGate )
00068 if ( Mio_GateParseFormula( pGate ) )
00069 return 1;
00070 return 0;
00071 }
00072
00073
00085 int Mio_GateParseFormula( Mio_Gate_t * pGate )
00086 {
00087 DdManager * dd = pGate->pLib->dd;
00088 char * pPinNames[100];
00089 char * pPinNamesCopy[100];
00090 Mio_Pin_t * pPin, ** ppPin;
00091 int nPins, iPin, i;
00092
00093
00094 pGate->dDelayMax = 0.0;
00095 nPins = 0;
00096 Mio_GateForEachPin( pGate, pPin )
00097 {
00098
00099 if ( pGate->dDelayMax < pPin->dDelayBlockMax )
00100 pGate->dDelayMax = pPin->dDelayBlockMax;
00101
00102 nPins++;
00103 }
00104
00105
00106 if ( nPins == 0 )
00107 {
00108 if ( strcmp( pGate->pForm, MIO_STRING_CONST0 ) == 0 )
00109 {
00110 pGate->bFunc = b0;
00111 pGate->pSop = Abc_SopRegister( pGate->pLib->pMmFlex, " 0\n" );
00112 pGate->pLib->pGate0 = pGate;
00113 }
00114 else if ( strcmp( pGate->pForm, MIO_STRING_CONST1 ) == 0 )
00115 {
00116 pGate->bFunc = b1;
00117 pGate->pSop = Abc_SopRegister( pGate->pLib->pMmFlex, " 1\n" );
00118 pGate->pLib->pGate1 = pGate;
00119 }
00120 else
00121 {
00122 printf( "Cannot parse formula \"%s\" of gate \"%s\".\n", pGate->pForm, pGate->pName );
00123 return 1;
00124 }
00125 Cudd_Ref( pGate->bFunc );
00126 return 0;
00127 }
00128
00129
00130 nPins = Mio_GateCollectNames( pGate->pForm, pPinNames );
00131 if ( nPins == 0 )
00132 {
00133 printf( "Cannot read formula \"%s\" of gate \"%s\".\n", pGate->pForm, pGate->pName );
00134 return 1;
00135 }
00136
00137
00138 pGate->nInputs = nPins;
00139
00140
00141 if ( strcmp( pGate->pPins->pName, "*" ) == 0 )
00142 {
00143
00144 pPin = pGate->pPins;
00145 FREE( pPin->pName );
00146
00147
00148 ppPin = &pPin->pNext;
00149 for ( i = 1; i < nPins; i++ )
00150 {
00151
00152 *ppPin = Mio_PinDup( pPin );
00153
00154 (*ppPin)->pName = pPinNames[i];
00155
00156 ppPin = &((*ppPin)->pNext);
00157 }
00158 *ppPin = NULL;
00159
00160
00161 pPin->pName = pPinNames[0];
00162 }
00163 else
00164 {
00165
00166 iPin = 0;
00167 Mio_GateForEachPin( pGate, pPin )
00168 {
00169
00170 for ( i = 0; i < nPins; i++ )
00171 {
00172 if ( pPinNames[i] && strcmp( pPinNames[i], pPin->pName ) == 0 )
00173 {
00174
00175
00176
00177 FREE( pPinNames[i] );
00178 pPinNamesCopy[iPin++] = pPin->pName;
00179 break;
00180 }
00181 if ( i == nPins )
00182 {
00183 printf( "Cannot find pin name \"%s\" in the formula \"%s\" of gate \"%s\".\n",
00184 pPin->pName, pGate->pForm, pGate->pName );
00185 return 1;
00186 }
00187 }
00188 }
00189
00190
00191 for ( i = 0; i < nPins; i++ )
00192 if ( pPinNames[i] )
00193 {
00194 printf( "Name \"%s\" appears in the formula \"%s\" of gate \"%s\" but there is no such pin.\n",
00195 pPinNames[i], pGate->pForm, pGate->pName );
00196 return 1;
00197 }
00198
00199
00200 memcpy( pPinNames, pPinNamesCopy, nPins * sizeof(char *) );
00201 }
00202
00203
00204 if ( dd->size < nPins )
00205 {
00206 Cudd_Quit( dd );
00207 dd = Cudd_Init( nPins + 10, 0, CUDD_UNIQUE_SLOTS, CUDD_CACHE_SLOTS, 0 );
00208 Cudd_zddVarsFromBddVars( dd, 2 );
00209 }
00210
00211
00212 pGate->bFunc = Parse_FormulaParser( stdout, pGate->pForm, nPins, 0, pPinNames, dd, dd->vars );
00213 Cudd_Ref( pGate->bFunc );
00214
00215
00216 pGate->pSop = Abc_ConvertBddToSop( pGate->pLib->pMmFlex, dd, pGate->bFunc, pGate->bFunc, nPins, 0, pGate->pLib->vCube, -1 );
00217 return 0;
00218 }
00219
00231 int Mio_GateCollectNames( char * pFormula, char * pPinNames[] )
00232 {
00233 char Buffer[1000];
00234 char * pTemp;
00235 int nPins, i;
00236
00237
00238 strcpy( Buffer, pFormula );
00239
00240
00241 for ( pTemp = Buffer; *pTemp; pTemp++ )
00242 if ( *pTemp == MIO_SYMB_AND || *pTemp == MIO_SYMB_OR || *pTemp == MIO_SYMB_NOT
00243 || *pTemp == MIO_SYMB_OPEN || *pTemp == MIO_SYMB_CLOSE || *pTemp == MIO_SYMB_AFTNOT )
00244 *pTemp = ' ';
00245
00246
00247 nPins = 0;
00248 pTemp = strtok( Buffer, " " );
00249 while ( pTemp )
00250 {
00251 for ( i = 0; i < nPins; i++ )
00252 if ( strcmp( pTemp, pPinNames[i] ) == 0 )
00253 break;
00254 if ( i == nPins )
00255 {
00256 pPinNames[nPins++] = Extra_UtilStrsav(pTemp);
00257 }
00258
00259 pTemp = strtok( NULL, " " );
00260 }
00261 return nPins;
00262 }
00263
00267
00268