chore: flat out the file structure

This commit is contained in:
2026-09-11 18:48:16 +02:00
parent 959d0a7773
commit 3b98f77c08
78 changed files with 76 additions and 40 deletions
+254
View File
@@ -0,0 +1,254 @@
#include "furc/middle/ir.hpp"
#include "furc/front/ast.hpp"
#include <stdexcept>
#include <utility>
namespace furc {
static ir_type ast_type_to_ir(const ast_type& type) {
switch (type.type) {
case ast_type::Void: return { ir_type::Void };
case ast_type::S8: return { ir_type::S8 };
case ast_type::U8: return { ir_type::U8 };
case ast_type::S16: return { ir_type::S16 };
case ast_type::U16: return { ir_type::U16 };
case ast_type::S32: return { ir_type::S32 };
case ast_type::U32: return { ir_type::U32 };
case ast_type::S64: return { ir_type::S64 };
case ast_type::U64: return { ir_type::U64 };
}
throw std::runtime_error("unreachable");
}
void ir_generator::visit_comp_stmt_node(const comp_stmt_node& node) {
// TODO: Introduce scopes for statements to naturally allow variable shadowing.
for (const auto& stmt : node.stmts) {
stmt->accept(*this);
}
}
void ir_generator::visit_if_stmt_node(const if_stmt_node& node) {
node.cond->accept(*this);
auto* branch = context().terminate(context().last_register(), context().blockIdx + 1, 0);
context().new_next();
node.thenBranch->accept(*this);
context().new_next();
branch->destination->value.blockPair.second = context().blockIdx; // NOLINT
if (node.elseBranch != nullptr) {
node.elseBranch->accept(*this);
context().new_next();
}
}
void ir_generator::visit_while_stmt_node(const while_stmt_node& node) {
context().new_next();
auto header = context().blockIdx;
node.cond->accept(*this);
auto* branch = context().terminate(context().last_register(), context().blockIdx + 1, 0);
context().new_next();
auto body = context().blockIdx;
node.body->accept(*this);
if (!context().blockPtr->is_terminated()) context().terminate(header);
context().new_next();
branch->destination->value.blockPair.second = context().blockIdx; // NOLINT
}
void ir_generator::visit_return_stmt_node(const return_stmt_node& node) {
if (node.value == nullptr) {
context().terminate();
} else {
node.value->accept(*this);
context().terminate(context().last_register());
}
}
void ir_generator::visit_var_decl_node(const var_decl_node& node) {
const auto* var = m_scope->allocate(m_module.arena, node.name, ast_type_to_ir(node.type));
if (node.init != nullptr) {
if (m_context.empty()) {
m_initContext.new_next();
m_context.push(std::move(m_initContext));
node.init->accept(*this);
context().add_instr(ir_instruction{ ir_instruction::Move, var->operand(), { context().last_register() } });
m_initContext = std::move(m_context.top());
m_context.pop();
} else {
node.init->accept(*this);
context().add_instr(ir_instruction{ ir_instruction::Move, var->operand(), { context().last_register() } });
}
}
}
void ir_generator::visit_func_decl_node(const func_decl_node& node) {
ir_function function;
function.previous = m_scope;
function.name = node.name;
for (const auto& param : node.params) {
function.params.push_back(ast_type_to_ir(param.type));
function.allocate(m_module.arena, param.name, ast_type_to_ir(param.type));
}
function.retType = ast_type_to_ir(node.type);
if (node.def.has_value()) {
auto* func = m_module.functions.emplace_back(m_module.arena.allocate<ir_function>(std::move(function)));
m_context.emplace(func);
m_scope = func;
node.def->body.accept(*this);
m_scope = m_scope->previous;
m_context.pop();
}
}
void ir_generator::visit_var_read_expr_node(const var_read_expr_node& node) {
const auto* var = m_scope->variable(node.name);
if (var == nullptr) throw std::runtime_error("unknown variable");
context().add_instr(ir_instruction{ ir_instruction::Move, context().next_register(), { var->operand() } });
}
void ir_generator::visit_func_call_expr_node(const func_call_expr_node& node) {
throw std::runtime_error("not implemented");
}
void ir_generator::visit_group_expr_node(const group_expr_node& node) {
node.inner->accept(*this);
}
void ir_generator::visit_binary_op_expr_node(const binary_op_expr_node& node) {
node.lhs->accept(*this);
auto lhs = context().last_register();
node.rhs->accept(*this);
auto rhs = context().last_register();
switch (node.type) {
case binary_op_expr_node::Add:
context().add_instr(ir_instruction{ ir_instruction::Add, context().next_register(), { lhs, rhs } });
return;
case binary_op_expr_node::Sub:
context().add_instr(ir_instruction{ ir_instruction::Sub, context().next_register(), { lhs, rhs } });
return;
case binary_op_expr_node::Mul:
context().add_instr(ir_instruction{ ir_instruction::Mul, context().next_register(), { lhs, rhs } });
return;
case binary_op_expr_node::Div:
context().add_instr(ir_instruction{ ir_instruction::Div, context().next_register(), { lhs, rhs } });
return;
case binary_op_expr_node::Mod:
context().add_instr(ir_instruction{ ir_instruction::Mod, context().next_register(), { lhs, rhs } });
return;
case binary_op_expr_node::Shl:
context().add_instr(ir_instruction{ ir_instruction::Shl, context().next_register(), { lhs, rhs } });
return;
case binary_op_expr_node::Shr:
context().add_instr(ir_instruction{ ir_instruction::Shr, context().next_register(), { lhs, rhs } });
return;
case binary_op_expr_node::BinAnd:
context().add_instr(ir_instruction{ ir_instruction::BinAnd, context().next_register(), { lhs, rhs } });
return;
case binary_op_expr_node::BinOr:
context().add_instr(ir_instruction{ ir_instruction::BinOr, context().next_register(), { lhs, rhs } });
return;
case binary_op_expr_node::BinXor:
context().add_instr(ir_instruction{ ir_instruction::BinXor, context().next_register(), { lhs, rhs } });
return;
case binary_op_expr_node::And:
context().add_instr(ir_instruction{ ir_instruction::And, context().next_register(), { lhs, rhs } });
return;
case binary_op_expr_node::Or:
context().add_instr(ir_instruction{ ir_instruction::Or, context().next_register(), { lhs, rhs } });
return;
case binary_op_expr_node::Equals:
context().add_instr(ir_instruction{ ir_instruction::Eq, context().next_register(), { lhs, rhs } });
return;
case binary_op_expr_node::NotEquals:
context().add_instr(ir_instruction{ ir_instruction::NotEq, context().next_register(), { lhs, rhs } });
return;
case binary_op_expr_node::LessThan:
context().add_instr(ir_instruction{ ir_instruction::LessThan, context().next_register(), { lhs, rhs } });
return;
case binary_op_expr_node::LessEquals:
context().add_instr(ir_instruction{ ir_instruction::LessEq, context().next_register(), { lhs, rhs } });
return;
case binary_op_expr_node::GreaterThan:
context().add_instr(ir_instruction{ ir_instruction::GreaterThan, context().next_register(), { lhs, rhs } });
return;
case binary_op_expr_node::GreaterEquals:
context().add_instr(ir_instruction{ ir_instruction::GreaterEq, context().next_register(), { lhs, rhs } });
return;
}
throw std::runtime_error("unreachable");
}
void ir_generator::visit_unary_op_expr_node(const unary_op_expr_node& node) {
if (node.type == unary_op_expr_node::PostInc || node.type == unary_op_expr_node::PostDec) {
node.lhs->accept(*this);
auto lhs = context().last_register();
node.lhs->accept(*this);
context().add_instr(node.type == unary_op_expr_node::PostInc ? ir_instruction::Increment
: ir_instruction::Decrement,
context().last_register());
return;
}
node.lhs->accept(*this);
auto lhs = context().last_register();
switch (node.type) {
case unary_op_expr_node::Positive: context().add_instr(ir_instruction::Positive, lhs);
case unary_op_expr_node::Negative: context().add_instr(ir_instruction::Negative, lhs);
case unary_op_expr_node::PreInc: context().add_instr(ir_instruction::Increment, lhs);
case unary_op_expr_node::PreDec: context().add_instr(ir_instruction::Decrement, lhs);
case unary_op_expr_node::BinNot: context().add_instr(ir_instruction::BinNot, lhs);
case unary_op_expr_node::Not: context().add_instr(ir_instruction::Not, lhs);
case unary_op_expr_node::Sizeof: context().add_instr(ir_instruction::Sizeof, lhs);
case unary_op_expr_node::Pointerof: context().add_instr(ir_instruction::Pointerof, lhs);
case unary_op_expr_node::Lengthof: context().add_instr(ir_instruction::Lenof, lhs);
case unary_op_expr_node::PostInc:
case unary_op_expr_node::PostDec: return;
}
throw std::runtime_error("unreachable");
}
void ir_generator::visit_if_expr_node(const if_expr_node& node) {
node.cond->accept(*this);
auto* branch = context().terminate(context().last_register(), context().blockIdx + 1, 0);
context().new_next();
auto thenBranch = context().blockIdx;
node.thenExpr->accept(*this);
auto thenReg = context().last_register();
context().new_next();
branch->destination->value.blockPair.second = context().blockIdx; // NOLINT
node.elseExpr->accept(*this);
auto elseReg = context().last_register();
auto resReg = context().next_register();
context().add_instr(ir_instruction{ ir_instruction::Move, resReg, { elseReg } });
context().new_next();
auto epilogue = context().blockIdx;
context().go(thenBranch);
context().add_instr(ir_instruction{ ir_instruction::Move, resReg, { thenReg } });
context().terminate(epilogue);
context().go(epilogue);
}
void ir_generator::visit_int_lit_node(const int_lit_node& node) {
context().add_instr(ir_instruction{ ir_instruction::Move,
context().next_register(),
{ ir_operand{ ir_operand::Integer, node.value } } });
}
void ir_generator::visit_char_lit_node(const char_lit_node& node) {
context().add_instr(ir_instruction{ ir_instruction::Move,
context().next_register(),
{ ir_operand{ ir_operand::Integer, static_cast<std::uint64_t>(node.value) } } });
}
} // namespace furc
+307
View File
@@ -0,0 +1,307 @@
/**
* Sources:
* - Practical Improvements to the Construction and Deconstruction of Static Single Assignment Form:
* https://web.archive.org/web/20100607003509/http://www.cs.rice.edu/~harv/my_papers/ssa.pdf
* - A Simple, Fast Dominance Algorithm:
* https://www.researchgate.net/publication/2569680_A_Simple_Fast_Dominance_Algorithm
*/
#include "furc/middle/ssa.hpp"
#include "furc/middle/ir.hpp"
#include <algorithm>
#include <cstddef>
#include <limits>
#include <stack>
#include <stdexcept>
#include <unordered_map>
#include <unordered_set>
#include <vector>
namespace furc {
void ssa::compute_cfg(const std::vector<ir_basic_block>& irBlocks, std::vector<cfg_block>& cfgBlocks) {
cfgBlocks.resize(irBlocks.size());
for (std::size_t i = 0; i < irBlocks.size(); ++i) {
const auto& block = irBlocks[i];
if (block.instructions.empty()) continue;
const auto& termInstr = block.instructions.back();
switch (termInstr.type) {
case ir_instruction::Branch: {
const auto& dst = termInstr.destination.value();
assert(dst.type == ir_operand::Block);
cfgBlocks[dst.value.block].preds.insert(i);
cfgBlocks[i].sucs.insert(dst.value.block);
} break;
case ir_instruction::BranchCond: {
const auto& dst = termInstr.destination.value();
assert(dst.type == ir_operand::BlockPair);
cfgBlocks[dst.value.blockPair.first].preds.insert(i);
cfgBlocks[dst.value.blockPair.second].preds.insert(i);
cfgBlocks[i].sucs.insert(dst.value.blockPair.first);
cfgBlocks[i].sucs.insert(dst.value.blockPair.second);
} break;
default: break;
}
}
}
void ssa::collect_registers(const std::vector<ir_basic_block>& irBlocks,
std::vector<register_info>& registers,
std::unordered_set<std::uint64_t>& globals) {
for (std::size_t i = 0; i < irBlocks.size(); ++i) {
const auto& block = irBlocks[i];
for (const auto& instr : block.instructions) {
for (const auto& src : instr.sources) {
if (src.type != ir_operand::Register) continue;
const auto& reg = registers.at(src.value.reg.name);
if (reg.sites.find(i) != reg.sites.end()) continue;
globals.insert(src.value.reg.name);
}
if (!instr.destination.has_value() || instr.destination->type != ir_operand::Register) continue;
registers[instr.destination->value.reg.name].sites.insert(i);
}
}
}
void ssa::build_dtree(const std::vector<cfg_block>& cfgBlocks,
std::vector<ssa_block>& ssaBlocks,
const std::vector<std::size_t>& order) {
ssaBlocks[order.front()].idom = order.front();
bool changed = true;
while (changed) {
changed = false;
for (auto it = order.begin() + 1; it != order.end(); ++it) {
static constexpr std::uint64_t INVALID = std::numeric_limits<std::uint64_t>::max();
std::uint64_t newIdom = -1;
bool found = false;
for (std::uint64_t pred : cfgBlocks[*it].preds) {
if (ssaBlocks[pred].idom == INVALID) continue;
newIdom = found ? intersect(ssaBlocks, pred, newIdom) : pred;
found = true;
}
if (ssaBlocks[*it].idom != newIdom) {
ssaBlocks[*it].idom = newIdom;
changed = true;
}
}
}
}
void ssa::compute_dfrontiers(const std::vector<cfg_block>& cfgBlocks, std::vector<ssa_block>& ssaBlocks) {
for (std::uint64_t i = 0; i < ssaBlocks.size(); ++i) {
if (cfgBlocks[i].preds.size() < 2) continue;
const auto& cfgBlock = cfgBlocks[i];
auto& ssaBlock = ssaBlocks[i];
for (std::uint64_t worker : cfgBlock.preds) {
while (worker != ssaBlock.idom) {
ssaBlocks[worker].df.insert(i);
worker = ssaBlocks[worker].idom;
}
}
}
}
void ssa::compute_rpo(std::vector<cfg_block>& cfgBlocks,
std::vector<ssa_block>& ssaBlocks,
std::vector<std::size_t>& order) {
std::unordered_set<std::size_t> visited;
if (!cfgBlocks.empty()) rpo_dfs(visited, order, 0, cfgBlocks);
std::reverse(order.begin(), order.end());
ssaBlocks.resize(cfgBlocks.size());
for (std::size_t i = 0; i < order.size(); ++i) {
ssaBlocks[order[i]].order = i;
}
}
void ssa::ssaification(std::vector<ir_basic_block>& irBlocks,
const std::vector<cfg_block>& cfgBlocks,
const std::vector<ssa_block>& ssaBlocks,
const std::vector<register_info>& registers,
const std::unordered_set<std::uint64_t>& globals) {
std::vector<std::uint64_t> worklist;
for (std::uint64_t i = 0; i < registers.size(); ++i) {
const auto& reg = registers[i];
if (reg.sites.size() < 2 || globals.find(i) == globals.end()) continue;
worklist.insert(worklist.end(), reg.sites.begin(), reg.sites.end());
std::unordered_set<std::uint64_t> done;
while (!worklist.empty()) {
const auto blockIdx = worklist.back();
worklist.pop_back();
for (auto frontier : ssaBlocks[blockIdx].df) {
if (done.find(frontier) != done.end()) continue;
done.insert(frontier);
auto& target = irBlocks[frontier];
ir_instruction instr = { ir_instruction::Phi, ir_operand{ ir_operand::Register, i } };
for (const auto& pred : cfgBlocks[frontier].preds)
instr.sources.emplace_back(ir_operand::PhiPair, i, pred);
target.instructions.emplace(target.instructions.begin(), std::move(instr));
if (reg.sites.find(frontier) == reg.sites.end()) worklist.push_back(frontier);
}
}
}
}
void ssa::rename(std::vector<ir_basic_block>& irBlocks,
std::size_t regCount,
const std::vector<cfg_block>& cfgBlocks,
std::vector<ssa_block>& ssaBlocks,
const std::vector<std::uint64_t>& order) {
std::vector<std::uint64_t> counters;
std::vector<std::stack<std::uint64_t>> stacks;
counters.resize(regCount);
stacks.resize(regCount);
for (auto it = order.begin() + 1; it != order.end(); ++it) {
std::uint64_t parent = ssaBlocks[*it].idom;
if (parent != std::numeric_limits<std::uint64_t>::max()) ssaBlocks[parent].children.emplace(*it);
}
rename_rec(counters, stacks, irBlocks, cfgBlocks, ssaBlocks, order.front());
}
void ssa::rename_rec(std::vector<std::uint64_t>& counters,
std::vector<std::stack<std::uint64_t>>& stacks,
std::vector<ir_basic_block>& irBlocks,
const std::vector<cfg_block>& cfgBlocks,
const std::vector<ssa_block>& ssaBlocks,
std::size_t blockIdx) {
std::unordered_map<std::uint64_t, std::size_t> pushed;
auto& block = irBlocks[blockIdx];
for (auto& instr : block.instructions) {
if (instr.type == ir_instruction::Phi) {
auto reg = instr.destination->value.reg.name;
stacks[reg].push(instr.destination->value.reg.ver = counters[reg]++);
++pushed[reg];
continue;
}
for (auto& op : instr.sources) {
if (op.type != ir_operand::Register) continue;
auto reg = op.value.reg.name;
op.value.reg.ver = stacks[reg].top();
}
if (!instr.destination.has_value() || instr.destination->type != ir_operand::Register) continue;
auto reg = instr.destination->value.reg.name;
stacks[reg].push(instr.destination->value.reg.ver = counters[reg]++);
++pushed[reg];
}
for (auto succIdx : cfgBlocks[blockIdx].sucs) {
auto& succ = irBlocks[succIdx];
for (auto& instr : succ.instructions) {
if (instr.type != ir_instruction::Phi) break;
for (auto& op : instr.sources) {
if (op.value.phiPair.block != blockIdx) continue;
op.value.phiPair.reg.ver = stacks[op.value.phiPair.reg.name].top();
}
}
}
for (std::uint64_t child : ssaBlocks[blockIdx].children)
rename_rec(counters, stacks, irBlocks, cfgBlocks, ssaBlocks, child);
for (auto [reg, count] : pushed)
while ((count--) > 0)
stacks[reg].pop();
}
void ssa::rpo_dfs(std::unordered_set<std::size_t>& visited,
std::vector<std::size_t>& order,
std::size_t block,
const std::vector<cfg_block>& blocks) {
visited.insert(block);
for (auto succ : blocks[block].sucs) {
if (visited.find(succ) != visited.end()) continue;
rpo_dfs(visited, order, succ, blocks);
}
order.push_back(block);
}
std::size_t ssa::intersect(std::vector<ssa_block>& m_blocks, std::size_t b1, std::size_t b2) {
while (b1 != b2) {
while (m_blocks[b1].order > m_blocks[b2].order)
b1 = m_blocks[b1].idom;
while (m_blocks[b2].order > m_blocks[b1].order)
b2 = m_blocks[b2].idom;
}
return b1;
}
// // 5. Renaming
// std::vector<std::uint64_t> counters;
// std::vector<std::stack<std::uint64_t>> stacks;
//
// counters.resize(func.regCount);
// stacks.resize(func.regCount);
//
// for (std::size_t i = 1; i < order.size(); ++i) {
// std::size_t parent = blocks[order[i]].idom;
// if (parent != std::numeric_limits<std::size_t>::max()) blocks[parent].children.emplace(order[i]);
// }
//
// auto rename = [&counters, &stacks, &blocks, &func](auto& self, std::size_t blockIdx) -> void {
// std::unordered_map<std::size_t, std::size_t> pushed;
//
// auto& block = func.blocks[blockIdx];
// for (auto& instr : block.instructions) {
// if (instr.type == ir_instruction::Phi) {
// auto reg = instr.destination->value.reg.name;
// auto idx = counters[reg]++;
// instr.destination->value.reg.ver = idx;
// stacks[reg].push(idx);
// ++pushed[reg];
// continue;
// }
//
// for (auto& op : instr.sources) {
// if (op.type != ir_operand::Register) continue;
// auto reg = op.value.reg.name;
// op.value.reg.ver = stacks[reg].top();
// }
//
// if (!instr.destination.has_value() || instr.destination->type != ir_operand::Register) continue;
// auto reg = instr.destination->value.reg.name;
// auto idx = counters[reg]++;
// instr.destination->value.reg.ver = idx;
// stacks[reg].push(idx);
// ++pushed[reg];
// }
//
// for (auto succIdx : blocks[blockIdx].sucs) {
// auto& succ = func.blocks[succIdx];
// for (auto& instr : succ.instructions) {
// if (instr.type != ir_instruction::Phi) break;
// for (auto& op : instr.sources) {
// if (op.value.phiPair.block != blockIdx) continue;
// op.value.phiPair.reg.ver = stacks[op.value.phiPair.reg.name].top();
// }
// }
// }
//
// for (std::size_t child : blocks[blockIdx].children)
// self(self, child);
//
// for (auto [reg, count] : pushed)
// while (count--)
// stacks[reg].pop();
// };
// rename(rename, order.front());
// }
} // namespace furc