36#include <gtest/gtest.h>
49 auto *
module = hir.make_module();
54 {types.integer_type(), types.integer_type()}, types.
integer_type()),
62 Compiler_Operator_Kind::Plus,
70 {types.integer_type(), types.integer_type()},
81 " Function(add): fn(Int, Int) -> Int\n"
82 " Params: x: Int, y: Int\n"
86 " Binary(Plus): Int\n"
92 " Variable(add): fn(Int, Int) -> Int\n"
94 " Constant(Integer, 40): Int\n"
95 " Constant(Integer, 2): Int\n");
104 auto *
module = ir.make_module();
110 {types.integer_type(), types.integer_type()}, types.
integer_type()));
114 Compiler_IR_Slot_Kind::Parameter,
119 Compiler_IR_Slot_Kind::Parameter,
130 *function, entry, Compiler_Operator_Kind::Plus, vx, vy, types.
integer_type());
146 {types.integer_type(), types.integer_type()}, types.
integer_type()));
164 " IRFunction(add): fn(Int, Int) -> Int\n"
168 " s0 [Parameter] x: Int\n"
169 " s1 [Parameter] y: Int\n"
170 " Block B0 [entry]\n"
172 " v1 = Load s0 : Int\n"
173 " v2 = Load s1 : Int\n"
174 " v3 = Binary(Plus) [v1, v2] : Int\n"
175 " Terminator: Return [v3]\n"
178 " Instructions: <none>\n"
179 " Terminator: Exit\n"
180 " Successors: <none>\n"
181 " IRFunction(<top-level>)\n"
184 " Block B0 [entry]\n"
186 " v1 = FunctionRef f0 : fn(Int, Int) -> Int\n"
187 " v2 = Constant(40) : Int\n"
188 " v3 = Constant(2) : Int\n"
189 " v4 = Call [v1, v2, v3] : Int\n"
190 " Store g0 [v4] : Int\n"
191 " Terminator: Jump\n"
194 " Instructions: <none>\n"
195 " Terminator: Exit\n"
196 " Successors: <none>\n");
Reusable builder helpers for constructing HIR directly.
Reusable builder helpers for constructing explicit IR directly.
Small reusable helper that allocates and connects HIR nodes.
static void append_top_level_statement(Compiler_HIR_Module &module, Compiler_HIR_Stmt *stmt)
Appends one top-level statement to module and updates the span.
Compiler_HIR_Let_Stmt * make_let_stmt(std::string name, const Compiler_Type_Id type_id, const Source_Span &name_span={}, Compiler_HIR_Expr *initializer=nullptr, const Source_Span &span={}) const
Allocates one lexical binding statement.
static void set_body(Compiler_HIR_Function &function, Compiler_HIR_Block_Stmt *body) noexcept
Attaches body to function and refreshes the aggregate span.
Compiler_HIR_Variable_Expr * make_variable(std::string name, const Compiler_Type_Id type_id=0, const Source_Span &span={}) const
Allocates one variable-reference expression.
Compiler_HIR_Call_Expr * make_call(Compiler_HIR_Expr *callee, const Compiler_Type_Id type_id=0, const Source_Span &lparen_span={}, const Source_Span &rparen_span={}) const
Allocates one call expression without arguments.
static void append_statement(Compiler_HIR_Block_Stmt &block, Compiler_HIR_Stmt *stmt)
Appends one nested statement to block and updates the span.
static Compiler_HIR_Param & add_parameter(Compiler_HIR_Function &function, std::string name, const Compiler_Type_Id type_id, const Source_Span &span={})
Adds one parameter metadata entry to function.
Compiler_HIR_Block_Stmt * make_block(const Source_Span &lbrace={}, const Source_Span &rbrace={}) const
Allocates one structured block statement.
static void append_function(Compiler_HIR_Module &module, Compiler_HIR_Function *function)
Appends one function to module and updates the module span.
Compiler_HIR_Binary_Expr * make_binary(Compiler_HIR_Expr *lhs, const Compiler_Operator_Kind op, Compiler_HIR_Expr *rhs, const Compiler_Type_Id type_id=0, const Source_Span &operator_span={}) const
Allocates one binary expression.
Compiler_HIR_Function * make_function(std::string name, const Compiler_Type_Id type_id=0, const Source_Span &name_span={}, Compiler_HIR_Block_Stmt *body=nullptr, const Source_Span &span={}) const
Allocates one function node without inserting it into any module.
Compiler_HIR_Return_Stmt * make_return_stmt(Compiler_HIR_Expr *value=nullptr, const Source_Span &span={}) const
Allocates one return statement.
Compiler_HIR_Constant_Expr * make_integer_constant(const long long value, const Compiler_Type_Id type_id, const Source_Span &span={}) const
Allocates one integer constant expression.
static void append_argument(Compiler_HIR_Call_Expr &call, Compiler_HIR_Expr *argument)
Appends one argument to call and updates the aggregate span.
Arena-backed ownership context for HIR nodes.
Small reusable helper that allocates and connects IR nodes.
static void emit_store_global(Compiler_IR_Function &function, const Compiler_IR_Block_Id block_id, const Compiler_IR_Global_Slot_Id slot_id, const Compiler_IR_Value_Id value_id, const Compiler_Type_Id type_id, const Source_Span &span={})
Emits one store into a global slot.
static Compiler_IR_Value_Id emit_constant(Compiler_IR_Function &function, const Compiler_IR_Block_Id block_id, const Compiler_Type_Id type_id, std::string text, const Source_Span &span={}, const bool bool_value=false)
Emits one constant instruction.
static Compiler_IR_Function_Id append_function(Compiler_IR_Module &module, Compiler_IR_Function *function)
Appends one non-top-level function to module and assigns its id.
static Compiler_IR_Local_Slot_Id add_local_slot(Compiler_IR_Function &function, const Compiler_IR_Slot_Kind kind, std::string name, const Compiler_Type_Id type_id, const Source_Span &span={})
Appends one local or parameter slot to function.
static void set_exit_block(Compiler_IR_Function &function, const Compiler_IR_Block_Id block_id)
Marks one block as the canonical exit block.
static Compiler_IR_Block_Id create_block(Compiler_IR_Function &function, std::string label)
Creates one empty block and returns its id.
static Compiler_IR_Value_Id emit_load_local(Compiler_IR_Function &function, const Compiler_IR_Block_Id block_id, const Compiler_IR_Local_Slot_Id slot_id, const Compiler_Type_Id type_id, const Source_Span &span={})
Emits one load from a local or parameter slot.
static void set_entry_block(Compiler_IR_Function &function, const Compiler_IR_Block_Id block_id)
Marks one block as the canonical entry block.
static Compiler_IR_Global_Slot_Id add_global_slot(Compiler_IR_Module &module, std::string name, const Compiler_Type_Id type_id, const Source_Span &span={})
Appends one global slot to module.
static void set_exit(Compiler_IR_Function &function, const Compiler_IR_Block_Id block_id, const Source_Span &span={})
Sets one block terminator to Exit.
static Compiler_IR_Value_Id emit_call(Compiler_IR_Function &function, const Compiler_IR_Block_Id block_id, const Compiler_IR_Value_Id callee, const DynArray< Compiler_IR_Value_Id > &arguments, const Compiler_Type_Id type_id, const Source_Span &span={})
Emits one call instruction.
static void set_return(Compiler_IR_Function &function, const Compiler_IR_Block_Id from, const Compiler_IR_Value_Id return_value, const Source_Span &span={})
Sets one return terminator and connects it to the exit block.
static void set_jump(Compiler_IR_Function &function, const Compiler_IR_Block_Id from, const Compiler_IR_Block_Id to, const Source_Span &span={})
Sets one jump terminator and predecessor/successor edge.
Compiler_IR_Function * make_function(std::string name, const Compiler_Type_Id type_id=0, const Source_Span &span={}, const Compiler_HIR_Function *source_function=nullptr) const
Allocates one function without inserting it into a module.
static Compiler_IR_Value_Id emit_binary(Compiler_IR_Function &function, const Compiler_IR_Block_Id block_id, const Compiler_Operator_Kind op, const Compiler_IR_Value_Id lhs, const Compiler_IR_Value_Id rhs, const Compiler_Type_Id type_id, const Source_Span &span={})
Emits one binary instruction.
static Compiler_IR_Value_Id emit_function_ref(Compiler_IR_Function &function, const Compiler_IR_Block_Id block_id, const Compiler_IR_Function_Id function_id, const Compiler_Type_Id type_id, const Source_Span &span={})
Emits one function reference instruction.
static Compiler_IR_Function * set_top_level(Compiler_IR_Module &module, Compiler_IR_Function *function)
Installs one top-level function body in module.
Arena-backed ownership context for IR nodes.
Context owning all compiler type nodes.
Compiler_Type_Id make_function_type(const DynArray< Compiler_Type_Id > ¶meters, const Compiler_Type_Id result)
Creates a function type.
Compiler_Type_Id integer_type() const noexcept
Returns the preloaded Int type id.
T & append()
Allocate a new entry to the end of array.
size_t blossom_maximum_cardinality_matching(const GT &g, DynDlist< typename GT::Arc * > &matching, SA sa=SA())
Alias of compute_maximum_cardinality_general_matching().
Main namespace for Aleph-w library functions.
std::string compiler_dump_ir_module(const Compiler_IR_Module *module, const Compiler_Type_Context *types=nullptr)
Dumps all lowered IR in one module deterministically.
Compiler_IR_Validation_Report validate_ir_module(const Compiler_IR_Module &module)
Validates all functions in one lowered IR module.
void exit(const char *file, int line, const char *format,...)
Print a message and exit the program.
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_IR_Validation_Report validate_ir_function(const Compiler_IR_Function &function, const Compiler_IR_Module *module=nullptr)
Validates one lowered IR function structurally.
T sum(const Container &container, const T &init=T{})
Compute sum of all elements.