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) {}
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");
40 if constexpr (WithAsserts) {
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) : state{other.state}, compiler(other.compiler) {
77 other.state.a = AssignmentData{};
80 ~ValueRef() {
reset(); }
82 ValueRef &operator=(
const ValueRef &) =
delete;
84 ValueRef &operator=(ValueRef &&other) {
89 assert(compiler == other.compiler);
90 this->state = other.state;
91 other.state.a.mode = 0;
95 bool has_assignment()
const {
return state.a.mode < 4; }
97 [[nodiscard]] ValueAssignment *assignment()
const {
98 assert(has_assignment());
99 assert(state.a.assignment !=
nullptr);
100 return state.a.assignment;
104 bool is_owned() {
return state.a.mode == 2; }
109 if (has_assignment()) {
116 ValueRef disowned() {
117 ValueRef res = *
this;
122 ValLocalIdx local_idx()
const {
123 assert(has_assignment());
124 return state.a.local_idx;
127 ValuePartRef part(
unsigned part) TPDE_LIFETIMEBOUND {
128 if (has_assignment()) {
130 compiler, local_idx(), state.a.assignment, part, is_owned()};
133 compiler, compiler->derived()->val_part_ref_special(state.s, part)};
138 ValuePartRef part_unowned(
unsigned part) TPDE_LIFETIMEBOUND {
139 if (has_assignment()) {
141 compiler, local_idx(), state.a.assignment, part,
false};
144 compiler, compiler->derived()->val_part_ref_special(state.s, part)};
150 bool variable_ref()
const {
151 assert(has_assignment());
152 return state.a.assignment->variable_ref;
156template <IRAdaptor Adaptor,
typename Derived, CompilerConfig Config>
157void CompilerBase<Adaptor, Derived, Config>::ValueRef::reset() {
158 if (state.a.mode == 1 || state.a.mode == 2) {
161 assert(!state.a.assignment->pending_free &&
"access of free'd assignment");
163 auto &ref_count = state.a.assignment->references_left;
164 assert(ref_count != 0);
165 if (--ref_count == 0) {
166 compiler->release_assignment(state.a.local_idx, state.a.assignment);
170 if constexpr (WithAsserts) {
171 state.a.assignment =
nullptr;
172 state.a.local_idx = INVALID_VAL_LOCAL_IDX;
A default implementation for ValRefSpecial.
The base class for the compiler.
void reset()
Reset any leftover data from the previous compilation such that it will not affect the next compilati...
CompilerBase(Adaptor *adaptor)
Initialize a CompilerBase, should be called by the derived classes.