main.c 10 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[500] = {0}, sCommandTmp[100], sReadCmd[20], sWriteCmd[20];
89 90
    const char * sOutFile, * sInFile;
    char * sCommand;
Alan Mishchenko committed
91
    int  fStatus = 0;
92
    int c, fBatch, fInitSource, fInitRead, fFinalWrite;
93

94 95 96
    // added to detect memory leaks
    // watch for {,,msvcrtd.dll}*__p__crtBreakAlloc()
    // (http://support.microsoft.com/kb/151585)
97
#if defined(_DEBUG) && defined(_MSC_VER)
Alan Mishchenko committed
98 99
    _CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
#endif
100

Alan Mishchenko committed
101 102
//    Npn_Experiment();
//    Npn_Generate();
Alan Mishchenko committed
103 104 105 106 107

    // get global frame (singleton pattern)
    // will be initialized on first call
    pAbc = Abc_FrameGetGlobalFrame();

108 109 110 111 112 113 114 115 116 117 118 119
#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");
120 121 122 123 124 125 126 127
        if (pModule)
        {
            Py_DECREF(pModule);
        }
        else
        {
            fprintf( pAbc->Err, "error: pyabc.py not found. PYTHONPATH may not be set properly.\n");
        }
128 129 130
    }
#endif /* ABC_PYTHON_EMBED */

Alan Mishchenko committed
131
    // default options
Alan Mishchenko committed
132
    fBatch      = 0;
Alan Mishchenko committed
133 134 135 136
    fInitSource = 1;
    fInitRead   = 0;
    fFinalWrite = 0;
    sInFile = sOutFile = NULL;
Alan Mishchenko committed
137 138
    sprintf( sReadCmd,  "read"  );
    sprintf( sWriteCmd, "write" );
139

Alan Mishchenko committed
140
    Extra_UtilGetoptReset();
141
    while ((c = Extra_UtilGetopt(argc, argv, "c:C:hf:F:o:st:T:xb")) != EOF) {
Alan Mishchenko committed
142 143
        switch(c) {
            case 'c':
Alan Mishchenko committed
144
                strcpy( sCommandUsr, globalUtilOptarg );
Alan Mishchenko committed
145 146
                fBatch = 1;
                break;
147 148 149 150 151 152

            case 'C':
                strcpy( sCommandUsr, globalUtilOptarg );
                fBatch = 2;
                break;

Alan Mishchenko committed
153
            case 'f':
Alan Mishchenko committed
154
                sprintf(sCommandUsr, "source %s", globalUtilOptarg);
Alan Mishchenko committed
155 156 157 158
                fBatch = 1;
                break;

            case 'F':
Alan Mishchenko committed
159
                sprintf(sCommandUsr, "source -x %s", globalUtilOptarg);
Alan Mishchenko committed
160 161
                fBatch = 1;
                break;
162

Alan Mishchenko committed
163 164 165
            case 'h':
                goto usage;
                break;
166

Alan Mishchenko committed
167
            case 'o':
Alan Mishchenko committed
168
                sOutFile = globalUtilOptarg;
Alan Mishchenko committed
169 170
                fFinalWrite = 1;
                break;
171

Alan Mishchenko committed
172 173 174
            case 's':
                fInitSource = 0;
                break;
175

Alan Mishchenko committed
176
            case 't':
Alan Mishchenko committed
177
                if ( TypeCheck( pAbc, globalUtilOptarg ) )
Alan Mishchenko committed
178
                {
Alan Mishchenko committed
179
                    if ( !strcmp(globalUtilOptarg, "none") == 0 )
Alan Mishchenko committed
180 181
                    {
                        fInitRead = 1;
Alan Mishchenko committed
182
                        sprintf( sReadCmd, "read_%s", globalUtilOptarg );
Alan Mishchenko committed
183 184 185 186 187 188 189
                    }
                }
                else {
                    goto usage;
                }
                fBatch = 1;
                break;
190

Alan Mishchenko committed
191
            case 'T':
Alan Mishchenko committed
192
                if ( TypeCheck( pAbc, globalUtilOptarg ) )
Alan Mishchenko committed
193
                {
Alan Mishchenko committed
194
                    if (!strcmp(globalUtilOptarg, "none") == 0)
Alan Mishchenko committed
195 196
                    {
                        fFinalWrite = 1;
Alan Mishchenko committed
197
                        sprintf( sWriteCmd, "write_%s", globalUtilOptarg);
Alan Mishchenko committed
198 199 200 201 202 203 204
                    }
                }
                else {
                    goto usage;
                }
                fBatch = 1;
                break;
205

Alan Mishchenko committed
206 207 208 209 210
            case 'x':
                fFinalWrite = 0;
                fInitRead   = 0;
                fBatch = 1;
                break;
211

212 213 214
            case 'b':
                Abc_FrameSetBridgeMode();
                break;
215

Alan Mishchenko committed
216 217 218 219
            default:
                goto usage;
        }
    }
