main.c 10.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
/*////////////////////////////////////////////////////////////////////////////

ABC: System for Sequential Synthesis and Verification

http://www.eecs.berkeley.edu/~alanmi/abc/

Copyright (c) The Regents of the University of California. All rights reserved.

Permission is hereby granted, without written agreement and without license or
royalty fees, to use, copy, modify, and distribute this software and its
documentation for any purpose, provided that the above copyright notice and
the following two paragraphs appear in all copies of this software.

IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF
THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS" BASIS,
AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE,
SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.

////////////////////////////////////////////////////////////////////////////*/

Alan Mishchenko committed
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
/**CFile****************************************************************

  FileName    [main.c]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [The main package.]

  Synopsis    [Here everything starts.]

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

  Date        [Ver. 1.0. Started - June 20, 2005.]

  Revision    [$Id: main.c,v 1.00 2005/06/20 00:00:00 alanmi Exp $]

***********************************************************************/
46

47

48 49 50 51
#ifdef ABC_PYTHON_EMBED
#include <Python.h>
#endif /* ABC_PYTHON_EMBED */

52
#include "base/abc/abc.h"
53 54
#include "mainInt.h"

55 56
ABC_NAMESPACE_IMPL_START

Alan Mishchenko committed
57
// this line should be included in the library project
Alan Mishchenko committed
58
//#define ABC_LIB
Alan Mishchenko committed
59

Alan Mishchenko committed
60 61
//#define ABC_USE_BINARY 1

Alan Mishchenko committed
62 63 64
////////////////////////////////////////////////////////////////////////
///                        DECLARATIONS                              ///
////////////////////////////////////////////////////////////////////////
65

66
static int TypeCheck( Abc_Frame_t * pAbc, const char * s);
Alan Mishchenko committed
67 68

////////////////////////////////////////////////////////////////////////
Alan Mishchenko committed
69
///                     FUNCTION DEFINITIONS                         ///
Alan Mishchenko committed
70 71
////////////////////////////////////////////////////////////////////////

Alan Mishchenko committed
72
#ifndef ABC_LIB
Alan Mishchenko committed
73

Alan Mishchenko committed
74 75 76 77 78 79 80 81 82 83 84
/**Function*************************************************************

  Synopsis    [The main() procedure.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
85
int Abc_RealMain( int argc, char * argv[] )
Alan Mishchenko committed
86 87
{
    Abc_Frame_t * pAbc;
88
    char sCommandUsr[ABC_MAX_STR] = {0}, sCommandTmp[ABC_MAX_STR], sReadCmd[1000], sWriteCmd[1000];
89 90
    const char * sOutFile, * sInFile;
    char * sCommand;
Alan Mishchenko committed
91
    int  fStatus = 0;
92 93 94 95 96 97 98 99
    int c, fInitSource, fInitRead, fFinalWrite;

    enum {
        INTERACTIVE, // interactive mode
        BATCH, // batch mode, run a command and quit
        BATCH_THEN_INTERACTIVE, // run a command, then back to interactive mode
        BATCH_QUIET // as in batch mode, but don't echo the command
    } fBatch;
100

101 102 103
    // added to detect memory leaks
    // watch for {,,msvcrtd.dll}*__p__crtBreakAlloc()
    // (http://support.microsoft.com/kb/151585)
104
#if defined(_DEBUG) && defined(_MSC_VER)
Alan Mishchenko committed
105 106
    _CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
#endif
107

Alan Mishchenko committed
108 109 110
    // get global frame (singleton pattern)
    // will be initialized on first call
    pAbc = Abc_FrameGetGlobalFrame();
111
    pAbc->sBinary = argv[0];
Alan Mishchenko committed
112

113 114 115 116 117 118 119 120 121 122 123 124
#ifdef ABC_PYTHON_EMBED
    {
        PyObject* pModule;
        void init_pyabc(void);

        Py_SetProgramName(argv[0]);
        Py_NoSiteFlag = 1;
        Py_Initialize();

        init_pyabc();

        pModule = PyImport_ImportModule("pyabc");
125 126 127 128 129 130 131 132
        if (pModule)
        {
            Py_DECREF(pModule);
        }
        else
        {
            fprintf( pAbc->Err, "error: pyabc.py not found. PYTHONPATH may not be set properly.\n");
        }
133 134 135
    }
#endif /* ABC_PYTHON_EMBED */

Alan Mishchenko committed
136
    // default options
137
    fBatch      = INTERACTIVE;
Alan Mishchenko committed
138 139 140 141
    fInitSource = 1;
    fInitRead   = 0;
    fFinalWrite = 0;
    sInFile = sOutFile = NULL;
Alan Mishchenko committed
142 143
    sprintf( sReadCmd,  "read"  );
    sprintf( sWriteCmd, "write" );
144

