diff --git a/src/SysYFIRBuilder/IRBuilder.cpp b/src/SysYFIRBuilder/IRBuilder.cpp index 951bd45..7ae2b07 100644 --- a/src/SysYFIRBuilder/IRBuilder.cpp +++ b/src/SysYFIRBuilder/IRBuilder.cpp @@ -8,8 +8,10 @@ namespace IR #define CONST_FLOAT(num) ConstantFloat::create(num, module) // You can define global variables here -// to store state +// to store state +std::vector> funcParam; +int exprRes = 0; // store temporary value Ptr tmp_val = nullptr; @@ -21,6 +23,24 @@ Ptr FLOAT_T; Ptr INT32PTR_T; Ptr FLOATPTR_T; +Ptr GetType(SyntaxTree::Type type){ + switch(type){ + case SyntaxTree::Type::INT: + return INT32_T; + case SyntaxTree::Type::FLOAT: + return INT1_T; + case SyntaxTree::Type::VOID: + return VOID_T; + case SyntaxTree::Type::BOOL: + return INT1_T; + case SyntaxTree::Type::STRING: + return INT32PTR_T; + default: + return nullptr; + } +} + + void IRBuilder::visit(SyntaxTree::Assembly &node) { VOID_T = Type::get_void_type(module); INT1_T = Type::get_int1_type(module); @@ -35,47 +55,123 @@ void IRBuilder::visit(SyntaxTree::Assembly &node) { // You need to fill them -void IRBuilder::visit(SyntaxTree::InitVal &node) {} +void IRBuilder::visit(SyntaxTree::InitVal &node) { + +} + +void IRBuilder::visit(SyntaxTree::FuncDef &node) { + node.param_list->accept(*this); + auto funcType = FunctionType::create(GetType(node.ret_type), funcParam); + auto createFunc = Function::create(funcType, node.name, module); + node.body->accept(*this); +} + +void IRBuilder::visit(SyntaxTree::FuncFParamList &node) { + funcParam.clear(); + for (const auto ¶ : node.params) { + para->accept(*this); + } +} +void IRBuilder::visit(SyntaxTree::FuncParam &node) { + switch(node.param_type) { + case SyntaxTree::Type::INT: + if (!node.array_index.empty()) { + // TODO: high dim array + funcParam.push_back(INT32PTR_T); + } + else + funcParam.push_back(INT32_T); + break; + case SyntaxTree::Type::FLOAT: + if (!node.array_index.empty()) { + // TODO: high dim array + funcParam.push_back(FLOATPTR_T); + } + else + funcParam.push_back(FLOAT_T); + break; + case SyntaxTree::Type::VOID: + funcParam.push_back(VOID_T); + break; + case SyntaxTree::Type::STRING: + funcParam.push_back(INT32PTR_T); + break; + case SyntaxTree::Type::BOOL: + funcParam.push_back(INT1_T); + break; + default: + funcParam.push_back(INT32_T); + break; + } +} + +void IRBuilder::visit(SyntaxTree::VarDef &node) { + +} + +void IRBuilder::visit(SyntaxTree::LVal &node) { + +} + +void IRBuilder::visit(SyntaxTree::AssignStmt &node) { + +} + +void IRBuilder::visit(SyntaxTree::Literal &node) { -void IRBuilder::visit(SyntaxTree::FuncDef &node) {} +} -void IRBuilder::visit(SyntaxTree::FuncFParamList &node) {} +void IRBuilder::visit(SyntaxTree::ReturnStmt &node) { -void IRBuilder::visit(SyntaxTree::FuncParam &node) {} +} -void IRBuilder::visit(SyntaxTree::VarDef &node) {} +void IRBuilder::visit(SyntaxTree::BlockStmt &node) { -void IRBuilder::visit(SyntaxTree::LVal &node) {} +} -void IRBuilder::visit(SyntaxTree::AssignStmt &node) {} +void IRBuilder::visit(SyntaxTree::EmptyStmt &node) { -void IRBuilder::visit(SyntaxTree::Literal &node) {} +} -void IRBuilder::visit(SyntaxTree::ReturnStmt &node) {} +void IRBuilder::visit(SyntaxTree::ExprStmt &node) { -void IRBuilder::visit(SyntaxTree::BlockStmt &node) {} +} -void IRBuilder::visit(SyntaxTree::EmptyStmt &node) {} +void IRBuilder::visit(SyntaxTree::UnaryCondExpr &node) { -void IRBuilder::visit(SyntaxTree::ExprStmt &node) {} +} -void IRBuilder::visit(SyntaxTree::UnaryCondExpr &node) {} +void IRBuilder::visit(SyntaxTree::BinaryCondExpr &node) { -void IRBuilder::visit(SyntaxTree::BinaryCondExpr &node) {} +} -void IRBuilder::visit(SyntaxTree::BinaryExpr &node) {} +void IRBuilder::visit(SyntaxTree::BinaryExpr &node) { -void IRBuilder::visit(SyntaxTree::UnaryExpr &node) {} +} -void IRBuilder::visit(SyntaxTree::FuncCallStmt &node) {} +void IRBuilder::visit(SyntaxTree::UnaryExpr &node) { -void IRBuilder::visit(SyntaxTree::IfStmt &node) {} +} -void IRBuilder::visit(SyntaxTree::WhileStmt &node) {} +void IRBuilder::visit(SyntaxTree::FuncCallStmt &node) { -void IRBuilder::visit(SyntaxTree::BreakStmt &node) {} +} -void IRBuilder::visit(SyntaxTree::ContinueStmt &node) {} +void IRBuilder::visit(SyntaxTree::IfStmt &node) { + +} + +void IRBuilder::visit(SyntaxTree::WhileStmt &node) { + +} + +void IRBuilder::visit(SyntaxTree::BreakStmt &node) { + +} + +void IRBuilder::visit(SyntaxTree::ContinueStmt &node) { + +} } } \ No newline at end of file