Implement Mario's suggestions:
authorManuele Conti <conti.ma@alice.it>
Thu, 03 Jan 2013 22:54:55 +0100
changeset 788 aa56031e5cb3
parent 787 6e2671e0f1a8
child 789 861e92dc879b
Implement Mario's suggestions:
- Clean up code replace merge algorithm with a function.
- Fix for_statement setting control variable to not a constant.
- Fix for_statement checking BY expression to get correct value for optimization
branch.

Thanks Mario.
stage3/constant_folding.cc
--- a/stage3/constant_folding.cc	Thu Jan 03 20:34:10 2013 +0100
+++ b/stage3/constant_folding.cc	Thu Jan 03 22:54:55 2013 +0100
@@ -236,8 +236,10 @@
 		}\
 }
 
-
-static std::map <std::string, symbol_c::const_value_t> values;
+typedef std::map <std::string, symbol_c::const_value_t> map_values_t;
+
+static map_values_t values;
+
 
 
 /***********************************************************************/
@@ -723,6 +725,30 @@
 	return NULL;
 }
 
+static map_values_t inner_left_join_values(map_values_t m1, map_values_t m2) {
+	map_values_t::const_iterator itr;
+	map_values_t ret;
+
+	itr = m1.begin();
+	for ( ; itr != m1.end(); ++itr) {
+		std::string name = itr->first;
+		symbol_c::const_value_t value;
+
+		if (m2.count(name) > 0) {
+			symbol_c::const_value_t c1 = itr->second;
+			symbol_c::const_value_t c2 = m2[name];
+			COMPUTE_MEET_SEMILATTICE (real64, c1, c2, value);
+			COMPUTE_MEET_SEMILATTICE (uint64, c1, c2, value);
+			COMPUTE_MEET_SEMILATTICE ( int64, c1, c2, value);
+			COMPUTE_MEET_SEMILATTICE (  bool, c1, c2, value);
+		} else
+			value = m1[name];
+		ret[name] = value;
+	}
+
+	return ret;
+}
+
 /***********************************************************************/
 /***********************************************************************/
 /***********************************************************************/
@@ -959,6 +985,7 @@
 	return NULL;
 }
 
+
 /**********************/
 /* B 1.5.3 - Programs */
 /**********************/
@@ -1269,10 +1296,10 @@
 /* B 3.2.3 Selection Statements */
 /********************************/
 void *constant_folding_c::visit(if_statement_c *symbol) {
-	std::map <std::string, symbol_c::const_value_t> values_incoming;
-	std::map <std::string, symbol_c::const_value_t> values_statement_result;
-	std::map <std::string, symbol_c::const_value_t> values_elsestatement_result;
-	std::map <std::string, symbol_c::const_value_t>::iterator itr;
+	map_values_t values_incoming;
+	map_values_t values_statement_result;
+	map_values_t values_elsestatement_result;
+	map_values_t::iterator itr;
 
 	/* Optimize dead code */
 	symbol->expression->accept(*this);
@@ -1288,23 +1315,7 @@
 		values_elsestatement_result = values;
 	} else
 		values_elsestatement_result = values_incoming;
-	values.clear();
-	itr = values_statement_result.begin();
-	for ( ; itr != values_statement_result.end(); ++itr) {
-		std::string name = itr->first;
-		symbol_c::const_value_t value;
-
-		if (values_elsestatement_result.count(name) > 0) {
-			symbol_c::const_value_t c1 = itr->second;
-			symbol_c::const_value_t c2 = values_elsestatement_result[name];
-			COMPUTE_MEET_SEMILATTICE (real64, c1, c2, value);
-			COMPUTE_MEET_SEMILATTICE (uint64, c1, c2, value);
-			COMPUTE_MEET_SEMILATTICE ( int64, c1, c2, value);
-			COMPUTE_MEET_SEMILATTICE (  bool, c1, c2, value);
-		} else
-			value = values_statement_result[name];
-		values[name] = value;
-	}
+	values = inner_left_join_values(values_statement_result, values_elsestatement_result);
 
 	return NULL;
 }