Alan Mishchenko committed
145
    Extra_UtilGetoptReset();
146
    while ((c = Extra_UtilGetopt(argc, argv, "c:q:C:hf:F:o:st:T:xb")) != EOF) {
Alan Mishchenko committed
147 148
        switch(c) {
            case 'c':
Alan Mishchenko committed
149
                strcpy( sCommandUsr, globalUtilOptarg );
150 151 152 153 154 155
                fBatch = BATCH;
                break;

            case 'q':
                strcpy( sCommandUsr, globalUtilOptarg );
                fBatch = BATCH_QUIET;
Alan Mishchenko committed
156
                break;
157 158 159

            case 'C':
                strcpy( sCommandUsr, globalUtilOptarg );
160
                fBatch = BATCH_THEN_INTERACTIVE;
161 162
                break;

Alan Mishchenko committed
163
            case 'f':
Alan Mishchenko committed
164
                sprintf(sCommandUsr, "source %s", globalUtilOptarg);
165
                fBatch = BATCH;
Alan Mishchenko committed
166 167 168
                break;

            case 'F':
Alan Mishchenko committed
169
                sprintf(sCommandUsr, "source -x %s", globalUtilOptarg);
170
                fBatch = BATCH;
Alan Mishchenko committed
171
                break;
172

Alan Mishchenko committed
173 174 175
            case 'h':
                goto usage;
                break;
176

Alan Mishchenko committed
177
            case 'o':
Alan Mishchenko committed
178
                sOutFile = globalUtilOptarg;
Alan Mishchenko committed
179 180
                fFinalWrite = 1;
                break;
181

Alan Mishchenko committed
182 183 184
            case 's':
                fInitSource = 0;
                break;
185

Alan Mishchenko committed
186
            case 't':
Alan Mishchenko committed
187
                if ( TypeCheck( pAbc, globalUtilOptarg ) )
Alan Mishchenko committed
188
                {
Alan Mishchenko committed
189
                    if ( !strcmp(globalUtilOptarg, "none") == 0 )
Alan Mishchenko committed
190 191
                    {
                        fInitRead = 1;
Alan Mishchenko committed
192
                        sprintf( sReadCmd, "read_%s", globalUtilOptarg );
Alan Mishchenko committed
193 194 195 196 197
                    }
                }
                else {
                    goto usage;
                }
198
                fBatch = BATCH;
Alan Mishchenko committed
199
                break;
200

Alan Mishchenko committed
201
            case 'T':
Alan Mishchenko committed
202
                if ( TypeCheck( pAbc, globalUtilOptarg ) )
Alan Mishchenko committed
203
                {
Alan Mishchenko committed
204
                    if (!strcmp(globalUtilOptarg, "none") == 0)
Alan Mishchenko committed
205 206
                    {
                        fFinalWrite = 1;
Alan Mishchenko committed
207
                        sprintf( sWriteCmd, "write_%s", globalUtilOptarg);
Alan Mishchenko committed
208 209 210 211 212
                    }
                }
                else {
                    goto usage;
                }
213
                fBatch = BATCH;
Alan Mishchenko committed
214
                break;
215

Alan Mishchenko committed
216 217 218
            case 'x':
                fFinalWrite = 0;
                fInitRead   = 0;
219
                fBatch = BATCH;
Alan Mishchenko committed
220
                break;
221

222 223 224
            case 'b':
                Abc_FrameSetBridgeMode();
                break;
225

Alan Mishchenko committed
226 227 228 229
            default:
                goto usage;
        }
    }
230 231 232 233 234 235

    if ( Abc_FrameIsBridgeMode() )
    {
        extern Gia_Man_t * Gia_ManFromBridge( FILE * pFile, Vec_Int_t ** pvInit );
        pAbc->pGia = Gia_ManFromBridge( stdin, NULL );
    }
236
    else if ( fBatch!=INTERACTIVE && fBatch!=BATCH_QUIET && sCommandUsr[0] )
237
        Abc_Print( 1, "ABC command line: \"%s\".\n\n", sCommandUsr );
238

239
    if ( fBatch!=INTERACTIVE )
