Clean up code (remove parsing of integers in stage 4).
authorMario de Sousa <msousa@fe.up.pt>
Thu, 14 Jun 2012 12:00:19 +0100
changeset 594 c8092e909886
parent 593 412780374bd3
child 595 c41975a290ce
Clean up code (remove parsing of integers in stage 4).
stage3/array_range_check.cc
stage4/generate_c/generate_c.cc
stage4/generate_c/generate_c_base.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_typedecl.cc
stage4/generate_c/generate_c_vardecl.cc
stage4/stage4.cc
stage4/stage4.hh
--- a/stage3/array_range_check.cc	Wed Jun 13 19:51:26 2012 +0200
+++ b/stage3/array_range_check.cc	Thu Jun 14 12:00:19 2012 +0100
@@ -135,6 +135,8 @@
     if ( VALID_CVALUE(uint64, l->elements[i]) && VALID_CVALUE(uint64, dimension->upper_limit))
       if ( GET_CVALUE(uint64, l->elements[i])   >  GET_CVALUE(uint64, dimension->upper_limit))
       {STAGE3_ERROR(0, symbol, symbol, "Array access out of bounds."); continue;}
+      
+    /* TODO: what happens when one has a int64 cvalue, and another has a uint64 cvalue? */
   }
 }
 
--- a/stage4/generate_c/generate_c.cc	Wed Jun 13 19:51:26 2012 +0200
+++ b/stage4/generate_c/generate_c.cc	Thu Jun 14 12:00:19 2012 +0100
@@ -349,85 +349,75 @@
 #define MILLISECOND 1000000
 #define SECOND 1000 * MILLISECOND
 
