Adding support for generating code for variable in step action association instead of action
authorlaurent
Wed, 02 Jun 2010 18:21:15 +0200
changeset 244 3a478229db8b
parent 242 0036357a990f
child 245 799466384604
Adding support for generating code for variable in step action association instead of action
stage4/generate_c/generate_c.cc
stage4/generate_c/generate_c_sfc.cc
stage4/generate_c/generate_c_sfcdecl.cc
--- a/stage4/generate_c/generate_c.cc	Thu May 20 23:36:53 2010 +0200
+++ b/stage4/generate_c/generate_c.cc	Wed Jun 02 18:21:15 2010 +0200
@@ -674,11 +674,11 @@
   delete vardecl;
   
   /* (A.4) Generate private internal variables for SFC */
-  sfcdecl = new generate_c_sfcdecl_c(&s4o_incl, generate_c_sfcdecl_c::sfcdecl_sd);
-  sfcdecl->print(symbol->fblock_body);
+  sfcdecl = new generate_c_sfcdecl_c(&s4o_incl, symbol);
+  sfcdecl->generate(symbol->fblock_body, generate_c_sfcdecl_c::sfcdecl_sd);
   delete sfcdecl;
   s4o_incl.print("\n");
-  
+
   /* (A.5) Function Block data structure type name. */
   s4o_incl.indent_left();
   s4o_incl.print("} ");
@@ -720,25 +720,23 @@
   vardecl->print(symbol->var_declarations, NULL, FB_FUNCTION_PARAM"->");
   delete vardecl;
   s4o.print("\n");
+
+  sfcdecl = new generate_c_sfcdecl_c(&s4o, symbol, FB_FUNCTION_PARAM"->");
+
   /* (B.3) Generate private internal variables for SFC */
-  sfcdecl = new generate_c_sfcdecl_c(&s4o, generate_c_sfcdecl_c::sfcinit_sd);
-  sfcdecl->print(symbol->fblock_body, FB_FUNCTION_PARAM"->");
-  delete sfcdecl;
+  sfcdecl->generate(symbol->fblock_body, generate_c_sfcdecl_c::sfcinit_sd);
+
   s4o.indent_left();
   s4o.print(s4o.indent_spaces + "}\n\n");
 
   
   /* (C) Function with FB body */
   /* (C.1) Step definitions */
-  sfcdecl = new generate_c_sfcdecl_c(&s4o, generate_c_sfcdecl_c::stepdef_sd);
-  sfcdecl->print(symbol->fblock_body);
-  delete sfcdecl;
+  sfcdecl->generate(symbol->fblock_body, generate_c_sfcdecl_c::stepdef_sd);
   
   /* (C.2) Action definitions */
-  sfcdecl = new generate_c_sfcdecl_c(&s4o, generate_c_sfcdecl_c::actiondef_sd);
-  sfcdecl->print(symbol->fblock_body);
-  delete sfcdecl;
-  
+  sfcdecl->generate(symbol->fblock_body, generate_c_sfcdecl_c::actiondef_sd);
+
   /* (C.3) Function declaration */
   s4o.print("// Code part\n");
   /* function interface */
@@ -798,13 +796,11 @@
   s4o.print(s4o.indent_spaces + "() \n\n");
 
   /* (C.6) Step undefinitions */
-  sfcdecl = new generate_c_sfcdecl_c(&s4o, generate_c_sfcdecl_c::stepundef_sd);
-  sfcdecl->print(symbol->fblock_body);
-  delete sfcdecl;
-  
+  sfcdecl->generate(symbol->fblock_body, generate_c_sfcdecl_c::stepundef_sd);
+
   /* (C.7) Action undefinitions */
-  sfcdecl = new generate_c_sfcdecl_c(&s4o, generate_c_sfcdecl_c::actionundef_sd);
-  sfcdecl->print(symbol->fblock_body);
+  sfcdecl->generate(symbol->fblock_body, generate_c_sfcdecl_c::actionundef_sd);
+
   delete sfcdecl;
 
   s4o.indent_left();
@@ -864,10 +860,12 @@
                 generate_c_vardecl_c::external_vt);
   vardecl->print(symbol->var_declarations);
   delete vardecl;
