41#ifndef COMPILER_HIR_MODEL_H
42#define COMPILER_HIR_MODEL_H
59 if (
not lhs.is_valid())
61 if (
not rhs.is_valid())
222 template <
typename T,
typename...
Args>
229 [](
void *raw)
noexcept
231 static_cast<T *
>(raw)->~
T();
239 for (
size_t i =
owned.size(); i > 0; --i)
241 auto &[ptr, destroy] =
owned.access(i - 1);
242 if (destroy !=
nullptr and ptr !=
nullptr)
304 std::string
value =
"",
306 const bool b =
false)
noexcept
555namespace Compiler_HIR_Detail {
560 for (
const char ch : text)
586 out << std::string(indent,
' ');
591 if (types ==
nullptr)
594 return "<unknown-type>";
616 out <<
"<null-hir-expr>\n";
631 out <<
", " << (node->bool_value ?
"true" :
"false");
633 out <<
", " << node->text;
634 out <<
"): " <<
type_text(types, node->type_id) <<
'\n';
641 out <<
"Variable(" << node->
name <<
"): " <<
type_text(types, node->type_id) <<
'\n';
649 <<
"): " <<
type_text(types, node->type_id) <<
'\n';
658 <<
"): " <<
type_text(types, node->type_id) <<
'\n';
667 out <<
"Call: " <<
type_text(types, node->type_id) <<
'\n';
673 for (
size_t i = 0; i < node->arguments.size(); ++i)
674 dump_expr(node->arguments.access(i),
out, indent + 4, types);
688 out <<
"<null-hir-stmt>\n";
695 out <<
"InvalidStmt\n";
709 out <<
"Let(" << node->
name <<
"): " <<
type_text(types, node->binding_type) <<
'\n';
710 if (node->initializer !=
nullptr)
719 if (node->value !=
nullptr)
728 for (
size_t i = 0; i < node->statements.size(); ++i)
729 dump_stmt(node->statements.access(i),
out, indent + 2, types);
738 out <<
"Condition:\n";
743 if (node->else_branch !=
nullptr)
757 out <<
"Condition:\n";
780 std::ostringstream
out;
789 std::ostringstream
out;
798 std::ostringstream
out;
799 if (function ==
nullptr)
801 out <<
"<null-hir-function>\n";
809 for (
size_t i = 0; i < function->
parameters.size(); ++i)
827 std::ostringstream
out;
828 if (module ==
nullptr)
830 out <<
"<null-hir-module>\n";
834 out <<
"HIRModule\n";
835 for (
size_t i = 0; i <
module->functions.size(); ++i)
838 const auto *function =
module->functions.access(i);
839 if (function ==
nullptr)
841 out <<
"Function: <null>\n";
844 out <<
"Function(" << function->name <<
"): " << types.
to_string(function->type_id) <<
'\n';
845 if (function->parameters.size() > 0)
849 for (
size_t j = 0; j < function->parameters.size(); ++j)
851 const auto &
param = function->parameters.access(j);
863 for (
size_t i = 0; i <
module->statements.size(); ++i)
Reusable operator vocabulary shared by HIR, IR, and backend layers.
Stable type graph for the compiler-support MVP.
Memory arena for fast bulk allocations.
Exception handling system with formatted messages for Aleph-w.
#define ah_runtime_error_unless(C)
Throws std::runtime_error if condition does NOT hold.
size_t size_t int32_t value
size_t size_t int32_t * out
Arena allocator for fast bump-pointer allocation.
void reset() noexcept
Reset arena, making all memory available again.
size_t capacity() const noexcept
Get total arena capacity.
size_t allocated_size() const noexcept
Get total bytes currently allocated.
static constexpr size_t DEFAULT_SIZE
Default arena size (1 MB).
Arena-backed ownership context for HIR nodes.
Compiler_HIR_Context & operator=(const Compiler_HIR_Context &)=delete
T * make(Args &&...args)
Allocates and constructs one HIR object.
DynArray< Owned_Object > owned
void reset() noexcept
Destroys all tracked objects and rewinds the arena.
Compiler_HIR_Context(const size_t arena_size=AhArenaAllocator::DEFAULT_SIZE)
Constructs a context with arena_size bytes.
size_t allocated_size() const noexcept
Returns bytes currently consumed in the arena.
size_t capacity() const noexcept
Returns total arena capacity in bytes.
~Compiler_HIR_Context() noexcept
Compiler_HIR_Context(const Compiler_HIR_Context &)=delete
Context owning all compiler type nodes.
std::string to_string(const Compiler_Type_Id id) const
Renders one type to a deterministic human-readable string.
size_t blossom_maximum_cardinality_matching(const GT &g, DynDlist< typename GT::Arc * > &matching, SA sa=SA())
Alias of compute_maximum_cardinality_general_matching().
void dump_expr(const Compiler_HIR_Expr *expr, std::ostream &out, const size_t indent, const Compiler_Type_Context *types)
void dump_stmt(const Compiler_HIR_Stmt *stmt, std::ostream &out, const size_t indent, const Compiler_Type_Context *types)
std::string escape_text(std::string_view text)
std::string type_text(const Compiler_Type_Context *types, const Compiler_Type_Id id)
void append_indent(std::ostream &out, const size_t indent)
Main namespace for Aleph-w library functions.
const char * compiler_hir_stmt_kind_name(const Compiler_HIR_Stmt_Kind kind) noexcept
Returns a stable debug name for an HIR statement kind.
const char * compiler_hir_expr_kind_name(const Compiler_HIR_Expr_Kind kind) noexcept
Returns a stable debug name for an HIR expression kind.
@ Binary
dst <- op(lhs, rhs)
@ Return
Return one register value.
@ Call
dst <- callee(args...)
@ String
String literal payload.
@ Character
Character literal payload.
@ Invalid
Uninitialized register/slot or explicit invalid placeholder.
Source_Span compiler_hir_merge_spans(const Source_Span &lhs, const Source_Span &rhs) noexcept
Merges two spans without depending on the parser-oriented AST layer.
const char * compiler_operator_name(const Compiler_Operator_Kind kind) noexcept
Returns a stable debug name for one operator kind.
@ While
Loop while a condition is true.
@ If
Conditional execution.
@ Continue
Skip to the next iteration of the innermost loop.
@ Let
Variable declaration and optional initialization.
@ Block
Scoped sequence of statements.
@ Break
Immediate exit from the innermost loop.
std::string compiler_dump_hir_function(const Compiler_HIR_Function *function, const Compiler_Type_Context &types)
Dumps one HIR function in a deterministic text format.
and
Check uniqueness with explicit hash + equality functors.
Compiler_HIR_Constant_Kind
Kinds of HIR constants.
std::decay_t< typename HeadC::Item_Type > T
std::string compiler_dump_hir_module(const Compiler_HIR_Module *module, const Compiler_Type_Context &types)
Dumps one HIR module in a deterministic text format.
Compiler_HIR_Expr_Kind
Kinds of HIR expressions.
std::string compiler_dump_hir_expr(const Compiler_HIR_Expr *expr, const Compiler_Type_Context &types)
Dumps one HIR expression in a deterministic text format.
const char * compiler_hir_constant_kind_name(const Compiler_HIR_Constant_Kind kind) noexcept
Returns a stable debug name for an HIR constant kind.
Compiler_HIR_Stmt_Kind
Kinds of HIR statements.
static std::atomic< bool > init
std::string compiler_dump_hir_stmt(const Compiler_HIR_Stmt *stmt, const Compiler_Type_Context &types)
Dumps one HIR statement in a deterministic text format.
Compiler_Operator_Kind
Stable operator kinds shared by reusable compiler layers.
Compiler_HIR_Expr * left
Left operand.
Source_Span operator_span
Span of the operator token.
Compiler_Operator_Kind op
Binary operator kind.
Compiler_HIR_Expr * right
Right operand.
Compiler_HIR_Binary_Expr(Compiler_HIR_Expr *lhs=nullptr, const Compiler_Operator_Kind k=Compiler_Operator_Kind::Invalid, const Compiler_Type_Id ty=0, const Source_Span &op_sp={}, Compiler_HIR_Expr *rhs=nullptr) noexcept
Structured block statement.
Source_Span rbrace_span
Span of ‘’}'`.
Compiler_HIR_Block_Stmt(const Source_Span &lbrace={}, const Source_Span &rbrace={}) noexcept
DynArray< Compiler_HIR_Stmt * > statements
Nested statements.
Source_Span lbrace_span
Span of ‘’{'`.
Compiler_HIR_Break_Stmt(const Source_Span &sp={}) noexcept
Typed function call expression.
Compiler_HIR_Expr * callee
Called expression.
Compiler_HIR_Call_Expr(Compiler_HIR_Expr *func=nullptr, const Compiler_Type_Id ty=0, const Source_Span &lparen={}, const Source_Span &rparen={}) noexcept
DynArray< Compiler_HIR_Expr * > arguments
Call arguments.
Source_Span rparen_span
Span of ‘’)'`.
Source_Span lparen_span
Span of ‘’('`.
Typed constant expression.
bool bool_value
Decoded boolean value for Bool constants.
std::string text
Original or normalized constant spelling.
Compiler_HIR_Constant_Kind constant_kind
Constant category.
Compiler_HIR_Constant_Expr(const Compiler_HIR_Constant_Kind k=Compiler_HIR_Constant_Kind::Unit, const Compiler_Type_Id ty=0, std::string value="", const Source_Span &sp={}, const bool b=false) noexcept
void(* destroy)(void *) noexcept
continue statement in HIR.
Compiler_HIR_Continue_Stmt(const Source_Span &sp={}) noexcept
Expression statement in HIR.
Compiler_HIR_Eval_Stmt(Compiler_HIR_Expr *value=nullptr, const Source_Span &sp={}) noexcept
Compiler_HIR_Expr * expr
Evaluated expression.
Base class for HIR expressions.
Compiler_HIR_Expr(const Compiler_HIR_Expr_Kind k=Compiler_HIR_Expr_Kind::Invalid, const Compiler_Type_Id ty=0, const Source_Span &sp={}) noexcept
Compiler_HIR_Expr_Kind kind
Runtime node kind.
Compiler_Type_Id type_id
Inferred or resolved type.
std::string name
Function name.
DynArray< Compiler_HIR_Param > parameters
Parameters in declaration order.
Compiler_Type_Id type_id
Full function type.
Compiler_HIR_Function(std::string value="", const Compiler_Type_Id ty=0, const Source_Span &name_sp={}, Compiler_HIR_Block_Stmt *block=nullptr, const Source_Span &sp={}) noexcept
Compiler_HIR_Block_Stmt * body
Function body.
Source_Span name_span
Span of the function name.
Structured conditional statement.
Compiler_HIR_If_Stmt(Compiler_HIR_Expr *cond=nullptr, Compiler_HIR_Stmt *then_stmt=nullptr, Compiler_HIR_Stmt *else_stmt=nullptr, const Source_Span &if_sp={}, const Source_Span &else_sp={}) noexcept
Compiler_HIR_Stmt * then_branch
True branch.
Source_Span if_span
Span of if.
Source_Span else_span
Span of else, if present.
Compiler_HIR_Stmt * else_branch
Optional false branch.
Compiler_HIR_Expr * condition
Condition expression.
Invalid HIR expression placeholder.
Compiler_HIR_Invalid_Expr(const Compiler_Type_Id ty=0, const Source_Span &sp={}) noexcept
Invalid HIR statement placeholder.
Compiler_HIR_Invalid_Stmt(const Source_Span &sp={}) noexcept
Typed lexical binding statement.
Compiler_Type_Id binding_type
Inferred binding type.
Compiler_HIR_Expr * initializer
Optional initializer.
Source_Span name_span
Span of the binding name.
std::string name
Bound identifier.
Compiler_HIR_Let_Stmt(std::string value="", const Compiler_Type_Id ty=0, const Source_Span &name_sp={}, Compiler_HIR_Expr *init=nullptr, const Source_Span &sp={}) noexcept
Compiler_HIR_Module(const Source_Span &sp={}) noexcept
DynArray< Compiler_HIR_Function * > functions
Lowered top-level functions.
DynArray< Compiler_HIR_Stmt * > statements
Lowered top-level statements.
Base node carrying a source span.
Source_Span span
Source region associated with the HIR node.
Compiler_HIR_Node(const Source_Span &sp={}) noexcept
Parameter metadata for one HIR function.
Source_Span span
Parameter source span.
Compiler_Type_Id type_id
Inferred or declared parameter type.
std::string name
Parameter name.
Compiler_HIR_Return_Stmt(Compiler_HIR_Expr *expr=nullptr, const Source_Span &sp={}) noexcept
Compiler_HIR_Expr * value
Returned value, or nullptr.
Base class for HIR statements.
Compiler_HIR_Stmt(const Compiler_HIR_Stmt_Kind k=Compiler_HIR_Stmt_Kind::Invalid, const Source_Span &sp={}) noexcept
Compiler_HIR_Stmt_Kind kind
Runtime node kind.
Compiler_HIR_Expr * operand
Operand expression.
Compiler_HIR_Unary_Expr(const Compiler_Operator_Kind k=Compiler_Operator_Kind::Invalid, const Compiler_Type_Id ty=0, const Source_Span &op_sp={}, Compiler_HIR_Expr *expr=nullptr) noexcept
Compiler_Operator_Kind op
Unary operator kind.
Source_Span operator_span
Span of the operator token.
Typed variable reference expression.
Source_Span name_span
Span of the original identifier.
Compiler_HIR_Variable_Expr(std::string value="", const Compiler_Type_Id ty=0, const Source_Span &sp={}) noexcept
std::string name
Variable or function name.
Structured while-loop statement.
Compiler_HIR_While_Stmt(Compiler_HIR_Expr *cond=nullptr, Compiler_HIR_Stmt *stmt=nullptr, const Source_Span &kw={}) noexcept
Compiler_HIR_Stmt * body
Loop body.
Source_Span keyword_span
Span of while.
Compiler_HIR_Expr * condition
Loop condition.
Half-open byte range inside a source file.
Source_File_Id file_id
Stable file identifier; 0 means invalid.
Lazy and scalable dynamic array implementation.