-/* A helper class that knows how to generate code for both the IL and ST languages... */
-class calculate_time_c: public iterator_visitor_c {
-  private:
-    unsigned long long time;
-    float current_value;
+unsigned long long calculate_time(symbol_c *symbol) {
+  if (NULL == symbol) return 0;
   
-  public:
-    calculate_time_c(void){time = 0;};
-    
-    unsigned long long get_time(void) {return time;};
-
-    /* NOTE: we should really remove this function, and replace it with  extract_integer_value() (in absyntax_utils.h)
-     *       but right now I don't want to spend time checking if this change will introduce some conversion bug
-     *       since it returns a long long, and not a float!
-     */
-    void *get_integer_value(token_c *token) {
-      std::string str = "";
-      for (unsigned int i = 0; i < strlen(token->value); i++)
-        if (token->value[i] != '_')
-          str += token->value[i];
-      current_value = atof(str.c_str());
-      return NULL;
-    }
-
-    /* NOTE: this function is incomplete, as it should also be removing '_' inserted into the literal,
-     *       but we leave it for now.
-     *       In truth, we should really have an extract_real_value() in absyntax_util.h !!!
-     */
-    void *get_float_value(token_c *token) {
-      current_value = atof(token->value);
-      return NULL;
-    }
-
-/******************************/
-/* B 1.2.1 - Numeric Literals */
-/******************************/
-
-    void *visit(integer_c *symbol) {return get_integer_value(symbol);}
-    
-/************************/
-/* B 1.2.3.1 - Duration */
-/************************/
+  interval_c *interval = dynamic_cast<interval_c *>(symbol);
+  duration_c *duration = dynamic_cast<duration_c *>(symbol);
   
+  if ((NULL == interval) && (NULL == duration)) ERROR;
+
+  if (NULL != duration) {
     /* SYM_REF2(duration_c, neg, interval) */
-    void *visit(duration_c *symbol) {
-      if (symbol->neg != NULL)
-        {STAGE4_ERROR(symbol, symbol, "Negative TIME literals are not currently supported"); ERROR;}
-      symbol->interval->accept(*this);
-      return NULL;
-    }
-    
-    /* SYM_TOKEN(fixed_point_c) */
-    void *visit(fixed_point_c *symbol) {return get_float_value(symbol);}
-    
-    
+    if (duration->neg != NULL)
+      {STAGE4_ERROR(duration, duration, "Negative TIME literals are not currently supported"); ERROR;}
+    return calculate_time(duration->interval);
+  }
+
+  if (NULL != interval) {
     /* SYM_REF5(interval_c, days, hours, minutes, seconds, milliseconds) */
-    void *visit(interval_c *symbol) {
-      current_value = 0;
-      if (NULL != symbol->milliseconds) symbol->milliseconds->accept(*this);
-      time += (unsigned long long)(current_value * MILLISECOND);
+      unsigned long long int time_ull = 0; 
+      long double            time_ld  = 0;
+      /*
+      const unsigned long long int MILLISECOND = 1000000;
+      const unsigned long long int      SECOND = 1000 * MILLISECOND
+      */
+      
+      if (NULL != interval->milliseconds) {
+        if      (VALID_CVALUE( int64, interval->milliseconds) &&           GET_CVALUE( int64, interval->milliseconds) < 0) ERROR; // interval elements should always be positive!
+        if      (VALID_CVALUE( int64, interval->milliseconds)) time_ull += GET_CVALUE( int64, interval->milliseconds) * MILLISECOND;
+        else if (VALID_CVALUE(uint64, interval->milliseconds)) time_ull += GET_CVALUE(uint64, interval->milliseconds) * MILLISECOND;
+        else if (VALID_CVALUE(real64, interval->milliseconds)) time_ld  += GET_CVALUE(real64, interval->milliseconds) * MILLISECOND;
+        else ERROR; // if (NULL != interval->milliseconds) is true, then it must have a valid constant value!
+      }
    
-      current_value = 0;
-      if (NULL != symbol->seconds)      symbol->seconds->accept(*this);
-      time += (unsigned long long)(current_value * SECOND);
-   
-      current_value = 0;
-      if (NULL != symbol->minutes)      symbol->minutes->accept(*this);
-      time += (unsigned long long)(current_value * 60 * SECOND);
-   
-      current_value = 0;
-      if (NULL != symbol->hours)        symbol->hours->accept(*this);
-      time += (unsigned long long)(current_value * 60 * 60 * SECOND);
-   
-      current_value = 0;
-      if (NULL != symbol->days)         symbol->days->accept(*this);
-      time += (unsigned long long)(current_value * 60 * 60 * 24 * SECOND);
-   
-      return NULL;
-    }      
+      if (NULL != interval->seconds     ) {
+        if      (VALID_CVALUE( int64, interval->seconds     ) &&           GET_CVALUE( int64, interval->seconds     ) < 0) ERROR; // interval elements should always be positive!
+        if      (VALID_CVALUE( int64, interval->seconds     )) time_ull += GET_CVALUE( int64, interval->seconds     ) * SECOND;
+        else if (VALID_CVALUE(uint64, interval->seconds     )) time_ull += GET_CVALUE(uint64, interval->seconds     ) * SECOND;
+        else if (VALID_CVALUE(real64, interval->seconds     )) time_ld  += GET_CVALUE(real64, interval->seconds     ) * SECOND;
+        else ERROR; // if (NULL != interval->seconds) is true, then it must have a valid constant value!
+      }
+
+      if (NULL != interval->minutes     ) {
+        if      (VALID_CVALUE( int64, interval->minutes     ) &&           GET_CVALUE( int64, interval->minutes     ) < 0) ERROR; // interval elements should always be positive!
+        if      (VALID_CVALUE( int64, interval->minutes     )) time_ull += GET_CVALUE( int64, interval->minutes     ) * SECOND * 60;
+        else if (VALID_CVALUE(uint64, interval->minutes     )) time_ull += GET_CVALUE(uint64, interval->minutes     ) * SECOND * 60;
+        else if (VALID_CVALUE(real64, interval->minutes     )) time_ld  += GET_CVALUE(real64, interval->minutes     ) * SECOND * 60;
+        else ERROR; // if (NULL != interval->minutes) is true, then it must have a valid constant value!
+      }
+
+      if (NULL != interval->hours       ) {
+        if      (VALID_CVALUE( int64, interval->hours       ) &&           GET_CVALUE( int64, interval->hours       ) < 0) ERROR; // interval elements should always be positive!
+        if      (VALID_CVALUE( int64, interval->hours       )) time_ull += GET_CVALUE( int64, interval->hours       ) * SECOND * 60 * 60;
+        else if (VALID_CVALUE(uint64, interval->hours       )) time_ull += GET_CVALUE(uint64, interval->hours       ) * SECOND * 60 * 60;
+        else if (VALID_CVALUE(real64, interval->hours       )) time_ld  += GET_CVALUE(real64, interval->hours       ) * SECOND * 60 * 60;
+        else ERROR; // if (NULL != interval->hours) is true, then it must have a valid constant value!
+      }
+
+      if (NULL != interval->days        ) {
+        if      (VALID_CVALUE( int64, interval->days        ) &&           GET_CVALUE( int64, interval->days        ) < 0) ERROR; // interval elements should always be positive!
+        if      (VALID_CVALUE( int64, interval->days        )) time_ull += GET_CVALUE( int64, interval->days        ) * SECOND * 60 * 60 * 24;
+        else if (VALID_CVALUE(uint64, interval->days        )) time_ull += GET_CVALUE(uint64, interval->days        ) * SECOND * 60 * 60 * 24;
+        else if (VALID_CVALUE(real64, interval->days        )) time_ld  += GET_CVALUE(real64, interval->days        ) * SECOND * 60 * 60 * 24;
+        else ERROR; // if (NULL != interval->days) is true, then it must have a valid constant value!
+      }
+
+      time_ull += time_ld;
+      return time_ull;
+  };
+  ERROR; // should never reach this point!
+  return 0; // humour the compiler!
 };
 
 /***********************************************************************/
@@ -481,14 +471,9 @@
 /*  TASK task_name task_initialization */
 //SYM_REF2(task_configuration_c, task_name, task_initialization)  
     void *visit(task_initialization_c *symbol) {
-      calculate_time_c calculate_time;
-      unsigned long long time = 0;
-      if (symbol->interval_data_source != NULL) {
-        symbol->interval_data_source->accept(calculate_time);
-        time = calculate_time.get_time();
-      }
-      if (time > 0)
-        update_ticktime(time);
+      unsigned long long time = calculate_time(symbol->interval_data_source);
+      if (time < 0)  ERROR;
+      else           update_ticktime(time);
       return NULL;
     }
 };    
