* sim/ucsim/*.*, sim/ucsim/configure, sim/ucsim/configure.in:
[fw/sdcc] / sim / ucsim / pobj.cc
index ae9e850bae0a55eca7825e6452db2c86481abba8..e32deed6a03105499583afb7a53432995192e88a 100644 (file)
@@ -46,18 +46,213 @@ Software Foundation, 59 Temple Place - Suite 330, Boston, MA
  * Initializing the object
  */
 
-cl_base::cl_base(void) {}
+cl_base::cl_base(void)
+{
+  name= 0;
+  parent= 0;
+  children= 0;
+}
 
 
 /* 
  * Destructing the object: calling hte virtual Done method
  */
 
-cl_base::~cl_base(void) {}
-
+cl_base::~cl_base(void)
+{
+  if (name)
+    free(name);
+  if (children)
+    {
+      int i;
+      for (i= 0; i < children->count; i++)
+       {
+       }
+      children->disconn_all();
+      delete children;
+    }
+  parent= 0;
+}
 
 int cl_base::init(void) {return(0);}
 
+const char *
+cl_base::get_name(const char *def)
+{
+  if (!name)
+    return(def);
+  return(name);
+}
+
+const char *
+cl_base::set_name(const char *new_name)
+{
+  if (name)
+    free(name);
+  if (!new_name)
+    name= 0;
+  else if (*new_name)
+    name= strdup(new_name);
+  else
+    name= strdup("");
+  return(name);
+}
+
+const char *
+cl_base::set_name(const char *new_name, const char *def_name)
+{
+  char *def;
+
+  if (!def_name ||
+      *def_name == '\0')
+    def= strdup("");
+  else
+    def= strdup(def_name);
+  if (name)
+    free(name);
+  if (!new_name)
+    name= def;
+  else if (*new_name)
+    name= strdup(new_name);
+  else
+    name= def;
+  return(name);
+}
+
+bool
+cl_base::is_named(const char *the_name)
+{
+  if (!name ||
+      !*name ||
+      !the_name ||
+      !*the_name)
+    return(DD_FALSE);
+  return(strcmp(name, the_name) == 0);
+}
+
+bool
+cl_base::is_inamed(const char *the_name)
+{
+  if (!name ||
+      !*name ||
+      !the_name ||
+      !*the_name)
+    return(DD_FALSE);
+  return(strcasecmp(name, the_name) == 0);
+}
+
+
+int
+cl_base::nuof_children(void)
+{
+  if (!children)
+    return(0);
+  return(children->count);
+}
+
+void
+cl_base::add_child(class cl_base *child)
+{
+  if (!children)
+    {
+      char *s;
+      s= (char*)malloc(strlen(get_name("?"))+100);
+      sprintf(s, "childs of %s", get_name("?"));
+      children= new cl_list(1, 1, s);
+      free(s);
+    }
+  if (child)
+    {
+      children->add(child);
+      child->parent= this;
+    }
+}
+
+void
+cl_base::remove_child(class cl_base *child)
+{
+  if (child &&
+      children)
+    {
+      child->unlink();
+      children->disconn(child);
+    }
+}
+
+void
+cl_base::remove_from_chain(void)
+{
+  if (parent)
+    parent->remove_child(this);
+}
+
+void
+cl_base::unlink(void)
+{
+  parent= 0;
+}
+
+class cl_base *
+cl_base::first_child(void)
+{
+  if (!children ||
+      children->count == 0)
+    return(0);
+  return(dynamic_cast<class cl_base *>(children->object_at(0)));
+}
+
+class cl_base *
+cl_base::next_child(class cl_base *child)
+{
+  if (!children ||
+      !child)
+    return(0);
+  return((class cl_base *)(children->next(child)));
+}
+
+
+bool
+cl_base::handle_event(class cl_event &event)
+{
+  return(pass_event_down(event));
+}
+
+bool
+cl_base::pass_event_down(class cl_event &event)
+{
+  int i;
+  if (!children)
+    return(DD_FALSE);
+  for (i= 0; i < children->count; i++)
+    {
+      class cl_base *child=
+       dynamic_cast<class cl_base *>(children->object_at(i));
+      if (child)
+       {
+         child->handle_event(event);
+         if (event.is_handled())
+           return(DD_TRUE);
+       }
+    }
+  return(DD_FALSE);
+}
+
+
+/*
+ * Event
+ */
+
+cl_event::cl_event(enum event what_event):
+  cl_base()
+{
+  handled= DD_FALSE;
+  what= what_event;
+}
+
+cl_event::~cl_event(void)
+{
+}
+
 
 /*                                                                         *
   ==========================================================================*
@@ -70,7 +265,7 @@ int cl_base::init(void) {return(0);}
  * Initializing a collection
  */
 
