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
+2 -2
View File
@@ -13,13 +13,13 @@ namespace ir {
// https://en.wikipedia.org/wiki/Basic_block
class block {
public:
using value_type = std::shared_ptr<instruction>;
using value_type = std::unique_ptr<instruction>;
public:
block() = default;
public:
template <typename T, typename... Args, typename = std::enable_if_t<std::is_base_of_v<instruction, T>>>
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; }
+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 <cstdint>
#include <optional>
#include <ostream>
namespace furlang {
namespace ir {
@@ -15,6 +17,7 @@ enum class instruction_t {
Call,
Branch,
BranchCond,
Return,
};
class instruction {
@@ -28,6 +31,26 @@ public:
instruction& operator=(const instruction&) = delete;
public:
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 {
@@ -49,6 +72,10 @@ public:
private:
operand m_source;
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 {
@@ -66,6 +93,23 @@ enum class binary_op_instruction_t {
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 {
public:
binary_op_instruction(binary_op_instruction_t type, operand&& lhs, operand&& rhs, operand&& dst)
@@ -89,6 +133,10 @@ private:
operand m_lhs;
operand m_rhs;
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;
@@ -110,6 +158,8 @@ public:
block_index block() const { return m_block; }
private:
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 {
@@ -133,6 +183,36 @@ private:
operand m_condition;
block_index m_ifBlock;
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
+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
#include <cstdint>
#include <ostream>
#include <string>
namespace furlang {
@@ -97,6 +98,16 @@ public:
register_operand reg() const { return m_value.reg; }
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:
operand() = default;
private: