Change order of SET_xxx() macros. (this will allow me to simplify the print_setter() methods later on)
authormjsousa
Sat, 29 Mar 2014 22:46:09 +0000
changeset 885 b2604fc6d25c
parent 880 599e88d12f9a
child 886 111414d79ecd
Change order of SET_xxx() macros. (this will allow me to simplify the print_setter() methods later on)
lib/accessor.h
lib/rtc.txt
lib/timer.txt
stage4/generate_c/generate_c.cc
stage4/generate_c/generate_c_il.cc
stage4/generate_c/generate_c_inlinefcall.cc
stage4/generate_c/generate_c_sfc.cc
stage4/generate_c/generate_c_sfcdecl.cc
stage4/generate_c/generate_c_st.cc
stage4/generate_c/generate_c_vardecl.cc
--- a/lib/accessor.h	Wed Mar 19 12:13:43 2014 +0000
+++ b/lib/accessor.h	Sat Mar 29 22:46:09 2014 +0000
@@ -113,15 +113,15 @@
 
 
 // variable setting macros
-#define __SET_VAR(prefix, name, new_value, ...)\
-	if (!(prefix name.flags & __IEC_FORCE_FLAG)) prefix name.value __VA_ARGS__ = new_value
-#define __SET_EXTERNAL(prefix, name, new_value, ...)\
+#define __SET_VAR(prefix, name, suffix, new_value)\
+	if (!(prefix name.flags & __IEC_FORCE_FLAG)) prefix name.value suffix = new_value
+#define __SET_EXTERNAL(prefix, name, suffix, new_value)\
 	{extern IEC_BYTE __IS_GLOBAL_##name##_FORCED();\
     if (!(prefix name.flags & __IEC_FORCE_FLAG || __IS_GLOBAL_##name##_FORCED()))\
-		(*(prefix name.value)) __VA_ARGS__ = new_value;}
-#define __SET_EXTERNAL_FB(prefix, name, new_value, ...)\
-	__SET_VAR((*(prefix name)), __VA_ARGS__, new_value)
-#define __SET_LOCATED(prefix, name, new_value, ...)\
-	if (!(prefix name.flags & __IEC_FORCE_FLAG)) *(prefix name.value) __VA_ARGS__ = new_value
+		(*(prefix name.value)) suffix = new_value;}
+#define __SET_EXTERNAL_FB(prefix, name, suffix, new_value)\
+	__SET_VAR((*(prefix name)), suffix, new_value)
+#define __SET_LOCATED(prefix, name, suffix, new_value)\
+	if (!(prefix name.flags & __IEC_FORCE_FLAG)) *(prefix name.value) suffix = new_value
 
 #endif //__ACCESSOR_H
--- a/lib/rtc.txt	Wed Mar 19 12:13:43 2014 +0000
+++ b/lib/rtc.txt	Sat Mar 29 22:46:09 2014 +0000
@@ -29,7 +29,7 @@
      CURRENT_TIME : DT;
    END_VAR
 
-   {__SET_VAR(data__->,CURRENT_TIME,__CURRENT_TIME)}
+   {__SET_VAR(data__->,CURRENT_TIME,,__CURRENT_TIME)}
 
    IF IN
    THEN
--- a/lib/timer.txt	Wed Mar 19 12:13:43 2014 +0000
+++ b/lib/timer.txt	Sat Mar 29 22:46:09 2014 +0000
@@ -90,7 +90,7 @@
     CURRENT_TIME, START_TIME : TIME;
   END_VAR
 
-  {__SET_VAR(data__->,CURRENT_TIME,__CURRENT_TIME)}
+  {__SET_VAR(data__->,CURRENT_TIME,,__CURRENT_TIME)}
 
   IF ((STATE = 0) AND NOT(PREV_IN) AND IN)   (* found rising edge on IN *)
   THEN
@@ -171,7 +171,7 @@
     CURRENT_TIME, START_TIME : TIME;
   END_VAR
 
-  {__SET_VAR(data__->,CURRENT_TIME,__CURRENT_TIME)}
+  {__SET_VAR(data__->,CURRENT_TIME,,__CURRENT_TIME)}
 
   IF ((STATE = 0) AND NOT(PREV_IN) AND IN)   (* found rising edge on IN *)
   THEN
@@ -255,7 +255,7 @@
     CURRENT_TIME, START_TIME : TIME;
   END_VAR
 
-  {__SET_VAR(data__->,CURRENT_TIME,__CURRENT_TIME)}
+  {__SET_VAR(data__->,CURRENT_TIME,,__CURRENT_TIME)}
 
   IF ((STATE = 0) AND PREV_IN AND NOT(IN))   (* found falling edge on IN *)
   THEN
--- a/stage4/generate_c/generate_c.cc	Wed Mar 19 12:13:43 2014 +0000
+++ b/stage4/generate_c/generate_c.cc	Sat Mar 29 22:46:09 2014 +0000
@@ -380,6 +380,131 @@
 /***********************************************************************/
 /***********************************************************************/
 /***********************************************************************/
+/* A helper class that analyses if the datatype of a variable is 'complex'. */
+/* 'complex' means that it is either a strcuture or an array!               */
+class print_getter_c: public search_visitor_c {
+  private:
+    static print_getter_c *singleton_;
+
+  public:
+    print_getter_c(void) {};
+    
+    static bool is_complex_type(symbol_c *symbol) {
+      if (NULL == symbol) ERROR;
+      if (!get_datatype_info_c::is_type_valid     (symbol->datatype)) return false;
+      return (   get_datatype_info_c::is_structure(symbol->datatype) 
+              || get_datatype_info_c::is_array    (symbol->datatype) 
+             );
+    }
+
+    
+  private:
+    symbol_c *last_fb, *first_non_fb_identifier;
+
+  public:  
+    /* returns the first element (from left to right) in a structured variable that is not a FB, i.e. is either a structure or an array! */
+    /* eg:
+     *      fb1.fb2.fb3.real       returns ??????
+     *      fb1.fb2.struct1.real   returns struct1
+     *      struct1.real           returns struct1
+     */
+    static symbol_c *find_first_nonfb(symbol_c *symbol) {
+      if (NULL == singleton_)       singleton_ = new print_getter_c();
+      if (NULL == singleton_)       ERROR;
+      if (NULL == symbol)           ERROR;
+      
+      singleton_->last_fb                 = NULL;
+      singleton_->first_non_fb_identifier = NULL;
+      return (symbol_c *)symbol->accept(*singleton_);
+    }
+    
+    /* returns true if a strcutured variable (e.g. fb1.fb2.strcut1.real) contains a structure or array */
+    /* eg:
+     *      fb1.fb2.fb3.real       returns FALSE
+     *      fb1.fb2.struct1.real   returns TRUE
+     *      struct1.real           returns TRUE
+     */
+    static bool contains_complex_type(symbol_c *symbol) {
+      if (NULL == symbol) ERROR;
+      if (!get_datatype_info_c::is_type_valid(symbol->datatype)) ERROR;
+      
+      symbol_c *first_non_fb = (symbol_c *)find_first_nonfb(symbol);
+      return is_complex_type(first_non_fb->datatype);
+    }
+    
+    
+    /* returns the datatype of the variable returned by find_first_nonfb() */
+    /* eg:
+     *      fb1.fb2.fb3.real       returns ??????
+     *      fb1.fb2.struct1.real   returns datatype of struct1
+     *      struct1.real           returns datatype of struct1
+     */
+    static search_var_instance_decl_c::vt_t first_nonfb_vardecltype(symbol_c *symbol, symbol_c *scope) {
+      if (NULL == symbol) ERROR;
+      if (!get_datatype_info_c::is_type_valid(symbol->datatype)) ERROR;
+      
+      symbol_c *first_non_fb = (symbol_c *)find_first_nonfb(symbol);
+      if (NULL != singleton_->last_fb) {
+        scope = singleton_->last_fb->datatype;
+        symbol = singleton_->first_non_fb_identifier;
+      }
+      
+      search_var_instance_decl_c search_var_instance_decl(scope);
+      
+      return search_var_instance_decl.get_vartype(symbol);
+    }
+    
+    
+    /*********************/
+    /* B 1.4 - Variables */
+    /*********************/
+    void *visit(symbolic_variable_c *symbol) {
+      if (!get_datatype_info_c::is_type_valid    (symbol->datatype)) ERROR;
+      if (!get_datatype_info_c::is_function_block(symbol->datatype)) {
+         first_non_fb_identifier = symbol; 
+         return (void *)symbol;
+      }
+      last_fb = symbol;
+      return NULL;
+    }
+    
+    /*************************************/
+    /* B.1.4.2   Multi-element Variables */
+    /*************************************/
+    
+    // SYM_REF2(structured_variable_c, record_variable, field_selector)
+    void *visit(structured_variable_c *symbol) {
+      symbol_c *res = (symbol_c *)symbol->record_variable->accept(*this);
+      if (NULL != res) return res;
+      
+      if (!get_datatype_info_c::is_type_valid    (symbol->datatype)) ERROR;
+      if (!get_datatype_info_c::is_function_block(symbol->datatype)) {
+         first_non_fb_identifier = symbol->field_selector; 
+         return (void *)symbol;
+      }
+
+      last_fb = symbol;      
+      return NULL;      
+    }
+    
+    /*  subscripted_variable '[' subscript_list ']' */
+    //SYM_REF2(array_variable_c, subscripted_variable, subscript_list)
+    void *visit(array_variable_c *symbol) {
+      void *res = symbol->subscripted_variable->accept(*this);
+      if (NULL != res) return res;
+      return (void *)symbol;      
+    }
+
+    
+};
+
+print_getter_c *print_getter_c::singleton_ = NULL;
+
+
+
+
+
+
 
 /* A helper class that analyses if the datatype of a variable is 'complex'. */
 /* 'complex' means that it is either a strcuture or an array!               */
@@ -1567,7 +1692,7 @@
   s4o.print(SET_VAR);
   s4o.print("(");
   s4o.print(FB_FUNCTION_PARAM);
-  s4o.print("->,ENO,__BOOL_LITERAL(FALSE));\n");
+  s4o.print("->,ENO,,__BOOL_LITERAL(FALSE));\n");
   s4o.print(s4o.indent_spaces + "return;\n");
   s4o.indent_left();
   s4o.print(s4o.indent_spaces + "}\n");
@@ -1577,7 +1702,7 @@
   s4o.print(SET_VAR);
   s4o.print("(");
   s4o.print(FB_FUNCTION_PARAM);
-  s4o.print("->,ENO,__BOOL_LITERAL(TRUE));\n");
+  s4o.print("->,ENO,,__BOOL_LITERAL(TRUE));\n");
   s4o.indent_left();
   s4o.print(s4o.indent_spaces + "}\n");
 
@@ -2397,7 +2522,7 @@
             s4o.print(SET_VAR);
             s4o.print("(");
             current_task_name->accept(*this);
-            s4o.print("_R_TRIG.,CLK, *");
+            s4o.print("_R_TRIG.,CLK,, *");
             symbol->single_data_source->accept(*this);
             s4o.print(");}\n");
             s4o.print(s4o.indent_spaces + "R_TRIG");
--- a/stage4/generate_c/generate_c_il.cc	Wed Mar 19 12:13:43 2014 +0000
+++ b/stage4/generate_c/generate_c_il.cc	Sat Mar 29 22:46:09 2014 +0000
@@ -445,8 +445,8 @@
         wanted_variablegeneration = complextype_base_assignment_vg;
       else
         wanted_variablegeneration = assignment_vg;
-
       symbol->accept(*this);
+/*
       s4o.print(",");
       if (negative) {
         if (get_datatype_info_c::is_BOOL_compatible(this->current_operand->datatype))
@@ -464,6 +464,24 @@
       s4o.print(")");
       wanted_variablegeneration = expression_vg;
       return NULL;
+*/
+      s4o.print(",");
+      if (type_is_complex) {
+        wanted_variablegeneration = complextype_suffix_vg;
+        symbol->accept(*this);
+      }
+      s4o.print(",");
+      if (negative) {
+        if (get_datatype_info_c::is_BOOL_compatible(this->current_operand->datatype))
+          s4o.print("!");
+        else
+          s4o.print("~");
+      }
+      wanted_variablegeneration = expression_vg;
+      print_check_function(type, value, fb_value);
+      s4o.print(")");
+      wanted_variablegeneration = expression_vg;
+      return NULL;
     }
 
 public:
