From b8ccc9538d4a87742c6d52b3199ae17cb7ccdc00 Mon Sep 17 00:00:00 2001 From: stivenkingsberg Date: Fri, 29 Dec 2023 17:28:17 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8A=8A=E4=B8=8A=E6=AC=A1=E4=BC=A0=E9=94=99?= =?UTF-8?q?=E7=9A=84=E6=96=87=E4=BB=B6=E4=BC=A0=E4=BA=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/SysYFIRBuilder/IRBuilder.cpp | 96 ++++++++++++++++++++------------ 1 file changed, 61 insertions(+), 35 deletions(-) diff --git a/src/SysYFIRBuilder/IRBuilder.cpp b/src/SysYFIRBuilder/IRBuilder.cpp index ea7f3e9..ec0b081 100644 --- a/src/SysYFIRBuilder/IRBuilder.cpp +++ b/src/SysYFIRBuilder/IRBuilder.cpp @@ -34,14 +34,17 @@ namespace SysYF Ptr FLOAT_T; Ptr INT32PTR_T; Ptr FLOATPTR_T; + + int if_BB_num = 0; + int while_BB_num = 0; #define get_true_type(left_type, right) \ if (!scope.in_global()) \ { \ - if (left_type == INT32_T && right->get_type() == FLOAT_T) \ + if ((left_type == INT32_T || left_type == INT32PTR_T) && right->get_type() == FLOAT_T) \ { \ right = builder->create_fptosi(right, INT32_T); \ } \ - else if (left_type == INT32_T && right->get_type() == INT1_T) \ + else if ((left_type == INT32_T || left_type == INT32PTR_T) && right->get_type() == INT1_T) \ { \ right = builder->create_zext(right, INT32_T); \ } \ @@ -53,22 +56,22 @@ namespace SysYF { \ right = builder->create_icmp_ne(right, CONST_INT(0)); \ } \ - else if (left_type == FLOAT_T && right->get_type() == INT32_T) \ + else if ((left_type == FLOAT_T || left_type == FLOATPTR_T) && right->get_type() == INT32_T) \ { \ right = builder->create_sitofp(right, FLOAT_T); \ } \ - else if (left_type == FLOAT_T && right->get_type() == INT1_T) \ + else if ((left_type == FLOAT_T || left_type == FLOATPTR_T) && right->get_type() == INT1_T) \ { \ right = builder->create_zext(right, FLOAT_T); \ } \ } \ else \ { \ - if (left_type == FLOAT_T && right->get_type() != FLOAT_T) \ + if ((left_type == FLOAT_T || left_type == INT32PTR_T) && right->get_type() != FLOAT_T) \ { \ right = CONST_FLOAT(static_cast(dynamic_pointer_cast(right)->get_value())); \ } \ - else if (left_type == INT32_T && right->get_type() != INT32_T) \ + else if ((left_type == INT32_T || left_type == FLOATPTR_T) && right->get_type() != INT32_T) \ { \ right = CONST_INT(static_cast(dynamic_pointer_cast(right)->get_value())); \ } \ @@ -349,7 +352,7 @@ namespace SysYF else { init_list.clear(); - while (init_list.size() < length.size()) // 如果init_list的长度小于length的长度,就把init_list其他的值补上0 + while (static_cast(init_list.size()) < array_len) // 如果init_list的长度小于length的长度,就把init_list其他的值补上0 { init_list.push_back(ConstantZero::create(type_map[node.btype], module)); } @@ -988,21 +991,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 + { + 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) + 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 +1016,7 @@ namespace SysYF tmp_val = value; } } - + arg_func++; args.push_back(tmp_val); } @@ -1022,26 +1027,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); - builder->set_insert_point(trueBB); - node.if_statement->accept(*this); - - builder->set_insert_point(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); + } + else + { + 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 +1076,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 +1108,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(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(condBB)); } }