main.c 9.98 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 103
    // get global frame (singleton pattern)
    // will be initialized on first call
    pAbc = Abc_FrameGetGlobalFrame();
104
    pAbc->sBinary = argv[0];
Alan Mishchenko committed
105

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

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

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

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

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

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

Alan Mishchenko committed
161 162 163
            case 'h':
                goto usage;
                break;
164

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

Alan Mishchenko committed
170 171 172
            case 's':
                fInitSource = 0;
                break;
173

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

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

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

210 211 212
            case 'b':
                Abc_FrameSetBridgeMode();
                break;
213

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

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

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

231

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

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

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

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

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

Alan Mishchenko committed
275
    }
276 277

    if ( !fBatch )
Alan Mishchenko committed
278 279
    {
        // start interactive mode
280

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

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

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

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

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

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

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

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

Alan Mishchenko committed
327 328
#endif

Alan Mishchenko committed
329 330 331 332
/**Function********************************************************************

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

Alan Mishchenko committed
333 334
  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
335 336 337 338

  SideEffects []

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

Alan Mishchenko committed
355 356 357



Alan Mishchenko committed
358 359 360 361 362
////////////////////////////////////////////////////////////////////////
///                       END OF FILE                                ///
////////////////////////////////////////////////////////////////////////


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