ABC: A System for Sequential Synthesis and Verification
 
Loading...
Searching...
No Matches
wlcStdin.c
Go to the documentation of this file.
1
20
21#include "wlc.h"
22
24
28
32
44void Wlc_ComputeSum( char * pRes, char * pAdd, int nBits, int Radix )
45{
46 char Carry = 0; int i;
47 for ( i = 0; i < nBits; i++ )
48 {
49 char Sum = pRes[i] + pAdd[i] + Carry;
50 if ( Sum >= Radix )
51 {
52 Sum -= Radix;
53 Carry = 1;
54 }
55 else
56 Carry = 0;
57 assert( Sum >= 0 && Sum < Radix );
58 pRes[i] = Sum;
59 }
60 assert( Carry == 0 );
61}
62Vec_Str_t * Wlc_ConvertToRadix( unsigned * pBits, int Start, int nBits, int Radix )
63{
64 Vec_Str_t * vRes = Vec_StrStart( nBits );
65 Vec_Str_t * vSum = Vec_StrStart( nBits );
66 char * pRes = Vec_StrArray( vRes );
67 char * pSum = Vec_StrArray( vSum ); int i;
68 assert( Radix >= 2 && Radix < 36 );
69 pSum[0] = 1;
70 // compute number
71 for ( i = 0; i < nBits; i++ )
72 {
73 if ( Abc_InfoHasBit(pBits, Start + i) )
74 Wlc_ComputeSum( pRes, pSum, nBits, Radix );
75 if ( i < nBits - 1 )
76 Wlc_ComputeSum( pSum, pSum, nBits, Radix );
77 }
78 Vec_StrFree( vSum );
79 // remove zeros
80 for ( i = nBits - 1; i >= 0; i-- )
81 if ( pRes[i] )
82 break;
83 Vec_StrShrink( vRes, i+1 );
84 // convert to chars
85 for ( ; i >= 0; i-- )
86 {
87 if ( pRes[i] < 10 )
88 pRes[i] += '0';
89 else
90 pRes[i] += 'a' - 10;
91 }
92 Vec_StrReverseOrder( vRes );
93 if ( Vec_StrSize(vRes) == 0 )
94 Vec_StrPush( vRes, '0' );
95 Vec_StrPush( vRes, '\0' );
96 return vRes;
97}
98
110void Wlc_NtkReport( Wlc_Ntk_t * p, Abc_Cex_t * pCex, char * pName, int Radix )
111{
112 Vec_Str_t * vNum;
113 int i, NameId, Name, Start = -1, nBits = -1;
114 assert( pCex->nRegs == 0 );
115 // get the name ID
116 NameId = Abc_NamStrFind( p->pManName, pName );
117 if ( NameId <= 0 )
118 {
119 printf( "Cannot find \"%s\" among nodes of the network.\n", pName );
120 return;
121 }
122 // get the primary input
123 Vec_IntForEachEntryTriple( &p->vValues, Name, Start, nBits, i )
124 if ( Name == NameId )
125 break;
126 // find the PI
127 if ( i == Vec_IntSize(&p->vValues) )
128 {
129 printf( "Cannot find \"%s\" among primary inputs of the network.\n", pName );
130 return;
131 }
132 // print value
133 assert( Radix == 2 || Radix == 10 || Radix == 16 );
134 vNum = Wlc_ConvertToRadix( pCex->pData, Start, nBits, Radix );
135 printf( "((%s %s%s))\n", pName, Radix == 16 ? "#x" : (Radix == 2 ? "#b" : ""), Vec_StrArray(vNum) );
136 Vec_StrFree( vNum );
137}
138
150static inline int Wlc_StdinCollectStop( Vec_Str_t * vInput, char * pLine, int LineSize )
151{
152 char * pStr = Vec_StrArray(vInput) + Vec_StrSize(vInput) - LineSize;
153 if ( Vec_StrSize(vInput) < LineSize )
154 return 0;
155 return !strncmp( pStr, pLine, LineSize );
156}
158{
159 int c, DirSize = strlen(pDir);
160 Vec_Str_t * vInput = Vec_StrAlloc( 1000 );
161 while ( (c = fgetc(stdin)) != EOF )
162 {
163 Vec_StrPush( vInput, (char)c );
164 if ( c == ')' && Wlc_StdinCollectStop(vInput, pDir, DirSize) )
165 break;
166 }
167 Vec_StrPush( vInput, '\0' );
168 return vInput;
169}
171{
172 int c, Count = 0, fSomeInput = 0;
173 Vec_Str_t * vInput = Vec_StrAlloc( 1000 );
174 while ( (c = fgetc(stdin)) != EOF )
175 {
176 Vec_StrPush( vInput, (char)c );
177 if ( c == '(' )
178 Count++, fSomeInput = 1;
179 else if ( c == ')' )
180 Count--;
181 if ( Count == 0 && fSomeInput )
182 break;
183 }
184 if ( c == EOF )
185 Vec_StrFreeP( &vInput );
186 else
187 Vec_StrPush( vInput, '\0' );
188 return vInput;
189}
190int Wlc_StdinProcessSmt( Abc_Frame_t * pAbc, char * pCmd )
191{
192 // collect stdin until (check-sat)
193 Vec_Str_t * vInput = Wlc_StdinCollectProblem( "(check-sat)" );
194 // parse input
195 Wlc_Ntk_t * pNtk = Wlc_ReadSmtBuffer( "top", Vec_StrArray(vInput), Vec_StrArray(vInput) + Vec_StrSize(vInput), 0, 0 );
196 Vec_StrFree( vInput );
197 // install current network
198 Wlc_SetNtk( pAbc, pNtk );
199 // execute command
200 if ( Cmd_CommandExecute(pAbc, pCmd) )
201 {
202 Abc_Print( 1, "Something did not work out with the command \"%s\".\n", pCmd );
203 return 0;
204 }
205 // solver finished
206 if ( Abc_FrameReadProbStatus(pAbc) == -1 )
207 printf( "undecided\n" );
208 else if ( Abc_FrameReadProbStatus(pAbc) == 1 )
209 printf( "unsat\n" );
210 else if ( Abc_FrameReadProbStatus(pAbc) == 0 )
211 printf( "sat\n" );
212 else assert( 0 );
213 fflush( stdout );
214 // wait for stdin for give directions
215 while ( (vInput = Wlc_StdinCollectQuery()) != NULL )
216 {
217 char * pName = strtok( Vec_StrArray(vInput), " \n\t\r()" );
218 // check directive
219 if ( strcmp(pName, "get-value") )
220 {
221 Abc_Print( 1, "ABC is expecting \"get-value\" in a follow-up input of the satisfiable problem.\n" );
222 Vec_StrFree( vInput );
223 return 0;
224 }
225 // check status
226 if ( Abc_FrameReadProbStatus(pAbc) != 0 )
227 {
228 Abc_Print( 1, "ABC received a follow-up input for a problem that is not known to be satisfiable.\n" );
229 Vec_StrFree( vInput );
230 return 0;
231 }
232 // get the variable number
233 pName = strtok( NULL, "() \n\t\r" );
234 // get the counter-example
235 if ( Abc_FrameReadCex(pAbc) == NULL )
236 {
237 Abc_Print( 1, "ABC does not have a counter-example available to process a \"get-value\" request.\n" );
238 Vec_StrFree( vInput );
239 return 0;
240 }
241 // report value of this variable
242 Wlc_NtkReport( (Wlc_Ntk_t *)pAbc->pAbcWlc, (Abc_Cex_t *)Abc_FrameReadCex(pAbc), pName, 16 );
243 Vec_StrFree( vInput );
244 fflush( stdout );
245 }
246 return 1;
247}
248
252
253
255
#define ABC_NAMESPACE_IMPL_START
#define ABC_NAMESPACE_IMPL_END
ABC_DLL int Abc_FrameReadProbStatus(Abc_Frame_t *pAbc)
Definition mainFrame.c:74
typedefABC_NAMESPACE_HEADER_START struct Abc_Frame_t_ Abc_Frame_t
INCLUDES ///.
Definition abcapis.h:38
ABC_DLL void * Abc_FrameReadCex(Abc_Frame_t *pAbc)
Definition mainFrame.c:75
struct Vec_Str_t_ Vec_Str_t
Definition bblif.c:46
ABC_DLL int Cmd_CommandExecute(Abc_Frame_t *pAbc, const char *sCommand)
Definition cmdApi.c:193
Cube * p
Definition exorList.c:222
typedefABC_NAMESPACE_HEADER_START struct Abc_Cex_t_ Abc_Cex_t
INCLUDES ///.
Definition utilCex.h:39
int Abc_NamStrFind(Abc_Nam_t *p, char *pStr)
Definition utilNam.c:433
#define assert(ex)
Definition util_old.h:213
int strncmp()
int strlen()
int strcmp()
char * strtok()
#define Vec_IntForEachEntryTriple(vVec, Entry1, Entry2, Entry3, i)
Definition vecInt.h:76
Vec_Str_t * Wlc_ConvertToRadix(unsigned *pBits, int Start, int nBits, int Radix)
Definition wlcStdin.c:62
Vec_Str_t * Wlc_StdinCollectQuery()
Definition wlcStdin.c:170
void Wlc_NtkReport(Wlc_Ntk_t *p, Abc_Cex_t *pCex, char *pName, int Radix)
Definition wlcStdin.c:110
Vec_Str_t * Wlc_StdinCollectProblem(char *pDir)
Definition wlcStdin.c:157
int Wlc_StdinProcessSmt(Abc_Frame_t *pAbc, char *pCmd)
Definition wlcStdin.c:190
ABC_NAMESPACE_IMPL_START void Wlc_ComputeSum(char *pRes, char *pAdd, int nBits, int Radix)
DECLARATIONS ///.
Definition wlcStdin.c:44
struct Wlc_Ntk_t_ Wlc_Ntk_t
Definition wlc.h:135
Wlc_Ntk_t * Wlc_ReadSmtBuffer(char *pFileName, char *pBuffer, char *pLimit, int fOldParser, int fPrintTree)
void Wlc_SetNtk(Abc_Frame_t *pAbc, Wlc_Ntk_t *pNtk)
Definition wlcCom.c:133