48 std::unique_ptr<Compiler_IR_Function> function;
49 std::unique_ptr<Compiler_IR_Function> top_level;
66 auto &
module = demo.module;
70 global.
kind = Compiler_IR_Slot_Kind::Global;
71 global.
name =
"answer";
72 module.global_slots.append(global);
74 demo.function = std::make_unique<Compiler_IR_Function>();
75 auto * function = demo.function.get();
77 function->name =
"add_one";
78 function->entry_block = 0;
79 function->exit_block = 1;
80 function->next_value_id = 4;
84 param.
kind = Compiler_IR_Slot_Kind::Parameter;
86 function->local_slots.append(param);
90 local.
kind = Compiler_IR_Slot_Kind::Local;
92 function->local_slots.append(local);
96 entry.
label =
"entry";
99 one.
kind = Compiler_IR_Instruction_Kind::Constant;
105 store_tmp.
kind = Compiler_IR_Instruction_Kind::Store;
111 load_x.
kind = Compiler_IR_Instruction_Kind::Load;
117 load_tmp.
kind = Compiler_IR_Instruction_Kind::Load;
123 add.kind = Compiler_IR_Instruction_Kind::Binary;
125 add.op = Compiler_Operator_Kind::Plus;
126 add.operands.append(2);
127 add.operands.append(3);
134 auto exit = make_exit_block();
135 exit.predecessors.append(0);
137 function->blocks.append(entry);
138 function->blocks.append(
exit);
139 module.functions.append(function);
141 demo.top_level = std::make_unique<Compiler_IR_Function>();
142 auto * top = demo.top_level.get();
144 top->name =
"<top-level>";
145 top->entry_block = 0;
147 top->next_value_id = 3;
151 top_entry.
label =
"entry";
154 function_ref.
kind = Compiler_IR_Instruction_Kind::Function_Ref;
160 constant.
kind = Compiler_IR_Instruction_Kind::Constant;
162 constant.
text =
"41";
166 call.kind = Compiler_IR_Instruction_Kind::Call;
168 call.operands.append(1);
169 call.operands.append(2);
173 store_answer.
kind = Compiler_IR_Instruction_Kind::Store;
181 auto top_exit = make_exit_block();
182 top_exit.predecessors.append(0);
184 top->blocks.append(top_entry);
185 top->blocks.append(top_exit);
186 module.top_level = top;
193 auto module = make_demo_module();
200 if (not emitted.valid)
202 std::cerr <<
"Emission errors:\n";
203 for (
size_t i = 0; i < emitted.errors.size(); ++i)
204 std::cerr <<
"- " << emitted.errors.access(i) <<
'\n';
208 std::cout << emitted.source;
209 if (not emitted.warnings.is_empty())
211 std::cout <<
"\n/* Warnings:\n";
212 for (
size_t i = 0; i < emitted.warnings.size(); ++i)
213 std::cout <<
" - " << emitted.warnings.access(i) <<
'\n';
Portable C backend that emits standalone C from Compiler_IR_Model.H.
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.
constexpr size_t compiler_ir_invalid_id() noexcept
Returns the sentinel invalid IR id.
Compiler_C_Backend_Emission compiler_emit_c_module(const Compiler_IR_Module *module, const Compiler_C_Backend_Options &options={}, const Compiler_Type_Context *types=nullptr)
Convenience wrapper that emits one IR module to portable C.
double add(double a, double b)
static struct argp_option options[]
Options that control one C backend emission request.
std::string module_name
Prefix used to derive stable C identifiers.
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.
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 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.