/[ascend]/trunk/ascend/compiler/link.c
ViewVC logotype

Diff of /trunk/ascend/compiler/link.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 2568 by jpye, Fri Feb 3 23:25:40 2012 UTC revision 2569 by jpye, Tue Feb 7 21:01:01 2012 UTC
# Line 53  Line 53 
53    
54    
55  /**< DS: beginning of LINK functions *******/  /**< DS: beginning of LINK functions *******/
56  /* implemented functions related to the LINK statements, probably they shouldn't be here*/  /* implemented functions related to the LINK statements, probably they shouldn't be here*/
57    
58  /**  /**
59      Find instances: Make sure at least one thing is found for each name item      Find instances: Make sure at least one thing is found for each name item
# Line 111  static struct gl_list_t *FindInsts( Line 111  static struct gl_list_t *FindInsts(
111      @return 0 if the tuple (key, instances) is the same as the link entry,      @return 0 if the tuple (key, instances) is the same as the link entry,
112      otherwise return 1.      otherwise return 1.
113   */   */
114  static int CmpLinkEntry(symchar *key, struct VariableList *vlist,struct link_entry_t *linkEntry){    static int CmpLinkEntry(symchar *key, struct VariableList *vlist,struct link_entry_t *linkEntry){
115      /* DS: Note: the key_cache and the u.vl were used instead of      /* DS: Note: the key_cache and the u.vl were used instead of
116      LINKStatKey(linkEntry->u.statptr) and LINKStatVlist(linkEntry->u.statptr),      LINKStatKey(linkEntry->u.statptr) and LINKStatVlist(linkEntry->u.statptr),
117      because for some reason the pointer to the statement becomes NULL after the      because for some reason the pointer to the statement becomes NULL after the
118      statement is executed */      statement is executed */
119    
# Line 123  static int CmpLinkEntry(symchar *key, st Line 123  static int CmpLinkEntry(symchar *key, st
123          /* if the keys are different, the LINKs are different */          /* if the keys are different, the LINKs are different */
124          return 1;          return 1;
125      }      }
126        
127      linkEntry_vlist = linkEntry->u.vl;      linkEntry_vlist = linkEntry->u.vl;
128      if(VariableListLength(vlist) != VariableListLength(linkEntry_vlist)) {      if(VariableListLength(vlist) != VariableListLength(linkEntry_vlist)) {
129          /* if the number of variables linked are different, the LINKs are different */          /* if the number of variables linked are different, the LINKs are different */
# Line 144  void CollectLinkTypes(struct Instance *m Line 144  void CollectLinkTypes(struct Instance *m
144      struct link_entry_t *link_entry;      struct link_entry_t *link_entry;
145      symchar *key, *key_result;      symchar *key, *key_result;
146      int c1, c2, len_table, len_result, existent;      int c1, c2, len_table, len_result, existent;
147            
148      modelType = InstanceTypeDesc(model);      modelType = InstanceTypeDesc(model);
149      if(modelType->t == model_type) {      if(modelType->t == model_type) {
150    
# Line 165  void CollectLinkTypes(struct Instance *m Line 165  void CollectLinkTypes(struct Instance *m
165              for(c2=1;c2<=len_result;c2++){              for(c2=1;c2<=len_result;c2++){
166                  key_result = (symchar *)gl_fetch(result,c2);                  key_result = (symchar *)gl_fetch(result,c2);
167                  if(CmpSymchar(key,key_result) == 0){                  if(CmpSymchar(key,key_result) == 0){
168                      existent = 1;                        existent = 1;
169                  }                  }
170              }              }
171              if(!existent){              if(!existent){
# Line 190  void CollectLinkTypes(struct Instance *m Line 190  void CollectLinkTypes(struct Instance *m
190              for(c2=1;c2<=len_result;c2++){              for(c2=1;c2<=len_result;c2++){
191                  key_result = (symchar *)gl_fetch(result,c2);                  key_result = (symchar *)gl_fetch(result,c2);
192                  if(CmpSymchar(key,key_result) == 0){                  if(CmpSymchar(key,key_result) == 0){
193                      existent = 1;                        existent = 1;
194                  }                  }
195              }              }
196              if(!existent){              if(!existent){
197                      gl_append_ptr(result,(VOIDPTR)key);                      gl_append_ptr(result,(VOIDPTR)key);
198              }              }
199          }          }
200      }        }
201      /*DS:If the instance or the child instance is not a model we do nothing      /*DS:If the instance or the child instance is not a model we do nothing
202      since a "link_table" is only present in the typedescription of a model (modarg) */      since a "link_table" is only present in the typedescription of a model (modarg) */
203  }  }
204    
205    
206  /** find all the keys in link table(s), optionally recursive. */  /** find all the keys in link table(s), optionally recursive. */
207  extern struct gl_list_t *getLinkTypes (struct Instance *model, int recursive){    extern struct gl_list_t *getLinkTypes (struct Instance *model, int recursive){
208      struct gl_list_t *result = gl_create(AVG_LINKS);      struct gl_list_t *result = gl_create(AVG_LINKS);
209        
210      if (recursive) {      if (recursive) {
211          VisitInstanceTreeTwo(model, (VisitTwoProc)CollectLinkTypes, 0,0, result);          VisitInstanceTreeTwo(model, (VisitTwoProc)CollectLinkTypes, 0,0, result);
212      }else{      }else{
# Line 236  void CollectLinks(struct Instance *model Line 236  void CollectLinks(struct Instance *model
236              for(c2=1;c2<=len_result;c2++){              for(c2=1;c2<=len_result;c2++){
237                  link_entry_result = (struct link_entry_t *)gl_fetch(result,c2);                  link_entry_result = (struct link_entry_t *)gl_fetch(result,c2);
238                  if(CmpLinkEntry(link_entry->key_cache,link_entry->u.vl,link_entry_result) == 0){                  if(CmpLinkEntry(link_entry->key_cache,link_entry->u.vl,link_entry_result) == 0){
239                      existent = 1;                        existent = 1;
240                  }                  }
241              }              }
242              if(!existent){              if(!existent){
243                  gl_append_ptr(result,(VOIDPTR)link_entry);                  gl_append_ptr(result,(VOIDPTR)link_entry);
244              }              }
245          }          }
246            
247          /* probe instance given (if appropriate kind) to get link info needed          /* probe instance given (if appropriate kind) to get link info needed
248          from the ModelInstance link_table */          from the ModelInstance link_table */
249          len_table = gl_length(MOD_INST(model)->link_table);          len_table = gl_length(MOD_INST(model)->link_table);
# Line 252  void CollectLinks(struct Instance *model Line 252  void CollectLinks(struct Instance *model
252              existent = 0;              existent = 0;
253              // verify if the LINK is unique in the result list              // verify if the LINK is unique in the result list
254              len_result = gl_length(result);              len_result = gl_length(result);
255                
256              for(c2=1;c2<=len_result;c2++){              for(c2=1;c2<=len_result;c2++){
257                  link_entry_result = (struct link_entry_t *)gl_fetch(result,c2);                  link_entry_result = (struct link_entry_t *)gl_fetch(result,c2);
258                  if(CmpLinkEntry(link_entry->key_cache,link_entry->u.vl,link_entry_result) == 0){                  if(CmpLinkEntry(link_entry->key_cache,link_entry->u.vl,link_entry_result) == 0){
259                      existent = 1;                        existent = 1;
260                  }                  }
261              }              }
262              if(!existent){              if(!existent){
263                  gl_append_ptr(result,(VOIDPTR)link_entry);                  gl_append_ptr(result,(VOIDPTR)link_entry);
264              }              }
265          }          }
266      }        }
267      /*DS:If the instance or the child instance is not a model we do nothing since      /*DS:If the instance or the child instance is not a model we do nothing since
268      a "link_table" is only present in the typedescription of a model (modarg) */      a "link_table" is only present in the typedescription of a model (modarg) */
269  }  }
270    
# Line 272  void CollectLinks(struct Instance *model Line 272  void CollectLinks(struct Instance *model
272  extern struct gl_list_t *getLinks(struct Instance *model  extern struct gl_list_t *getLinks(struct Instance *model
273      , symchar *target_key, int recursive      , symchar *target_key, int recursive
274  ){  ){
275      struct gl_list_t *result = gl_create(AVG_LINKS); /**< DS: hardcoded for now but will eventually be a constant */      struct gl_list_t *result = gl_create(AVG_LINKS); /**< DS: hardcoded for now but will eventually be a constant */
276      int c1, len_result;      int c1, len_result;
277      struct link_entry_t *link_entry;      struct link_entry_t *link_entry;
278    
# Line 281  extern struct gl_list_t *getLinks(struct Line 281  extern struct gl_list_t *getLinks(struct
281      }else{      }else{
282          CollectLinks(model, result);          CollectLinks(model, result);
283      }      }
284        
285      len_result = gl_length(result);      len_result = gl_length(result);
286        
287      for(c1=1;c1<=len_result;c1++) {      for(c1=1;c1<=len_result;c1++) {
288          link_entry = (struct link_entry_t *)gl_fetch(result,c1);          link_entry = (struct link_entry_t *)gl_fetch(result,c1);
289          if(CmpSymchar(link_entry->key_cache,target_key) !=0 ){          if(CmpSymchar(link_entry->key_cache,target_key) !=0 ){
# Line 320  extern struct gl_list_t *getLinksReferen Line 320  extern struct gl_list_t *getLinksReferen
320          }else{          }else{
321              link_instances = link_entry->instances_cache;              link_instances = link_entry->instances_cache;
322          }          }
323            
324          len_inst = gl_length(link_instances);          len_inst = gl_length(link_instances);
325          containsInst = 0;          containsInst = 0;
326          if(CmpSymchar(link_entry->key_cache,key) == 0){          if(CmpSymchar(link_entry->key_cache,key) == 0){
# Line 355  extern void addLinkEntry(struct Instance Line 355  extern void addLinkEntry(struct Instance
355    
356      /* in case the LINK key is in fact the index of a for loop, the value of the      /* in case the LINK key is in fact the index of a for loop, the value of the
357      index at the current iteration is turned into a symchar and stored as a key*/      index at the current iteration is turned into a symchar and stored as a key*/
358      if(GetEvaluationForTable() && (ptr = FindForVar((struct gl_list_t *)GetEvaluationForTable(),key)) != NULL) {      if(GetEvaluationForTable() && (ptr = FindForVar((struct gl_list_t *)GetEvaluationForTable(),key)) != NULL) {
359          char index_key[10];          char index_key[10];
360          sprintf(index_key,"%ld",GetForInteger(ptr));          sprintf(index_key,"%ld",GetForInteger(ptr));
361          key = AddSymbol(index_key);          key = AddSymbol(index_key);
362      }      }
363        
364      if(declarative == 0) {      if(declarative == 0) {
365          /* we first check if the LINK we are about to add isn't already present in the declartive LINK table */          /* we first check if the LINK we are about to add isn't already present in the declartive LINK table */
366          len = gl_length(MOD_INST(model)->link_table);          len = gl_length(MOD_INST(model)->link_table);
# Line 370  extern void addLinkEntry(struct Instance Line 370  extern void addLinkEntry(struct Instance
370                  exist = 1;                  exist = 1;
371              }              }
372          }          }
373            
374          if(!exist){          if(!exist){
375    
376              link_entry = (struct link_entry_t *)ascmalloc(sizeof(struct link_entry_t));              link_entry = (struct link_entry_t *)ascmalloc(sizeof(struct link_entry_t));
377              link_entry->key_cache = key;              link_entry->key_cache = key;
378            link_entry->u.statptr = stat;                link_entry->u.statptr = stat;
379              link_entry->link_type = stat->v.lnk.key_type;              link_entry->link_type = stat->v.lnk.key_type;
380              link_entry->u.vl = LINKStatVlist(stat);              link_entry->u.vl = LINKStatVlist(stat);
381              link_entry->instances_cache = instances;              link_entry->instances_cache = instances;
382              link_entry->flags = 1;              link_entry->flags = 1;
383            link_entry->length = gl_length(instances);            link_entry->length = gl_length(instances);
384    
385              /**< DS: in case the link entry is non-declartive, it is appended to the linktable in the model instance */              /**< DS: in case the link entry is non-declartive, it is appended to the linktable in the model instance */
386              gl_append_ptr(MOD_INST(model)->link_table,(VOIDPTR)link_entry);              gl_append_ptr(MOD_INST(model)->link_table,(VOIDPTR)link_entry);
387              printf("\n non-declarative LINK no of instances in cache: %ld \n", gl_length(link_entry->instances_cache));              CONSOLE_DEBUG("procedural LINK no of instances in cache: %ld", gl_length(link_entry->instances_cache));
388              printf("\n non-declarative LINK key %s \n", SCP(key));              CONSOLE_DEBUG("procedural LINK key '%s'", SCP(key));
389          }else{          }else{
390              ERROR_REPORTER_HERE(ASC_USER_WARNING,"The LINK entry to-be added is already present in the non-declarative LINK table.");              ERROR_REPORTER_HERE(ASC_USER_WARNING,"The LINK entry to-be added is already present in the non-declarative LINK table.");
391          }          }
# Line 406  extern void addLinkEntry(struct Instance Line 406  extern void addLinkEntry(struct Instance
406              if(!exist){              if(!exist){
407                  link_entry = (struct link_entry_t *)ascmalloc(sizeof(struct link_entry_t));                  link_entry = (struct link_entry_t *)ascmalloc(sizeof(struct link_entry_t));
408                  link_entry->key_cache = key;                  link_entry->key_cache = key;
409                link_entry->u.statptr = stat;                    link_entry->u.statptr = stat;
410                  link_entry->link_type = stat->v.lnk.key_type;                  link_entry->link_type = stat->v.lnk.key_type;
411                  link_entry->u.vl = LINKStatVlist(stat);                  link_entry->u.vl = LINKStatVlist(stat);
412                  link_entry->instances_cache = instances;                  link_entry->instances_cache = instances;
413                  link_entry->flags = 1;                  link_entry->flags = 1;
414                link_entry->length = gl_length(instances);                link_entry->length = gl_length(instances);
415        
416                  /**< DS: in case the link entry is declarative, it is appeneded to the linktable in the model type description */                  /**< DS: in case the link entry is declarative, it is appeneded to the linktable in the model type description */
417                  gl_append_ptr(modelType->u.modarg.link_table,(VOIDPTR)link_entry);                  gl_append_ptr(modelType->u.modarg.link_table,(VOIDPTR)link_entry);
418                
419                  /* DS: testing purposes: */                  /* DS: testing purposes: */
420                  printf("\n declarative LINK no of instances in cache: %ld \n", gl_length(link_entry->instances_cache));                  CONSOLE_DEBUG("declarative LINK no of instances in cache: %ld", gl_length(link_entry->instances_cache));
421                  printf("\n declarative LINK key %s \n", SCP(key));                        CONSOLE_DEBUG("declarative LINK key %s", SCP(key));
422              }else{              }else{
423                  ERROR_REPORTER_HERE(ASC_USER_WARNING,"The LINK entry to-be added is already present in the declarative LINK table.");                  ERROR_REPORTER_HERE(ASC_USER_WARNING,"The LINK entry to-be added is already present in the declarative LINK table.");
424              }              }
# Line 447  extern void ignoreDeclLinkEntry(struct I Line 447  extern void ignoreDeclLinkEntry(struct I
447          }          }
448      }      }
449      len = gl_length(modelType->u.modarg.link_table);      len = gl_length(modelType->u.modarg.link_table);
450      printf("\n new declarative link_table size %d\n",len);      printf("\n new declarative link_table size %d\n",len);
451    
452      if(!exist){      if(!exist){
453         ERROR_REPORTER_HERE(ASC_USER_ERROR,"The LINK entry to-be ignored does not exist.");         ERROR_REPORTER_HERE(ASC_USER_ERROR,"The LINK entry to-be ignored does not exist.");
# Line 472  extern void removeLinkEntry(struct Insta Line 472  extern void removeLinkEntry(struct Insta
472    
473      /* in case the LINK key is in fact the index of a for loop, the value of      /* in case the LINK key is in fact the index of a for loop, the value of
474      the index at the current iteration is turned into a symchar and stored as a key*/      the index at the current iteration is turned into a symchar and stored as a key*/
475      if(GetEvaluationForTable() && (ptr = FindForVar((struct gl_list_t *)GetEvaluationForTable(),key)) != NULL) {      if(GetEvaluationForTable() && (ptr = FindForVar((struct gl_list_t *)GetEvaluationForTable(),key)) != NULL) {
476          char index_key[10];          char index_key[10];
477          sprintf(index_key,"%ld",GetForInteger(ptr));          sprintf(index_key,"%ld",GetForInteger(ptr));
478          key = AddSymbol(index_key);          key = AddSymbol(index_key);
# Line 497  extern void removeLinkEntry(struct Insta Line 497  extern void removeLinkEntry(struct Insta
497      if(!exist){      if(!exist){
498         ERROR_REPORTER_HERE(ASC_USER_ERROR,"The LINK entry to-be removed does not exist.");         ERROR_REPORTER_HERE(ASC_USER_ERROR,"The LINK entry to-be removed does not exist.");
499      }      }
500  }  }
501    
502    
503  /** DS: (current Implementation) check if the the non-declarative link table contains any of the instances in the list under the given key, if so remove them from the entries */  /** DS: (current Implementation) check if the the non-declarative link table contains any of the instances in the list under the given key, if so remove them from the entries */
# Line 514  extern void removeNonDeclarativeLinkEntr Line 514  extern void removeNonDeclarativeLinkEntr
514      }else{      }else{
515          CollectLinks(model, result);          CollectLinks(model, result);
516      }      }
517        
518      printf("\n execute removeNonDeclarativeLinkEntry \n");      printf("\n execute removeNonDeclarativeLinkEntry \n");
519      len =  gl_length(MOD_INST(model)->link_table);      len =  gl_length(MOD_INST(model)->link_table);
520      printf("\n non-declarative link_table size: %d\n",len);      printf("\n non-declarative link_table size: %d\n",len);
# Line 529  extern void removeNonDeclarativeLinkEntr Line 529  extern void removeNonDeclarativeLinkEntr
529              c++;              c++;
530          }          }
531      }      }
532        
533      len = gl_length(MOD_INST(model)->link_table);      len = gl_length(MOD_INST(model)->link_table);
534      printf("\n non-declarative link_table size: %d\n",len);      printf("\n non-declarative link_table size: %d\n",len);
535  }  }
536    
537    
538  const struct gl_list_t *getLinkInstances(struct Instance *inst  const struct gl_list_t *getLinkInstances(struct Instance *inst
539      , struct link_entry_t *link_entry,int status      , struct link_entry_t *link_entry,int status
540  ){  ){
541      struct gl_list_t *result = gl_create(AVG_LINKS_INST);      struct gl_list_t *result = gl_create(AVG_LINKS_INST);
542      enum find_errors err;      enum find_errors err;
543        
544      result = FindInstsNonFlat(inst,link_entry->u.vl,&err);      result = FindInstsNonFlat(inst,link_entry->u.vl,&err);
545    
546      if (result==NULL) {      if (result==NULL) {
# Line 558  const struct gl_list_t *getLinkInstances Line 558  const struct gl_list_t *getLinkInstances
558  const struct gl_list_t *getLinkInstancesFlat(struct Instance *inst  const struct gl_list_t *getLinkInstancesFlat(struct Instance *inst
559      , struct link_entry_t *link_entry,int status      , struct link_entry_t *link_entry,int status
560  ){  ){
561      struct gl_list_t *result = gl_create(AVG_LINKS_INST);      struct gl_list_t *result = gl_create(AVG_LINKS_INST);
562      enum find_errors err;      enum find_errors err;
563      if(link_entry->instances_cache == NULL) {      if(link_entry->instances_cache == NULL) {
564          result = FindInsts(inst,link_entry->u.vl,&err);          result = FindInsts(inst,link_entry->u.vl,&err);
# Line 614  extern int isDeclarative(struct Instance Line 614  extern int isDeclarative(struct Instance
614      /* DS: get all the links that contain the target instance from the declarative part (i.e. stored in the model TypeDescription) */      /* DS: get all the links that contain the target instance from the declarative part (i.e. stored in the model TypeDescription) */
615      modelType = InstanceTypeDesc(model);      modelType = InstanceTypeDesc(model);
616      len = gl_length(modelType->u.modarg.link_table);      len = gl_length(modelType->u.modarg.link_table);
617        
618      for(c=1;c<=len;c++){      for(c=1;c<=len;c++){
619          link_entry = (struct link_entry_t *)gl_fetch(modelType->u.modarg.link_table,c);          link_entry = (struct link_entry_t *)gl_fetch(modelType->u.modarg.link_table,c);
620          if(CmpLinkEntry(target_link_entry->key_cache,target_link_entry->u.vl,link_entry) == 0) {          if(CmpLinkEntry(target_link_entry->key_cache,target_link_entry->u.vl,link_entry) == 0) {
# Line 778  void TestingRoutine(struct Instance *mod Line 778  void TestingRoutine(struct Instance *mod
778      struct TypeDescription *modelType;      struct TypeDescription *modelType;
779      modelType = InstanceTypeDesc(model);      modelType = InstanceTypeDesc(model);
780      int c1, len1, len2;      int c1, len1, len2;
781        
782      /* test getLinkTypes */      /* test getLinkTypes */
783      struct gl_list_t *linkTypes;      struct gl_list_t *linkTypes;
784      symchar *keyc1;      symchar *keyc1;
# Line 799  void TestingRoutine(struct Instance *mod Line 799  void TestingRoutine(struct Instance *mod
799      links = getLinks(model,keyc1,0);      links = getLinks(model,keyc1,0);
800      len2 = gl_length(links);      len2 = gl_length(links);
801      CONSOLE_DEBUG("\n number of links with key %s is: %d \n",SCP(keyc1),len2);      CONSOLE_DEBUG("\n number of links with key %s is: %d \n",SCP(keyc1),len2);
802        
803      /* just a test for comparing two instances pointer-wise */      /* just a test for comparing two instances pointer-wise */
804      /*        /*
805      struct Instance *i11,*i22;      struct Instance *i11,*i22;
806      struct link_entry_t *lnk1,lnk2;      struct link_entry_t *lnk1,lnk2;
807        
808      lnk1 = (struct link_entry_t *)gl_fetch(modelType->u.modarg.link_table,1);      lnk1 = (struct link_entry_t *)gl_fetch(modelType->u.modarg.link_table,1);
809      lnk2 = (struct link_entry_t *)gl_fetch(MOD_INST(model)->link_table,1);      lnk2 = (struct link_entry_t *)gl_fetch(MOD_INST(model)->link_table,1);
810      i11 = (struct Instance *)gl_fetch(lnk1->instances_cache,2);      i11 = (struct Instance *)gl_fetch(lnk1->instances_cache,2);
# Line 813  void TestingRoutine(struct Instance *mod Line 813  void TestingRoutine(struct Instance *mod
813          printf("\n are equal \n");          printf("\n are equal \n");
814      } */      } */
815    
816      /* test getLinksReferencing */        /* test getLinksReferencing */
817      lnk = (struct link_entry_t *)gl_fetch(MOD_INST(model)->link_table,1);      lnk = (struct link_entry_t *)gl_fetch(MOD_INST(model)->link_table,1);
818          /* take the first link from all the non-declarative and declarative LINK Tables, just for testing */          /* take the first link from all the non-declarative and declarative LINK Tables, just for testing */
819      populateLinkCache(model);      populateLinkCache(model);
820      i1= (struct Instance *)gl_fetch(lnk->instances_cache,1);      i1= (struct Instance *)gl_fetch(lnk->instances_cache,1);
# Line 827  void TestingRoutine(struct Instance *mod Line 827  void TestingRoutine(struct Instance *mod
827      /* test getLinkInstancesFlat */      /* test getLinkInstancesFlat */
828    
829      /* test isDeclarative */      /* test isDeclarative */
830      modelType = InstanceTypeDesc(model);          modelType = InstanceTypeDesc(model);
831      CONSOLE_DEBUG("\n the link should be declarative %d\n"      CONSOLE_DEBUG("\n the link should be declarative %d\n"
832          ,isDeclarative(model,(struct link_entry_t *)gl_fetch(modelType->u.modarg.link_table,1))          ,isDeclarative(model,(struct link_entry_t *)gl_fetch(modelType->u.modarg.link_table,1))
833      );      );

Legend:
Removed from v.2568  
changed lines
  Added in v.2569

john.pye@anu.edu.au
ViewVC Help
Powered by ViewVC 1.1.22