1 /**CFile****************************************************************
2 
3   FileName    [cmdApi.c]
4 
5   SystemName  [ABC: Logic synthesis and verification system.]
6 
7   PackageName [Command processing package.]
8 
9   Synopsis    [External procedures of the command package.]
10 
11   Author      [Alan Mishchenko]
12 
13   Affiliation [UC Berkeley]
14 
15   Date        [Ver. 1.0. Started - June 20, 2005.]
16 
17   Revision    [$Id: cmdApi.c,v 1.00 2005/06/20 00:00:00 alanmi Exp $]
18 
19 ***********************************************************************/
20 
21 #include "base/abc/abc.h"
22 #include "base/main/mainInt.h"
23 #include "cmdInt.h"
24 
25 ABC_NAMESPACE_IMPL_START
26 
27 
28 ////////////////////////////////////////////////////////////////////////
29 ///                        DECLARATIONS                              ///
30 ////////////////////////////////////////////////////////////////////////
31 
32 ////////////////////////////////////////////////////////////////////////
33 ///                     FUNCTION DEFINITIONS                         ///
34 ////////////////////////////////////////////////////////////////////////
35 
36 /**Function*************************************************************
37 
38   Synopsis    []
39 
40   Description []
41 
42   SideEffects []
43 
44   SeeAlso     []
45 
46 ***********************************************************************/
Cmd_CommandIsDefined(Abc_Frame_t * pAbc,const char * sName)47 int Cmd_CommandIsDefined( Abc_Frame_t * pAbc, const char * sName )
48 {
49     return st__is_member( pAbc->tCommands, sName );
50 }
51 
52 /**Function*************************************************************
53 
54   Synopsis    []
55 
56   Description []
57 
58   SideEffects []
59 
60   SeeAlso     []
61 
62 ***********************************************************************/
Cmd_CommandAdd(Abc_Frame_t * pAbc,const char * sGroup,const char * sName,Cmd_CommandFuncType pFunc,int fChanges)63 void Cmd_CommandAdd( Abc_Frame_t * pAbc, const char * sGroup, const char * sName, Cmd_CommandFuncType pFunc, int fChanges )
64 {
65     const char * key;
66     char * value;
67     Abc_Command * pCommand;
68     int fStatus;
69 
70     key = sName;
71     if ( st__delete( pAbc->tCommands, &key, &value ) )
72     {
73         // delete existing definition for this command
74         fprintf( pAbc->Err, "Cmd warning: redefining '%s'\n", sName );
75         CmdCommandFree( (Abc_Command *)value );
76     }
77 
78     // create the new command
79     pCommand = ABC_ALLOC( Abc_Command, 1 );
80     pCommand->sName   = Extra_UtilStrsav( sName );
81     pCommand->sGroup  = Extra_UtilStrsav( sGroup );
82     pCommand->pFunc   = pFunc;
83     pCommand->fChange = fChanges;
84     fStatus = st__insert( pAbc->tCommands, pCommand->sName, (char *)pCommand );
85     assert( !fStatus );  // the command should not be in the table
86 }
87 
88 /**Function*************************************************************
89 
90   Synopsis    []
91 
92   Description []
93 
94   SideEffects []
95 
96   SeeAlso     []
97 
98 ***********************************************************************/
Cmd_CommandHandleSpecial(Abc_Frame_t * pAbc,const char * sCommand)99 int Cmd_CommandHandleSpecial( Abc_Frame_t * pAbc, const char * sCommand )
100 {
101     Abc_Ntk_t * pNtk = Abc_FrameReadNtk(pAbc);
102     int piCountNew = (pNtk && Abc_NtkHasMapping(pNtk)) ? Abc_NtkCiNum(pNtk)         : 0, piCount = 0;
103     int poCountNew = (pNtk && Abc_NtkHasMapping(pNtk)) ? Abc_NtkCoNum(pNtk)         : 0, poCount = 0;
104     int ndCountNew = (pNtk && Abc_NtkHasMapping(pNtk)) ? Abc_NtkNodeNum(pNtk)       : 0, ndCount = 0;
105     double AreaNew = (pNtk && Abc_NtkHasMapping(pNtk)) ? Abc_NtkGetMappedArea(pNtk) : 0, Area    = 0;
106     int DepthNew   = (pNtk && Abc_NtkHasMapping(pNtk)) ? Abc_NtkLevel(pNtk)         : 0, Depth   = 0;
107     if ( strstr(sCommand, "#PS") )
108     {
109         printf( "pi=%d ",   piCountNew );
110         printf( "po=%d ",   poCountNew );
111         printf( "fn=%d ",   ndCountNew );
112         printf( "ma=%.1f ", AreaNew    );
113         printf( "de=%d ",   DepthNew   );
114         printf( "\n" );
115         return 1;
116     }
117     if ( strstr(sCommand, "#CEC") )
118     {
119         //int proofStatus = Abc_NtkVerifyUsingCec(pNtk);
120         int proofStatus = 1;
121         // -1 (undecided), 0 (different), 1 (equivalent)
122         printf( "proofStatus=%d\n", proofStatus );
123         return 1;
124     }
125     if ( strstr(sCommand, "#ASSERT") )
126     {
127         int Status    = 0;
128         char * pNumb  = strrchr( sCommand, '=' );
129         if ( strstr(sCommand, "_PI_") )
130         {
131             piCount = pNumb ? atoi(pNumb+1) : 0;
132             if ( strstr( sCommand, "==" ) )
133                 Status = piCountNew == piCount;
134             else if ( strstr( sCommand, "<=" ) )
135                 Status = piCountNew <= piCount;
136             else return 0;
137         }
138         else if ( strstr(sCommand, "_PO_") )
139         {
140             poCount = pNumb ? atoi(pNumb+1) : 0;
141             if ( strstr( sCommand, "==" ) )
142                 Status = poCountNew == poCount;
143             else if ( strstr( sCommand, "<=" ) )
144                 Status = poCountNew <= poCount;
145             else return 0;
146         }
147         else if ( strstr(sCommand, "_NODE_") )
148         {
149             ndCount = pNumb ? atoi(pNumb+1) : 0;
150             if ( strstr( sCommand, "==" ) )
151                 Status = ndCountNew == ndCount;
152             else if ( strstr( sCommand, "<=" ) )
153                 Status = ndCountNew <= ndCount;
154             else return 0;
155         }
156         else if ( strstr(sCommand, "_AREA_") )
157         {
158             double Eplison = 1.0;
159             Area = pNumb ? atof(pNumb+1) : 0;
160             if ( strstr( sCommand, "==" ) )
161                 Status = AreaNew >= Area - Eplison && AreaNew <= Area + Eplison;
162             else if ( strstr( sCommand, "<=" ) )
163                 Status = AreaNew <= Area + Eplison;
164             else return 0;
165         }
166         else if ( strstr(sCommand, "_DEPTH_") )
167         {
168             Depth = pNumb ? atoi(pNumb+1) : 0;
169             if ( strstr( sCommand, "==" ) )
170                 Status = DepthNew == Depth;
171             else if ( strstr( sCommand, "<=" ) )
172                 Status = DepthNew <= Depth;
173             else return 0;
174         }
175         else return 0;
176         printf( "%s\n", Status ? "succeeded" : "failed" );
177         return 1;
178     }
179     return 0;
180 }
181 
182 /**Function*************************************************************
183 
184   Synopsis    []
185 
186   Description []
187 
188   SideEffects []
189 
190   SeeAlso     []
191 
192 ***********************************************************************/
Cmd_CommandExecute(Abc_Frame_t * pAbc,const char * sCommand)193 int Cmd_CommandExecute( Abc_Frame_t * pAbc, const char * sCommand )
194 {
195     int fStatus = 0, argc, loop;
196     const char * sCommandNext;
197     char **argv;
198 
199     if ( !pAbc->fAutoexac && !pAbc->fSource )
200         Cmd_HistoryAddCommand(pAbc, sCommand);
201     sCommandNext = sCommand;
202     do
203     {
204         if ( sCommandNext[0] == '#' && Cmd_CommandHandleSpecial( pAbc, sCommandNext ) )
205             break;
206         sCommandNext = CmdSplitLine( pAbc, sCommandNext, &argc, &argv );
207         loop = 0;
208         fStatus = CmdApplyAlias( pAbc, &argc, &argv, &loop );
209         if ( fStatus == 0 )
210             fStatus = CmdCommandDispatch( pAbc, &argc, &argv );
211         CmdFreeArgv( argc, argv );
212     }
213     while ( fStatus == 0 && *sCommandNext != '\0' );
214     return fStatus;
215 }
216 
217 ////////////////////////////////////////////////////////////////////////
218 ///                       END OF FILE                                ///
219 ////////////////////////////////////////////////////////////////////////
220 
221 
222 ABC_NAMESPACE_IMPL_END
223 
224