Skip to content

Commit

Permalink
Rename code generator to describe the target
Browse files Browse the repository at this point in the history
  • Loading branch information
Lai-YT authored and leewei05 committed Oct 4, 2023
1 parent cb9f407 commit 057d538
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 18 deletions.
8 changes: 4 additions & 4 deletions include/code_generator.hpp → include/qbe_ir_generator.hpp
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -28,4 +28,4 @@ class CodeGenerator : public NonModifyingVisitor {
void Visit(const SimpleAssignmentExprNode&) override;
};

#endif // CODE_GENERATOR_HPP_
#endif // QBE_IR_GENERATOR_HPP_
4 changes: 2 additions & 2 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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();
Expand Down
24 changes: 12 additions & 12 deletions src/code_generator.cpp → src/qbe_ir_generator.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "code_generator.hpp"
#include "qbe_ir_generator.hpp"

#include <cassert>
#include <fstream>
Expand Down Expand Up @@ -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;

Expand All @@ -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);
Expand All @@ -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_);
Expand All @@ -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);
Expand All @@ -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<const BinaryExprNode&>(expr)); \
}

Expand All @@ -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) << ", "
Expand Down

0 comments on commit 057d538

Please sign in to comment.