main.c 8.28 KB
Newer Older
Alan Mishchenko committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/**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 $]

***********************************************************************/
20 21

#include "abc.h"
Alan Mishchenko committed
22 23
#include "mainInt.h"

24 25 26 27 28 29 30
#ifdef ABC_PYTHON_EMBED
#include <Python.h>

#endif /* ABC_PYTHON_EMBED */

ABC_NAMESPACE_IMPL_START

Alan Mishchenko committed
31
// this line should be included in the library project
Alan Mishchenko committed
32
//#define ABC_LIB
Alan Mishchenko committed
33

Alan Mishchenko committed
34 35
//#define ABC_USE_BINARY 1

Alan Mishchenko committed
36 37 38
////////////////////////////////////////////////////////////////////////
///                        DECLARATIONS                              ///
////////////////////////////////////////////////////////////////////////
Alan Mishchenko committed
39
 
40
static int TypeCheck( Abc_Frame_t * pAbc, const char * s);
Alan Mishchenko committed
41 42

////////////////////////////////////////////////////////////////////////
Alan Mishchenko committed
43
///                     FUNCTION DEFINITIONS                         ///
Alan Mishchenko committed
44 45
////////////////////////////////////////////////////////////////////////

Alan Mishchenko committed
46
#ifndef ABC_LIB
Alan Mishchenko committed
47

Alan Mishchenko committed
48 49 50 51 52 53 54 55 56 57 58
/**Function*************************************************************

  Synopsis    [The main() procedure.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
59
int Abc_RealMain( int argc, char * argv[] )
Alan Mishchenko committed
60 61
{
    Abc_Frame_t * pAbc;
62
    char sCommandUsr[500], sCommandTmp[100], sReadCmd[20], sWriteCmd[20];
63 64
    const char * sOutFile, * sInFile;
    char * sCommand;
Alan Mishchenko committed
65
    int  fStatus = 0;
66
    int c, fBatch, fInitSource, fInitRead, fFinalWrite;
67 68 69 70
 
    // added to detect memory leaks
    // watch for {,,msvcrtd.dll}*__p__crtBreakAlloc()
    // (http://support.microsoft.com/kb/151585)
Alan Mishchenko committed
71
#if defined(_DEBUG) && defined(_MSC_VER) 
Alan Mishchenko committed
72 73
    _CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
#endif
74
     
Alan Mishchenko committed
75 76
//    Npn_Experiment();
//    Npn_Generate();
Alan Mishchenko committed
77 78 79 80 81

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

82 83 84 85 86 87 88 89 90 91 92 93 94
#ifdef ABC_PYTHON_EMBED
    {
        PyObject* pName;
        PyObject* pModule;
        void init_pyabc(void);

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

        init_pyabc();

        pModule = PyImport_ImportModule("pyabc");
95 96 97 98 99 100 101 102
        if (pModule)
        {
            Py_DECREF(pModule);
        }
        else
        {
            fprintf( pAbc->Err, "error: pyabc.py not found. PYTHONPATH may not be set properly.\n");
        }
103 104 105
    }
#endif /* ABC_PYTHON_EMBED */

Alan Mishchenko committed
106
    // default options
Alan Mishchenko committed
107
    fBatch      = 0;
Alan Mishchenko committed
108 109 110 111
    fInitSource = 1;
    fInitRead   = 0;
    fFinalWrite = 0;
    sInFile = sOutFile = NULL;
Alan Mishchenko committed
112 113
    sprintf( sReadCmd,  "read"  );
    sprintf( sWriteCmd, "write" );
Alan Mishchenko committed
114
    
