把上次传错的文件传了

main^2
stivenkingsberg 2 years ago
parent c8246d98f5
commit b8ccc9538d

@ -34,14 +34,17 @@ namespace SysYF
Ptr<Type> FLOAT_T; Ptr<Type> FLOAT_T;
Ptr<Type> INT32PTR_T; Ptr<Type> INT32PTR_T;
Ptr<Type> FLOATPTR_T; Ptr<Type> FLOATPTR_T;
int if_BB_num = 0;
int while_BB_num = 0;
#define get_true_type(left_type, right) \ #define get_true_type(left_type, right) \
if (!scope.in_global()) \ 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); \ 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); \ right = builder->create_zext(right, INT32_T); \
} \ } \
@ -53,22 +56,22 @@ namespace SysYF
{ \ { \
right = builder->create_icmp_ne(right, CONST_INT(0)); \ 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); \ 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); \ right = builder->create_zext(right, FLOAT_T); \
} \ } \
} \ } \
else \ 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<float>(dynamic_pointer_cast<ConstantInt>(right)->get_value())); \ right = CONST_FLOAT(static_cast<float>(dynamic_pointer_cast<ConstantInt>(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<int>(dynamic_pointer_cast<ConstantFloat>(right)->get_value())); \ right = CONST_INT(static_cast<int>(dynamic_pointer_cast<ConstantFloat>(right)->get_value())); \
} \ } \
@ -349,7 +352,7 @@ namespace SysYF
else else
{ {
init_list.clear(); init_list.clear();
while (init_list.size() < length.size()) // 如果init_list的长度小于length的长度就把init_list其他的值补上0 while (static_cast<int>(init_list.size()) < array_len) // 如果init_list的长度小于length的长度就把init_list其他的值补上0
{ {
init_list.push_back(ConstantZero::create(type_map[node.btype], module)); 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 (arg_func_type == INT32PTR_T || arg_func_type == FLOATPTR_T)
{ {
// if(value->get_type() == INT32PTR_T || value->get_type() == FLOATPTR_T){ if (value->get_type() == INT32PTR_T || value->get_type() == FLOATPTR_T)
// tmp_val = value; {
// } tmp_val = value;
// else{ }
// tmp_val = builder->create_alloca(arg_func_type); else
// builder->create_store(value, tmp_val); {
// } tmp_val = builder->create_alloca(arg_func_type);
builder->create_store(value, tmp_val);
}
} }
else 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); 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); tmp_val = builder->create_fptosi(value, INT32_T);
} }
@ -1011,7 +1016,7 @@ namespace SysYF
tmp_val = value; tmp_val = value;
} }
} }
arg_func++;
args.push_back(tmp_val); args.push_back(tmp_val);
} }
@ -1022,26 +1027,47 @@ namespace SysYF
void IRBuilder::visit(SyntaxTree::IfStmt &node) void IRBuilder::visit(SyntaxTree::IfStmt &node)
{ {
scope.enter(); 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()); 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()); module->get_functions().back());
scope.push("trueBB", trueBB); scope.push("trueBB" + std::to_string(if_BB_num), trueBB);
scope.push("falseBB", nextBB); scope.push("falseBB" + std::to_string(if_BB_num), nextBB);
node.cond_exp->accept(*this); node.cond_exp->accept(*this);
LVal_to_RVal(tmp_val); LVal_to_RVal(tmp_val);
auto cond = 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) 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); 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(); scope.exit();
} }
@ -1050,17 +1076,17 @@ namespace SysYF
{ {
// auto func_name = scope.find("func", 0); // auto func_name = scope.find("func", 0);
scope.enter(); 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()); 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()); 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()); module->get_functions().back());
scope.push("condBB_while", condBB); scope.push("condBB_while" + std::to_string(while_BB_num), condBB);
scope.push("trueBB_while", trueBB); scope.push("trueBB_while" + std::to_string(while_BB_num), trueBB);
scope.push("falseBB_while", falseBB); scope.push("falseBB_while" + std::to_string(while_BB_num), falseBB);
while_BB_num++;
builder->create_br(condBB); builder->create_br(condBB);
builder->set_insert_point(condBB); builder->set_insert_point(condBB);
@ -1082,13 +1108,13 @@ namespace SysYF
void IRBuilder::visit(SyntaxTree::BreakStmt &node) 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)); builder->create_br(dynamic_pointer_cast<SysYF::IR::BasicBlock>(falseBB));
} }
void IRBuilder::visit(SyntaxTree::ContinueStmt &node) 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)); builder->create_br(dynamic_pointer_cast<SysYF::IR::BasicBlock>(condBB));
} }
} }

Loading…
Cancel
Save