diff --git a/include/code_generator.hpp b/include/qbe_ir_generator.hpp similarity index 87% rename from include/code_generator.hpp rename to include/qbe_ir_generator.hpp index b5ede898..461d2ed9 100644 --- a/include/code_generator.hpp +++ b/include/qbe_ir_generator.hpp @@ -1,9 +1,9 @@ -#ifndef CODE_GENERATOR_HPP_ -#define CODE_GENERATOR_HPP_ +#ifndef QBE_IR_GENERATOR_HPP_ +#define QBE_IR_GENERATOR_HPP_ #include "visitor.hpp" -class CodeGenerator : public NonModifyingVisitor { +class QbeIrGenerator : public NonModifyingVisitor { public: void Visit(const DeclNode&) override; void Visit(const BlockStmtNode&) override; @@ -28,4 +28,4 @@ class CodeGenerator : public NonModifyingVisitor { void Visit(const SimpleAssignmentExprNode&) override; }; -#endif // CODE_GENERATOR_HPP_ +#endif // QBE_IR_GENERATOR_HPP_ diff --git a/main.cpp b/main.cpp index aab6e528..6ed18adb 100644 --- a/main.cpp +++ b/main.cpp @@ -6,7 +6,7 @@ #include "ast.hpp" #include "ast_dumper.hpp" -#include "code_generator.hpp" +#include "qbe_ir_generator.hpp" #include "scope.hpp" #include "type_checker.hpp" #include "util.hpp" @@ -55,7 +55,7 @@ int main(int argc, char** argv) { auto ast_dumper = AstDumper{indenter}; program->Accept(ast_dumper); } - auto code_generator = CodeGenerator{}; + auto code_generator = QbeIrGenerator{}; program->Accept(code_generator); output.close(); diff --git a/src/code_generator.cpp b/src/qbe_ir_generator.cpp similarity index 89% rename from src/code_generator.cpp rename to src/qbe_ir_generator.cpp index 744ca588..8acc6eb9 100644 --- a/src/code_generator.cpp +++ b/src/qbe_ir_generator.cpp @@ -1,4 +1,4 @@ -#include "code_generator.hpp" +#include "qbe_ir_generator.hpp" #include #include @@ -72,7 +72,7 @@ auto num_recorder = PrevExprNumRecorder{}; } // namespace -void CodeGenerator::Visit(const DeclNode& decl) { +void QbeIrGenerator::Visit(const DeclNode& decl) { int id_num = NextLocalNum(); output << PrefixSigil(id_num) << " =l alloc4 4" << std::endl; @@ -86,7 +86,7 @@ void CodeGenerator::Visit(const DeclNode& decl) { id_to_num[decl.id_] = id_num; } -void CodeGenerator::Visit(const BlockStmtNode& block) { +void QbeIrGenerator::Visit(const BlockStmtNode& block) { output << "@start" << std::endl; for (const auto& decl : block.decls_) { decl->Accept(*this); @@ -96,27 +96,27 @@ void CodeGenerator::Visit(const BlockStmtNode& block) { } } -void CodeGenerator::Visit(const ProgramNode& program) { +void QbeIrGenerator::Visit(const ProgramNode& program) { output << "export function w $main() {" << std::endl; program.block_->Accept(*this); output << "}"; } -void CodeGenerator::Visit(const NullStmtNode&) { +void QbeIrGenerator::Visit(const NullStmtNode&) { /* do nothing */ } -void CodeGenerator::Visit(const ReturnStmtNode& ret_stmt) { +void QbeIrGenerator::Visit(const ReturnStmtNode& ret_stmt) { ret_stmt.expr_->Accept(*this); int ret_num = num_recorder.NumOfPrevExpr(); output << " ret " << PrefixSigil(ret_num) << std::endl; } -void CodeGenerator::Visit(const ExprStmtNode& expr_stmt) { +void QbeIrGenerator::Visit(const ExprStmtNode& expr_stmt) { expr_stmt.expr_->Accept(*this); } -void CodeGenerator::Visit(const IdExprNode& id_expr) { +void QbeIrGenerator::Visit(const IdExprNode& id_expr) { /// @brief Plays the role of a "pointer". Its value has to be loaded to /// the register before use. int id_num = id_to_num.at(id_expr.id_); @@ -126,13 +126,13 @@ void CodeGenerator::Visit(const IdExprNode& id_expr) { num_recorder.Record(reg_num); } -void CodeGenerator::Visit(const IntConstExprNode& int_expr) { +void QbeIrGenerator::Visit(const IntConstExprNode& int_expr) { int num = NextLocalNum(); output << PrefixSigil(num) << " =w copy " << int_expr.val_ << std::endl; num_recorder.Record(num); } -void CodeGenerator::Visit(const BinaryExprNode& bin_expr) { +void QbeIrGenerator::Visit(const BinaryExprNode& bin_expr) { bin_expr.lhs_->Accept(*this); int left_num = num_recorder.NumOfPrevExpr(); bin_expr.rhs_->Accept(*this); @@ -148,7 +148,7 @@ void CodeGenerator::Visit(const BinaryExprNode& bin_expr) { /// `BinaryExprNode`. /// @param classname A subclass of `BinaryExprNode`. #define DISPATCH_TO_VISIT_BINARY_EXPR(classname) \ - void CodeGenerator::Visit(const classname& expr) { \ + void QbeIrGenerator::Visit(const classname& expr) { \ Visit(static_cast(expr)); \ } @@ -166,7 +166,7 @@ DISPATCH_TO_VISIT_BINARY_EXPR(NotEqualToExprNode); #undef DISPATCH_TO_VISIT_BINARY_EXPR -void CodeGenerator::Visit(const SimpleAssignmentExprNode& assign_expr) { +void QbeIrGenerator::Visit(const SimpleAssignmentExprNode& assign_expr) { assign_expr.expr_->Accept(*this); int expr_num = num_recorder.NumOfPrevExpr(); output << "storew " << PrefixSigil(expr_num) << ", "