abc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
mainReal.c
Go to the documentation of this file.
1 /*////////////////////////////////////////////////////////////////////////////
2 
3 ABC: System for Sequential Synthesis and Verification
4 
5 http://www.eecs.berkeley.edu/~alanmi/abc/
6 
7 Copyright (c) The Regents of the University of California. All rights reserved.
8 
9 Permission is hereby granted, without written agreement and without license or
10 royalty fees, to use, copy, modify, and distribute this software and its
11 documentation for any purpose, provided that the above copyright notice and
12 the following two paragraphs appear in all copies of this software.
13 
14 IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
15 DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF
16 THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
17 CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
18 
19 THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
20 BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS" BASIS,
22 AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE,
23 SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
24 
25 ////////////////////////////////////////////////////////////////////////////*/
26 
27 /**CFile****************************************************************
28 
29  FileName [main.c]
30 
31  SystemName [ABC: Logic synthesis and verification system.]
32 
33  PackageName [The main package.]
34 
35  Synopsis [Here everything starts.]
36 
37  Author [Alan Mishchenko]
38 
39  Affiliation [UC Berkeley]
40 
41  Date [Ver. 1.0. Started - June 20, 2005.]
42 
43  Revision [$Id: main.c,v 1.00 2005/06/20 00:00:00 alanmi Exp $]
44 
45 ***********************************************************************/
46 
47 
48 #ifdef ABC_PYTHON_EMBED
49 #include <Python.h>
50 #endif /* ABC_PYTHON_EMBED */
51 
52 #include "base/abc/abc.h"
53 #include "mainInt.h"
54 
55 ABC_NAMESPACE_IMPL_START
56 
57 
58 ////////////////////////////////////////////////////////////////////////
59 /// DECLARATIONS ///
60 ////////////////////////////////////////////////////////////////////////
61 
62 static int TypeCheck( Abc_Frame_t * pAbc, const char * s);
63 
64 ////////////////////////////////////////////////////////////////////////
65 /// FUNCTION DEFINITIONS ///
66 ////////////////////////////////////////////////////////////////////////
67 
68 
69 /**Function*************************************************************
70 
71  Synopsis [The main() procedure.]
72 
73  Description []
74 
75  SideEffects []
76 
77  SeeAlso []
78 
79 ***********************************************************************/
80 int Abc_RealMain( int argc, char * argv[] )
81 {
82  Abc_Frame_t * pAbc;
83  char sCommandUsr[ABC_MAX_STR] = {0}, sCommandTmp[ABC_MAX_STR], sReadCmd[1000], sWriteCmd[1000];
84  const char * sOutFile, * sInFile;
85  char * sCommand;
86  int fStatus = 0;
87  int c, fInitSource, fInitRead, fFinalWrite;
88 
89  enum {
90  INTERACTIVE, // interactive mode
91  BATCH, // batch mode, run a command and quit
92  BATCH_THEN_INTERACTIVE, // run a command, then back to interactive mode
93  BATCH_QUIET // as in batch mode, but don't echo the command
94  } fBatch;
95 
96  // added to detect memory leaks
97  // watch for {,,msvcrtd.dll}*__p__crtBreakAlloc()
98  // (http://support.microsoft.com/kb/151585)
99 #if defined(_DEBUG) && defined(_MSC_VER)
100  _CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
101 #endif
102 
103  // get global frame (singleton pattern)
104  // will be initialized on first call
105  pAbc = Abc_FrameGetGlobalFrame();
106  pAbc->sBinary = argv[0];
107 
108 #ifdef ABC_PYTHON_EMBED
109  {
110  PyObject* pModule;
111  void init_pyabc(void);
112 
113  Py_SetProgramName(argv[0]);
114  Py_NoSiteFlag = 1;
115  Py_Initialize();
116 
117  init_pyabc();
118 
119  pModule = PyImport_ImportModule("pyabc");
120  if (pModule)
121  {
122  Py_DECREF(pModule);
123  }
124  else
125  {
126  fprintf( pAbc->Err, "error: pyabc.py not found. PYTHONPATH may not be set properly.\n");
127  }
128  }
129 #endif /* ABC_PYTHON_EMBED */
130 
131  // default options
132  fBatch = INTERACTIVE;
133  fInitSource = 1;
134  fInitRead = 0;
135  fFinalWrite = 0;
136  sInFile = sOutFile = NULL;
137  sprintf( sReadCmd, "read" );
138  sprintf( sWriteCmd, "write" );
139 
140  Extra_UtilGetoptReset();
141  while ((c = Extra_UtilGetopt(argc, argv, "c:q:C:hf:F:o:st:T:xb")) != EOF) {
142  switch(c) {
143  case 'c':
144  strcpy( sCommandUsr, globalUtilOptarg );
145  fBatch = BATCH;
146  break;
147 
148  case 'q':
149  strcpy( sCommandUsr, globalUtilOptarg );
150  fBatch = BATCH_QUIET;
151  break;
152 
153  case 'C':
154  strcpy( sCommandUsr, globalUtilOptarg );
155  fBatch = BATCH_THEN_INTERACTIVE;
156  break;
157 
158  case 'f':
159  sprintf(sCommandUsr, "source %s", globalUtilOptarg);
160  fBatch = BATCH;
161  break;
162 
163  case 'F':
164  sprintf(sCommandUsr, "source -x %s", globalUtilOptarg);
165  fBatch = BATCH;
166  break;
167 
168  case 'h':
169  goto usage;
170  break;
171 
172  case 'o':
173  sOutFile = globalUtilOptarg;
174  fFinalWrite = 1;
175  break;
176 
177  case 's':
178  fInitSource = 0;
179  break;
180 
181  case 't':
182  if ( TypeCheck( pAbc, globalUtilOptarg ) )
183  {
184  if ( !strcmp(globalUtilOptarg, "none") == 0 )
185  {
186  fInitRead = 1;
187  sprintf( sReadCmd, "read_%s", globalUtilOptarg );
188  }
189  }
190  else {
191  goto usage;
192  }
193  fBatch = BATCH;
194  break;
195 
196  case 'T':
197  if ( TypeCheck( pAbc, globalUtilOptarg ) )
198  {
199  if (!strcmp(globalUtilOptarg, "none") == 0)
200  {
201  fFinalWrite = 1;
202  sprintf( sWriteCmd, "write_%s", globalUtilOptarg);
203  }
204  }
205  else {
206  goto usage;
207  }
208  fBatch = BATCH;
209  break;
210 
211  case 'x':
212  fFinalWrite = 0;
213  fInitRead = 0;
214  fBatch = BATCH;
215  break;
216 
217  case 'b':
218  Abc_FrameSetBridgeMode();
219  break;
220 
221  default:
222  goto usage;
223  }
224  }
225 
226  if ( Abc_FrameIsBridgeMode() )
227  {
228  extern Gia_Man_t * Gia_ManFromBridge( FILE * pFile, Vec_Int_t ** pvInit );
229  pAbc->pGia = Gia_ManFromBridge( stdin, NULL );
230  }
231  else if ( fBatch!=INTERACTIVE && fBatch!=BATCH_QUIET && sCommandUsr[0] )
232  Abc_Print( 1, "ABC command line: \"%s\".\n\n", sCommandUsr );
233 
234  if ( fBatch!=INTERACTIVE )
235  {
236  pAbc->fBatchMode = 1;
237 
238 
239  if (argc - globalUtilOptind == 0)
240  {
241  sInFile = NULL;
242  }
243  else if (argc - globalUtilOptind == 1)
244  {
245  fInitRead = 1;
246  sInFile = argv[globalUtilOptind];
247  }
248  else
249  {
250  Abc_UtilsPrintUsage( pAbc, argv[0] );
251  }
252 
253  // source the resource file
254  if ( fInitSource )
255  {
256  Abc_UtilsSource( pAbc );
257  }
258 
259  fStatus = 0;
260  if ( fInitRead && sInFile )
261  {
262  sprintf( sCommandTmp, "%s %s", sReadCmd, sInFile );
263  fStatus = Cmd_CommandExecute( pAbc, sCommandTmp );
264  }
265 
266  if ( fStatus == 0 )
267  {
268  /* cmd line contains `source <file>' */
269  fStatus = Cmd_CommandExecute( pAbc, sCommandUsr );
270  if ( (fStatus == 0 || fStatus == -1) && fFinalWrite && sOutFile )
271  {
272  sprintf( sCommandTmp, "%s %s", sWriteCmd, sOutFile );
273  fStatus = Cmd_CommandExecute( pAbc, sCommandTmp );
274  }
275  }
276 
277  if (fBatch == BATCH_THEN_INTERACTIVE){
278  fBatch = INTERACTIVE;
279  pAbc->fBatchMode = 0;
280  }
281 
282  }
283 
284  if ( fBatch==INTERACTIVE )
285  {
286  // start interactive mode
287 
288  // print the hello line
289  Abc_UtilsPrintHello( pAbc );
290  // print history of the recent commands
291  Cmd_HistoryPrint( pAbc, 10 );
292 
293  // source the resource file
294  if ( fInitSource )
295  {
296  Abc_UtilsSource( pAbc );
297  }
298 
299  // execute commands given by the user
300  while ( !feof(stdin) )
301  {
302  // print command line prompt and
303  // get the command from the user
304  sCommand = Abc_UtilsGetUsersInput( pAbc );
305 
306  // execute the user's command
307  fStatus = Cmd_CommandExecute( pAbc, sCommand );
308 
309  // stop if the user quitted or an error occurred
310  if ( fStatus == -1 || fStatus == -2 )
311  break;
312  }
313  }
314 
315 #ifdef ABC_PYTHON_EMBED
316  {
317  Py_Finalize();
318  }
319 #endif /* ABC_PYTHON_EMBED */
320 
321  // if the memory should be freed, quit packages
322 // if ( fStatus < 0 )
323  {
324  Abc_Stop();
325  }
326  return 0;
327 
328 usage:
329  Abc_UtilsPrintHello( pAbc );
330  Abc_UtilsPrintUsage( pAbc, argv[0] );
331  return 1;
332 }
333 
334 /**Function********************************************************************
335 
336  Synopsis [Returns 1 if s is a file type recognized, else returns 0.]
337 
338  Description [Returns 1 if s is a file type recognized by ABC, else returns 0.
339  Recognized types are "blif", "bench", "pla", and "none".]
340 
341  SideEffects []
342 
343 ******************************************************************************/
344 static int TypeCheck( Abc_Frame_t * pAbc, const char * s )
345 {
346  if (strcmp(s, "blif") == 0)
347  return 1;
348  else if (strcmp(s, "bench") == 0)
349  return 1;
350  else if (strcmp(s, "pla") == 0)
351  return 1;
352  else if (strcmp(s, "none") == 0)
353  return 1;
354  else {
355  fprintf( pAbc->Err, "unknown type %s\n", s );
356  return 0;
357  }
358 }
359 
360 
361 
362 
363 ////////////////////////////////////////////////////////////////////////
364 /// END OF FILE ///
365 ////////////////////////////////////////////////////////////////////////
366 
367 
368 ABC_NAMESPACE_IMPL_END
369