stage3/constant_folding.cc
changeset 788 aa56031e5cb3
parent 787 6e2671e0f1a8
child 789 861e92dc879b
equal deleted inserted replaced
787:6e2671e0f1a8 788:aa56031e5cb3
   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 /***********************************************************************/
   719 		SET_CVALUE(real64, symbol, pow(GET_CVALUE(real64, oper1), GET_CVALUE( int64, oper2)));
   721 		SET_CVALUE(real64, symbol, pow(GET_CVALUE(real64, oper1), GET_CVALUE( int64, oper2)));
   720 	if (VALID_CVALUE(real64, oper1) && VALID_CVALUE(uint64, oper2))
   722 	if (VALID_CVALUE(real64, oper1) && VALID_CVALUE(uint64, oper2))
   721 		SET_CVALUE(real64, symbol, pow(GET_CVALUE(real64, oper1), GET_CVALUE(uint64, oper2)));
   723 		SET_CVALUE(real64, symbol, pow(GET_CVALUE(real64, oper1), GET_CVALUE(uint64, oper2)));
   722 	CHECK_OVERFLOW_real64(symbol);
   724 	CHECK_OVERFLOW_real64(symbol);
   723 	return NULL;
   725 	return NULL;
       
   726 }
       
   727 
       
   728 static map_values_t inner_left_join_values(map_values_t m1, map_values_t m2) {
       
   729 	map_values_t::const_iterator itr;
       
   730 	map_values_t ret;
       
   731 
       
   732 	itr = m1.begin();
       
   733 	for ( ; itr != m1.end(); ++itr) {
       
   734 		std::string name = itr->first;
       
   735 		symbol_c::const_value_t value;
       
   736 
       
   737 		if (m2.count(name) > 0) {
       
   738 			symbol_c::const_value_t c1 = itr->second;
       
   739 			symbol_c::const_value_t c2 = m2[name];
       
   740 			COMPUTE_MEET_SEMILATTICE (real64, c1, c2, value);
       
   741 			COMPUTE_MEET_SEMILATTICE (uint64, c1, c2, value);
       
   742 			COMPUTE_MEET_SEMILATTICE ( int64, c1, c2, value);
       
   743 			COMPUTE_MEET_SEMILATTICE (  bool, c1, c2, value);
       
   744 		} else
       
   745 			value = m1[name];
       
   746 		ret[name] = value;
       
   747 	}
       
   748 
       
   749 	return ret;
   724 }
   750 }
   725 
   751 
   726 /***********************************************************************/
   752 /***********************************************************************/
   727 /***********************************************************************/
   753 /***********************************************************************/
   728 /***********************************************************************/
   754 /***********************************************************************/
   957 		symbol->const_value = values[varName];
   983 		symbol->const_value = values[varName];
   958 	}
   984 	}
   959 	return NULL;
   985 	return NULL;
   960 }
   986 }
   961 
   987 
       
   988 
   962 /**********************/
   989 /**********************/
   963 /* B 1.5.3 - Programs */
   990 /* B 1.5.3 - Programs */
   964 /**********************/
   991 /**********************/
   965 void *constant_folding_c::visit(program_declaration_c *symbol) {
   992 void *constant_folding_c::visit(program_declaration_c *symbol) {
   966 	symbol_c *var_name;
   993 	symbol_c *var_name;
  1267 
  1294 
  1268 /********************************/
  1295 /********************************/
  1269 /* B 3.2.3 Selection Statements */
  1296 /* B 3.2.3 Selection Statements */
  1270 /********************************/
  1297 /********************************/
  1271 void *constant_folding_c::visit(if_statement_c *symbol) {
  1298 void *constant_folding_c::visit(if_statement_c *symbol) {
  1272 	std::map <std::string, symbol_c::const_value_t> values_incoming;
  1299 	map_values_t values_incoming;
  1273 	std::map <std::string, symbol_c::const_value_t> values_statement_result;
  1300 	map_values_t values_statement_result;
  1274 	std::map <std::string, symbol_c::const_value_t> values_elsestatement_result;
  1301 	map_values_t values_elsestatement_result;
  1275 	std::map <std::string, symbol_c::const_value_t>::iterator itr;
  1302 	map_values_t::iterator itr;
  1276 
  1303 
  1277 	/* Optimize dead code */
  1304 	/* Optimize dead code */
  1278 	symbol->expression->accept(*this);
  1305 	symbol->expression->accept(*this);
  1279 	if (VALID_CVALUE(bool, symbol->expression) && GET_CVALUE(bool, symbol->expression) == false)
  1306 	if (VALID_CVALUE(bool, symbol->expression) && GET_CVALUE(bool, symbol->expression) == false)
  1280 		return NULL;
  1307 		return NULL;
  1286 		values = values_incoming;
  1313 		values = values_incoming;
  1287 		symbol->else_statement_list->accept(*this);
  1314 		symbol->else_statement_list->accept(*this);
  1288 		values_elsestatement_result = values;
  1315 		values_elsestatement_result = values;
  1289 	} else
  1316 	} else
  1290 		values_elsestatement_result = values_incoming;
  1317 		values_elsestatement_result = values_incoming;
  1291 	values.clear();
  1318 	values = inner_left_join_values(values_statement_result, values_elsestatement_result);
  1292 	itr = values_statement_result.begin();
       
  1293 	for ( ; itr != values_statement_result.end(); ++itr) {
       
  1294 		std::string name = itr->first;
       
  1295 		symbol_c::const_value_t value;
       
  1296 
       
  1297 		if (values_elsestatement_result.count(name) > 0) {
       
  1298 			symbol_c::const_value_t c1 = itr->second;
       
  1299 			symbol_c::const_value_t c2 = values_elsestatement_result[name];
       
  1300 			COMPUTE_MEET_SEMILATTICE (real64, c1, c2, value);
       
  1301 			COMPUTE_MEET_SEMILATTICE (uint64, c1, c2, value);
       
  1302 			COMPUTE_MEET_SEMILATTICE ( int64, c1, c2, value);
       
  1303 			COMPUTE_MEET_SEMILATTICE (  bool, c1, c2, value);
       
  1304 		} else
       
  1305 			value = values_statement_result[name];
       
  1306 		values[name] = value;
       
  1307 	}
       
  1308 
  1319 
  1309 	return NULL;
  1320 	return NULL;
  1310 }
  1321 }
  1311 
  1322 
  1312 /********************************/
  1323 /********************************/
  1313 /* B 3.2.4 Iteration Statements */
  1324 /* B 3.2.4 Iteration Statements */
  1314 /********************************/
  1325 /********************************/
  1315 void *constant_folding_c::visit(for_statement_c *symbol) {
  1326 void *constant_folding_c::visit(for_statement_c *symbol) {
  1316 	std::map <std::string, symbol_c::const_value_t> values_incoming;
  1327 	map_values_t values_incoming;
  1317 	std::map <std::string, symbol_c::const_value_t> values_statement_result;
  1328 	map_values_t values_statement_result;
  1318 	std::map <std::string, symbol_c::const_value_t>::iterator itr;
  1329 	map_values_t::iterator itr;
  1319 	std::string varName;
  1330 	std::string varName;
       
  1331 
  1320 
  1332 
  1321 	values_incoming = values; /* save incoming status */
  1333 	values_incoming = values; /* save incoming status */
  1322 	symbol->beg_expression->accept(*this);
  1334 	symbol->beg_expression->accept(*this);
  1323 	symbol->end_expression->accept(*this);
  1335 	symbol->end_expression->accept(*this);
  1324 	varName =  get_var_name_c::get_name(symbol->control_variable)->value;
  1336 	varName =  get_var_name_c::get_name(symbol->control_variable)->value;
  1325 	values[varName] = symbol->beg_expression->const_value;
  1337 	values[varName]._int64.status = symbol_c::cs_non_const;
  1326 
  1338 
  1327 	/* Optimize dead code */
  1339 	/* Optimize dead code */
  1328 	if (VALID_CVALUE(int64, symbol->beg_expression) && VALID_CVALUE(int64, symbol->end_expression) &&
  1340 	if (NULL != symbol->by_expression) {
  1329 		  GET_CVALUE(int64, symbol->beg_expression) >    GET_CVALUE(int64, symbol->end_expression))
  1341 		symbol->by_expression->accept(*this);
  1330 		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 
  1331 
  1359 
  1332 	symbol->statement_list->accept(*this);
  1360 	symbol->statement_list->accept(*this);
  1333 	values_statement_result = values;
  1361 	values_statement_result = values;
  1334 	values.clear();
  1362 	values = inner_left_join_values(values_statement_result, values_incoming);
  1335 	itr = values_statement_result.begin();
       
  1336 	for ( ; itr != values_statement_result.end(); ++itr) {
       
  1337 		std::string name = itr->first;
       
  1338 		symbol_c::const_value_t value;
       
  1339 
       
  1340 		if (values_incoming.count(name) > 0) {
       
  1341 			symbol_c::const_value_t c1 = itr->second;
       
  1342 			symbol_c::const_value_t c2 = values_incoming[name];
       
  1343 			COMPUTE_MEET_SEMILATTICE (real64, c1, c2, value);
       
  1344 			COMPUTE_MEET_SEMILATTICE (uint64, c1, c2, value);
       
  1345 			COMPUTE_MEET_SEMILATTICE ( int64, c1, c2, value);
       
  1346 			COMPUTE_MEET_SEMILATTICE (  bool, c1, c2, value);
       
  1347 		} else
       
  1348 			value = values_statement_result[name];
       
  1349 		values[name] = value;
       
  1350 	}
       
  1351 
  1363 
  1352 	return NULL;
  1364 	return NULL;
  1353 }
  1365 }
  1354 
  1366 
  1355 void *constant_folding_c::visit(while_statement_c *symbol) {
  1367 void *constant_folding_c::visit(while_statement_c *symbol) {
  1356 	std::map <std::string, symbol_c::const_value_t> values_incoming;
  1368 	map_values_t values_incoming;
  1357 	std::map <std::string, symbol_c::const_value_t> values_statement_result;
  1369 	map_values_t values_statement_result;
  1358 	std::map <std::string, symbol_c::const_value_t>::iterator itr;
       
  1359 
  1370 
  1360 	/* Optimize dead code */
  1371 	/* Optimize dead code */
  1361 	symbol->expression->accept(*this);
  1372 	symbol->expression->accept(*this);
  1362 	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)
  1363 		return NULL;
  1374 		return NULL;
  1364 
  1375 
  1365 	values_incoming = values; /* save incoming status */
  1376 	values_incoming = values; /* save incoming status */
  1366 	symbol->statement_list->accept(*this);
  1377 	symbol->statement_list->accept(*this);
  1367 	values_statement_result = values;
  1378 	values_statement_result = values;
  1368 	values.clear();
  1379 	values = inner_left_join_values(values_statement_result, values_incoming);
  1369 	itr = values_statement_result.begin();
       
  1370 	for ( ; itr != values_statement_result.end(); ++itr) {
       
  1371 		std::string name = itr->first;
       
  1372 		symbol_c::const_value_t value;
       
  1373 
       
  1374 		if (values_incoming.count(name) > 0) {
       
  1375 			symbol_c::const_value_t c1 = itr->second;
       
  1376 			symbol_c::const_value_t c2 = values_incoming[name];
       
  1377 			COMPUTE_MEET_SEMILATTICE (real64, c1, c2, value);
       
  1378 			COMPUTE_MEET_SEMILATTICE (uint64, c1, c2, value);
       
  1379 			COMPUTE_MEET_SEMILATTICE ( int64, c1, c2, value);
       
  1380 			COMPUTE_MEET_SEMILATTICE (  bool, c1, c2, value);
       
  1381 		} else
       
  1382 			value = values_statement_result[name];
       
  1383 		values[name] = value;
       
  1384 	}
       
  1385 
       
  1386 
  1380 
  1387 	return NULL;
  1381 	return NULL;
  1388 }
  1382 }
  1389 
  1383 
  1390 void *constant_folding_c::visit(repeat_statement_c *symbol) {
  1384 void *constant_folding_c::visit(repeat_statement_c *symbol) {
  1391 	std::map <std::string, symbol_c::const_value_t> values_incoming;
  1385 	map_values_t values_incoming;
  1392 	std::map <std::string, symbol_c::const_value_t> values_statement_result;
  1386 	map_values_t values_statement_result;
  1393 	std::map <std::string, symbol_c::const_value_t>::iterator itr;
       
  1394 
  1387 
  1395 	/* Optimize dead code */
  1388 	/* Optimize dead code */
  1396 	symbol->expression->accept(*this);
  1389 	symbol->expression->accept(*this);
  1397 	if (VALID_CVALUE(bool, symbol->expression) && GET_CVALUE(bool, symbol->expression) == false)
  1390 	if (VALID_CVALUE(bool, symbol->expression) && GET_CVALUE(bool, symbol->expression) == false)
  1398 		return NULL;
  1391 		return NULL;
  1399 
  1392 
  1400 	values_incoming = values; /* save incoming status */
  1393 	values_incoming = values; /* save incoming status */
  1401 	symbol->statement_list->accept(*this);
  1394 	symbol->statement_list->accept(*this);
  1402 	values_statement_result = values;
  1395 	values_statement_result = values;
  1403 	values.clear();
  1396 	values = inner_left_join_values(values_statement_result, values_incoming);
  1404 	itr = values_statement_result.begin();
  1397 
  1405 	for ( ; itr != values_statement_result.end(); ++itr) {
  1398 	return NULL;
  1406 		std::string name = itr->first;
  1399 }
  1407 		symbol_c::const_value_t value;
  1400 
  1408 
  1401 
  1409 		if (values_incoming.count(name) > 0) {
  1402 
  1410 			symbol_c::const_value_t c1 = itr->second;
  1403 
  1411 			symbol_c::const_value_t c2 = values_incoming[name];
       
  1412 			COMPUTE_MEET_SEMILATTICE (real64, c1, c2, value);
       
  1413 			COMPUTE_MEET_SEMILATTICE (uint64, c1, c2, value);
       
  1414 			COMPUTE_MEET_SEMILATTICE ( int64, c1, c2, value);
       
  1415 			COMPUTE_MEET_SEMILATTICE (  bool, c1, c2, value);
       
  1416 		} else
       
  1417 			value = values_statement_result[name];
       
  1418 		values[name] = value;
       
  1419 	}
       
  1420 
       
  1421 
       
  1422 	return NULL;
       
  1423 }
       
  1424 
       
  1425 
       
  1426