abc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
mioForm.c
Go to the documentation of this file.
1 /**CFile****************************************************************
2 
3  FileName [mioForm.c]
4 
5  PackageName [MVSIS 1.3: Multi-valued logic synthesis system.]
6 
7  Synopsis [Parsing equestion formula.]
8 
9  Author [MVSIS Group]
10 
11  Affiliation [UC Berkeley]
12 
13  Date [Ver. 1.0. Started - September 8, 2003.]
14 
15  Revision [$Id: mioForm.c,v 1.4 2004/06/28 14:20:25 alanmi Exp $]
16 
17 ***********************************************************************/
18 
19 #include "mioInt.h"
20 #include "bdd/parse/parse.h"
21 
23 
24 
25 ////////////////////////////////////////////////////////////////////////
26 /// DECLARATIONS ///
27 ////////////////////////////////////////////////////////////////////////
28 
29 // these symbols (and no other) can appear in the formulas
30 #define MIO_SYMB_AND '*'
31 #define MIO_SYMB_AND2 '&'
32 #define MIO_SYMB_OR '+'
33 #define MIO_SYMB_OR2 '|'
34 #define MIO_SYMB_XOR '^'
35 #define MIO_SYMB_NOT '!'
36 #define MIO_SYMB_AFTNOT '\''
37 #define MIO_SYMB_OPEN '('
38 #define MIO_SYMB_CLOSE ')'
39 
40 static int Mio_GateParseFormula( Mio_Gate_t * pGate );
41 static int Mio_GateCollectNames( char * pFormula, char * pPinNames[] );
42 
43 ////////////////////////////////////////////////////////////////////////
44 /// FUNCTION DEFINITIONS ///
45 ////////////////////////////////////////////////////////////////////////
46 
47 /**Function*************************************************************
48 
49  Synopsis [Deriving the functionality of the gates.]
50 
51  Description []
52 
53  SideEffects []
54 
55  SeeAlso []
56 
57 ***********************************************************************/
59 {
60  Mio_Gate_t * pGate;
61 
62  // count the gates
63  pLib->nGates = 0;
64  Mio_LibraryForEachGate( pLib, pGate )
65  pLib->nGates++;
66 
67  // start a temporary BDD manager
68  pLib->dd = Cudd_Init( 20, 0, CUDD_UNIQUE_SLOTS, CUDD_CACHE_SLOTS, 0 );
69  // introduce ZDD variables
70  Cudd_zddVarsFromBddVars( pLib->dd, 2 );
71 
72  // for each gate, derive its function
73  Mio_LibraryForEachGate( pLib, pGate )
74  if ( Mio_GateParseFormula( pGate ) )
75  return 1;
76  return 0;
77 }
78 
79 
80 /**Function*************************************************************
81 
82  Synopsis [Registers the cube string with the network.]
83 
84  Description []
85 
86  SideEffects []
87 
88  SeeAlso []
89 
90 ***********************************************************************/
91 char * Mio_SopRegister( Mem_Flex_t * pMan, char * pName )
92 {
93  char * pRegName;
94  if ( pName == NULL ) return NULL;
95  pRegName = Mem_FlexEntryFetch( pMan, strlen(pName) + 1 );
96  strcpy( pRegName, pName );
97  return pRegName;
98 }
99 
100 /**Function*************************************************************
101 
102  Synopsis [Deriving the functionality of the gates.]
103 
104  Description []
105 
106  SideEffects []
107 
108  SeeAlso []
109 
110 ***********************************************************************/
112 {
113  extern char * Abc_ConvertBddToSop( Mem_Flex_t * pMan, DdManager * dd, DdNode * bFuncOn, DdNode * bFuncOnDc, int nFanins, int fAllPrimes, Vec_Str_t * vCube, int fMode );
114  DdManager * dd = pGate->pLib->dd;
115  char * pPinNames[100];
116  char * pPinNamesCopy[100];
117  Mio_Pin_t * pPin, ** ppPin;
118  int nPins, iPin, i;
119 
120  // set the maximum delay of the gate; count pins
121  pGate->dDelayMax = 0.0;
122  nPins = 0;
123  Mio_GateForEachPin( pGate, pPin )
124  {
125  // set the maximum delay of the gate
126  if ( pGate->dDelayMax < pPin->dDelayBlockMax )
127  pGate->dDelayMax = pPin->dDelayBlockMax;
128  // count the pin
129  nPins++;
130  }
131 
132  // check for the gate with const function
133  if ( nPins == 0 )
134  {
135  if ( strcmp( pGate->pForm, MIO_STRING_CONST0 ) == 0 )
136  {
137  pGate->bFunc = b0;
138  pGate->pSop = Mio_SopRegister( (Mem_Flex_t *)pGate->pLib->pMmFlex, " 0\n" );
139  pGate->pLib->pGate0 = pGate;
140  }
141  else if ( strcmp( pGate->pForm, MIO_STRING_CONST1 ) == 0 )
142  {
143  pGate->bFunc = b1;
144  pGate->pSop = Mio_SopRegister( (Mem_Flex_t *)pGate->pLib->pMmFlex, " 1\n" );
145  pGate->pLib->pGate1 = pGate;
146  }
147  else
148  {
149  printf( "Cannot parse formula \"%s\" of gate \"%s\".\n", pGate->pForm, pGate->pName );
150  return 1;
151  }
152  Cudd_Ref( pGate->bFunc );
153  return 0;
154  }
155 
156  // collect the names as they appear in the formula
157  nPins = Mio_GateCollectNames( pGate->pForm, pPinNames );
158  if ( nPins == 0 )
159  {
160  printf( "Cannot read formula \"%s\" of gate \"%s\".\n", pGate->pForm, pGate->pName );
161  return 1;
162  }
163 
164  // set the number of inputs
165  pGate->nInputs = nPins;
166 
167  // consider the case when all the pins have identical pin info
168  if ( strcmp( pGate->pPins->pName, "*" ) == 0 )
169  {
170  // get the topmost (generic) pin
171  pPin = pGate->pPins;
172  ABC_FREE( pPin->pName );
173 
174  // create individual pins from the generic pin
175  ppPin = &pPin->pNext;
176  for ( i = 1; i < nPins; i++ )
177  {
178  // get the new pin
179  *ppPin = Mio_PinDup( pPin );
180  // set its name
181  (*ppPin)->pName = pPinNames[i];
182  // prepare the next place in the list
183  ppPin = &((*ppPin)->pNext);
184  }
185  *ppPin = NULL;
186 
187  // set the name of the topmost pin
188  pPin->pName = pPinNames[0];
189  }
190  else
191  {
192  // reorder the variable names to appear the save way as the pins
193  iPin = 0;
194  Mio_GateForEachPin( pGate, pPin )
195  {
196  // find the pin with the name pPin->pName
197  for ( i = 0; i < nPins; i++ )
198  {
199  if ( pPinNames[i] && strcmp( pPinNames[i], pPin->pName ) == 0 )
200  {
201  // free pPinNames[i] because it is already available as pPin->pName
202  // setting pPinNames[i] to NULL is useful to make sure that
203  // this name is not assigned to two pins in the list
204  ABC_FREE( pPinNames[i] );
205  pPinNamesCopy[iPin++] = pPin->pName;
206  break;
207  }
208  if ( i == nPins )
209  {
210  printf( "Cannot find pin name \"%s\" in the formula \"%s\" of gate \"%s\".\n",
211  pPin->pName, pGate->pForm, pGate->pName );
212  return 1;
213  }
214  }
215  }
216 
217  // check for the remaining names
218  for ( i = 0; i < nPins; i++ )
219  if ( pPinNames[i] )
220  {
221  printf( "Name \"%s\" appears in the formula \"%s\" of gate \"%s\" but there is no such pin.\n",
222  pPinNames[i], pGate->pForm, pGate->pName );
223  return 1;
224  }
225 
226  // copy the names back
227  memcpy( pPinNames, pPinNamesCopy, nPins * sizeof(char *) );
228  }
229 
230  // expand the manager if necessary
231  if ( dd->size < nPins )
232  {
233  Cudd_Quit( dd );
234  dd = Cudd_Init( nPins + 10, 0, CUDD_UNIQUE_SLOTS, CUDD_CACHE_SLOTS, 0 );
235  Cudd_zddVarsFromBddVars( dd, 2 );
236  }
237 
238  // derive the formula as the BDD
239  pGate->bFunc = Parse_FormulaParser( stdout, pGate->pForm, nPins, 0, pPinNames, dd, dd->vars );
240  if ( pGate->bFunc == NULL )
241  return 1;
242  Cudd_Ref( pGate->bFunc );
243 
244  // derive the cover (SOP)
245  pGate->pSop = Abc_ConvertBddToSop( pGate->pLib->pMmFlex, dd, pGate->bFunc, pGate->bFunc, nPins, 0, pGate->pLib->vCube, -1 );
246 
247  // derive the truth table
248  if ( pGate->nInputs <= 6 )
249  pGate->uTruth = Mio_DeriveTruthTable6( pGate );
250 
251  return 0;
252 }
253 
254 /**Function*************************************************************
255 
256  Synopsis [Collect the pin names in the formula.]
257 
258  Description []
259 
260  SideEffects []
261 
262  SeeAlso []
263 
264 ***********************************************************************/
265 int Mio_GateCollectNames( char * pFormula, char * pPinNames[] )
266 {
267  char Buffer[1000];
268  char * pTemp;
269  int nPins, i;
270 
271  // save the formula as it was
272  strcpy( Buffer, pFormula );
273 
274  // remove the non-name symbols
275  for ( pTemp = Buffer; *pTemp; pTemp++ )
276  if ( *pTemp == MIO_SYMB_AND || *pTemp == MIO_SYMB_AND2 ||
277  *pTemp == MIO_SYMB_OR || *pTemp == MIO_SYMB_OR2 ||
278  *pTemp == MIO_SYMB_XOR ||
279  *pTemp == MIO_SYMB_NOT || *pTemp == MIO_SYMB_AFTNOT ||
280  *pTemp == MIO_SYMB_OPEN || *pTemp == MIO_SYMB_CLOSE )
281  *pTemp = ' ';
282 
283  // save the names
284  nPins = 0;
285  pTemp = strtok( Buffer, " " );
286  while ( pTemp )
287  {
288  for ( i = 0; i < nPins; i++ )
289  if ( strcmp( pTemp, pPinNames[i] ) == 0 )
290  break;
291  if ( i == nPins )
292  { // cannot find this name; save it
293  pPinNames[nPins++] = Abc_UtilStrsav(pTemp);
294  }
295  // get the next name
296  pTemp = strtok( NULL, " " );
297  }
298  return nPins;
299 }
300 
301 ////////////////////////////////////////////////////////////////////////
302 /// END OF FILE ///
303 ////////////////////////////////////////////////////////////////////////
304 
305 
307 
#define MIO_SYMB_AND2
Definition: mioForm.c:31
char * pName
Definition: mioInt.h:82
Mio_Pin_t * pNext
Definition: mioInt.h:114
#define CUDD_UNIQUE_SLOTS
Definition: cudd.h:97
Definition: cudd.h:278
double dDelayBlockMax
Definition: mioInt.h:113
#define Mio_GateForEachPin(Gate, Pin)
Definition: mio.h:76
int size
Definition: cuddInt.h:361
#define b1
Definition: extraBdd.h:76
#define MIO_SYMB_NOT
Definition: mioForm.c:35
char * pSop
Definition: mioInt.h:96
char * strtok()
Mem_Flex_t * pMmFlex
Definition: mioInt.h:75
Mio_Library_t * pLib
Definition: mioInt.h:88
#define CUDD_CACHE_SLOTS
Definition: cudd.h:98
char * memcpy()
#define MIO_SYMB_OR
Definition: mioForm.c:32
word Mio_DeriveTruthTable6(Mio_Gate_t *pGate)
Definition: mioUtils.c:514
Mio_Gate_t * pGate0
Definition: mioInt.h:68
static int Mio_GateParseFormula(Mio_Gate_t *pGate)
Definition: mioForm.c:111
#define MIO_SYMB_OPEN
Definition: mioForm.c:37
char * Mem_FlexEntryFetch(Mem_Flex_t *p, int nBytes)
Definition: mem.c:372
char * pName
Definition: mioInt.h:105
Mio_Pin_t * pPins
Definition: mioInt.h:85
int strcmp()
ABC_NAMESPACE_HEADER_START DdNode * Parse_FormulaParser(FILE *pOutput, char *pFormula, int nVars, int nRanks, char *ppVarNames[], DdManager *dd, DdNode *pbVars[])
INCLUDES ///.
Definition: parseCore.c:116
static int Mio_GateCollectNames(char *pFormula, char *pPinNames[])
Definition: mioForm.c:265
char * pForm
Definition: mioInt.h:84
char * Abc_ConvertBddToSop(Mem_Flex_t *pMan, DdManager *dd, DdNode *bFuncOn, DdNode *bFuncOnDc, int nFanins, int fAllPrimes, Vec_Str_t *vCube, int fMode)
Definition: abcFunc.c:209
#define MIO_SYMB_AND
DECLARATIONS ///.
Definition: mioForm.c:30
#define MIO_SYMB_CLOSE
Definition: mioForm.c:38
#define ABC_NAMESPACE_IMPL_END
Definition: abc_global.h:108
DdManager * Cudd_Init(unsigned int numVars, unsigned int numVarsZ, unsigned int numSlots, unsigned int cacheSize, unsigned long maxMemory)
Definition: cuddInit.c:125
Vec_Str_t * vCube
Definition: mioInt.h:76
Mio_Pin_t * Mio_PinDup(Mio_Pin_t *pPin)
Definition: mioUtils.c:125
STRUCTURE DEFINITIONS ///.
Definition: mioInt.h:61
char * strcpy()
double dDelayMax
Definition: mioInt.h:95
#define ABC_NAMESPACE_IMPL_START
Definition: abc_global.h:107
#define b0
Definition: extraBdd.h:75
#define ABC_FREE(obj)
Definition: abc_global.h:232
DdNode ** vars
Definition: cuddInt.h:390
#define MIO_SYMB_XOR
Definition: mioForm.c:34
int Mio_LibraryParseFormulas(Mio_Library_t *pLib)
FUNCTION DEFINITIONS ///.
Definition: mioForm.c:58
char * Mio_SopRegister(Mem_Flex_t *pMan, char *pName)
Definition: mioForm.c:91
int Cudd_zddVarsFromBddVars(DdManager *dd, int multiplicity)
Definition: cuddAPI.c:519
int strlen()
void Cudd_Quit(DdManager *unique)
Definition: cuddInit.c:225
#define MIO_SYMB_OR2
Definition: mioForm.c:33
void Cudd_Ref(DdNode *n)
Definition: cuddRef.c:129
#define Mio_LibraryForEachGate(Lib, Gate)
GLOBAL VARIABLES ///.
Definition: mio.h:65
char * Abc_UtilStrsav(char *s)
Definition: starter.c:47
#define MIO_STRING_CONST1
Definition: mioInt.h:51
#define MIO_SYMB_AFTNOT
Definition: mioForm.c:36
#define MIO_STRING_CONST0
Definition: mioInt.h:50
Mio_Gate_t * pGate1
Definition: mioInt.h:69