Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
compiler_backend_c_example.cc
Go to the documentation of this file.
1/*
2 Aleph_w
3
4 Data structures & Algorithms
5 version 2.0.0b
6 https://github.com/lrleon/Aleph-w
7
8 This file is part of Aleph-w library
9
10 Copyright (c) 2002-2026 Leandro Rabindranath Leon
11
12 Permission is hereby granted, free of charge, to any person obtaining a copy
13 of this software and associated documentation files (the "Software"), to deal
14 in the Software without restriction, including without limitation the rights
15 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16 copies of the Software, and to permit persons to whom the Software is
17 furnished to do so, subject to the following conditions:
18
19 The above copyright notice and this permission notice shall be included in all
20 copies or substantial portions of the Software.
21
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 SOFTWARE.
29*/
30
36#include <iostream>
37#include <memory>
38
39#include <Compiler_Backend_C.H>
40
41using namespace Aleph;
42
43namespace
44{
45 struct Demo_IR_Module
46 {
47 Compiler_IR_Module module;
48 std::unique_ptr<Compiler_IR_Function> function;
49 std::unique_ptr<Compiler_IR_Function> top_level;
50 };
51
54 {
56 block.id = 1;
57 block.label = "exit";
58 block.terminator.kind = Compiler_IR_Terminator_Kind::Exit;
59 return block;
60 }
61
62 Demo_IR_Module
64 {
65 Demo_IR_Module demo;
66 auto &module = demo.module;
67
69 global.id = 0;
70 global.kind = Compiler_IR_Slot_Kind::Global;
71 global.name = "answer";
72 module.global_slots.append(global);
73
74 demo.function = std::make_unique<Compiler_IR_Function>();
75 auto * function = demo.function.get();
76 function->id = 0;
77 function->name = "add_one";
78 function->entry_block = 0;
79 function->exit_block = 1;
80 function->next_value_id = 4;
81
83 param.id = 0;
84 param.kind = Compiler_IR_Slot_Kind::Parameter;
85 param.name = "x";
86 function->local_slots.append(param);
87
88 Compiler_IR_Slot local;
89 local.id = 1;
90 local.kind = Compiler_IR_Slot_Kind::Local;
91 local.name = "tmp";
92 function->local_slots.append(local);
93
95 entry.id = 0;
96 entry.label = "entry";
97
99 one.kind = Compiler_IR_Instruction_Kind::Constant;
100 one.result_id = 1;
101 one.text = "1";
102 entry.instructions.append(one);
103
105 store_tmp.kind = Compiler_IR_Instruction_Kind::Store;
106 store_tmp.local_slot_id = 1;
107 store_tmp.operands.append(1);
108 entry.instructions.append(store_tmp);
109
111 load_x.kind = Compiler_IR_Instruction_Kind::Load;
112 load_x.result_id = 2;
113 load_x.local_slot_id = 0;
114 entry.instructions.append(load_x);
115
117 load_tmp.kind = Compiler_IR_Instruction_Kind::Load;
118 load_tmp.result_id = 3;
119 load_tmp.local_slot_id = 1;
120 entry.instructions.append(load_tmp);
121
123 add.kind = Compiler_IR_Instruction_Kind::Binary;
124 add.result_id = 4;
125 add.op = Compiler_Operator_Kind::Plus;
126 add.operands.append(2);
127 add.operands.append(3);
128 entry.instructions.append(add);
129
130 entry.terminator.kind = Compiler_IR_Terminator_Kind::Return;
131 entry.terminator.return_value = 4;
133
134 auto exit = make_exit_block();
135 exit.predecessors.append(0);
136
137 function->blocks.append(entry);
138 function->blocks.append(exit);
139 module.functions.append(function);
140
141 demo.top_level = std::make_unique<Compiler_IR_Function>();
142 auto * top = demo.top_level.get();
143 top->id = compiler_ir_invalid_id();
144 top->name = "<top-level>";
145 top->entry_block = 0;
146 top->exit_block = 1;
147 top->next_value_id = 3;
148
150 top_entry.id = 0;
151 top_entry.label = "entry";
152
154 function_ref.kind = Compiler_IR_Instruction_Kind::Function_Ref;
155 function_ref.result_id = 1;
156 function_ref.function_id = 0;
157 top_entry.instructions.append(function_ref);
158
160 constant.kind = Compiler_IR_Instruction_Kind::Constant;
161 constant.result_id = 2;
162 constant.text = "41";
163 top_entry.instructions.append(constant);
164
166 call.kind = Compiler_IR_Instruction_Kind::Call;
167 call.result_id = 3;
168 call.operands.append(1);
169 call.operands.append(2);
170 top_entry.instructions.append(call);
171
173 store_answer.kind = Compiler_IR_Instruction_Kind::Store;
174 store_answer.global_slot_id = 0;
175 store_answer.operands.append(3);
176 top_entry.instructions.append(store_answer);
177
178 top_entry.terminator.kind = Compiler_IR_Terminator_Kind::Jump;
179 top_entry.terminator.successors.append(1);
180
181 auto top_exit = make_exit_block();
182 top_exit.predecessors.append(0);
183
184 top->blocks.append(top_entry);
185 top->blocks.append(top_exit);
186 module.top_level = top;
187 return demo;
188 }
189}
190
191int main()
192{
193 auto module = make_demo_module();
194
196 options.module_name = "example_demo";
197 options.emit_main = true;
198
199 const auto emitted = compiler_emit_c_module(&module.module, options);
200 if (not emitted.valid)
201 {
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';
205 return 1;
206 }
207
208 std::cout << emitted.source;
209 if (not emitted.warnings.is_empty())
210 {
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';
214 std::cout << "*/\n";
215 }
216
217 return 0;
218}
Portable C backend that emits standalone C from Compiler_IR_Model.H.
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().
Definition Blossom.H:466
Compiler_SSA_Block & block(Compiler_SSA_Function &function, const Compiler_SSA_Block_Id id)
Definition SSA.H:636
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
void exit(const char *file, int line, const char *format,...)
Print a message and exit the program.
Definition ahDefs.C:125
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[]
Definition ntreepic.C:1886
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.
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.
Definition SSA.H:163
std::string label
Deterministic debug label.
Definition SSA.H:165
Compiler_SSA_Terminator terminator
Explicit block terminator.
Definition SSA.H:169
Compiler_IR_Terminator_Kind kind
Terminator category.
Definition SSA.H:153