Skip to content

Commit

Permalink
Rename to TransUnitNode and remove checking main function
Browse files Browse the repository at this point in the history
  • Loading branch information
leewei05 authored and Lai-YT committed Jun 22, 2024
1 parent f37a274 commit 10f19db
Show file tree
Hide file tree
Showing 57 changed files with 78 additions and 85 deletions.
17 changes: 9 additions & 8 deletions include/ast.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -220,8 +220,8 @@ struct CompoundStmtNode : public StmtNode {
std::vector<std::unique_ptr<StmtNode>> stmts;
};

/// @brief An external definition is an external declaration that is also a
/// definition of a function(other than an inline definition) or an object.
/// @brief An external declaration can be a definition of a function or an
/// object.
struct ExternDeclNode : public AstNode {
ExternDeclNode(
Location loc,
Expand All @@ -236,17 +236,18 @@ struct ExternDeclNode : public AstNode {
decl;
};

/// @brief Root of the entire program.
struct ProgramNode : public AstNode {
/// @brief A translation unit, which the compiler handles individually,
/// representing a high-level entity in the compilation process.
struct TransUnitNode : public AstNode {
/// @note vector of move-only elements are move-only
ProgramNode(Location loc,
std::vector<std::unique_ptr<ExternDeclNode>> trans_unit)
: AstNode{loc}, trans_unit{std::move(trans_unit)} {}
TransUnitNode(Location loc,
std::vector<std::unique_ptr<ExternDeclNode>> extern_decls)
: AstNode{loc}, extern_decls{std::move(extern_decls)} {}

void Accept(NonModifyingVisitor&) const override;
void Accept(ModifyingVisitor&) override;

std::vector<std::unique_ptr<ExternDeclNode>> trans_unit;
std::vector<std::unique_ptr<ExternDeclNode>> extern_decls;
};

struct IfStmtNode : public StmtNode {
Expand Down
2 changes: 1 addition & 1 deletion include/ast_dumper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class AstDumper : public NonModifyingVisitor {
void Visit(const FuncDefNode&) override;
void Visit(const CompoundStmtNode&) override;
void Visit(const ExternDeclNode&) override;
void Visit(const ProgramNode&) override;
void Visit(const TransUnitNode&) override;
void Visit(const IfStmtNode&) override;
void Visit(const WhileStmtNode&) override;
void Visit(const ForStmtNode&) override;
Expand Down
2 changes: 1 addition & 1 deletion include/qbe_ir_generator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class QbeIrGenerator : public NonModifyingVisitor {
void Visit(const FuncDefNode&) override;
void Visit(const CompoundStmtNode&) override;
void Visit(const ExternDeclNode&) override;
void Visit(const ProgramNode&) override;
void Visit(const TransUnitNode&) override;
void Visit(const IfStmtNode&) override;
void Visit(const WhileStmtNode&) override;
void Visit(const ForStmtNode&) override;
Expand Down
3 changes: 1 addition & 2 deletions include/type_checker.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class TypeChecker : public ModifyingVisitor {
void Visit(FuncDefNode&) override;
void Visit(CompoundStmtNode&) override;
void Visit(ExternDeclNode&) override;
void Visit(ProgramNode&) override;
void Visit(TransUnitNode&) override;
void Visit(IfStmtNode&) override;
void Visit(WhileStmtNode&) override;
void Visit(ForStmtNode&) override;
Expand Down Expand Up @@ -52,7 +52,6 @@ class TypeChecker : public ModifyingVisitor {

private:
ScopeStack& env_;
bool has_main_func_{false};

/// @brief Installs the built-in functions into the environment.
void InstallBuiltins_(ScopeStack&);
Expand Down
4 changes: 2 additions & 2 deletions include/visitor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ struct FuncDefNode;
struct LoopInitNode;
struct CompoundStmtNode;
struct ExternDeclNode;
struct ProgramNode;
struct TransUnitNode;
struct IfStmtNode;
struct WhileStmtNode;
struct ForStmtNode;
Expand Down Expand Up @@ -85,7 +85,7 @@ class Visitor {
virtual void Visit(CondMut<LoopInitNode>&){};
virtual void Visit(CondMut<CompoundStmtNode>&){};
virtual void Visit(CondMut<ExternDeclNode>&){};
virtual void Visit(CondMut<ProgramNode>&){};
virtual void Visit(CondMut<TransUnitNode>&){};
virtual void Visit(CondMut<IfStmtNode>&){};
virtual void Visit(CondMut<WhileStmtNode>&){};
virtual void Visit(CondMut<ForStmtNode>&){};
Expand Down
10 changes: 5 additions & 5 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ int main( // NOLINT(bugprone-exception-escape): Using a big try-catch block to
}

/// @brief The root node of the program.
auto program = std::unique_ptr<AstNode>{};
yy::parser parser{program};
auto trans_unit = std::unique_ptr<AstNode>{};
yy::parser parser{trans_unit};
int ret = parser.parse();

// NOLINTNEXTLINE(cppcoreguidelines-owning-memory)
Expand All @@ -85,19 +85,19 @@ int main( // NOLINT(bugprone-exception-escape): Using a big try-catch block to
// perform analyses and transformations on the ast
auto scopes = ScopeStack{};
TypeChecker type_checker{scopes};
program->Accept(type_checker);
trans_unit->Accept(type_checker);
if (opts["dump"].as<bool>()) {
const auto max_level = 80u;
AstDumper ast_dumper{Indenter{' ', Indenter::SizePerLevel{2},
Indenter::MaxLevel{max_level}}};
program->Accept(ast_dumper);
trans_unit->Accept(ast_dumper);
}

// generate intermediate representation
auto input_basename = input_path.stem().string();
auto output_ir = std::ofstream{fmt::format("{}.ssa", input_basename)};
QbeIrGenerator code_generator{output_ir};
program->Accept(code_generator);
trans_unit->Accept(code_generator);

output_ir.close();

Expand Down
4 changes: 2 additions & 2 deletions parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ std::unique_ptr<Type> ResolveType(std::unique_ptr<Type> resolved_type,
%language "c++"
%locations

%parse-param {std::unique_ptr<AstNode>& program}
%parse-param {std::unique_ptr<AstNode>& trans_unit}

// Use complete symbols (parser::symbol_type).
%define api.token.constructor
Expand Down Expand Up @@ -156,7 +156,7 @@ std::unique_ptr<Type> ResolveType(std::unique_ptr<Type> resolved_type,

%%
entry: trans_unit {
program = std::make_unique<ProgramNode>(Loc(@1), $1);
trans_unit = std::make_unique<TransUnitNode>(Loc(@1), $1);
}
;

Expand Down
4 changes: 2 additions & 2 deletions src/ast.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,11 @@ void CompoundStmtNode::Accept(ModifyingVisitor& v) {
v.Visit(*this);
}

void ProgramNode::Accept(NonModifyingVisitor& v) const {
void TransUnitNode::Accept(NonModifyingVisitor& v) const {
v.Visit(*this);
}

void ProgramNode::Accept(ModifyingVisitor& v) {
void TransUnitNode::Accept(ModifyingVisitor& v) {
v.Visit(*this);
}

Expand Down
9 changes: 5 additions & 4 deletions src/ast_dumper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -199,11 +199,12 @@ void AstDumper::Visit(const ExternDeclNode& extern_decl) {
indenter_.DecreaseLevel();
}

void AstDumper::Visit(const ProgramNode& program) {
std::cout << indenter_.Indent() << "ProgramNode <" << program.loc << ">\n";
void AstDumper::Visit(const TransUnitNode& trans_unit) {
std::cout << indenter_.Indent() << "TransUnitNode <" << trans_unit.loc
<< ">\n";
indenter_.IncreaseLevel();
for (const auto& trans_unit : program.trans_unit) {
trans_unit->Accept(*this);
for (const auto& extern_decl : trans_unit.extern_decls) {
extern_decl->Accept(*this);
}
indenter_.DecreaseLevel();
}
Expand Down
4 changes: 2 additions & 2 deletions src/qbe_ir_generator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -316,12 +316,12 @@ void QbeIrGenerator::Visit(const ExternDeclNode& extern_decl) {
extern_decl.decl);
}

void QbeIrGenerator::Visit(const ProgramNode& program) {
void QbeIrGenerator::Visit(const TransUnitNode& trans_unit) {
// Generate the data of builtin functions.
Write_("data {} = align 1 {{ b \"%d\\012\\000\", }}\n",
user_defined::GlobalPointer{"__builtin_print_format"});

for (const auto& extern_decl : program.trans_unit) {
for (const auto& extern_decl : trans_unit.extern_decls) {
extern_decl->Accept(*this);
}
}
Expand Down
12 changes: 2 additions & 10 deletions src/type_checker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,10 +186,6 @@ void TypeChecker::Visit(FuncDefNode& func_def) {
// TODO: redefinition of function id
}

if (func_def.id == "main") {
has_main_func_ = true;
}

env_.PushScope(ScopeKind::kFunc);
// NOTE: This block scope will be merged with the function body. Don't pop it.
env_.PushScope(ScopeKind::kBlock);
Expand Down Expand Up @@ -255,17 +251,13 @@ void TypeChecker::Visit(ExternDeclNode& extern_decl) {
extern_decl.decl);
}

void TypeChecker::Visit(ProgramNode& program) {
void TypeChecker::Visit(TransUnitNode& trans_unit) {
env_.PushScope(ScopeKind::kFile);
InstallBuiltins_(env_);
for (auto& extern_decl : program.trans_unit) {
for (auto& extern_decl : trans_unit.extern_decls) {
extern_decl->Accept(*this);
}

if (!has_main_func_) {
// TODO: no main function
assert(false);
}
env_.PopScope();
}

Expand Down
2 changes: 1 addition & 1 deletion test/typecheck/array.exp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ProgramNode <1:1>
TransUnitNode <1:1>
ExternDeclNode <1:1>
FuncDefNode <1:5> main: int ()
CompoundStmtNode <1:12>
Expand Down
2 changes: 1 addition & 1 deletion test/typecheck/array_param.exp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ProgramNode <2:1>
TransUnitNode <2:1>
ExternDeclNode <2:1>
FuncDefNode <2:5> func: int (int*)
ParamNode <2:14> a: int*
Expand Down
2 changes: 1 addition & 1 deletion test/typecheck/assignment_expr.exp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ProgramNode <1:1>
TransUnitNode <1:1>
ExternDeclNode <1:1>
FuncDefNode <1:5> main: int ()
CompoundStmtNode <1:12>
Expand Down
2 changes: 1 addition & 1 deletion test/typecheck/bin_expr.exp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ProgramNode <1:1>
TransUnitNode <1:1>
ExternDeclNode <1:1>
FuncDefNode <1:5> main: int ()
CompoundStmtNode <1:12>
Expand Down
2 changes: 1 addition & 1 deletion test/typecheck/break_stmt.exp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ProgramNode <1:1>
TransUnitNode <1:1>
ExternDeclNode <1:1>
FuncDefNode <1:5> main: int ()
CompoundStmtNode <1:12>
Expand Down
2 changes: 1 addition & 1 deletion test/typecheck/comma_expr.exp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ProgramNode <1:1>
TransUnitNode <1:1>
ExternDeclNode <1:1>
FuncDefNode <1:5> main: int ()
CompoundStmtNode <1:12>
Expand Down
2 changes: 1 addition & 1 deletion test/typecheck/comment.exp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ProgramNode <4:1>
TransUnitNode <4:1>
ExternDeclNode <4:1>
FuncDefNode <4:5> main: int ()
CompoundStmtNode <4:40>
Expand Down
2 changes: 1 addition & 1 deletion test/typecheck/comp_expr.exp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ProgramNode <1:1>
TransUnitNode <1:1>
ExternDeclNode <1:1>
FuncDefNode <1:5> main: int ()
CompoundStmtNode <1:12>
Expand Down
2 changes: 1 addition & 1 deletion test/typecheck/compound_stmt.exp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ProgramNode <1:1>
TransUnitNode <1:1>
ExternDeclNode <1:1>
FuncDefNode <1:5> main: int ()
CompoundStmtNode <1:12>
Expand Down
2 changes: 1 addition & 1 deletion test/typecheck/cond_expr.exp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ProgramNode <1:1>
TransUnitNode <1:1>
ExternDeclNode <1:1>
FuncDefNode <1:5> main: int ()
CompoundStmtNode <1:12>
Expand Down
2 changes: 1 addition & 1 deletion test/typecheck/continue_stmt.exp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ProgramNode <1:1>
TransUnitNode <1:1>
ExternDeclNode <1:1>
FuncDefNode <1:5> main: int ()
CompoundStmtNode <1:12>
Expand Down
2 changes: 1 addition & 1 deletion test/typecheck/decl.exp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ProgramNode <1:1>
TransUnitNode <1:1>
ExternDeclNode <1:1>
FuncDefNode <1:5> main: int ()
CompoundStmtNode <1:12>
Expand Down
2 changes: 1 addition & 1 deletion test/typecheck/do_while_single_stmt.exp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ProgramNode <1:1>
TransUnitNode <1:1>
ExternDeclNode <1:1>
FuncDefNode <1:5> main: int ()
CompoundStmtNode <1:12>
Expand Down
2 changes: 1 addition & 1 deletion test/typecheck/do_while_stmt.exp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ProgramNode <1:1>
TransUnitNode <1:1>
ExternDeclNode <1:1>
FuncDefNode <1:5> main: int ()
CompoundStmtNode <1:12>
Expand Down
2 changes: 1 addition & 1 deletion test/typecheck/for_infinite_loop.exp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ProgramNode <1:1>
TransUnitNode <1:1>
ExternDeclNode <1:1>
FuncDefNode <1:5> main: int ()
CompoundStmtNode <1:12>
Expand Down
2 changes: 1 addition & 1 deletion test/typecheck/for_nested_stmt.exp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ProgramNode <1:1>
TransUnitNode <1:1>
ExternDeclNode <1:1>
FuncDefNode <1:5> main: int ()
CompoundStmtNode <1:12>
Expand Down
2 changes: 1 addition & 1 deletion test/typecheck/for_stmt.exp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ProgramNode <1:1>
TransUnitNode <1:1>
ExternDeclNode <1:1>
FuncDefNode <1:5> main: int ()
CompoundStmtNode <1:12>
Expand Down
2 changes: 1 addition & 1 deletion test/typecheck/func_call.exp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ProgramNode <1:1>
TransUnitNode <1:1>
ExternDeclNode <1:1>
FuncDefNode <1:5> five: int ()
CompoundStmtNode <1:12>
Expand Down
2 changes: 1 addition & 1 deletion test/typecheck/func_call_param.exp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ProgramNode <1:1>
TransUnitNode <1:1>
ExternDeclNode <1:1>
FuncDefNode <1:5> sum: int (int, int, int)
ParamNode <1:13> a: int
Expand Down
2 changes: 1 addition & 1 deletion test/typecheck/func_pointer.exp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ProgramNode <1:1>
TransUnitNode <1:1>
ExternDeclNode <1:1>
FuncDefNode <1:5> add: int (int, int)
ParamNode <1:13> a: int
Expand Down
2 changes: 1 addition & 1 deletion test/typecheck/func_pointer_param.exp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ProgramNode <1:1>
TransUnitNode <1:1>
ExternDeclNode <1:1>
FuncDefNode <1:5> add: int (int, int)
ParamNode <1:13> a: int
Expand Down
2 changes: 1 addition & 1 deletion test/typecheck/global_decl.exp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ProgramNode <1:1>
TransUnitNode <1:1>
ExternDeclNode <1:1>
DeclStmtNode <1:1>
VarDeclNode <1:5> c: int
Expand Down
2 changes: 1 addition & 1 deletion test/typecheck/goto_stmt.exp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ProgramNode <1:1>
TransUnitNode <1:1>
ExternDeclNode <1:1>
FuncDefNode <1:5> main: int ()
CompoundStmtNode <1:12>
Expand Down
2 changes: 1 addition & 1 deletion test/typecheck/id_expr.exp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ProgramNode <1:1>
TransUnitNode <1:1>
ExternDeclNode <1:1>
FuncDefNode <1:5> main: int ()
CompoundStmtNode <1:12>
Expand Down
2 changes: 1 addition & 1 deletion test/typecheck/identifier.exp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ProgramNode <1:1>
TransUnitNode <1:1>
ExternDeclNode <1:1>
FuncDefNode <1:5> main: int ()
CompoundStmtNode <1:12>
Expand Down
2 changes: 1 addition & 1 deletion test/typecheck/if_else_nested_single_stmt.exp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ProgramNode <1:1>
TransUnitNode <1:1>
ExternDeclNode <1:1>
FuncDefNode <1:5> main: int ()
CompoundStmtNode <1:12>
Expand Down
2 changes: 1 addition & 1 deletion test/typecheck/if_else_nested_stmt.exp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ProgramNode <1:1>
TransUnitNode <1:1>
ExternDeclNode <1:1>
FuncDefNode <1:5> main: int ()
CompoundStmtNode <1:12>
Expand Down
2 changes: 1 addition & 1 deletion test/typecheck/if_else_single_stmt.exp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ProgramNode <1:1>
TransUnitNode <1:1>
ExternDeclNode <1:1>
FuncDefNode <1:5> main: int ()
CompoundStmtNode <1:12>
Expand Down
2 changes: 1 addition & 1 deletion test/typecheck/if_else_stmt.exp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ProgramNode <1:1>
TransUnitNode <1:1>
ExternDeclNode <1:1>
FuncDefNode <1:5> main: int ()
CompoundStmtNode <1:12>
Expand Down
2 changes: 1 addition & 1 deletion test/typecheck/if_single_stmt.exp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ProgramNode <1:1>
TransUnitNode <1:1>
ExternDeclNode <1:1>
FuncDefNode <1:5> main: int ()
CompoundStmtNode <1:12>
Expand Down
Loading

0 comments on commit 10f19db

Please sign in to comment.