Skip to content

Commit

Permalink
IR gen cleanup.
Browse files Browse the repository at this point in the history
  • Loading branch information
Goubermouche committed Jan 28, 2024
1 parent 1ebb2ac commit 41a2de9
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 41 deletions.
48 changes: 24 additions & 24 deletions source/ir_translator/ir_translator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,41 +12,41 @@ namespace sigma {
m_context.semantics.reset_active_scope();
}

handle<ir::node> ir_translator::translate_node(handle<ast::node> ast_node) {
switch (ast_node->type) {
case ast::node_type::FUNCTION_DECLARATION: translate_function_declaration(ast_node); break;
case ast::node_type::FUNCTION_CALL: return translate_function_call(ast_node);

auto ir_translator::translate_node(handle<ast::node> ast_node) -> handle<ir::node> {
switch(ast_node->type) {
// declarations
case ast::node_type::NAMESPACE_DECLARATION: translate_namespace_declaration(ast_node); break;
case ast::node_type::SIZEOF: return translate_sizeof(ast_node);

// flow control
case ast::node_type::RETURN: translate_return(ast_node); break;
case ast::node_type::CONDITIONAL_BRANCH: translate_conditional_branch(ast_node, nullptr); break;

// variables
case ast::node_type::FUNCTION_DECLARATION: translate_function_declaration(ast_node); break;
case ast::node_type::VARIABLE_DECLARATION: translate_variable_declaration(ast_node); break;
case ast::node_type::STORE: return translate_variable_assignment(ast_node);

case ast::node_type::LOAD: return translate_load(ast_node);

case ast::node_type::VARIABLE_ACCESS: return translate_variable_access(ast_node);
case ast::node_type::ARRAY_ACCESS: return translate_array_access(ast_node);
// literals
case ast::node_type::NUMERICAL_LITERAL: return translate_numerical_literal(ast_node);
case ast::node_type::CHARACTER_LITERAL: return translate_character_literal(ast_node);
case ast::node_type::STRING_LITERAL: return translate_string_literal(ast_node);
case ast::node_type::BOOL_LITERAL: return translate_bool_literal(ast_node);

// operators:
// expressions
case ast::node_type::OPERATOR_ADD:
case ast::node_type::OPERATOR_SUBTRACT:
case ast::node_type::OPERATOR_MULTIPLY:
case ast::node_type::OPERATOR_DIVIDE:
case ast::node_type::OPERATOR_MODULO: return translate_binary_math_operator(ast_node);

// statements
case ast::node_type::RETURN: translate_return(ast_node); break;
case ast::node_type::CONDITIONAL_BRANCH: translate_conditional_branch(ast_node, nullptr); break;

// loads / stores
case ast::node_type::VARIABLE_ACCESS: return translate_variable_access(ast_node);
case ast::node_type::ARRAY_ACCESS: return translate_array_access(ast_node);
case ast::node_type::STORE: return translate_store(ast_node);
case ast::node_type::LOAD: return translate_load(ast_node);

// other
case ast::node_type::FUNCTION_CALL: return translate_function_call(ast_node);
case ast::node_type::SIZEOF: return translate_sizeof(ast_node);
case ast::node_type::CAST: return translate_cast(ast_node);

// literals
case ast::node_type::NUMERICAL_LITERAL: return translate_numerical_literal(ast_node);
case ast::node_type::CHARACTER_LITERAL: return translate_character_literal(ast_node);
case ast::node_type::STRING_LITERAL: return translate_string_literal(ast_node);
case ast::node_type::BOOL_LITERAL: return translate_bool_literal(ast_node);
default: PANIC("irgen for node '{}' is not implemented", ast_node->type.to_string());
}

Expand Down Expand Up @@ -306,7 +306,7 @@ namespace sigma {
return m_context.semantics.get_variable(accessed_variable.key)->value;
}

auto ir_translator::translate_variable_assignment(handle<ast::node> assignment_node) -> handle<ir::node> {
auto ir_translator::translate_store(handle<ast::node> assignment_node) -> handle<ir::node> {
const auto& variable = assignment_node->children[0]->get<ast::named_type_expression>();
const u16 alignment = variable.type.get_byte_width();

Expand Down
37 changes: 22 additions & 15 deletions source/ir_translator/ir_translator.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,24 @@ namespace sigma {
ir_translator(backend_context& context);
auto translate() -> utility::result<void>;

handle<ir::node> translate_node(handle<ast::node> ast_node);
auto translate_node(handle<ast::node> ast_node) -> handle<ir::node>;

// declarations
void translate_namespace_declaration(handle<ast::node> namespace_node);
void translate_function_declaration(handle<ast::node> function_node);
void translate_variable_declaration(handle<ast::node> variable_node);
void translate_namespace_declaration(handle<ast::node> namespace_node);

// literals
auto translate_numerical_literal(handle<ast::node> numerical_literal_node) const -> handle<ir::node>;
auto translate_character_literal(handle<ast::node> character_literal_node) const -> handle<ir::node>;
auto translate_string_literal(handle<ast::node> string_literal_node) const -> handle<ir::node>;
auto translate_bool_literal(handle<ast::node> bool_literal_node) const -> handle<ir::node>;

// expressions
auto translate_binary_math_operator(handle<ast::node> operator_node) -> handle<ir::node>;

// statements
void translate_return(handle<ast::node> return_node);
auto translate_sizeof(handle<ast::node> sizeof_node) const -> handle<ir::node>;

/**
* \brief Translates a conditional ast branch node into IR.
Expand All @@ -36,22 +46,19 @@ namespace sigma {
*/
void translate_branch(handle<ast::node> branch_node, handle<ir::node> exit_control);

// loads / stores
auto translate_variable_access(handle<ast::node> access_node) const -> handle<ir::node>;
auto translate_array_access(handle<ast::node> access_node) -> handle<ir::node>;
auto translate_variable_access(handle<ast::node> access_node) const->handle<ir::node>;

auto translate_numerical_literal(handle<ast::node> numerical_literal_node) const->handle<ir::node>;
auto translate_character_literal(handle<ast::node> character_literal_node) const->handle<ir::node>;
auto translate_string_literal(handle<ast::node> string_literal_node) const->handle<ir::node>;
auto translate_bool_literal(handle<ast::node> bool_literal_node) const->handle<ir::node>;

auto translate_binary_math_operator(handle<ast::node> operator_node) -> handle<ir::node>;
auto translate_cast(handle<ast::node> cast_node) -> handle<ir::node>;
auto translate_function_call(handle<ast::node> call_node) -> handle<ir::node>;
auto translate_store(handle<ast::node> assignment_node) -> handle<ir::node>;
auto translate_load(handle<ast::node> load_node) -> handle<ir::node>;

auto translate_variable_assignment(handle<ast::node> assignment_node) -> handle<ir::node>;
// other
auto translate_function_call(handle<ast::node> call_node) -> handle<ir::node>;
auto translate_cast(handle<ast::node> cast_node) -> handle<ir::node>;
auto translate_sizeof(handle<ast::node> sizeof_node) const -> handle<ir::node>;

auto literal_to_ir(const ast::named_type_expression& literal) const-> handle<ir::node>;
// utility
auto literal_to_ir(const ast::named_type_expression& literal) const -> handle<ir::node>;
private:
backend_context& m_context;
};
Expand Down
2 changes: 1 addition & 1 deletion source/type_checker/type_checker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ namespace sigma {

auto type_checker::type_check_node(ast_node target, ast_node parent, data_type expected) -> type_check_result {
switch(target->type) {
// declaration
// declarations
case ast::node_type::NAMESPACE_DECLARATION: return type_check_namespace_declaration(target);
case ast::node_type::FUNCTION_DECLARATION: return type_check_function_declaration(target);
case ast::node_type::VARIABLE_DECLARATION: return type_check_variable_declaration(target);
Expand Down
2 changes: 1 addition & 1 deletion source/type_checker/type_checker.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ namespace sigma {

auto type_check_node(ast_node target, ast_node parent, data_type expected = {}) -> type_check_result;

// declaration
// declarations
auto type_check_namespace_declaration(ast_node declaration) -> type_check_result;
auto type_check_function_declaration(ast_node declaration) -> type_check_result;
auto type_check_variable_declaration(ast_node declaration) -> type_check_result;
Expand Down

0 comments on commit 41a2de9

Please sign in to comment.