stage3/constant_folding.cc
changeset 974 a47c2df5ae3d
parent 973 f86d5d6bb04e
child 981 aad6aa35ce60
equal deleted inserted replaced
973:f86d5d6bb04e 974:a47c2df5ae3d
  1058       // debug_c::print(list->elements[i]);
  1058       // debug_c::print(list->elements[i]);
  1059       ERROR;
  1059       ERROR;
  1060     }
  1060     }
  1061     list->elements[i]->const_value = init_value->const_value;
  1061     list->elements[i]->const_value = init_value->const_value;
  1062     if (fixed_init_value_) {
  1062     if (fixed_init_value_) {
  1063       std::string varName = var_name->value;
  1063       values[var_name->value] = init_value->const_value;
  1064       values[varName] = init_value->const_value;
       
  1065     }
  1064     }
  1066   }
  1065   }
  1067   return NULL;
  1066   return NULL;
  1068 }
  1067 }
  1069 
  1068 
  1169 //SYM_REF2(external_declaration_c, global_var_name, specification)
  1168 //SYM_REF2(external_declaration_c, global_var_name, specification)
  1170 void *constant_folding_c::visit(external_declaration_c *symbol) {
  1169 void *constant_folding_c::visit(external_declaration_c *symbol) {
  1171   // Note that specification->const_value will have been set by handle_var_extern_global_pair(), which is called from declaration_check_c
  1170   // Note that specification->const_value will have been set by handle_var_extern_global_pair(), which is called from declaration_check_c
  1172   symbol->global_var_name->const_value = symbol->specification->const_value;
  1171   symbol->global_var_name->const_value = symbol->specification->const_value;
  1173   if (fixed_init_value_) {
  1172   if (fixed_init_value_) {
  1174     std::string varName = get_var_name_c::get_name(symbol->global_var_name)->value;
  1173     values[get_var_name_c::get_name(symbol->global_var_name)->value] = symbol->specification->const_value;
  1175     values[varName] = symbol->specification->const_value;
       
  1176   }
  1174   }
  1177   // If the datatype specification is a subrange or array, do constant folding of all the literals in that type declaration... (ex: literals in array subrange limits)
  1175   // If the datatype specification is a subrange or array, do constant folding of all the literals in that type declaration... (ex: literals in array subrange limits)
  1178   symbol->specification->accept(*this);  // should never get to change the const_value of the symbol->specification symbol (only its children!).
  1176   symbol->specification->accept(*this);  // should never get to change the const_value of the symbol->specification symbol (only its children!).
  1179   return NULL;
  1177   return NULL;
  1180 }
  1178 }
  1720 	std::string varName;
  1718 	std::string varName;
  1721 
  1719 
  1722 	symbol->r_exp->accept(*this);
  1720 	symbol->r_exp->accept(*this);
  1723 	symbol->l_exp->accept(*this); // if the lvalue has an array, do contant folding of the array indexes!
  1721 	symbol->l_exp->accept(*this); // if the lvalue has an array, do contant folding of the array indexes!
  1724 	symbol->l_exp->const_value = symbol->r_exp->const_value;
  1722 	symbol->l_exp->const_value = symbol->r_exp->const_value;
  1725 	varName = get_var_name_c::get_name(symbol->l_exp)->value;
  1723 	values[get_var_name_c::get_name(symbol->l_exp)->value] = symbol->l_exp->const_value;
  1726 	values[varName] = symbol->l_exp->const_value;
       
  1727 	return NULL;
  1724 	return NULL;
  1728 }
  1725 }
  1729 
  1726 
  1730 #if DO_CONSTANT_PROPAGATION__
  1727 #if DO_CONSTANT_PROPAGATION__
  1731 /********************************/
  1728 /********************************/
  1760 /* B 3.2.4 Iteration Statements */
  1757 /* B 3.2.4 Iteration Statements */
  1761 /********************************/
  1758 /********************************/
  1762 void *constant_folding_c::visit(for_statement_c *symbol) {
  1759 void *constant_folding_c::visit(for_statement_c *symbol) {
  1763 	map_values_t values_incoming;
  1760 	map_values_t values_incoming;
  1764 	map_values_t values_statement_result;
  1761 	map_values_t values_statement_result;
  1765 	std::string varName;
       
  1766 
  1762 
  1767 	values_incoming = values; /* save incoming status */
  1763 	values_incoming = values; /* save incoming status */
  1768 	symbol->beg_expression->accept(*this);
  1764 	symbol->beg_expression->accept(*this);
  1769 	symbol->end_expression->accept(*this);
  1765 	symbol->end_expression->accept(*this);
  1770 	varName =  get_var_name_c::get_name(symbol->control_variable)->value;
  1766 	values[get_var_name_c::get_name(symbol->control_variable)->value]._int64.status = const_value_c::cs_non_const;
  1771 	values[varName]._int64.status = const_value_c::cs_non_const;
       
  1772 
  1767 
  1773 	/* Optimize dead code */
  1768 	/* Optimize dead code */
  1774 	if (NULL != symbol->by_expression) {
  1769 	if (NULL != symbol->by_expression) {
  1775 		symbol->by_expression->accept(*this);
  1770 		symbol->by_expression->accept(*this);
  1776 		if (VALID_CVALUE(int64, symbol->by_expression ) &&   GET_CVALUE(int64, symbol->by_expression ) > 0 &&
  1771 		if (VALID_CVALUE(int64, symbol->by_expression ) &&   GET_CVALUE(int64, symbol->by_expression ) > 0 &&