220 221 222 223 224 225

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

Alan Mishchenko committed
229 230 231 232
    if ( fBatch )
    {
        pAbc->fBatchMode = 1;

233

Alan Mishchenko committed
234
        if (argc - globalUtilOptind == 0)
Alan Mishchenko committed
235 236 237
        {
            sInFile = NULL;
        }
Alan Mishchenko committed
238
        else if (argc - globalUtilOptind == 1)
Alan Mishchenko committed
239 240
        {
            fInitRead = 1;
Alan Mishchenko committed
241
            sInFile = argv[globalUtilOptind];
Alan Mishchenko committed
242 243 244 245 246
        }
        else
        {
            Abc_UtilsPrintUsage( pAbc, argv[0] );
        }
247

Alan Mishchenko committed
248 249 250 251 252
        // source the resource file
        if ( fInitSource )
        {
            Abc_UtilsSource( pAbc );
        }
253

Alan Mishchenko committed
254 255 256 257 258 259
        fStatus = 0;
        if ( fInitRead && sInFile )
        {
            sprintf( sCommandTmp, "%s %s", sReadCmd, sInFile );
            fStatus = Cmd_CommandExecute( pAbc, sCommandTmp );
        }
260

Alan Mishchenko committed
261 262 263 264 265 266 267 268 269 270
        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 );
            }
        }
271 272 273 274 275 276

        if (fBatch == 2){
            fBatch = 0;
            pAbc->fBatchMode = 0;
        }

Alan Mishchenko committed
277
    }
278 279

    if ( !fBatch )
Alan Mishchenko committed
280 281
    {
        // start interactive mode
282

Alan Mishchenko committed
283 284
        // print the hello line
        Abc_UtilsPrintHello( pAbc );
285 286
        // print history of the recent commands
        Cmd_HistoryPrint( pAbc, 10 );
287

Alan Mishchenko committed
288 289 290 291 292
        // source the resource file
        if ( fInitSource )
        {
            Abc_UtilsSource( pAbc );
        }
293

Alan Mishchenko committed
294 295 296 297 298 299
        // execute commands given by the user
        while ( !feof(stdin) )
        {
            // print command line prompt and
            // get the command from the user
            sCommand = Abc_UtilsGetUsersInput( pAbc );
300

Alan Mishchenko committed
301 302
            // execute the user's command
            fStatus = Cmd_CommandExecute( pAbc, sCommand );
303

Alan Mishchenko committed
304 305 306 307
            // stop if the user quitted or an error occurred
            if ( fStatus == -1 || fStatus == -2 )
                break;
        }
308
    }
309 310 311 312 313 314 315

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

Alan Mishchenko committed
316
    // if the memory should be freed, quit packages
317
//    if ( fStatus < 0 ) 
Alan Mishchenko committed
318
    {
319 320 321
        Abc_Stop();
    }
    return 0;
Alan Mishchenko committed
322 323 324 325 326 327 328

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

Alan Mishchenko committed
329 330
#endif

Alan Mishchenko committed
331 332 333 334
/**Function********************************************************************

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

Alan Mishchenko committed
335 336
  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
337 338 339 340

  SideEffects []

******************************************************************************/
341
static int TypeCheck( Abc_Frame_t * pAbc, const char * s )
Alan Mishchenko committed
342
{
Alan Mishchenko committed
343
    if (strcmp(s, "blif") == 0)
Alan Mishchenko committed
344
        return 1;
Alan Mishchenko committed
345
    else if (strcmp(s, "bench") == 0)
Alan Mishchenko committed
346
        return 1;
Alan Mishchenko committed
347
    else if (strcmp(s, "pla") == 0)
Alan Mishchenko committed
348
        return 1;
Alan Mishchenko committed
349
    else if (strcmp(s, "none") == 0)
Alan Mishchenko committed
350 351 352 353 354 355 356
        return 1;
    else {
        fprintf( pAbc->Err, "unknown type %s\n", s );
        return 0;
    }
}

Alan Mishchenko committed
357 358 359



Alan Mishchenko committed
360 361 362 363 364
////////////////////////////////////////////////////////////////////////
///                       END OF FILE                                ///
////////////////////////////////////////////////////////////////////////


365 366 367 368 369 370 371 372 373 374
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);
}