stage3/constant_folding.cc
changeset 570 cb704eca7e37
parent 569 0d1ab9e78574
child 572 c353bc67bf91
equal deleted inserted replaced
569:0d1ab9e78574 570:cb704eca7e37
   236 	symbol->exp->accept(*this);
   236 	symbol->exp->accept(*this);
   237 	if (VALID_CVALUE(int64, symbol->exp)) {
   237 	if (VALID_CVALUE(int64, symbol->exp)) {
   238 		NEW_CVALUE( int64, symbol);
   238 		NEW_CVALUE( int64, symbol);
   239 		SET_CVALUE( int64, symbol, - GET_CVALUE( int64, symbol->exp));
   239 		SET_CVALUE( int64, symbol, - GET_CVALUE( int64, symbol->exp));
   240 	}
   240 	}
       
   241         /* TODO: check for overflows */
   241 	return NULL;
   242 	return NULL;
   242 }
   243 }
   243 
   244 
   244 
   245 
   245 void *constant_folding_c::visit(binary_integer_c *symbol) {
   246 void *constant_folding_c::visit(binary_integer_c *symbol) {
   432 		 if (isnan(GET_CVALUE(real64, symbol)))                                                                                              \
   433 		 if (isnan(GET_CVALUE(real64, symbol)))                                                                                              \
   433 			SET_OVFLOW(real64, symbol);
   434 			SET_OVFLOW(real64, symbol);
   434 void *constant_folding_c::visit(add_expression_c *symbol) {
   435 void *constant_folding_c::visit(add_expression_c *symbol) {
   435 	symbol->l_exp->accept(*this);
   436 	symbol->l_exp->accept(*this);
   436 	symbol->r_exp->accept(*this);
   437 	symbol->r_exp->accept(*this);
   437 	DO_BIN_OPER(uint64, +);
   438 	DO_BIN_OPER(uint64, +);   CHECK_OVERFLOW_SUM(uint64);
   438 	DO_BIN_OPER( int64, +);
   439 	DO_BIN_OPER( int64, +);   CHECK_OVERFLOW_SUM( int64);
   439 	DO_BIN_OPER(real64, +);
   440 	DO_BIN_OPER(real64, +);   CHECK_OVERFLOW_real64;
   440 	CHECK_OVERFLOW_SUM(uint64);
       
   441 	CHECK_OVERFLOW_SUM( int64);
       
   442 	CHECK_OVERFLOW_real64;
       
   443 	return NULL;
   441 	return NULL;
   444 }
   442 }
   445 
   443 
   446 
   444 
   447 #define CHECK_OVERFLOW_SUB(dtype)\
   445 #define CHECK_OVERFLOW_SUB(dtype)\
   450 		    (((std::numeric_limits< dtype##_t >::min() + GET_CVALUE(dtype, symbol->l_exp)) > (-GET_CVALUE(dtype, symbol->r_exp))) ? 1 : 0))   \
   448 		    (((std::numeric_limits< dtype##_t >::min() + GET_CVALUE(dtype, symbol->l_exp)) > (-GET_CVALUE(dtype, symbol->r_exp))) ? 1 : 0))   \
   451 			SET_OVFLOW(dtype, symbol);
   449 			SET_OVFLOW(dtype, symbol);
   452 void *constant_folding_c::visit(sub_expression_c *symbol) {
   450 void *constant_folding_c::visit(sub_expression_c *symbol) {
   453 	symbol->l_exp->accept(*this);
   451 	symbol->l_exp->accept(*this);
   454 	symbol->r_exp->accept(*this);
   452 	symbol->r_exp->accept(*this);
   455 	DO_BIN_OPER(uint64, -);
   453 	DO_BIN_OPER(uint64, -);   CHECK_OVERFLOW_SUB(uint64);
   456 	DO_BIN_OPER( int64, -);
   454 	DO_BIN_OPER( int64, -);   CHECK_OVERFLOW_SUB( int64);
   457 	DO_BIN_OPER(real64, -);
   455 	DO_BIN_OPER(real64, -);   CHECK_OVERFLOW_real64;
   458 	CHECK_OVERFLOW_SUB(uint64);
       
   459 	CHECK_OVERFLOW_SUB( int64);
       
   460 	CHECK_OVERFLOW_real64;
       
   461 	return NULL;
   456 	return NULL;
   462 }
   457 }
   463 
   458 
   464 
   459 
   465 /* TODO!!! */
   460 /* TODO!!! */
   468 		if (false)                                                                                                                                   \
   463 		if (false)                                                                                                                                   \
   469 			SET_OVFLOW(dtype, symbol);
   464 			SET_OVFLOW(dtype, symbol);
   470 void *constant_folding_c::visit(mul_expression_c *symbol) {
   465 void *constant_folding_c::visit(mul_expression_c *symbol) {
   471 	symbol->l_exp->accept(*this);
   466 	symbol->l_exp->accept(*this);
   472 	symbol->r_exp->accept(*this);
   467 	symbol->r_exp->accept(*this);
   473 	DO_BIN_OPER(uint64, *);
   468 	DO_BIN_OPER(uint64, *);  CHECK_OVERFLOW_MUL(uint64);
   474 	DO_BIN_OPER( int64, *);
   469 	DO_BIN_OPER( int64, *);  CHECK_OVERFLOW_MUL( int64);
   475 	DO_BIN_OPER(real64, *);
   470 	DO_BIN_OPER(real64, *);  CHECK_OVERFLOW_real64;
   476 	CHECK_OVERFLOW_MUL(uint64);
       
   477 	CHECK_OVERFLOW_MUL( int64);
       
   478 	CHECK_OVERFLOW_real64;
       
   479 	return NULL;
   471 	return NULL;
   480 }
   472 }
   481 
   473 
   482 
   474 
   483 
   475