+
   /* (A.4) Generate private internal variables for SFC */
-  sfcdecl = new generate_c_sfcdecl_c(&s4o_incl, generate_c_sfcdecl_c::sfcdecl_sd);
-  sfcdecl->print(symbol->function_block_body);
+  sfcdecl = new generate_c_sfcdecl_c(&s4o_incl, symbol);
+  sfcdecl->generate(symbol->function_block_body, generate_c_sfcdecl_c::sfcdecl_sd);
   delete sfcdecl;
+  s4o_incl.print("\n");
   
   /* (A.5) Program data structure type name. */
   s4o_incl.indent_left();
@@ -908,24 +906,21 @@
   vardecl->print(symbol->var_declarations, NULL,  FB_FUNCTION_PARAM"->");
   delete vardecl;
   s4o.print("\n");
+
+  sfcdecl = new generate_c_sfcdecl_c(&s4o, symbol, FB_FUNCTION_PARAM"->");
+
   /* (B.3) Generate private internal variables for SFC */
-  sfcdecl = new generate_c_sfcdecl_c(&s4o, generate_c_sfcdecl_c::sfcinit_sd);
-  sfcdecl->print(symbol->function_block_body,FB_FUNCTION_PARAM"->");
-  delete sfcdecl;
+  sfcdecl->generate(symbol->function_block_body, generate_c_sfcdecl_c::sfcinit_sd);
 
   s4o.indent_left();
   s4o.print(s4o.indent_spaces + "}\n\n");
 
   /* (C) Function with PROGRAM body */
   /* (C.1) Step definitions */
-  sfcdecl = new generate_c_sfcdecl_c(&s4o, generate_c_sfcdecl_c::stepdef_sd);
-  sfcdecl->print(symbol->function_block_body);
-  delete sfcdecl;
+  sfcdecl->generate(symbol->function_block_body, generate_c_sfcdecl_c::stepdef_sd);
   
   /* (C.2) Action definitions */
-  sfcdecl = new generate_c_sfcdecl_c(&s4o, generate_c_sfcdecl_c::actiondef_sd);
-  sfcdecl->print(symbol->function_block_body);
-  delete sfcdecl;
+  sfcdecl->generate(symbol->function_block_body, generate_c_sfcdecl_c::actiondef_sd);
 
   /* (C.3) Function declaration */
   s4o.print("// Code part\n");
@@ -961,15 +956,13 @@
   s4o.print(s4o.indent_spaces + "() \n\n");
 
   /* (C.6) Step undefinitions */
-  sfcdecl = new generate_c_sfcdecl_c(&s4o, generate_c_sfcdecl_c::stepundef_sd);
-  sfcdecl->print(symbol->function_block_body);
+  sfcdecl->generate(symbol->function_block_body, generate_c_sfcdecl_c::stepundef_sd);
+  
+  /* (C.7) Action undefinitions */
+  sfcdecl->generate(symbol->function_block_body, generate_c_sfcdecl_c::actionundef_sd);
+  
   delete sfcdecl;
-  
-  /* (C.7) Action undefinitions */
-  sfcdecl = new generate_c_sfcdecl_c(&s4o, generate_c_sfcdecl_c::actionundef_sd);
-  sfcdecl->print(symbol->function_block_body);
-  delete sfcdecl;
-  
+
   s4o.indent_left();
   s4o.print("\n\n\n\n");
 
--- a/stage4/generate_c/generate_c_sfc.cc	Thu May 20 23:36:53 2010 +0200
+++ b/stage4/generate_c/generate_c_sfc.cc	Wed Jun 02 18:21:15 2010 +0200
@@ -38,10 +38,6 @@
   int index;
 } TRANSITION;
 
-
-
-
-
 /***********************************************************************/
 /***********************************************************************/
 /***********************************************************************/
@@ -64,7 +60,6 @@
     generate_c_il_c *generate_c_il;
     generate_c_st_c *generate_c_st;
     generate_c_SFC_IL_ST_c *generate_c_code;
-    search_var_instance_decl_c *search_var_instance_decl;
     
     int transition_number;
     std::list<TRANSITION> transition_list;
