VIS

src/io/ioReadBlifMv.c

Go to the documentation of this file.
00001 
00036 #include "ioInt.h"
00037 
00038 static char rcsid[] UNUSED = "$Id: ioReadBlifMv.c,v 1.12 2002/09/10 04:35:24 fabio Exp $";
00039 
00040 /*---------------------------------------------------------------------------*/
00041 /* Constant declarations                                                     */
00042 /*---------------------------------------------------------------------------*/
00043 #ifndef NAWK
00044 #define NAWK "gawk"
00045 #endif
00046 
00047 
00048 /*---------------------------------------------------------------------------*/
00049 /* Type declarations                                                         */
00050 /*---------------------------------------------------------------------------*/
00051 
00052 
00053 /*---------------------------------------------------------------------------*/
00054 /* Stucture declarations                                                     */
00055 /*---------------------------------------------------------------------------*/
00056 
00057 
00058 /*---------------------------------------------------------------------------*/
00059 /* Variable declarations                                                     */
00060 /*---------------------------------------------------------------------------*/
00061 static jmp_buf env;
00062 extern FILE *IoYyin;
00063 extern int globalCurrentStackDepth;
00064 #ifdef IODEBUG
00065 extern int IoYydebug;
00066 #endif /*IODEBUG */
00067 
00068 /*---------------------------------------------------------------------------*/
00069 /* Macro declarations                                                        */
00070 /*---------------------------------------------------------------------------*/
00071 
00072 
00075 /*---------------------------------------------------------------------------*/
00076 /* Static function prototypes                                                */
00077 /*---------------------------------------------------------------------------*/
00078 
00079 static void _IoGlobalVariablesInitialize(void);
00080 static void _IoGlobalVariablesFree(void);
00081 static void _IoGlobalSubcktInfoFree(void);
00082 static void _IoSubcktArrayFree(array_t *array);
00083 static void _IoSubcktFree(IoSubckt_t *subckt);
00084 static void _IoGlobalResetInfoFree(void);
00085 static boolean _IoNodeTestCompatibilityAux(Hrc_Manager_t *hmgr, Hrc_Node_t *hnode1, Hrc_Node_t *hnode2, boolean mode);
00086 static void _IoManagerCanonicalize(Hrc_Manager_t *hmgr);
00087 
00091 /*---------------------------------------------------------------------------*/
00092 /* Definition of exported functions                                          */
00093 /*---------------------------------------------------------------------------*/
00113 Hrc_Manager_t *
00114 Io_BlifMvRead(
00115   FILE *fp, 
00116   Hrc_Manager_t *hmgr, 
00117   boolean isCanonical,
00118   boolean isIncremental,
00119   boolean isVerbose)
00120 {
00121   Hrc_Node_t *root;
00122   static Hrc_Manager_t *newHmgr;
00123 
00124   if (isIncremental == 0){
00125     newHmgr = Hrc_ManagerAlloc(); 
00126   }
00127   else { /* isIncremental == 1 */
00128     if (Hrc_ManagerReadCurrentNode(hmgr) == NIL(Hrc_Node_t)){
00129       (void)fprintf(vis_stderr,"No hierarchy has been created. Cannot do inremental read-in.\n");
00130       return NIL(Hrc_Manager_t);
00131     }
00132     newHmgr = hmgr;
00133   }
00134 
00135   if (setjmp(env)){
00136     if (isIncremental == 0){
00137       Hrc_ManagerFree(newHmgr);
00138     }
00139     else {
00140       int i;
00141       /* free all the new models defined in an incremental file */
00142       for (i=0; i < array_n(globalNewModelArray); i++){
00143         Hrc_ModelDelete(hmgr,Hrc_ModelReadName(array_fetch(Hrc_Model_t *,globalNewModelArray,i)));
00144       }
00145     }
00146     _IoGlobalVariablesFree();
00147     return NIL(Hrc_Manager_t);
00148   }
00149   else {
00150     _IoGlobalVariablesInitialize();
00151     if (isIncremental == 0){
00152       globalYaccHmgr = newHmgr;
00153     }
00154     else {
00155       globalYaccHmgr = hmgr;
00156     }
00157 #ifdef IODEBUG
00158     IoYydebug = 1;
00159 #endif /* IODEBUG */
00160     IoYyin = fp;
00161     IoYyrestart(IoYyin);
00162     if (IoYyparse() == 1){
00163       IoError();
00164     }
00165 
00166     /* globalNewModelArray contains all the models defined in the previous
00167     IoYyparse(). If isIncremental==0, then they are simply all the models.
00168     If isIncremental==1, then they are new models defined in the file. */
00169 
00170     if (IoNetworkTestConsistency(newHmgr,globalNewModelArray,globalParserSubcktInfo,globalParserResetInfo,isVerbose) == 0){
00171       IoError();
00172     }
00173 
00174     if (globalRootModel == NIL(Hrc_Model_t)){
00175       globalRootModel = globalFirstModel;
00176       globalRootInstanceName = util_strsav(Hrc_ModelReadName(globalRootModel));
00177     }
00178     else if (globalRootInstanceName == NIL(char)){
00179       globalRootInstanceName = util_strsav(Hrc_ModelReadName(globalRootModel));
00180     }
00181 
00182     if (isCanonical == 1){
00183       _IoManagerCanonicalize(newHmgr);
00184     }
00185 
00186     root = Hrc_ModelCreateHierarchy(newHmgr,globalRootModel,globalRootInstanceName);
00187     FREE(globalRootInstanceName);
00188 
00189     if (isIncremental == 0){
00190       Hrc_ManagerSetRootNode(newHmgr,root);
00191       Hrc_ManagerSetCurrentNode(newHmgr,root);
00192       _IoGlobalVariablesFree();
00193       return newHmgr;
00194     }    
00195     else { /* isIncremental == 1, note that newHmgr == hmgr  */
00196       if (_IoNodeTestCompatibility(hmgr,Hrc_ManagerReadCurrentNode(hmgr),root) == 0){
00197         int i;
00198         (void)fprintf(vis_stderr,"The blif-mv file is not compatible with the existing hierarchy.\n");
00199         Hrc_TreeReplace(NIL(Hrc_Node_t),root);
00200         /* free all the new models defined in an incremental file */
00201         for (i=0; i < array_n(globalNewModelArray); i++){
00202           Hrc_ModelDelete(hmgr,Hrc_ModelReadName(array_fetch(Hrc_Model_t *,globalNewModelArray,i)));
00203         }
00204         _IoGlobalVariablesFree();
00205         return NIL(Hrc_Manager_t);
00206       }
00207       Hrc_TreeReplace(Hrc_ManagerReadCurrentNode(hmgr),root);
00208       Hrc_ManagerSetCurrentNode(hmgr,root);
00209       _IoGlobalVariablesFree();
00210       return hmgr;
00211     }
00212   }
00213 }
00214 
00231 Hrc_Manager_t *
00232 Io_BlifRead(
00233   char *fileName,
00234   boolean isVerbose)
00235 {
00236   FILE *fp;
00237 #if HAVE_MKSTEMP && HAVE_CLOSE
00238   int  fd;
00239 #else
00240   char buffer[512];
00241 #endif
00242   char *realFileName, *blifMvFileName, *visDirectoryName;
00243   char command[512];
00244   Hrc_Manager_t *hmgr;
00245   
00246   fp = Cmd_FileOpen(fileName, "r", &realFileName, /* silent */ 1);
00247 
00248   if (fp == NIL(FILE)){
00249     FREE(realFileName);
00250     (void)fprintf(vis_stderr,"File %s is not found.\n", fileName);
00251     return NIL(Hrc_Manager_t);
00252   }
00253   if (fp != stdin){
00254     (void)fclose(fp);
00255   }
00256 
00257 #if HAVE_MKSTEMP && HAVE_CLOSE
00258   blifMvFileName = util_strsav("/tmp/vis.XXXXXX");
00259   fd = mkstemp(blifMvFileName);
00260   if (fd == -1){
00261 #else
00262   blifMvFileName = util_strsav(tmpnam(buffer));
00263   if (blifMvFileName == NIL(char)){
00264 #endif
00265     FREE(realFileName);
00266     (void)fprintf(vis_stderr,"Could not create temporary file. ");
00267     (void)fprintf(vis_stderr,"Clean up /tmp an try again.\n");
00268     return NIL(Hrc_Manager_t);
00269   }
00270 #if HAVE_MKSTEMP && HAVE_CLOSE
00271   close(fd);
00272 #endif
00273   /* Invoking an awk script */
00274   visDirectoryName = Vm_VisObtainLibrary();
00275   (void)sprintf(command,"sed 's/^\\./@/g' %s | sed 's/\\./$/g' | sed 's/^@/\\./g' | sed 's/{/</g' | sed 's/}/>/g'| sed 's/(/<</g' | sed 's/)/>>/g'| %s -f %s/ioBlifToMv.nawk > %s", realFileName, NAWK, visDirectoryName, blifMvFileName);
00276   /* the following is missing two new sed processings 
00277   (void)sprintf(command,"sed 's/^\\./@/g' %s | sed 's/\\./$/g' | sed 's/^@/\\./g' | sed 's/{/</g' | sed 's/}/>/g'| %s -f %s/ioBlifToMv.nawk > %s", realFileName, NAWK, visDirectoryName, blifMvFileName);
00278   */
00279   (void)system(command);
00280   FREE(visDirectoryName);
00281   FREE(realFileName);
00282 
00283   error_init();
00284 
00285   fp = Cmd_FileOpen(blifMvFileName, "r", NIL(char *), 1);
00286   assert(fp != NIL(FILE));
00287   hmgr = Io_BlifMvRead(fp,NIL(Hrc_Manager_t),0,0,isVerbose);
00288   fclose(fp);
00289 #if HAVE_UNLINK
00290   unlink(blifMvFileName);
00291 #endif
00292   FREE(blifMvFileName);
00293   return hmgr;
00294 }
00295 
00296 /*---------------------------------------------------------------------------*/
00297 /* Definition of internal functions                                          */
00298 /*---------------------------------------------------------------------------*/
00299 
00311 void
00312 IoError(void)
00313 {
00314   longjmp(env,1);
00315 }
00316 
00317 /*---------------------------------------------------------------------------*/
00318 /* Definition of static functions                                            */
00319 /*---------------------------------------------------------------------------*/
00320 
00321 
00333 static void
00334 _IoGlobalVariablesInitialize(void)
00335 {
00336   globalLineNumber = 1;
00337   globalModel = NIL(Hrc_Model_t);
00338   globalHnode = NIL(Hrc_Node_t);
00339   globalFirstModel = NIL(Hrc_Model_t); /* set once */
00340   globalRootModel = NIL(Hrc_Model_t); /* set once */
00341   globalRootInstanceName = NIL(char); /* set once */
00342   globalMvNameArray = NIL(array_t);
00343   globalSymValueArray = NIL(array_t);
00344   globalTableInputArray = NIL(array_t);
00345   globalTableOutputArray = NIL(array_t);
00346   globalTableDefaultArray = NIL(array_t);
00347   globalTableSymCubeArray = NIL(array_t);
00348   globalFormalNameArray = NIL(array_t);
00349   globalActualNameArray = NIL(array_t);
00350 
00351   globalSubcktArray = NIL(array_t);
00352   globalResetArray = NIL(array_t);
00353 
00354   globalNewModelArray = array_alloc(Hrc_Model_t *,0);
00355 
00356   globalCurrentStackDepth = 0;
00357 
00358   /* a hash table from a model name to an array of resets/subcircuits in the model */
00359   globalParserResetInfo = st_init_table(st_ptrcmp,st_ptrhash);
00360   globalParserSubcktInfo = st_init_table(st_ptrcmp,st_ptrhash);
00361 }
00362 
00374 static void
00375 _IoGlobalVariablesFree(void)
00376 {
00377    array_free(globalNewModelArray);
00378    _IoGlobalResetInfoFree(); 
00379    _IoGlobalSubcktInfoFree();
00380 }
00381 
00382 
00394 static void
00395 _IoGlobalSubcktInfoFree(void)
00396 {
00397   st_generator *gen;
00398   char *key, *val;
00399 
00400   st_foreach_item(globalParserSubcktInfo,gen,&key,&val){
00401     if ((array_t *)val != NIL(array_t)){
00402       _IoSubcktArrayFree((array_t *)val);
00403     }
00404   }
00405   st_free_table(globalParserSubcktInfo);
00406 }
00407 
00408 
00420 static void
00421 _IoSubcktArrayFree(array_t *array)
00422 {
00423   int i;
00424 
00425   for (i=0; i < array_n(array); i++){
00426     _IoSubcktFree(array_fetch(IoSubckt_t *,array,i));
00427   }
00428   array_free(array);
00429 }
00430 
00442 static void
00443 _IoSubcktFree(IoSubckt_t *subckt)
00444 {
00445   FREE(subckt->modelName);
00446   FREE(subckt->instanceName);
00447   IoStringArrayFree(subckt->formalNameArray);
00448   IoStringArrayFree(subckt->actualNameArray);
00449   FREE(subckt);
00450 }
00451 
00452 
00464 static void
00465 _IoGlobalResetInfoFree(void)
00466 {
00467   st_generator *gen;
00468   char *key, *val;
00469   int i;
00470   Tbl_Table_t *resetTable;
00471 
00472   st_foreach_item(globalParserResetInfo,gen,&key,&val){
00473     if ((array_t *)val != NIL(array_t)){
00474       for (i=0; i < array_n((array_t *)val); i++){
00475         resetTable = array_fetch(Tbl_Table_t *,(array_t *)val,i);
00476         if (resetTable != NIL(Tbl_Table_t)){
00477           Tbl_TableFree(resetTable);
00478         }
00479       }
00480       array_free((array_t *)val);
00481     }
00482   }
00483   st_free_table(globalParserResetInfo);
00484 }
00485 
00486 
00500 boolean
00501 _IoNodeTestCompatibility(
00502   Hrc_Manager_t *hmgr,
00503   Hrc_Node_t *hnode1,
00504   Hrc_Node_t *hnode2)
00505 {
00506   return (_IoNodeTestCompatibilityAux(hmgr,hnode1,hnode2,0)
00507           && _IoNodeTestCompatibilityAux(hmgr,hnode1,hnode2,1));
00508 }
00509 
00524 static boolean
00525 _IoNodeTestCompatibilityAux(
00526   Hrc_Manager_t *hmgr,
00527   Hrc_Node_t *hnode1,
00528   Hrc_Node_t *hnode2,
00529   boolean mode)
00530 {
00531   int i, n;
00532   array_t *formalVars1, *formalVars2;
00533   Var_Variable_t *var1, *var2;
00534 
00535   if (mode == 0){
00536     formalVars1 = Hrc_NodeReadFormalInputs(hnode1);
00537     formalVars2 = Hrc_NodeReadFormalInputs(hnode2);
00538   }
00539   else {
00540     formalVars1 = Hrc_NodeReadFormalOutputs(hnode1);
00541     formalVars2 = Hrc_NodeReadFormalOutputs(hnode2);
00542   }
00543   if ((n = array_n(formalVars1)) != array_n(formalVars2)){
00544     error_append("Two hnodes have different number of formal ");
00545     if (mode == 0){
00546       error_append("inputs.\n");
00547     }
00548     else {
00549       error_append("outputs.\n");
00550     }
00551     return 0;
00552   } 
00553   for (i=0; i < n; i++){
00554     var1 = array_fetch(Var_Variable_t *,formalVars1,i);
00555     var2 = array_fetch(Var_Variable_t *,formalVars2,i);
00556     if (strcmp(Var_VariableReadName(var1),Var_VariableReadName(var2)) != 0){
00557       error_append("Two hnodes have different ports, ");
00558       error_append(Var_VariableReadName(var1));
00559       error_append(" and ");
00560       error_append(Var_VariableReadName(var2));
00561       error_append("\n");
00562       return 0; 
00563     }
00564     if (Var_VariablesTestHaveSameDomain(var1,var2) == 0){
00565       error_append("Two hnodes have ports defined over different domains, ");
00566       error_append(Var_VariableReadName(var1));
00567       error_append(" and ");
00568       error_append(Var_VariableReadName(var2));
00569       error_append("\n");
00570       return 0; 
00571     }
00572   }
00573   return 1;
00574 }
00575 
00576 
00587 static void
00588 _IoManagerCanonicalize(
00589   Hrc_Manager_t *hmgr)
00590 {
00591   int i;
00592   Hrc_Model_t *model;
00593   Hrc_Node_t *node;
00594   Tbl_Table_t *table;
00595   st_generator *gen, *gen2;
00596   char *modelName, *latchName;
00597   Hrc_Latch_t *latch;
00598 
00599   Hrc_ManagerForEachModel(hmgr,gen,modelName,model){
00600     node = Hrc_ModelReadMasterNode(model);
00601     Hrc_NodeForEachNameTable(node,i,table){
00602       Tbl_TableCanonicalize(table);
00603     }
00604     Hrc_NodeForEachLatch(node,gen2,latchName,latch){
00605       Tbl_TableCanonicalize(Hrc_LatchReadResetTable(latch));
00606     }
00607   }
00608 }