--- a/stage4/generate_c/generate_c_inlinefcall.cc	Wed Mar 19 12:13:43 2014 +0000
+++ b/stage4/generate_c/generate_c_inlinefcall.cc	Sat Mar 29 22:46:09 2014 +0000
@@ -281,7 +281,7 @@
       else
         s4o.print(SET_VAR);
       s4o.print("(,");
-
+/*
       wanted_variablegeneration = complextype_base_vg;
       symbol->accept(*this);
       s4o.print(",");
@@ -295,6 +295,20 @@
       s4o.print(")");
       wanted_variablegeneration = expression_vg;
       return NULL;
+*/
+      wanted_variablegeneration = complextype_base_vg;
+      symbol->accept(*this);
+      s4o.print(",");
+      if (analyse_variable_c::contains_complex_type(symbol)) {
+        wanted_variablegeneration = complextype_suffix_vg;
+        symbol->accept(*this);
+      }
+      s4o.print(",");
+      wanted_variablegeneration = expression_vg;
+      print_check_function(type, value, NULL, true);
+      s4o.print(")");
+      wanted_variablegeneration = expression_vg;
+      return NULL;
     }
 
     /*********************/
--- a/stage4/generate_c/generate_c_sfc.cc	Wed Mar 19 12:13:43 2014 +0000
+++ b/stage4/generate_c/generate_c_sfc.cc	Sat Mar 29 22:46:09 2014 +0000
@@ -136,7 +136,7 @@
       s4o.print(SET_VAR);
       s4o.print("(");
       print_step_argument(step_name, "state", true);
