Adding support for compiling struct data types
authorlbessard
Fri, 19 Dec 2008 15:09:29 +0100
changeset 160 59d58f5e6caa
parent 159 1e4eb0d48385
child 161 a27957e13d42
Adding support for compiling struct data types
stage4/generate_c/function_param_iterator.cc
stage4/generate_c/generate_c_base.cc
stage4/generate_c/generate_c_sfcdecl.cc
stage4/generate_c/generate_c_st.cc
stage4/generate_c/generate_c_typedecl.cc
stage4/generate_c/generate_c_vardecl.cc
stage4/generate_c/generate_var_list.cc
stage4/generate_c/search_base_type.cc
stage4/generate_c/search_varfb_instance_type.cc
stage4/generate_c/type_initial_value.cc
--- a/stage4/generate_c/function_param_iterator.cc	Mon Dec 15 17:23:48 2008 +0100
+++ b/stage4/generate_c/function_param_iterator.cc	Fri Dec 19 15:09:29 2008 +0100
@@ -244,13 +244,16 @@
       if (en_declared) ERROR;
       return (void *)declare_en_param();
     }
+
+    /* var1_list ':' array_spec_init */
+    //SYM_REF2(array_var_init_decl_c, var1_list, array_spec_init)
+    void *visit(array_var_init_decl_c *symbol) {TRACE("array_var_init_decl_c"); return symbol->var1_list->accept(*this);}
+    
+    /*  var1_list ':' initialized_structure */
+    //SYM_REF2(structured_var_init_decl_c, var1_list, initialized_structure)
+    void *visit(structured_var_init_decl_c *symbol) {TRACE("structured_var_init_decl_c"); return symbol->var1_list->accept(*this);}
+    
 #if 0
-/* var1_list ':' array_spec_init */
-SYM_REF2(array_var_init_decl_c, var1_list, array_spec_init)
-
-/*  var1_list ':' initialized_structure */
-SYM_REF2(structured_var_init_decl_c, var1_list, initialized_structure)
-
 /* name_list ':' function_block_type_name ASSIGN structure_initialization */
 /* structure_initialization -> may be NULL ! */
 SYM_REF4(fb_name_decl_c, fb_name_list, function_block_type_name, structure_initialization, unused)
@@ -276,15 +279,16 @@
     }
     void *visit(var_declaration_list_c *symbol) {TRACE("var_declaration_list_c"); return iterate_list(symbol);}
 
-#if 0
-/*  var1_list ':' array_specification */
-SYM_REF2(array_var_declaration_c, var1_list, array_specification)
-
-/*  var1_list ':' structure_type_name */
-SYM_REF2(structured_var_declaration_c, var1_list, structure_type_name)
-#endif
-
-/* VAR [CONSTANT] var_init_decl_list END_VAR */
+
+    /*  var1_list ':' array_specification */
+    //SYM_REF2(array_var_declaration_c, var1_list, array_specification)
+    void *visit(array_var_declaration_c *symbol) {TRACE("array_var_declaration_c"); return symbol->var1_list->accept(*this);}
+
+    /*  var1_list ':' structure_type_name */
+    //SYM_REF2(structured_var_declaration_c, var1_list, structure_type_name)
+    void *visit(structured_var_declaration_c *symbol) {TRACE("structured_var_declaration_c"); return symbol->var1_list->accept(*this);}
+
+    /* VAR [CONSTANT] var_init_decl_list END_VAR */
     void *visit(var_declarations_c *symbol) {TRACE("var_declarations_c"); return NULL;}
 
 #if 0
--- a/stage4/generate_c/generate_c_base.cc	Mon Dec 15 17:23:48 2008 +0100
+++ b/stage4/generate_c/generate_c_base.cc	Fri Dec 19 15:09:29 2008 +0100
@@ -87,6 +87,7 @@
     ~generate_c_base_c(void) {}
 
     void set_variable_prefix(const char *variable_prefix) {variable_prefix_ = variable_prefix;}
