stage3/constant_folding.cc
changeset 964 5f4dfe6670da
parent 963 e3d4dca7520b
child 965 c9eeb67ba939
equal deleted inserted replaced
963:e3d4dca7520b 964:5f4dfe6670da
  1035   return 0;
  1035   return 0;
  1036 }
  1036 }
  1037 
  1037 
  1038 
  1038 
  1039 void *constant_folding_c::handle_var_list_decl(symbol_c *var_list, symbol_c *type_decl) {
  1039 void *constant_folding_c::handle_var_list_decl(symbol_c *var_list, symbol_c *type_decl) {
       
  1040   type_decl->accept(*this);  // Do constant folding of the initial value, and literals in subranges! (we will probably be doing this multiple times for the same init value, but this is safe as the cvalue is idem-potent)
  1040   symbol_c *init_value = type_initial_value_c::get(type_decl);  
  1041   symbol_c *init_value = type_initial_value_c::get(type_decl);  
  1041   if (NULL == init_value)   return NULL; // this is probably a FB datatype, for which no initial value exists! Do nothing and return.
  1042   if (NULL == init_value)   return NULL; // this is probably a FB datatype, for which no initial value exists! Do nothing and return.
  1042   init_value->accept(*this);  // Do constant folding of the initial value! (we will probably be doing this multiple times for the same init value, but this is safe as the cvalue is idem-potent)
       
  1043 
  1043 
  1044   list_c *list = dynamic_cast<list_c *>(var_list);
  1044   list_c *list = dynamic_cast<list_c *>(var_list);
  1045   if (NULL == list) ERROR;
  1045   if (NULL == list) ERROR;
  1046   for (int i = 0; i < list->n; i++) {
  1046   for (int i = 0; i < list->n; i++) {
  1047     token_c *var_name = dynamic_cast<token_c *>(list->elements[i]);
  1047     token_c *var_name = dynamic_cast<token_c *>(list->elements[i]);
  1084 //SYM_REF2(fb_name_decl_c, fb_name_list, fb_spec_init)                   // We do not yet handle FBs!
  1084 //SYM_REF2(fb_name_decl_c, fb_name_list, fb_spec_init)                   // We do not yet handle FBs!
  1085 /* fb_name_list ',' fb_name */
  1085 /* fb_name_list ',' fb_name */
  1086 //SYM_LIST(fb_name_list_c)                                               // Not needed!
  1086 //SYM_LIST(fb_name_list_c)                                               // Not needed!
  1087 /* VAR_INPUT [option] input_declaration_list END_VAR */
  1087 /* VAR_INPUT [option] input_declaration_list END_VAR */
  1088 /* option -> the RETAIN/NON_RETAIN/<NULL> directive... */
  1088 /* option -> the RETAIN/NON_RETAIN/<NULL> directive... */
  1089 //SYM_REF3(input_declarations_c, option, input_declaration_list, method)
  1089 //SYM_REF3(input_declarations_c, option, input_declaration_list, method) // Not needed since we inherit from iterator_visitor_c!
  1090 void *constant_folding_c::visit(input_declarations_c *symbol) {return NULL;}  // Input variables can take any initial value, so we can not set the const_value annotation!
  1090 // NOTE: Input variables can take any initial value, so we can not set the const_value annotation => we do NOT call handle_var_list_decl() !!!
  1091 
  1091 //       We must still visit it iteratively, to set the const_value of all literals in the type declarations.
  1092 /* helper symbol for input_declarations */
  1092 /* helper symbol for input_declarations */
  1093 //SYM_LIST(input_declaration_list_c)                                     // Not needed!
  1093 //SYM_LIST(input_declaration_list_c)                                     // Not needed!
  1094 /* VAR_OUTPUT [RETAIN | NON_RETAIN] var_init_decl_list END_VAR */
  1094 /* VAR_OUTPUT [RETAIN | NON_RETAIN] var_init_decl_list END_VAR */
  1095 /* option -> may be NULL ! */
  1095 /* option -> may be NULL ! */
  1096 //SYM_REF3(output_declarations_c, option, var_init_decl_list, method)    // Not needed since we inherit from iterator_visitor_c!
  1096 //SYM_REF3(output_declarations_c, option, var_init_decl_list, method)    // Not needed since we inherit from iterator_visitor_c!
  1097 
  1097 
  1098 /*  VAR_IN_OUT var_declaration_list END_VAR */
  1098 /*  VAR_IN_OUT var_declaration_list END_VAR */
  1099 //SYM_REF1(input_output_declarations_c, var_declaration_list)
  1099 //SYM_REF1(input_output_declarations_c, var_declaration_list)            // Not needed since we inherit from iterator_visitor_c!
  1100 void *constant_folding_c::visit(input_output_declarations_c *symbol) {return NULL;}  // Input variables can take any initial value, so we can not set the const_value annotation!
  1100 // NOTE: Input variables can take any initial value, so we can not set the const_value annotation => we do NOT call handle_var_list_decl() !!!
       
  1101 //       We must still visit it iteratively, to set the const_value of all literals in the type declarations.
  1101 /* helper symbol for input_output_declarations */
  1102 /* helper symbol for input_output_declarations */
  1102 /* var_declaration_list var_declaration ';' */
  1103 /* var_declaration_list var_declaration ';' */
  1103 //SYM_LIST(var_declaration_list_c)                                       // Not needed since we inherit from iterator_visitor_c!
  1104 //SYM_LIST(var_declaration_list_c)                                       // Not needed since we inherit from iterator_visitor_c!
  1104 /*  var1_list ':' array_specification */
  1105 /*  var1_list ':' array_specification */
  1105 //SYM_REF2(array_var_declaration_c, var1_list, array_specification)      // We do not yet handle arrays! 
  1106 //SYM_REF2(array_var_declaration_c, var1_list, array_specification)      // We do not yet handle arrays! 
  1108 /* VAR [CONSTANT] var_init_decl_list END_VAR */
  1109 /* VAR [CONSTANT] var_init_decl_list END_VAR */
  1109 /* option -> may be NULL ! */
  1110 /* option -> may be NULL ! */
  1110 //SYM_REF2(var_declarations_c, option, var_init_decl_list)               // Not needed: we inherit from iterator_c
  1111 //SYM_REF2(var_declarations_c, option, var_init_decl_list)               // Not needed: we inherit from iterator_c
  1111 
  1112 
  1112 /*  VAR RETAIN var_init_decl_list END_VAR */
  1113 /*  VAR RETAIN var_init_decl_list END_VAR */
  1113 //SYM_REF1(retentive_var_declarations_c, var_init_decl_list)
  1114 //SYM_REF1(retentive_var_declarations_c, var_init_decl_list)             // Not needed since we inherit from iterator_visitor_c!
  1114 void *constant_folding_c::visit(retentive_var_declarations_c *symbol) {return NULL;}  // Retentive variables can take any initial value, so we can not set the const_value annotation!
  1115 // NOTE: Retentive variables can take any initial value, so we can not set the const_value annotation => we do NOT call handle_var_list_decl() !!!
       
  1116 //       We must still visit it iteratively, to set the const_value of all literals in the type declarations.
  1115 /*  VAR [CONSTANT|RETAIN|NON_RETAIN] located_var_decl_list END_VAR */
  1117 /*  VAR [CONSTANT|RETAIN|NON_RETAIN] located_var_decl_list END_VAR */
  1116 /* option -> may be NULL ! */
  1118 /* option -> may be NULL ! */
  1117 
  1119 
  1118 #if 0  
  1120 #if 0  
  1119 // TODO
  1121 // TODO
  1141 void *constant_folding_c::visit(external_declaration_c *symbol) {
  1143 void *constant_folding_c::visit(external_declaration_c *symbol) {
  1142   // Note that specification->const_value will have been set by handle_var_extern_global_pair, which is called from declaration_check_c
  1144   // Note that specification->const_value will have been set by handle_var_extern_global_pair, which is called from declaration_check_c
  1143   symbol->global_var_name->const_value = symbol->specification->const_value;
  1145   symbol->global_var_name->const_value = symbol->specification->const_value;
  1144   std::string varName = get_var_name_c::get_name(symbol->global_var_name)->value;
  1146   std::string varName = get_var_name_c::get_name(symbol->global_var_name)->value;
  1145   values[varName] = symbol->specification->const_value;
  1147   values[varName] = symbol->specification->const_value;
       
  1148   // If the datatype specification is a subrange or array, do constant folding of all the literals in that type declaration...
       
  1149   symbol->specification->accept(*this);  // should never get to change the const_value of the symbol->specification symbol (only its children!).
  1146   return NULL;
  1150   return NULL;
  1147 }
  1151 }
  1148 
  1152 
  1149 /* Visitors related to GLOBAL variables are not really needed, 
  1153 /* Visitors related to GLOBAL variables are not really needed, 
  1150  * as they are already handled by handle_var_extern_global_pair, which is called from declaration_check_c 
  1154  * as they are already handled by handle_var_extern_global_pair, which is called from declaration_check_c