-cl_list::cl_list(t_index alimit, t_index adelta):
+cl_list::cl_list(t_index alimit, t_index adelta, const char *aname):
   cl_base()
 {
   count= 0;
@@ -78,6 +273,7 @@ cl_list::cl_list(t_index alimit, t_index adelta):
   Limit= 0;
   Delta= adelta;
   set_limit(alimit);
+  set_name(aname, "unnamed list");
 }
 
 
@@ -87,7 +283,8 @@ cl_list::cl_list(t_index alimit, t_index adelta):
 
 cl_list::~cl_list(void)
 {
-  delete Items;
+  //delete Items;
+  free(Items);
 }
 
 
@@ -104,6 +301,15 @@ cl_list::at(t_index index)
   return(Items[index]);
 }
 
+class cl_base *
+cl_list::object_at(t_index index)
+{
+  if (index < 0 ||
+      index >= count)
+    error(1, index);
+  return((class cl_base *)(Items[index]));
+}
+
 /*void *
 cl_list::operator[](t_index index)
 {
@@ -117,7 +323,7 @@ cl_list::operator[](t_index index)
 /*
  * Deleting the indexed item from the collection
  */
-
+#include "globals.h"
 void
 cl_list::disconn_at(t_index index)
 {
@@ -125,6 +331,16 @@ cl_list::disconn_at(t_index index)
       index >= count)
     error(1, 0);
   count--;
+  /*{ char s[1000];
+  s[0]='\0';
+  sprintf(s, "disconn_at(%d) PC=0x%x", index,
+         application?
+         ((application->sim)?
+          ((application->sim->uc)?(application->sim->uc->PC):
+           -3):
+          -1):
+         -2);
+         strcat(s,"\n");}*/
   memmove(&Items[index], &Items[index+1], (count-index)*sizeof(void *));
 }
 
@@ -136,7 +352,10 @@ cl_list::disconn_at(t_index index)
 void
 cl_list::disconn(void *item)
 {
-  disconn_at(index_of(item));
+  t_index i;
+
+  if (index_of(item, &i))
+    disconn_at(i);
 }
 
 
@@ -164,6 +383,19 @@ cl_list::free_at(t_index index)
   free_item(Item);
 }
 
+void
+cl_list::free_all(void)
+{
+  t_index i;
+
+  if (count)
+    {
+      for (i= count-1; i; i--)
+       free_at(i);
+      free_at(0);
+    }
+}
+
 
 /*
  * Inserting a new item to the exact position
@@ -177,6 +409,16 @@ cl_list::add_at(t_index index, void *item)
   if (count == Limit)
     set_limit(count + Delta);
 
+  { char s[1000];
+  s[0]='\0';
+  sprintf(s, "%s add_at(%d,%p) PC=0x%x (count=%d)", get_name("?"), index, item,
+         application?
+         ((application->sim)?
+          ((application->sim->uc)?(application->sim->uc->PC):
+           -3):
+          -1):
+         -2, count);
+  strcat(s,"\n");}
   memmove(&Items[index+1], &Items[index], (count-index)*sizeof(void *));
   count++;
 
@@ -218,7 +460,7 @@ cl_list::error(t_index code, t_index info)
  */
 
 void *
