61 global.kind = Compiler_IR_Slot_Kind::Global;
62 global.name =
"answer";
63 module.global_slots.append(global);
67 function->name =
"choose";
68 function->entry_block = 0;
69 function->exit_block = 1;
70 function->next_value_id = 5;
74 flag.
kind = Compiler_IR_Slot_Kind::Parameter;
76 function->local_slots.append(flag);
80 value.kind = Compiler_IR_Slot_Kind::Local;
82 function->local_slots.append(
value);
86 entry.
label =
"entry";
89 zero.
kind = Compiler_IR_Instruction_Kind::Constant;
95 init_store.
kind = Compiler_IR_Instruction_Kind::Store;
101 load_flag.
kind = Compiler_IR_Instruction_Kind::Load;
114 exit.terminator.kind = Compiler_IR_Terminator_Kind::Exit;
115 exit.predecessors.append(3);
119 then_block.
label =
"if.then.0";
122 one.
kind = Compiler_IR_Instruction_Kind::Constant;
128 then_store.
kind = Compiler_IR_Instruction_Kind::Store;
139 join_block.
label =
"if.end.0";
142 load_value.
kind = Compiler_IR_Instruction_Kind::Load;
155 else_block.
label =
"if.else.0";
158 two.
kind = Compiler_IR_Instruction_Kind::Constant;
164 else_store.
kind = Compiler_IR_Instruction_Kind::Store;
173 function->blocks.append(entry);
174 function->blocks.append(
exit);
175 function->blocks.append(then_block);
176 function->blocks.append(join_block);
177 function->blocks.append(else_block);
178 module.functions.append(function);
182 top->name =
"<top-level>";
183 top->entry_block = 0;
185 top->next_value_id = 3;
189 top_entry.
label =
"entry";
192 function_ref.
kind = Compiler_IR_Instruction_Kind::Function_Ref;
198 constant.
kind = Compiler_IR_Instruction_Kind::Constant;
200 constant.
text =
"true";
205 call.kind = Compiler_IR_Instruction_Kind::Call;
207 call.operands.append(1);
208 call.operands.append(2);
212 store_global.
kind = Compiler_IR_Instruction_Kind::Store;
220 auto top_exit = make_exit_block();
221 top_exit.predecessors.append(0);
223 top->blocks.append(top_entry);
224 top->blocks.append(top_exit);
225 module.top_level = top;
248 auto module = make_demo_module();
253 if (not report.valid)
255 std::cout <<
"Validation errors:\n";
256 for (
size_t i = 0; i < report.errors.size(); ++i)
257 std::cout <<
"- " << report.errors.access(i) <<
'\n';
258 delete module.functions.access(0);
259 delete module.top_level;
271 print_result(
"Call choose(false)", direct_call, bytecode);
274 delete module.functions.access(0);
275 delete module.top_level;
276 return top_level.ok()
and direct_call.ok() ? 0 : 1;
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.
T & append()
Allocate a new entry to the end of array.
void print_result(const string &label, const Multipoly &poly)
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.
and
Check uniqueness with explicit hash + equality functors.
Compiler_Bytecode_Validation_Report validate_bytecode_module(const Compiler_Bytecode_Module &module)
Validates all bytecode functions in one module.
std::string compiler_bytecode_value_to_string(const Compiler_Bytecode_Value &value, const Compiler_Bytecode_Module *module=nullptr)
Renders one bytecode value deterministically.
const char * compiler_bytecode_completion_kind_name(const Compiler_Bytecode_Completion_Kind kind) noexcept
Stable debug name for one execution completion kind.
Result of one bytecode execution request.
Compiler_Bytecode_Runtime_Error error
Structured failure when kind == Runtime_Error.
size_t executed_instructions
Number of bytecode instructions observed by the VM.
bool ok() const noexcept
Returns whether execution completed without runtime errors.
Compiler_Bytecode_Completion_Kind kind
Final completion category.
Compiler_Bytecode_Value value
Produced return value or final Unit.
Bytecode module sharing global slot layout with the source IR module.
std::string message
Human-readable runtime message.
std::string code
Stable runtime code such as BCV013.
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.
One instruction producing an optional explicit result value.
std::string text
Constant spelling or debug payload.
bool bool_value
Decoded boolean payload for bool constants.
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.