Add 'widen' tables for AND, OR and XOR operations.
authorMario de Sousa <msousa@fe.up.pt>
Wed, 14 Mar 2012 10:26:03 +0000
changeset 481 16f943328696
parent 480 8a58d7b8b26c
child 482 b62a5ecce54d
Add 'widen' tables for AND, OR and XOR operations.
stage3/datatype_functions.cc
stage3/datatype_functions.hh
stage3/fill_candidate_datatypes.cc
stage3/narrow_candidate_datatypes.cc
--- a/stage3/datatype_functions.cc	Tue Mar 13 19:04:07 2012 +0000
+++ b/stage3/datatype_functions.cc	Wed Mar 14 10:26:03 2012 +0000
@@ -53,7 +53,7 @@
 #define __ANY_DERIVED(DO)
 #define __ANY_ELEMENTARY(DO)      __ANY_MAGNITUDE(DO) __ANY_BIT(DO) __ANY_STRING(DO) __ANY_DATE(DO)
 #define __ANY_MAGNITUDE(DO)       __ANY_NUM(DO) DO(TIME)
-#define __ANY_BIT(DO)             __ANY_NBIT(DO) DO(BOOL)
+#define __ANY_BIT(DO)             __ANY_NBIT(DO) DO(bool)
 #define __ANY_NBIT(DO)            DO(byte) DO(word) DO(dword) DO(lword)
 //#define __ANY_STRING(DO)          DO(string) DO(wstring)
 #define __ANY_STRING(DO)          DO(string)
@@ -294,8 +294,59 @@
 };
  
  
