abc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
mio.c
Go to the documentation of this file.
1 /**CFile****************************************************************
2 
3  pFileName [mio.c]
4 
5  PackageName [MVSIS 1.3: Multi-valued logic synthesis system.]
6 
7  Synopsis [File reading/writing for technology mapping.]
8 
9  Author [MVSIS Group]
10 
11  Affiliation [UC Berkeley]
12 
13  Date [Ver. 1.0. Started - August 18, 2003.]
14 
15  Revision [$Id: mio.c,v 1.4 2004/08/05 18:34:51 satrajit Exp $]
16 
17 ***********************************************************************/
18 
19 #define _BSD_SOURCE
20 
21 #ifndef WIN32
22 #include <unistd.h>
23 #endif
24 
25 #include "base/main/main.h"
26 #include "mio.h"
27 #include "map/mapper/mapper.h"
28 #include "map/amap/amap.h"
29 
31 
32 
33 ////////////////////////////////////////////////////////////////////////
34 /// DECLARATIONS ///
35 ////////////////////////////////////////////////////////////////////////
36 
37 static int Mio_CommandReadLiberty( Abc_Frame_t * pAbc, int argc, char **argv );
38 static int Mio_CommandReadGenlib( Abc_Frame_t * pAbc, int argc, char **argv );
39 static int Mio_CommandWriteGenlib( Abc_Frame_t * pAbc, int argc, char **argv );
40 static int Mio_CommandPrintGenlib( Abc_Frame_t * pAbc, int argc, char **argv );
41 
42 /*
43 // internal version of genlib library
44 static char * pMcncGenlib[25] = {
45  "GATE inv1 1 O=!a; PIN * INV 1 999 0.9 0.0 0.9 0.0\n",
46  "GATE inv2 2 O=!a; PIN * INV 2 999 1.0 0.0 1.0 0.0\n",
47  "GATE inv3 3 O=!a; PIN * INV 3 999 1.1 0.0 1.1 0.0\n",
48  "GATE inv4 4 O=!a; PIN * INV 4 999 1.2 0.0 1.2 0.0\n",
49  "GATE nand2 2 O=!(a*b); PIN * INV 1 999 1.0 0.0 1.0 0.0\n",
50  "GATE nand3 3 O=!(a*b*c); PIN * INV 1 999 1.1 0.0 1.1 0.0\n",
51  "GATE nand4 4 O=!(a*b*c*d); PIN * INV 1 999 1.4 0.0 1.4 0.0\n",
52  "GATE nor2 2 O=!(a+b); PIN * INV 1 999 1.4 0.0 1.4 0.0\n",
53  "GATE nor3 3 O=!(a+b+c); PIN * INV 1 999 2.4 0.0 2.4 0.0\n",
54  "GATE nor4 4 O=!(a+b+c+d); PIN * INV 1 999 3.8 0.0 3.8 0.0\n",
55  "GATE xora 5 O=a*!b+!a*b; PIN * UNKNOWN 2 999 1.9 0.0 1.9 0.0\n",
56  "GATE xorb 5 O=!(a*b+!a*!b); PIN * UNKNOWN 2 999 1.9 0.0 1.9 0.0\n",
57  "GATE xnora 5 O=a*b+!a*!b; PIN * UNKNOWN 2 999 2.1 0.0 2.1 0.0\n",
58  "GATE xnorb 5 O=!(!a*b+a*!b); PIN * UNKNOWN 2 999 2.1 0.0 2.1 0.0\n",
59  "GATE aoi21 3 O=!(a*b+c); PIN * INV 1 999 1.6 0.0 1.6 0.0\n",
60  "GATE aoi22 4 O=!(a*b+c*d); PIN * INV 1 999 2.0 0.0 2.0 0.0\n",
61  "GATE oai21 3 O=!((a+b)*c); PIN * INV 1 999 1.6 0.0 1.6 0.0\n",
62  "GATE oai22 4 O=!((a+b)*(c+d)); PIN * INV 1 999 2.0 0.0 2.0 0.0\n",
63  "GATE buf 1 O=a; PIN * NONINV 1 999 1.0 0.0 1.0 0.0\n",
64  "GATE zero 0 O=CONST0;\n",
65  "GATE one 0 O=CONST1;\n"
66 };
67 */
68 ////////////////////////////////////////////////////////////////////////
69 /// FUNCTION DEFINITIONS ///
70 ////////////////////////////////////////////////////////////////////////
71 
72 /**Function*************************************************************
73 
74  Synopsis []
75 
76  Description []
77 
78  SideEffects []
79 
80  SeeAlso []
81 
82 ***********************************************************************/
83 void Mio_Init( Abc_Frame_t * pAbc )
84 {
85 // Cmd_CommandAdd( pAbc, "SC mapping", "read_liberty", Mio_CommandReadLiberty, 0 );
86 
87  Cmd_CommandAdd( pAbc, "SC mapping", "read_genlib", Mio_CommandReadGenlib, 0 );
88  Cmd_CommandAdd( pAbc, "SC mapping", "write_genlib", Mio_CommandWriteGenlib, 0 );
89  Cmd_CommandAdd( pAbc, "SC mapping", "print_genlib", Mio_CommandPrintGenlib, 0 );
90 
91  Cmd_CommandAdd( pAbc, "SC mapping", "read_library", Mio_CommandReadGenlib, 0 );
92  Cmd_CommandAdd( pAbc, "SC mapping", "write_library", Mio_CommandWriteGenlib, 0 );
93  Cmd_CommandAdd( pAbc, "SC mapping", "print_library", Mio_CommandPrintGenlib, 0 );
94 }
95 
96 /**Function*************************************************************
97 
98  Synopsis []
99 
100  Description []
101 
102  SideEffects []
103 
104  SeeAlso []
105 
106 ***********************************************************************/
107 void Mio_End( Abc_Frame_t * pAbc )
108 {
111 }
112 
113 /**Function*************************************************************
114 
115  Synopsis []
116 
117  Description []
118 
119  SideEffects []
120 
121  SeeAlso []
122 
123 ***********************************************************************/
125 {
126  // free the current superlib because it depends on the old Mio library
127  if ( Abc_FrameReadLibSuper() )
128  {
130  Abc_FrameSetLibSuper( NULL );
131  }
132 
133  // replace the current library
135  Abc_FrameSetLibGen( pLib );
136 
137  // replace the current library
139  Abc_FrameSetLibGen2( NULL );
140 }
141 int Mio_UpdateGenlib2( Vec_Str_t * vStr, Vec_Str_t * vStr2, char * pFileName, int fVerbose )
142 {
143  Mio_Library_t * pLib;
144  // set the new network
145  pLib = Mio_LibraryRead( pFileName, Vec_StrArray(vStr), NULL, fVerbose );
146  if ( pLib == NULL )
147  return 0;
148 
149  // free the current superlib because it depends on the old Mio library
150  if ( Abc_FrameReadLibSuper() )
151  {
153  Abc_FrameSetLibSuper( NULL );
154  }
155 
156  // replace the current library
158  Abc_FrameSetLibGen( pLib );
159 
160  // set the new network
161  pLib = (Mio_Library_t *)Amap_LibReadAndPrepare( pFileName, Vec_StrArray(vStr2), 0, 0 );
162  if ( pLib == NULL )
163  return 0;
164 
165  // replace the current library
167  Abc_FrameSetLibGen2( pLib );
168  return 1;
169 }
170 
171 /**Function*************************************************************
172 
173  Synopsis []
174 
175  Description []
176 
177  SideEffects []
178 
179  SeeAlso []
180 
181 ***********************************************************************/
182 int Mio_CommandReadLiberty( Abc_Frame_t * pAbc, int argc, char **argv )
183 {
184  int fUseFileInterface = 0;
185  char Command[1000];
186  FILE * pFile;
187  FILE * pOut, * pErr;
188  Abc_Ntk_t * pNet;
189  char * pFileName;
190  int fVerbose;
191  int c;
192 
193  pNet = Abc_FrameReadNtk(pAbc);
194  pOut = Abc_FrameReadOut(pAbc);
195  pErr = Abc_FrameReadErr(pAbc);
196 
197  // set the defaults
198  fVerbose = 0;
200  while ( (c = Extra_UtilGetopt(argc, argv, "vh")) != EOF )
201  {
202  switch (c)
203  {
204  case 'v':
205  fVerbose ^= 1;
206  break;
207  case 'h':
208  goto usage;
209  break;
210  default:
211  goto usage;
212  }
213  }
214 
215  if ( argc != globalUtilOptind + 1 )
216  goto usage;
217 
218  // get the input file name
219  pFileName = argv[globalUtilOptind];
220  if ( (pFile = Io_FileOpen( pFileName, "open_path", "r", 0 )) == NULL )
221  {
222  fprintf( pErr, "Cannot open input file \"%s\". ", pFileName );
223  if ( (pFileName = Extra_FileGetSimilarName( pFileName, ".genlib", ".lib", ".scl", ".g", NULL )) )
224  fprintf( pErr, "Did you mean \"%s\"?", pFileName );
225  fprintf( pErr, "\n" );
226  return 1;
227  }
228  fclose( pFile );
229 
230  if ( fUseFileInterface )
231  {
232  if ( !Amap_LibertyParse( pFileName, fVerbose ) )
233  return 0;
234  assert( strlen(pFileName) < 900 );
235  sprintf( Command, "read_genlib %s", Extra_FileNameGenericAppend(pFileName, ".genlib") );
236  Cmd_CommandExecute( pAbc, Command );
237  }
238  else
239  {
240  Vec_Str_t * vStr, * vStr2;
241  int RetValue;
242  vStr = Amap_LibertyParseStr( pFileName, fVerbose );
243  if ( vStr == NULL )
244  return 0;
245  vStr2 = Vec_StrDup(vStr);
246  RetValue = Mio_UpdateGenlib2( vStr, vStr2, pFileName, fVerbose );
247  Vec_StrFree( vStr );
248  Vec_StrFree( vStr2 );
249  if ( !RetValue )
250  printf( "Reading library has filed.\n" );
251  }
252  return 0;
253 
254 usage:
255  fprintf( pErr, "usage: read_liberty [-vh]\n");
256  fprintf( pErr, "\t read standard cell library in Liberty format\n" );
257  fprintf( pErr, "\t (if the library contains more than one gate\n" );
258  fprintf( pErr, "\t with the same Boolean function, only the gate\n" );
259  fprintf( pErr, "\t with the smallest area will be used)\n" );
260  fprintf( pErr, "\t-v : toggle verbose printout [default = %s]\n", fVerbose? "yes": "no" );
261  fprintf( pErr, "\t-h : enable verbose output\n");
262  return 1;
263 }
264 
265 /**Function*************************************************************
266 
267  Synopsis []
268 
269  Description []
270 
271  SideEffects []
272 
273  SeeAlso []
274 
275 ***********************************************************************/
276 int Mio_CommandReadGenlib( Abc_Frame_t * pAbc, int argc, char **argv )
277 {
278  FILE * pFile;
279  FILE * pOut, * pErr;
280  Mio_Library_t * pLib;
281  Amap_Lib_t * pLib2;
282  Abc_Ntk_t * pNet;
283  char * pFileName;
284  char * pExcludeFile = NULL;
285  int fVerbose;
286  double WireDelay;
287  int c;
288 
289  pNet = Abc_FrameReadNtk(pAbc);
290  pOut = Abc_FrameReadOut(pAbc);
291  pErr = Abc_FrameReadErr(pAbc);
292 
293  // set the defaults
294  WireDelay = 0.0;
295  fVerbose = 1;
297  while ( (c = Extra_UtilGetopt(argc, argv, "WEvh")) != EOF )
298  {
299  switch (c)
300  {
301  case 'W':
302  if ( globalUtilOptind >= argc )
303  {
304  Abc_Print( -1, "Command line switch \"-W\" should be followed by a floating point number.\n" );
305  goto usage;
306  }
307  WireDelay = (float)atof(argv[globalUtilOptind]);
308  globalUtilOptind++;
309  if ( WireDelay <= 0.0 )
310  goto usage;
311  break;
312  case 'E':
313  if ( globalUtilOptind >= argc )
314  {
315  Abc_Print( -1, "Command line switch \"-E\" should be followed by a file name.\n" );
316  goto usage;
317  }
318  pExcludeFile = argv[globalUtilOptind];
319  globalUtilOptind++;
320  break;
321  case 'v':
322  fVerbose ^= 1;
323  break;
324  case 'h':
325  goto usage;
326  break;
327  default:
328  goto usage;
329  }
330  }
331  if ( argc != globalUtilOptind + 1 )
332  {
333  goto usage;
334  }
335 
336  // get the input file name
337  pFileName = argv[globalUtilOptind];
338  if ( (pFile = Io_FileOpen( pFileName, "open_path", "r", 0 )) == NULL )
339  {
340  fprintf( pErr, "Cannot open input file \"%s\". ", pFileName );
341  if ( (pFileName = Extra_FileGetSimilarName( pFileName, ".genlib", ".lib", ".scl", ".g", NULL )) )
342  fprintf( pErr, "Did you mean \"%s\"?", pFileName );
343  fprintf( pErr, "\n" );
344  return 1;
345  }
346  fclose( pFile );
347 
348  // set the new network
349  pLib = Mio_LibraryRead( pFileName, NULL, pExcludeFile, fVerbose );
350  if ( pLib == NULL )
351  {
352  fprintf( pErr, "Reading genlib library has failed.\n" );
353  return 1;
354  }
355  if ( fVerbose )
356  printf( "Entered genlib library with %d gates from file \"%s\".\n", Mio_LibraryReadGateNum(pLib), pFileName );
357 
358  // add the fixed number (wire delay) to all delays in the library
359  if ( WireDelay != 0.0 )
360  Mio_LibraryShiftDelay( pLib, WireDelay );
361 
362  // prepare libraries
363  Mio_UpdateGenlib( pLib );
364 
365  // replace the current library
366  pLib2 = Amap_LibReadAndPrepare( pFileName, NULL, 0, 0 );
367  if ( pLib2 == NULL )
368  {
369  fprintf( pErr, "Reading second genlib library has failed.\n" );
370  return 1;
371  }
372  Abc_FrameSetLibGen2( pLib2 );
373  return 0;
374 
375 usage:
376  fprintf( pErr, "usage: read_genlib [-W float] [-E filename] [-vh]\n");
377  fprintf( pErr, "\t read the library from a genlib file\n" );
378  fprintf( pErr, "\t (if the library contains more than one gate\n" );
379  fprintf( pErr, "\t with the same Boolean function, only the gate\n" );
380  fprintf( pErr, "\t with the smallest area will be used)\n" );
381  fprintf( pErr, "\t-W float : wire delay (added to pin-to-pin gate delays) [default = %g]\n", WireDelay );
382  fprintf( pErr, "\t-E file : the file name with gates to be excluded [default = none]\n" );
383  fprintf( pErr, "\t-v : toggle verbose printout [default = %s]\n", fVerbose? "yes": "no" );
384  fprintf( pErr, "\t-h : enable verbose output\n");
385  return 1;
386 }
387 
388 /**Function*************************************************************
389 
390  Synopsis [Command procedure to read LUT libraries.]
391 
392  Description []
393 
394  SideEffects []
395 
396  SeeAlso []
397 
398 ***********************************************************************/
399 int Mio_CommandWriteGenlib( Abc_Frame_t * pAbc, int argc, char **argv )
400 {
401  FILE * pOut, * pErr, * pFile;
402  Abc_Ntk_t * pNet;
403  char * pFileName;
404  int fVerbose;
405  int c;
406 
407  pNet = Abc_FrameReadNtk(pAbc);
408  pOut = Abc_FrameReadOut(pAbc);
409  pErr = Abc_FrameReadErr(pAbc);
410 
411  // set the defaults
412  fVerbose = 1;
414  while ( (c = Extra_UtilGetopt(argc, argv, "vh")) != EOF )
415  {
416  switch (c)
417  {
418  case 'v':
419  fVerbose ^= 1;
420  break;
421  case 'h':
422  goto usage;
423  break;
424  default:
425  goto usage;
426  }
427  }
428  if ( Abc_FrameReadLibGen() == NULL )
429  {
430  printf( "Library is not available.\n" );
431  return 1;
432  }
433  if ( argc != globalUtilOptind + 1 )
434  {
435  printf( "The file name is not given.\n" );
436  return 1;
437  }
438 
439  pFileName = argv[globalUtilOptind];
440  pFile = fopen( pFileName, "w" );
441  if ( pFile == NULL )
442  {
443  printf( "Error! Cannot open file \"%s\" for writing the library.\n", pFileName );
444  return 1;
445  }
447  fclose( pFile );
448  printf( "The current genlib library is written into file \"%s\".\n", pFileName );
449  return 0;
450 
451 usage:
452  fprintf( pErr, "\nusage: write_genlib [-vh] <file>\n");
453  fprintf( pErr, "\t writes the current genlib library into a file\n" );
454  fprintf( pErr, "\t-v : toggles enabling of verbose output [default = %s]\n", fVerbose? "yes" : "no" );
455  fprintf( pErr, "\t-h : print the command usage\n");
456  fprintf( pErr, "\t<file> : optional file name to write the library\n");
457  return 1;
458 }
459 
460 /**Function*************************************************************
461 
462  Synopsis [Command procedure to read LUT libraries.]
463 
464  Description []
465 
466  SideEffects []
467 
468  SeeAlso []
469 
470 ***********************************************************************/
471 int Mio_CommandPrintGenlib( Abc_Frame_t * pAbc, int argc, char **argv )
472 {
473  FILE * pOut, * pErr;
474  Abc_Ntk_t * pNet;
475  int fVerbose;
476  int c;
477 
478  pNet = Abc_FrameReadNtk(pAbc);
479  pOut = Abc_FrameReadOut(pAbc);
480  pErr = Abc_FrameReadErr(pAbc);
481 
482  // set the defaults
483  fVerbose = 1;
485  while ( (c = Extra_UtilGetopt(argc, argv, "vh")) != EOF )
486  {
487  switch (c)
488  {
489  case 'v':
490  fVerbose ^= 1;
491  break;
492  case 'h':
493  goto usage;
494  break;
495  default:
496  goto usage;
497  }
498  }
499  if ( Abc_FrameReadLibGen() == NULL )
500  {
501  printf( "Library is not available.\n" );
502  return 1;
503  }
505  return 0;
506 
507 usage:
508  fprintf( pErr, "\nusage: print_genlib [-vh]\n");
509  fprintf( pErr, "\t print the current genlib library\n" );
510  fprintf( pErr, "\t-v : toggles enabling of verbose output [default = %s]\n", fVerbose? "yes" : "no" );
511  fprintf( pErr, "\t-h : print the command usage\n");
512  return 1;
513 }
514 
515 ////////////////////////////////////////////////////////////////////////
516 /// END OF FILE ///
517 ////////////////////////////////////////////////////////////////////////
518 
519 
521 
FILE * Io_FileOpen(const char *FileName, const char *PathVar, const char *Mode, int fVerbose)
Definition: ioUtil.c:819
ABC_DLL void Abc_FrameSetLibSuper(void *pLib)
Definition: mainFrame.c:84
void Map_SuperLibFree(Map_SuperLib_t *p)
Definition: mapperLib.c:167
ABC_DLL void * Abc_FrameReadLibGen()
Definition: mainFrame.c:56
Mio_Library_t * Mio_LibraryRead(char *FileName, char *pBuffer, char *ExcludeFile, int fVerbose)
Definition: mioRead.c:54
ABC_DLL void * Abc_FrameReadLibGen2()
Definition: mainFrame.c:57
void Mio_LibraryDelete(Mio_Library_t *pLib)
DECLARATIONS ///.
Definition: mioUtils.c:48
void Mio_End(Abc_Frame_t *pAbc)
Definition: mio.c:107
int Cmd_CommandExecute(void *pAbc, char *pCommandLine)
static char * Vec_StrArray(Vec_Str_t *p)
Definition: vecStr.h:272
char * Extra_FileGetSimilarName(char *pFileNameWrong, char *pS1, char *pS2, char *pS3, char *pS4, char *pS5)
Definition: extraUtilFile.c:71
void Mio_LibraryShiftDelay(Mio_Library_t *pLib, double Shift)
Definition: mioUtils.c:802
void Cmd_CommandAdd(Abc_Frame_t *pAbc, const char *sGroup, const char *sName, Cmd_CommandFuncType pFunc, int fChanges)
Definition: cmdApi.c:63
int Amap_LibertyParse(char *pFileName, int fVerbose)
Definition: amapLiberty.c:1012
ABC_DLL void Abc_FrameSetLibGen2(void *pLib)
Definition: mainFrame.c:83
Amap_Lib_t * Amap_LibReadAndPrepare(char *pFileName, char *pBuffer, int fVerbose, int fVeryVerbose)
Definition: amapLib.c:329
Vec_Str_t * Amap_LibertyParseStr(char *pFileName, int fVerbose)
Definition: amapLiberty.c:1059
typedefABC_NAMESPACE_HEADER_START struct Abc_Frame_t_ Abc_Frame_t
INCLUDES ///.
ABC_DLL void Extra_UtilGetoptReset()
Definition: extraUtilUtil.c:80
void Mio_Init(Abc_Frame_t *pAbc)
FUNCTION DEFINITIONS ///.
Definition: mio.c:83
static void Vec_StrFree(Vec_Str_t *p)
Definition: bblif.c:616
void Amap_LibFree(Amap_Lib_t *p)
Definition: amapLib.c:67
#define ABC_NAMESPACE_IMPL_END
Definition: abc_global.h:108
int globalUtilOptind
Definition: extraUtilUtil.c:45
ABC_DLL void * Abc_FrameReadLibSuper()
Definition: mainFrame.c:58
int Mio_UpdateGenlib2(Vec_Str_t *vStr, Vec_Str_t *vStr2, char *pFileName, int fVerbose)
Definition: mio.c:141
STRUCTURE DEFINITIONS ///.
Definition: mioInt.h:61
char * sprintf()
static void Abc_Print(int level, const char *format,...)
Definition: abc_global.h:313
static int Mio_CommandPrintGenlib(Abc_Frame_t *pAbc, int argc, char **argv)
Definition: mio.c:471
void Mio_WriteLibrary(FILE *pFile, Mio_Library_t *pLib, int fPrintSops)
Definition: mioUtils.c:251
double atof()
#define ABC_NAMESPACE_IMPL_START
Definition: abc_global.h:107
ABC_DLL Abc_Ntk_t * Abc_FrameReadNtk(Abc_Frame_t *p)
Definition: mainFrame.c:282
typedefABC_NAMESPACE_HEADER_START struct Amap_Lib_t_ Amap_Lib_t
INCLUDES ///.
Definition: amap.h:42
static int Mio_CommandReadGenlib(Abc_Frame_t *pAbc, int argc, char **argv)
Definition: mio.c:276
int Extra_UtilGetopt(int argc, char *argv[], const char *optstring)
Definition: extraUtilUtil.c:98
static ABC_NAMESPACE_IMPL_START int Mio_CommandReadLiberty(Abc_Frame_t *pAbc, int argc, char **argv)
DECLARATIONS ///.
Definition: mio.c:182
void Mio_UpdateGenlib(Mio_Library_t *pLib)
FUNCTION DEFINITIONS ///.
Definition: mio.c:124
ABC_DLL FILE * Abc_FrameReadErr(Abc_Frame_t *p)
Definition: mainFrame.c:330
#define assert(ex)
Definition: util_old.h:213
char * Extra_FileNameGenericAppend(char *pBase, char *pSuffix)
int strlen()
static int Mio_CommandWriteGenlib(Abc_Frame_t *pAbc, int argc, char **argv)
Definition: mio.c:399
ABC_DLL void Abc_FrameSetLibGen(void *pLib)
Definition: mainFrame.c:82
ABC_DLL FILE * Abc_FrameReadOut(Abc_Frame_t *p)
Definition: mainFrame.c:314
static Vec_Str_t * Vec_StrDup(Vec_Str_t *pVec)
Definition: vecStr.h:158
int Mio_LibraryReadGateNum(Mio_Library_t *pLib)
Definition: mioApi.c:44