-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
style: reorganize scarlet's parser and codegen (#58)
* reorganize parser * nit * done with parser reorg * reorg codegen * update docs
- Loading branch information
1 parent
9087598
commit a90adc3
Showing
34 changed files
with
2,869 additions
and
2,649 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#pragma once | ||
|
||
#include "codegen.hh" | ||
|
||
namespace scarlet { | ||
namespace codegen { | ||
|
||
#define SETVARCONSTANTREG(src) \ | ||
if (!variable_buffer.empty()) { \ | ||
src->set_type(scar::val_type::VAR); \ | ||
src->set_reg_name(variable_buffer); \ | ||
variable_buffer.clear(); \ | ||
} else if (!constant_buffer.empty()) { \ | ||
src->set_type(scar::val_type::CONSTANT); \ | ||
src->set_value(constant_buffer); \ | ||
constant_buffer.clear(); \ | ||
} else { \ | ||
src->set_type(scar::val_type::VAR); \ | ||
src->set_reg_name(get_prev_reg_name()); \ | ||
} | ||
|
||
} // namespace codegen | ||
} // namespace scarlet |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
#include <codegen/codegen.hh> | ||
|
||
namespace scarlet { | ||
namespace codegen { | ||
|
||
void Codegen::pretty_print() { | ||
std::cout << "Program(" << std::endl; | ||
for (auto function : scar.get_functions()) { | ||
std::cout << "\tFunction(" << std::endl; | ||
std::cout << "\t\tname=\"" << function->get_identifier()->get_value() | ||
<< "\"," << std::endl; | ||
std::cout << "\t\tbody=[" << std::endl; | ||
for (auto statement : function->get_instructions()) { | ||
std::cout << "\t\t\t" << to_string(statement->get_type()) << "("; | ||
if (statement->get_type() == scar::instruction_type::RETURN) { | ||
if (statement->get_src1()->get_type() == scar::val_type::CONSTANT) { | ||
std::cout << "Constant(" << statement->get_src1()->get_value() << ")"; | ||
} else if (statement->get_src1()->get_type() == scar::val_type::VAR) { | ||
std::cout << "Var(" << statement->get_src1()->get_reg() << ")"; | ||
} | ||
std::cout << ")" << std::endl; | ||
} else if (statement->get_type() == scar::instruction_type::UNARY) { | ||
std::cout << unop::to_string(statement->get_unop()) << ", "; | ||
if (statement->get_src1()->get_type() == scar::val_type::CONSTANT) { | ||
std::cout << "Constant(" << statement->get_src1()->get_value() << ")"; | ||
} else if (statement->get_src1()->get_type() == scar::val_type::VAR) { | ||
std::cout << "Var(" << statement->get_src1()->get_reg() << ")"; | ||
} | ||
std::cout << ", "; | ||
if (statement->get_dst()->get_type() == scar::val_type::VAR) { | ||
std::cout << "Var(" << statement->get_dst()->get_reg() << ")"; | ||
} else if (statement->get_dst()->get_type() == | ||
scar::val_type::CONSTANT) { | ||
std::cout << "Constant(" << statement->get_dst()->get_value() << ")"; | ||
} | ||
std::cout << ")" << std::endl; | ||
} else if (statement->get_type() == scar::instruction_type::BINARY) { | ||
std::cout << binop::to_string(statement->get_binop()) << ", "; | ||
if (statement->get_src1()->get_type() == scar::val_type::VAR) { | ||
std::cout << "Var(" << statement->get_src1()->get_reg() << ")"; | ||
} else if (statement->get_src1()->get_type() == | ||
scar::val_type::CONSTANT) { | ||
std::cout << "Constant(" << statement->get_src1()->get_value() << ")"; | ||
} | ||
std::cout << ", "; | ||
if (statement->get_src2()->get_type() == scar::val_type::VAR) { | ||
std::cout << "Var(" << statement->get_src2()->get_reg() << ")"; | ||
} else if (statement->get_src2()->get_type() == | ||
scar::val_type::CONSTANT) { | ||
std::cout << "Constant(" << statement->get_src2()->get_value() << ")"; | ||
} | ||
std::cout << ", "; | ||
if (statement->get_dst()->get_type() == scar::val_type::VAR) { | ||
std::cout << "Var(" << statement->get_dst()->get_reg() << ")"; | ||
} else if (statement->get_dst()->get_type() == | ||
scar::val_type::CONSTANT) { | ||
std::cout << "Constant(" << statement->get_dst()->get_value() << ")"; | ||
} | ||
std::cout << ")" << std::endl; | ||
} else if (statement->get_type() == scar::instruction_type::COPY) { | ||
if (statement->get_src1()->get_type() == scar::val_type::VAR) { | ||
std::cout << "Var(" << statement->get_src1()->get_reg() << ")"; | ||
} else if (statement->get_src1()->get_type() == | ||
scar::val_type::CONSTANT) { | ||
std::cout << "Constant(" << statement->get_src1()->get_value() << ")"; | ||
} | ||
std::cout << " ,"; | ||
if (statement->get_dst()->get_type() == scar::val_type::VAR) { | ||
std::cout << "Var(" << statement->get_dst()->get_reg() << ")"; | ||
} | ||
std::cout << ")" << std::endl; | ||
} else if (statement->get_type() == scar::instruction_type::JUMP or | ||
statement->get_type() == scar::instruction_type::LABEL) { | ||
std::cout << statement->get_src1()->get_value() << ")" << std::endl; | ||
} else if (statement->get_type() == | ||
scar::instruction_type::JUMP_IF_ZERO or | ||
statement->get_type() == | ||
scar::instruction_type::JUMP_IF_NOT_ZERO) { | ||
if (statement->get_src1()->get_type() == scar::val_type::VAR) { | ||
std::cout << "Var(" << statement->get_src1()->get_reg() << ")"; | ||
} else if (statement->get_src1()->get_type() == | ||
scar::val_type::CONSTANT) { | ||
std::cout << "Constant(" << statement->get_src1()->get_value() << ")"; | ||
} | ||
std::cout << ", "; | ||
std::cout << statement->get_dst()->get_value() << ")" << std::endl; | ||
} | ||
} | ||
std::cout << "\t\t]" << std::endl; | ||
std::cout << "\t)," << std::endl; | ||
} | ||
std::cout << ")" << std::endl; | ||
} | ||
|
||
} // namespace codegen | ||
} // namespace scarlet |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#include <codegen/codegen.hh> | ||
|
||
namespace scarlet { | ||
namespace codegen { | ||
|
||
void Codegen::gen_scar() { | ||
scar::scar_Program_Node scar_program; | ||
for (auto it : program.get_functions()) { | ||
MAKE_SHARED(scar::scar_Function_Node, scar_function); | ||
MAKE_SHARED(scar::scar_Identifier_Node, identifier); | ||
identifier->set_value(it->get_identifier()->get_value()); | ||
scar_function->set_identifier(identifier); | ||
|
||
gen_scar_block(it->get_block(), scar_function); | ||
|
||
// Add a complementary return 0 at the end of the function | ||
// in case there is no return statement | ||
MAKE_SHARED(scar::scar_Instruction_Node, scar_instruction); | ||
scar_instruction->set_type(scar::instruction_type::RETURN); | ||
MAKE_SHARED(scar::scar_Val_Node, scar_val_src); | ||
scar_val_src->set_type(scar::val_type::CONSTANT); | ||
scar_val_src->set_value("0"); | ||
scar_instruction->set_src1(scar_val_src); | ||
scar_function->add_instruction(scar_instruction); | ||
|
||
scar_program.add_function(scar_function); | ||
} | ||
|
||
this->scar = scar_program; | ||
} | ||
|
||
} // namespace codegen | ||
} // namespace scarlet |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#include <codegen/codegen.hh> | ||
|
||
namespace scarlet { | ||
namespace codegen { | ||
|
||
void Codegen::gen_scar_block( | ||
std::shared_ptr<ast::AST_Block_Node> block, | ||
std::shared_ptr<scar::scar_Function_Node> scar_function) { | ||
for (auto inst : block->get_blockItems()) { | ||
switch (inst->get_type()) { | ||
case ast::BlockItemType::STATEMENT: | ||
gen_scar_statement(inst->get_statement(), scar_function); | ||
break; | ||
case ast::BlockItemType::DECLARATION: | ||
gen_scar_declaration(inst->get_declaration(), scar_function); | ||
break; | ||
case ast::BlockItemType::UNKNOWN: | ||
UNREACHABLE() | ||
} | ||
} | ||
} | ||
|
||
} // namespace codegen | ||
} // namespace scarlet |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#include <codegen/common.hh> | ||
|
||
namespace scarlet { | ||
namespace codegen { | ||
|
||
void Codegen::gen_scar_declaration( | ||
std::shared_ptr<ast::AST_Declaration_Node> declaration, | ||
std::shared_ptr<scar::scar_Function_Node> scar_function) { | ||
// if there is no definition, we ignore it | ||
if (declaration->get_exp() != nullptr) { | ||
gen_scar_exp(declaration->get_exp(), scar_function); | ||
MAKE_SHARED(scar::scar_Instruction_Node, scar_instruction); | ||
scar_instruction->set_type(scar::instruction_type::COPY); | ||
MAKE_SHARED(scar::scar_Val_Node, scar_val_src); | ||
SETVARCONSTANTREG(scar_val_src); | ||
scar_instruction->set_src1(scar_val_src); | ||
MAKE_SHARED(scar::scar_Val_Node, scar_val_dst); | ||
scar_val_dst->set_type(scar::val_type::VAR); | ||
scar_val_dst->set_reg_name(declaration->get_identifier()->get_value()); | ||
scar_instruction->set_dst(scar_val_dst); | ||
scar_function->add_instruction(scar_instruction); | ||
} | ||
} | ||
|
||
} // namespace codegen | ||
} // namespace scarlet |
Oops, something went wrong.