Alan Mishchenko committed
240 241 242
    {
        pAbc->fBatchMode = 1;

243

Alan Mishchenko committed
244
        if (argc - globalUtilOptind == 0)
Alan Mishchenko committed
245 246 247
        {
            sInFile = NULL;
        }
Alan Mishchenko committed
248
        else if (argc - globalUtilOptind == 1)
Alan Mishchenko committed
249 250
        {
            fInitRead = 1;
Alan Mishchenko committed
251
            sInFile = argv[globalUtilOptind];
Alan Mishchenko committed
252 253 254 255 256
        }
        else
        {
            Abc_UtilsPrintUsage( pAbc, argv[0] );
        }
257

Alan Mishchenko committed
258 259 260 261 262
        // source the resource file
        if ( fInitSource )
        {
            Abc_UtilsSource( pAbc );
        }
263

Alan Mishchenko committed
264 265 266 267 268 269
        fStatus = 0;
        if ( fInitRead && sInFile )
        {
            sprintf( sCommandTmp, "%s %s", sReadCmd, sInFile );
            fStatus = Cmd_CommandExecute( pAbc, sCommandTmp );
        }
270

Alan Mishchenko committed
271 272 273 274 275 276 277 278 279 280
        if ( fStatus == 0 )
        {
            /* cmd line contains `source <file>' */
            fStatus = Cmd_CommandExecute( pAbc, sCommandUsr );
            if ( (fStatus == 0 || fStatus == -1) && fFinalWrite && sOutFile )
            {
                sprintf( sCommandTmp, "%s %s", sWriteCmd, sOutFile );
                fStatus = Cmd_CommandExecute( pAbc, sCommandTmp );
            }
        }
281

282 283
        if (fBatch == BATCH_THEN_INTERACTIVE){
            fBatch = INTERACTIVE;
284 285 286
            pAbc->fBatchMode = 0;
        }

Alan Mishchenko committed
287
    }
288

289
    if ( fBatch==INTERACTIVE )
Alan Mishchenko committed
290 291
    {
        // start interactive mode
292

Alan Mishchenko committed
293 294
        // print the hello line
        Abc_UtilsPrintHello( pAbc );
295 296
        // print history of the recent commands
        Cmd_HistoryPrint( pAbc, 10 );
297

Alan Mishchenko committed
298 299 300 301 302
        // source the resource file
        if ( fInitSource )
        {
            Abc_UtilsSource( pAbc );
        }
303

Alan Mishchenko committed
304 305 306 307 308 309
        // execute commands given by the user
        while ( !feof(stdin) )
        {
            // print command line prompt and
            // get the command from the user
            sCommand = Abc_UtilsGetUsersInput( pAbc );
310

Alan Mishchenko committed
311 312
            // execute the user's command
            fStatus = Cmd_CommandExecute( pAbc, sCommand );
313

Alan Mishchenko committed
314 315 316 317
            // stop if the user quitted or an error occurred
            if ( fStatus == -1 || fStatus == -2 )
                break;
        }
318
    }
319 320 321 322 323 324 325

#ifdef ABC_PYTHON_EMBED
    {
        Py_Finalize();
    }
#endif /* ABC_PYTHON_EMBED */

Alan Mishchenko committed
326
    // if the memory should be freed, quit packages
327
//    if ( fStatus < 0 ) 
Alan Mishchenko committed
328
    {
329 330 331
        Abc_Stop();
    }
    return 0;
Alan Mishchenko committed
332 333 334 335 336 337 338

usage:
    Abc_UtilsPrintHello( pAbc );
    Abc_UtilsPrintUsage( pAbc, argv[0] );
    return 1;
}

Alan Mishchenko committed
339 340
#endif

Alan Mishchenko committed
341 342 343 344
/**Function********************************************************************

  Synopsis    [Returns 1 if s is a file type recognized, else returns 0.]

Alan Mishchenko committed
345 346
  Description [Returns 1 if s is a file type recognized by ABC, else returns 0. 
  Recognized types are "blif", "bench", "pla", and "none".]
Alan Mishchenko committed
347 348 349 350

  SideEffects []

******************************************************************************/
351
static int TypeCheck( Abc_Frame_t * pAbc, const char * s )
Alan Mishchenko committed
352
{
Alan Mishchenko committed
353
    if (strcmp(s, "blif") == 0)
Alan Mishchenko committed
354
        return 1;
Alan Mishchenko committed
355
    else if (strcmp(s, "bench") == 0)
Alan Mishchenko committed
356
        return 1;
Alan Mishchenko committed
357
    else if (strcmp(s, "pla") == 0)
Alan Mishchenko committed
358
        return 1;
Alan Mishchenko committed
359
    else if (strcmp(s, "none") == 0)
Alan Mishchenko committed
360 361 362 363 364 365 366
        return 1;
    else {
        fprintf( pAbc->Err, "unknown type %s\n", s );
        return 0;
    }
}

Alan Mishchenko committed
367 368 369



Alan Mishchenko committed
370 371 372 373 374
////////////////////////////////////////////////////////////////////////
///                       END OF FILE                                ///
////////////////////////////////////////////////////////////////////////


375 376 377 378 379 380 381 382 383 384
ABC_NAMESPACE_IMPL_END

#if defined(ABC_USE_BINARY)
int main_( int argc, char * argv[] )
#else
int main( int argc, char * argv[] )
#endif
{
  return ABC_NAMESPACE_PREFIX Abc_RealMain(argc, argv);
}