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