abc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
ioReadEqn.c
Go to the documentation of this file.
1 /**CFile****************************************************************
2 
3  FileName [ioReadEqn.c]
4 
5  SystemName [ABC: Logic synthesis and verification system.]
6 
7  PackageName [Command processing package.]
8 
9  Synopsis [Procedures to read equation format files.]
10 
11  Author [Alan Mishchenko]
12 
13  Affiliation [UC Berkeley]
14 
15  Date [Ver. 1.0. Started - June 20, 2005.]
16 
17  Revision [$Id: ioReadEqn.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 
31 static void Io_ReadEqnStrCompact( char * pStr );
32 static int Io_ReadEqnStrFind( Vec_Ptr_t * vTokens, char * pName );
33 static void Io_ReadEqnStrCutAt( char * pStr, char * pStop, int fUniqueOnly, Vec_Ptr_t * vTokens );
34 
35 ////////////////////////////////////////////////////////////////////////
36 /// FUNCTION DEFINITIONS ///
37 ////////////////////////////////////////////////////////////////////////
38 
39 /**Function*************************************************************
40 
41  Synopsis [Reads the network from a BENCH file.]
42 
43  Description []
44 
45  SideEffects []
46 
47  SeeAlso []
48 
49 ***********************************************************************/
50 Abc_Ntk_t * Io_ReadEqn( char * pFileName, int fCheck )
51 {
53  Abc_Ntk_t * pNtk;
54 
55  // start the file
56  p = Extra_FileReaderAlloc( pFileName, "#", ";", "=" );
57  if ( p == NULL )
58  return NULL;
59 
60  // read the network
61  pNtk = Io_ReadEqnNetwork( p );
63  if ( pNtk == NULL )
64  return NULL;
65 
66  // make sure that everything is okay with the network structure
67  if ( fCheck && !Abc_NtkCheckRead( pNtk ) )
68  {
69  printf( "Io_ReadEqn: The network check has failed.\n" );
70  Abc_NtkDelete( pNtk );
71  return NULL;
72  }
73  return pNtk;
74 }
75 
76 /**Function*************************************************************
77 
78  Synopsis []
79 
80  Description []
81 
82  SideEffects []
83 
84  SeeAlso []
85 
86 ***********************************************************************/
88 {
89  ProgressBar * pProgress;
90  Vec_Ptr_t * vTokens;
91  Vec_Ptr_t * vVars;
92  Abc_Ntk_t * pNtk;
93  Abc_Obj_t * pNode;
94  char * pNodeName, * pFormula, * pFormulaCopy, * pVarName;
95  int iLine, i;
96 
97  // allocate the empty network
99  // set the specs
102 
103  // go through the lines of the file
104  vVars = Vec_PtrAlloc( 100 );
105  pProgress = Extra_ProgressBarStart( stdout, Extra_FileReaderGetFileSize(p) );
106  for ( iLine = 0; (vTokens = (Vec_Ptr_t *)Extra_FileReaderGetTokens(p)); iLine++ )
107  {
109 
110  // check if the first token contains anything
111  Io_ReadEqnStrCompact( (char *)vTokens->pArray[0] );
112  if ( strlen((char *)vTokens->pArray[0]) == 0 )
113  break;
114 
115  // if the number of tokens is different from two, error
116  if ( vTokens->nSize != 2 )
117  {
118  printf( "%s: Wrong input file format.\n", Extra_FileReaderGetFileName(p) );
119  Abc_NtkDelete( pNtk );
120  return NULL;
121  }
122 
123  // get the type of the line
124  if ( strncmp( (char *)vTokens->pArray[0], "INORDER", 7 ) == 0 )
125  {
126  Io_ReadEqnStrCutAt( (char *)vTokens->pArray[1], " \n\r\t", 0, vVars );
127  Vec_PtrForEachEntry( char *, vVars, pVarName, i )
128  Io_ReadCreatePi( pNtk, pVarName );
129  }
130  else if ( strncmp( (char *)vTokens->pArray[0], "OUTORDER", 8 ) == 0 )
131  {
132  Io_ReadEqnStrCutAt( (char *)vTokens->pArray[1], " \n\r\t", 0, vVars );
133  Vec_PtrForEachEntry( char *, vVars, pVarName, i )
134  Io_ReadCreatePo( pNtk, pVarName );
135  }
136  else
137  {
138  extern Hop_Obj_t * Parse_FormulaParserEqn( FILE * pOutput, char * pFormInit, Vec_Ptr_t * vVarNames, Hop_Man_t * pMan );
139 
140  // get hold of the node name and its formula
141  pNodeName = (char *)vTokens->pArray[0];
142  pFormula = (char *)vTokens->pArray[1];
143  // compact the formula
144  Io_ReadEqnStrCompact( pFormula );
145 
146  // consider the case of the constant node
147  if ( pFormula[1] == 0 && (pFormula[0] == '0' || pFormula[0] == '1') )
148  {
149  pFormulaCopy = NULL;
150  Vec_PtrClear( vVars );
151  }
152  else
153  {
154  // make a copy of formula for names
155  pFormulaCopy = Extra_UtilStrsav( pFormula );
156  // find the names of the fanins of this node
157  Io_ReadEqnStrCutAt( pFormulaCopy, "!*+()", 1, vVars );
158  }
159  // create the node
160  pNode = Io_ReadCreateNode( pNtk, pNodeName, (char **)Vec_PtrArray(vVars), Vec_PtrSize(vVars) );
161  // derive the function
162  pNode->pData = Parse_FormulaParserEqn( stdout, pFormula, vVars, (Hop_Man_t *)pNtk->pManFunc );
163  // remove the cubes
164  ABC_FREE( pFormulaCopy );
165  }
166  }
167  Extra_ProgressBarStop( pProgress );
168  Vec_PtrFree( vVars );
169  Abc_NtkFinalizeRead( pNtk );
170  return pNtk;
171 }
172 
173 
174 
175 /**Function*************************************************************
176 
177  Synopsis [Compacts the string by throwing away space-like chars.]
178 
179  Description []
180 
181  SideEffects []
182 
183  SeeAlso []
184 
185 ***********************************************************************/
186 void Io_ReadEqnStrCompact( char * pStr )
187 {
188  char * pCur, * pNew;
189  for ( pNew = pCur = pStr; *pCur; pCur++ )
190  if ( !(*pCur == ' ' || *pCur == '\n' || *pCur == '\r' || *pCur == '\t') )
191  *pNew++ = *pCur;
192  *pNew = 0;
193 }
194 
195 /**Function*************************************************************
196 
197  Synopsis [Determines unique variables in the string.]
198 
199  Description []
200 
201  SideEffects []
202 
203  SeeAlso []
204 
205 ***********************************************************************/
206 int Io_ReadEqnStrFind( Vec_Ptr_t * vTokens, char * pName )
207 {
208  char * pToken;
209  int i;
210  Vec_PtrForEachEntry( char *, vTokens, pToken, i )
211  if ( strcmp( pToken, pName ) == 0 )
212  return i;
213  return -1;
214 }
215 
216 /**Function*************************************************************
217 
218  Synopsis [Cuts the string into pieces using stop chars.]
219 
220  Description []
221 
222  SideEffects []
223 
224  SeeAlso []
225 
226 ***********************************************************************/
227 void Io_ReadEqnStrCutAt( char * pStr, char * pStop, int fUniqueOnly, Vec_Ptr_t * vTokens )
228 {
229  char * pToken;
230  Vec_PtrClear( vTokens );
231  for ( pToken = strtok( pStr, pStop ); pToken; pToken = strtok( NULL, pStop ) )
232  if ( !fUniqueOnly || Io_ReadEqnStrFind( vTokens, pToken ) == -1 )
233  Vec_PtrPush( vTokens, pToken );
234 }
235 
236 
237 ////////////////////////////////////////////////////////////////////////
238 /// END OF FILE ///
239 ////////////////////////////////////////////////////////////////////////
240 
241 
242 
244 
typedefABC_NAMESPACE_HEADER_START struct Vec_Ptr_t_ Vec_Ptr_t
INCLUDES ///.
Definition: vecPtr.h:42
static void Io_ReadEqnStrCutAt(char *pStr, char *pStop, int fUniqueOnly, Vec_Ptr_t *vTokens)
Definition: ioReadEqn.c:227
static Llb_Mgr_t * p
Definition: llb3Image.c:950
char * Extra_FileReaderGetFileName(Extra_FileReader_t *p)
char * strtok()
void Extra_FileReaderFree(Extra_FileReader_t *p)
static void Vec_PtrPush(Vec_Ptr_t *p, void *Entry)
Definition: vecPtr.h:606
Abc_Ntk_t * Io_ReadEqn(char *pFileName, int fCheck)
FUNCTION DEFINITIONS ///.
Definition: ioReadEqn.c:50
ABC_DLL void Abc_NtkFinalizeRead(Abc_Ntk_t *pNtk)
Definition: abcNtk.c:360
static int Vec_PtrSize(Vec_Ptr_t *p)
Definition: vecPtr.h:295
Definition: hop.h:65
Extra_FileReader_t * Extra_FileReaderAlloc(char *pFileName, char *pCharsComment, char *pCharsStop, char *pCharsClean)
FUNCTION DEFINITIONS ///.
ABC_DLL void Abc_NtkDelete(Abc_Ntk_t *pNtk)
Definition: abcNtk.c:1233
char * Extra_UtilStrsav(const char *s)
ABC_DLL Abc_Ntk_t * Abc_NtkAlloc(Abc_NtkType_t Type, Abc_NtkFunc_t Func, int fUseMemMan)
DECLARATIONS ///.
Definition: abcNtk.c:50
int strcmp()
void * pManFunc
Definition: abc.h:191
DECLARATIONS ///.
Abc_Obj_t * Io_ReadCreatePi(Abc_Ntk_t *pNtk, char *pName)
Definition: ioUtil.c:610
ABC_DLL int Abc_NtkCheckRead(Abc_Ntk_t *pNtk)
Definition: abcCheck.c:77
char * Extra_FileNameGeneric(char *FileName)
#define ABC_NAMESPACE_IMPL_END
Definition: abc_global.h:108
void Extra_ProgressBarStop(ProgressBar *p)
#define ABC_NAMESPACE_IMPL_START
Definition: abc_global.h:107
char * pSpec
Definition: abc.h:159
void * Extra_FileReaderGetTokens(Extra_FileReader_t *p)
static Vec_Ptr_t * Vec_PtrAlloc(int nCap)
FUNCTION DEFINITIONS ///.
Definition: vecPtr.h:83
Hop_Obj_t * Parse_FormulaParserEqn(FILE *pOutput, char *pFormInit, Vec_Ptr_t *vVarNames, Hop_Man_t *pMan)
FUNCTION DEFINITIONS ///.
Definition: parseEqn.c:72
int Extra_FileReaderGetFileSize(Extra_FileReader_t *p)
#define ABC_FREE(obj)
Definition: abc_global.h:232
ProgressBar * Extra_ProgressBarStart(FILE *pFile, int nItemsTotal)
FUNCTION DEFINITIONS ///.
int strncmp()
Abc_Obj_t * Io_ReadCreateNode(Abc_Ntk_t *pNtk, char *pNameOut, char *pNamesIn[], int nInputs)
Definition: ioUtil.c:725
static void Extra_ProgressBarUpdate(ProgressBar *p, int nItemsCur, char *pString)
Definition: extra.h:243
static void Vec_PtrClear(Vec_Ptr_t *p)
Definition: vecPtr.h:545
static int Io_ReadEqnStrFind(Vec_Ptr_t *vTokens, char *pName)
Definition: ioReadEqn.c:206
int strlen()
void * pData
Definition: abc.h:145
#define Vec_PtrForEachEntry(Type, vVec, pEntry, i)
MACRO DEFINITIONS ///.
Definition: vecPtr.h:55
Abc_Obj_t * Io_ReadCreatePo(Abc_Ntk_t *pNtk, char *pName)
Definition: ioUtil.c:635
static ABC_NAMESPACE_IMPL_START Abc_Ntk_t * Io_ReadEqnNetwork(Extra_FileReader_t *p)
DECLARATIONS ///.
Definition: ioReadEqn.c:87
static void ** Vec_PtrArray(Vec_Ptr_t *p)
Definition: vecPtr.h:279
typedefABC_NAMESPACE_HEADER_START struct Hop_Man_t_ Hop_Man_t
INCLUDES ///.
Definition: hop.h:49
char * pName
Definition: abc.h:158
static void Io_ReadEqnStrCompact(char *pStr)
Definition: ioReadEqn.c:186
static void Vec_PtrFree(Vec_Ptr_t *p)
Definition: vecPtr.h:223
int Extra_FileReaderGetCurPosition(Extra_FileReader_t *p)