+    const char *get_variable_prefix(void) {return variable_prefix_;}
     bool is_variable_prefix_null(void) {return variable_prefix_ == NULL;}
     void print_variable_prefix(void) {
       if (variable_prefix_ != NULL)
--- a/stage4/generate_c/generate_c_sfcdecl.cc	Mon Dec 15 17:23:48 2008 +0100
+++ b/stage4/generate_c/generate_c_sfcdecl.cc	Fri Dec 19 15:09:29 2008 +0100
@@ -166,6 +166,7 @@
           s4o.print(s4o.indent_spaces + "}\n");
           
           /* last_ticktime initialisation */
+          s4o.print(s4o.indent_spaces);
           print_variable_prefix();
           s4o.print("__lasttick_time = __CURRENT_TIME;\n");
           break;
--- a/stage4/generate_c/generate_c_st.cc	Mon Dec 15 17:23:48 2008 +0100
+++ b/stage4/generate_c/generate_c_st.cc	Fri Dec 19 15:09:29 2008 +0100
@@ -628,7 +628,9 @@
   function_call_param_iterator_c function_call_param_iterator(symbol);
   for(int i = 1; (param_name = fp_iterator.next()) != NULL; i++) {
     function_param_iterator_c::param_direction_t param_direction = fp_iterator.param_direction();
-
+    
+    fprintf(stderr, "param : %s\n", param_name->value);
+    
     /* Get the value from a foo(<param_name> = <param_value>) style call */
     symbol_c *param_value = function_call_param_iterator.search(param_name);
 
--- a/stage4/generate_c/generate_c_typedecl.cc	Mon Dec 15 17:23:48 2008 +0100
+++ b/stage4/generate_c/generate_c_typedecl.cc	Fri Dec 19 15:09:29 2008 +0100
@@ -203,7 +203,7 @@
   current_basetypedeclaration = none_bd;
   s4o_incl.print(" ");
   symbol->subrange_type_name->accept(*basedecl);
-  s4o_incl.print(";\n\n");
+  s4o_incl.print(";\n");
   
   current_type_name = symbol->subrange_type_name;
   
@@ -376,13 +376,20 @@
   return NULL;
 }
 
-/* array_specification [ASSIGN array_initialization} */
+/* array_specification [ASSIGN array_initialization] */
 /* array_initialization may be NULL ! */
 void *visit(array_spec_init_c *symbol) {
   TRACE("array_spec_init_c");
-  current_typedefinition = array_td;
-  symbol->array_specification->accept(*this);
-  current_typedefinition = none_td;
+  
+  identifier_c *array_type_name = dynamic_cast<identifier_c *>(symbol->array_specification);
+  
+  if (array_type_name == NULL) {
+    current_typedefinition = array_td;
+    symbol->array_specification->accept(*this);
+    current_typedefinition = none_td;
+  }
+  else
+    symbol->array_specification->accept(*basedecl);
   return NULL;
 }
 
@@ -435,7 +442,7 @@
 /* helper symbol for data_type_declaration */
 void *visit(type_declaration_list_c *symbol) {
   TRACE("type_declaration_list_c");
-  return print_list(symbol, "", "\n");
+  return print_list_incl(symbol, "", "\n", "\n");
 }
 
 /*  simple_type_name ':' simple_spec_init */
@@ -531,6 +538,8 @@
 //SYM_REF2(initialized_structure_c, structure_type_name, structure_initialization)
 void *visit(initialized_structure_c *symbol) {
   TRACE("initialized_structure_c");
+  fprintf(stderr, "initialized_structure_c\n");
+  
   symbol->structure_type_name->accept(*this);
   return NULL;
 }
@@ -541,15 +550,15 @@
 //SYM_LIST(structure_element_declaration_list_c)
 void *visit(structure_element_declaration_list_c *symbol) {
   TRACE("structure_element_declaration_list_c");
-  s4o.print("struct {\n");
-  s4o.indent_right();
-  s4o.print(s4o.indent_spaces);
-
-  print_list(symbol);
-
-  s4o.indent_left();
-  s4o.print(s4o.indent_spaces);
-  s4o.print("}");
+  s4o_incl.print("struct {\n");
+  s4o_incl.indent_right();
+  s4o_incl.print(s4o_incl.indent_spaces);
+
+  print_list_incl(symbol, "", s4o_incl.indent_spaces, "");
+
+  s4o_incl.indent_left();
+  s4o_incl.print(s4o_incl.indent_spaces);
+  s4o_incl.print("}");
   return NULL;
 }
 
@@ -559,10 +568,10 @@
   TRACE("structure_element_declaration_c");
 
   symbol->spec_init->accept(*this);
-  s4o.print(" ");
-  symbol->structure_element_name->accept(*this);
-  s4o.print(";\n");
-  s4o.print(s4o.indent_spaces);
+  s4o_incl.print(" ");
+  symbol->structure_element_name->accept(*basedecl);
+  s4o_incl.print(";\n");
+  s4o_incl.print(s4o.indent_spaces);
 
   return NULL;
 }
@@ -575,7 +584,7 @@
   TRACE("structure_element_initialization_list_c");
 
   // TODO ???
-  ERROR;
+  //ERROR;
   return NULL;
 }
 
@@ -585,7 +594,7 @@
   TRACE("structure_element_initialization_c");
 
   // TODO ???
-  ERROR;
+  //ERROR;
   return NULL;
 }
 
--- a/stage4/generate_c/generate_c_vardecl.cc	Mon Dec 15 17:23:48 2008 +0100
+++ b/stage4/generate_c/generate_c_vardecl.cc	Fri Dec 19 15:09:29 2008 +0100
@@ -56,25 +56,37 @@
 
     arrayinitialization_mode_t current_mode;
     
+    symbol_c* array_base_type;
     symbol_c* array_default_value;
+    symbol_c* array_default_initialization;
 
   private:
-    int dimension_number, current_dimension, array_size;
+    int dimension_number;
+    int current_dimension;
+    int array_size;
+    int defined_values_count;
+    int current_initialization_count;
 
   public:
     generate_c_array_initialization_c(stage4out_c *s4o_ptr): generate_c_typedecl_c(s4o_ptr) {}
     ~generate_c_array_initialization_c(void) {}
 
+    void init_array_dimensions(symbol_c *array_specification) {
+      dimension_number = 0;
+      current_dimension = 0;
+      array_size = 1;
+      defined_values_count = 0;
+      current_initialization_count = 0;
+      array_base_type = array_default_value = array_default_initialization = NULL;
+      
+      current_mode = dimensioncount_am;
+      array_specification->accept(*this);
+    }
+
     void init_array(symbol_c *var1_list, symbol_c *array_specification, symbol_c *array_initialization) {
       int i;
       
-      dimension_number = 0;
-      current_dimension = 0;
-      array_size = 1;
-      array_default_value = NULL;
-      
-      current_mode = dimensioncount_am;
-      array_specification->accept(*this);
+      init_array_dimensions(array_specification);
       
       current_mode = initializationvalue_am;
       s4o.print("\n");
@@ -86,7 +98,7 @@
       s4o.print(s4o.indent_spaces);
       array_specification->accept(*this);
       s4o.print(" temp = ");
-      array_initialization->accept(*this);
+      init_array_values(array_initialization);
       s4o.print(";\n");
       
       current_mode = arrayassignment_am;
@@ -103,7 +115,23 @@
       s4o.indent_left();
       s4o.print(s4o.indent_spaces + "}");
     }
