stage3/narrow_candidate_datatypes.cc
changeset 478 b13feab3b918
parent 472 d26759a2274a
child 480 8a58d7b8b26c
equal deleted inserted replaced
477:c0c6ff7357b7 478:b13feab3b918
    93 		set_datatype(datatype, symbol->prev_il_instruction[i]);
    93 		set_datatype(datatype, symbol->prev_il_instruction[i]);
    94 }
    94 }
    95 
    95 
    96 
    96 
    97 
    97 
    98 bool narrow_candidate_datatypes_c::is_widening_compatible(symbol_c *left_type, symbol_c *right_type, symbol_c *result_type, const struct widen_entry widen_table[]) {
    98 bool narrow_candidate_datatypes_c::is_widening_compatible(const struct widen_entry widen_table[], symbol_c *left_type, symbol_c *right_type, symbol_c *result_type, bool &deprecated_status) {
    99 	for (int k = 0; NULL != widen_table[k].left;  k++) {
    99 	for (int k = 0; NULL != widen_table[k].left;  k++) {
   100 		if        ((typeid(*left_type)   == typeid(*widen_table[k].left))
   100 		if        ((typeid(*left_type)   == typeid(*widen_table[k].left))
   101 		        && (typeid(*right_type)  == typeid(*widen_table[k].right))
   101 		        && (typeid(*right_type)  == typeid(*widen_table[k].right))
   102 				&& (typeid(*result_type) == typeid(*widen_table[k].result))) {
   102 			&& (typeid(*result_type) == typeid(*widen_table[k].result))) {
       
   103 			deprecated_status = (widen_table[k].status == widen_entry::deprecated);
   103 			return true;
   104 			return true;
   104 		}
   105 		}
   105 	}
   106 	}
   106 	return false;
   107 	return false;
   107 }
   108 }
   747 
   748 
   748 
   749 
   749 /*******************/
   750 /*******************/
   750 /* B 2.2 Operators */
   751 /* B 2.2 Operators */
   751 /*******************/
   752 /*******************/
       
   753 void *narrow_candidate_datatypes_c::handle_il_instruction_widen(symbol_c *symbol, bool &deprecated_operation, const struct widen_entry widen_table[]) {
       
   754 	symbol_c *prev_instruction_type, *operand_type;
       
   755 	int count = 0;
       
   756 	bool deprecated_status;
       
   757 
       
   758 	if (NULL == symbol->datatype)
       
   759 		/* next IL instructions were unable to determine the datatype this instruction should produce */
       
   760 		return NULL;
       
   761 
       
   762 	/* NOTE 1: the il_operand __may__ be pointing to a parenthesized list of IL instructions. 
       
   763 	 * e.g.  LD 33
       
   764 	 *       AND ( 45
       
   765 	 *            OR 56
       
   766 	 *            )
       
   767 	 *       When we handle the first 'AND' IL_operator, the il_operand will point to an simple_instr_list_c.
       
   768 	 *       In this case, when we call il_operand->accept(*this);, the prev_il_instruction pointer will be overwritten!
       
   769 	 *
       
   770 	 *       We must therefore set the prev_il_instruction->datatype = symbol->datatype;
       
   771 	 *       __before__ calling il_operand->accept(*this) !!
       
   772 	 *
       
   773 	 * NOTE 2: We do not need to call prev_il_instruction->accept(*this), as the object to which prev_il_instruction
       
   774 	 *         is pointing to will be later narrowed by the call from the for() loop of the instruction_list_c
       
   775 	 *         (or simple_instr_list_c), which iterates backwards.
       
   776 	 */
       
   777         deprecated_operation = false;
       
   778 	for(unsigned int i = 0; i < fake_prev_il_instruction->candidate_datatypes.size(); i++) {
       
   779 		for(unsigned int j = 0; j < il_operand->candidate_datatypes.size(); j++) {
       
   780 			prev_instruction_type = fake_prev_il_instruction->candidate_datatypes[i];
       
   781 			operand_type = il_operand->candidate_datatypes[j];
       
   782 			if (is_widening_compatible(widen_table, prev_instruction_type, operand_type, symbol->datatype, deprecated_operation)) {
       
   783 				/* set the desired datatype of the previous il instruction */
       
   784 				set_datatype_in_prev_il_instructions(prev_instruction_type, fake_prev_il_instruction);
       
   785 				/* set the datatype for the operand */
       
   786 				il_operand->datatype = operand_type;
       
   787 				
       
   788 				count ++;
       
   789 			}
       
   790 		}
       
   791 	}
       
   792 
       
   793 // 	if (count > 1) ERROR; /* Since we also support SAFE data types, this assertion is not necessarily always tru! */
       
   794 	if (is_type_valid(symbol->datatype) && (count <= 0)) ERROR;
       
   795 
       
   796 	il_operand->accept(*this);
       
   797 	return NULL;
       
   798 }
       
   799 
       
   800 
   752 
   801 
   753 void *narrow_candidate_datatypes_c::handle_il_instruction(symbol_c *symbol) {
   802 void *narrow_candidate_datatypes_c::handle_il_instruction(symbol_c *symbol) {
   754 	if (NULL == symbol->datatype)
   803 	if (NULL == symbol->datatype)
   755 		/* next IL instructions were unable to determine the datatype this instruction should produce */
   804 		/* next IL instructions were unable to determine the datatype this instruction should produce */
   756 		return NULL;
   805 		return NULL;
   776 	il_operand->datatype = symbol->datatype;
   825 	il_operand->datatype = symbol->datatype;
   777 	il_operand->accept(*this);
   826 	il_operand->accept(*this);
   778 	return NULL;
   827 	return NULL;
   779 }
   828 }
   780 
   829 
       
   830 
       
   831 
       
   832 
   781 void *narrow_candidate_datatypes_c::visit(LD_operator_c *symbol)   {
   833 void *narrow_candidate_datatypes_c::visit(LD_operator_c *symbol)   {
   782 	if (NULL == symbol->datatype)
   834 	if (NULL == symbol->datatype)
   783 		/* next IL instructions were unable to determine the datatype this instruction should produce */
   835 		/* next IL instructions were unable to determine the datatype this instruction should produce */
   784 		return NULL;
   836 		return NULL;
   785 	/* set the datatype for the operand */
   837 	/* set the datatype for the operand */
   860 void *narrow_candidate_datatypes_c::visit(  OR_operator_c *symbol)  {return handle_il_instruction(symbol);}
   912 void *narrow_candidate_datatypes_c::visit(  OR_operator_c *symbol)  {return handle_il_instruction(symbol);}
   861 void *narrow_candidate_datatypes_c::visit( XOR_operator_c *symbol)  {return handle_il_instruction(symbol);}
   913 void *narrow_candidate_datatypes_c::visit( XOR_operator_c *symbol)  {return handle_il_instruction(symbol);}
   862 void *narrow_candidate_datatypes_c::visit(ANDN_operator_c *symbol)  {return handle_il_instruction(symbol);}
   914 void *narrow_candidate_datatypes_c::visit(ANDN_operator_c *symbol)  {return handle_il_instruction(symbol);}
   863 void *narrow_candidate_datatypes_c::visit( ORN_operator_c *symbol)  {return handle_il_instruction(symbol);}
   915 void *narrow_candidate_datatypes_c::visit( ORN_operator_c *symbol)  {return handle_il_instruction(symbol);}
   864 void *narrow_candidate_datatypes_c::visit(XORN_operator_c *symbol)  {return handle_il_instruction(symbol);}
   916 void *narrow_candidate_datatypes_c::visit(XORN_operator_c *symbol)  {return handle_il_instruction(symbol);}
   865 void *narrow_candidate_datatypes_c::visit( ADD_operator_c *symbol)  {return handle_il_instruction(symbol);}
   917 void *narrow_candidate_datatypes_c::visit( ADD_operator_c *symbol)  {return handle_il_instruction_widen(symbol, symbol->deprecated_operation, widen_ADD_table);}
   866 void *narrow_candidate_datatypes_c::visit( SUB_operator_c *symbol)  {return handle_il_instruction(symbol);}
   918 void *narrow_candidate_datatypes_c::visit( SUB_operator_c *symbol)  {return handle_il_instruction_widen(symbol, symbol->deprecated_operation, widen_SUB_table);}
   867 void *narrow_candidate_datatypes_c::visit( MUL_operator_c *symbol)  {return handle_il_instruction(symbol);}
   919 void *narrow_candidate_datatypes_c::visit( MUL_operator_c *symbol)  {return handle_il_instruction_widen(symbol, symbol->deprecated_operation, widen_MUL_table);}
   868 void *narrow_candidate_datatypes_c::visit( DIV_operator_c *symbol)  {return handle_il_instruction(symbol);}
   920 void *narrow_candidate_datatypes_c::visit( DIV_operator_c *symbol)  {return handle_il_instruction_widen(symbol, symbol->deprecated_operation, widen_DIV_table);}
   869 void *narrow_candidate_datatypes_c::visit( MOD_operator_c *symbol)  {return handle_il_instruction(symbol);}
   921 void *narrow_candidate_datatypes_c::visit( MOD_operator_c *symbol)  {return handle_il_instruction(symbol);}
   870 void *narrow_candidate_datatypes_c::visit(  GT_operator_c *symbol)  {return handle_il_instruction(symbol);}
   922 void *narrow_candidate_datatypes_c::visit(  GT_operator_c *symbol)  {return handle_il_instruction(symbol);}
   871 void *narrow_candidate_datatypes_c::visit(  GE_operator_c *symbol)  {return handle_il_instruction(symbol);}
   923 void *narrow_candidate_datatypes_c::visit(  GE_operator_c *symbol)  {return handle_il_instruction(symbol);}
   872 void *narrow_candidate_datatypes_c::visit(  EQ_operator_c *symbol)  {return handle_il_instruction(symbol);}
   924 void *narrow_candidate_datatypes_c::visit(  EQ_operator_c *symbol)  {return handle_il_instruction(symbol);}
   873 void *narrow_candidate_datatypes_c::visit(  LT_operator_c *symbol)  {return handle_il_instruction(symbol);}
   925 void *narrow_candidate_datatypes_c::visit(  LT_operator_c *symbol)  {return handle_il_instruction(symbol);}
  1176 		ERROR;
  1228 		ERROR;
  1177 	return NULL;
  1229 	return NULL;
  1178 }
  1230 }
  1179 
  1231 
  1180 void *narrow_candidate_datatypes_c::visit(add_expression_c *symbol) {
  1232 void *narrow_candidate_datatypes_c::visit(add_expression_c *symbol) {
       
  1233 	symbol_c *left_type, *right_type;
  1181 	int count = 0;
  1234 	int count = 0;
  1182 
  1235 	bool deprecated_status;
  1183 	if (is_ANY_NUM_compatible(symbol->datatype)) {
  1236 
  1184 		symbol->l_exp->datatype = symbol->datatype;
  1237 	for(unsigned int i = 0; i < symbol->l_exp->candidate_datatypes.size(); i++) {
  1185 		symbol->r_exp->datatype = symbol->datatype;
  1238 		for(unsigned int j = 0; j < symbol->r_exp->candidate_datatypes.size(); j++) {
  1186 		count++;
  1239 			/* test widening compatibility */
  1187 	} else {
  1240 			left_type  = symbol->l_exp->candidate_datatypes[i];
  1188 		/* TIME data type */
  1241 			right_type = symbol->r_exp->candidate_datatypes[j];
  1189 		for(unsigned int i = 0; i < symbol->l_exp->candidate_datatypes.size(); i++) {
  1242 			if (is_widening_compatible(widen_ADD_table, left_type, right_type, symbol->datatype, deprecated_status)) {
  1190 			for(unsigned int j = 0; j < symbol->r_exp->candidate_datatypes.size(); j++) {
  1243 				symbol->l_exp->datatype = left_type;
  1191 				/* test widening compatibility */
  1244 				symbol->r_exp->datatype = right_type;
  1192 				if (is_widening_compatible(symbol->l_exp->candidate_datatypes[i],
  1245 				symbol->deprecated_operation = deprecated_status;
  1193 						symbol->r_exp->candidate_datatypes[j],
  1246 				count ++;
  1194 						symbol->datatype, widen_ADD_table)) {
       
  1195 					symbol->l_exp->datatype = symbol->l_exp->candidate_datatypes[i];
       
  1196 					symbol->r_exp->datatype = symbol->r_exp->candidate_datatypes[j];
       
  1197 					count ++;
       
  1198 				}
       
  1199 			}
  1247 			}
  1200 		}
  1248 		}
  1201 	}
  1249 	}
  1202 	if (count > 1)
  1250 // 	if (count > 1) ERROR; /* Since we also support SAFE data types, this assertion is not necessarily always tru! */
  1203 		ERROR;
  1251 	if (is_type_valid(symbol->datatype) && (count <= 0)) ERROR;
       
  1252 	
  1204 	symbol->l_exp->accept(*this);
  1253 	symbol->l_exp->accept(*this);
  1205 	symbol->r_exp->accept(*this);
  1254 	symbol->r_exp->accept(*this);
  1206 	return NULL;
  1255 	return NULL;
  1207 }
  1256 }
  1208 
  1257 
  1209 
  1258 
  1210 
  1259 
  1211 void *narrow_candidate_datatypes_c::visit(sub_expression_c *symbol) {
  1260 void *narrow_candidate_datatypes_c::visit(sub_expression_c *symbol) {
       
  1261 	symbol_c *left_type, *right_type;
  1212 	int count = 0;
  1262 	int count = 0;
  1213 
  1263 	bool deprecated_status;
  1214 	if (is_ANY_NUM_compatible(symbol->datatype)) {
  1264 
  1215 		symbol->l_exp->datatype = symbol->datatype;
  1265 	for(unsigned int i = 0; i < symbol->l_exp->candidate_datatypes.size(); i++) {
  1216 		symbol->r_exp->datatype = symbol->datatype;
  1266 		for(unsigned int j = 0; j < symbol->r_exp->candidate_datatypes.size(); j++) {
  1217 		count++;
  1267 			/* test widening compatibility */
  1218 	} else {
  1268 			left_type  = symbol->l_exp->candidate_datatypes[i];
  1219 		/* TIME data type */
  1269 			right_type = symbol->r_exp->candidate_datatypes[j];
  1220 		for(unsigned int i = 0; i < symbol->l_exp->candidate_datatypes.size(); i++) {
  1270 			if (is_widening_compatible(widen_SUB_table, left_type, right_type, symbol->datatype, deprecated_status)) {
  1221 			for(unsigned int j = 0; j < symbol->r_exp->candidate_datatypes.size(); j++) {
  1271 				symbol->l_exp->datatype = left_type;
  1222 				/* test widening compatibility */
  1272 				symbol->r_exp->datatype = right_type;
  1223 				if (is_widening_compatible(symbol->l_exp->candidate_datatypes[i],
  1273 				symbol->deprecated_operation = deprecated_status;
  1224 						symbol->r_exp->candidate_datatypes[j],
  1274 				count ++;
  1225 						symbol->datatype, widen_SUB_table)) {
       
  1226 					symbol->l_exp->datatype = symbol->l_exp->candidate_datatypes[i];
       
  1227 					symbol->r_exp->datatype = symbol->r_exp->candidate_datatypes[j];
       
  1228 					count ++;
       
  1229 				}
       
  1230 			}
  1275 			}
  1231 		}
  1276 		}
  1232 	}
  1277 	}
  1233 	if (count > 1)
  1278 // 	if (count > 1) ERROR; /* Since we also support SAFE data types, this assertion is not necessarily always tru! */
  1234 		ERROR;
  1279 	if (is_type_valid(symbol->datatype) && (count <= 0)) ERROR;
       
  1280 	
  1235 	symbol->l_exp->accept(*this);
  1281 	symbol->l_exp->accept(*this);
  1236 	symbol->r_exp->accept(*this);
  1282 	symbol->r_exp->accept(*this);
  1237 	return NULL;
  1283 	return NULL;
  1238 }
  1284 }
  1239 
  1285 
       
  1286 
       
  1287 
  1240 void *narrow_candidate_datatypes_c::visit(mul_expression_c *symbol) {
  1288 void *narrow_candidate_datatypes_c::visit(mul_expression_c *symbol) {
       
  1289 	symbol_c *left_type, *right_type;
  1241 	int count = 0;
  1290 	int count = 0;
  1242 
  1291 	bool deprecated_status;
  1243 	if (is_ANY_NUM_compatible(symbol->datatype)) {
  1292 
  1244 		symbol->l_exp->datatype = symbol->datatype;
  1293 	for(unsigned int i = 0; i < symbol->l_exp->candidate_datatypes.size(); i++) {
  1245 		symbol->r_exp->datatype = symbol->datatype;
  1294 		for(unsigned int j = 0; j < symbol->r_exp->candidate_datatypes.size(); j++) {
  1246 		count++;
  1295 			/* test widening compatibility */
  1247 	} else {
  1296 			left_type  = symbol->l_exp->candidate_datatypes[i];
  1248 		/* TIME data type */
  1297 			right_type = symbol->r_exp->candidate_datatypes[j];
  1249 		for(unsigned int i = 0; i < symbol->l_exp->candidate_datatypes.size(); i++) {
  1298 			if (is_widening_compatible(widen_MUL_table, left_type, right_type, symbol->datatype, deprecated_status)) {
  1250 			for(unsigned int j = 0; j < symbol->r_exp->candidate_datatypes.size(); j++) {
  1299 				symbol->l_exp->datatype = left_type;
  1251 				/* test widening compatibility */
  1300 				symbol->r_exp->datatype = right_type;
  1252 				if (is_widening_compatible(symbol->l_exp->candidate_datatypes[i],
  1301 				symbol->deprecated_operation = deprecated_status;
  1253 						symbol->r_exp->candidate_datatypes[j],
  1302 				count ++;
  1254 						symbol->datatype, widen_MUL_table)) {
       
  1255 					symbol->l_exp->datatype = symbol->l_exp->candidate_datatypes[i];
       
  1256 					symbol->r_exp->datatype = symbol->r_exp->candidate_datatypes[j];
       
  1257 					count ++;
       
  1258 				}
       
  1259 			}
  1303 			}
  1260 		}
  1304 		}
  1261 	}
  1305 	}
  1262 	if (count > 1)
  1306 // 	if (count > 1) ERROR; /* Since we also support SAFE data types, this assertion is not necessarily always tru! */
  1263 		ERROR;
  1307 	if (is_type_valid(symbol->datatype) && (count <= 0)) ERROR;
       
  1308 	
  1264 	symbol->l_exp->accept(*this);
  1309 	symbol->l_exp->accept(*this);
  1265 	symbol->r_exp->accept(*this);
  1310 	symbol->r_exp->accept(*this);
  1266 	return NULL;
  1311 	return NULL;
  1267 }
  1312 }
  1268 
  1313 
       
  1314 
       
  1315 
       
  1316 
  1269 void *narrow_candidate_datatypes_c::visit(div_expression_c *symbol) {
  1317 void *narrow_candidate_datatypes_c::visit(div_expression_c *symbol) {
       
  1318 	symbol_c *left_type, *right_type;
  1270 	int count = 0;
  1319 	int count = 0;
  1271 
  1320 	bool deprecated_status;
  1272 	if (is_ANY_NUM_compatible(symbol->datatype)) {
  1321 
  1273 		symbol->l_exp->datatype = symbol->datatype;
  1322 	for(unsigned int i = 0; i < symbol->l_exp->candidate_datatypes.size(); i++) {
  1274 		symbol->r_exp->datatype = symbol->datatype;
  1323 		for(unsigned int j = 0; j < symbol->r_exp->candidate_datatypes.size(); j++) {
  1275 		count++;
  1324 			/* test widening compatibility */
  1276 	} else {
  1325 			left_type  = symbol->l_exp->candidate_datatypes[i];
  1277 		/* TIME data type */
  1326 			right_type = symbol->r_exp->candidate_datatypes[j];
  1278 		for(unsigned int i = 0; i < symbol->l_exp->candidate_datatypes.size(); i++) {
  1327 			if (is_widening_compatible(widen_DIV_table, left_type, right_type, symbol->datatype, deprecated_status)) {
  1279 			for(unsigned int j = 0; j < symbol->r_exp->candidate_datatypes.size(); j++) {
  1328 				symbol->l_exp->datatype = left_type;
  1280 				/* test widening compatibility */
  1329 				symbol->r_exp->datatype = right_type;
  1281 				if (is_widening_compatible(symbol->l_exp->candidate_datatypes[i],
  1330 				symbol->deprecated_operation = deprecated_status;
  1282 						symbol->r_exp->candidate_datatypes[j],
  1331 				count ++;
  1283 						symbol->datatype, widen_DIV_table)) {
       
  1284 					symbol->l_exp->datatype = symbol->l_exp->candidate_datatypes[i];
       
  1285 					symbol->r_exp->datatype = symbol->r_exp->candidate_datatypes[j];
       
  1286 					count ++;
       
  1287 				}
       
  1288 			}
  1332 			}
  1289 		}
  1333 		}
  1290 	}
  1334 	}
  1291 	if (count > 1)
  1335 // 	if (count > 1) ERROR; /* Since we also support SAFE data types, this assertion is not necessarily always tru! */
  1292 		ERROR;
  1336 	if (is_type_valid(symbol->datatype) && (count <= 0)) ERROR;
       
  1337 	
  1293 	symbol->l_exp->accept(*this);
  1338 	symbol->l_exp->accept(*this);
  1294 	symbol->r_exp->accept(*this);
  1339 	symbol->r_exp->accept(*this);
  1295 	return NULL;
  1340 	return NULL;
  1296 }
  1341 }
       
  1342 
       
  1343 
  1297 
  1344 
  1298 
  1345 
  1299 void *narrow_candidate_datatypes_c::visit(mod_expression_c *symbol) {
  1346 void *narrow_candidate_datatypes_c::visit(mod_expression_c *symbol) {
  1300 	symbol->l_exp->datatype = symbol->datatype;
  1347 	symbol->l_exp->datatype = symbol->datatype;
  1301 	symbol->l_exp->accept(*this);
  1348 	symbol->l_exp->accept(*this);