36#include <gtest/gtest.h>
55 const auto file_id =
sm.add_virtual_file(
"main.aw", source);
57 const auto *
module = parser.parse_module();
85 "let value = add(1, 2);\n",
86 sm, dx, ast_ctx, hir_ctx, ir_ctx, typed);
98 " IRFunction(add): fn(Int, Int) -> Int\n"
102 " s0 [Parameter] x: Int\n"
103 " s1 [Parameter] y: Int\n"
104 " s2 [Local] z: Int\n"
105 " Block B0 [entry]\n"
107 " v1 = Load s0 : Int\n"
108 " v2 = Load s1 : Int\n"
109 " v3 = Binary(Plus) [v1, v2] : Int\n"
110 " Store s2 [v3] : Int\n"
111 " v4 = Load s2 : Int\n"
112 " Terminator: Return [v4]\n"
115 " Instructions: <none>\n"
116 " Terminator: Exit\n"
117 " Successors: <none>\n"
118 " IRFunction(<top-level>)\n"
121 " Block B0 [entry]\n"
123 " v1 = FunctionRef f0 : fn(Int, Int) -> Int\n"
124 " v2 = Constant(1) : Int\n"
125 " v3 = Constant(2) : Int\n"
126 " v4 = Call [v1, v2, v3] : Int\n"
127 " Store g0 [v4] : Int\n"
128 " Terminator: Jump\n"
131 " Instructions: <none>\n"
132 " Terminator: Exit\n"
133 " Successors: <none>\n");
148 " return x + base;\n"
150 sm, dx, ast_ctx, hir_ctx, ir_ctx, typed);
152 const auto & function = *ir->functions.access(0);
154 ASSERT_EQ(function.blocks.access(0).instructions.size(), 3u);
155 EXPECT_EQ(function.blocks.access(0).instructions.access(1).kind,
156 Compiler_IR_Instruction_Kind::Load);
157 EXPECT_EQ(function.blocks.access(0).instructions.access(1).global_slot_id, 0u);
173 " if (value == 0) {\n"
182 sm, dx, ast_ctx, hir_ctx, ir_ctx, typed);
184 const auto & function = *ir->functions.access(0);
189 EXPECT_EQ(function.blocks.access(2).terminator.kind,
190 Compiler_IR_Terminator_Kind::Branch);
207 sm, dx, ast_ctx, hir_ctx, ir_ctx, typed);
212 EXPECT_NE(
report.warnings.access(0).find(
"unreachable"), std::string::npos);
218 function.
name =
"manual";
225 entry.
label =
"entry";
233 exit.terminator.kind = Compiler_IR_Terminator_Kind::Exit;
238 wrong_target.terminator.kind = Compiler_IR_Terminator_Kind::Unreachable;
240 function.
blocks.append(entry);
247 EXPECT_NE(
report.errors.access(0).find(
"must point to exit block B1"), std::string::npos);
Lowering from the current MVP typed AST into the reusable HIR model.
Lowering from the current MVP HIR into the reusable explicit IR model.
Recursive-descent parser for the compiler-support MVP grammar.
Arena-backed ownership context for AST nodes.
Arena-backed ownership context for HIR nodes.
Lowers the MVP typed AST into HIR.
Arena-backed ownership context for IR nodes.
Lowers typed HIR into explicit-value IR.
Recursive-descent parser that produces an AST in Compiler_Ast_Context.
Inference-oriented semantic pass for the MVP compiler front-end.
void analyze_module(const Compiler_Module *module)
Runs the typed semantic analysis for module.
const Compiler_Type_Context & type_context() const noexcept
Returns the internal type context.
Diagnostic accumulator and renderer.
bool has_errors() const noexcept
Returns whether any error or fatal diagnostic was emitted.
T & append()
Allocate a new entry to the end of array.
Stores source files and resolves offsets into human-readable data.
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.
Compiler_IR_Validation_Report validate_ir_function(const Compiler_IR_Function &function, const Compiler_IR_Module *module=nullptr)
Validates one lowered IR function structurally.
One basic block of IR instructions.
std::string label
Deterministic debug label.
Compiler_IR_Terminator terminator
Explicit block terminator.
Compiler_IR_Block_Id id
Stable block id.
Lowered IR for one function or top-level body.
Compiler_IR_Value_Id next_value_id
Next value id to allocate while lowering.
DynArray< Compiler_IR_Block > blocks
Basic blocks in deterministic id order.
std::string name
Debug or source-level name.
Compiler_IR_Block_Id exit_block
Canonical exit block.
Compiler_IR_Block_Id entry_block
Canonical entry block.
Lowered IR module with shared global slots and functions.
Compiler_IR_Terminator_Kind kind
Terminator category.
DynArray< Compiler_IR_Block_Id > successors
Successor blocks in deterministic order.
Compiler_IR_Value_Id return_value
Return value, if any.