-
+    
+    void init_array_values(symbol_c *array_initialization) {
+      s4o.print("{");
+      array_initialization->accept(*this);
+      if (array_default_initialization != NULL && defined_values_count < array_size)
+        array_default_initialization->accept(*this);
+      if (defined_values_count < array_size) {
+        for (int i = defined_values_count; i < array_size; i++) {
+          if (defined_values_count > 0)
+            s4o.print(", ");
+          array_default_value->accept(*this);
+          defined_values_count++;
+        }
+      }
+      s4o.print("}");
+    }
+    
     void *visit(identifier_c *type_name) {
       symbol_c *type_decl;
       switch (current_mode) {
@@ -150,15 +178,30 @@
 /* B 1.3.3 - Derived data types */
 /********************************/
     
+    /* array_specification [ASSIGN array_initialization] */
+    /* array_initialization may be NULL ! */
+    void *visit(array_spec_init_c *symbol) {
+      switch (current_mode) {
+        case dimensioncount_am:
+          array_default_initialization = symbol->array_initialization;
+          break;
+        default:
+          break;
+      }
+      symbol->array_specification->accept(*this);
+      return NULL;
+    }
+    
     /* ARRAY '[' array_subrange_list ']' OF non_generic_type_name */
     void *visit(array_specification_c *symbol) {
+      symbol->array_subrange_list->accept(*this);
       switch (current_mode) {
         case dimensioncount_am:
-          symbol->array_subrange_list->accept(*this);
+          array_base_type = symbol->non_generic_type_name;
           array_default_value = (symbol_c *)symbol->non_generic_type_name->accept(*type_initial_value_c::instance());;
+          if (array_default_value == NULL) ERROR;
           break;
         default:
-          symbol->array_subrange_list->accept(*this);
           break;
       } 
       return NULL;
@@ -196,25 +239,24 @@
     void *visit(array_initial_elements_list_c *symbol) {
       switch (current_mode) {
         case initializationvalue_am:
-          int i;
-      
-          s4o.print("{");
-          for (i = 0; i < symbol->n; i++) {
-            if (i > 0)
-              s4o.print(", ");
-            symbol->elements[i]->accept(*this);
-            array_size--;
+          current_initialization_count = 0;
+          for (int i = 0; i < symbol->n; i++) {
+            if (current_initialization_count >= defined_values_count) {
+              if (defined_values_count >= array_size)
+                ERROR;
+              if (defined_values_count > 0)
+                s4o.print(", ");
+              symbol->elements[i]->accept(*this);
+              defined_values_count++;
+            }
+            else {
+              array_initial_elements_c *array_initial_element = dynamic_cast<array_initial_elements_c *>(symbol->elements[i]);
+            
+              if (array_initial_element != NULL)
+                symbol->elements[i]->accept(*this);
+            }
+            current_initialization_count++;
           }
-          if (array_size > 0) {
-            if (symbol->n > 0)
-              s4o.print(", ");
-            for (i = 0; i < array_size; i++) {
-              if (i > 0)
-                s4o.print(", ");
-              array_default_value->accept(*this);
-            }
-          }
-          s4o.print("}");
           break;
         default:
           break;
@@ -230,17 +272,32 @@
       switch (current_mode) {
         case initializationvalue_am:
           initial_element_number = extract_integer(symbol->integer);
-          
+          if (current_initialization_count < defined_values_count) {
+            int temp_element_number = 0;
+            int diff = defined_values_count - current_initialization_count;
+            if (diff <= initial_element_number)
+              temp_element_number = initial_element_number - diff;
+            current_initialization_count += initial_element_number - 1;
+            initial_element_number = temp_element_number;
+            if (initial_element_number > 0) {
+              defined_values_count++;
+              s4o.print(", ");
+            }
+          }
+          else
+            current_initialization_count += initial_element_number - 1;
+          if (defined_values_count + initial_element_number > array_size)
+            ERROR;
           for (int i = 0; i < initial_element_number; i++) {
             if (i > 0)
               s4o.print(", ");
             if (symbol->array_initial_element != NULL)
               symbol->array_initial_element->accept(*this);
-            else if (array_default_value != NULL)
+            else
               array_default_value->accept(*this);
           }
           if (initial_element_number > 1)
-            array_size -= initial_element_number - 1;
+            defined_values_count += initial_element_number - 1;
           break;
         default:
           break;
@@ -248,6 +305,7 @@
       return NULL;
     }
 
+    void *visit(structure_element_initialization_list_c *symbol);
 };
 
 /***********************************************************************/
@@ -259,6 +317,334 @@
 /***********************************************************************/
 /***********************************************************************/
 
+/* given a structure_element_declaration_list_c, iterate 
+ * through each element, returning the name
+ * of each element...structure_element_iterator_c
+ */
+class structure_element_iterator_c : public null_visitor_c {
+  
+  private:
+    /* a pointer to the structure_element_declaration_list_c
+     * currently being analysed.
+     */
+    symbol_c *type_decl;
+    int next_element, element_count;
+    identifier_c *current_element_name;
+    symbol_c *current_element_type;
+    symbol_c *current_element_default_value;
+    
+    
+  public:
+    /* start off at the first parameter once again... */
+    void reset(void) {
+      next_element = element_count = 0;
+      current_element_name = NULL;
+      current_element_type = current_element_default_value = NULL;
+    }
+    
+    /* initialise the iterator object.
+     * We must be given a reference to the function declaration
+     * that will be analysed...
+     */
+    structure_element_iterator_c(symbol_c *type_decl) {
+      this->type_decl = type_decl;
+      reset();
+    }
+    
+    /* Skip to the next element. After object creation,
+     * the object references on 
+     * element _before_ the first, so
+     * this function must be called once to get the object to
+     * reference the first element...
+     *
+     * Returns the element's name!
+     */
+    identifier_c *next(void) {
+      void *res;
+      identifier_c *identifier;
+      element_count = 0;
+      next_element++;
+      res = type_decl->accept(*this);
+      if (res != NULL) {
+        symbol_c *sym = (symbol_c *)res;
+        identifier = dynamic_cast<identifier_c *>(sym);
+        if (identifier == NULL)
+          ERROR;
+      }
+      else
+        return NULL;
+      
+      current_element_name = identifier;
+      return current_element_name;
+    }
+    
+    /* Returns the currently referenced element's default value,
+     * or NULL if none is specified in the structure declaration itself.
+     */
+    symbol_c *default_value(void) {
+      return current_element_default_value;
+    }
+
+    /* Returns the currently referenced element's type name. */
+    symbol_c *element_type(void) {
+      return current_element_type;
+    }
+
+/********************************/
+/* B 1.3.3 - Derived data types */
+/********************************/
+
+    /* helper symbol for structure_declaration */
+    /* structure_element_declaration_list structure_element_declaration ';' */
+    void *visit(structure_element_declaration_list_c *symbol) {
+      void *res;
+      for (int i = 0; i < symbol->n; i++) {
+        res = symbol->elements[i]->accept(*this);
+        if (res != NULL)
+          return res;
+      }
+      return NULL;
+    }
+
+    /*  structure_element_name ':' *_spec_init */
+    void *visit(structure_element_declaration_c *symbol) {
+      element_count++;
+      if (next_element == element_count) {
+        current_element_default_value = spec_init_sperator_c::get_init(symbol->spec_init);
+        current_element_type = spec_init_sperator_c::get_spec(symbol->spec_init);
+        return symbol->structure_element_name;
+      }
+      /* not yet the desired element... */
+      return NULL;
+    }
+
+};
+
+/*
+ * Structure init element iterator.
+ * It will search through the elements of a structure initialization
+ */
+class structure_init_element_iterator_c : public null_visitor_c {
+  private:
+    /* a pointer to the structure initialization
+     * currently being analysed.
+     */
+    symbol_c *structure_initialization;
+    identifier_c *search_element_name;
+    
+  public:
+    /* initialise the iterator object.
+     * We must be given a reference to the structure initialization
+     * that will be analysed...
+     */
+    structure_init_element_iterator_c(symbol_c *structure_initialization) {
+      this->structure_initialization = structure_initialization;
+      search_element_name = NULL;
+    }
+    
+    /* Search for the value passed to the element named <element_name>...  */
+    symbol_c *search(symbol_c *element_name) {
+      if (NULL == element_name) ERROR;
+      search_element_name = dynamic_cast<identifier_c *>(element_name);
+      if (NULL == search_element_name) ERROR;
+      void *res = structure_initialization->accept(*this);
+      return (symbol_c *)res;
+    }
+    
+    /* helper symbol for structure_initialization */
+    /* structure_element_initialization_list ',' structure_element_initialization */
+    void *visit(structure_element_initialization_list_c *symbol) {
+      void *res;
+      for (int i = 0; i < symbol->n; i++) {
+        res = symbol->elements[i]->accept(*this);
+        if (res != NULL)
+          return res;
+      }
+      return NULL;
+    }
+    
+    /*  structure_element_name ASSIGN value */
+    void *visit(structure_element_initialization_c *symbol) {
+      identifier_c *element_name = dynamic_cast<identifier_c *>(symbol->structure_element_name);
+      
+      if (element_name == NULL) ERROR;
+      
+      if (strcasecmp(search_element_name->value, element_name->value) == 0)
+        /* FOUND! This is the same element!! */
+        return (void *)symbol->value;
+      return NULL;
+    }
+};
+
+class generate_c_structure_initialization_c: public generate_c_typedecl_c {
+
+  public:
+    typedef enum {
+      none_sm,
+      typedecl_sm,
+      initializationvalue_sm,
+      varlistparse_sm
+    } structureinitialization_mode_t;
+
+    structureinitialization_mode_t current_mode;
+    
+  private:
+    symbol_c* structure_type_decl;
+    symbol_c* current_element_type;
+
+  public:
+    generate_c_structure_initialization_c(stage4out_c *s4o_ptr): generate_c_typedecl_c(s4o_ptr) {}
+    ~generate_c_structure_initialization_c(void) {}
+
+    void init_structure_default(symbol_c *structure_type_name) {
+      structure_type_decl = NULL;
+      current_element_type = NULL;
+      
+      current_mode = typedecl_sm;
+      structure_type_name->accept(*this);
+    }
+
+    void init_structure(symbol_c *var1_list, symbol_c *structure_type_name, symbol_c *structure_initialization) {
+      int i;
+      
+      init_structure_default(structure_type_name);
+      
+      current_mode = initializationvalue_sm;
+      s4o.print("\n");
+      s4o.print(s4o.indent_spaces + "{\n");
+      s4o.indent_right();
+      s4o.print(s4o.indent_spaces);
+      structure_type_name->accept(*this);
+      s4o.print(" temp = ");
+      structure_initialization->accept(*this);
+      s4o.print(";\n");
+      
+      current_mode = varlistparse_sm;
+      var1_list->accept(*this);
+      
+      s4o.indent_left();
+      s4o.print(s4o.indent_spaces + "}");
+    }
+
+    void *visit(identifier_c *type_name) {
+      symbol_c *type_decl;
+      switch (current_mode) {
+        case typedecl_sm:
+          /* look up the type declaration... */
+          type_decl = type_symtable.find_value(type_name);
+          if (type_decl == type_symtable.end_value())
+            /* Type declaration not found!! */
+            ERROR;
+          type_decl->accept(*this);
+          break;
+        default:
+          print_token(type_name);
+          break;
+      }
+      return NULL;
+    }
+    
+    void *visit(var1_list_c *symbol) {
+      int i, j;
+      
+      for (i = 0; i < symbol->n; i++) {
+        s4o.print(s4o.indent_spaces);
+        print_variable_prefix();
+        symbol->elements[i]->accept(*this);
+        s4o.print(" = temp;\n");
+      }
+      return NULL;
+    }
+
+/********************************/
+/* B 1.3.3 - Derived data types */
+/********************************/
+
+    /* helper symbol for structure_declaration */
+    /* structure_element_declaration_list structure_element_declaration ';' */
+    void *visit(structure_element_declaration_list_c *symbol) {
+      switch (current_mode) {
+        case typedecl_sm:
+          structure_type_decl = (symbol_c *)symbol;
+          break;
+        default:
+          break;
+      } 
+      return NULL;
+    }
+
+    /* helper symbol for structure_initialization */
+    /* structure_element_initialization_list ',' structure_element_initialization */
+    void *visit(structure_element_initialization_list_c *symbol) {
+      s4o.print("{");
+      structure_element_iterator_c structure_iterator(structure_type_decl);
+      
+      identifier_c *element_name;
+      structure_init_element_iterator_c structure_init_element_iterator(symbol);
+      for(int i = 1; (element_name = structure_iterator.next()) != NULL; i++) {
+        if (i > 1)
+          s4o.print(", ");
+        
+        /* Get the value from an initialization */
+        symbol_c *element_value = structure_init_element_iterator.search(element_name);
+        
+        if (element_value == NULL) {
+          /* No value given for parameter, so we must use the default... */
+          /* First check whether default value specified in function declaration...*/
+          element_value = structure_iterator.default_value();
+          current_element_type = structure_iterator.element_type();
+        }
+        
+        if (element_value == NULL) {
+          if (current_element_type == NULL) ERROR;
+          
+          /* If not, get the default value of this variable's type */
+          element_value = (symbol_c *)current_element_type->accept(*type_initial_value_c::instance());
+        }
+        
+        if (element_value == NULL) ERROR;
+        
+        element_value->accept(*this);
+      }
+      s4o.print("}");
+      return NULL;
+    }
+
+    /* helper symbol for array_initialization */
+    /* array_initial_elements_list ',' array_initial_elements */
+    void *visit(array_initial_elements_list_c *symbol) {
+      generate_c_array_initialization_c *array_initialization = new generate_c_array_initialization_c(&s4o);
+      array_initialization->set_variable_prefix(get_variable_prefix());
+      array_initialization->init_array_dimensions(current_element_type);
+      array_initialization->current_mode = generate_c_array_initialization_c::initializationvalue_am;
+      array_initialization->init_array_values(symbol);
+      delete array_initialization;
+      return NULL;
+    }
+
+};
+
+
+/* helper symbol for array_initialization */
+/* structure_element_initialization_list ',' structure_element_initialization */
+void *generate_c_array_initialization_c::visit(structure_element_initialization_list_c *symbol) {
+  generate_c_structure_initialization_c *structure_initialization = new generate_c_structure_initialization_c(&s4o);
+  structure_initialization->set_variable_prefix(get_variable_prefix());
+  structure_initialization->init_structure_default(array_base_type);
+  structure_initialization->current_mode = generate_c_structure_initialization_c::initializationvalue_sm;
+  symbol->accept(*structure_initialization);
+  delete structure_initialization;
+  return NULL;
+}
+
+/***********************************************************************/
+/***********************************************************************/
+/***********************************************************************/
+/***********************************************************************/
+/***********************************************************************/
+/***********************************************************************/
+/***********************************************************************/
+/***********************************************************************/
 
 
 
@@ -413,8 +799,6 @@
 
 
   private:
-    generate_c_array_initialization_c *generate_c_array_initialization;
-    
     /* variable used to store the types of variables that need to be processed... */
     /* Only set in the constructor...! */
     /* Will contain a set of values of generate_c_vardecl_c::XXXX_vt */
@@ -583,7 +967,6 @@
   public:
     generate_c_vardecl_c(stage4out_c *s4o_ptr, varformat_t varformat, unsigned int vartype, symbol_c* res_name = NULL)
     : generate_c_typedecl_c(s4o_ptr) {
-      generate_c_array_initialization = new generate_c_array_initialization_c(s4o_ptr);
       wanted_varformat = varformat;
       wanted_vartype   = vartype;
       current_vartype  = none_vt;
@@ -596,9 +979,7 @@
       eno_declared = false;
     }
 
-    ~generate_c_vardecl_c(void) {
-      delete generate_c_array_initialization;
-    }
+    ~generate_c_vardecl_c(void) {}
 
     bool is_en_declared(void) {
       return en_declared;
@@ -610,7 +991,6 @@
 
     void print(symbol_c *symbol, symbol_c *scope = NULL, const char *variable_prefix = NULL) {
       this->set_variable_prefix(variable_prefix);
-      this->generate_c_array_initialization->set_variable_prefix(variable_prefix);
       if (globalinit_vf == wanted_varformat)
         globalnamespace = scope;
 
@@ -793,8 +1173,12 @@
   update_type_init(symbol->array_spec_init);
 
   /* now to produce the c equivalent... */
-  if (wanted_varformat == constructorinit_vf)
-    generate_c_array_initialization->init_array(symbol->var1_list, this->current_var_type_symbol, this->current_var_init_symbol);
+  if (wanted_varformat == constructorinit_vf) {
+    generate_c_array_initialization_c *array_initialization = new generate_c_array_initialization_c(&s4o);
+    array_initialization->set_variable_prefix(get_variable_prefix());
+    array_initialization->init_array(symbol->var1_list, this->current_var_type_symbol, this->current_var_init_symbol);
+    delete array_initialization;
+  }
   else
     symbol->var1_list->accept(*this);
 
@@ -821,7 +1205,14 @@
   update_type_init(symbol->initialized_structure);
 
   /* now to produce the c equivalent... */
-  symbol->var1_list->accept(*this);
+  if (wanted_varformat == constructorinit_vf) {
+    generate_c_structure_initialization_c *structure_initialization = new generate_c_structure_initialization_c(&s4o);
+    structure_initialization->set_variable_prefix(get_variable_prefix());
+    structure_initialization->init_structure(symbol->var1_list, this->current_var_type_symbol, this->current_var_init_symbol);
+    delete structure_initialization;
+  }
+  else
+    symbol->var1_list->accept(*this);
 
   /* Values no longer in scope, and therefore no longer used.
    * Make an effort to keep them set to NULL when not in use
@@ -949,8 +1340,15 @@
    */
   update_type_init(symbol->structure_type_name);
 
-  /* now to produce the c equivalent... */
-  symbol->var1_list->accept(*this);
+  if (wanted_varformat == constructorinit_vf) {
+    generate_c_structure_initialization_c *structure_initialization = new generate_c_structure_initialization_c(&s4o);
+    structure_initialization->set_variable_prefix(get_variable_prefix());
+    structure_initialization->init_structure(symbol->var1_list, this->current_var_type_symbol, this->current_var_init_symbol);
+    delete structure_initialization;
+  }
+  else
+    /* now to produce the c equivalent... */
+    symbol->var1_list->accept(*this);
 
   /* Values no longer in scope, and therefore no longer used.
    * Make an effort to keep them set to NULL when not in use
--- a/stage4/generate_c/generate_var_list.cc	Mon Dec 15 17:23:48 2008 +0100
+++ b/stage4/generate_c/generate_var_list.cc	Fri Dec 19 15:09:29 2008 +0100
@@ -65,9 +65,21 @@
     } declarationtype_t;
 
     declarationtype_t current_declarationtype;
-  
+    
+    typedef enum {
+      none_vtc,
+      variable_vtc,
+      pointer_vtc,
+      array_vtc,
+      structure_vtc,
+      function_block_vtc
+    } vartypecategory_t;
+    
+    vartypecategory_t current_var_type_category;
+    
   private:
     symbol_c *current_var_type_symbol;
+    symbol_c *current_var_type_name;
     unsigned int current_var_number;
     unsigned int step_number;
     unsigned int transition_number;
@@ -82,24 +94,34 @@
     : generate_c_typedecl_c(s4o_ptr) {
       search_fb_typedecl = new search_fb_typedecl_c(scope);
       current_var_number = 0;
-      current_var_type_symbol = NULL;
+      current_var_type_symbol = current_var_type_name = NULL;
       current_declarationtype = none_dt;
+      current_var_type_category = none_vtc;
     }
     
     ~generate_var_list_c(void) {
       delete search_fb_typedecl;
     }
     
-    void update_var_type_symbol(symbol_c *symbol, bool is_fb = false) {
-      
-      this->current_var_type_symbol = spec_init_sperator_c::get_spec(symbol);
-      if (this->current_var_type_symbol == NULL)
+    void update_var_type_symbol(symbol_c *symbol) {
+      
+      this->current_var_type_name = spec_init_sperator_c::get_spec(symbol);
+      if (this->current_var_type_name == NULL)
         ERROR;
       
-      if (is_fb)
-        this->current_var_type_symbol = search_fb_typedecl->get_decl(this->current_var_type_symbol);
-      else
-        this->current_var_type_symbol = (symbol_c *)(this->current_var_type_symbol->accept(search_base_type));
+      this->current_var_type_symbol = search_fb_typedecl->get_decl(this->current_var_type_name);
+      if (this->current_var_type_symbol != NULL)
+        this->current_var_type_category = function_block_vtc;
+      else {
+        this->current_var_type_symbol = (symbol_c *)(this->current_var_type_name->accept(search_base_type));
+        
+        structure_element_declaration_list_c *structure_symbol = dynamic_cast<structure_element_declaration_list_c *>(this->current_var_type_symbol);
+        if (structure_symbol != NULL)
+          this->current_var_type_category = structure_vtc;
+        else
+          this->current_var_type_category = variable_vtc;
+      }
+      
       if (this->current_var_type_symbol == NULL)
         ERROR;
     }
@@ -128,20 +150,36 @@
       s4o.print("\n");
     }
     
-    void declare_variables(symbol_c *symbol, const char* type = "VAR") {
+    void declare_variables(symbol_c *symbol) {
       list_c *list = dynamic_cast<list_c *>(symbol);
       /* should NEVER EVER occur!! */
       if (list == NULL) ERROR;
 
       for(int i = 0; i < list->n; i++) {
-        declare_variable(list->elements[i], type);
-      }
-    }
-    
-    void declare_variable(symbol_c *symbol, const char* type = "VAR") {
+        declare_variable(list->elements[i]);
+      }
+    }
+    
+    void declare_variable(symbol_c *symbol) {
       print_var_number();
       s4o.print(";");
-      s4o.print(type);
+      switch (this->current_var_type_category) {
+        case pointer_vtc:
+          s4o.print("PT");
+          break;
+        case array_vtc:
+          s4o.print("ARRAY");
+          break;
+        case structure_vtc:
+          s4o.print("STRUCT");
+          break;
+        case function_block_vtc:
+          s4o.print("FB");
+          break;
+        default:
+          s4o.print("VAR");
+          break;
+      }
       s4o.print(";");
       print_symbol_list();
       symbol->accept(*this);
@@ -149,17 +187,26 @@
       print_symbol_list();
       symbol->accept(*this);
       s4o.print(";");
-      if (strcmp(type, "FB") == 0) {
-        SYMBOL *current_name;
-        current_name = new SYMBOL;
-        current_name->symbol = symbol;
-        current_symbol_list.push_back(*current_name);
-        this->current_var_type_symbol->accept(*this);
-        current_symbol_list.pop_back();
-      }
-      else {
-        this->current_var_type_symbol->accept(*this);
-        s4o.print(";\n");
+      switch (this->current_var_type_category) {
+        case structure_vtc:
+        case function_block_vtc:
+          this->current_var_type_name->accept(*this);
+          s4o.print(";\n");
+          SYMBOL *current_name;
+          current_name = new SYMBOL;
+          current_name->symbol = symbol;
+          current_symbol_list.push_back(*current_name);
+          this->current_var_type_symbol->accept(*this);
+          current_symbol_list.pop_back();
+          break;
+        case array_vtc:
+          this->current_var_type_name->accept(*this);
+          s4o.print(";\n");
+          break;
+        default:
+          this->current_var_type_symbol->accept(*this);
+          s4o.print(";\n");
+          break;
       }
     }
     
@@ -209,8 +256,10 @@
          */
         update_var_type_symbol(symbol->located_var_spec_init);
         
-        if (symbol->variable_name != NULL)
-          declare_variable(symbol->variable_name, "PT");
+        if (symbol->variable_name != NULL) {
+          this->current_var_type_category = pointer_vtc;
+          declare_variable(symbol->variable_name);
+        }
         
         current_var_type_symbol = NULL;
         return NULL;
@@ -224,7 +273,8 @@
        * current_var_init_symbol private variables...
        */
       update_var_type_symbol(symbol->array_spec_init);
-    
+      
+      this->current_var_type_category = array_vtc;
       declare_variables(symbol->var1_list);
     
       /* Values no longer in scope, and therefore no longer used.
@@ -272,10 +322,10 @@
       /* Start off by setting the current_var_type_symbol and
        * current_var_init_symbol private variables...
        */
-      update_var_type_symbol(symbol, true);
+      update_var_type_symbol(symbol);
     
       /* now to produce the c equivalent... */
-      declare_variables(symbol->fb_name_list, "FB");
+      declare_variables(symbol->fb_name_list);
     
       /* Values no longer in scope, and therefore no longer used.
        * Make an effort to keep them set to NULL when not in use
@@ -297,14 +347,12 @@
       /* Start off by setting the current_var_type_symbol and
        * current_var_init_symbol private variables...
        */
-      this->current_var_type_symbol = (symbol_c *)(symbol->specification->accept(*search_fb_typedecl));
-      if (this->current_var_type_symbol == NULL) {
-        this->current_var_type_symbol = symbol->specification;
-      
-        declare_variable(symbol->global_var_name, "PT");
-      }
-      else
-        declare_variable(symbol->global_var_name, "FB");
+      update_var_type_symbol(symbol);
+      
+      /* now to produce the c equivalent... */
+      if (this->current_var_type_category == variable_vtc)
+        this->current_var_type_category = pointer_vtc;
+      declare_variable(symbol->global_var_name);
       
       /* Values no longer in scope, and therefore no longer used.
        * Make an effort to keep them set to NULL when not in use
@@ -350,7 +398,8 @@
     // SYM_REF2(global_var_spec_c, global_var_name, location)
     void *visit(global_var_spec_c *symbol) {
       if (symbol->global_var_name != NULL)
-        declare_variable(symbol->global_var_name, "PT");
+        this->current_var_type_category = pointer_vtc;
+        declare_variable(symbol->global_var_name);
       return NULL;
     }
     
@@ -381,6 +430,31 @@
       return NULL;
     }
 
+    void *visit(structure_element_declaration_list_c *symbol) {
+      for(int i = 0; i < symbol->n; i++) {
+        symbol->elements[i]->accept(*this);
+      }
+      return NULL;
+    }
+
+    void *visit(structure_element_declaration_c *symbol) {
+      /* Start off by setting the current_var_type_symbol and
+       * current_var_init_symbol private variables...
+       */
+      update_var_type_symbol(symbol->spec_init);
+      
+      /* now to produce the c equivalent... */
+      declare_variable(symbol->structure_element_name);
+      
+      /* Values no longer in scope, and therefore no longer used.
+       * Make an effort to keep them set to NULL when not in use
+       * in order to catch bugs as soon as possible...
+       */
+      reset_var_type_symbol();
+      
+      return NULL;
+    }
+
 /**************************************/
 /* B.1.5 - Program organization units */
 /**************************************/
@@ -397,8 +471,6 @@
 /*****************************/
     void *visit(function_block_declaration_c *symbol) {
       if (current_declarationtype == variables_dt && configuration_defined) {
-        symbol->fblock_name->accept(*this);
-        s4o.print(";\n");
         symbol->var_declarations->accept(*this);
         symbol->fblock_body->accept(*this);
       }
@@ -410,8 +482,6 @@
 /**********************/
     void *visit(program_declaration_c *symbol) {
       if (current_declarationtype == variables_dt && configuration_defined) {
-        symbol->program_type_name->accept(*this);
-        s4o.print(";\n");
         symbol->var_declarations->accept(*this);
         symbol->function_block_body->accept(*this);
       }
@@ -552,9 +622,9 @@
           /* Start off by setting the current_var_type_symbol and
            * current_var_init_symbol private variables...
            */
-          update_var_type_symbol(symbol->program_type_name, true);
+          update_var_type_symbol(symbol->program_type_name);
           
-          declare_variable(symbol->program_name, "FB");
+          declare_variable(symbol->program_name);
           
           /* Values no longer in scope, and therefore no longer used.
            * Make an effort to keep them set to NULL when not in use
--- a/stage4/generate_c/search_base_type.cc	Mon Dec 15 17:23:48 2008 +0100
+++ b/stage4/generate_c/search_base_type.cc	Fri Dec 19 15:09:29 2008 +0100
@@ -203,7 +203,7 @@
 /* structure_element_declaration_list structure_element_declaration ';' */
     void *visit(structure_element_declaration_list_c *symbol)	{
       if (NULL == this->current_type_name) ERROR;
-      return (void *)this->current_type_name;
+      return (void *)symbol;
     }
 
 /*  structure_element_name ':' *_spec_init */
--- a/stage4/generate_c/search_varfb_instance_type.cc	Mon Dec 15 17:23:48 2008 +0100
+++ b/stage4/generate_c/search_varfb_instance_type.cc	Fri Dec 19 15:09:29 2008 +0100
@@ -72,7 +72,7 @@
        */
       symbol_c *var_name_part = decompose_var_instance_name->next_part();
       if (NULL == var_name_part) ERROR;
-
+      
       /* Now we try to find the variable instance declaration, to determine its type... */
       symbol_c *var_decl = search_var_instance_decl.get_decl(var_name_part);
       if (NULL == var_decl) {
@@ -185,11 +185,17 @@
 /* array_specification [ASSIGN array_initialization} */
 /* array_initialization may be NULL ! */
     void *visit(array_spec_init_c *symbol) {
+      symbol_c *var_name = decompose_var_instance_name->next_part();
+      if (NULL != var_name)
+        current_structelement_name = var_name;
       return symbol->array_specification->accept(*this);
     }
     
 /* ARRAY '[' array_subrange_list ']' OF non_generic_type_name */
     void *visit(array_specification_c *symbol) {
+      symbol_c *var_name = decompose_var_instance_name->next_part();
+      if (NULL != var_name)
+        current_structelement_name = var_name;
       return symbol->non_generic_type_name->accept(*this);
     }
 
@@ -241,7 +247,9 @@
 /* helper symbol for structure_declaration */
 /* structure_declaration:  STRUCT structure_element_declaration_list END_STRUCT */
 /* structure_element_declaration_list structure_element_declaration ';' */
-    void *visit(structure_element_declaration_list_c *symbol)	{return visit_list(symbol);}
+    void *visit(structure_element_declaration_list_c *symbol)	{
+      return visit_list(symbol);
+    }
 
 /*  structure_element_name ':' spec_init */
     void *visit(structure_element_declaration_c *symbol) {
--- a/stage4/generate_c/type_initial_value.cc	Mon Dec 15 17:23:48 2008 +0100
+++ b/stage4/generate_c/type_initial_value.cc	Fri Dec 19 15:09:29 2008 +0100
@@ -227,7 +227,7 @@
     }
 /* ARRAY '[' array_subrange_list ']' OF non_generic_type_name */
     void *visit(array_specification_c *symbol)	{
-      symbol_c *init_value = (symbol_c *)symbol->non_generic_type_name->accept(*this);
+      //symbol_c *init_value = (symbol_c *)symbol->non_generic_type_name->accept(*this);
 
       /* Now build a array_initial_elements_list_c list, and populate it
        * with 1 element of the array_initial_elements_c class
@@ -250,12 +250,12 @@
        * in something else...
        */
 	// NOTE: We are leaking memory, as the integer will never get free'd!!
-      integer_c *integer = new integer_c("1");
+      //integer_c *integer = new integer_c("1");
 	// NOTE: We are leaking memory, as the array_initial_elements will never get free'd!!
-      array_initial_elements_c *array_initial_elements = new array_initial_elements_c(integer, init_value);
+      //array_initial_elements_c *array_initial_elements = new array_initial_elements_c(integer, init_value);
 	// NOTE: We are leaking memory, as the array_initial_elements_list will never get free'd!!
       array_initial_elements_list_c *array_initial_elements_list  = new array_initial_elements_list_c();
-      array_initial_elements_list->add_element(array_initial_elements);
+      //array_initial_elements_list->add_element(array_initial_elements);
       return array_initial_elements_list;
     }
 /* helper symbol for array_specification */
@@ -277,11 +277,16 @@
     void *visit(structure_type_declaration_c *symbol) {return NULL;}
 /* structure_type_name ASSIGN structure_initialization */
 /* structure_initialization may be NULL ! */
-    void *visit(initialized_structure_c *symbol)	{return NULL;}
+    void *visit(initialized_structure_c *symbol)	{
+      return handle_type_spec(symbol->structure_type_name, symbol->structure_initialization);
+    }
 /* helper symbol for structure_declaration */
 /* structure_declaration:  STRUCT structure_element_declaration_list END_STRUCT */
 /* structure_element_declaration_list structure_element_declaration ';' */
-    void *visit(structure_element_declaration_list_c *symbol)	{return NULL;}
+    void *visit(structure_element_declaration_list_c *symbol)	{
+      structure_element_initialization_list_c *structure_element_initialization_list = new structure_element_initialization_list_c();
+      return structure_element_initialization_list;
+    }
 /*  structure_element_name ':' *_spec_init */
     void *visit(structure_element_declaration_c *symbol)	{return NULL;}
 /* helper symbol for structure_initialization */