TPDE
Loading...
Searching...
No Matches
Compiler.hpp
1// SPDX-FileCopyrightText: 2025 Contributors to TPDE <https://tpde.org>
2//
3// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4#pragma once
5
6#include "CompilerConfig.hpp"
7#include "base.hpp"
8#include "tpde/Assembler.hpp"
9#include <concepts>
10#include <type_traits>
11
12#ifdef ARG
13 #error ARG is used as a temporary preprocessor macro
14#endif
15
16#define ARG(x) std::declval<x>()
17
18namespace tpde {
19
20class AssignmentPartRef;
21class CCAssigner;
22struct RegBank;
23
24template <bool B>
25concept IsTrue = (B == true);
26
27template <typename T>
28concept ValueParts = requires(T a) {
29 /// Provides the number of parts for a value
30 { a.count() } -> std::convertible_to<u32>;
31
32 /// Provides the size in bytes of a value part (must be a power of two)
33 { a.size_bytes(ARG(u32)) } -> std::convertible_to<u32>;
34
35 /// Provides the bank for a value part
36 { a.reg_bank(ARG(u32)) } -> std::convertible_to<RegBank>;
37};
38
39template <typename T>
40concept ValRefSpecialStruct = requires(T a) {
41 // Clang does not support std::is_corresponding_member.
42 { a.mode } -> std::same_as<uint8_t &>;
43 requires std::is_standard_layout_v<T>;
44 requires offsetof(T, mode) == 0;
45};
46
47template <typename T, typename Config>
48concept Compiler = CompilerConfig<Config> && requires(T a) {
49 // mostly platform things
50 { T::NUM_FIXED_ASSIGNMENTS } -> SameBaseAs<u32[Config::NUM_BANKS]>;
51
52 // (func_idx)
53 { a.start_func(ARG(u32)) };
54
55 { a.gen_func_prolog_and_args(ARG(CCAssigner *)) };
56
57 // This has to call assembler->finish_func
58 // (func_idx)
59 { a.finish_func(ARG(u32)) };
60
61 // (reg_to_spill, frame_off, size)
62 { a.spill_reg(ARG(typename Config::AsmReg), ARG(u32), ARG(u32)) };
63
64 // (dst_reg, frame_off, size)
65 { a.load_from_stack(ARG(typename Config::AsmReg), ARG(u32), ARG(u32)) };
66
67 // (dst_reg, src_reg, size)
68 {
69 a.mov(ARG(typename Config::AsmReg), ARG(typename Config::AsmReg), ARG(u32))
70 };
71
72 // (value); might allocate register
73 {
74 a.gval_expr_as_reg(ARG(typename T::GenericValuePart &))
75 } -> std::same_as<typename Config::AsmReg>;
76
77 {
78 a.select_fixed_assignment_reg(ARG(AssignmentPartRef),
79 ARG(typename T::IRValueRef))
80 } -> std::same_as<typename Config::AsmReg>;
81
82
83 // mostly implementor stuff
84
85 { a.cur_func_may_emit_calls() } -> std::convertible_to<bool>;
86
87 /// Provides the personality function for the current function or
88 /// an invalid SymRef otherwise.
89 { a.cur_personality_func() } -> std::same_as<SymRef>;
90
91 /// Provides a calling convention assigner for the current function.
92 /// Optional, if not implemented, the default C calling convention will be
93 /// used.
94 { a.cur_cc_assigner() } -> std::convertible_to<CCAssigner *>;
95
96 {
97 a.try_force_fixed_assignment(ARG(typename T::IRValueRef))
98 } -> std::convertible_to<bool>;
99
100 { a.val_parts(ARG(typename T::IRValueRef)) } -> ValueParts;
101
102 /// A compiler can provide a data structure that is a non-assignment ValueRef.
103 /// This struct is used in a union, so it must be a standard-layout struct and
104 /// have "uint8_t mode;" as first member, which must be >= 4.
105 requires ValRefSpecialStruct<typename T::ValRefSpecial>;
106
107 /// Provides the implementation to return special ValueRefs, e.g. for
108 /// constants or globals.
109 {
110 a.val_ref_special(ARG(typename T::IRValueRef))
111 } -> std::same_as<std::optional<typename T::ValRefSpecial>>;
112
113 /// Provides the implementation to construct a ValuePartRef from a
114 /// ValRefSpecial.
115 {
116 a.val_part_ref_special(ARG(typename T::ValRefSpecial &), ARG(u32))
117 } -> std::same_as<typename T::ValuePart>;
118
119 /// The compiler numbers functions and this gives the derived implementation
120 /// a chance to save that mapping
121 ///
122 /// (func_ref, idx)
123 { a.define_func_idx(ARG(typename T::IRFuncRef), ARG(u32)) };
124
125 /// When the default variable reference handling is disabled, the
126 /// implementation has to provide a few helpers to tell us what to do
127 requires IsTrue<Config::DEFAULT_VAR_REF_HANDLING> || requires {
128 /// Called when starting to compile a func.
129 /// This may initialize variable reference assignments
130 /// which should be live at the beginning of the function
131 { a.setup_var_ref_assignments() };
132
133 /// Load the address of a variable reference referred to
134 /// by the AssignmentPartRef into a register
135 // (dst_reg, ap_ref)
136 {
137 a.load_address_of_var_reference(ARG(typename Config::AsmReg),
138 ARG(AssignmentPartRef))
139 };
140 };
141
142 {
143 a.compile_inst(ARG(typename T::IRInstRef), ARG(typename T::InstRange))
144 } -> std::convertible_to<bool>;
145};
146
147} // namespace tpde