-      s4o.print(",0);\n");
+      s4o.print(",,0);\n");
     }
     
     void print_set_step(symbol_c *step_name) {
@@ -144,7 +144,7 @@
       s4o.print(SET_VAR);
       s4o.print("(");
       print_step_argument(step_name, "state", true);
-      s4o.print(",1);\n" + s4o.indent_spaces);
+      s4o.print(",,1);\n" + s4o.indent_spaces);
       print_step_argument(step_name, "elapsed_time");
       s4o.print(" = __time_to_timespec(1, 0, 0, 0, 0, 0);\n");
     }
@@ -299,7 +299,7 @@
           print_variable_prefix();
           s4o.print(",__transition_list[");
           print_transition_number();
-          s4o.print("],0);\n");
+          s4o.print("],,0);\n");
           s4o.indent_left();
           s4o.print(s4o.indent_spaces + "}\n");
           break;
@@ -358,7 +358,7 @@
               s4o.print("__");
             s4o.print("transition_list[");
             print_transition_number();
-            s4o.print("],");
+            s4o.print("],,");
             generate_c_il->print_implicit_variable_back();
             // generate_c_il->reset_default_variable_name(); // generate_c_il does not require his anymore
             s4o.print(");\n");
@@ -376,7 +376,7 @@
               s4o.print("__");
             s4o.print("transition_list[");
             print_transition_number();
