stage3/constant_folding.cc
changeset 791 ab601bdea102
parent 786 c370918ca7fb
parent 790 a722594dcd64
child 792 78083edf93d5
equal deleted inserted replaced
786:c370918ca7fb 791:ab601bdea102
   234 			resValue._##dtype.status = symbol_c::cs_const_value;\
   234 			resValue._##dtype.status = symbol_c::cs_const_value;\
   235 			resValue._##dtype.value  = c1._##dtype.value;\
   235 			resValue._##dtype.value  = c1._##dtype.value;\
   236 		}\
   236 		}\
   237 }
   237 }
   238 
   238 
   239 
   239 typedef std::map <std::string, symbol_c::const_value_t> map_values_t;
   240 static std::map <std::string, symbol_c::const_value_t> values;
   240 
       
   241 static map_values_t values;
       
   242 
   241 
   243 
   242 
   244 
   243 /***********************************************************************/
   245 /***********************************************************************/
   244 /***********************************************************************/
   246 /***********************************************************************/
   245 /***********************************************************************/
   247 /***********************************************************************/
   721 		SET_CVALUE(real64, symbol, pow(GET_CVALUE(real64, oper1), GET_CVALUE( int64, oper2)));
   723 		SET_CVALUE(real64, symbol, pow(GET_CVALUE(real64, oper1), GET_CVALUE( int64, oper2)));
   722 	if (VALID_CVALUE(real64, oper1) && VALID_CVALUE(uint64, oper2))
   724 	if (VALID_CVALUE(real64, oper1) && VALID_CVALUE(uint64, oper2))
   723 		SET_CVALUE(real64, symbol, pow(GET_CVALUE(real64, oper1), GET_CVALUE(uint64, oper2)));
   725 		SET_CVALUE(real64, symbol, pow(GET_CVALUE(real64, oper1), GET_CVALUE(uint64, oper2)));
   724 	CHECK_OVERFLOW_real64(symbol);
   726 	CHECK_OVERFLOW_real64(symbol);
   725 	return NULL;
   727 	return NULL;
       
   728 }
       
   729 
       
   730 static map_values_t inner_left_join_values(map_values_t m1, map_values_t m2) {
       
   731 	map_values_t::const_iterator itr;
       
   732 	map_values_t ret;
       
   733 
       
   734 	itr = m1.begin();
       
   735 	for ( ; itr != m1.end(); ++itr) {
       
   736 		std::string name = itr->first;
       
   737 		symbol_c::const_value_t value;
       
   738 
       
   739 		if (m2.count(name) > 0) {
       
   740 			symbol_c::const_value_t c1 = itr->second;
       
   741 			symbol_c::const_value_t c2 = m2[name];
       
   742 			COMPUTE_MEET_SEMILATTICE (real64, c1, c2, value);
       
   743 			COMPUTE_MEET_SEMILATTICE (uint64, c1, c2, value);
       
   744 			COMPUTE_MEET_SEMILATTICE ( int64, c1, c2, value);
       
   745 			COMPUTE_MEET_SEMILATTICE (  bool, c1, c2, value);
       
   746 		} else
       
   747 			value = m1[name];
       
   748 		ret[name] = value;
       
   749 	}
       
   750 
       
   751 	return ret;
   726 }
   752 }
   727 
   753 
   728 /***********************************************************************/
   754 /***********************************************************************/
   729 /***********************************************************************/
   755 /***********************************************************************/
   730 /***********************************************************************/
   756 /***********************************************************************/
   959 		symbol->const_value = values[varName];
   985 		symbol->const_value = values[varName];
   960 	}
   986 	}
   961 	return NULL;
   987 	return NULL;
   962 }
   988 }
   963 
   989 
       
   990 
   964 /**********************/
   991 /**********************/
   965 /* B 1.5.3 - Programs */
   992 /* B 1.5.3 - Programs */
   966 /**********************/
   993 /**********************/
   967 void *constant_folding_c::visit(program_declaration_c *symbol) {
   994 void *constant_folding_c::visit(program_declaration_c *symbol) {
   968 	symbol_c *var_name;
   995 	symbol_c *var_name;
   969 
   996 
       
   997 	symbol->var_declarations->accept(*this);
   970 	values.clear(); /* Clear global map */
   998 	values.clear(); /* Clear global map */
   971 	search_var_instance_decl_c search_var_instance_decl(symbol);
   999 	search_var_instance_decl_c search_var_instance_decl(symbol);
   972 	function_param_iterator_c fpi(symbol);
  1000 	function_param_iterator_c fpi(symbol);
   973 	while((var_name = fpi.next()) != NULL) {
  1001 	while((var_name = fpi.next()) != NULL) {
   974 		std::string varName = get_var_name_c::get_name(var_name)->value;
  1002 		std::string varName = get_var_name_c::get_name(var_name)->value;
  1268 
  1296 
  1269 /********************************/
  1297 /********************************/
  1270 /* B 3.2.3 Selection Statements */
  1298 /* B 3.2.3 Selection Statements */
  1271 /********************************/
  1299 /********************************/
  1272 void *constant_folding_c::visit(if_statement_c *symbol) {
  1300 void *constant_folding_c::visit(if_statement_c *symbol) {
  1273 	std::map <std::string, symbol_c::const_value_t> values_incoming;
  1301 	map_values_t values_incoming;
  1274 	std::map <std::string, symbol_c::const_value_t> values_statement_result;
  1302 	map_values_t values_statement_result;
  1275 	std::map <std::string, symbol_c::const_value_t> values_elsestatement_result;
  1303 	map_values_t values_elsestatement_result;
  1276 	std::map <std::string, symbol_c::const_value_t>::iterator itr;
  1304 	map_values_t::iterator itr;
  1277 
  1305 
  1278 	/* Optimize dead code */
  1306 	/* Optimize dead code */
  1279 	symbol->expression->accept(*this);
  1307 	symbol->expression->accept(*this);
  1280 	if (VALID_CVALUE(bool, symbol->expression) && GET_CVALUE(bool, symbol->expression) == false)
  1308 	if (VALID_CVALUE(bool, symbol->expression) && GET_CVALUE(bool, symbol->expression) == false)
  1281 		return NULL;
  1309 		return NULL;
  1287 		values = values_incoming;
  1315 		values = values_incoming;
  1288 		symbol->else_statement_list->accept(*this);
  1316 		symbol->else_statement_list->accept(*this);
  1289 		values_elsestatement_result = values;
  1317 		values_elsestatement_result = values;
  1290 	} else
  1318 	} else
  1291 		values_elsestatement_result = values_incoming;
  1319 		values_elsestatement_result = values_incoming;
  1292 	values.clear();
  1320 	values = inner_left_join_values(values_statement_result, values_elsestatement_result);
  1293 	itr = values_statement_result.begin();
       
  1294 	for ( ; itr != values_statement_result.end(); ++itr) {
       
  1295 		std::string name = itr->first;
       
  1296 		symbol_c::const_value_t value;
       
  1297 
       
  1298 		if (values_elsestatement_result.count(name) > 0) {
       
  1299 			symbol_c::const_value_t c1 = itr->second;
       
  1300 			symbol_c::const_value_t c2 = values_elsestatement_result[name];
       
  1301 			COMPUTE_MEET_SEMILATTICE (real64, c1, c2, value);
       
  1302 			COMPUTE_MEET_SEMILATTICE (uint64, c1, c2, value);
       
  1303 			COMPUTE_MEET_SEMILATTICE ( int64, c1, c2, value);
       
  1304 			COMPUTE_MEET_SEMILATTICE (  bool, c1, c2, value);
       
  1305 		} else
       
  1306 			value = values_statement_result[name];
       
  1307 		values[name] = value;
       
  1308 	}
       
  1309 
  1321 
  1310 	return NULL;
  1322 	return NULL;
  1311 }
  1323 }
  1312 
  1324 
  1313 /********************************/
  1325 /********************************/
  1314 /* B 3.2.4 Iteration Statements */
  1326 /* B 3.2.4 Iteration Statements */
  1315 /********************************/
  1327 /********************************/
  1316 void *constant_folding_c::visit(for_statement_c *symbol) {
  1328 void *constant_folding_c::visit(for_statement_c *symbol) {
  1317 	std::map <std::string, symbol_c::const_value_t> values_incoming;
  1329 	map_values_t values_incoming;
  1318 	std::map <std::string, symbol_c::const_value_t> values_statement_result;
  1330 	map_values_t values_statement_result;
  1319 	std::map <std::string, symbol_c::const_value_t>::iterator itr;
       
  1320 	std::string varName;
  1331 	std::string varName;
  1321 
  1332 
  1322 	values_incoming = values; /* save incoming status */
  1333 	values_incoming = values; /* save incoming status */
  1323 	symbol->beg_expression->accept(*this);
  1334 	symbol->beg_expression->accept(*this);
  1324 	symbol->end_expression->accept(*this);
  1335 	symbol->end_expression->accept(*this);
  1325 	varName =  get_var_name_c::get_name(symbol->control_variable)->value;
  1336 	varName =  get_var_name_c::get_name(symbol->control_variable)->value;
  1326 	values[varName] = symbol->beg_expression->const_value;
  1337 	values[varName]._int64.status = symbol_c::cs_non_const;
  1327 
  1338 
  1328 	/* Optimize dead code */
  1339 	/* Optimize dead code */
  1329 	if (VALID_CVALUE(int64, symbol->beg_expression) && VALID_CVALUE(int64, symbol->end_expression) &&
  1340 	if (NULL != symbol->by_expression) {
  1330 		  GET_CVALUE(int64, symbol->beg_expression) >    GET_CVALUE(int64, symbol->end_expression))
  1341 		symbol->by_expression->accept(*this);
  1331 		return NULL;
  1342 		if (VALID_CVALUE(int64, symbol->by_expression ) &&   GET_CVALUE(int64, symbol->by_expression ) > 0 &&
       
  1343 			VALID_CVALUE(int64, symbol->beg_expression) && VALID_CVALUE(int64, symbol->end_expression)     &&
       
  1344 			  GET_CVALUE(int64, symbol->beg_expression) >    GET_CVALUE(int64, symbol->end_expression))
       
  1345 			return NULL;
       
  1346 
       
  1347 		if (VALID_CVALUE(int64, symbol->by_expression ) &&   GET_CVALUE(int64, symbol->by_expression ) < 0 &&
       
  1348 			VALID_CVALUE(int64, symbol->beg_expression) && VALID_CVALUE(int64, symbol->end_expression)    &&
       
  1349 			  GET_CVALUE(int64, symbol->beg_expression) <    GET_CVALUE(int64, symbol->end_expression))
       
  1350 			return NULL;
       
  1351 
       
  1352 	} else {
       
  1353 		if (VALID_CVALUE(int64, symbol->beg_expression) && VALID_CVALUE(int64, symbol->end_expression)     &&
       
  1354 			  GET_CVALUE(int64, symbol->beg_expression) >    GET_CVALUE(int64, symbol->end_expression))
       
  1355 			return NULL;
       
  1356 
       
  1357 	}
       
  1358 
  1332 
  1359 
  1333 	symbol->statement_list->accept(*this);
  1360 	symbol->statement_list->accept(*this);
  1334 	values_statement_result = values;
  1361 	values_statement_result = values;
  1335 	values.clear();
  1362 	values = inner_left_join_values(values_statement_result, values_incoming);
  1336 	itr = values_statement_result.begin();
       
  1337 	for ( ; itr != values_statement_result.end(); ++itr) {
       
  1338 		std::string name = itr->first;
       
  1339 		symbol_c::const_value_t value;
       
  1340 
       
  1341 		if (values_incoming.count(name) > 0) {
       
  1342 			symbol_c::const_value_t c1 = itr->second;
       
  1343 			symbol_c::const_value_t c2 = values_incoming[name];
       
  1344 			COMPUTE_MEET_SEMILATTICE (real64, c1, c2, value);
       
  1345 			COMPUTE_MEET_SEMILATTICE (uint64, c1, c2, value);
       
  1346 			COMPUTE_MEET_SEMILATTICE ( int64, c1, c2, value);
       
  1347 			COMPUTE_MEET_SEMILATTICE (  bool, c1, c2, value);
       
  1348 		} else
       
  1349 			value = values_statement_result[name];
       
  1350 		values[name] = value;
       
  1351 	}
       
  1352 
  1363 
  1353 	return NULL;
  1364 	return NULL;
  1354 }
  1365 }
  1355 
  1366 
  1356 void *constant_folding_c::visit(while_statement_c *symbol) {
  1367 void *constant_folding_c::visit(while_statement_c *symbol) {
  1357 	std::map <std::string, symbol_c::const_value_t> values_incoming;
  1368 	map_values_t values_incoming;
  1358 	std::map <std::string, symbol_c::const_value_t> values_statement_result;
  1369 	map_values_t values_statement_result;
  1359 	std::map <std::string, symbol_c::const_value_t>::iterator itr;
       
  1360 
  1370 
  1361 	/* Optimize dead code */
  1371 	/* Optimize dead code */
  1362 	symbol->expression->accept(*this);
  1372 	symbol->expression->accept(*this);
  1363 	if (VALID_CVALUE(bool, symbol->expression) && GET_CVALUE(bool, symbol->expression) == false)
  1373 	if (VALID_CVALUE(bool, symbol->expression) && GET_CVALUE(bool, symbol->expression) == false)
  1364 		return NULL;
  1374 		return NULL;
  1365 
  1375 
  1366 	values_incoming = values; /* save incoming status */
  1376 	values_incoming = values; /* save incoming status */
  1367 	symbol->statement_list->accept(*this);
  1377 	symbol->statement_list->accept(*this);
  1368 	values_statement_result = values;
  1378 	values_statement_result = values;
  1369 	values.clear();
  1379 	values = inner_left_join_values(values_statement_result, values_incoming);
  1370 	itr = values_statement_result.begin();
       
  1371 	for ( ; itr != values_statement_result.end(); ++itr) {
       
  1372 		std::string name = itr->first;
       
  1373 		symbol_c::const_value_t value;
       
  1374 
       
  1375 		if (values_incoming.count(name) > 0) {
       
  1376 			symbol_c::const_value_t c1 = itr->second;
       
  1377 			symbol_c::const_value_t c2 = values_incoming[name];
       
  1378 			COMPUTE_MEET_SEMILATTICE (real64, c1, c2, value);
       
  1379 			COMPUTE_MEET_SEMILATTICE (uint64, c1, c2, value);
       
  1380 			COMPUTE_MEET_SEMILATTICE ( int64, c1, c2, value);
       
  1381 			COMPUTE_MEET_SEMILATTICE (  bool, c1, c2, value);
       
  1382 		} else
       
  1383 			value = values_statement_result[name];
       
  1384 		values[name] = value;
       
  1385 	}
       
  1386 
       
  1387 
  1380 
  1388 	return NULL;
  1381 	return NULL;
  1389 }
  1382 }
  1390 
  1383 
  1391 void *constant_folding_c::visit(repeat_statement_c *symbol) {
  1384 void *constant_folding_c::visit(repeat_statement_c *symbol) {
  1392 	std::map <std::string, symbol_c::const_value_t> values_incoming;
  1385 	map_values_t values_incoming;
  1393 	std::map <std::string, symbol_c::const_value_t> values_statement_result;
  1386 	map_values_t values_statement_result;
  1394 	std::map <std::string, symbol_c::const_value_t>::iterator itr;
  1387 
       
  1388 	values_incoming = values; /* save incoming status */
       
  1389 	symbol->statement_list->accept(*this);
  1395 
  1390 
  1396 	/* Optimize dead code */
  1391 	/* Optimize dead code */
  1397 	symbol->expression->accept(*this);
  1392 	symbol->expression->accept(*this);
  1398 	if (VALID_CVALUE(bool, symbol->expression) && GET_CVALUE(bool, symbol->expression) == false)
  1393 	if (VALID_CVALUE(bool, symbol->expression) && GET_CVALUE(bool, symbol->expression) == true)
  1399 		return NULL;
  1394 		return NULL;
  1400 
  1395 
  1401 	values_incoming = values; /* save incoming status */
       
  1402 	symbol->statement_list->accept(*this);
       
  1403 	values_statement_result = values;
  1396 	values_statement_result = values;
  1404 	values.clear();
  1397 	values = inner_left_join_values(values_statement_result, values_incoming);
  1405 	itr = values_statement_result.begin();
  1398 
  1406 	for ( ; itr != values_statement_result.end(); ++itr) {
  1399 	return NULL;
  1407 		std::string name = itr->first;
  1400 }
  1408 		symbol_c::const_value_t value;
  1401 
  1409 
  1402 
  1410 		if (values_incoming.count(name) > 0) {
  1403 
  1411 			symbol_c::const_value_t c1 = itr->second;
  1404 
  1412 			symbol_c::const_value_t c2 = values_incoming[name];
       
  1413 			COMPUTE_MEET_SEMILATTICE (real64, c1, c2, value);
       
  1414 			COMPUTE_MEET_SEMILATTICE (uint64, c1, c2, value);
       
  1415 			COMPUTE_MEET_SEMILATTICE ( int64, c1, c2, value);
       
  1416 			COMPUTE_MEET_SEMILATTICE (  bool, c1, c2, value);
       
  1417 		} else
       
  1418 			value = values_statement_result[name];
       
  1419 		values[name] = value;
       
  1420 	}
       
  1421 
       
  1422 
       
  1423 	return NULL;
       
  1424 }
       
  1425 
       
  1426 
       
  1427