TPDE
Loading...
Searching...
No Matches
ValuePartRef.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 "tpde/ValueAssignment.hpp"
7
8#include <cstring>
9#include <span>
10
11namespace tpde {
12
13template <IRAdaptor Adaptor, typename Derived, CompilerConfig Config>
14class CompilerBase<Adaptor, Derived, Config>::ValuePart {
15private:
16 struct ConstantData {
17 AsmReg reg = AsmReg::make_invalid();
18 bool has_assignment = false;
19 bool owned;
20 bool is_const : 1;
21 bool const_inline : 1;
22 union {
23 const u64 *data;
24 u64 inline_data;
25 };
26 RegBank bank;
27 u32 size;
28 };
29
30 struct ValueData {
31 AsmReg reg = AsmReg::make_invalid(); // only valid if fixed/locked
32 bool has_assignment = true;
33 bool owned;
34 ValLocalIdx local_idx;
35 u32 part;
36 ValueAssignment *assignment;
37 };
38
39 union {
40 ConstantData c;
41 ValueData v;
42 } state;
43
44public:
45 ValuePart() noexcept : state{ConstantData{.is_const = false}} {}
46
47 ValuePart(RegBank bank) noexcept
48 : state{
49 ConstantData{.is_const = false, .bank = bank}
50 } {
51 assert(bank.id() < Config::NUM_BANKS);
52 }
53
54 ValuePart(ValLocalIdx local_idx,
55 ValueAssignment *assignment,
56 u32 part,
57 bool owned) noexcept
58 : state{
59 .v = ValueData{
60 .owned = owned,
61 .local_idx = local_idx,
62 .part = part,
63 .assignment = assignment,
64 }
65 } {
66 assert(this->assignment().variable_ref() ||
67 state.v.assignment->references_left);
68 assert(!owned || state.v.assignment->references_left == 1);
69 }
70
71 ValuePart(const u64 *data, u32 size, RegBank bank) noexcept
72 : state{
73 .c = ConstantData{.is_const = true,
74 .const_inline = false,
75 .data = data,
76 .bank = bank,
77 .size = size}
78 } {
79 assert(data && "constant data must not be null");
80 assert(bank.id() < Config::NUM_BANKS);
81 }
82
83 ValuePart(const u64 val, u32 size, RegBank bank) noexcept
84 : state{
85 .c = ConstantData{.is_const = true,
86 .const_inline = true,
87 .inline_data = val,
88 .bank = bank,
89 .size = size}
90 } {
91 assert(size <= sizeof(val));
92 assert(bank.id() < Config::NUM_BANKS);
93 }
94
95 explicit ValuePart(const ValuePart &) = delete;
96
97 ValuePart(ValuePart &&other) noexcept : state{other.state} {
98 other.state.c = ConstantData{};
99 }
100
101 ~ValuePart() noexcept {
102 assert(!state.c.reg.valid() && "must call reset() on ValuePart explicitly");
103 }
104
105 ValuePart &operator=(const ValuePart &) = delete;
106
107 ValuePart &operator=(ValuePart &&other) noexcept {
108 if (this == &other) {
109 return *this;
110 }
111 assert(!state.c.reg.valid() && "must call reset() on ValuePart explicitly");
112 this->state = other.state;
113 other.state.c = ConstantData{};
114 return *this;
115 }
116
117 bool has_assignment() const noexcept { return state.v.has_assignment; }
118
119 bool is_const() const noexcept {
120 return !state.c.has_assignment && state.c.is_const;
121 }
122
123 bool is_owned() const noexcept {
124 assert(has_assignment());
125 return state.c.owned;
126 }
127
128 [[nodiscard]] AssignmentPartRef assignment() const noexcept {
129 assert(has_assignment());
130 return AssignmentPartRef{state.v.assignment, state.v.part};
131 }
132
133 /// If it is known that the value part has a register, this function can be
134 /// used to quickly access it
135 AsmReg cur_reg() const noexcept {
136 assert(state.v.reg.valid());
137 return state.v.reg;
138 }
139
140 /// Current register or none, even if the value is unlocked and could be
141 /// evicted by any other operation.
142 AsmReg cur_reg_unlocked() const noexcept {
143 if (state.v.reg.valid()) {
144 return state.v.reg;
145 }
146 if (has_assignment()) {
147 if (auto ap = assignment(); ap.register_valid()) {
148 return ap.get_reg();
149 }
150 }
151 return AsmReg::make_invalid();
152 }
153
154 /// Is the value part currently in the specified register?
155 bool is_in_reg(AsmReg reg) const noexcept {
156 if (has_reg()) {
157 return cur_reg() == reg;
158 }
159 if (has_assignment()) {
160 auto ap = assignment();
161 return ap.register_valid() && ap.get_reg() == reg;
162 }
163 return false;
164 }
165
166 bool has_reg() const noexcept { return state.v.reg.valid(); }
167
168private:
169 AsmReg alloc_reg_impl(CompilerBase *compiler,
170 u64 exclusion_mask,
171 bool reload) noexcept;
172 AsmReg alloc_specific_impl(CompilerBase *compiler,
173 AsmReg reg,
174 bool reload) noexcept;
175
176public:
177 /// Allocate and lock a register for the value part, *without* reloading the
178 /// value. Does nothing if a register is already allocated.
179 AsmReg alloc_reg(CompilerBase *compiler, u64 exclusion_mask = 0) noexcept {
180 return alloc_reg_impl(compiler, exclusion_mask, /*reload=*/false);
181 }
182
183 /// Allocate register, but try to reuse the register from ref first. This
184 /// method is complicated and must be used carefully. If ref is locked in a
185 /// register and owns the register (can_salvage()), the ownership of the
186 /// register is transferred to this ValuePart without modifying the value.
187 /// Otherwise, a new register is allocated.
188 ///
189 /// Usage example:
190 /// AsmReg operand_reg = operand_ref.load_to_reg();
191 /// AsmReg result_reg = result_ref.alloc_try_reuse(operand_ref);
192 /// if (operand_reg == result_reg) {
193 /// // reuse successful
194 /// ASM(ADD64ri, result_reg, 1);
195 /// } else {
196 /// ASM(LEA64rm, result_reg, FE_MEM(FE_NOREG, 1, operand_reg, 1));
197 /// }
198 AsmReg alloc_try_reuse(CompilerBase *compiler, ValuePart &ref) noexcept {
199 assert(ref.has_reg());
200 if (!has_assignment() || !assignment().register_valid()) {
201 assert(!has_assignment() || !assignment().fixed_assignment());
202 if (ref.can_salvage()) {
203 set_value(compiler, std::move(ref));
204 if (has_assignment()) {
205 lock(compiler);
206 }
207 return cur_reg();
208 }
209 }
210 return alloc_reg(compiler);
211 }
212
213 /// Allocate and lock a specific register for the value part, spilling the
214 /// register if it is currently used (must not be fixed), *without* reloading
215 /// or copying the value into the new register. The value must not be locked.
216 /// An existing assignment register is discarded. Value part must not be a
217 /// fixed assignment.
218 void alloc_specific(CompilerBase *compiler, AsmReg reg) noexcept {
219 alloc_specific_impl(compiler, reg, false);
220 }
221
222 /// Allocate, fill, and lock a register for the value part, reloading from
223 /// the stack or materializing the constant if necessary. Requires that the
224 /// value is currently unlocked (i.e., has_reg() is false).
225 AsmReg load_to_reg(CompilerBase *compiler) noexcept {
226 return alloc_reg_impl(compiler, 0, /*reload=*/true);
227 }
228
229 /// Allocate, fill, and lock a specific register for the value part, spilling
230 /// the register if it is currently used (must not be fixed). The value is
231 /// moved (assignment updated) or reloaded to this register. Value part must
232 /// not be a fixed assignment.
233 ///
234 /// \warning Do not overwrite the register content as it is not saved
235 /// \note The target register or the current value part may not be fixed
236 void load_to_specific(CompilerBase *compiler, AsmReg reg) noexcept {
237 alloc_specific_impl(compiler, reg, true);
238 }
239
240 /// Copy value into a different register.
241 AsmReg reload_into_specific_fixed(CompilerBase *compiler,
242 AsmReg reg,
243 unsigned size = 0) noexcept;
244
245 /// For a locked value, get an unonwed ValuePart referring to the register.
246 ValuePart get_unowned() noexcept {
247 assert(has_reg());
248 ValuePart res{bank()};
249 res.state.c =
250 ConstantData{.reg = cur_reg(), .owned = false, .is_const = false};
251 return res;
252 }
253
254 /// Move into a temporary register, reuse an existing register if possible.
255 ValuePart into_temporary(CompilerBase *compiler) && noexcept {
256 if (is_const()) {
257 if (state.c.const_inline) {
258 ValuePart res{state.c.inline_data, state.c.size, state.c.bank};
259 res.load_to_reg(compiler);
260 return res;
261 } else {
262 ValuePart res{state.c.data, state.c.size, state.c.bank};
263 res.load_to_reg(compiler);
264 return res;
265 }
266 }
267
268 // TODO: implement this. This needs size information to copy the value.
269 assert((has_assignment() || state.c.owned) &&
270 "into_temporary from unowned ValuePart not implemented");
271 ValuePart res{bank()};
272 res.set_value(compiler, std::move(*this));
273 if (!res.has_reg()) [[unlikely]] {
274 assert(res.is_const());
275 res.load_to_reg(compiler);
276 }
277 return res;
278 }
279
280 /// Extend integer value, reuse existing register if possible. Constants are
281 /// extended without allocating a register.
282 ValuePart into_extended(CompilerBase *compiler,
283 bool sign,
284 u32 from,
285 u32 to) && noexcept {
286 assert(from < to && "invalid integer extension sizes");
287 if (is_const() && to <= 64) {
288 u64 val = const_data()[0];
289 u64 extended = sign ? util::sext(val, from) : util::zext(val, from);
290 return ValuePart{extended, (to + 7) / 8, state.c.bank};
291 }
292 ValuePart res{bank()};
293 Reg src_reg = has_reg() ? cur_reg() : load_to_reg(compiler);
294 if (can_salvage()) {
295 res.set_value(compiler, std::move(*this));
296 assert(src_reg == res.cur_reg());
297 } else {
298 res.alloc_reg(compiler);
299 }
300 compiler->derived()->generate_raw_intext(
301 res.cur_reg(), src_reg, sign, from, to);
302 return res;
303 }
304
305 void lock(CompilerBase *compiler) noexcept;
306 void unlock(CompilerBase *compiler) noexcept;
307
308 void set_modified() noexcept {
309 assert(has_reg() && has_assignment());
310 assignment().set_modified(true);
311 }
312
313 /// Set the value to the value of a different value part, possibly taking
314 /// ownership of allocated registers. If this value part has an assignment,
315 /// the value part will be unlocked.
316 void set_value(CompilerBase *compiler, ValuePart &&other) noexcept;
317
318 /// Set the value to the value of the specified register, possibly taking
319 /// ownership of the register. Intended for filling in arguments/calls results
320 /// which inherently get stored to fixed registers. There must not be a
321 /// currently locked register.
322 void set_value_reg(CompilerBase *compiler, AsmReg reg) noexcept;
323
324 bool can_salvage() const noexcept {
325 if (!has_assignment()) {
326 return state.c.owned && state.c.reg.valid();
327 }
328
329 return state.v.owned && assignment().register_valid();
330 }
331
332private:
333 AsmReg salvage_keep_used(CompilerBase *compiler) noexcept;
334
335public:
336 // only call when can_salvage returns true and a register is known to be
337 // allocated
338 AsmReg salvage(CompilerBase *compiler) noexcept {
339 AsmReg reg = salvage_keep_used(compiler);
340 compiler->register_file.unmark_used(reg);
341 return reg;
342 }
343
344 ValLocalIdx local_idx() const noexcept {
345 assert(has_assignment());
346 return state.v.local_idx;
347 }
348
349 u32 part() const noexcept {
350 assert(has_assignment());
351 return state.v.part;
352 }
353
354 RegBank bank() const noexcept {
355 return !has_assignment() ? state.c.bank : assignment().bank();
356 }
357
358 u32 part_size() const noexcept {
359 return !has_assignment() ? state.c.size : assignment().part_size();
360 }
361
362 std::span<const u64> const_data() const noexcept {
363 assert(is_const());
364 if (state.c.const_inline) {
365 return {&state.c.inline_data, 1};
366 }
367 return {state.c.data, (state.c.size + 7) / 8};
368 }
369
370 /// Reset the reference to the value part
371 void reset(CompilerBase *compiler) noexcept;
372};
373
374template <IRAdaptor Adaptor, typename Derived, CompilerConfig Config>
375typename CompilerBase<Adaptor, Derived, Config>::AsmReg
376 CompilerBase<Adaptor, Derived, Config>::ValuePart::alloc_reg_impl(
377 CompilerBase *compiler,
378 u64 exclusion_mask,
379 const bool reload) noexcept {
380 // The caller has no control over the selected register, so it must assume
381 // that this function evicts some register. This is not permitted if the value
382 // state ought to be the same.
383 assert(compiler->may_change_value_state());
384 assert(!state.c.reg.valid());
385
386 RegBank bank;
387 if (has_assignment()) {
388 auto ap = assignment();
389 if (ap.register_valid()) {
390 lock(compiler);
391 // TODO: implement this if needed
392 assert((exclusion_mask & (1ull << state.v.reg.id())) == 0 &&
393 "moving registers in alloc_reg is unsupported");
394 return state.v.reg;
395 }
396
397 bank = ap.bank();
398 } else {
399 bank = state.c.bank;
400 }
401
402 Reg reg = compiler->select_reg(bank, exclusion_mask);
403 auto &reg_file = compiler->register_file;
404 reg_file.mark_clobbered(reg);
405 if (has_assignment()) {
406 reg_file.mark_used(reg, state.v.local_idx, state.v.part);
407 auto ap = assignment();
408 ap.set_reg(reg);
409 ap.set_register_valid(true);
410
411 // We must lock the value here, otherwise, load_from_stack could evict the
412 // register again.
413 lock(compiler);
414
415 if (reload) {
416 compiler->derived()->reload_to_reg(reg, ap);
417 } else {
418 assert(!ap.stack_valid() && "alloc_reg called on initialized value");
419 }
420 } else {
421 reg_file.mark_used(reg, INVALID_VAL_LOCAL_IDX, 0);
422 reg_file.mark_fixed(reg);
423 state.c.reg = reg;
424 state.c.owned = true;
425
426 if (reload) {
427 assert(is_const() && "cannot reload temporary value");
428 compiler->derived()->materialize_constant(
429 const_data().data(), state.c.bank, state.c.size, reg);
430 }
431 }
432
433 return reg;
434}
435
436template <IRAdaptor Adaptor, typename Derived, CompilerConfig Config>
437typename CompilerBase<Adaptor, Derived, Config>::AsmReg
438 CompilerBase<Adaptor, Derived, Config>::ValuePart::alloc_specific_impl(
439 CompilerBase *compiler, AsmReg reg, const bool reload) noexcept {
440 assert(!state.c.reg.valid());
441
442 if (has_assignment()) {
443 auto ap = assignment();
444 assert(!ap.fixed_assignment());
445
446 if (ap.register_valid() && ap.get_reg() == reg) {
447 lock(compiler);
448 return ap.get_reg();
449 }
450 }
451
452 auto &reg_file = compiler->register_file;
453 if (reg_file.is_used(reg)) {
454 compiler->evict_reg(reg);
455 }
456
457 reg_file.mark_clobbered(reg);
458 if (has_assignment()) {
459 assert(compiler->may_change_value_state());
460
461 reg_file.mark_used(reg, state.v.local_idx, state.v.part);
462 auto ap = assignment();
463 auto old_reg = AsmReg::make_invalid();
464 if (ap.register_valid()) {
465 old_reg = ap.get_reg();
466 }
467
468 ap.set_reg(reg);
469 ap.set_register_valid(true);
470
471 // We must lock the value here, otherwise, load_from_stack could evict the
472 // register again.
473 lock(compiler);
474
475 if (reload) {
476 if (old_reg.valid()) {
477 compiler->derived()->mov(reg, old_reg, ap.part_size());
478 reg_file.unmark_used(old_reg);
479 } else {
480 compiler->derived()->reload_to_reg(reg, ap);
481 }
482 } else {
483 assert(!ap.stack_valid() && "alloc_reg with valid stack slot");
484 }
485 } else {
486 reg_file.mark_used(reg, INVALID_VAL_LOCAL_IDX, 0);
487 reg_file.mark_fixed(reg);
488
489 if (reload) {
490 if (state.c.reg.valid()) {
491 // TODO: size
492 compiler->derived()->mov(reg, state.c.reg, 8);
493 reg_file.unmark_fixed(state.c.reg);
494 reg_file.unmark_used(state.c.reg);
495 } else {
496 assert(is_const() && "cannot reload temporary value");
497 compiler->derived()->materialize_constant(
498 const_data().data(), state.c.bank, state.c.size, reg);
499 }
500 }
501
502 state.c.reg = reg;
503 state.c.owned = true;
504 }
505
506 return reg;
507}
508
509template <IRAdaptor Adaptor, typename Derived, CompilerConfig Config>
510typename CompilerBase<Adaptor, Derived, Config>::AsmReg
513 AsmReg reg,
514 unsigned size) noexcept {
515 if (is_const()) {
516 compiler->derived()->materialize_constant(
517 const_data().data(), state.c.bank, state.c.size, reg);
518 return reg;
519 }
520 if (!has_assignment()) {
521 assert(has_reg());
522 assert(reg != cur_reg());
523 // TODO: value size
524 assert(size != 0);
525 compiler->derived()->mov(reg, cur_reg(), size);
526 return reg;
527 }
528
529 auto ap = assignment();
530 if (has_reg()) {
531 assert(cur_reg() != reg);
532 compiler->derived()->mov(reg, cur_reg(), ap.part_size());
533 } else if (ap.register_valid()) {
534 assert(ap.get_reg() != reg);
535
536 compiler->derived()->mov(reg, ap.get_reg(), ap.part_size());
537 } else {
538 assert(!ap.fixed_assignment());
539 compiler->derived()->reload_to_reg(reg, ap);
540 }
541
542 compiler->register_file.mark_clobbered(reg);
543 return reg;
544}
545
546template <IRAdaptor Adaptor, typename Derived, CompilerConfig Config>
547void CompilerBase<Adaptor, Derived, Config>::ValuePart::lock(
548 CompilerBase *compiler) noexcept {
549 assert(has_assignment());
550 assert(!has_reg());
551 auto ap = assignment();
552 assert(ap.register_valid());
553
554 const auto reg = ap.get_reg();
555 compiler->register_file.inc_lock_count(reg);
556 state.v.reg = reg;
557}
558
559template <IRAdaptor Adaptor, typename Derived, CompilerConfig Config>
560void CompilerBase<Adaptor, Derived, Config>::ValuePart::unlock(
561 CompilerBase *compiler) noexcept {
562 assert(has_assignment());
563 if (!state.v.reg.valid()) {
564 return;
565 }
566
567 compiler->register_file.dec_lock_count(state.v.reg);
568 state.v.reg = AsmReg::make_invalid();
569}
570
571template <IRAdaptor Adaptor, typename Derived, CompilerConfig Config>
572void CompilerBase<Adaptor, Derived, Config>::ValuePart::set_value(
573 CompilerBase *compiler, ValuePart &&other) noexcept {
574 auto &reg_file = compiler->register_file;
575 if (!has_assignment()) {
576 assert(!is_const()); // probably don't want to allow mutating constants
577 if (!other.can_salvage()) {
578 // We cannot take the register of other, so copy the value
579 AsmReg cur_reg = alloc_reg(compiler);
580 other.reload_into_specific_fixed(compiler, cur_reg);
581 other.reset(compiler);
582 return;
583 }
584
585 // This is a temporary, which might currently have a register. We want to
586 // have a temporary register that holds the value at the end.
587 if (!other.has_assignment()) {
588 assert(other.state.c.owned && "can_salvage true for unowned value??");
589 // When other is a temporary/constant, just take the value and drop our
590 // own register (if we have any).
591 reset(compiler);
592 *this = std::move(other);
593 return;
594 }
595
596 // We can take the register of other.
597 reset(compiler);
598
599 state.c.reg = other.salvage_keep_used(compiler);
600 state.c.owned = true;
601 reg_file.mark_fixed(state.c.reg);
602 reg_file.update_reg_assignment(state.c.reg, INVALID_VAL_LOCAL_IDX, 0);
603 return;
604 }
605
606 // Update the value of the assignment part
607 auto ap = assignment();
608 assert(!ap.variable_ref() && "cannot update variable ref");
609
610 if (ap.fixed_assignment() || !other.can_salvage()) {
611 // Source value owns no register or it is not reusable: copy value
612 AsmReg cur_reg = alloc_reg(compiler);
613 other.reload_into_specific_fixed(compiler, cur_reg, ap.part_size());
614 other.reset(compiler);
615 unlock(compiler);
616 ap.set_register_valid(true);
617 ap.set_modified(true);
618 return;
619 }
620
621 // Reuse register of other assignment
622 if (ap.register_valid()) {
623 // If we currently have a register, drop it
624 unlock(compiler);
625 auto cur_reg = ap.get_reg();
626 assert(!reg_file.is_fixed(cur_reg));
627 reg_file.unmark_used(cur_reg);
628 }
629
630 AsmReg new_reg = other.salvage_keep_used(compiler);
631 reg_file.update_reg_assignment(new_reg, local_idx(), part());
632 ap.set_reg(new_reg);
633 ap.set_register_valid(true);
634 ap.set_modified(true);
635}
636
637template <IRAdaptor Adaptor, typename Derived, CompilerConfig Config>
638void CompilerBase<Adaptor, Derived, Config>::ValuePart::set_value_reg(
639 CompilerBase *compiler, AsmReg value_reg) noexcept {
640 assert(compiler->may_change_value_state());
641
642 auto &reg_file = compiler->register_file;
643
644 // We could support this, but there shouldn't bee the need for that.
645 assert(value_reg.valid() && "cannot initialize with invalid register");
646 assert(!state.c.reg.valid() &&
647 "attempted to overwrite already initialized and locked ValuePartRef");
648
649 if (!has_assignment()) {
650 assert(!is_const() && "cannot mutate constant ValuePartRef");
651 state.c.reg = value_reg;
652 state.c.owned = true;
653 reg_file.mark_used(state.c.reg, INVALID_VAL_LOCAL_IDX, 0);
654 reg_file.mark_fixed(state.c.reg);
655 return;
656 }
657
658 // Update the value of the assignment part
659 auto ap = assignment();
660 assert(!ap.variable_ref() && "cannot update variable ref");
661
662 if (ap.fixed_assignment()) {
663 // For fixed assignments, copy the value into the fixed register.
664 auto cur_reg = ap.get_reg();
665 assert(reg_file.is_used(cur_reg));
666 assert(reg_file.is_fixed(cur_reg));
667 assert(reg_file.reg_local_idx(cur_reg) == local_idx());
668 // TODO: can this happen? If so, conditionally emit move.
669 assert(cur_reg != value_reg);
670 compiler->derived()->mov(cur_reg, value_reg, ap.part_size());
671 ap.set_register_valid(true);
672 ap.set_modified(true);
673 return;
674 }
675
676 // Otherwise, take the register.
677 assert(!ap.register_valid() && !ap.stack_valid() &&
678 "attempted to overwrite already initialized ValuePartRef");
679
680 reg_file.mark_used(value_reg, local_idx(), part());
681 reg_file.mark_clobbered(value_reg);
682 ap.set_reg(value_reg);
683 ap.set_register_valid(true);
684 ap.set_modified(true);
685}
686
687template <IRAdaptor Adaptor, typename Derived, CompilerConfig Config>
688typename CompilerBase<Adaptor, Derived, Config>::AsmReg
689 CompilerBase<Adaptor, Derived, Config>::ValuePart::salvage_keep_used(
690 CompilerBase *compiler) noexcept {
691 assert(compiler->may_change_value_state());
692 assert(can_salvage());
693 if (!has_assignment()) {
694 AsmReg reg = state.c.reg;
695 compiler->register_file.unmark_fixed(reg);
696 state.c.reg = AsmReg::make_invalid();
697 return reg;
698 }
699
700 auto ap = assignment();
701 assert(ap.register_valid());
702 auto cur_reg = ap.get_reg();
703
704 unlock(compiler);
705 assert(ap.fixed_assignment() || !compiler->register_file.is_fixed(cur_reg));
706 if (ap.fixed_assignment()) {
707 compiler->register_file.dec_lock_count(cur_reg); // release fixed register
708 }
709
710 ap.set_register_valid(false);
711 ap.set_fixed_assignment(false);
712 return cur_reg;
713}
714
715template <IRAdaptor Adaptor, typename Derived, CompilerConfig Config>
716void CompilerBase<Adaptor, Derived, Config>::ValuePart::reset(
717 CompilerBase *compiler) noexcept {
718 AsmReg reg = state.c.reg;
719 if (!reg.valid()) {
720 return;
721 }
722
723#ifndef NDEBUG
724 // In debug builds, touch assignment to catch cases where the assignment was
725 // already free'ed.
726 assert(!has_assignment() || assignment().modified() || true);
727#endif
728
729 if (!has_assignment()) {
730 if (state.c.owned) {
731 compiler->register_file.unmark_fixed(reg);
732 compiler->register_file.unmark_used(reg);
733 }
734 } else {
735 assert(compiler->may_change_value_state());
736 bool reg_unlocked = compiler->register_file.dec_lock_count(reg);
737 if (reg_unlocked && state.v.owned) {
738 assert(assignment().register_valid());
739 assert(!assignment().fixed_assignment());
740 compiler->register_file.unmark_used(reg);
741 assignment().set_register_valid(false);
742 }
743 }
744
745 state.c.reg = AsmReg::make_invalid();
746}
747
748template <IRAdaptor Adaptor, typename Derived, CompilerConfig Config>
749struct CompilerBase<Adaptor, Derived, Config>::ValuePartRef : ValuePart {
750 CompilerBase *compiler;
751
752 template <typename... Args>
753 ValuePartRef(CompilerBase *compiler, Args &&...args) noexcept
754 : ValuePart(std::forward<Args>(args)...), compiler(compiler) {}
755
756 explicit ValuePartRef(const ValuePartRef &) = delete;
757
758 ValuePartRef(ValuePartRef &&other) noexcept
759 : ValuePart(std::move(other)), compiler(other.compiler) {}
760
761 ~ValuePartRef() noexcept { reset(); }
762
763 ValuePartRef &operator=(const ValuePartRef &) = delete;
764
765 ValuePartRef &operator=(ValuePartRef &&other) noexcept {
766 if (this == &other) {
767 return *this;
768 }
769 reset();
770 ValuePart::operator=(std::move(other));
771 return *this;
772 }
773
774 AsmReg alloc_reg(u64 exclusion_mask = 0) noexcept {
775 return ValuePart::alloc_reg(compiler, exclusion_mask);
776 }
777
778 AsmReg alloc_try_reuse(ValuePart &ref) noexcept {
779 return ValuePart::alloc_try_reuse(compiler, ref);
780 }
781
782 void alloc_specific(AsmReg reg) noexcept {
783 ValuePart::alloc_specific(compiler, reg);
784 }
785
786 AsmReg load_to_reg() noexcept { return ValuePart::load_to_reg(compiler); }
787
788 void load_to_specific(AsmReg reg) noexcept {
789 ValuePart::load_to_specific(compiler, reg);
790 }
791
792 AsmReg reload_into_specific_fixed(AsmReg reg, unsigned size = 0) noexcept {
793 return ValuePart::reload_into_specific_fixed(compiler, reg, size);
794 }
795
796 AsmReg reload_into_specific_fixed(CompilerBase *compiler,
797 AsmReg reg,
798 unsigned size = 0) noexcept {
799 return ValuePart::reload_into_specific_fixed(compiler, reg, size);
800 }
801
802 ValuePartRef get_unowned_ref() noexcept {
803 return ValuePartRef{compiler, ValuePart::get_unowned()};
804 }
805
806 ValuePartRef into_temporary() && noexcept {
807 return ValuePartRef{
808 compiler,
809 std::move(*static_cast<ValuePart *>(this)).into_temporary(compiler)};
810 }
811
812 ValuePartRef into_extended(bool sign, u32 from, u32 to) && noexcept {
813 return ValuePartRef{compiler,
814 std::move(*static_cast<ValuePart *>(this))
815 .into_extended(compiler, sign, from, to)};
816 }
817
818 void lock() noexcept { ValuePart::lock(compiler); }
819 void unlock() noexcept { ValuePart::unlock(compiler); }
820
821 void set_value(ValuePart &&other) noexcept {
822 ValuePart::set_value(compiler, std::move(other));
823 }
824
825 void set_value_reg(AsmReg value_reg) noexcept {
826 ValuePart::set_value_reg(compiler, value_reg);
827 }
828
829 AsmReg salvage() noexcept { return ValuePart::salvage(compiler); }
830
831 void reset() noexcept { ValuePart::reset(compiler); }
832};
833
834} // namespace tpde
CompilerBase(Adaptor *adaptor)
Initialize a CompilerBase, should be called by the derived classes.