summaryrefslogtreecommitdiffstats
path: root/src/aig/gia/giaGig.c
blob: b1696b09d39ed4d341d192b5705fa6bbf4598211 (plain)
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
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
230
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
/**CFile****************************************************************

  FileName    [giaGig.c]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [Scalable AIG package.]

  Synopsis    [Parser for Gate-Inverter Graph by Niklas Een.]

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

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

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

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

#include "gia.h"
#include "misc/extra/extra.h"
#include "misc/util/utilTruth.h"

ABC_NAMESPACE_IMPL_START


////////////////////////////////////////////////////////////////////////
///                        DECLARATIONS                              ///
////////////////////////////////////////////////////////////////////////

#define MAX_LINE 1000000

// network types
enum { 
    GLS_NONE = -1,  // not used
    GLS_ZERO =  0,  // zero
    GLS_ONE  =  1,  // one
    GLS_PI   =  2,  // primary input
    GLS_PO   =  3,  // primary output
    GLS_BAR  =  4,  // barrier
    GLS_SEQ  =  5,  // sequential
    GLS_SEL  =  6,  // fan
    GLS_LUT4 =  7,  // LUT4
    GLS_LUT6 =  8,  // LUT6
    GLS_BOX  =  9,  // sequential box
    GLS_DEL  = 10,  // delay box
    GLS_FINAL
};

static char * s_Strs[GLS_FINAL] =
{
    "0",     // GLS_ZERO =  0,  // zero
    "1",     // GLS_ONE  =  1,  // one
    "PI",    // GLS_PI   =  2,  // primary input
    "PO",    // GLS_PO   =  3,  // primary output
    "Bar",   // GLS_BAR  =  4,  // barrier
    "Seq",   // GLS_SEQ  =  5,  // sequential
    "Sel",   // GLS_SEL  =  6,  // fan
    "Lut4",  // GLS_LUT4 =  7,  // LUT4
    "Lut6",  // GLS_LUT6 =  8,  // LUT6
    "Box",   // GLS_BOX  =  9,  // sequential box
    "Del"    // GLS_DEL  = 10,  // delay box
};

typedef struct Gls_Man_t_ Gls_Man_t;
struct Gls_Man_t_
{
    // general
    Vec_Str_t *    vLines;       // line types
    Vec_Str_t *    vTypes;       // gate types
    Vec_Int_t *    vIndexes;     // gate indexes
    // specific types
    Vec_Int_t *    vLut4s;       // 4-LUTs (4-tuples)
    Vec_Int_t *    vLut4TTs;     // truth tables
    Vec_Int_t *    vLut6s;       // 6-LUTs (6-tuples)
    Vec_Wrd_t *    vLut6TTs;     // truth tables
    Vec_Int_t *    vBoxes;       // boxes (5-tuples)
    Vec_Wec_t *    vDelayIns;    // delay fanins
    Vec_Wec_t *    vDelayOuts;   // delay fanouts
    Vec_Int_t *    vDelays;      // delay values
    // ordering
    Vec_Int_t *    vOrderPis;  
    Vec_Int_t *    vOrderPos;  
    Vec_Int_t *    vOrderBoxes;  
    Vec_Int_t *    vOrderDelays;  
    Vec_Int_t *    vOrderLuts;  
    Vec_Int_t *    vOrderSeqs;  
};

////////////////////////////////////////////////////////////////////////
///                     FUNCTION DEFINITIONS                         ///
////////////////////////////////////////////////////////////////////////

/**Function*************************************************************

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Gls_Man_t * Gls_ManAlloc( Vec_Str_t * vLines, int * pCounts )
{
    Gls_Man_t * p = ABC_CALLOC( Gls_Man_t, 1 );
    p->vLines     = vLines;
    p->vTypes     = Vec_StrStart( Vec_StrSize(vLines)+100 ); 
    p->vIndexes   = Vec_IntStart( Vec_StrSize(vLines)+100 ); 
    p->vLut4s     = Vec_IntAlloc( 4 * pCounts[GLS_LUT4] );
    p->vLut4TTs   = Vec_IntAlloc( pCounts[GLS_LUT4] );
    p->vLut6s     = Vec_IntAlloc( 6 * pCounts[GLS_LUT6] );
    p->vLut6TTs   = Vec_WrdAlloc( pCounts[GLS_LUT6] );
    p->vBoxes     = Vec_IntAlloc( 5 * pCounts[GLS_BOX] );
    p->vDelays    = Vec_IntAlloc( pCounts[GLS_DEL] );
    p->vDelayIns  = Vec_WecAlloc( pCounts[GLS_DEL] );
    p->vDelayOuts = Vec_WecAlloc( pCounts[GLS_DEL] );
    // ordering
    p->vOrderPis    = Vec_IntAlloc( pCounts[GLS_PI] );
    p->vOrderPos    = Vec_IntAlloc( pCounts[GLS_PO] );
    p->vOrderBoxes  = Vec_IntAlloc( pCounts[GLS_BOX] );
    p->vOrderDelays = Vec_IntAlloc( pCounts[GLS_DEL] );
    p->vOrderLuts   = Vec_IntAlloc( pCounts[GLS_LUT4] + pCounts[GLS_LUT6] + 2*pCounts[GLS_BAR] );
    p->vOrderSeqs   = Vec_IntAlloc( pCounts[GLS_SEQ] );
    return p;
}
void Gls_ManStop( Gls_Man_t * p )
{
    Vec_StrFree( p->vLines );
    Vec_StrFree( p->vTypes );
    Vec_IntFree( p->vIndexes );
    Vec_IntFree( p->vLut4s );
    Vec_IntFree( p->vLut4TTs );
    Vec_IntFree( p->vLut6s );
    Vec_WrdFree( p->vLut6TTs );
    Vec_IntFree( p->vBoxes );
    Vec_IntFree( p->vDelays );
    Vec_WecFree( p->vDelayIns );
    Vec_WecFree( p->vDelayOuts );
    // ordering
    Vec_IntFree( p->vOrderPis );
    Vec_IntFree( p->vOrderPos );
    Vec_IntFree( p->vOrderBoxes );
    Vec_IntFree( p->vOrderDelays );
    Vec_IntFree( p->vOrderLuts );
    Vec_IntFree( p->vOrderSeqs );
    ABC_FREE( p );
}

/**Function*************************************************************

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Vec_Str_t * Gls_ManCount( FILE * pFile, int pCounts[GLS_FINAL] )
{
    char * pLine, * pBuffer = ABC_ALLOC(char, MAX_LINE); int Type;
    Vec_Str_t * vLines = Vec_StrAlloc( 10000 );
    memset( pCounts, 0, sizeof(int)*GLS_FINAL );
    while ( fgets( pBuffer, MAX_LINE, pFile ) != NULL )
    {
        pLine = pBuffer;
        while ( *pLine )
            if ( *pLine++ == '=' )
                break;
        while ( *pLine == ' ' )
            pLine++;
        if ( *pLine == 'L' )
        {
            if ( pLine[3] == '4' )
                Type = GLS_LUT4;
            else if ( pLine[3] == '6' )
                Type = GLS_LUT6;
            else assert( 0 );
        }
        else if ( *pLine == 'P' )
        {
            if ( pLine[1] == 'I' )
                Type = GLS_PI;
            else if ( pLine[1] == 'O' )
                Type = GLS_PO;
            else assert( 0 );
        }
        else if ( *pLine == 'B' )
        {
            if ( pLine[1] == 'o' )
                Type = GLS_BOX;
            else if ( pLine[1] == 'a' )
                Type = GLS_BAR;
            else assert( 0 );
        }
        else if ( *pLine == 'S' )
        {
            if ( pLine[2] == 'l' )
                Type = GLS_SEL;
            else if ( pLine[2] == 'q' )
                Type = GLS_SEQ;
            else assert( 0 );
        }
        else if ( *pLine == 'D' )
            Type = GLS_DEL;
        else assert( 0 );
        Vec_StrPush( vLines, (char)Type );
        pCounts[Type]++;
    }
    ABC_FREE( pBuffer );
    return vLines;
}
int Gls_ManParseOne( char ** ppLine )
{
    int Entry;
    char * pLine = *ppLine;
    while ( *pLine == ' ' )   pLine++;
    if ( *pLine == '-' )
        Entry = GLS_NONE;
    else if ( *pLine == '0' )
        Entry = 0;
    else if ( *pLine == '1' )
        Entry = 1;
    else if ( *pLine == 'w' )
        Entry = atoi(++pLine);
    else assert( 0 );
    while ( *pLine == '-' || (*pLine >= '0' && *pLine <= '9') )   pLine++;
    while ( *pLine == ' ' )   pLine++;
    *ppLine = pLine;
    return Entry;
}
int Gls_ManParse( FILE * pFile, Gls_Man_t * p )
{
    char * pLine, * pBuffer = ABC_ALLOC(char, MAX_LINE); 
    int i, k, Type, iObj, Entry, iItem;  word Truth;
    for ( i = 0; fgets( pBuffer, MAX_LINE, pFile ) != NULL; i++ )
    {
        pLine = pBuffer;
        Type = Vec_StrEntry( p->vLines, i );
        iObj = Gls_ManParseOne( &pLine );
        Vec_StrWriteEntry( p->vTypes, iObj, (char)Type );
        if ( Type == GLS_PI )
        {
            Vec_IntPush( p->vOrderPis, iObj );
            Vec_IntWriteEntry( p->vIndexes, iObj, -1 );
            continue;
        }
        while ( *pLine )
            if ( *pLine++ == '(' )
                break;
        Entry = Gls_ManParseOne( &pLine );
        if ( Type == GLS_PO || Type == GLS_BAR || Type == GLS_SEQ || Type == GLS_SEL )
        {
            if ( Type == GLS_PO )
                Vec_IntPush( p->vOrderPos, iObj );
            else if ( Type == GLS_BAR )
                Vec_IntPush( p->vOrderLuts, iObj );
            else if ( Type == GLS_SEQ )
                Vec_IntPush( p->vOrderSeqs, iObj );
            else if ( Type == GLS_SEL )
            {
                if ( (int)Vec_StrEntry(p->vTypes, Entry) == GLS_DEL )
                {
                    Vec_Int_t * vOuts = Vec_WecEntry( p->vDelayOuts, Vec_IntEntry(p->vIndexes, Entry) );
                    Vec_IntPush( vOuts, iObj );
                }
                else if ( (int)Vec_StrEntry(p->vTypes, Entry) == GLS_BAR )
                    Vec_IntPush( p->vOrderLuts, iObj );
                else assert( 0 );
            }
            Vec_IntWriteEntry( p->vIndexes, iObj, Entry );
            continue;
        }
        if ( Type == GLS_LUT4 )
        {
            Vec_IntWriteEntry( p->vIndexes, iObj, Vec_IntSize(p->vLut4TTs) );
            Vec_IntPush( p->vLut4s, Entry );
            for ( k = 1; ; k++ )
            {
                if ( *pLine != ',' )     break;
                pLine++;
                Entry = Gls_ManParseOne( &pLine );
                Vec_IntPush( p->vLut4s, Entry );
            }
            assert( *pLine == ')' );
            assert( k == 4 );
            pLine++;
            while ( *pLine )
                if ( *pLine++ == '[' )
                    break;
            Abc_TtReadHex( &Truth, pLine );
            Vec_IntPush( p->vLut4TTs, (unsigned)Truth );
            Vec_IntPush( p->vOrderLuts, iObj );
        }
        else if ( Type == GLS_LUT6 )
        {
            Vec_IntWriteEntry( p->vIndexes, iObj, Vec_WrdSize(p->vLut6TTs) );
            Vec_IntPush( p->vLut6s, Entry );
            for ( k = 1; ; k++ )
            {
                if ( *pLine != ',' )     break;
                pLine++;
                Entry = Gls_ManParseOne( &pLine );
                Vec_IntPush( p->vLut6s, Entry );
            }
            assert( *pLine == ')' );
            assert( k == 4 );
            pLine++;
            while ( *pLine )
                if ( *pLine++ == '[' )
                    break;
            Abc_TtReadHex( &Truth, pLine );
            Vec_WrdPush( p->vLut6TTs, Truth );
            Vec_IntPush( p->vOrderLuts, iObj );
        }
        else if ( Type == GLS_BOX )
        {
            Vec_IntWriteEntry( p->vIndexes, iObj, Vec_IntSize(p->vBoxes)/5 );
            Vec_IntPush( p->vBoxes, Entry );
            for ( k = 1; ; k++ )
            {
                if ( *pLine != ',' )     break;
                pLine++;
                Entry = Gls_ManParseOne( &pLine );
                Vec_IntPush( p->vBoxes, Entry );
            }
            assert( *pLine == ')' );
            assert( k == 4 || k == 5 );
            if ( k == 4 )
                Vec_IntPush( p->vBoxes, GLS_NONE );
            Vec_IntPush( p->vOrderBoxes, iObj );
        }
        else if ( Type == GLS_DEL )
        {
            Vec_Int_t * vIns  = Vec_WecPushLevel( p->vDelayIns );
            Vec_Int_t * vOuts = Vec_WecPushLevel( p->vDelayOuts );
            Vec_IntWriteEntry( p->vIndexes, iObj, Vec_IntSize(p->vDelays) );
            Vec_IntPush( vIns, Entry );
            if ( *pLine != ')' )
            {
                for ( k = 1; ; k++ )
                {
                    if ( *pLine != ',' )     break;
                    pLine++;
                    Entry = Gls_ManParseOne( &pLine );
                    Vec_IntPush( vIns, Entry );
                }
            }
            assert( *pLine == ')' );
            pLine++;
            while ( *pLine )
                if ( *pLine++ == '[' )
                    break;
            iItem = atoi(pLine);
            Vec_IntPush( p->vDelays, iItem );
            Vec_IntPush( p->vOrderDelays, iObj );
            vOuts = vIns; // harmless use to prevent a compiler warning
        }
        else assert( 0 );
    }
    ABC_FREE( pBuffer );
    return 1;
}

/**Function*************************************************************

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Gia_Man_t * Gls_ManConstruct( Gls_Man_t * p, char * pFileName )
{
    extern int Kit_TruthToGia( Gia_Man_t * pMan, unsigned * pTruth, int nVars, Vec_Int_t * vMemory, Vec_Int_t * vLeaves, int fHash );
    Gia_Man_t * pGia = NULL;  
    Vec_Int_t * vMap, * vArray;
    Vec_Int_t * vCover = Vec_IntAlloc(0);
    Vec_Int_t * vLeaves = Vec_IntAlloc(6);
    int  k, iObj, iLit, Index;  char Type;
    // create new manager
    pGia = Gia_ManStart( Vec_StrSize(p->vTypes) );
    pGia->pName = Abc_UtilStrsav( pFileName );
    pGia->pSpec = Abc_UtilStrsav( pFileName );
    // create constants
    vMap = Vec_IntStartFull( Vec_StrSize(p->vTypes) );
    Vec_IntWriteEntry( vMap, 0, 0 );
    Vec_IntWriteEntry( vMap, 1, 1 );
    // create primary inputs
    Vec_IntForEachEntry( p->vOrderPis, iObj, k )
        Vec_IntWriteEntry( vMap, iObj, Gia_ManAppendCi(pGia) );
    // create box outputs
    Vec_IntForEachEntry( p->vOrderBoxes, iObj, k )
        Vec_IntWriteEntry( vMap, iObj, Gia_ManAppendCi(pGia) );
    // create delay outputs
    Vec_IntForEachEntry( p->vOrderDelays, iObj, Index )
    {
        assert( Index == Vec_IntEntry(p->vIndexes, iObj) );
        vArray = Vec_WecEntry(p->vDelayOuts, Index);
        if ( Vec_IntSize(vArray) == 0 )
            Vec_IntWriteEntry( vMap, iObj, Gia_ManAppendCi(pGia) );
        else
            Vec_IntForEachEntry( vArray, iObj, k )
                Vec_IntWriteEntry( vMap, iObj, Gia_ManAppendCi(pGia) );
    }
    // construct LUTs
    Vec_IntForEachEntry( p->vOrderLuts, iObj, Index )
    {
        Type = Vec_StrEntry( p->vTypes, iObj );
        if ( Type == GLS_LUT4 || Type == GLS_LUT6 )
        {
            int Limit = Type == GLS_LUT4 ? 4 : 6;
            int Index = Vec_IntEntry(p->vIndexes, iObj);
            int * pFanins = Type == GLS_LUT4 ? Vec_IntEntryP(p->vLut4s, 4*Index) : Vec_IntEntryP(p->vLut6s, 6*Index);
            word Truth = Type == GLS_LUT4 ? (word)Vec_IntEntry(p->vLut4TTs, Index) : Vec_WrdEntry(p->vLut6TTs, Index);
            Vec_IntClear( vLeaves );
            for ( k = 0; k < Limit; k++ )
                Vec_IntPush( vLeaves, pFanins[k] == GLS_NONE ? 0 : Vec_IntEntry(vMap, pFanins[k]) );
            iLit = Kit_TruthToGia( pGia, (unsigned *)&Truth, Vec_IntSize(vLeaves), vCover, vLeaves, 0 );
            Vec_IntWriteEntry( vMap, iObj, iLit );
        }
        else if ( Type == GLS_BAR || Type == GLS_SEL )
        {
            iLit = Vec_IntEntry( vMap, Vec_IntEntry(p->vIndexes, iObj) );
            Vec_IntWriteEntry( vMap, iObj, iLit );
        }
    }
    // delay inputs
    Vec_IntForEachEntry( p->vOrderDelays, iObj, Index )
    {
        vArray = Vec_WecEntry(p->vDelayIns, Index);
        assert( Vec_IntSize(vArray) > 0 );
        Vec_IntForEachEntry( vArray, iObj, k )
            Gia_ManAppendCo( pGia, Vec_IntEntry(vMap, iObj) );
    }
    // create primary outputs
    Vec_IntForEachEntry( p->vOrderPos, iObj, k )
        Gia_ManAppendCo( pGia, Vec_IntEntry(vMap, Vec_IntEntry(p->vIndexes, iObj)) );
    // create sequential nodes
    Vec_IntForEachEntry( p->vOrderSeqs, iObj, k )
        Gia_ManAppendCo( pGia, Vec_IntEntry(vMap, Vec_IntEntry(p->vIndexes, iObj)) );
    Vec_IntFree( vMap );
    Vec_IntFree( vCover );
    Vec_IntFree( vLeaves );
    // print delay boxes
//    for ( k = 0; k < Vec_IntSize(p->vDelays); k++ )
//        printf( "%d:%d  ", Vec_IntSize(Vec_WecEntry(p->vDelayIns, k)), Vec_IntSize(Vec_WecEntry(p->vDelayOuts, k)) );
//    printf( "\n" );
    return pGia;
}

/**Function*************************************************************

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Gia_Man_t * Gia_ManReadGig( char * pFileName )
{
    abctime clk = Abc_Clock();
    Gls_Man_t * p = NULL;
    Gia_Man_t * pGia = NULL;
    Vec_Str_t * vLines;
    int i, pCounts[GLS_FINAL];
    FILE * pFile = fopen( pFileName, "rb" );
    if ( pFile == NULL )
    {
        printf( "Cannot read file \"%s\".\n", pFileName );
        return NULL;
    }
    vLines = Gls_ManCount( pFile, pCounts );
    rewind( pFile );
    // statistics
    for ( i = 0; i < GLS_FINAL; i++ )
        if ( pCounts[i] )
            printf( "%s=%d  ", s_Strs[i], pCounts[i] );
    Abc_PrintTime( 1, "Time", Abc_Clock() - clk );
    // collect data and derive AIG
    p = Gls_ManAlloc( vLines, pCounts );
    if ( Gls_ManParse( pFile, p ) )
        pGia = Gls_ManConstruct( p, pFileName );
    Gls_ManStop( p );
    fclose( pFile );
    //printf( "\n" );
    return pGia;
}

////////////////////////////////////////////////////////////////////////
///                       END OF FILE                                ///
////////////////////////////////////////////////////////////////////////


ABC_NAMESPACE_IMPL_END