@@ -800,7 +785,7 @@
           break;
         case arraydeclaration_im:
           s4o_incl.print("[");
-          s4o_incl.print_integer(symbol->dimension);
+          s4o_incl.print(symbol->dimension);
           s4o_incl.print("]");
         default:
           generate_c_typedecl_c::visit(symbol);
@@ -1815,7 +1800,7 @@
 
     declaretype_t wanted_declaretype;
 
-    unsigned long common_ticktime;
+    unsigned long long common_ticktime;
     
     const char *current_program_name;
 
@@ -2192,12 +2177,10 @@
             current_task_name->accept(*this);
             s4o.print(" = ");
             if (symbol->interval_data_source != NULL) {
-              calculate_time_c calculate_time;
-              symbol->interval_data_source->accept(calculate_time);
-              unsigned long time = calculate_time.get_time();
+              unsigned long long int time = calculate_time(symbol->interval_data_source);
               if (time != 0) {
                 s4o.print("!(tick % ");
-                s4o.print_integer((int)(time / common_ticktime));
+                s4o.print(time / common_ticktime);
                 s4o.print(")");
               }
               else
--- a/stage4/generate_c/generate_c_base.cc	Wed Jun 13 19:51:26 2012 +0200
+++ b/stage4/generate_c/generate_c_base.cc	Thu Jun 14 12:00:19 2012 +0100
@@ -385,7 +385,7 @@
 
       str += '"';
       s4o.print("__STRING_LITERAL(");
-      s4o.print_integer(count); 
+      s4o.print(count); 
       s4o.print(",");
       s4o.print(str);
       s4o.print(")");
--- a/stage4/generate_c/generate_c_il.cc	Wed Jun 13 19:51:26 2012 +0200
+++ b/stage4/generate_c/generate_c_il.cc	Thu Jun 14 12:00:19 2012 +0100
@@ -971,7 +971,7 @@
       print_function_parameter_data_types_c overloaded_func_suf(&s4o);
       f_decl->accept(overloaded_func_suf);
     }
-    s4o.print_integer(fcall_number);
+    s4o.print(fcall_number);
   }
   else {
     if (function_name != NULL) {
@@ -1379,7 +1379,7 @@
       print_function_parameter_data_types_c overloaded_func_suf(&s4o);
       f_decl->accept(overloaded_func_suf);
     }
-    s4o.print_integer(fcall_number);
+    s4o.print(fcall_number);
   }
   else {
     if (function_name != NULL) {
--- a/stage4/generate_c/generate_c_inlinefcall.cc	Wed Jun 13 19:51:26 2012 +0200
+++ b/stage4/generate_c/generate_c_inlinefcall.cc	Thu Jun 14 12:00:19 2012 +0100
@@ -179,7 +179,7 @@
       if (function_type_suffix) {
         function_type_suffix->accept(*this);
       }
-      s4o.print_integer(fcall_number);
+      s4o.print(fcall_number);
       s4o.print("(");
       s4o.indent_right();
 
--- a/stage4/generate_c/generate_c_sfc.cc	Wed Jun 13 19:51:26 2012 +0200
+++ b/stage4/generate_c/generate_c_sfc.cc	Thu Jun 14 12:00:19 2012 +0100
@@ -117,7 +117,7 @@
     }      
 
     void print_transition_number(void) {
-      s4o.print_integer(transition_number);
+      s4o.print(transition_number);
     }
 
     void print_reset_step(symbol_c *step_name) {
--- a/stage4/generate_c/generate_c_sfcdecl.cc	Wed Jun 13 19:51:26 2012 +0200
+++ b/stage4/generate_c/generate_c_sfcdecl.cc	Thu Jun 14 12:00:19 2012 +0100
@@ -89,24 +89,24 @@
           
           /* steps table declaration */
           s4o.print(s4o.indent_spaces + "STEP __step_list[");
-          s4o.print_integer(step_number);
+          s4o.print(step_number);
           s4o.print("];\n");
           s4o.print(s4o.indent_spaces + "UINT __nb_steps;\n");
           
           /* actions table declaration */
           s4o.print(s4o.indent_spaces + "ACTION __action_list[");
-          s4o.print_integer(action_number);
+          s4o.print(action_number);
           s4o.print("];\n");
           s4o.print(s4o.indent_spaces + "UINT __nb_actions;\n");
           
           /* transitions table declaration */
           s4o.print(s4o.indent_spaces + "__IEC_BOOL_t __transition_list[");
-          s4o.print_integer(transition_number);
+          s4o.print(transition_number);
           s4o.print("];\n");
           
           /* transitions debug table declaration */
           s4o.print(s4o.indent_spaces + "__IEC_BOOL_t __debug_transition_list[");
-          s4o.print_integer(transition_number);
+          s4o.print(transition_number);
           s4o.print("];\n");
           s4o.print(s4o.indent_spaces + "UINT __nb_transitions;\n");
           
@@ -124,7 +124,7 @@
           s4o.print(s4o.indent_spaces);
           print_variable_prefix();
           s4o.print("__nb_steps = ");
-          s4o.print_integer(step_number);
+          s4o.print(step_number);
           s4o.print(";\n");
           step_number = 0;
           wanted_sfcdeclaration = sfcinit_sd;
@@ -150,7 +150,7 @@
           s4o.print(s4o.indent_spaces);
           print_variable_prefix();
           s4o.print("__nb_actions = ");
-          s4o.print_integer(action_number);
+          s4o.print(action_number);
           s4o.print(";\n");
           action_number = 0;
           wanted_sfcdeclaration = sfcinit_sd;
@@ -174,7 +174,7 @@
           s4o.print(s4o.indent_spaces);
           print_variable_prefix();
           s4o.print("__nb_transitions = ");
-          s4o.print_integer(transition_number);
+          s4o.print(transition_number);
           s4o.print(";\n");
           transition_number = 0;
           wanted_sfcdeclaration = sfcinit_sd;
@@ -199,7 +199,7 @@
               s4o.print(SFC_STEP_ACTION_PREFIX);
               pt->symbol->accept(*this);
               s4o.print(" ");
-              s4o.print_integer(action_number);
+              s4o.print(action_number);
               s4o.print("\n");
               action_number++;
             }
@@ -251,7 +251,7 @@
           s4o.print("(");
           print_variable_prefix();
           s4o.print(",__step_list[");
-          s4o.print_integer(step_number);
+          s4o.print(step_number);
           s4o.print("].state,1);\n");
           step_number++;
           break;
@@ -260,7 +260,7 @@
           s4o.print(SFC_STEP_ACTION_PREFIX);
           symbol->step_name->accept(*this);
           s4o.print(" ");
-          s4o.print_integer(step_number);
+          s4o.print(step_number);
           s4o.print("\n");
           step_number++;
           break;
@@ -292,7 +292,7 @@
           s4o.print(SFC_STEP_ACTION_PREFIX);
           symbol->step_name->accept(*this);
           s4o.print(" ");
-          s4o.print_integer(step_number);
+          s4o.print(step_number);
           s4o.print("\n");
           step_number++;
           break;
@@ -346,7 +346,7 @@
           s4o.print(SFC_STEP_ACTION_PREFIX);
           symbol->action_name->accept(*this);
           s4o.print(" ");
-          s4o.print_integer(action_number);
+          s4o.print(action_number);
           s4o.print("\n");
           action_number++;
           break;
--- a/stage4/generate_c/generate_c_st.cc	Wed Jun 13 19:51:26 2012 +0200
+++ b/stage4/generate_c/generate_c_st.cc	Thu Jun 14 12:00:19 2012 +0100
@@ -792,7 +792,7 @@
       print_function_parameter_data_types_c overloaded_func_suf(&s4o);
       f_decl->accept(overloaded_func_suf);
     }
