合并IRbuilder

main
llh 11 months ago
parent 712dd334d4
commit 355db97ef0

@ -0,0 +1,36 @@
project(SysYFCompiler)
cmake_minimum_required(VERSION 3.5)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O0 -Wall -Wextra -Wno-unused -Wshadow -Werror -g -pedantic")
# include generated files in project environment
include_directories(${CMAKE_CURRENT_BINARY_DIR})
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include/AST)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include/ErrorReporter)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include/Frontend)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include/SysYFIR)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include/SysYFIRBuilder)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/src/AST)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/src/ErrorReporter)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/src/Frontend)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/src/SysYFIR)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/src/SysYFIRBuilder)
add_executable(
SysYFCompiler
${CMAKE_CURRENT_SOURCE_DIR}/src/main.cpp
)
target_link_libraries(
SysYFCompiler
SysYFIRBuilder
IRLib
Driver
ASTPrinter
ErrReporter
)

@ -1,7 +1,7 @@
# PW6 实验报告
PB21000117 陈应豪
PB21111663 李璐豪
## 问题回答
### task1
@ -29,10 +29,25 @@ PB21000117 陈应豪
## 实验设计
在task1的时候学习SysYF的语法并编写等效的ll文件
在task2的时候学习能够生成SysYF的IR文法使之能生成等效的ll文件
在task3的时候利用实验框架通过类比上一关的IR认罚的编写利用已经生成的AST树为不同的模块编写对应的IR语法
## 实验难点及解决方案
在函数外表达式的计算中由于全局变量无法使用bb模块内的build相关函数需要根据变量名单独取出值并计算
Const数组不会在函数外进行再赋值的操作
## 实验总结
利用PW5学到的AST以及访问者模式我们编写了能够生成IR的实验框架对AST和IR的理解更加深刻
## 实验反馈
配置文件不够齐全
## 组间交流

@ -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));
}
}

Loading…
Cancel
Save