Implement basic IR generator

Signed-off-by: CHatingPython <chatingpython@gmail.com>
This commit is contained in:
CHatingPython
2026-05-31 16:15:53 +02:00
committed by CHatingPython
parent f431d9b725
commit 646d185d40
8 changed files with 324 additions and 6 deletions
+44
View File
@@ -0,0 +1,44 @@
#ifndef FURC_FRONT_IR_GENERATOR_HPP
#define FURC_FRONT_IR_GENERATOR_HPP
#include "furc/ast/fwd.hpp"
#include "furc/ast/visitor.hpp"
#include "furlang/ir/module.hpp"
#include <unordered_map>
namespace furc {
namespace front {
class ir_generator final : public ast::visitor {
public:
ir_generator() = default;
~ir_generator() override = default;
ir_generator(ir_generator&&) = default;
ir_generator& operator=(ir_generator&&) = default;
ir_generator(const ir_generator&) = default;
ir_generator& operator=(const ir_generator&) = default;
public:
furlang::ir::module&& move_module() { return std::move(m_module); }
public:
void visit_function_definition_node(const ast::function_definition_node& funcDef) override;
void visit_return_statement_node(const ast::return_statement_node& returnStmt) override;
void visit_string_literal_node(const ast::string_literal_node& node) override;
void visit_integer_literal_node(const ast::integer_literal_node& node) override;
void visit_var_read_expression_node(const ast::var_read_expression_node& node) override;
void visit_unaryop_expression_node(const ast::unaryop_expression_node& node) override;
void visit_binop_expression_node(const ast::binop_expression_node& node) override;
void visit_var_assign_expression_node(const ast::var_assign_expression_node& node) override;
private:
furlang::ir::module m_module;
std::shared_ptr<furlang::ir::block> m_currentBlock;
std::uint32_t m_registerCounter = 0;
std::unordered_map<std::string_view, std::uint32_t> m_variableMap;
};
} // namespace front
} // namespace furc
#endif // FURC_FRONT_IR_GENERATOR_HPP
+96
View File
@@ -0,0 +1,96 @@
#include "furc/front/ir_generator.hpp"
#include "furc/ast/declaration.hpp" // IWYU pragma: keep
#include "furc/ast/expression.hpp" // IWYU pragma: keep
#include "furc/ast/literal.hpp" // IWYU pragma: keep
#include "furc/ast/statement.hpp" // IWYU pragma: keep
#include <iostream>
namespace furc::front {
namespace {
namespace ir = furlang::ir;
}
void ir_generator::visit_function_definition_node(const ast::function_definition_node& funcDef) {
auto func = std::make_unique<furlang::ir::function>(std::string(funcDef.name()->string));
m_currentBlock = func->push();
if (funcDef.body().has_error()) {
std::cerr << funcDef.body().error() << '\n';
return;
}
for (const auto& stmt : funcDef.body()->statements) {
stmt->accept(*this);
}
m_module.push(std::move(func));
}
void ir_generator::visit_return_statement_node(const ast::return_statement_node& returnStmt) {
if (returnStmt.value().has_error()) {
std::cerr << returnStmt.value() << '\n';
}
if (returnStmt.value().present()) {
returnStmt.value()->accept(*this);
m_currentBlock->emplace<ir::return_instruction>(ir::operand::new_reg(m_registerCounter - 1));
} else {
m_currentBlock->emplace<ir::return_instruction>();
}
}
void ir_generator::visit_string_literal_node(const ast::string_literal_node& node) {
m_currentBlock->emplace<furlang::ir::assign_instruction>(ir::operand::new_string(std::string(*node.value())),
ir::operand::new_reg(m_registerCounter++));
}
void ir_generator::visit_integer_literal_node(const ast::integer_literal_node& node) {
m_currentBlock->emplace<furlang::ir::assign_instruction>(ir::operand::new_integer(*node.value()),
ir::operand::new_reg(m_registerCounter++));
}
void ir_generator::visit_var_read_expression_node(const ast::var_read_expression_node& node) {
throw std::runtime_error("unimplemented");
}
void ir_generator::visit_unaryop_expression_node(const ast::unaryop_expression_node& node) {
throw std::runtime_error("unimplemented");
}
static inline furlang::ir::binary_op_instruction_t binary_op_instruction_t(ast::binop_expression_node_t type) {
switch (type) {
case ast::binop_expression_node_t::Add: return furlang::ir::binary_op_instruction_t::Add;
case ast::binop_expression_node_t::Sub: return furlang::ir::binary_op_instruction_t::Sub;
case ast::binop_expression_node_t::Mul: return furlang::ir::binary_op_instruction_t::Mul;
case ast::binop_expression_node_t::Div: return furlang::ir::binary_op_instruction_t::Div;
case ast::binop_expression_node_t::Mod: return furlang::ir::binary_op_instruction_t::Mod;
case ast::binop_expression_node_t::Equal: return furlang::ir::binary_op_instruction_t::Eq;
case ast::binop_expression_node_t::NotEqual: return furlang::ir::binary_op_instruction_t::NotEq;
case ast::binop_expression_node_t::LessThan: return furlang::ir::binary_op_instruction_t::LessThan;
case ast::binop_expression_node_t::GreaterThan: return furlang::ir::binary_op_instruction_t::GreaterThan;
case ast::binop_expression_node_t::LessEqual: return furlang::ir::binary_op_instruction_t::LessEq;
case ast::binop_expression_node_t::GreaterEqual: return furlang::ir::binary_op_instruction_t::GreaterEq;
case ast::binop_expression_node_t::None:
default: throw std::runtime_error("unreachable");
}
}
void ir_generator::visit_binop_expression_node(const ast::binop_expression_node& node) {
node.lhs()->accept(*this);
std::uint32_t lhs = m_registerCounter - 1;
node.rhs()->accept(*this);
std::uint32_t rhs = m_registerCounter - 1;
std::uint32_t dst = m_registerCounter++;
m_currentBlock->emplace<furlang::ir::binary_op_instruction>(binary_op_instruction_t(node.type()),
ir::operand::new_reg(lhs),
ir::operand::new_reg(rhs),
ir::operand::new_reg(dst));
}
void ir_generator::visit_var_assign_expression_node(const ast::var_assign_expression_node& node) {
throw std::runtime_error("unimplemented");
}
} // namespace furc::front
+24 -4
View File
@@ -1,14 +1,34 @@
#ifndef LIBFURC // #ifndef LIBFURC
#include "furc/front/ir_generator.hpp"
#include "furc/front/parser.hpp" #include "furc/front/parser.hpp"
#include <iostream> #include <iostream>
int main(void) { int main(void) {
furc::front::parser parser("<TEMP>", "func main() {\n return x = y;\n}"); furc::front::parser parser("<TEMP>", "func main() {\n return 7 + 6 * 10;\n}");
std::cout << parser.parse() << '\n'; furc::front::ir_generator generator;
auto program = parser.parse();
if (program.has_error()) {
std::cerr << program << '\n';
}
program->accept(generator);
auto module = std::move(generator.move_module());
std::cout << "Generated IR:\n";
for (const auto& function : module.functions()) {
std::cout << function->name() << ":\n";
furlang::ir::block_index blockIndex = 0;
for (const auto& block : function->blocks()) {
std::cout << " # block " << blockIndex++ << '\n';
for (const auto& instruction : block->instructions()) {
std::cout << " " << *instruction << '\n';
}
}
}
return 0; return 0;
} }
#endif // LIBFURC // #endif // LIBFURC
+2 -2
View File
@@ -13,13 +13,13 @@ namespace ir {
// https://en.wikipedia.org/wiki/Basic_block // https://en.wikipedia.org/wiki/Basic_block
class block { class block {
public: public:
using value_type = std::shared_ptr<instruction>; using value_type = std::unique_ptr<instruction>;
public: public:
block() = default; block() = default;
public: public:
template <typename T, typename... Args, typename = std::enable_if_t<std::is_base_of_v<instruction, T>>> template <typename T, typename... Args, typename = std::enable_if_t<std::is_base_of_v<instruction, T>>>
void emplace(Args&&... args) { void emplace(Args&&... args) {
m_instructions.emplace_back(std::make_shared<T>(std::forward<Args>(args)...)); m_instructions.emplace_back(std::make_unique<T>(std::forward<Args>(args)...));
} }
std::vector<value_type>& instructions() { return m_instructions; } std::vector<value_type>& instructions() { return m_instructions; }
+35
View File
@@ -0,0 +1,35 @@
#ifndef FURLANG_IR_FUNCTION_HPP
#define FURLANG_IR_FUNCTION_HPP
#include "furlang/ir/block.hpp"
#include <memory>
#include <vector>
namespace furlang {
namespace ir {
class function {
public:
using value_type = std::shared_ptr<block>;
public:
function(const std::string& name)
: m_name(name) {}
function(std::string&& name)
: m_name(std::move(name)) {}
public:
const std::string& name() const { return m_name; }
value_type push() { return m_blocks.emplace_back(std::make_shared<block>()); }
const std::vector<value_type>& blocks() const { return m_blocks; }
private:
std::string m_name;
std::vector<value_type> m_blocks;
};
} // namespace ir
} // namespace furlang
#endif // FURLANG_IR_FUNCTION_HPP
@@ -4,6 +4,8 @@
#include "furlang/ir/operand.hpp" #include "furlang/ir/operand.hpp"
#include <cstdint> #include <cstdint>
#include <optional>
#include <ostream>
namespace furlang { namespace furlang {
namespace ir { namespace ir {
@@ -15,6 +17,7 @@ enum class instruction_t {
Call, Call,
Branch, Branch,
BranchCond, BranchCond,
Return,
}; };
class instruction { class instruction {
@@ -28,6 +31,26 @@ public:
instruction& operator=(const instruction&) = delete; instruction& operator=(const instruction&) = delete;
public: public:
virtual instruction_t type() const = 0; virtual instruction_t type() const = 0;
public:
friend std::ostream& operator<<(std::ostream& os, const instruction& instruction) { return instruction.print(os); }
protected:
virtual std::ostream& print(std::ostream& os) const = 0;
};
class alloca_instruction final : public instruction {
public:
alloca_instruction() {}
~alloca_instruction() override = default;
alloca_instruction(alloca_instruction&&) = default;
alloca_instruction& operator=(alloca_instruction&&) = default;
alloca_instruction(const alloca_instruction&) = delete;
alloca_instruction& operator=(const alloca_instruction&) = delete;
public:
instruction_t type() const override { return instruction_t::Alloca; }
protected:
std::ostream& print(std::ostream& os) const override { return os << "alloca"; }
}; };
class assign_instruction final : public instruction { class assign_instruction final : public instruction {
@@ -49,6 +72,10 @@ public:
private: private:
operand m_source; operand m_source;
operand m_destination; operand m_destination;
protected:
std::ostream& print(std::ostream& os) const override {
return os << "assign " << m_source << ", " << m_destination;
}
}; };
enum class binary_op_instruction_t { enum class binary_op_instruction_t {
@@ -66,6 +93,23 @@ enum class binary_op_instruction_t {
GreaterEq, GreaterEq,
}; };
static inline std::ostream& operator<<(std::ostream& os, binary_op_instruction_t type) {
switch (type) {
case binary_op_instruction_t::Add: return os << '+';
case binary_op_instruction_t::Sub: return os << '-';
case binary_op_instruction_t::Mul: return os << '*';
case binary_op_instruction_t::Div: return os << '/';
case binary_op_instruction_t::Mod: return os << '%';
case binary_op_instruction_t::Eq: return os << "==";
case binary_op_instruction_t::NotEq: return os << "!=";
case binary_op_instruction_t::LessThan: return os << '<';
case binary_op_instruction_t::GreaterThan: return os << '>';
case binary_op_instruction_t::LessEq: return os << "<=";
case binary_op_instruction_t::GreaterEq: return os << ">=";
}
return os;
}
class binary_op_instruction final : public instruction { class binary_op_instruction final : public instruction {
public: public:
binary_op_instruction(binary_op_instruction_t type, operand&& lhs, operand&& rhs, operand&& dst) binary_op_instruction(binary_op_instruction_t type, operand&& lhs, operand&& rhs, operand&& dst)
@@ -89,6 +133,10 @@ private:
operand m_lhs; operand m_lhs;
operand m_rhs; operand m_rhs;
operand m_dst; operand m_dst;
protected:
std::ostream& print(std::ostream& os) const override {
return os << "binop(" << m_type << ") " << m_lhs << ", " << m_rhs << ", " << m_dst;
}
}; };
using block_index = std::uint64_t; using block_index = std::uint64_t;
@@ -110,6 +158,8 @@ public:
block_index block() const { return m_block; } block_index block() const { return m_block; }
private: private:
block_index m_block; block_index m_block;
protected:
std::ostream& print(std::ostream& os) const override { return os << "branch #" << m_block; }
}; };
class branch_cond_instruction final : public instruction { class branch_cond_instruction final : public instruction {
@@ -133,6 +183,36 @@ private:
operand m_condition; operand m_condition;
block_index m_ifBlock; block_index m_ifBlock;
block_index m_elseBlock; block_index m_elseBlock;
protected:
std::ostream& print(std::ostream& os) const override {
return os << "branch_cond " << m_condition << ", #" << m_ifBlock << ", #" << m_elseBlock;
}
};
class return_instruction final : public instruction {
public:
return_instruction() {}
return_instruction(operand&& value)
: m_value(std::move(value)) {}
~return_instruction() override = default;
return_instruction(return_instruction&&) = default;
return_instruction& operator=(return_instruction&&) = default;
return_instruction(const return_instruction&) = delete;
return_instruction& operator=(const return_instruction&) = delete;
public:
instruction_t type() const override { return instruction_t::Return; }
const std::optional<operand>& value() const { return m_value; }
private:
std::optional<operand> m_value;
protected:
std::ostream& print(std::ostream& os) const override {
os << "return";
if (m_value.has_value()) os << ' ' << m_value.value();
return os;
}
}; };
} // namespace ir } // namespace ir
+32
View File
@@ -0,0 +1,32 @@
#ifndef FURLANG_IR_MODULE_HPP
#define FURLANG_IR_MODULE_HPP
#include "furlang/ir/function.hpp"
#include <memory>
#include <vector>
namespace furlang {
namespace ir {
class module {
public:
using value_type = std::unique_ptr<function>;
public:
module() = default;
public:
template <typename... Args>
value_type& push(Args&&... args) {
return m_functions.emplace_back(std::forward<Args>(args)...);
}
std::vector<value_type>& functions() { return m_functions; }
const std::vector<value_type>& functions() const { return m_functions; }
private:
std::vector<value_type> m_functions;
};
} // namespace ir
} // namespace furlang
#endif // FURLANG_IR_MODULE_HPP
+11
View File
@@ -2,6 +2,7 @@
#define FURLANG_IR_OPERAND_HPP #define FURLANG_IR_OPERAND_HPP
#include <cstdint> #include <cstdint>
#include <ostream>
#include <string> #include <string>
namespace furlang { namespace furlang {
@@ -97,6 +98,16 @@ public:
register_operand reg() const { return m_value.reg; } register_operand reg() const { return m_value.reg; }
integer_operand integer() const { return m_value.integer; } integer_operand integer() const { return m_value.integer; }
public:
friend std::ostream& operator<<(std::ostream& os, const operand& operand) {
switch (operand.m_type) {
case operand_t::None: return os << "none";
case operand_t::Register: return os << '%' << operand.m_value.reg;
case operand_t::Integer: return os << operand.m_value.integer;
case operand_t::String: return os << '"' << operand.m_value.string << '"';
}
return os;
}
private: private:
operand() = default; operand() = default;
private: private: