5#include "tpde/ValueAssignment.hpp"
11template <IRAdaptor Adaptor,
typename Derived, CompilerConfig Config>
13 struct AssignmentData {
16 ValLocalIdx local_idx;
17 ValueAssignment *assignment;
19 static_assert(ValRefSpecialStruct<AssignmentData>);
23 Derived::ValRefSpecial s;
29 : state{AssignmentData()}, compiler(compiler) {}
31 ValueRef(
CompilerBase *compiler, ValLocalIdx local_idx) noexcept
32 : state{AssignmentData{
33 .local_idx = local_idx,
34 .assignment = compiler->val_assignment(local_idx),
36 }, compiler(compiler) {
37 assert(!state.a.assignment->pending_free &&
"access of free'd assignment");
41 if (!variable_ref()) {
42 const auto &liveness =
43 compiler->analyzer.liveness_info(state.a.local_idx);
44 assert(liveness.last >= compiler->cur_block_idx &&
45 "ref-counted value used outside of its live range");
46 assert(state.a.assignment->references_left != 0);
47 if (state.a.assignment->references_left == 1 && !liveness.last_full) {
48 assert(liveness.last == compiler->cur_block_idx &&
49 "liveness of non-last-full value must end at last use");
56 }
else if (state.a.assignment->references_left <= 1 &&
57 !state.a.assignment->delay_free) {
64 template <
typename... T>
66 : state{.s =
typename Derived::ValRefSpecial(std::forward<T>(args)...)},
68 assert(state.a.mode >= 4);
73 ValueRef(
const ValueRef &other) =
default;
76 ValueRef(ValueRef &&other) noexcept
77 : state{other.state}, compiler(other.compiler) {
78 other.state.a = AssignmentData{};
81 ~ValueRef() noexcept {
reset(); }
83 ValueRef &operator=(
const ValueRef &) =
delete;
85 ValueRef &operator=(ValueRef &&other)
noexcept {
90 assert(compiler == other.compiler);
91 this->state = other.state;
92 other.state.a.mode = 0;
96 bool has_assignment() const noexcept {
return state.a.mode < 4; }
98 [[nodiscard]] ValueAssignment *assignment() const noexcept {
99 assert(has_assignment());
100 assert(state.a.assignment !=
nullptr);
101 return state.a.assignment;
106 void disown() noexcept {
107 if (has_assignment()) {
114 ValueRef disowned() noexcept {
115 ValueRef res = *
this;
120 ValLocalIdx local_idx() const noexcept {
121 assert(has_assignment());
122 return state.a.local_idx;
125 ValuePartRef part(
unsigned part)
noexcept TPDE_LIFETIMEBOUND {
126 if (has_assignment()) {
128 compiler, local_idx(), state.a.assignment, part, state.a.mode == 2};
131 compiler, compiler->derived()->val_part_ref_special(state.s, part)};
136 ValuePartRef part_unowned(
unsigned part)
noexcept TPDE_LIFETIMEBOUND {
137 if (has_assignment()) {
139 compiler, local_idx(), state.a.assignment, part,
false};
142 compiler, compiler->derived()->val_part_ref_special(state.s, part)};
146 void reset() noexcept;
148 bool variable_ref() const noexcept {
149 assert(has_assignment());
150 return state.a.assignment->variable_ref;
154template <IRAdaptor Adaptor,
typename Derived, CompilerConfig Config>
155void CompilerBase<Adaptor, Derived, Config>::ValueRef::reset() noexcept {
156 if (state.a.mode == 1 || state.a.mode == 2) {
159 assert(!state.a.assignment->pending_free &&
"access of free'd assignment");
161 auto &ref_count = state.a.assignment->references_left;
162 assert(ref_count != 0);
163 if (--ref_count == 0) {
164 compiler->release_assignment(state.a.local_idx, state.a.assignment);
169 state.a.assignment =
nullptr;
170 state.a.local_idx = INVALID_VAL_LOCAL_IDX;
CompilerBase(Adaptor *adaptor)
Initialize a CompilerBase, should be called by the derived classes.