abc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
ioReadDsd.c
Go to the documentation of this file.
1 /**CFile****************************************************************
2 
3  FileName [ioReadDsd.c]
4 
5  SystemName [ABC: Logic synthesis and verification system.]
6 
7  PackageName [Command processing package.]
8 
9  Synopsis [Procedure to read network from file.]
10 
11  Author [Alan Mishchenko]
12 
13  Affiliation [UC Berkeley]
14 
15  Date [Ver. 1.0. Started - June 20, 2005.]
16 
17  Revision [$Id: ioReadDsd.c,v 1.00 2005/06/20 00:00:00 alanmi Exp $]
18 
19 ***********************************************************************/
20 
21 #include "ioAbc.h"
22 
24 
25 
26 ////////////////////////////////////////////////////////////////////////
27 /// DECLARATIONS ///
28 ////////////////////////////////////////////////////////////////////////
29 
30 ////////////////////////////////////////////////////////////////////////
31 /// FUNCTION DEFINITIONS ///
32 ////////////////////////////////////////////////////////////////////////
33 
34 /**Function*************************************************************
35 
36  Synopsis [Finds the end of the part.]
37 
38  Description []
39 
40  SideEffects []
41 
42  SeeAlso []
43 
44 ***********************************************************************/
45 char * Io_ReadDsdFindEnd( char * pCur )
46 {
47  char * pEnd;
48  int nParts = 0;
49  assert( *pCur == '(' );
50  for ( pEnd = pCur; *pEnd; pEnd++ )
51  {
52  if ( *pEnd == '(' )
53  nParts++;
54  else if ( *pEnd == ')' )
55  nParts--;
56  if ( nParts == 0 )
57  return pEnd;
58  }
59  return NULL;
60 }
61 
62 /**Function*************************************************************
63 
64  Synopsis [Splits the formula into parts.]
65 
66  Description []
67 
68  SideEffects []
69 
70  SeeAlso []
71 
72 ***********************************************************************/
73 int Io_ReadDsdStrSplit( char * pCur, char * pParts[], int * pTypeXor )
74 {
75  int fAnd = 0, fXor = 0, fPri = 0, nParts = 0;
76  assert( *pCur );
77  // process the parts
78  while ( 1 )
79  {
80  // save the current part
81  pParts[nParts++] = pCur;
82  // skip the complement
83  if ( *pCur == '!' )
84  pCur++;
85  // skip var
86  if ( *pCur >= 'a' && *pCur <= 'z' )
87  pCur++;
88  else
89  {
90  // skip hex truth table
91  while ( (*pCur >= '0' && *pCur <= '9') || (*pCur >= 'A' && *pCur <= 'F') )
92  pCur++;
93  // process parantheses
94  if ( *pCur != '(' )
95  {
96  printf( "Cannot find the opening paranthesis.\n" );
97  break;
98  }
99  // find the corresponding closing paranthesis
100  pCur = Io_ReadDsdFindEnd( pCur );
101  if ( pCur == NULL )
102  {
103  printf( "Cannot find the closing paranthesis.\n" );
104  break;
105  }
106  pCur++;
107  }
108  // check the end
109  if ( *pCur == 0 )
110  break;
111  // check symbol
112  if ( *pCur != '*' && *pCur != '+' && *pCur != ',' )
113  {
114  printf( "Wrong separating symbol.\n" );
115  break;
116  }
117  // remember the symbol
118  fAnd |= (*pCur == '*');
119  fXor |= (*pCur == '+');
120  fPri |= (*pCur == ',');
121  *pCur++ = 0;
122  }
123  // check separating symbols
124  if ( fAnd + fXor + fPri > 1 )
125  {
126  printf( "Different types of separating symbol ennPartsed.\n" );
127  return 0;
128  }
129  *pTypeXor = fXor;
130  return nParts;
131 }
132 
133 /**Function*************************************************************
134 
135  Synopsis [Recursively parses the formula.]
136 
137  Description []
138 
139  SideEffects []
140 
141  SeeAlso []
142 
143 ***********************************************************************/
144 Abc_Obj_t * Io_ReadDsd_rec( Abc_Ntk_t * pNtk, char * pCur, char * pSop )
145 {
146  Abc_Obj_t * pObj, * pFanin;
147  char * pEnd, * pParts[32];
148  int i, nParts, TypeExor;
149 
150  // consider complemented formula
151  if ( *pCur == '!' )
152  {
153  pObj = Io_ReadDsd_rec( pNtk, pCur + 1, NULL );
154  return Abc_NtkCreateNodeInv( pNtk, pObj );
155  }
156  if ( *pCur == '(' )
157  {
158  assert( pCur[strlen(pCur)-1] == ')' );
159  pCur[strlen(pCur)-1] = 0;
160  nParts = Io_ReadDsdStrSplit( pCur+1, pParts, &TypeExor );
161  if ( nParts == 0 )
162  {
163  Abc_NtkDelete( pNtk );
164  return NULL;
165  }
166  pObj = Abc_NtkCreateNode( pNtk );
167  if ( pSop )
168  {
169 // for ( i = nParts - 1; i >= 0; i-- )
170  for ( i = 0; i < nParts; i++ )
171  {
172  pFanin = Io_ReadDsd_rec( pNtk, pParts[i], NULL );
173  if ( pFanin == NULL )
174  return NULL;
175  Abc_ObjAddFanin( pObj, pFanin );
176  }
177  }
178  else
179  {
180  for ( i = 0; i < nParts; i++ )
181  {
182  pFanin = Io_ReadDsd_rec( pNtk, pParts[i], NULL );
183  if ( pFanin == NULL )
184  return NULL;
185  Abc_ObjAddFanin( pObj, pFanin );
186  }
187  }
188  if ( pSop )
189  pObj->pData = Abc_SopRegister( (Mem_Flex_t *)pNtk->pManFunc, pSop );
190  else if ( TypeExor )
191  pObj->pData = Abc_SopCreateXorSpecial( (Mem_Flex_t *)pNtk->pManFunc, nParts );
192  else
193  pObj->pData = Abc_SopCreateAnd( (Mem_Flex_t *)pNtk->pManFunc, nParts, NULL );
194  return pObj;
195  }
196  if ( *pCur >= 'a' && *pCur <= 'z' )
197  {
198  assert( *(pCur+1) == 0 );
199  return Abc_NtkPi( pNtk, *pCur - 'a' );
200  }
201 
202  // skip hex truth table
203  pEnd = pCur;
204  while ( (*pEnd >= '0' && *pEnd <= '9') || (*pEnd >= 'A' && *pEnd <= 'F') )
205  pEnd++;
206  if ( *pEnd != '(' )
207  {
208  printf( "Cannot find the end of hexidecimal truth table.\n" );
209  return NULL;
210  }
211 
212  // parse the truth table
213  *pEnd = 0;
214  pSop = Abc_SopFromTruthHex( pCur );
215  *pEnd = '(';
216  pObj = Io_ReadDsd_rec( pNtk, pEnd, pSop );
217  ABC_FREE( pSop );
218  return pObj;
219 }
220 
221 /**Function*************************************************************
222 
223  Synopsis [Derives the DSD network of the formula.]
224 
225  Description []
226 
227  SideEffects []
228 
229  SeeAlso []
230 
231 ***********************************************************************/
232 Abc_Ntk_t * Io_ReadDsd( char * pForm )
233 {
234  Abc_Ntk_t * pNtk;
235  Abc_Obj_t * pObj, * pTop;
236  Vec_Ptr_t * vNames;
237  char * pCur, * pFormCopy;
238  int i, nInputs;
239 
240  // count the number of elementary variables
241  nInputs = 0;
242  for ( pCur = pForm; *pCur; pCur++ )
243  if ( *pCur >= 'a' && *pCur <= 'z' )
244  nInputs = Abc_MaxInt( nInputs, *pCur - 'a' );
245  nInputs++;
246 
247  // create the network
249  pNtk->pName = Extra_UtilStrsav( "dsd" );
250 
251  // create PIs
252  vNames = Abc_NodeGetFakeNames( nInputs );
253  for ( i = 0; i < nInputs; i++ )
254  Abc_ObjAssignName( Abc_NtkCreatePi(pNtk), (char *)Vec_PtrEntry(vNames, i), NULL );
255  Abc_NodeFreeNames( vNames );
256 
257  // transform the formula by inserting parantheses
258  // this transforms strings like PRIME(a,b,cd) into (PRIME((a),(b),(cd)))
259  pCur = pFormCopy = ABC_ALLOC( char, 3 * strlen(pForm) + 10 );
260  *pCur++ = '(';
261  for ( ; *pForm; pForm++ )
262  if ( *pForm == '(' )
263  {
264  *pCur++ = '(';
265  *pCur++ = '(';
266  }
267  else if ( *pForm == ')' )
268  {
269  *pCur++ = ')';
270  *pCur++ = ')';
271  }
272  else if ( *pForm == ',' )
273  {
274  *pCur++ = ')';
275  *pCur++ = ',';
276  *pCur++ = '(';
277  }
278  else
279  *pCur++ = *pForm;
280  *pCur++ = ')';
281  *pCur = 0;
282 
283  // parse the formula
284  pObj = Io_ReadDsd_rec( pNtk, pFormCopy, NULL );
285  ABC_FREE( pFormCopy );
286  if ( pObj == NULL )
287  return NULL;
288 
289  // create output
290  pTop = Abc_NtkCreatePo(pNtk);
291  Abc_ObjAssignName( pTop, "F", NULL );
292  Abc_ObjAddFanin( pTop, pObj );
293 
294  // create the only PO
295  if ( !Abc_NtkCheck( pNtk ) )
296  {
297  fprintf( stdout, "Io_ReadDsd(): Network check has failed.\n" );
298  Abc_NtkDelete( pNtk );
299  return NULL;
300  }
301  return pNtk;
302 }
303 
304 
305 
306 ////////////////////////////////////////////////////////////////////////
307 /// END OF FILE ///
308 ////////////////////////////////////////////////////////////////////////
309 
310 
311 
313 
typedefABC_NAMESPACE_HEADER_START struct Vec_Ptr_t_ Vec_Ptr_t
INCLUDES ///.
Definition: vecPtr.h:42
ABC_NAMESPACE_IMPL_START char * Io_ReadDsdFindEnd(char *pCur)
DECLARATIONS ///.
Definition: ioReadDsd.c:45
ABC_DLL char * Abc_SopCreateAnd(Mem_Flex_t *pMan, int nVars, int *pfCompl)
Definition: abcSop.c:162
#define ABC_ALLOC(type, num)
Definition: abc_global.h:229
ABC_DLL int Abc_NtkCheck(Abc_Ntk_t *pNtk)
FUNCTION DEFINITIONS ///.
Definition: abcCheck.c:61
ABC_DLL char * Abc_ObjAssignName(Abc_Obj_t *pObj, char *pName, char *pSuffix)
Definition: abcNames.c:68
static int Abc_MaxInt(int a, int b)
Definition: abc_global.h:238
ABC_DLL char * Abc_SopFromTruthHex(char *pTruth)
Definition: abcSop.c:948
ABC_DLL void Abc_NtkDelete(Abc_Ntk_t *pNtk)
Definition: abcNtk.c:1233
char * Extra_UtilStrsav(const char *s)
ABC_DLL void Abc_ObjAddFanin(Abc_Obj_t *pObj, Abc_Obj_t *pFanin)
Definition: abcFanio.c:84
ABC_DLL Abc_Ntk_t * Abc_NtkAlloc(Abc_NtkType_t Type, Abc_NtkFunc_t Func, int fUseMemMan)
DECLARATIONS ///.
Definition: abcNtk.c:50
ABC_DLL void Abc_NodeFreeNames(Vec_Ptr_t *vNames)
Definition: abcNames.c:257
void * pManFunc
Definition: abc.h:191
ABC_DLL char * Abc_SopRegister(Mem_Flex_t *pMan, char *pName)
DECLARATIONS ///.
Definition: abcSop.c:56
ABC_DLL Abc_Obj_t * Abc_NtkCreateNodeInv(Abc_Ntk_t *pNtk, Abc_Obj_t *pFanin)
Definition: abcObj.c:662
ABC_DLL Vec_Ptr_t * Abc_NodeGetFakeNames(int nNames)
Definition: abcNames.c:221
#define ABC_NAMESPACE_IMPL_END
Definition: abc_global.h:108
static Abc_Obj_t * Abc_NtkCreatePi(Abc_Ntk_t *pNtk)
Definition: abc.h:303
#define ABC_NAMESPACE_IMPL_START
Definition: abc_global.h:107
static void * Vec_PtrEntry(Vec_Ptr_t *p, int i)
Definition: vecPtr.h:362
#define ABC_FREE(obj)
Definition: abc_global.h:232
ABC_DLL char * Abc_SopCreateXorSpecial(Mem_Flex_t *pMan, int nVars)
Definition: abcSop.c:291
static Abc_Obj_t * Abc_NtkCreateNode(Abc_Ntk_t *pNtk)
Definition: abc.h:308
Abc_Ntk_t * Io_ReadDsd(char *pForm)
Definition: ioReadDsd.c:232
Abc_Obj_t * Io_ReadDsd_rec(Abc_Ntk_t *pNtk, char *pCur, char *pSop)
Definition: ioReadDsd.c:144
#define assert(ex)
Definition: util_old.h:213
int strlen()
static Abc_Obj_t * Abc_NtkPi(Abc_Ntk_t *pNtk, int i)
Definition: abc.h:315
void * pData
Definition: abc.h:145
static Abc_Obj_t * Abc_NtkCreatePo(Abc_Ntk_t *pNtk)
Definition: abc.h:304
char * pName
Definition: abc.h:158
int Io_ReadDsdStrSplit(char *pCur, char *pParts[], int *pTypeXor)
Definition: ioReadDsd.c:73