Fixing bug with external variables refering to global variables defined in configurations
authorlaurent
Fri, 30 Dec 2011 18:25:39 +0100
changeset 396 155560bfe837
parent 395 25f3b479a8ce
child 397 85151b2f35ca
Fixing bug with external variables refering to global variables defined in configurations
lib/accessor.h
stage4/generate_c/generate_c.cc
stage4/generate_c/generate_c_vardecl.cc
--- a/lib/accessor.h	Fri Dec 23 15:17:45 2011 +0100
+++ b/lib/accessor.h	Fri Dec 30 18:25:39 2011 +0100
@@ -32,6 +32,8 @@
 	type* __GET_GLOBAL_##name(void) {\
 		return (*GLOBAL__##name).value;\
 	}
+#define __DECLARE_GLOBAL_PROTOTYPE(type, name)\
+    extern type* __GET_GLOBAL_##name();
 #define __DECLARE_EXTERNAL(type, name)\
 	__IEC_##type##_p name;
 #define __DECLARE_LOCATED(type, name)\
--- a/stage4/generate_c/generate_c.cc	Fri Dec 23 15:17:45 2011 +0100
+++ b/stage4/generate_c/generate_c.cc	Fri Dec 30 18:25:39 2011 +0100
@@ -104,6 +104,7 @@
 #define DECLARE_GLOBAL_LOCATED "__DECLARE_GLOBAL_LOCATED"
 #define DECLARE_EXTERNAL "__DECLARE_EXTERNAL"
 #define DECLARE_LOCATED "__DECLARE_LOCATED"
+#define DECLARE_GLOBAL_PROTOTYPE "__DECLARE_GLOBAL_PROTOTYPE"
 
 /* Variable declaration symbol for accessor macros */
 #define INIT_VAR "__INIT_VAR"
@@ -126,6 +127,8 @@
 #define SET_EXTERNAL "__SET_EXTERNAL"
 #define SET_LOCATED "__SET_LOCATED"
 
+/* Variable initial value symbol for accessor macros */
+#define INITIAL_VALUE "__INITIAL_VALUE"
 
 /* Generate a name for a temporary variable.
  * Each new name generated is appended a different number,
@@ -1558,11 +1561,13 @@
 class generate_c_config_c: public generate_c_typedecl_c {
     private:
     stage4out_c *s4o_ptr;
-
+    stage4out_c *s4o_incl_ptr;
+    
     public:
-    generate_c_config_c(stage4out_c *s4o_ptr)
-      : generate_c_typedecl_c(s4o_ptr) {
+    generate_c_config_c(stage4out_c *s4o_ptr, stage4out_c *s4o_incl_ptr)
+      : generate_c_typedecl_c(s4o_ptr, s4o_incl_ptr) {
       generate_c_config_c::s4o_ptr = s4o_ptr;
+      generate_c_config_c::s4o_incl_ptr = s4o_incl_ptr;
     };
 
     virtual ~generate_c_config_c(void) {}
@@ -1581,8 +1586,17 @@
 /********************/
 /* 2.1.6 - Pragmas  */
 /********************/
-void *visit(enable_code_generation_pragma_c * symbol)   {s4o_ptr->enable_output();  return NULL;}
-void *visit(disable_code_generation_pragma_c * symbol)  {s4o_ptr->disable_output(); return NULL;} 
+void *visit(enable_code_generation_pragma_c * symbol)   {
+    s4o_ptr->enable_output();
+    s4o_incl_ptr->enable_output();
+    return NULL;
+}
+
+void *visit(disable_code_generation_pragma_c * symbol)  {
+    s4o_ptr->disable_output();
+    s4o_incl_ptr->disable_output();    
+    return NULL;
+}
     
     
 /********************************/
@@ -1626,6 +1640,15 @@
   delete vardecl;
   s4o.print("\n");
 
+  /* (A.3) Declare global prototypes in include file */
+  vardecl = new generate_c_vardecl_c(&s4o_incl,
+                                     generate_c_vardecl_c::globalprototype_vf,
+                                     generate_c_vardecl_c::global_vt,
+                                     symbol->configuration_name);
+  vardecl->print(symbol);
+  delete vardecl;
+  s4o_incl.print("\n");
+
   /* (B) Initialisation Function */
   /* (B.1) Ressources initialisation protos... */
   wanted_declaretype = initprotos_dt;
@@ -1755,20 +1778,24 @@
 
   private:
     /* The name of the resource curretnly being processed... */
+    symbol_c *current_configuration;
     symbol_c *current_resource_name;
     symbol_c *current_task_name;
     symbol_c *current_global_vars;
