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;
87 init_store.
kind = Compiler_IR_Instruction_Kind::Store;
93 load_flag.
kind = Compiler_IR_Instruction_Kind::Load;
103 auto exit = make_exit_block();
104 exit.predecessors.append(3);
108 then_block.
label =
"if.then.0";
111 one.
kind = Compiler_IR_Instruction_Kind::Constant;
117 then_store.
kind = Compiler_IR_Instruction_Kind::Store;
128 join_block.
label =
"if.end.0";
131 load_value.
kind = Compiler_IR_Instruction_Kind::Load;
144 else_block.
label =
"if.else.0";
147 two.
kind = Compiler_IR_Instruction_Kind::Constant;
153 else_store.
kind = Compiler_IR_Instruction_Kind::Store;
162 function.
blocks.append(entry);
164 function.
blocks.append(then_block);
165 function.
blocks.append(join_block);
166 function.
blocks.append(else_block);
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";
197 constant.
kind = Compiler_IR_Instruction_Kind::Constant;
203 store.
kind = Compiler_IR_Instruction_Kind::Store;
209 load_x.
kind = Compiler_IR_Instruction_Kind::Load;
215 load_tmp.
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);
232 auto exit = make_exit_block();
233 exit.predecessors.append(0);
235 function.
blocks.append(entry);
241 make_module_with_top_level_call()
247 global.kind = Compiler_IR_Slot_Kind::Global;
248 global.name =
"answer";
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";
266 function_ref.
kind = Compiler_IR_Instruction_Kind::Function_Ref;
272 constant.
kind = Compiler_IR_Instruction_Kind::Constant;
274 constant.
text =
"41";
278 call.kind = Compiler_IR_Instruction_Kind::Call;
280 call.operands.append(1);
281 call.operands.append(2);
285 store_global.
kind = Compiler_IR_Instruction_Kind::Store;
293 auto exit = make_exit_block();
294 exit.predecessors.append(0);
296 top->blocks.append(entry);
297 top->blocks.append(
exit);
298 module.top_level = top;
303 make_divide_by_zero_ir()
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);
340 auto exit = make_exit_block();
341 exit.predecessors.append(0);
343 function.
blocks.append(entry);
349TEST(CompilerBytecodeInterpreter, ExecutesBranchFunction)
355 auto source = make_branch_ir();
356 module.functions.append(&source);
365 ASSERT_TRUE(when_true.ok());
366 EXPECT_EQ(when_true.kind, Compiler_Bytecode_Completion_Kind::Returned);
367 ASSERT_TRUE(when_true.value.is_runtime_value());
368 EXPECT_EQ(when_true.value.runtime_value.kind, Interpreter_Value_Kind::Integer);
369 EXPECT_EQ(when_true.value.runtime_value.integer_value, 1);
373 ASSERT_TRUE(when_false.ok());
374 EXPECT_EQ(when_false.kind, Compiler_Bytecode_Completion_Kind::Returned);
375 ASSERT_TRUE(when_false.value.is_runtime_value());
376 EXPECT_EQ(when_false.value.runtime_value.kind, Interpreter_Value_Kind::Integer);
377 EXPECT_EQ(when_false.value.runtime_value.integer_value, 2);
380TEST(CompilerBytecodeInterpreter, ExecutesTopLevelAndStoresGlobals)
385 auto module = make_module_with_top_level_call();
390 ASSERT_TRUE(executed.ok());
391 EXPECT_EQ(executed.kind, Compiler_Bytecode_Completion_Kind::Halted);
392 ASSERT_TRUE(executed.value.is_runtime_value());
393 EXPECT_TRUE(executed.value.runtime_value.is_unit());
396 ASSERT_NE(answer,
nullptr);
397 ASSERT_TRUE(answer->is_runtime_value());
398 EXPECT_EQ(answer->runtime_value.kind, Interpreter_Value_Kind::Integer);
399 EXPECT_EQ(answer->runtime_value.integer_value, 42);
402 EXPECT_NE(dump.find(
"BytecodeGlobals"), std::string::npos);
403 EXPECT_NE(dump.find(
"g0 answer = Int(42)"), std::string::npos);
405 delete module.functions.access(0);
406 delete module.top_level;
409TEST(CompilerBytecodeInterpreter, ReportsStructuredRuntimeErrors)
415 auto source = make_divide_by_zero_ir();
416 module.functions.append(&source);
424 ASSERT_FALSE(executed.ok());
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.
Compiler_Bytecode_Module * lower_module(const Compiler_IR_Module *module) const
Lowers one IR module to bytecode.
Small portable VM for executing one Compiler_Bytecode_Module.
std::string dump_globals() const
Returns a deterministic dump of the current global state.
Compiler_Bytecode_Execution_Result call_function(const Compiler_Bytecode_Function_Id function_id, const DynArray< Compiler_Bytecode_Value > &arguments, const size_t max_steps=0)
Executes one bytecode function directly.
Compiler_Bytecode_Execution_Result run_top_level(const size_t max_steps=0)
Executes the module top-level body.
const Compiler_Bytecode_Value * global_slot(const Compiler_IR_Global_Slot_Id slot_id) const noexcept
Returns one global slot value if the slot id is valid.
T & access(const size_t i) noexcept
Fast access without checking allocation and bound_min_clock checking.
T & append()
Allocate a new entry to the end of array.
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_Block_Id > predecessors
Predecessor block ids.
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.
Compiler_IR_Function_Id function_id
Referenced function id, when relevant.
Compiler_IR_Global_Slot_Id global_slot_id
Referenced global 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.