36#include <gtest/gtest.h>
59 function.
name =
"add_one";
66 param.
kind = Compiler_IR_Slot_Kind::Parameter;
72 local.
kind = Compiler_IR_Slot_Kind::Local;
78 entry.
label =
"entry";
81 constant.
kind = Compiler_IR_Instruction_Kind::Constant;
87 store.
kind = Compiler_IR_Instruction_Kind::Store;
93 load_x.
kind = Compiler_IR_Instruction_Kind::Load;
99 load_tmp.
kind = Compiler_IR_Instruction_Kind::Load;
105 add.kind = Compiler_IR_Instruction_Kind::Binary;
107 add.op = Compiler_Operator_Kind::Plus;
108 add.operands.append(2);
109 add.operands.append(3);
116 auto exit = make_exit_block();
117 exit.predecessors.append(0);
119 function.
blocks.append(entry);
129 function.
name =
"choose";
136 flag.
kind = Compiler_IR_Slot_Kind::Parameter;
142 value.kind = Compiler_IR_Slot_Kind::Local;
143 value.name =
"value";
148 entry.
label =
"entry";
151 zero.
kind = Compiler_IR_Instruction_Kind::Constant;
157 init_store.
kind = Compiler_IR_Instruction_Kind::Store;
163 load_flag.
kind = Compiler_IR_Instruction_Kind::Load;
173 auto exit = make_exit_block();
174 exit.predecessors.append(3);
178 then_block.
label =
"if.then.0";
181 one.
kind = Compiler_IR_Instruction_Kind::Constant;
187 then_store.
kind = Compiler_IR_Instruction_Kind::Store;
198 join_block.
label =
"if.end.0";
201 load_value.
kind = Compiler_IR_Instruction_Kind::Load;
214 else_block.
label =
"if.else.0";
217 two.
kind = Compiler_IR_Instruction_Kind::Constant;
223 else_store.
kind = Compiler_IR_Instruction_Kind::Store;
232 function.
blocks.append(entry);
234 function.
blocks.append(then_block);
235 function.
blocks.append(join_block);
236 function.
blocks.append(else_block);
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;
303TEST(CompilerBytecode, LowersLinearFunctionWithConstantsAndLocals)
309 auto source = make_linear_ir();
310 module.functions.append(&source);
313 ASSERT_EQ(bytecode->functions.size(), 1u);
314 const auto & function = *bytecode->functions.access(0);
317 ASSERT_TRUE(report.valid);
318 EXPECT_EQ(function.register_count, 5u);
319 ASSERT_EQ(function.constants.size(), 1u);
320 EXPECT_EQ(function.constants.access(0).kind,
321 Compiler_Bytecode_Constant_Kind::Integer);
322 EXPECT_EQ(function.constants.access(0).integer_value, 1);
324 ASSERT_GE(function.code.size(), 6u);
325 EXPECT_EQ(function.code.access(0).opcode,
326 Compiler_Bytecode_Opcode::Load_Constant);
327 EXPECT_EQ(function.code.access(1).opcode,
328 Compiler_Bytecode_Opcode::Store_Local);
329 EXPECT_EQ(function.code.access(2).opcode,
330 Compiler_Bytecode_Opcode::Load_Local);
331 EXPECT_EQ(function.code.access(4).opcode,
332 Compiler_Bytecode_Opcode::Binary);
333 EXPECT_EQ(function.code.access(5).opcode,
334 Compiler_Bytecode_Opcode::Return);
337TEST(CompilerBytecode, PatchesBranchTargetsToConcreteProgramCounters)
343 auto source = make_branch_ir();
344 module.functions.append(&source);
347 const auto & function = *bytecode->functions.access(0);
350 ASSERT_TRUE(report.valid);
351 ASSERT_EQ(function.
blocks.size(), 5u);
353 const auto & entry_branch = function.code.access(3);
354 EXPECT_EQ(entry_branch.opcode, Compiler_Bytecode_Opcode::Branch);
355 EXPECT_EQ(entry_branch.target_pc, function.
blocks.access(2).begin_pc);
356 EXPECT_EQ(entry_branch.false_target_pc, function.
blocks.access(4).begin_pc);
358 const auto & then_jump = function.code.access(function.
blocks.access(2).end_pc - 1);
359 EXPECT_EQ(then_jump.opcode, Compiler_Bytecode_Opcode::Jump);
360 EXPECT_EQ(then_jump.target_pc, function.
blocks.access(3).begin_pc);
363TEST(CompilerBytecode, LowersTopLevelCallAndGlobalStore)
368 auto module = make_module_with_top_level_call();
372 ASSERT_TRUE(report.valid);
373 ASSERT_EQ(bytecode->functions.size(), 1u);
374 ASSERT_NE(bytecode->top_level,
nullptr);
375 ASSERT_EQ(bytecode->global_slots.size(), 1u);
377 const auto & top = *bytecode->top_level;
378 ASSERT_GE(top.code.size(), 5u);
379 EXPECT_EQ(top.code.access(0).opcode, Compiler_Bytecode_Opcode::Load_Function);
380 EXPECT_EQ(top.code.access(1).opcode, Compiler_Bytecode_Opcode::Load_Constant);
381 EXPECT_EQ(top.code.access(2).opcode, Compiler_Bytecode_Opcode::Call);
382 EXPECT_EQ(top.code.access(3).opcode, Compiler_Bytecode_Opcode::Store_Global);
383 EXPECT_EQ(top.code.access(4).opcode, Compiler_Bytecode_Opcode::Jump);
386 EXPECT_NE(dump.find(
"BytecodeModule"), std::string::npos);
387 EXPECT_NE(dump.find(
"LoadFunction"), std::string::npos);
388 EXPECT_NE(dump.find(
"StoreGlobal"), std::string::npos);
390 delete module.functions.access(0);
391 delete module.top_level;
Reusable bytecode format and lowering from Compiler_IR_Model.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.
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.
void exit(const char *file, int line, const char *format,...)
Print a message and exit the program.
std::string compiler_dump_bytecode_module(const Compiler_Bytecode_Module *module, const Compiler_Type_Context *types=nullptr)
Dumps all lowered bytecode in one module deterministically.
constexpr size_t compiler_ir_invalid_id() noexcept
Returns the sentinel invalid IR id.
Compiler_Bytecode_Validation_Report validate_bytecode_function(const Compiler_Bytecode_Function &function, const Compiler_Bytecode_Module *module=nullptr)
Validates one lowered bytecode function structurally.
Compiler_Bytecode_Validation_Report validate_bytecode_module(const Compiler_Bytecode_Module &module)
Validates all bytecode functions in one module.
double add(double a, double b)
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.