-    s4o.print_integer(fcall_number);
+    s4o.print(fcall_number);
   }
   else {
     function_name->accept(*this);
--- a/stage4/generate_c/generate_c_typedecl.cc	Wed Jun 13 19:51:26 2012 +0200
+++ b/stage4/generate_c/generate_c_typedecl.cc	Thu Jun 14 12:00:19 2012 +0100
@@ -75,18 +75,6 @@
 
     basetypedeclaration_t current_basetypedeclaration;
 
-    void print_integer(unsigned long long int integer) {
-      char str[24];
-      sprintf(str, "%llu", integer);
-      s4o.print(str);
-    }
-
-    void print_integer_incl(unsigned long long int integer) {
-      char str[24];
-      sprintf(str, "%llu", integer);
-      s4o_incl.print(str);
-    }
-
     void *print_list_incl(list_c *list,
          std::string pre_elem_str = "",
          std::string inter_elem_str = "",
@@ -255,7 +243,7 @@
     case array_td:
       if (current_basetypedeclaration == arraysubrange_bd) {
         s4o_incl.print("[");
-        print_integer_incl(symbol->dimension);
+        s4o_incl.print(symbol->dimension);
         s4o_incl.print("]");
       }
       else
--- a/stage4/generate_c/generate_c_vardecl.cc	Wed Jun 13 19:51:26 2012 +0200
+++ b/stage4/generate_c/generate_c_vardecl.cc	Thu Jun 14 12:00:19 2012 +0100
@@ -223,7 +223,7 @@
           break;
         case typedecl_am:
           s4o.print("_");
-          s4o.print_integer(symbol->dimension);
+          s4o.print(symbol->dimension);
           break;
         default:
           break;
@@ -1413,7 +1413,7 @@
 //SYM_REF2(subrange_c, lower_limit, upper_limit)
 void *visit(subrange_c *symbol) {
   s4o.print("_");
-  print_integer(symbol->dimension);
+  s4o.print(symbol->dimension);
   return NULL;
 }
 
--- a/stage4/stage4.cc	Wed Jun 13 19:51:26 2012 +0200
+++ b/stage4/stage4.cc	Thu Jun 14 12:00:19 2012 +0100
@@ -136,18 +136,18 @@
     indent_spaces.erase();
 }
 
-
-void *stage4out_c::print(const char *str) {
-  if (!allow_output) return NULL;
-  *out << str;
-  return NULL;
-}
-
-void *stage4out_c::print_integer(int integer) {
-  if (!allow_output) return NULL;
-  *out << integer;
-  return NULL;
-}
+void *stage4out_c::print(           std::string value) {if (!allow_output) return NULL; *out << value; return NULL;}
+void *stage4out_c::print(           const char *value) {if (!allow_output) return NULL; *out << value; return NULL;}
+//void *stage4out_c::print(               int64_t value) {if (!allow_output) return NULL; *out << value; return NULL;}
+//void *stage4out_c::print(              uint64_t value) {if (!allow_output) return NULL; *out << value; return NULL;}
+void *stage4out_c::print(              real64_t value) {if (!allow_output) return NULL; *out << value; return NULL;}
+void *stage4out_c::print(                   int value) {if (!allow_output) return NULL; *out << value; return NULL;}
+void *stage4out_c::print(              long int value) {if (!allow_output) return NULL; *out << value; return NULL;}
+void *stage4out_c::print(         long long int value) {if (!allow_output) return NULL; *out << value; return NULL;}
+void *stage4out_c::print(unsigned           int value) {if (!allow_output) return NULL; *out << value; return NULL;}
+void *stage4out_c::print(unsigned      long int value) {if (!allow_output) return NULL; *out << value; return NULL;}
+void *stage4out_c::print(unsigned long long int value) {if (!allow_output) return NULL; *out << value; return NULL;}
+
 
 void *stage4out_c::print_long_integer(unsigned long l_integer, bool suffix) {
   if (!allow_output) return NULL;
@@ -163,23 +163,6 @@
   return NULL;
 }
 
-void *stage4out_c::print_int64(int64_t integer) {
-  if (!allow_output) return NULL;
-  *out << integer;
-  return NULL;
-}
-
-void *stage4out_c::print_uint64(uint64_t integer) {
-  if (!allow_output) return NULL;
-  *out << integer;
-  return NULL;
-}
-
-void *stage4out_c::print_real64(real64_t integer) {
-  if (!allow_output) return NULL;
-  *out << integer;
-  return NULL;
-}
 
 void *stage4out_c::printupper(const char *str) {
   if (!allow_output) return NULL;
@@ -214,12 +197,6 @@
 }
 
 
-void *stage4out_c::print(std::string str) {
-  if (!allow_output) return NULL;
-  *out << str;
-  return NULL;
-}
-
 
 void *stage4out_c::printupper(std::string str) {
   if (!allow_output) return NULL;
--- a/stage4/stage4.hh	Wed Jun 13 19:51:26 2012 +0200
+++ b/stage4/stage4.hh	Thu Jun 14 12:00:19 2012 +0100
@@ -61,16 +61,21 @@
     void indent_right(void);
     void indent_left(void);
 
-    void *print(const char *str);
-    void *print(std::string str);
+    void *print(          std::string  value);
+    void *print(           const char *value);
+    //void *print(               int64_t value); // not required, since we have long long int, or similar
+    //void *print(              uint64_t value); // not required, since we have long long int, or similar
+    void *print(              real64_t value);
+    void *print(                   int value);
+    void *print(              long int value);
+    void *print(         long long int value);
+    void *print(unsigned           int value);
+    void *print(unsigned      long int value);
+    void *print(unsigned long long int value);
     
-    void *print_integer(int integer);
     void *print_long_integer(unsigned long l_integer, bool suffix=true);
     void *print_long_long_integer(unsigned long long ll_integer, bool suffix=true);
 
-    void *print_int64(int64_t integer);
-    void *print_uint64(uint64_t integer);
-    void *print_real64(real64_t integer);
 
     void *printupper(const char *str);
     void *printupper(std::string str);