@@ -80,7 +75,6 @@
       generate_c_il = new generate_c_il_c(s4o_ptr, name, scope, variable_prefix);
       generate_c_st = new generate_c_st_c(s4o_ptr, name, scope, variable_prefix);
       generate_c_code = new generate_c_SFC_IL_ST_c(s4o_ptr, name, scope, variable_prefix);
-      search_var_instance_decl = new search_var_instance_decl_c(scope);
       this->set_variable_prefix(variable_prefix);
     }
     
@@ -89,7 +83,6 @@
       delete generate_c_il;
       delete generate_c_st;
       delete generate_c_code;
-      delete search_var_instance_decl;
     }
 
     void reset_transition_number(void) {transition_number = 0;}
@@ -131,9 +124,7 @@
     }      
 
     void print_transition_number(void) {
-      char str[10];
-      sprintf(str, "%d", transition_number);
-      s4o.print(str);
+      s4o.print_integer(transition_number);
     }
 
     void print_reset_step(symbol_c *step_name) {
@@ -154,13 +145,6 @@
       s4o.print(" = __time_to_timespec(1, 0, 0, 0, 0, 0);\n");
     }
     
-    bool is_variable(symbol_c *symbol) {
-      /* we try to find the variable instance declaration, to determine its type... */
-      symbol_c *var_decl = search_var_instance_decl->get_decl(symbol);
-      
-      return var_decl != NULL;
-    }
-
 /*********************************************/
 /* B.1.6  Sequential function chart elements */
 /*********************************************/