-            s4o.print("],");
+            s4o.print("],,");
             symbol->transition_condition_st->accept(*generate_c_st);
             s4o.print(");\n");
           }
@@ -389,7 +389,7 @@
             print_variable_prefix();
             s4o.print(",__debug_transition_list[");
             print_transition_number();
-            s4o.print("],");
+            s4o.print("],,");
             s4o.print(GET_VAR);
             s4o.print("(");
             print_variable_prefix();
@@ -514,7 +514,7 @@
             s4o.print(SET_VAR);
             s4o.print("(");
             print_action_argument(symbol->action_name, "state", true);
-            s4o.print(",1);\n");
+            s4o.print(",,1);\n");
             s4o.indent_left();
             s4o.print(s4o.indent_spaces + "}");
           }
@@ -575,7 +575,7 @@
                 print_variable_prefix();
                 s4o.print(",");
                 current_action->accept(*this);
-                s4o.print(",1);\n");
+                s4o.print(",,1);\n");
                 s4o.indent_left();
                 s4o.print(s4o.indent_spaces + "}\n");
                 s4o.print(s4o.indent_spaces + "else if (active) {\n");
@@ -591,14 +591,14 @@
                 print_variable_prefix();
                 s4o.print(",");
                 current_action->accept(*this);
-                s4o.print(",0);\n");
+                s4o.print(",,0);\n");
               }
 
               else {
                 s4o.print(SET_VAR);
                 s4o.print("(");
                 print_action_argument(current_action, "state", true);
-                s4o.print(",1);\n");
+                s4o.print(",,1);\n");
               }
             }
             if (strcmp(qualifier, "S") == 0 || strcmp(qualifier, "SL") == 0) {
@@ -773,7 +773,7 @@
       s4o.print(SET_VAR);
       s4o.print("(");
       print_variable_prefix();
-      s4o.print(",__action_list[i].state,0);\n");
+      s4o.print(",__action_list[i].state,,0);\n");
       s4o.print(s4o.indent_spaces);
       print_variable_prefix();
       s4o.print("__action_list[i].set = 0;\n");
@@ -895,7 +895,7 @@
       s4o.print(SET_VAR);
       s4o.print("(");
       print_variable_prefix();
-      s4o.print(",__action_list[i].state,");
+      s4o.print(",__action_list[i].state,,");
       s4o.print(GET_VAR);
       s4o.print("(");
       print_variable_prefix();
@@ -932,7 +932,7 @@
             print_variable_prefix();
             s4o.print(",");
             pt->symbol->accept(*this);
-            s4o.print(",0);\n");
+            s4o.print(",,0);\n");
             s4o.indent_left();
             s4o.print(s4o.indent_spaces + "}\n");
             s4o.print(s4o.indent_spaces + "else if (");
@@ -953,7 +953,7 @@
             print_variable_prefix();
             s4o.print(",");
             pt->symbol->accept(*this);
-            s4o.print(",1);\n");
+            s4o.print(",,1);\n");
             s4o.indent_left();
             s4o.print(s4o.indent_spaces + "}\n");
           }