Alan Mishchenko committed
115 116
    Extra_UtilGetoptReset();
    while ((c = Extra_UtilGetopt(argc, argv, "c:hf:F:o:st:T:x")) != EOF) {
Alan Mishchenko committed
117 118
        switch(c) {
            case 'c':
Alan Mishchenko committed
119
                strcpy( sCommandUsr, globalUtilOptarg );
Alan Mishchenko committed
120 121 122 123
                fBatch = 1;
                break;
                
            case 'f':
Alan Mishchenko committed
124
                sprintf(sCommandUsr, "source %s", globalUtilOptarg);
Alan Mishchenko committed
125 126 127 128
                fBatch = 1;
                break;

            case 'F':
Alan Mishchenko committed
129
                sprintf(sCommandUsr, "source -x %s", globalUtilOptarg);
Alan Mishchenko committed
130 131 132 133 134 135 136 137
                fBatch = 1;
                break;
                
            case 'h':
                goto usage;
                break;
                
            case 'o':
Alan Mishchenko committed
138
                sOutFile = globalUtilOptarg;
Alan Mishchenko committed
139 140 141 142 143 144 145 146
                fFinalWrite = 1;
                break;
                
            case 's':
                fInitSource = 0;
                break;
                
            case 't':
Alan Mishchenko committed
147
                if ( TypeCheck( pAbc, globalUtilOptarg ) )
Alan Mishchenko committed
148
                {
Alan Mishchenko committed
149
                    if ( !strcmp(globalUtilOptarg, "none") == 0 )
Alan Mishchenko committed
150 151
                    {
                        fInitRead = 1;
Alan Mishchenko committed
152
                        sprintf( sReadCmd, "read_%s", globalUtilOptarg );
Alan Mishchenko committed
153 154 155 156 157 158 159 160 161
                    }
                }
                else {
                    goto usage;
                }
                fBatch = 1;
                break;
                
            case 'T':
Alan Mishchenko committed
162
                if ( TypeCheck( pAbc, globalUtilOptarg ) )
Alan Mishchenko committed
163
                {
Alan Mishchenko committed
164
                    if (!strcmp(globalUtilOptarg, "none") == 0)
Alan Mishchenko committed
165 166
                    {
                        fFinalWrite = 1;
Alan Mishchenko committed
167
                        sprintf( sWriteCmd, "write_%s", globalUtilOptarg);
Alan Mishchenko committed
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
                    }
                }
                else {
                    goto usage;
                }
                fBatch = 1;
                break;
                
            case 'x':
                fFinalWrite = 0;
                fInitRead   = 0;
                fBatch = 1;
                break;
                
            default:
                goto usage;
        }
    }
    
    if ( fBatch )
    {
        pAbc->fBatchMode = 1;

Alan Mishchenko committed
191
        if (argc - globalUtilOptind == 0)
Alan Mishchenko committed
192 193 194
        {
            sInFile = NULL;
        }
Alan Mishchenko committed
195
        else if (argc - globalUtilOptind == 1)
Alan Mishchenko committed
196 197
        {
            fInitRead = 1;
Alan Mishchenko committed
198
            sInFile = argv[globalUtilOptind];
Alan Mishchenko committed
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
        }
        else
        {
            Abc_UtilsPrintUsage( pAbc, argv[0] );
        }
        
        // source the resource file
        if ( fInitSource )
        {
            Abc_UtilsSource( pAbc );
        }
        
        fStatus = 0;
        if ( fInitRead && sInFile )
        {
            sprintf( sCommandTmp, "%s %s", sReadCmd, sInFile );
            fStatus = Cmd_CommandExecute( pAbc, sCommandTmp );
        }
        
        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 );
            }
        }
        
    }
Alan Mishchenko committed
230
    else 
Alan Mishchenko committed
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
    {
        // start interactive mode
        // print the hello line
        Abc_UtilsPrintHello( pAbc );
        
        // source the resource file
        if ( fInitSource )
        {
            Abc_UtilsSource( pAbc );
        }
                
        // execute commands given by the user
        while ( !feof(stdin) )
        {
            // print command line prompt and
            // get the command from the user
            sCommand = Abc_UtilsGetUsersInput( pAbc );
            
            // execute the user's command
            fStatus = Cmd_CommandExecute( pAbc, sCommand );
            
            // stop if the user quitted or an error occurred
            if ( fStatus == -1 || fStatus == -2 )
                break;
        }
Alan Mishchenko committed
256
    } 
257 258 259 260 261 262 263

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

Alan Mishchenko committed
264
    // if the memory should be freed, quit packages
265
//    if ( fStatus < 0 ) 
Alan Mishchenko committed
266
    {
Alan Mishchenko committed
267
        Abc_Stop(); 
Alan Mishchenko committed
268 269
    }    
    return 0;  
Alan Mishchenko committed
270 271 272 273 274 275 276

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

Alan Mishchenko committed
277 278
#endif

Alan Mishchenko committed
279 280 281 282
/**Function********************************************************************

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

Alan Mishchenko committed
283 284
  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
285 286 287 288

  SideEffects []

******************************************************************************/
289
static int TypeCheck( Abc_Frame_t * pAbc, const char * s )
Alan Mishchenko committed
290
{
Alan Mishchenko committed
291
    if (strcmp(s, "blif") == 0)
Alan Mishchenko committed
292
        return 1;
Alan Mishchenko committed
293
    else if (strcmp(s, "bench") == 0)
Alan Mishchenko committed
294
        return 1;
Alan Mishchenko committed
295
    else if (strcmp(s, "pla") == 0)
Alan Mishchenko committed
296
        return 1;
Alan Mishchenko committed
297
    else if (strcmp(s, "none") == 0)
Alan Mishchenko committed
298 299 300 301 302 303 304
        return 1;
    else {
        fprintf( pAbc->Err, "unknown type %s\n", s );
        return 0;
    }
}

Alan Mishchenko committed
305 306 307



Alan Mishchenko committed
308 309 310 311 312
////////////////////////////////////////////////////////////////////////
///                       END OF FILE                                ///
////////////////////////////////////////////////////////////////////////


313 314 315 316 317 318 319 320 321 322
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);
}