|
|
|
@ -34,6 +34,9 @@ namespace SysYF
|
|
|
|
|
Ptr<Type> FLOAT_T;
|
|
|
|
|
Ptr<Type> INT32PTR_T;
|
|
|
|
|
Ptr<Type> FLOATPTR_T;
|
|
|
|
|
|
|
|
|
|
int if_BB_num = 0;
|
|
|
|
|
int while_BB_num = 0;
|
|
|
|
|
#define get_true_type(left_type, right) \
|
|
|
|
|
if (!scope.in_global()) \
|
|
|
|
|
{ \
|
|
|
|
@ -803,22 +806,43 @@ namespace SysYF
|
|
|
|
|
case SyntaxTree::BinOp::DIVIDE:
|
|
|
|
|
if (expr_type == INT32_T)
|
|
|
|
|
{
|
|
|
|
|
if(rhs_int == 0){
|
|
|
|
|
cout << "Wrong Number" << endl;
|
|
|
|
|
}
|
|
|
|
|
else{
|
|
|
|
|
tmp_val = CONST_INT(lhs_int / rhs_int);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if(rhs_float == 0){
|
|
|
|
|
cout << "Wrong Number" << endl;
|
|
|
|
|
}
|
|
|
|
|
else{
|
|
|
|
|
|
|
|
|
|
tmp_val = CONST_FLOAT(lhs_float / rhs_float);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case SyntaxTree::BinOp::MODULO:
|
|
|
|
|
if (expr_type == INT32_T)
|
|
|
|
|
{
|
|
|
|
|
if(rhs_int == 0){
|
|
|
|
|
cout << "Wrong Number" << endl;
|
|
|
|
|
}
|
|
|
|
|
else{
|
|
|
|
|
tmp_val = CONST_INT(lhs_int % rhs_int);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if(rhs_float == 0){
|
|
|
|
|
cout << "Wrong Number" << endl;
|
|
|
|
|
}
|
|
|
|
|
else{
|
|
|
|
|
tmp_val = CONST_INT(static_cast<int>(lhs_float) % static_cast<int>(rhs_float));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
@ -988,21 +1012,23 @@ namespace SysYF
|
|
|
|
|
// 数组
|
|
|
|
|
if (arg_func_type == INT32PTR_T || arg_func_type == FLOATPTR_T)
|
|
|
|
|
{
|
|
|
|
|
// if(value->get_type() == INT32PTR_T || value->get_type() == FLOATPTR_T){
|
|
|
|
|
// tmp_val = value;
|
|
|
|
|
// }
|
|
|
|
|
// else{
|
|
|
|
|
// tmp_val = builder->create_alloca(arg_func_type);
|
|
|
|
|
// builder->create_store(value, tmp_val);
|
|
|
|
|
// }
|
|
|
|
|
if (value->get_type() == INT32PTR_T || value->get_type() == FLOATPTR_T)
|
|
|
|
|
{
|
|
|
|
|
tmp_val = value;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (value->get_type() == INT32_T && arg_func_type == FLOAT_T)
|
|
|
|
|
tmp_val = builder->create_alloca(arg_func_type);
|
|
|
|
|
builder->create_store(value, tmp_val);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (value->get_type() == INT32_T && (arg_func_type == FLOAT_T || arg_func_type == FLOATPTR_T))
|
|
|
|
|
{
|
|
|
|
|
tmp_val = builder->create_sitofp(value, FLOAT_T);
|
|
|
|
|
}
|
|
|
|
|
else if (value->get_type() == FLOAT_T && arg_func_type == INT32_T)
|
|
|
|
|
else if (value->get_type() == FLOAT_T && (arg_func_type == INT32_T || arg_func_type == INT32PTR_T))
|
|
|
|
|
{
|
|
|
|
|
tmp_val = builder->create_fptosi(value, INT32_T);
|
|
|
|
|
}
|
|
|
|
@ -1011,7 +1037,7 @@ namespace SysYF
|
|
|
|
|
tmp_val = value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
arg_func++;
|
|
|
|
|
args.push_back(tmp_val);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -1022,26 +1048,47 @@ namespace SysYF
|
|
|
|
|
void IRBuilder::visit(SyntaxTree::IfStmt &node)
|
|
|
|
|
{
|
|
|
|
|
scope.enter();
|
|
|
|
|
auto trueBB = BasicBlock::create(module, "trueBB_if",
|
|
|
|
|
auto trueBB = BasicBlock::create(module, "trueBB_if" + std::to_string(if_BB_num),
|
|
|
|
|
module->get_functions().back());
|
|
|
|
|
auto nextBB = BasicBlock::create(module, "nextBB_if",
|
|
|
|
|
auto nextBB = BasicBlock::create(module, "nextBB_if" + std::to_string(if_BB_num),
|
|
|
|
|
module->get_functions().back());
|
|
|
|
|
|
|
|
|
|
scope.push("trueBB", trueBB);
|
|
|
|
|
scope.push("falseBB", nextBB);
|
|
|
|
|
scope.push("trueBB" + std::to_string(if_BB_num), trueBB);
|
|
|
|
|
scope.push("falseBB" + std::to_string(if_BB_num), nextBB);
|
|
|
|
|
|
|
|
|
|
node.cond_exp->accept(*this);
|
|
|
|
|
LVal_to_RVal(tmp_val);
|
|
|
|
|
auto cond = tmp_val;
|
|
|
|
|
|
|
|
|
|
builder->create_cond_br(cond, trueBB, nextBB);
|
|
|
|
|
if (node.else_statement)
|
|
|
|
|
{
|
|
|
|
|
auto elseBB = BasicBlock::create(module, "elseBB_if" + std::to_string(if_BB_num),
|
|
|
|
|
module->get_functions().back());
|
|
|
|
|
|
|
|
|
|
scope.push("elseBB_if" + std::to_string(if_BB_num), elseBB);
|
|
|
|
|
|
|
|
|
|
if_BB_num++;
|
|
|
|
|
|
|
|
|
|
builder->create_cond_br(cond, trueBB, elseBB);
|
|
|
|
|
builder->set_insert_point(trueBB);
|
|
|
|
|
node.if_statement->accept(*this);
|
|
|
|
|
builder->create_br(nextBB);
|
|
|
|
|
|
|
|
|
|
builder->set_insert_point(elseBB);
|
|
|
|
|
node.else_statement->accept(*this);
|
|
|
|
|
builder->create_br(nextBB);
|
|
|
|
|
|
|
|
|
|
builder->set_insert_point(nextBB);
|
|
|
|
|
if (node.else_statement)
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
node.else_statement->accept(*this);
|
|
|
|
|
if_BB_num++;
|
|
|
|
|
builder->create_cond_br(cond, trueBB, nextBB);
|
|
|
|
|
builder->set_insert_point(trueBB);
|
|
|
|
|
node.if_statement->accept(*this);
|
|
|
|
|
builder->create_br(nextBB);
|
|
|
|
|
|
|
|
|
|
builder->set_insert_point(nextBB);
|
|
|
|
|
}
|
|
|
|
|
scope.exit();
|
|
|
|
|
}
|
|
|
|
@ -1050,17 +1097,17 @@ namespace SysYF
|
|
|
|
|
{
|
|
|
|
|
// auto func_name = scope.find("func", 0);
|
|
|
|
|
scope.enter();
|
|
|
|
|
auto condBB = BasicBlock::create(module, "condBB_while",
|
|
|
|
|
auto condBB = BasicBlock::create(module, "condBB_while" + std::to_string(while_BB_num),
|
|
|
|
|
module->get_functions().back());
|
|
|
|
|
auto trueBB = BasicBlock::create(module, "trueBB_while",
|
|
|
|
|
auto trueBB = BasicBlock::create(module, "trueBB_while" + std::to_string(while_BB_num),
|
|
|
|
|
module->get_functions().back());
|
|
|
|
|
auto falseBB = BasicBlock::create(module, "falseBB_while",
|
|
|
|
|
auto falseBB = BasicBlock::create(module, "falseBB_while" + std::to_string(while_BB_num),
|
|
|
|
|
module->get_functions().back());
|
|
|
|
|
|
|
|
|
|
scope.push("condBB_while", condBB);
|
|
|
|
|
scope.push("trueBB_while", trueBB);
|
|
|
|
|
scope.push("falseBB_while", falseBB);
|
|
|
|
|
|
|
|
|
|
scope.push("condBB_while" + std::to_string(while_BB_num), condBB);
|
|
|
|
|
scope.push("trueBB_while" + std::to_string(while_BB_num), trueBB);
|
|
|
|
|
scope.push("falseBB_while" + std::to_string(while_BB_num), falseBB);
|
|
|
|
|
while_BB_num++;
|
|
|
|
|
builder->create_br(condBB);
|
|
|
|
|
|
|
|
|
|
builder->set_insert_point(condBB);
|
|
|
|
@ -1082,13 +1129,13 @@ namespace SysYF
|
|
|
|
|
|
|
|
|
|
void IRBuilder::visit(SyntaxTree::BreakStmt &node)
|
|
|
|
|
{
|
|
|
|
|
auto falseBB = scope.find("falseBB_while", 0);
|
|
|
|
|
auto falseBB = scope.find("falseBB_while" + std::to_string(while_BB_num - 1), 0);
|
|
|
|
|
builder->create_br(dynamic_pointer_cast<SysYF::IR::BasicBlock>(falseBB));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void IRBuilder::visit(SyntaxTree::ContinueStmt &node)
|
|
|
|
|
{
|
|
|
|
|
auto condBB = scope.find("condBB_while", 0);
|
|
|
|
|
auto condBB = scope.find("condBB_while" + std::to_string(while_BB_num - 1), 0);
|
|
|
|
|
builder->create_br(dynamic_pointer_cast<SysYF::IR::BasicBlock>(condBB));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|