abc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
bblif.h
Go to the documentation of this file.
1 /**CFile****************************************************************
2 
3  FileName [bblif.h]
4 
5  SystemName [ABC: Logic synthesis and verification system.]
6 
7  PackageName [Binary BLIF representation for logic networks.]
8 
9  Synopsis [External declarations.]
10 
11  Author [Alan Mishchenko]
12 
13  Affiliation [UC Berkeley]
14 
15  Date [Ver. 1.0. Started - February 28, 2009.]
16 
17  Revision [$Id: bblif.h,v 1.00 2009/02/28 00:00:00 alanmi Exp $]
18 
19 ***********************************************************************/
20 
21 #ifndef ABC__aig__bbl__bblif_h
22 #define ABC__aig__bbl__bblif_h
23 
24 
25 /*
26  This file (taken together with "bblif.c") implements a stand-alone
27  interface between ABC and an application that uses ABC.
28 
29  The interface is designed to pass a combinational logic network
30  from the calling application to ABC using a binary BLIF format (BBLIF)
31  and return the network after synthesis/mapping/verification in ABC
32  back to the caller.
33 
34  The interface can do the following:
35  (1) accept a combinational logic network via a set of APIs
36  (2) write the logic network into a binary BLIF file readable by ABC
37  (3) read a binary BLIF file with a mapped network produced by ABC
38  (4) return the mapped network to the caller through a set of APIs
39 
40  It should be noted that the BBLIF interface can be used to pass
41  the network from the calling application into ABC without writing it
42  into a file. In this case, ABC should be compiled as a library and
43  linked to the calling application. The BBLIF manager can be given
44  directly to the procedure Bbl_ManToAbc() to convert it into an AIG.
45  Similarly, the resulting mapped network can be converted into
46  BBLIF manager and passed back after the call to Bbl_ManFromAbc().
47 
48  Here these steps are described in more detail:
49 
50  (1) The BBLIF manager is allocated by calling Bbl_ManStart() and
51  deallocated by calling Bbl_ManStop().
52 
53  The combinational network is composed of three types of objects:
54  (a) combinational inputs (CIs), (b) combinational outputs (COs),
55  (c) internal logic nodes represented using Sum-of-Products (SOPs)
56  similar to the way logic nodes are represented in SIS. Sequential
57  elements (flops) are currently not supported. A CI has no fanins.
58  A CO has exactly one fanin and no fanouts. Internal nodes can
59  have any number of fanins and fanouts. Only an internal node can
60  have a logic function.
61 
62  Before constructing the BBLIF manager, each object should be
63  assigned a unique non-negative (0-based) integer ID. The sequence
64  of ID numbers may have gaps in it (for example, 0, 1, 2, 5, 6, etc)
65  but care should be taken that the ID numbers do not grow too large
66  because internally they are used to index the objects. So if
67  the largest given ID has value N, an array of 4*N bytes will be
68  allocated internally by the BBLIF manager. Obviously if N = 1M,
69  the array will use 4Mb, but if N = 100M, it will use 0.4Gb.
70 
71  This object ID (called also "the original ID of the object") is
72  given to Bbl_ManCreateObject(), which construct the BBLIF objects
73  and to the procedure Bbl_ManAddFanin(), which creates fanin/fanout
74  relations between two objects. The exact number of fanins of an
75  object should be declared when calling Bbl_ManCreateObject().
76  Later on, each node should be assigned as many fanins using
77  Bbl_ManAddFanin(). The order/number of fanins corresponds to the
78  order/number of variables in the SOP of the logic function of the
79  node. The declared and actual number of fanins should be the same.
80  otherwise the interface will not function correctly. This is checked
81  by the procedure Bbl_ManCheck(), which should be called when
82  constructing all objects and their fanins is finished.
83 
84  The SOP representation of the logic function should be given to
85  every internal node. It is given as a C-string, showing the SOP
86  as it would appear in a BLIF or PLA file. Each cube is composed
87  of characters '0', '1', and '-', and ended by a seqence of three
88  characters: space ' ', followed by '0' or '1' (depending on whether
89  on- or off-set is used), followed by the new line character '\n'.
90  For example, a two-input OR has the following SOP representation:
91  "1- 1\n-1 1\n", or equivalently, "00 0\n". The SOP for a constant
92  function with no fanins is represented as " 0\n" (constant 0) and
93  " 1\n" (constant 1). SOP for a constant node with some fanins
94  may also be represented. For example, constant 0 node with three
95  fanins will have SOP representation as follows: "--- 0\n".
96 
97  The objects can be added to the BBLIF manager in any order, but
98  by the time the fanin/fanout connections are created, corresponding
99  objects should be already created.
100 
101  The number of objects is limited by 2^31. The number of fanins
102  of one object is restricted to 2^28. The SOP representation can
103  have arbitrary many products (cubes), as long as memory is enough
104  to represent them in the C-string form, as described above.
105 
106  (2) To write the manager into a file, call procedure Bbl_ManDumpBinaryBlif().
107  It is recommended to use files with extension ".bblif" because it
108  will allow ABC to call the approapriate reader in command "read".
109 
110  (3) To read the network from file, call procedure Bbl_ManReadBinaryBlif().
111 
112  (4) It is assumed that ABC will return the network after mapping.
113  This network will arrive in a BBLIF file, from which the BBLIF
114  manager is created by the call to Bbl_ManReadBinaryBlif(). The
115  following APIs are useful to extract the mapped network from the manager:
116 
117  Iterator Bbl_ManForEachObj() iterates through the pointers to the
118  BBLIF objects, which are guaranteed to be in a topological order.
119 
120  For each object, the following APIs can be used:
121  Bbl_ObjIsInput() returns 1 if the object is a CI
122  Bbl_ObjIsOutput() returns 1 if the object is a CO
123  Bbl_ObjIsLut() returns 1 if the object is a logic node (lookup table)
124  Bbl_ObjFaninNumber() returns the number of fanins of the node
125  Bbl_ObjSop() returns the SOP representation of the node, as described above.
126 
127  A special attention should be given to the representation of object IDs
128  after mapping. Recall that when the outgoing BBLIF network is constructed,
129  the IDs of objects are assigned by the calling application and given to
130  the BBLIF manager when procedure Bbl_ManCreateObject() is called.
131  We refer to these object IDs as "original IDs of the objects".
132 
133  When the network has been given to ABC, mapped, and returned to the
134  calling application in the incoming BBLIF file, only CIs and COs are
135  guaranteed to preserve their "original IDs". Other objects may be created
136  during synthesis/mapping. The original IDs of these objects are set to -1.
137 
138  The following two APIs are used to return the IDs of objects after mapping:
139  Bbl_ObjId() returns the new ID (useful to construct network after mapping)
140  Bbl_ObjIdOriginal() returns the original ID (or -1 if this is a new object).
141 
142  !!!***!!!
143  Note: The original ID currently cannot be returned by Bbl_ObjIdOriginal().
144  It is recommended to use the work-around described below.
145  !!!***!!!
146 
147  The original ID is useful to map CIs/COs after mapping into CIs/COs before
148  mapping. However, the order of CIs/COs after mapping in the incoming network
149  is the same as the order of their creation by the calling application
150  in the outgoing network. This allows for a workaround that does not have
151  the need for the original IDs. We can simply iterate through the objects
152  after mapping, and create CIs and COs in the order of their appearance,
153  and this order is guaranteed to be the same as the order of their
154  construction by the calling application.
155 
156  It is also worth noting that currently the internal node names are not
157  preserved by ABC during synthesis. This may change in the future. and then
158  some of the internal nodes will preserve their IDs, which may allow the
159  calling application to reconstruct the names of some of the nodes after
160  synthesis/mapping in ABC using their original IDs whenever available.
161 
162  Finally, iterator Bbl_ObjForEachFanin() can be used to iterate through
163  the fanins of each mapped object. For CIs, there will be no fanins.
164  For COs, there will be exactly one fanin. For the internal nodes (LUTs)
165  the number of fanins is the number of inputs of these nodes.
166 
167  A demo of using this interface is included at the bottom of file "bblif.c" in
168  procedure Bbl_ManSimpleDemo(). Additional examples can be found in the files
169  "abc\src\base\io\ioReadBblif.c" and "abc\src\base\io\ioWriteBblif.c". These
170  files illustrate how an ABC network is created from the BBLIF data manager
171  and how the data manager is created from the ABC network.
172 
173  Note that only the files "bblif.h" and "bblif.c" are needed for interfacing
174  the user's application with ABC, while other files should not be compiled
175  as part of the application code.
176 
177  Finally, a warning regarding endianness. The interface may not work
178  if the BBLIF file is produced on a machine whose engianness is different
179  from the machine, which is reading this file.
180 */
181 
182 ////////////////////////////////////////////////////////////////////////
183 /// INCLUDES ///
184 ////////////////////////////////////////////////////////////////////////
185 
186 ////////////////////////////////////////////////////////////////////////
187 /// PARAMETERS ///
188 ////////////////////////////////////////////////////////////////////////
189 
190 
191 
193 
194 
195 #ifdef _WIN32
196 #define inline __inline
197 #endif
198 
199 ////////////////////////////////////////////////////////////////////////
200 /// BASIC TYPES ///
201 ////////////////////////////////////////////////////////////////////////
202 
203 // object types
204 typedef enum {
205  BBL_OBJ_NONE, // 0: non-existent object
206  BBL_OBJ_CI, // 1: primary input
207  BBL_OBJ_CO, // 2: primary output
208  BBL_OBJ_NODE, // 3: buffer node
209  BBL_OBJ_VOID // 4: unused object
210 } Bbl_Type_t;
211 
212 // data manager
213 typedef struct Bbl_Man_t_ Bbl_Man_t;
214 
215 // data object
216 typedef struct Bbl_Obj_t_ Bbl_Obj_t;
217 
218 ////////////////////////////////////////////////////////////////////////
219 /// MACRO DEFINITIONS ///
220 ////////////////////////////////////////////////////////////////////////
221 
222 ////////////////////////////////////////////////////////////////////////
223 /// FUNCTION DECLARATIONS ///
224 ////////////////////////////////////////////////////////////////////////
225 
226 // (1) creating the data manager in the application code
227 extern Bbl_Man_t * Bbl_ManStart( char * pName );
228 extern void Bbl_ManCreateObject( Bbl_Man_t * p, Bbl_Type_t Type, int ObjId, int nFanins, char * pSop );
229 extern void Bbl_ManAddFanin( Bbl_Man_t * p, int ObjId, int FaninId );
230 extern int Bbl_ManCheck( Bbl_Man_t * p );
231 extern void Bbl_ManPrintStats( Bbl_Man_t * p );
232 extern void Bbl_ManStop( Bbl_Man_t * p );
233 
234 // (2) writing the data manager into file
235 extern void Bbl_ManDumpBinaryBlif( Bbl_Man_t * p, char * pFileName );
236 
237 // (3) reading the data manager from file
238 extern Bbl_Man_t * Bbl_ManReadBinaryBlif( char * pFileName );
239 
240 // (4) returning the mapped network after reading the data manaager from file
241 extern char * Bbl_ManName( Bbl_Man_t * p );
242 extern int Bbl_ObjIsInput( Bbl_Obj_t * p );
243 extern int Bbl_ObjIsOutput( Bbl_Obj_t * p );
244 extern int Bbl_ObjIsLut( Bbl_Obj_t * p );
245 extern int Bbl_ObjId( Bbl_Obj_t * p );
246 extern int Bbl_ObjIdOriginal( Bbl_Man_t * pMan, Bbl_Obj_t * p );
247 extern int Bbl_ObjFaninNumber( Bbl_Obj_t * p );
248 extern char * Bbl_ObjSop( Bbl_Man_t * pMan, Bbl_Obj_t * p );
249 
250 // for the use in iterators only
251 extern Bbl_Obj_t * Bbl_ManObjFirst( Bbl_Man_t * p );
252 extern Bbl_Obj_t * Bbl_ManObjNext( Bbl_Man_t * p, Bbl_Obj_t * pObj );
253 extern Bbl_Obj_t * Bbl_ObjFaninFirst( Bbl_Obj_t * p );
254 extern Bbl_Obj_t * Bbl_ObjFaninNext( Bbl_Obj_t * p, Bbl_Obj_t * pPrev );
255 
256 // iterator through the objects
257 #define Bbl_ManForEachObj( p, pObj ) \
258  for ( pObj = Bbl_ManObjFirst(p); pObj; pObj = Bbl_ManObjNext(p, pObj) )
259 // iterator through the fanins fo the an object
260 #define Bbl_ObjForEachFanin( pObj, pFanin ) \
261  for ( pFanin = Bbl_ObjFaninFirst(pObj); pFanin; pFanin = Bbl_ObjFaninNext(pObj, pFanin) )
262 
263 // these additional procedures are provided to transform truth tables into SOPs, and vice versa
264 extern char * Bbl_ManTruthToSop( unsigned * pTruth, int nVars );
265 extern unsigned * Bbl_ManSopToTruth( char * pSop, int nVars );
266 
267 // write text BLIF file for debugging
268 extern void Bbl_ManDumpBlif( Bbl_Man_t * p, char * pFileName );
269 
270 // a simple demo procedure
271 extern void Bbl_ManSimpleDemo();
272 
273 
274 
275 
277 
278 
279 
280 #endif
281 
282 ////////////////////////////////////////////////////////////////////////
283 /// END OF FILE ///
284 ////////////////////////////////////////////////////////////////////////
285 
int Bbl_ObjIdOriginal(Bbl_Man_t *pMan, Bbl_Obj_t *p)
Definition: bblif.c:1097
static Llb_Mgr_t * p
Definition: llb3Image.c:950
int Bbl_ObjIsInput(Bbl_Obj_t *p)
Definition: bblif.c:1093
Bbl_Obj_t * Bbl_ObjFaninNext(Bbl_Obj_t *p, Bbl_Obj_t *pPrev)
Definition: bblif.c:1197
int Bbl_ObjIsOutput(Bbl_Obj_t *p)
Definition: bblif.c:1094
void Bbl_ManCreateObject(Bbl_Man_t *p, Bbl_Type_t Type, int ObjId, int nFanins, char *pSop)
Definition: bblif.c:988
char * Bbl_ObjSop(Bbl_Man_t *pMan, Bbl_Obj_t *p)
Definition: bblif.c:1099
void Bbl_ManSimpleDemo()
Definition: bblif.c:1446
unsigned * Bbl_ManSopToTruth(char *pSop, int nVars)
Definition: bblif.c:1362
unsigned nFanins
Definition: bblif.c:63
void Bbl_ManDumpBlif(Bbl_Man_t *p, char *pFileName)
Definition: bblif.c:1218
void Bbl_ManStop(Bbl_Man_t *p)
Definition: bblif.c:775
Bbl_Obj_t * Bbl_ManObjNext(Bbl_Man_t *p, Bbl_Obj_t *pObj)
Definition: bblif.c:1163
Bbl_Man_t * Bbl_ManReadBinaryBlif(char *pFileName)
Definition: bblif.c:712
Bbl_Obj_t * Bbl_ManObjFirst(Bbl_Man_t *p)
Definition: bblif.c:1147
#define ABC_NAMESPACE_HEADER_START
NAMESPACES ///.
Definition: abc_global.h:105
int Bbl_ManCheck(Bbl_Man_t *p)
Definition: bblif.c:1062
#define ABC_NAMESPACE_HEADER_END
Definition: abc_global.h:106
Bbl_Obj_t * Bbl_ObjFaninFirst(Bbl_Obj_t *p)
Definition: bblif.c:1181
char * Bbl_ManTruthToSop(unsigned *pTruth, int nVars)
Definition: bblif.c:1273
Bbl_Man_t * Bbl_ManStart(char *pName)
MACRO DEFINITIONS ///.
Definition: bblif.c:806
char * Bbl_ManName(Bbl_Man_t *p)
Definition: bblif.c:1115
int Bbl_ObjId(Bbl_Obj_t *p)
Definition: bblif.c:1096
int Bbl_ObjIsLut(Bbl_Obj_t *p)
Definition: bblif.c:1095
Bbl_Type_t
INCLUDES ///.
Definition: bblif.h:204
void Bbl_ManDumpBinaryBlif(Bbl_Man_t *p, char *pFileName)
Definition: bblif.c:691
int Bbl_ObjFaninNumber(Bbl_Obj_t *p)
Definition: bblif.c:1098
void Bbl_ManAddFanin(Bbl_Man_t *p, int ObjId, int FaninId)
Definition: bblif.c:1023
void Bbl_ManPrintStats(Bbl_Man_t *p)
Definition: bblif.c:749