36#include <gtest/gtest.h>
59 function.
name =
"choose";
66 flag.
kind = Compiler_IR_Slot_Kind::Parameter;
72 value.kind = Compiler_IR_Slot_Kind::Local;
78 entry.
label =
"entry";
81 zero.
kind = Compiler_IR_Instruction_Kind::Constant;
104 exit.predecessors.append(3);
111 one.
kind = Compiler_IR_Instruction_Kind::Constant;
122 then_block.terminator.kind = Compiler_IR_Terminator_Kind::Jump;
136 join_block.terminator.kind = Compiler_IR_Terminator_Kind::Return;
147 two.
kind = Compiler_IR_Instruction_Kind::Constant;
158 else_block.terminator.kind = Compiler_IR_Terminator_Kind::Jump;
162 function.
blocks.append(entry);
175 function.
name =
"add_one";
182 param.kind = Compiler_IR_Slot_Kind::Parameter;
188 local.
kind = Compiler_IR_Slot_Kind::Local;
194 entry.
label =
"entry";
203 store.
kind = Compiler_IR_Instruction_Kind::Store;
209 load_x.
kind = Compiler_IR_Instruction_Kind::Load;
221 add.kind = Compiler_IR_Instruction_Kind::Binary;
223 add.op = Compiler_Operator_Kind::Plus;
224 add.operands.append(2);
225 add.operands.append(3);
233 exit.predecessors.append(0);
235 function.
blocks.append(entry);
247 global.kind = Compiler_IR_Slot_Kind::Global;
249 module.global_slots.append(global);
252 module.functions.append(function);
256 top->name =
"<top-level>";
257 top->entry_block = 0;
259 top->next_value_id = 3;
263 entry.
label =
"entry";
278 call.
kind = Compiler_IR_Instruction_Kind::Call;
294 exit.predecessors.append(0);
296 top->blocks.append(entry);
297 top->blocks.append(
exit);
298 module.top_level = top;
307 function.
name =
"explode";
314 entry.
label =
"entry";
317 lhs.
kind = Compiler_IR_Instruction_Kind::Constant;
323 rhs.
kind = Compiler_IR_Instruction_Kind::Constant;
329 div.kind = Compiler_IR_Instruction_Kind::Binary;
331 div.op = Compiler_Operator_Kind::Slash;
332 div.operands.append(1);
333 div.operands.append(2);
341 exit.predecessors.append(0);
343 function.
blocks.append(entry);
356 module.functions.append(&source);
364 const auto when_true =
vm.call_function(0, arguments);
385 auto module = make_module_with_top_level_call();
389 const auto executed =
vm.run_top_level();
391 EXPECT_EQ(executed.kind, Compiler_Bytecode_Completion_Kind::Halted);
393 EXPECT_TRUE(executed.value.runtime_value.is_unit());
395 const auto * answer =
vm.global_slot(0);
398 EXPECT_EQ(answer->runtime_value.kind, Interpreter_Value_Kind::Integer);
399 EXPECT_EQ(answer->runtime_value.integer_value, 42);
401 const auto dump =
vm.dump_globals();
403 EXPECT_NE(
dump.find(
"g0 answer = Int(42)"), std::string::npos);
405 delete module.functions.access(0);
406 delete module.top_level;
416 module.functions.append(&source);
422 const auto executed =
vm.call_function(0, arguments);
425 EXPECT_EQ(executed.kind, Compiler_Bytecode_Completion_Kind::Runtime_Error);
426 EXPECT_EQ(executed.error.code,
"BCV013");
427 EXPECT_EQ(executed.error.message,
"division by zero");
428 EXPECT_EQ(executed.error.function_name,
"explode");
Simple portable VM for executing the register bytecode in Bytecode.H.
size_t size_t int32_t value
Arena-backed ownership context for bytecode functions and modules.
Lowers explicit IR into register-based bytecode.
Small portable VM for executing one Compiler_Bytecode_Module.
T & access(const size_t i) const noexcept
Fast access without checking allocation and bound_min_clock checking.
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().
Compiler_SSA_Block & block(Compiler_SSA_Function &function, const Compiler_SSA_Block_Id id)
Main namespace for Aleph-w library functions.
constexpr size_t compiler_bytecode_invalid_id() noexcept
Returns the sentinel invalid bytecode id.
void exit(const char *file, int line, const char *format,...)
Print a message and exit the program.
constexpr size_t compiler_ir_invalid_id() noexcept
Returns the sentinel invalid IR id.
double add(double a, double b)
double div(double a, double b)
static Compiler_Bytecode_Value make_bool(const bool value)
Builds a boolean runtime value.
One basic block of IR instructions.
std::string label
Deterministic debug label.
Compiler_IR_Terminator terminator
Explicit block terminator.
DynArray< Compiler_IR_Instruction > instructions
Linear instruction list.
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.
DynArray< Compiler_IR_Slot > local_slots
Parameter/local slots in stable order.
Compiler_IR_Function_Id id
Stable function id within the module.
One instruction producing an optional explicit result value.
std::string text
Constant spelling or debug payload.
Compiler_IR_Instruction_Kind kind
Instruction category.
Compiler_IR_Value_Id result_id
Produced value id, or 0 for void instructions.
DynArray< Compiler_IR_Value_Id > operands
Input values in deterministic order.
Compiler_IR_Local_Slot_Id local_slot_id
Referenced local/parameter slot, when relevant.
Lowered IR module with shared global slots and functions.
One storage slot used by the IR.
std::string name
Debug name of the slot.
size_t id
Stable slot id within its storage space.
Compiler_IR_Slot_Kind kind
Slot storage category.
Compiler_IR_Terminator_Kind kind
Terminator category.
DynArray< Compiler_IR_Block_Id > successors
Successor blocks in deterministic order.
Compiler_IR_Value_Id condition_value
Branch condition value, if any.
Compiler_IR_Value_Id return_value
Return value, if any.
Compiler_SSA_Block_Id id
Dense SSA block id.
std::string label
Deterministic debug label.
Compiler_SSA_Terminator terminator
Explicit block terminator.
Compiler_IR_Terminator_Kind kind
Terminator category.