- 
- 
+
+
+/**************************************************************/
+/**************************************************************/
+/**************************************************************/
+/*******                                                *******/
+/*******  TABLE 26: Standard bitwise Boolean functions  *******/
+/*******                                                *******/
+/**************************************************************/
+/**************************************************************/
+/**************************************************************/
+
+const struct widen_entry widen_AND_table[] = {
+#define __and(TYPE)       \
+    { &search_constant_type_c::TYPE##_type_name,        &search_constant_type_c::TYPE##_type_name,          &search_constant_type_c::TYPE##_type_name,       widen_entry::ok     }, \
+    { &search_constant_type_c::safe##TYPE##_type_name,  &search_constant_type_c::TYPE##_type_name,          &search_constant_type_c::TYPE##_type_name,       widen_entry::ok     }, \
+    { &search_constant_type_c::TYPE##_type_name,        &search_constant_type_c::safe##TYPE##_type_name,    &search_constant_type_c::TYPE##_type_name,       widen_entry::ok     }, \
+    { &search_constant_type_c::safe##TYPE##_type_name,  &search_constant_type_c::safe##TYPE##_type_name,    &search_constant_type_c::safe##TYPE##_type_name, widen_entry::ok     },
+    __ANY_BIT(__and)
+#undef __and
+
+    { NULL, NULL, NULL, widen_entry::ok },
+};
+
+
+const struct widen_entry widen_OR_table[] = {
+#define __or(TYPE)       \
+    { &search_constant_type_c::TYPE##_type_name,        &search_constant_type_c::TYPE##_type_name,          &search_constant_type_c::TYPE##_type_name,       widen_entry::ok     }, \
+    { &search_constant_type_c::safe##TYPE##_type_name,  &search_constant_type_c::TYPE##_type_name,          &search_constant_type_c::TYPE##_type_name,       widen_entry::ok     }, \
+    { &search_constant_type_c::TYPE##_type_name,        &search_constant_type_c::safe##TYPE##_type_name,    &search_constant_type_c::TYPE##_type_name,       widen_entry::ok     }, \
+    { &search_constant_type_c::safe##TYPE##_type_name,  &search_constant_type_c::safe##TYPE##_type_name,    &search_constant_type_c::safe##TYPE##_type_name, widen_entry::ok     },
+    __ANY_BIT(__or)
+#undef __or
+
+    { NULL, NULL, NULL, widen_entry::ok },
+};
+
+
+
+const struct widen_entry widen_XOR_table[] = {
+#define __xor(TYPE)       \
+    { &search_constant_type_c::TYPE##_type_name,        &search_constant_type_c::TYPE##_type_name,          &search_constant_type_c::TYPE##_type_name,       widen_entry::ok     }, \
+    { &search_constant_type_c::safe##TYPE##_type_name,  &search_constant_type_c::TYPE##_type_name,          &search_constant_type_c::TYPE##_type_name,       widen_entry::ok     }, \
+    { &search_constant_type_c::TYPE##_type_name,        &search_constant_type_c::safe##TYPE##_type_name,    &search_constant_type_c::TYPE##_type_name,       widen_entry::ok     }, \
+    { &search_constant_type_c::safe##TYPE##_type_name,  &search_constant_type_c::safe##TYPE##_type_name,    &search_constant_type_c::safe##TYPE##_type_name, widen_entry::ok     },
+    __ANY_BIT(__xor)
+#undef __xor
+
+    { NULL, NULL, NULL, widen_entry::ok },
+};
+
+
+
 /* Search for a datatype inside a candidate_datatypes list.
  * Returns: position of datatype in the list, or -1 if not found.
  */
--- a/stage3/datatype_functions.hh	Tue Mar 13 19:04:07 2012 +0000
+++ b/stage3/datatype_functions.hh	Wed Mar 14 10:26:03 2012 +0000
@@ -133,6 +133,9 @@
 extern const struct widen_entry widen_MUL_table[];
 extern const struct widen_entry widen_DIV_table[];
 extern const struct widen_entry widen_MOD_table[];
+extern const struct widen_entry widen_AND_table[];
+extern const struct widen_entry widen_OR_table[];
+extern const struct widen_entry widen_XOR_table[];
 
 /* Search for a datatype inside a candidate_datatypes list.
  * Returns: position of datatype in the list, or -1 if not found.
--- a/stage3/fill_candidate_datatypes.cc	Tue Mar 13 19:04:07 2012 +0000
+++ b/stage3/fill_candidate_datatypes.cc	Wed Mar 14 10:26:03 2012 +0000
@@ -1106,8 +1106,12 @@
 void *fill_candidate_datatypes_c::visit(IN_operator_c  *symbol) {handle_implicit_il_fb_call(symbol,  "IN", symbol->called_fb_declaration); return NULL;}
 void *fill_candidate_datatypes_c::visit(PT_operator_c  *symbol) {handle_implicit_il_fb_call(symbol,  "PT", symbol->called_fb_declaration); return NULL;}
 
-
-void *fill_candidate_datatypes_c::visit(AND_operator_c *symbol) {
+void *fill_candidate_datatypes_c::visit(AND_operator_c *symbol) {return handle_binary_operator(widen_AND_table, symbol, prev_il_instruction, il_operand);}
+void *fill_candidate_datatypes_c::visit(OR_operator_c  *symbol) {return handle_binary_operator(widen_OR_table,  symbol, prev_il_instruction, il_operand);}
+void *fill_candidate_datatypes_c::visit(XOR_operator_c *symbol) {return handle_binary_operator(widen_XOR_table, symbol, prev_il_instruction, il_operand);}
+
+
+void *fill_candidate_datatypes_c::visit(ANDN_operator_c *symbol) {
 	symbol_c *prev_instruction_type, *operand_type;
 
 	if (NULL == prev_il_instruction) return NULL;
@@ -1120,11 +1124,11 @@
 				add_datatype_to_candidate_list(symbol, prev_instruction_type);
 		}
 	}
-	if (debug) std::cout <<  "AND [" << prev_il_instruction->candidate_datatypes.size() << "," << il_operand->candidate_datatypes.size() << "] ==> "  << symbol->candidate_datatypes.size() << " result.\n";
-	return NULL;
-}
-
-void *fill_candidate_datatypes_c::visit(OR_operator_c *symbol) {
+	if (debug) std::cout <<  "ANDN [" << prev_il_instruction->candidate_datatypes.size() << "," << il_operand->candidate_datatypes.size() << "] ==> "  << symbol->candidate_datatypes.size() << " result.\n";
+	return NULL;
+}
+
+void *fill_candidate_datatypes_c::visit(ORN_operator_c *symbol) {
 	symbol_c *prev_instruction_type, *operand_type;
 
 	if (NULL == prev_il_instruction) return NULL;
@@ -1137,11 +1141,11 @@
 				add_datatype_to_candidate_list(symbol, prev_instruction_type);
 		}
 	}
-	if (debug) std::cout <<  "OR [" << prev_il_instruction->candidate_datatypes.size() << "," << il_operand->candidate_datatypes.size() << "] ==> "  << symbol->candidate_datatypes.size() << " result.\n";
-	return NULL;
-}
-
-void *fill_candidate_datatypes_c::visit(XOR_operator_c *symbol) {
+	if (debug) std::cout <<  "ORN [" << prev_il_instruction->candidate_datatypes.size() << "," << il_operand->candidate_datatypes.size() << "] ==> "  << symbol->candidate_datatypes.size() << " result.\n";
+	return NULL;
+}
+
+void *fill_candidate_datatypes_c::visit(XORN_operator_c *symbol) {
 	symbol_c *prev_instruction_type, *operand_type;
 
 	if (NULL == prev_il_instruction) return NULL;
@@ -1154,57 +1158,6 @@
 				add_datatype_to_candidate_list(symbol, prev_instruction_type);
 		}
 	}
-	if (debug) std::cout <<  "XOR [" << prev_il_instruction->candidate_datatypes.size() << "," << il_operand->candidate_datatypes.size() << "] ==> "  << symbol->candidate_datatypes.size() << " result.\n";
-	return NULL;
-}
-
-void *fill_candidate_datatypes_c::visit(ANDN_operator_c *symbol) {
-	symbol_c *prev_instruction_type, *operand_type;
-
-	if (NULL == prev_il_instruction) return NULL;
-	for (unsigned int i = 0; i < prev_il_instruction->candidate_datatypes.size(); i++) {
-		for(unsigned int j = 0; j < il_operand->candidate_datatypes.size(); j++) {
-			prev_instruction_type = prev_il_instruction->candidate_datatypes[i];
-			operand_type = il_operand->candidate_datatypes[j];
-			if (is_type_equal(prev_instruction_type, operand_type) &&
-					is_ANY_BIT_compatible(operand_type))
-				add_datatype_to_candidate_list(symbol, prev_instruction_type);
-		}
-	}
-	if (debug) std::cout <<  "ANDN [" << prev_il_instruction->candidate_datatypes.size() << "," << il_operand->candidate_datatypes.size() << "] ==> "  << symbol->candidate_datatypes.size() << " result.\n";
-	return NULL;
-}
-
-void *fill_candidate_datatypes_c::visit(ORN_operator_c *symbol) {
-	symbol_c *prev_instruction_type, *operand_type;
-
-	if (NULL == prev_il_instruction) return NULL;
-	for (unsigned int i = 0; i < prev_il_instruction->candidate_datatypes.size(); i++) {
-		for(unsigned int j = 0; j < il_operand->candidate_datatypes.size(); j++) {
-			prev_instruction_type = prev_il_instruction->candidate_datatypes[i];
-			operand_type = il_operand->candidate_datatypes[j];
-			if (is_type_equal(prev_instruction_type, operand_type) &&
-					is_ANY_BIT_compatible(operand_type))
-				add_datatype_to_candidate_list(symbol, prev_instruction_type);
-		}
-	}
-	if (debug) std::cout <<  "ORN [" << prev_il_instruction->candidate_datatypes.size() << "," << il_operand->candidate_datatypes.size() << "] ==> "  << symbol->candidate_datatypes.size() << " result.\n";
-	return NULL;
-}
-
-void *fill_candidate_datatypes_c::visit(XORN_operator_c *symbol) {
-	symbol_c *prev_instruction_type, *operand_type;
-
-	if (NULL == prev_il_instruction) return NULL;
-	for (unsigned int i = 0; i < prev_il_instruction->candidate_datatypes.size(); i++) {
-		for(unsigned int j = 0; j < il_operand->candidate_datatypes.size(); j++) {
-			prev_instruction_type = prev_il_instruction->candidate_datatypes[i];
-			operand_type = il_operand->candidate_datatypes[j];
-			if (is_type_equal(prev_instruction_type, operand_type) &&
-					is_ANY_BIT_compatible(operand_type))
-				add_datatype_to_candidate_list(symbol, prev_instruction_type);
-		}
-	}
 	if (debug) std::cout <<  "XORN [" << prev_il_instruction->candidate_datatypes.size() << "," << il_operand->candidate_datatypes.size() << "] ==> "  << symbol->candidate_datatypes.size() << " result.\n";
 	return NULL;
 }
@@ -1417,49 +1370,9 @@
 /***********************/
 /* B 3.1 - Expressions */
 /***********************/
-
-void *fill_candidate_datatypes_c::visit(or_expression_c *symbol) {
-	symbol->l_exp->accept(*this);
-	symbol->r_exp->accept(*this);
-	for (unsigned  int i = 0; i < symbol->l_exp->candidate_datatypes.size(); i++) {
-		for (unsigned  int j = 0; j < symbol->r_exp->candidate_datatypes.size(); j++) {
-			if (is_type_equal(symbol->l_exp->candidate_datatypes[i], symbol->r_exp->candidate_datatypes[j])
-					&& is_ANY_BIT_compatible(symbol->l_exp->candidate_datatypes[i]))
-				add_datatype_to_candidate_list(symbol, symbol->l_exp->candidate_datatypes[i]);
-		}
-	}
-	return NULL;
-}
-
-
-void *fill_candidate_datatypes_c::visit(xor_expression_c *symbol) {
-	symbol->l_exp->accept(*this);
-	symbol->r_exp->accept(*this);
-
-	for (unsigned  int i = 0; i < symbol->l_exp->candidate_datatypes.size(); i++) {
-		for (unsigned  int j = 0; j < symbol->r_exp->candidate_datatypes.size(); j++) {
-			if (is_type_equal(symbol->l_exp->candidate_datatypes[i], symbol->r_exp->candidate_datatypes[j])
-					&& is_ANY_BIT_compatible(symbol->l_exp->candidate_datatypes[i]))
-				add_datatype_to_candidate_list(symbol, symbol->l_exp->candidate_datatypes[i]);
-		}
-	}
-	return NULL;
-}
-
-
-void *fill_candidate_datatypes_c::visit(and_expression_c *symbol) {
-	symbol->l_exp->accept(*this);
-	symbol->r_exp->accept(*this);
-
-	for (unsigned  int i = 0; i < symbol->l_exp->candidate_datatypes.size(); i++) {
-		for (unsigned int j = 0; j < symbol->r_exp->candidate_datatypes.size(); j++) {
-			if (is_type_equal(symbol->l_exp->candidate_datatypes[i], symbol->r_exp->candidate_datatypes[j])
-					&& is_ANY_BIT_compatible(symbol->l_exp->candidate_datatypes[i]))
-				add_datatype_to_candidate_list(symbol, symbol->l_exp->candidate_datatypes[i]);
-		}
-	}
-	return NULL;
-}
+void *fill_candidate_datatypes_c::visit(or_expression_c  *symbol) {return handle_binary_expression(widen_OR_table,  symbol, symbol->l_exp, symbol->r_exp);}
+void *fill_candidate_datatypes_c::visit(xor_expression_c *symbol) {return handle_binary_expression(widen_XOR_table, symbol, symbol->l_exp, symbol->r_exp);}
+void *fill_candidate_datatypes_c::visit(and_expression_c *symbol) {return handle_binary_expression(widen_AND_table, symbol, symbol->l_exp, symbol->r_exp);}
 
 
 void *fill_candidate_datatypes_c::visit(equ_expression_c *symbol) {
--- a/stage3/narrow_candidate_datatypes.cc	Tue Mar 13 19:04:07 2012 +0000
+++ b/stage3/narrow_candidate_datatypes.cc	Wed Mar 14 10:26:03 2012 +0000
@@ -911,9 +911,9 @@
 void *narrow_candidate_datatypes_c::visit(  PT_operator_c *symbol)  {return narrow_implicit_il_fb_call(symbol, "PT",  symbol->called_fb_declaration);}
 
 
-void *narrow_candidate_datatypes_c::visit( AND_operator_c *symbol)  {return handle_il_instruction(symbol);}
-void *narrow_candidate_datatypes_c::visit(  OR_operator_c *symbol)  {return handle_il_instruction(symbol);}
-void *narrow_candidate_datatypes_c::visit( XOR_operator_c *symbol)  {return handle_il_instruction(symbol);}
+void *narrow_candidate_datatypes_c::visit( AND_operator_c *symbol)  {return narrow_binary_operator(widen_AND_table, symbol);}
+void *narrow_candidate_datatypes_c::visit(  OR_operator_c *symbol)  {return narrow_binary_operator( widen_OR_table, symbol);}
+void *narrow_candidate_datatypes_c::visit( XOR_operator_c *symbol)  {return narrow_binary_operator(widen_XOR_table, symbol);}
 void *narrow_candidate_datatypes_c::visit(ANDN_operator_c *symbol)  {return handle_il_instruction(symbol);}
 void *narrow_candidate_datatypes_c::visit( ORN_operator_c *symbol)  {return handle_il_instruction(symbol);}
 void *narrow_candidate_datatypes_c::visit(XORN_operator_c *symbol)  {return handle_il_instruction(symbol);}
@@ -1036,73 +1036,10 @@
 }
 
 
-void *narrow_candidate_datatypes_c::visit(or_expression_c *symbol) {
-	symbol_c * selected_type = NULL;
-	for(unsigned int i = 0; i < symbol->l_exp->candidate_datatypes.size(); i++) {
-		for(unsigned int j = 0; j < symbol->r_exp->candidate_datatypes.size(); j++) {
-			if (is_type_equal(symbol->l_exp->candidate_datatypes[i], symbol->r_exp->candidate_datatypes[j])) {
-				selected_type = symbol->l_exp->candidate_datatypes[i];
-				break;
-			}
-		}
-	}
-
-	if (NULL != selected_type) {
-		symbol->l_exp->datatype = selected_type;
-		symbol->l_exp->accept(*this);
-		symbol->r_exp->datatype = selected_type;
-		symbol->r_exp->accept(*this);
-	}
-	else
-		ERROR;
-	return NULL;
-}
-
-
-void *narrow_candidate_datatypes_c::visit(xor_expression_c *symbol) {
-	symbol_c * selected_type = NULL;
-	for(unsigned int i = 0; i < symbol->l_exp->candidate_datatypes.size(); i++) {
-		for(unsigned int j = 0; j < symbol->r_exp->candidate_datatypes.size(); j++) {
-			if (is_type_equal(symbol->l_exp->candidate_datatypes[i], symbol->r_exp->candidate_datatypes[j])) {
-				selected_type = symbol->l_exp->candidate_datatypes[i];
-				break;
-			}
-		}
-	}
-
-	if (NULL != selected_type) {
-		symbol->l_exp->datatype = selected_type;
-		symbol->l_exp->accept(*this);
-		symbol->r_exp->datatype = selected_type;
-		symbol->r_exp->accept(*this);
-	}
-	else
-		ERROR;
-	return NULL;
-}
-
-
-void *narrow_candidate_datatypes_c::visit(and_expression_c *symbol) {
-	symbol_c * selected_type = NULL;
-	for(unsigned int i = 0; i < symbol->l_exp->candidate_datatypes.size(); i++) {
-		for(unsigned int j = 0; j < symbol->r_exp->candidate_datatypes.size(); j++) {
-			if (typeid(*symbol->l_exp->candidate_datatypes[i]) == typeid(*symbol->r_exp->candidate_datatypes[j])) {
-				selected_type = symbol->l_exp->candidate_datatypes[i];
-				break;
-			}
-		}
-	}
-
-	if (NULL != selected_type) {
-		symbol->l_exp->datatype = selected_type;
-		symbol->l_exp->accept(*this);
-		symbol->r_exp->datatype = selected_type;
-		symbol->r_exp->accept(*this);
-	}
-	else
-		ERROR;
-	return NULL;
-}
+
+void *narrow_candidate_datatypes_c::visit( or_expression_c *symbol) {return narrow_binary_expression( widen_OR_table, symbol, symbol->l_exp, symbol->r_exp);}
+void *narrow_candidate_datatypes_c::visit(xor_expression_c *symbol) {return narrow_binary_expression(widen_XOR_table, symbol, symbol->l_exp, symbol->r_exp);}
+void *narrow_candidate_datatypes_c::visit(and_expression_c *symbol) {return narrow_binary_expression(widen_AND_table, symbol, symbol->l_exp, symbol->r_exp);}
 
 
 void *narrow_candidate_datatypes_c::visit(equ_expression_c *symbol) {