-cl_list::first_that(match_func test, void *arg)
+cl_list::first_that(match_func test, const void *arg)
 {
   for (t_index i= 0; i < count; i++)
     {
@@ -248,7 +490,7 @@ cl_list::for_each(iterator_func action, void *arg)
 void
 cl_list::free_item(void *item)
 {
-  delete item;
+  delete (class cl_base*)item;
 }
 
 
@@ -297,6 +539,32 @@ cl_list::index_of(void *item)
   return(0);    /* Needed by Sun! */
 }
 
+bool
+cl_list::index_of(void *item, t_index *idx)
+{
+  for (t_index i= 0; i < count; i++)
+    if (item == Items[i])
+      {
+       if (idx)
+         *idx= i;
+       return(DD_TRUE);
+      }
+  return(DD_FALSE);
+}
+
+void *
+cl_list::next(void *item)
+{
+  for (t_index i= 0; i < count; i++)
+    if (item == Items[i])
+      {
+       if (count >= 2 &&
+           i < count-1)
+         return(Items[i+1]);
+      }
+  return(0);
+}
+
 
 /* 
  * Inserting a new item to the collection.
@@ -311,6 +579,13 @@ cl_list::add(void *item)
   return(loc);
 }
 
+t_index
+cl_list::add(class cl_base *item, class cl_base *parent)
+{
+  if (parent && item)
+    parent->add_child(item);
+  return(add(item));
+}
 
 void
 cl_list::push(void *item)
@@ -377,13 +652,14 @@ cl_list::set_limit(t_index alimit)
        AItems= 0;
       else
        {
-         AItems = new void *[alimit];
-         //i= ALimit*(sizeof(void *));
-         //AItems= (void **)malloc(i);
+         //AItems = new void *[alimit];
+         int i= alimit*(sizeof(void *));
+         AItems= (void **)malloc(i);
          if (count)
            memcpy(AItems, Items, count*sizeof(void *));
        }
-      delete Items;
+      //delete Items;
+      free(Items);
       Items= AItems;
       Limit= alimit;
     }
@@ -401,10 +677,10 @@ cl_list::set_limit(t_index alimit)
  * Initilizing the sorted collection
  */
 
-cl_sorted_list::cl_sorted_list(t_index alimit, t_index adelta):
-  cl_list(alimit, adelta)
+cl_sorted_list::cl_sorted_list(t_index alimit, t_index adelta, const char *aname):
+  cl_list(alimit, adelta, aname)
 {
-  Duplicates= FALSE;
+  Duplicates= DD_FALSE;
 }
 
 
@@ -415,7 +691,7 @@ cl_sorted_list::~cl_sorted_list(void) {}
  * Get the address of the key field in an item.
  */
 
-void *
+const void *
 cl_sorted_list::key_of(void *item)
 {
   return(item);
@@ -470,11 +746,11 @@ cl_sorted_list::add(void *item)
  */
 
 bool
-cl_sorted_list::search(void *key, t_index &index)
+cl_sorted_list::search(const void *key, t_index &index)
 {
   t_index l  = 0;
   t_index h  = count - 1;
-  bool   res= FALSE;
+  bool    res= DD_FALSE;
   
   while (l <= h)
     {
@@ -486,7 +762,7 @@ cl_sorted_list::search(void *key, t_index &index)
          h= i - 1;
          if (c == 0)
            {
-             res= TRUE;
+             res= DD_TRUE;
              if (!Duplicates)
                l= i;
            }
@@ -508,10 +784,10 @@ cl_sorted_list::search(void *key, t_index &index)
  * Initilizing the string collection
  */
 
-cl_strings::cl_strings(t_index alimit, t_index adelta):
-  cl_sorted_list(alimit, adelta)
+cl_strings::cl_strings(t_index alimit, t_index adelta, const char *aname):
+  cl_sorted_list(alimit, adelta, aname)
 {
-  Duplicates= TRUE;
+  Duplicates= DD_TRUE;
 }
 
 
@@ -523,9 +799,9 @@ cl_strings::~cl_strings(void) {}
  */
 
 int
-cl_strings::compare(void *key1, void *key2)
+cl_strings::compare(const void *key1, const void *key2)
 {
-  return(strcmp((char *)key1, (char *)key2));
+  return(strcmp(static_cast<const char *>(key1), static_cast<const char *>(key2)));
 }
 
 
@@ -536,7 +812,7 @@ cl_strings::compare(void *key1, void *key2)
 void
 cl_strings::free_item(void* item)
 {
-  delete item;
+  delete (class cl_base*)item;
 }
 
 
@@ -551,8 +827,8 @@ cl_strings::free_item(void* item)
  * Initilizing the unsorted string collection
  */
 
-cl_ustrings::cl_ustrings(t_index alimit, t_index adelta):
-  cl_strings(alimit, adelta)
+cl_ustrings::cl_ustrings(t_index alimit, t_index adelta, const char *aname):
+  cl_strings(alimit, adelta, aname)
 {}
 
 
@@ -564,7 +840,7 @@ cl_ustrings::~cl_ustrings(void) {}
  */
 
 int
-cl_ustrings::compare(void *key1, void *key2)
+cl_ustrings::compare(const void *key1, const void *key2)
 {
   return(-1);
 }
@@ -575,11 +851,11 @@ cl_ustrings::compare(void *key1, void *key2)
  */
 
 bool
-cl_ustrings::search(void *key, t_index& index)
+cl_ustrings::search(const void *key, t_index& index)
 {
   t_index i    = 0;
-  bool    found= FALSE;
-  void    *Actual;
+  bool    found= DD_FALSE;
+  const void *Actual;
 
   if ((count) && key)
     {