VIS

src/ntmaig/ntmaigCmd.c

Go to the documentation of this file.
00001 
00017 #include "ntmaigInt.h"
00018 
00019 static char rcsid[] UNUSED = "$Id: ntmaigCmd.c,v 1.26 2009/01/18 01:54:51 fabio Exp $";
00020 
00023 /*---------------------------------------------------------------------------*/
00024 /* Static function prototypes                                                */
00025 /*---------------------------------------------------------------------------*/
00026 
00027 static int CommandBuildPartitionMAigs(Hrc_Manager_t ** hmgr, int argc, char ** argv);
00028 static int nodenameCompare(const void * node1, const void * node2);
00029 
00033 /*---------------------------------------------------------------------------*/
00034 /* Definition of exported functions                                          */
00035 /*---------------------------------------------------------------------------*/
00036 extern void bAig_BuildAigBFSManner( Ntk_Network_t *network, mAig_Manager_t *manager, st_table *leaves, int sweep);
00037 extern void bAig_BddSweepMain(Ntk_Network_t *network, array_t *nodeArray);
00038 
00048 void
00049 ntmaig_Init(void)
00050 {
00051   Cmd_CommandAdd("build_partition_maigs", CommandBuildPartitionMAigs, /* doesn't changes_network */ 0);
00052 }
00053 
00063 void
00064 ntmaig_End(void)
00065 {
00066 }
00067 
00068 /*---------------------------------------------------------------------------*/
00069 /* Definition of internal functions                                          */
00070 /*---------------------------------------------------------------------------*/
00071 
00072 
00073 /*---------------------------------------------------------------------------*/
00074 /* Definition of static functions                                            */
00075 /*---------------------------------------------------------------------------*/
00076 
00077 
00089 static int
00090 nodenameCompare(
00091   const void * node1,
00092   const void * node2)
00093 {
00094 Ntk_Node_t *v1, *v2;
00095 char *name1, *name2;
00096 
00097   v1 = *(Ntk_Node_t **)(node1);
00098   v2 = *(Ntk_Node_t **)(node2);
00099   name1 = Ntk_NodeReadName(v1);
00100   name2 = Ntk_NodeReadName(v2);
00101   
00102   return (strcmp(name1, name2));
00103 } 
00104 
00147 static int
00148 CommandBuildPartitionMAigs(
00149   Hrc_Manager_t ** hmgr,
00150   int  argc,
00151   char ** argv)
00152 {
00153   Ntk_Node_t    *node, *latch;
00154   lsGen          gen;
00155   array_t       *result; /* array of MvfAig_Function_t* */
00156   array_t       *roots;
00157   st_table      *leaves;
00158   array_t *inputs;
00159   Ntk_Network_t *network = Ntk_HrcManagerReadCurrentNetwork(*hmgr);
00160   mAig_Manager_t *manager;
00161   MvfAig_Function_t  *mvfAig;
00162   st_table       *nodeToMvfAigTable;  /* mapes each node with its mvfAig */
00163   int c, sweepFlag, DFSFlag;
00164   int i;
00165 
00166   sweepFlag = 1;
00167   DFSFlag = 0;
00168   util_getopt_reset();
00169   while ((c = util_getopt(argc,argv,"sdnh")) != EOF){
00170     switch(c){
00171       case 's':
00172         sweepFlag = 1;
00173         break;
00174       case 'd':
00175         DFSFlag = 1;
00176         break;
00177       case 'n':
00178         sweepFlag = 0;
00179         break;
00180       case 'h':
00181         goto usage;
00182       default:
00183         goto usage;
00184     }
00185   }
00186   if (network == NIL(Ntk_Network_t)) {
00187     return 1;
00188   }
00189 
00190   manager = Ntk_NetworkReadMAigManager(network);
00191 
00192   if (manager == NIL(mAig_Manager_t)) {
00193       manager = mAig_initAig();
00194       Ntk_NetworkSetMAigManager(network, manager);
00195       /*
00196       mAig_mAigSetNetwork(manager, network);
00197       */
00198   }
00199 
00200   /* Check if there is already a MvfAigs Table attached to the network */
00201   nodeToMvfAigTable = (st_table *) Ntk_NetworkReadApplInfo(network, 
00202                                                   MVFAIG_NETWORK_APPL_KEY);
00203 
00204   if (nodeToMvfAigTable != NIL(st_table)){
00205     /* 
00206     st_foreach_item(nodeToMvfAigTable, stGen, (char **) &node, (char **) &MvfAig) {
00207        printf("\nNode name = %s :",Ntk_NodeReadName(node));
00208        for (j=0; j< array_n(MvfAig); j++){
00209         printf(" %d ",array_fetch(bAigEdge_t, (array_t *) MvfAig, j));
00210        }
00211     }
00212     */
00213     (void) fprintf(vis_stderr, "maig partition already built; reinvoke\n");
00214     (void) fprintf(vis_stderr, "flatten_hierarchy to remove the current maig partition\n");
00215      return 1;
00216   }
00217 
00218   roots = array_alloc(Ntk_Node_t *, 0);
00219   Ntk_NetworkForEachCombOutput(network, gen, node) {
00220     array_insert_last(Ntk_Node_t *, roots, node);
00221   }
00222   Ntk_NetworkForEachNode(network, gen, node) {
00223     if(Ntk_NodeTestIsShadow(node))      continue;
00224     if(Ntk_NodeTestIsCombInput(node))   continue;
00225     if(Ntk_NodeTestIsCombOutput(node))continue;
00226 
00227     if(Ntk_NodeReadNumFanouts(node) == 0 && Ntk_NodeReadNumFanins(node)) {
00231       array_insert_last(Ntk_Node_t *, roots, node);
00232     }
00233   }
00234 
00235   leaves = st_init_table(st_ptrcmp, st_ptrhash);
00236 
00237   inputs = array_alloc(Ntk_Node_t *, 16);
00238   Ntk_NetworkForEachCombInput(network, gen, node) {
00239     array_insert_last(Ntk_Node_t *, inputs, node);
00240   }
00241   array_sort(inputs, nodenameCompare);
00242   for(i=0; i<array_n(inputs); i++) {
00243     node = array_fetch(Ntk_Node_t *, inputs, i);
00244     st_insert(leaves,  node, (char *) ntmaig_UNUSED);
00245     Ntk_NodeSetMAigId(node, 
00246                        mAig_CreateVar(manager,
00247                                       Ntk_NodeReadName(node),
00248                                       Var_VariableReadNumValues(Ntk_NodeReadVariable(node))));
00249   }
00250 
00251   if(DFSFlag == 0)
00252     bAig_BuildAigBFSManner(network, manager, leaves, sweepFlag);
00253 
00254   result = ntmaig_NetworkBuildMvfAigs(network, roots, leaves);
00255 
00256   nodeToMvfAigTable = (st_table *) Ntk_NetworkReadApplInfo(network, 
00257                                                   MVFAIG_NETWORK_APPL_KEY);
00258   Ntk_NetworkForEachLatch(network, gen, latch) {
00259     node  = Ntk_LatchReadDataInput(latch);
00260     st_lookup(nodeToMvfAigTable, node,  &mvfAig);
00261     Ntk_NodeSetMAigId(node, 
00262             mAig_CreateVarFromAig(manager, Ntk_NodeReadName(node), mvfAig));
00263     node  = Ntk_LatchReadInitialInput(latch);
00264     st_lookup(nodeToMvfAigTable, node,  &mvfAig);
00265     Ntk_NodeSetMAigId(node, 
00266             mAig_CreateVarFromAig(manager, Ntk_NodeReadName(node), mvfAig));
00267   }
00268 
00269   roots->num = 0;
00270   Ntk_NetworkForEachLatch(network, gen, latch) {
00271     if(!st_lookup(nodeToMvfAigTable, latch,  &mvfAig))
00272       array_insert_last(Ntk_Node_t *, roots, latch);
00273   }
00274   result = ntmaig_NetworkBuildMvfAigs(network, roots, leaves);
00275 
00276   array_free(roots);
00277   array_free(inputs);
00278   st_free_table(leaves);
00279 
00280   /*
00281    * Free the array of MvfAigs.
00282    */
00283   MvfAig_FunctionArrayFree(result);
00284 
00285   if(sweepFlag) 
00286     bAig_BddSweepMain(network, 0);
00287   
00288   return 0;
00289   
00290 usage:
00291   (void) fprintf(vis_stderr, "usage: build_partition_maig [-h]\n");
00292   (void) fprintf(vis_stderr, "   -h print the command usage\n");
00293   (void) fprintf(vis_stderr, "   -n disable bdd sweeping\n");
00294   (void) fprintf(vis_stderr, "   -d build AIG DFS manner\n");
00295   return 1;             /* error exit */
00296 }