Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
compiler_builder_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
38# include <Compiler_HIR_Builder.H>
39# include <Compiler_IR_Builder.H>
40
41using namespace Aleph;
42
43int main()
44{
46
47 Compiler_HIR_Context hir_ctx(1 << 15);
48 Compiler_HIR_Builder hir(hir_ctx);
49 auto * hir_module = hir.make_module();
50 auto * hir_body = hir.make_block();
51 auto * hir_function = hir.make_function(
52 "add",
54 {types.integer_type(), types.integer_type()}, types.integer_type()));
55 hir.add_parameter(*hir_function, "x", types.integer_type());
56 hir.add_parameter(*hir_function, "y", types.integer_type());
58 hir.append_function(*hir_module, hir_function);
60 *hir_body,
62 Compiler_Operator_Kind::Plus,
63 hir.make_variable("y", types.integer_type()),
64 types.integer_type())));
65
66 auto * call = hir.make_call(
67 hir.make_variable("add",
69 {types.integer_type(), types.integer_type()},
70 types.integer_type())),
71 types.integer_type());
72 hir.append_argument(*call, hir.make_integer_constant(40, types.integer_type()));
73 hir.append_argument(*call, hir.make_integer_constant(2, types.integer_type()));
75 *hir_module, hir.make_let_stmt("value", types.integer_type(), {}, call));
76
77 Compiler_IR_Context ir_ctx(1 << 15);
78 Compiler_IR_Builder ir(ir_ctx);
79 auto * ir_module = ir.make_module();
80 const auto global_value = ir.add_global_slot(*ir_module, "value", types.integer_type());
81 auto * ir_function = ir.make_function(
82 "add",
84 {types.integer_type(), types.integer_type()}, types.integer_type()));
85 const auto ir_function_id = ir.append_function(*ir_module, ir_function);
86 const auto x
88 Compiler_IR_Slot_Kind::Parameter,
89 "x",
90 types.integer_type());
91 const auto y
93 Compiler_IR_Slot_Kind::Parameter,
94 "y",
95 types.integer_type());
96 const auto entry = ir.create_block(*ir_function, "entry");
97 const auto exit = ir.create_block(*ir_function, "exit");
98 ir.set_entry_block(*ir_function, entry);
100 const auto vx = ir.emit_load_local(*ir_function, entry, x, types.integer_type());
101 const auto vy = ir.emit_load_local(*ir_function, entry, y, types.integer_type());
102 const auto sum
104 entry,
105 Compiler_Operator_Kind::Plus,
106 vx,
107 vy,
108 types.integer_type());
109 ir.set_return(*ir_function, entry, sum);
111
112 auto * top = ir.make_function("<top-level>");
113 ir.set_top_level(*ir_module, top);
114 const auto top_entry = ir.create_block(*top, "entry");
115 const auto top_exit = ir.create_block(*top, "exit");
116 ir.set_entry_block(*top, top_entry);
117 ir.set_exit_block(*top, top_exit);
118 const auto add_ref = ir.emit_function_ref(
119 *top,
120 top_entry,
122 types.make_function_type(
123 {types.integer_type(), types.integer_type()}, types.integer_type()));
125 arguments.append(ir.emit_constant(*top, top_entry, types.integer_type(), "40"));
126 arguments.append(ir.emit_constant(*top, top_entry, types.integer_type(), "2"));
127 const auto result
128 = ir.emit_call(*top, top_entry, add_ref, arguments, types.integer_type());
129 ir.emit_store_global(*top, top_entry, global_value, result, types.integer_type());
130 ir.set_jump(*top, top_entry, top_exit);
131 ir.set_exit(*top, top_exit);
132
133 std::cout << compiler_dump_hir_module(hir_module, types);
134 std::cout << '\n';
135 std::cout << compiler_dump_ir_module(ir_module, &types);
136 return 0;
137}
Reusable builder helpers for constructing HIR directly.
Reusable builder helpers for constructing explicit IR directly.
Small reusable helper that allocates and connects HIR nodes.
static void append_top_level_statement(Compiler_HIR_Module &module, Compiler_HIR_Stmt *stmt)
Appends one top-level statement to module and updates the span.
Compiler_HIR_Let_Stmt * make_let_stmt(std::string name, const Compiler_Type_Id type_id, const Source_Span &name_span={}, Compiler_HIR_Expr *initializer=nullptr, const Source_Span &span={}) const
Allocates one lexical binding statement.
static void set_body(Compiler_HIR_Function &function, Compiler_HIR_Block_Stmt *body) noexcept
Attaches body to function and refreshes the aggregate span.
Compiler_HIR_Variable_Expr * make_variable(std::string name, const Compiler_Type_Id type_id=0, const Source_Span &span={}) const
Allocates one variable-reference expression.
Compiler_HIR_Call_Expr * make_call(Compiler_HIR_Expr *callee, const Compiler_Type_Id type_id=0, const Source_Span &lparen_span={}, const Source_Span &rparen_span={}) const
Allocates one call expression without arguments.
static void append_statement(Compiler_HIR_Block_Stmt &block, Compiler_HIR_Stmt *stmt)
Appends one nested statement to block and updates the span.
static Compiler_HIR_Param & add_parameter(Compiler_HIR_Function &function, std::string name, const Compiler_Type_Id type_id, const Source_Span &span={})
Adds one parameter metadata entry to function.
Compiler_HIR_Block_Stmt * make_block(const Source_Span &lbrace={}, const Source_Span &rbrace={}) const
Allocates one structured block statement.
static void append_function(Compiler_HIR_Module &module, Compiler_HIR_Function *function)
Appends one function to module and updates the module span.
Compiler_HIR_Binary_Expr * make_binary(Compiler_HIR_Expr *lhs, const Compiler_Operator_Kind op, Compiler_HIR_Expr *rhs, const Compiler_Type_Id type_id=0, const Source_Span &operator_span={}) const
Allocates one binary expression.
Compiler_HIR_Function * make_function(std::string name, const Compiler_Type_Id type_id=0, const Source_Span &name_span={}, Compiler_HIR_Block_Stmt *body=nullptr, const Source_Span &span={}) const
Allocates one function node without inserting it into any module.
Compiler_HIR_Module * make_module(const Source_Span &span={}) const
Allocates one empty module.
Compiler_HIR_Return_Stmt * make_return_stmt(Compiler_HIR_Expr *value=nullptr, const Source_Span &span={}) const
Allocates one return statement.
Compiler_HIR_Constant_Expr * make_integer_constant(const long long value, const Compiler_Type_Id type_id, const Source_Span &span={}) const
Allocates one integer constant expression.
static void append_argument(Compiler_HIR_Call_Expr &call, Compiler_HIR_Expr *argument)
Appends one argument to call and updates the aggregate span.
Arena-backed ownership context for HIR nodes.
Small reusable helper that allocates and connects IR nodes.
static void emit_store_global(Compiler_IR_Function &function, const Compiler_IR_Block_Id block_id, const Compiler_IR_Global_Slot_Id slot_id, const Compiler_IR_Value_Id value_id, const Compiler_Type_Id type_id, const Source_Span &span={})
Emits one store into a global slot.
static Compiler_IR_Value_Id emit_constant(Compiler_IR_Function &function, const Compiler_IR_Block_Id block_id, const Compiler_Type_Id type_id, std::string text, const Source_Span &span={}, const bool bool_value=false)
Emits one constant instruction.
static Compiler_IR_Function_Id append_function(Compiler_IR_Module &module, Compiler_IR_Function *function)
Appends one non-top-level function to module and assigns its id.
static Compiler_IR_Local_Slot_Id add_local_slot(Compiler_IR_Function &function, const Compiler_IR_Slot_Kind kind, std::string name, const Compiler_Type_Id type_id, const Source_Span &span={})
Appends one local or parameter slot to function.
static void set_exit_block(Compiler_IR_Function &function, const Compiler_IR_Block_Id block_id)
Marks one block as the canonical exit block.
static Compiler_IR_Block_Id create_block(Compiler_IR_Function &function, std::string label)
Creates one empty block and returns its id.
static Compiler_IR_Value_Id emit_load_local(Compiler_IR_Function &function, const Compiler_IR_Block_Id block_id, const Compiler_IR_Local_Slot_Id slot_id, const Compiler_Type_Id type_id, const Source_Span &span={})
Emits one load from a local or parameter slot.
static void set_entry_block(Compiler_IR_Function &function, const Compiler_IR_Block_Id block_id)
Marks one block as the canonical entry block.
Compiler_IR_Module * make_module() const
Allocates one empty IR module.
static Compiler_IR_Global_Slot_Id add_global_slot(Compiler_IR_Module &module, std::string name, const Compiler_Type_Id type_id, const Source_Span &span={})
Appends one global slot to module.
static void set_exit(Compiler_IR_Function &function, const Compiler_IR_Block_Id block_id, const Source_Span &span={})
Sets one block terminator to Exit.
static Compiler_IR_Value_Id emit_call(Compiler_IR_Function &function, const Compiler_IR_Block_Id block_id, const Compiler_IR_Value_Id callee, const DynArray< Compiler_IR_Value_Id > &arguments, const Compiler_Type_Id type_id, const Source_Span &span={})
Emits one call instruction.
static void set_return(Compiler_IR_Function &function, const Compiler_IR_Block_Id from, const Compiler_IR_Value_Id return_value, const Source_Span &span={})
Sets one return terminator and connects it to the exit block.
static void set_jump(Compiler_IR_Function &function, const Compiler_IR_Block_Id from, const Compiler_IR_Block_Id to, const Source_Span &span={})
Sets one jump terminator and predecessor/successor edge.
Compiler_IR_Function * make_function(std::string name, const Compiler_Type_Id type_id=0, const Source_Span &span={}, const Compiler_HIR_Function *source_function=nullptr) const
Allocates one function without inserting it into a module.
static Compiler_IR_Value_Id emit_binary(Compiler_IR_Function &function, const Compiler_IR_Block_Id block_id, const Compiler_Operator_Kind op, const Compiler_IR_Value_Id lhs, const Compiler_IR_Value_Id rhs, const Compiler_Type_Id type_id, const Source_Span &span={})
Emits one binary instruction.
static Compiler_IR_Value_Id emit_function_ref(Compiler_IR_Function &function, const Compiler_IR_Block_Id block_id, const Compiler_IR_Function_Id function_id, const Compiler_Type_Id type_id, const Source_Span &span={})
Emits one function reference instruction.
static Compiler_IR_Function * set_top_level(Compiler_IR_Module &module, Compiler_IR_Function *function)
Installs one top-level function body in module.
Arena-backed ownership context for IR nodes.
Context owning all compiler type nodes.
Compiler_Type_Id make_function_type(const DynArray< Compiler_Type_Id > &parameters, const Compiler_Type_Id result)
Creates a function type.
Compiler_Type_Id integer_type() const noexcept
Returns the preloaded Int type id.
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
static mpfr_t y
Definition mpfr_mul_d.c:3
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
std::string compiler_dump_ir_module(const Compiler_IR_Module *module, const Compiler_Type_Context *types=nullptr)
Dumps all lowered IR in one module deterministically.
void exit(const char *file, int line, const char *format,...)
Print a message and exit the program.
Definition ahDefs.C:125
std::string compiler_dump_hir_module(const Compiler_HIR_Module *module, const Compiler_Type_Context &types)
Dumps one HIR module in a deterministic text format.
T sum(const Container &container, const T &init=T{})
Compute sum of all elements.