--- a/stage4/generate_c/generate_c_sfcdecl.cc	Wed Mar 19 12:13:43 2014 +0000
+++ b/stage4/generate_c/generate_c_sfcdecl.cc	Sat Mar 29 22:46:09 2014 +0000
@@ -252,7 +252,7 @@
           print_variable_prefix();
           s4o.print(",__step_list[");
           s4o.print(step_number);
-          s4o.print("].state,1);\n");
+          s4o.print("].state,,1);\n");
           step_number++;
           break;
         case stepdef_sd:
--- a/stage4/generate_c/generate_c_st.cc	Wed Mar 19 12:13:43 2014 +0000
+++ b/stage4/generate_c/generate_c_st.cc	Sat Mar 29 22:46:09 2014 +0000
@@ -213,6 +213,7 @@
   else
     wanted_variablegeneration = assignment_vg;
   
+/*
   symbol->accept(*this);
   s4o.print(",");
   wanted_variablegeneration = expression_vg;
@@ -225,6 +226,19 @@
   s4o.print(")");
   wanted_variablegeneration = expression_vg;
   return NULL;
+*/
+  symbol->accept(*this);
+  s4o.print(",");
+  if (type_is_complex) {
+    wanted_variablegeneration = complextype_suffix_vg;
+    symbol->accept(*this);
+  }
+  s4o.print(",");
+  wanted_variablegeneration = expression_vg;
+  print_check_function(type, value, fb_value);
+  s4o.print(")");
+  wanted_variablegeneration = expression_vg;
+  return NULL;
 }
 
 /********************************/
--- a/stage4/generate_c/generate_c_vardecl.cc	Wed Mar 19 12:13:43 2014 +0000
+++ b/stage4/generate_c/generate_c_vardecl.cc	Sat Mar 29 22:46:09 2014 +0000
@@ -169,7 +169,7 @@
         print_variable_prefix();
         s4o.print(",");
         symbol->elements[i]->accept(*this);
-        s4o.print(",temp);\n");
+        s4o.print(",,temp);\n");
       }
       return NULL;
     }
@@ -577,7 +577,7 @@
         print_variable_prefix();
         s4o.print(",");
         symbol->elements[i]->accept(*this);
-        s4o.print(",temp);\n");
+        s4o.print(",,temp);\n");
       }
       return NULL;
     }
@@ -1000,7 +1000,7 @@
               s4o.print("}\n");
             }
             else if (wanted_varformat == init_vf) {
-              s4o.print(",");
+              s4o.print(",,");
               this->current_var_init_symbol->accept(*this);
               s4o.print(");\n");
             }