@@ -1313,49 +1324,49 @@
 /* B 3.2.4 Iteration Statements */
 /********************************/
 void *constant_folding_c::visit(for_statement_c *symbol) {
-	std::map <std::string, symbol_c::const_value_t> values_incoming;
-	std::map <std::string, symbol_c::const_value_t> values_statement_result;
-	std::map <std::string, symbol_c::const_value_t>::iterator itr;
+	map_values_t values_incoming;
+	map_values_t values_statement_result;
+	map_values_t::iterator itr;
 	std::string varName;
 
+
 	values_incoming = values; /* save incoming status */
 	symbol->beg_expression->accept(*this);
 	symbol->end_expression->accept(*this);
 	varName =  get_var_name_c::get_name(symbol->control_variable)->value;
-	values[varName] = symbol->beg_expression->const_value;
+	values[varName]._int64.status = symbol_c::cs_non_const;
 
 	/* Optimize dead code */
-	if (VALID_CVALUE(int64, symbol->beg_expression) && VALID_CVALUE(int64, symbol->end_expression) &&
-		  GET_CVALUE(int64, symbol->beg_expression) >    GET_CVALUE(int64, symbol->end_expression))
-		return NULL;
+	if (NULL != symbol->by_expression) {
+		symbol->by_expression->accept(*this);
+		if (VALID_CVALUE(int64, symbol->by_expression ) &&   GET_CVALUE(int64, symbol->by_expression ) > 0 &&
+			VALID_CVALUE(int64, symbol->beg_expression) && VALID_CVALUE(int64, symbol->end_expression)     &&
+			  GET_CVALUE(int64, symbol->beg_expression) >    GET_CVALUE(int64, symbol->end_expression))
+			return NULL;
+
+		if (VALID_CVALUE(int64, symbol->by_expression ) &&   GET_CVALUE(int64, symbol->by_expression ) < 0 &&
+			VALID_CVALUE(int64, symbol->beg_expression) && VALID_CVALUE(int64, symbol->end_expression)    &&
+			  GET_CVALUE(int64, symbol->beg_expression) <    GET_CVALUE(int64, symbol->end_expression))
+			return NULL;
+
+	} else {
+		if (VALID_CVALUE(int64, symbol->beg_expression) && VALID_CVALUE(int64, symbol->end_expression)     &&
+			  GET_CVALUE(int64, symbol->beg_expression) >    GET_CVALUE(int64, symbol->end_expression))
+			return NULL;
+
+	}
+
 
 	symbol->statement_list->accept(*this);
 	values_statement_result = values;
-	values.clear();
-	itr = values_statement_result.begin();
-	for ( ; itr != values_statement_result.end(); ++itr) {
-		std::string name = itr->first;
-		symbol_c::const_value_t value;
-
-		if (values_incoming.count(name) > 0) {
-			symbol_c::const_value_t c1 = itr->second;
-			symbol_c::const_value_t c2 = values_incoming[name];
-			COMPUTE_MEET_SEMILATTICE (real64, c1, c2, value);
-			COMPUTE_MEET_SEMILATTICE (uint64, c1, c2, value);
-			COMPUTE_MEET_SEMILATTICE ( int64, c1, c2, value);
-			COMPUTE_MEET_SEMILATTICE (  bool, c1, c2, value);
-		} else
-			value = values_statement_result[name];
-		values[name] = value;
-	}
+	values = inner_left_join_values(values_statement_result, values_incoming);
 
 	return NULL;
 }
 
 void *constant_folding_c::visit(while_statement_c *symbol) {
-	std::map <std::string, symbol_c::const_value_t> values_incoming;
-	std::map <std::string, symbol_c::const_value_t> values_statement_result;
-	std::map <std::string, symbol_c::const_value_t>::iterator itr;
+	map_values_t values_incoming;
+	map_values_t values_statement_result;
 
 	/* Optimize dead code */
 	symbol->expression->accept(*this);
@@ -1365,32 +1376,14 @@
 	values_incoming = values; /* save incoming status */
 	symbol->statement_list->accept(*this);
 	values_statement_result = values;
-	values.clear();
-	itr = values_statement_result.begin();
-	for ( ; itr != values_statement_result.end(); ++itr) {
-		std::string name = itr->first;
-		symbol_c::const_value_t value;
-
-		if (values_incoming.count(name) > 0) {
-			symbol_c::const_value_t c1 = itr->second;
-			symbol_c::const_value_t c2 = values_incoming[name];
-			COMPUTE_MEET_SEMILATTICE (real64, c1, c2, value);
-			COMPUTE_MEET_SEMILATTICE (uint64, c1, c2, value);
-			COMPUTE_MEET_SEMILATTICE ( int64, c1, c2, value);
-			COMPUTE_MEET_SEMILATTICE (  bool, c1, c2, value);
-		} else
-			value = values_statement_result[name];
-		values[name] = value;
-	}
-
+	values = inner_left_join_values(values_statement_result, values_incoming);
 
 	return NULL;
 }
 
 void *constant_folding_c::visit(repeat_statement_c *symbol) {
-	std::map <std::string, symbol_c::const_value_t> values_incoming;
-	std::map <std::string, symbol_c::const_value_t> values_statement_result;
-	std::map <std::string, symbol_c::const_value_t>::iterator itr;
+	map_values_t values_incoming;
+	map_values_t values_statement_result;
 
 	/* Optimize dead code */
 	symbol->expression->accept(*this);
@@ -1400,27 +1393,11 @@
 	values_incoming = values; /* save incoming status */
 	symbol->statement_list->accept(*this);
 	values_statement_result = values;
-	values.clear();
-	itr = values_statement_result.begin();
-	for ( ; itr != values_statement_result.end(); ++itr) {
-		std::string name = itr->first;
-		symbol_c::const_value_t value;
-
-		if (values_incoming.count(name) > 0) {
-			symbol_c::const_value_t c1 = itr->second;
-			symbol_c::const_value_t c2 = values_incoming[name];
-			COMPUTE_MEET_SEMILATTICE (real64, c1, c2, value);
-			COMPUTE_MEET_SEMILATTICE (uint64, c1, c2, value);
-			COMPUTE_MEET_SEMILATTICE ( int64, c1, c2, value);
-			COMPUTE_MEET_SEMILATTICE (  bool, c1, c2, value);
-		} else
-			value = values_statement_result[name];
-		values[name] = value;
-	}
-
-
-	return NULL;
-}
-
-
-
+	values = inner_left_join_values(values_statement_result, values_incoming);
+
+	return NULL;
+}
+
+
+
+