stage3/constant_folding.hh
changeset 984 634269b0f104
parent 982 760b26477193
child 988 3b12a6cf9fbd
equal deleted inserted replaced
983:ead554e12195 984:634269b0f104
    51 #define DO_CONSTANT_PROPAGATION__ 0
    51 #define DO_CONSTANT_PROPAGATION__ 0
    52 
    52 
    53 
    53 
    54 
    54 
    55 class constant_folding_c : public iterator_visitor_c {
    55 class constant_folding_c : public iterator_visitor_c {
    56     search_varfb_instance_type_c *search_varfb_instance_type;
    56   protected:
    57     int error_count;
    57     int error_count;
    58     bool warning_found;
    58     bool warning_found;
    59     int current_display_error_level;
    59     int current_display_error_level;
       
    60   private:
    60     /* Pointer to the previous IL instruction, which contains the current cvalue of the data stored in the IL stack, i.e. the default variable, a.k.a. accumulator */
    61     /* Pointer to the previous IL instruction, which contains the current cvalue of the data stored in the IL stack, i.e. the default variable, a.k.a. accumulator */
    61     symbol_c *prev_il_instruction;
    62     symbol_c *prev_il_instruction;
    62     /* the current IL operand being analyzed */
    63     /* the current IL operand being analyzed */
    63     symbol_c *il_operand;
    64     symbol_c *il_operand;
    64 
    65 
    66 
    67 
    67   public:
    68   public:
    68     constant_folding_c(symbol_c *symbol = NULL);
    69     constant_folding_c(symbol_c *symbol = NULL);
    69     virtual ~constant_folding_c(void);
    70     virtual ~constant_folding_c(void);
    70     int get_error_count();
    71     int get_error_count();
    71     typedef symtable_c<const_value_c> map_values_t;
    72  
    72   private:
    73   private:
    73     symbol_c *current_resource;
       
    74     symbol_c *current_configuration;
       
    75     map_values_t values;
       
    76     map_values_t var_global_values;
       
    77     void *handle_var_list_decl(symbol_c *var_list, symbol_c *type_decl);
       
    78     void *handle_var_decl     (symbol_c *var_list, bool fixed_init_value);
       
    79     // Flag to indicate whether the variables in the variable declaration list will always have a fixed value when the POU is executed!
       
    80     // VAR CONSTANT ... END_VAR will always be true
       
    81     // VAR          ... END_VAR will always be true for functions (who initialise local variables every time they are called), but false for FBs and PROGRAMS
       
    82     bool fixed_init_value_; 
       
    83     bool function_pou_;
       
    84     bool is_constant(symbol_c *option);
       
    85     bool is_retain  (symbol_c *option);
       
    86 
       
    87 
       
    88   public:
       
    89 	#if 0
       
    90 	// not currently needed, so comment it out!...
       
    91 	/* utility functions for other stage3 algorithms to access the contant folded values */
       
    92 	/* written as static since we do not need to iteratively visit the symbols! */
       
    93 	// returns true if both symbols have the same value in all the cvalues
       
    94 	static bool is_equal_cvalue(symbol_c *symbol_1, symbol_c *symbol_2);
       
    95 	#endif
       
    96 
       
    97   private:
       
    98     /***************************/
       
    99     /* B 0 - Programming Model */
       
   100     /***************************/
       
   101     void *visit(library_c *symbol);
       
   102     /*********************/
    74     /*********************/
   103     /* B 1.2 - Constants */
    75     /* B 1.2 - Constants */
   104     /*********************/
    76     /*********************/
   105     /******************************/
    77     /******************************/
   106     /* B 1.2.1 - Numeric Literals */
    78     /* B 1.2.1 - Numeric Literals */
   121 
    93 
   122     /************************/
    94     /************************/
   123     /* B 1.2.3.1 - Duration */
    95     /* B 1.2.3.1 - Duration */
   124     /********* **************/
    96     /********* **************/
   125     void *visit(fixed_point_c *symbol);
    97     void *visit(fixed_point_c *symbol);
   126 
       
   127     /*********************/
       
   128     /* B 1.4 - Variables */
       
   129     /*********************/
       
   130     #if DO_CONSTANT_PROPAGATION__
       
   131     void *visit(symbolic_variable_c *symbol);
       
   132     #endif // DO_CONSTANT_PROPAGATION__
       
   133     void *visit(symbolic_constant_c *symbol);
       
   134                              
       
   135     /******************************************/
       
   136     /* B 1.4.3 - Declaration & Initialisation */
       
   137     /******************************************/
       
   138     void *visit(        input_declarations_c *symbol);
       
   139     void *visit(       output_declarations_c *symbol);
       
   140     void *visit( input_output_declarations_c *symbol);
       
   141     void *visit(          var_declarations_c *symbol);
       
   142     void *visit(retentive_var_declarations_c *symbol);
       
   143     void *visit( external_var_declarations_c *symbol);
       
   144     void *visit(   global_var_declarations_c *symbol);
       
   145     void *visit(  external_declaration_c     *symbol);
       
   146     void *visit(global_var_decl_c            *symbol);
       
   147     void *visit( var1_init_decl_c            *symbol);
       
   148 
       
   149     /**************************************/
       
   150     /* B.1.5 - Program organization units */
       
   151     /**************************************/
       
   152     /***********************/
       
   153     /* B 1.5.1 - Functions */
       
   154     /***********************/
       
   155     void *visit(function_declaration_c *symbol);
       
   156     void *visit(function_var_decls_c   *symbol);
       
   157 
       
   158     /*****************************/
       
   159     /* B 1.5.2 - Function Blocks */
       
   160     /*****************************/
       
   161     void *visit(function_block_declaration_c *symbol);
       
   162     void *visit(            temp_var_decls_c *symbol);
       
   163     void *visit(   non_retentive_var_decls_c *symbol);
       
   164 
       
   165     /**********************/
       
   166     /* B 1.5.3 - Programs */
       
   167     /**********************/
       
   168     void *visit(       program_declaration_c *symbol);
       
   169 
       
   170     /********************************/
       
   171     /* B 1.7 Configuration elements */
       
   172     /********************************/
       
   173     void *visit( configuration_declaration_c *symbol);
       
   174     void *visit(      resource_declaration_c *symbol);
       
   175     void *visit(     program_configuration_c *symbol);
       
   176     void *visit(                   fb_task_c *symbol);
       
   177 
       
   178 
    98 
   179     /****************************************/
    99     /****************************************/
   180     /* B.2 - Language IL (Instruction List) */
   100     /* B.2 - Language IL (Instruction List) */
   181     /****************************************/
   101     /****************************************/
   182     /***********************************/
   102     /***********************************/
   266     void *visit(   mod_expression_c *symbol);
   186     void *visit(   mod_expression_c *symbol);
   267     void *visit( power_expression_c *symbol);
   187     void *visit( power_expression_c *symbol);
   268     void *visit(   neg_expression_c *symbol);
   188     void *visit(   neg_expression_c *symbol);
   269     void *visit(   not_expression_c *symbol);
   189     void *visit(   not_expression_c *symbol);
   270     //void *visit(function_invocation_c *symbol); /* TODO */
   190     //void *visit(function_invocation_c *symbol); /* TODO */
       
   191 };
       
   192 
       
   193 
       
   194 
       
   195 
       
   196 
       
   197 
       
   198 
       
   199 
       
   200 
       
   201 
       
   202 
       
   203 
       
   204 
       
   205 class constant_propagation_c : public constant_folding_c {
       
   206   public:
       
   207     constant_propagation_c(symbol_c *symbol = NULL);
       
   208     virtual ~constant_propagation_c(void);
       
   209     typedef symtable_c<const_value_c> map_values_t;
       
   210   private:
       
   211     symbol_c *current_resource;
       
   212     symbol_c *current_configuration;
       
   213     map_values_t values;
       
   214     map_values_t var_global_values;
       
   215     void *handle_var_list_decl(symbol_c *var_list, symbol_c *type_decl);
       
   216     void *handle_var_decl     (symbol_c *var_list, bool fixed_init_value);
       
   217     // Flag to indicate whether the variables in the variable declaration list will always have a fixed value when the POU is executed!
       
   218     // VAR CONSTANT ... END_VAR will always be true
       
   219     // VAR          ... END_VAR will always be true for functions (who initialise local variables every time they are called), but false for FBs and PROGRAMS
       
   220     bool fixed_init_value_; 
       
   221     bool function_pou_;
       
   222     bool is_constant(symbol_c *option);
       
   223     bool is_retain  (symbol_c *option);
       
   224     static map_values_t inner_left_join_values(map_values_t m1, map_values_t m2);
       
   225 
       
   226 
       
   227   private:
       
   228     /***************************/
       
   229     /* B 0 - Programming Model */
       
   230     /***************************/
       
   231     void *visit(library_c *symbol);
       
   232 
       
   233     /*********************/
       
   234     /* B 1.4 - Variables */
       
   235     /*********************/
       
   236     #if DO_CONSTANT_PROPAGATION__
       
   237     void *visit(symbolic_variable_c *symbol);
       
   238     #endif // DO_CONSTANT_PROPAGATION__
       
   239     void *visit(symbolic_constant_c *symbol);
       
   240                              
       
   241     /******************************************/
       
   242     /* B 1.4.3 - Declaration & Initialisation */
       
   243     /******************************************/
       
   244     void *visit(        input_declarations_c *symbol);
       
   245     void *visit(       output_declarations_c *symbol);
       
   246     void *visit( input_output_declarations_c *symbol);
       
   247     void *visit(          var_declarations_c *symbol);
       
   248     void *visit(retentive_var_declarations_c *symbol);
       
   249     void *visit( external_var_declarations_c *symbol);
       
   250     void *visit(   global_var_declarations_c *symbol);
       
   251     void *visit(  external_declaration_c     *symbol);
       
   252     void *visit(global_var_decl_c            *symbol);
       
   253     void *visit( var1_init_decl_c            *symbol);
       
   254 
       
   255     /**************************************/
       
   256     /* B.1.5 - Program organization units */
       
   257     /**************************************/
       
   258     /***********************/
       
   259     /* B 1.5.1 - Functions */
       
   260     /***********************/
       
   261     void *visit(function_declaration_c *symbol);
       
   262     void *visit(function_var_decls_c   *symbol);
       
   263 
       
   264     /*****************************/
       
   265     /* B 1.5.2 - Function Blocks */
       
   266     /*****************************/
       
   267     void *visit(function_block_declaration_c *symbol);
       
   268     void *visit(            temp_var_decls_c *symbol);
       
   269     void *visit(   non_retentive_var_decls_c *symbol);
       
   270 
       
   271     /**********************/
       
   272     /* B 1.5.3 - Programs */
       
   273     /**********************/
       
   274     void *visit(       program_declaration_c *symbol);
       
   275 
       
   276     /********************************/
       
   277     /* B 1.7 Configuration elements */
       
   278     /********************************/
       
   279     void *visit( configuration_declaration_c *symbol);
       
   280     void *visit(      resource_declaration_c *symbol);
       
   281     void *visit(     program_configuration_c *symbol);
       
   282     void *visit(                   fb_task_c *symbol);
       
   283 
       
   284 
       
   285     /****************************************/
       
   286     /* B.2 - Language IL (Instruction List) */
       
   287     /****************************************/
       
   288     /***********************************/
       
   289     /* B 2.1 Instructions and Operands */
       
   290     /***********************************/
       
   291     //void *visit(il_function_call_c *symbol);  /* TODO */
       
   292     // void *visit(il_fb_call_c *symbol);       /* TODO: move from constant_folding_c */
       
   293     //void *visit(il_formal_funct_call_c *symbol);   /* TODO */
       
   294     //void *visit(il_operand_list_c *symbol);  /* Not needed, since we inherit from iterator_visitor_c */
       
   295 
       
   296 
       
   297     /*******************/
       
   298     /* B 2.2 Operators */
       
   299     /*******************/
       
   300     /* Symbol class handled together with function call checks */
       
   301     // void *visit(il_assign_operator_c *symbol, variable_name);
       
   302     /* Symbol class handled together with function call checks */
       
   303     // void *visit(il_assign_operator_c *symbol, option, variable_name);
       
   304 
       
   305     /***************************************/
       
   306     /* B.3 - Language ST (Structured Text) */
       
   307     /***************************************/
       
   308     /***********************/
       
   309     /* B 3.1 - Expressions */
       
   310     /***********************/
       
   311     //void *visit(function_invocation_c *symbol); /* TODO */
   271 
   312 
   272     #if DO_CONSTANT_PROPAGATION__
   313     #if DO_CONSTANT_PROPAGATION__
   273     /*********************************/
   314     /*********************************/
   274     /* B 3.2.1 Assignment Statements */
   315     /* B 3.2.1 Assignment Statements */
   275     /*********************************/
   316     /*********************************/