+    bool configuration_name;
     stage4out_c *s4o_ptr;
 
   public:
     generate_c_resources_c(stage4out_c *s4o_ptr, symbol_c *config_scope, symbol_c *resource_scope, unsigned long time)
       : generate_c_typedecl_c(s4o_ptr) {
+      current_configuration = config_scope;
       search_config_instance   = new search_var_instance_decl_c(config_scope);
       search_resource_instance = new search_var_instance_decl_c(resource_scope);
       common_ticktime = time;
       current_resource_name = NULL;
       current_task_name = NULL;
       current_global_vars = NULL;
+      configuration_name = false;
       generate_c_resources_c::s4o_ptr = s4o_ptr;
     };
 
@@ -1821,6 +1848,20 @@
       return NULL;
     }
 
+    /*************************/
+    /* B.1 - Common elements */
+    /*************************/
+    /*******************************************/
+    /* B 1.1 - Letters, digits and identifiers */
+    /*******************************************/
+    
+    void *visit(identifier_c *symbol) {
+        if (configuration_name)
+          s4o.print(symbol->value);
+        else
+          generate_c_base_c::visit(symbol);
+        return NULL;
+    }
 
     /********************/
     /* 2.1.6 - Pragmas  */
@@ -1848,9 +1889,13 @@
       return NULL;
     }
 
-/********************************/
-/* B 1.7 Configuration elements */
-/********************************/
+    /********************************/
+    /* B 1.7 Configuration elements */
+    /********************************/
+
+    void *visit(configuration_declaration_c *symbol) {
+      return symbol->configuration_name->accept(*this);
+    }
 
 /*
 RESOURCE resource_name ON resource_type_name
@@ -1893,7 +1938,12 @@
       
       s4o.print("extern unsigned long long common_ticktime__;\n\n");
 
-      s4o.print("#include \"accessor.h\"\n\n");
+      s4o.print("#include \"accessor.h\"\n");
+      s4o.print("#include \"");
+      configuration_name = true;
+      current_configuration->accept(*this);
+      configuration_name = false;
+      s4o.print(".h\"\n");
       s4o.print("#include \"POUS.h\"\n\n");
 
       /* (A.2) Global variables... */
@@ -2446,9 +2496,10 @@
             }
 
             symbol->configuration_name->accept(*this);
-
+            
             stage4out_c config_s4o(current_builddir, current_name, "c");
-            generate_c_config_c generate_c_config(&config_s4o);
+            stage4out_c config_incl_s4o(current_builddir, current_name, "h");
+            generate_c_config_c generate_c_config(&config_s4o, &config_incl_s4o);
             symbol->accept(generate_c_config);
 
             config_s4o.print("unsigned long long common_ticktime__ = ");
--- a/stage4/generate_c/generate_c_vardecl.cc	Fri Dec 23 15:17:45 2011 +0100
+++ b/stage4/generate_c/generate_c_vardecl.cc	Fri Dec 30 18:25:39 2011 +0100
@@ -811,7 +811,8 @@
                   localinit_vf,
                   init_vf,
                   constructorinit_vf,
-                  globalinit_vf
+                  globalinit_vf,
+                  globalprototype_vf,
                  } varformat_t;
 
 
@@ -1991,14 +1992,29 @@
 		  symbol->global_var_name->accept(*this);
 	    else
 		  symbol->location->accept(*this);
-	    s4o.print(",__INITIAL_VALUE(");
+	    s4o.print(",");
+	    s4o.print(INITIAL_VALUE);
+	    s4o.print("(");
 	    this->current_var_init_symbol->accept(*this);
 	    s4o.print(")");
 	    print_retain();
 	    s4o.print(")");
       }
       break;
-
+    
+    case globalprototype_vf:
+      s4o.print(s4o.indent_spaces);
+      s4o.print(DECLARE_GLOBAL_PROTOTYPE);
+	  s4o.print("(");
+	  this->current_var_type_symbol->accept(*this);
+	  s4o.print(",");
+	  if (symbol->global_var_name != NULL)
+		symbol->global_var_name->accept(*this);
+	  else
+		symbol->location->accept(*this);
+	  s4o.print(")\n");
+      break;
+    
     default:
       ERROR;
   } /* switch() */
@@ -2052,7 +2068,9 @@
           this->current_var_type_symbol->accept(*this);
           s4o.print(",");
           list->elements[i]->accept(*this);
-          s4o.print(",__INITIAL_VALUE(");
+          s4o.print(",");
+          s4o.print(INITIAL_VALUE);
+          s4o.print("(");
           this->current_var_init_symbol->accept(*this);
           s4o.print(")");
           print_retain();
@@ -2084,6 +2102,18 @@
       }
       break;
 
+    case globalprototype_vf:
+      for(int i = 0; i < list->n; i++) {
+        s4o.print(s4o.indent_spaces);
+        s4o.print(DECLARE_GLOBAL_PROTOTYPE);
+        s4o.print("(");
+        this->current_var_type_symbol->accept(*this);
+        s4o.print(",");
+        list->elements[i]->accept(*this);
+        s4o.print(")\n");
+      }
+      break;
+
     default:
       ERROR; /* not supported, and not needed either... */
   }