@@ -564,29 +548,17 @@
               s4o.print(" = 1;\n");  
             }
             if (strcmp(qualifier, "S") == 0) {
-              if (is_variable(current_action)) {
-                print_variable_prefix();
-                current_action->accept(*this);
-              }
-              else
-                print_action_argument(current_action, "set");
+              print_action_argument(current_action, "set");
               s4o.print(" = 1;\n");
             }
             if (strcmp(qualifier, "R") == 0) {
-              if (is_variable(current_action)) {
-                print_variable_prefix();
-                current_action->accept(*this);
-                s4o.print(" = 0;\n");
-              }
-              else {
-                print_action_argument(current_action, "reset");
-                s4o.print(" = 1;\n");
-              }
+              print_action_argument(current_action, "reset");
+              s4o.print(" = 1;\n");
             }
             if (strcmp(qualifier, "SD") == 0 || strcmp(qualifier, "DS") == 0 || 
                 strcmp(qualifier, "SL") == 0) {
               if (strcmp(qualifier, "SL") == 0) {
-                print_action_argument(current_action, "reset_remaining_time");  
+                print_action_argument(current_action, "reset_remaining_time");
               }
               else {
                 print_action_argument(current_action, "set_remaining_time");
@@ -623,7 +595,6 @@
 }; /* generate_c_sfc_actiondecl_c */
  
  
- 
 /***********************************************************************/
 /***********************************************************************/
 /***********************************************************************/
@@ -632,17 +603,30 @@
 class generate_c_sfc_c: public generate_c_typedecl_c {
   
   private:
+    std::list<VARIABLE> variable_list;
+
     generate_c_sfc_elements_c *generate_c_sfc_elements;
+    search_var_instance_decl_c *search_var_instance_decl;
     
   public:
     generate_c_sfc_c(stage4out_c *s4o_ptr, symbol_c *name, symbol_c *scope, const char *variable_prefix = NULL)
     : generate_c_typedecl_c(s4o_ptr) {
       generate_c_sfc_elements = new generate_c_sfc_elements_c(s4o_ptr, name, scope, variable_prefix);
+      search_var_instance_decl = new search_var_instance_decl_c(scope);
       this->set_variable_prefix(variable_prefix);
     }
   
     virtual ~generate_c_sfc_c(void) {
+      variable_list.clear();
       delete generate_c_sfc_elements;
+      delete search_var_instance_decl;
+    }
+
+    bool is_variable(symbol_c *symbol) {
+      /* we try to find the variable instance declaration, to determine if symbol is variable... */
+      symbol_c *var_decl = search_var_instance_decl->get_decl(symbol);
+
+      return var_decl != NULL;
     }
 
 /*********************************************/
@@ -654,6 +638,7 @@
       
       generate_c_sfc_elements->reset_transition_number();
       for(i = 0; i < symbol->n; i++) {
+        symbol->elements[i]->accept(*this);
         generate_c_sfc_elements->generate(symbol->elements[i], generate_c_sfc_elements_c::transitionlist_sg);
       }
       
@@ -850,6 +835,38 @@
       
       /* generate action execution */
       s4o.print(s4o.indent_spaces + "// Actions execution\n");
+      {
+        std::list<VARIABLE>::iterator pt;
+        for(pt = variable_list.begin(); pt != variable_list.end(); pt++) {
+          symbol_c *var_decl = search_var_instance_decl->get_decl(pt->symbol);
+          if (var_decl != NULL) {
+            unsigned int vartype = search_var_instance_decl->get_vartype();
+
+            s4o.print(s4o.indent_spaces);
+            if (vartype == search_var_instance_decl_c::external_vt) {
+          	  s4o.print(SET_EXTERNAL);
+          	  s4o.print("(");
+          	  pt->symbol->accept(*this);
+              s4o.print(",");
+            }
+          	else {
+          	  if (vartype == search_var_instance_decl_c::located_vt)
+          	    s4o.print(SET_LOCATED);
+          	  else
+          	    s4o.print(SET_VAR);
+              s4o.print("(");
+          	}
+            print_variable_prefix();
+            pt->symbol->accept(*this);
+            s4o.print(",");
+            print_variable_prefix();
+            s4o.print("__action_list[");
+            s4o.print(SFC_STEP_ACTION_PREFIX);
+            pt->symbol->accept(*this);
+            s4o.print("].state);\n");
+          }
+        }
+      }
       for(i = 0; i < symbol->n; i++) {
         generate_c_sfc_elements->generate(symbol->elements[i], generate_c_sfc_elements_c::actionbody_sg);
       }
@@ -858,6 +875,41 @@
       return NULL;
     }
     
+    void *visit(initial_step_c *symbol) {
+      symbol->action_association_list->accept(*this);
+      return NULL;
+    }
+
+    void *visit(step_c *symbol) {
+      symbol->action_association_list->accept(*this);
+      return NULL;
+    }
+
+    void *visit(action_association_c *symbol) {
+      symbol_c *var_decl = search_var_instance_decl->get_decl(symbol->action_name);
+
+      if (var_decl != NULL) {
+        std::list<VARIABLE>::iterator pt;
+        for(pt = variable_list.begin(); pt != variable_list.end(); pt++) {
+          if (!compare_identifiers(pt->symbol, symbol->action_name))
+            return NULL;
+        }
+        VARIABLE *variable;
+        variable = new VARIABLE;
+        variable->symbol = (identifier_c*)(symbol->action_name);
+        variable_list.push_back(*variable);
+      }
+      return NULL;
+    }
+
+    void *visit(transition_c *symbol) {
+      return NULL;
+    }
+
+    void *visit(action_c *symbol) {
+      return NULL;
+    }
+
     void generate(sequential_function_chart_c *sfc) {
       sfc->accept(*this);
     }
--- a/stage4/generate_c/generate_c_sfcdecl.cc	Thu May 20 23:36:53 2010 +0200
+++ b/stage4/generate_c/generate_c_sfcdecl.cc	Wed Jun 02 18:21:15 2010 +0200
@@ -31,8 +31,10 @@
  * code.
  */
 
-
-
+typedef struct
+{
+  identifier_c *symbol;
+} VARIABLE;
 
 /***********************************************************************/
 /***********************************************************************/
@@ -55,25 +57,32 @@
        } sfcdeclaration_t;
   
   private:
-    char step_number;
-    char action_number;
-    char transition_number;
+    int step_number;
+    int action_number;
+    int transition_number;
+    std::list<VARIABLE> variable_list;
     
     sfcdeclaration_t wanted_sfcdeclaration;
+
+    search_var_instance_decl_c *search_var_instance_decl;
     
   public:
-    generate_c_sfcdecl_c(stage4out_c *s4o_ptr, sfcdeclaration_t sfcdeclaration)
+    generate_c_sfcdecl_c(stage4out_c *s4o_ptr, symbol_c *scope, const char *variable_prefix = NULL)
     : generate_c_typedecl_c(s4o_ptr) {
-      wanted_sfcdeclaration = sfcdeclaration;
-    }
-    ~generate_c_sfcdecl_c(void) {}
-    
-    void print(symbol_c *symbol, const char *variable_prefix = NULL) {
       this->set_variable_prefix(variable_prefix);
-      
+      search_var_instance_decl = new search_var_instance_decl_c(scope);
+    }
+    ~generate_c_sfcdecl_c(void) {
+      variable_list.clear();
+      delete search_var_instance_decl;
+    }
+    
+    void generate(symbol_c *symbol, sfcdeclaration_t declaration_type) {
+      wanted_sfcdeclaration = declaration_type;
+
       symbol->accept(*this);
     }
-    
+
 /*********************************************/
 /* B.1.6  Sequential function chart elements */
 /*********************************************/
@@ -143,7 +152,7 @@
           for(int i = 0; i < symbol->n; i++)
             symbol->elements[i]->accept(*this);
           
-          /* steps table count */
+          /* actions table count */
           wanted_sfcdeclaration = actioncount_sd;
           for(int i = 0; i < symbol->n; i++)
             symbol->elements[i]->accept(*this);
@@ -192,6 +201,18 @@
           break;
         case actiondef_sd:
           s4o.print("// Actions definitions\n");
+          {
+            std::list<VARIABLE>::iterator pt;
+            for(pt = variable_list.begin(); pt != variable_list.end(); pt++) {
+              s4o.print("#define ");
+              s4o.print(SFC_STEP_ACTION_PREFIX);
+              pt->symbol->accept(*this);
+              s4o.print(" ");
+              s4o.print_integer(action_number);
+              s4o.print("\n");
+              action_number++;
+            }
+          }
           for(int i = 0; i < symbol->n; i++)
             symbol->elements[i]->accept(*this);
           s4o.print("\n");
@@ -204,6 +225,15 @@
           break;
         case actionundef_sd:
           s4o.print("// Actions undefinitions\n");
+          {
+            std::list<VARIABLE>::iterator pt;
+            for(pt = variable_list.begin(); pt != variable_list.end(); pt++) {
+              s4o.print("#undef ");
+              s4o.print(SFC_STEP_ACTION_PREFIX);
+              pt->symbol->accept(*this);
+              s4o.print("\n");
+            }
+          }
           for(int i = 0; i < symbol->n; i++)
             symbol->elements[i]->accept(*this);
           s4o.print("\n");
@@ -216,8 +246,12 @@
     
     void *visit(initial_step_c *symbol) {
       switch (wanted_sfcdeclaration) {
+        case actioncount_sd:
+          symbol->action_association_list->accept(*this);
+          break;
+        case sfcdecl_sd:
+          symbol->action_association_list->accept(*this);
         case stepcount_sd:
-        case sfcdecl_sd:
           step_number++;
           break;
         case sfcinit_sd:
@@ -253,8 +287,12 @@
     
     void *visit(step_c *symbol) {
       switch (wanted_sfcdeclaration) {
+        case actioncount_sd:
+          symbol->action_association_list->accept(*this);
+          break;
+        case sfcdecl_sd:
+          symbol->action_association_list->accept(*this);
         case stepcount_sd:
-        case sfcdecl_sd:
         case sfcinit_sd:
           step_number++;
           break;
@@ -279,6 +317,25 @@
       return NULL;
     }
 
+    void *visit(action_association_c *symbol) {
+      /* we try to find the variable instance declaration, to determine if symbol is variable... */
+      symbol_c *var_decl = search_var_instance_decl->get_decl(symbol->action_name);
+
+      if (var_decl != NULL) {
+    	std::list<VARIABLE>::iterator pt;
+        for(pt = variable_list.begin(); pt != variable_list.end(); pt++) {
+          if (!compare_identifiers(pt->symbol, symbol->action_name))
+            return NULL;
+        }
+        VARIABLE *variable;
+        variable = new VARIABLE;
+        variable->symbol = (identifier_c*)(symbol->action_name);
+        variable_list.push_back(*variable);
+        action_number++;
+      }
+      return NULL;
+    }
+
     void *visit(transition_c *symbol) {
       switch (wanted_sfcdeclaration) {
         case sfcdecl_sd: