feat: first prototype of optimizers + alloca optimizer

This commit is contained in:
2026-01-01 22:08:43 +01:00
parent 8a2d98e69e
commit 0b9cb7e7d9
12 changed files with 274 additions and 40 deletions

View File

@@ -1,7 +1,7 @@
#pragma once
#include <unordered_map>
#include "parser/nodes.hpp"
#include "prelude/string.hpp"
#include "parser/ast.hpp"
#include "ir/value.hpp"
#include "ir/ops.hpp"
@@ -26,7 +26,7 @@ namespace IR
{
auto value = ParseExpression(varDecl->value());
// TODO: gather type information from var decl signature, aka local <int> v = 0;
auto dst = AllocateNamed<Pointer>();
auto dst = AllocateNamed<Pointer>(value->GetType());
m_ops->Push(new AllocateOp(dst, value->GetType()));
m_ops->Push(new StoreOp(value, reinterpret_cast<Pointer *>(dst)));
m_locals.insert(std::make_pair(varDecl->name(), reinterpret_cast<Pointer *>(dst)));
@@ -34,14 +34,14 @@ namespace IR
ValueHandle *ParseVariable(const VariableNode *var)
{
// auto dst = AllocateValue();
// m_ops->Push(new LoadOp(dst, var->name()));
if (m_locals.find(var->name()) == m_locals.end())
{
// TODO: throw proper error
assert(0 && "ERROR: variable does not exist");
}
return reinterpret_cast<ValueHandle *>(m_locals[var->name()]);
auto dst = AllocateNamed<Instruction>(m_locals[var->name()]->GetValueType());
m_ops->Push(new LoadOp(dst, m_locals[var->name()]));
return reinterpret_cast<ValueHandle *>(dst);
}
ValueHandle *ParseFnCall(const FnCallNode *fn)
@@ -54,7 +54,7 @@ namespace IR
argRegs.Push(arg);
}
// TODO: gather return type of the function
auto dst = AllocateUnnamed<Void>();
auto dst = VoidValue();
m_ops->Push(new CallOp(dst, fn->name(), argRegs.view()));
return dst;
}
@@ -75,7 +75,7 @@ namespace IR
}
assert(0 && "unreachable");
return reinterpret_cast<ValueHandle *>(new Void(0));
return reinterpret_cast<ValueHandle *>(new Void());
}
ValueHandle *ParseExpression(const Node *expression)
@@ -187,12 +187,18 @@ namespace IR
return new V(ValueHandle::kNoId, std::forward<Args>(args)...);
}
ValueHandle *VoidValue()
{
return new Void();
}
private:
const Node *m_root = nullptr;
OpBuilder *m_ops = nullptr;
unsigned int m_value_counter = 0;
unsigned int m_block_counter = 0;
std::unordered_map<StringView, Pointer *> m_locals;
Builder<OpBuilder *> m_containers;