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 // TODO: implement this. This needs size information to copy the value.
257 assert((has_assignment() || state.c.owned) &&
258 "into_temporary from unowned ValuePart not implemented");
259 ValuePart res{bank()};
260 res.set_value(compiler, std::move(*this));
261 if (!res.has_reg()) [[unlikely]] {
262 assert(res.is_const());
263 res.load_to_reg(compiler);
264 }
265 return res;
266 }
267
268 /// Extend integer value, reuse existing register if possible. Constants are
269 /// extended without allocating a register.
270 ValuePart into_extended(CompilerBase *compiler,
271 bool sign,
272 u32 from,
273 u32 to) && noexcept {
274 assert(from < to && "invalid integer extension sizes");
275 if (is_const() && to <= 64) {
276 u64 val = const_data()[0];
277 u64 extended = sign ? util::sext(val, from) : util::zext(val, from);
278 return ValuePart{extended, (to + 7) / 8, state.c.bank};
279 }
280 ValuePart res{bank()};
281 Reg src_reg = has_reg() ? cur_reg() : load_to_reg(compiler);
282 if (can_salvage()) {
283 res.set_value(compiler, std::move(*this));
284 assert(src_reg == res.cur_reg());
285 } else {
286 res.alloc_reg(compiler);
287 }
288 compiler->derived()->generate_raw_intext(
289 res.cur_reg(), src_reg, sign, from, to);
290 return res;
291 }
292
293 void lock(CompilerBase *compiler) noexcept;
294 void unlock(CompilerBase *compiler) noexcept;
295
296 void set_modified() noexcept {
297 assert(has_reg() && has_assignment());
298 assignment().set_modified(true);
299 }
300
301 void set_value(CompilerBase *compiler, ValuePart &&other) noexcept;
302
303 /// Set the value to the value of the specified register, possibly taking
304 /// ownership of the register. Intended for filling in arguments/calls results
305 /// which inherently get stored to fixed registers. There must not be a
306 /// currently locked register.
307 void set_value_reg(CompilerBase *compiler, AsmReg reg) noexcept;
308
309 bool can_salvage() const noexcept {
310 if (!has_assignment()) {
311 return state.c.owned && state.c.reg.valid();
312 }
313
314 return state.v.owned && assignment().register_valid();
315 }
316
317private:
318 AsmReg salvage_keep_used(CompilerBase *compiler) noexcept;
319
320public:
321 // only call when can_salvage returns true and a register is known to be
322 // allocated
323 AsmReg salvage(CompilerBase *compiler) noexcept {
324 AsmReg reg = salvage_keep_used(compiler);
325 compiler->register_file.unmark_used(reg);
326 return reg;
327 }
328
329 ValLocalIdx local_idx() const noexcept {
330 assert(has_assignment());
331 return state.v.local_idx;
332 }
333
334 u32 part() const noexcept {
335 assert(has_assignment());
336 return state.v.part;
337 }
338
339 RegBank bank() const noexcept {
340 return !has_assignment() ? state.c.bank : assignment().bank();
341 }
342
343 u32 part_size() const noexcept {
344 return !has_assignment() ? state.c.size : assignment().part_size();
345 }
346
347 std::span<const u64> const_data() const noexcept {
348 assert(is_const());
349 if (state.c.const_inline) {
350 return {&state.c.inline_data, 1};
351 }
352 return {state.c.data, (state.c.size + 7) / 8};
353 }
354
355 /// Reset the reference to the value part
356 void reset(CompilerBase *compiler) noexcept;
357};
358
359template <IRAdaptor Adaptor, typename Derived, CompilerConfig Config>
360typename CompilerBase<Adaptor, Derived, Config>::AsmReg
361 CompilerBase<Adaptor, Derived, Config>::ValuePart::alloc_reg_impl(
362 CompilerBase *compiler,
363 u64 exclusion_mask,
364 const bool reload) noexcept {
365 // The caller has no control over the selected register, so it must assume
366 // that this function evicts some register. This is not permitted if the value
367 // state ought to be the same.
368 assert(compiler->may_change_value_state());
369 assert(!state.c.reg.valid());
370
371 RegBank bank;
372 if (has_assignment()) {
373 auto ap = assignment();
374 if (ap.register_valid()) {
375 lock(compiler);
376 // TODO: implement this if needed
377 assert((exclusion_mask & (1ull << state.v.reg.id())) == 0 &&
378 "moving registers in alloc_reg is unsupported");
379 return state.v.reg;
380 }
381
382 bank = ap.bank();
383 } else {
384 bank = state.c.bank;
385 }
386
387 auto &reg_file = compiler->register_file;
388 auto reg = reg_file.find_first_free_excluding(bank, exclusion_mask);
389 // TODO(ts): need to grab this from the registerfile since the reg type
390 // could be different
391 if (reg.invalid()) [[unlikely]] {
392 reg = reg_file.find_clocked_nonfixed_excluding(bank, exclusion_mask);
393 if (reg.invalid()) [[unlikely]] {
394 TPDE_FATAL("ran out of registers for value part");
395 }
396 compiler->evict_reg(reg);
397 }
398
399 reg_file.mark_clobbered(reg);
400 if (has_assignment()) {
401 reg_file.mark_used(reg, state.v.local_idx, state.v.part);
402 auto ap = assignment();
403 ap.set_reg(reg);
404 ap.set_register_valid(true);
405
406 // We must lock the value here, otherwise, load_from_stack could evict the
407 // register again.
408 lock(compiler);
409
410 if (reload) {
411 compiler->derived()->reload_to_reg(reg, ap);
412 } else {
413 assert(!ap.stack_valid() && "alloc_reg called on initialized value");
414 }
415 } else {
416 reg_file.mark_used(reg, INVALID_VAL_LOCAL_IDX, 0);
417 reg_file.mark_fixed(reg);
418 state.c.reg = reg;
419 state.c.owned = true;
420
421 if (reload) {
422 assert(is_const() && "cannot reload temporary value");
423 compiler->derived()->materialize_constant(
424 const_data().data(), state.c.bank, state.c.size, reg);
425 }
426 }
427
428 return reg;
429}
430
431template <IRAdaptor Adaptor, typename Derived, CompilerConfig Config>
432typename CompilerBase<Adaptor, Derived, Config>::AsmReg
433 CompilerBase<Adaptor, Derived, Config>::ValuePart::alloc_specific_impl(
434 CompilerBase *compiler, AsmReg reg, const bool reload) noexcept {
435 assert(!state.c.reg.valid());
436
437 if (has_assignment()) {
438 auto ap = assignment();
439 assert(!ap.fixed_assignment());
440
441 if (ap.register_valid() && ap.get_reg() == reg) {
442 lock(compiler);
443 return ap.get_reg();
444 }
445 }
446
447 auto &reg_file = compiler->register_file;
448 if (reg_file.is_used(reg)) {
449 compiler->evict_reg(reg);
450 }
451
452 reg_file.mark_clobbered(reg);
453 if (has_assignment()) {
454 assert(compiler->may_change_value_state());
455
456 reg_file.mark_used(reg, state.v.local_idx, state.v.part);
457 auto ap = assignment();
458 auto old_reg = AsmReg::make_invalid();
459 if (ap.register_valid()) {
460 old_reg = ap.get_reg();
461 }
462
463 ap.set_reg(reg);
464 ap.set_register_valid(true);
465
466 // We must lock the value here, otherwise, load_from_stack could evict the
467 // register again.
468 lock(compiler);
469
470 if (reload) {
471 if (old_reg.valid()) {
472 compiler->derived()->mov(reg, old_reg, ap.part_size());
473 reg_file.unmark_used(old_reg);
474 } else {
475 compiler->derived()->reload_to_reg(reg, ap);
476 }
477 } else {
478 assert(!ap.stack_valid() && "alloc_reg with valid stack slot");
479 }
480 } else {
481 reg_file.mark_used(reg, INVALID_VAL_LOCAL_IDX, 0);
482 reg_file.mark_fixed(reg);
483
484 if (reload) {
485 if (state.c.reg.valid()) {
486 // TODO: size
487 compiler->derived()->mov(reg, state.c.reg, 8);
488 reg_file.unmark_fixed(state.c.reg);
489 reg_file.unmark_used(state.c.reg);
490 } else {
491 assert(is_const() && "cannot reload temporary value");
492 compiler->derived()->materialize_constant(
493 const_data().data(), state.c.bank, state.c.size, reg);
494 }
495 }
496
497 state.c.reg = reg;
498 state.c.owned = true;
499 }
500
501 return reg;
502}
503
504template <IRAdaptor Adaptor, typename Derived, CompilerConfig Config>
505typename CompilerBase<Adaptor, Derived, Config>::AsmReg
506 CompilerBase<Adaptor, Derived, Config>::ValuePart::
507 reload_into_specific_fixed(CompilerBase *compiler,
508 AsmReg reg,
509 unsigned size) noexcept {
510 if (is_const()) {
511 compiler->derived()->materialize_constant(
512 const_data().data(), state.c.bank, state.c.size, reg);
513 return reg;
514 }
515 if (!has_assignment()) {
516 assert(has_reg());
517 assert(reg != cur_reg());
518 // TODO: value size
519 assert(size != 0);
520 compiler->derived()->mov(reg, cur_reg(), size);
521 return reg;
522 }
523
524 auto ap = assignment();
525 if (has_reg()) {
526 assert(cur_reg() != reg);
527 compiler->derived()->mov(reg, cur_reg(), ap.part_size());
528 } else if (ap.register_valid()) {
529 assert(ap.get_reg() != reg);
530
531 compiler->derived()->mov(reg, ap.get_reg(), ap.part_size());
532 } else {
533 assert(!ap.fixed_assignment());
534 compiler->derived()->reload_to_reg(reg, ap);
535 }
536
537 compiler->register_file.mark_clobbered(reg);
538 return reg;
539}
540
541template <IRAdaptor Adaptor, typename Derived, CompilerConfig Config>
542void CompilerBase<Adaptor, Derived, Config>::ValuePart::lock(
543 CompilerBase *compiler) noexcept {
544 assert(has_assignment());
545 assert(!has_reg());
546 auto ap = assignment();
547 assert(ap.register_valid());
548
549 const auto reg = ap.get_reg();
550 compiler->register_file.inc_lock_count(reg);
551 state.v.reg = reg;
552}
553
554template <IRAdaptor Adaptor, typename Derived, CompilerConfig Config>
555void CompilerBase<Adaptor, Derived, Config>::ValuePart::unlock(
556 CompilerBase *compiler) noexcept {
557 assert(has_assignment());
558 if (!state.v.reg.valid()) {
559 return;
560 }
561
562 compiler->register_file.dec_lock_count(state.v.reg);
563 state.v.reg = AsmReg::make_invalid();
564}
565
566template <IRAdaptor Adaptor, typename Derived, CompilerConfig Config>
567void CompilerBase<Adaptor, Derived, Config>::ValuePart::set_value(
568 CompilerBase *compiler, ValuePart &&other) noexcept {
569 auto &reg_file = compiler->register_file;
570 if (!has_assignment()) {
571 assert(!is_const()); // probably don't want to allow mutating constants
572 if (!other.can_salvage()) {
573 // We cannot take the register of other, so copy the value
574 AsmReg cur_reg = alloc_reg(compiler);
575 other.reload_into_specific_fixed(compiler, cur_reg);
576 other.reset(compiler);
577 return;
578 }
579
580 // This is a temporary, which might currently have a register. We want to
581 // have a temporary register that holds the value at the end.
582 if (!other.has_assignment()) {
583 assert(other.state.c.owned && "can_salvage true for unowned value??");
584 // When other is a temporary/constant, just take the value and drop our
585 // own register (if we have any).
586 reset(compiler);
587 *this = std::move(other);
588 return;
589 }
590
591 // We can take the register of other.
592 reset(compiler);
593
594 state.c.reg = other.salvage_keep_used(compiler);
595 state.c.owned = true;
596 reg_file.mark_fixed(state.c.reg);
597 reg_file.update_reg_assignment(state.c.reg, INVALID_VAL_LOCAL_IDX, 0);
598 return;
599 }
600
601 // Update the value of the assignment part
602 auto ap = assignment();
603 assert(!ap.variable_ref() && "cannot update variable ref");
604
605 if (ap.fixed_assignment() || !other.can_salvage()) {
606 // Source value owns no register or it is not reusable: copy value
607 AsmReg cur_reg = alloc_reg(compiler);
608 other.reload_into_specific_fixed(compiler, cur_reg, ap.part_size());
609 other.reset(compiler);
610 ap.set_register_valid(true);
611 ap.set_modified(true);
612 return;
613 }
614
615 // Reuse register of other assignment
616 if (ap.register_valid()) {
617 // If we currently have a register, drop it
618 unlock(compiler);
619 auto cur_reg = ap.get_reg();
620 assert(!reg_file.is_fixed(cur_reg));
621 reg_file.unmark_used(cur_reg);
622 }
623
624 AsmReg new_reg = other.salvage_keep_used(compiler);
625 reg_file.update_reg_assignment(new_reg, local_idx(), part());
626 ap.set_reg(new_reg);
627 ap.set_register_valid(true);
628 ap.set_modified(true);
629}
630
631template <IRAdaptor Adaptor, typename Derived, CompilerConfig Config>
632void CompilerBase<Adaptor, Derived, Config>::ValuePart::set_value_reg(
633 CompilerBase *compiler, AsmReg value_reg) noexcept {
634 assert(compiler->may_change_value_state());
635
636 auto &reg_file = compiler->register_file;
637
638 // We could support this, but there shouldn't bee the need for that.
639 assert(value_reg.valid() && "cannot initialize with invalid register");
640 assert(!state.c.reg.valid() &&
641 "attempted to overwrite already initialized and locked ValuePartRef");
642
643 if (!has_assignment()) {
644 assert(!is_const() && "cannot mutate constant ValuePartRef");
645 state.c.reg = value_reg;
646 state.c.owned = true;
647 reg_file.mark_used(state.c.reg, INVALID_VAL_LOCAL_IDX, 0);
648 reg_file.mark_fixed(state.c.reg);
649 return;
650 }
651
652 // Update the value of the assignment part
653 auto ap = assignment();
654 assert(!ap.variable_ref() && "cannot update variable ref");
655
656 if (ap.fixed_assignment()) {
657 // For fixed assignments, copy the value into the fixed register.
658 auto cur_reg = ap.get_reg();
659 assert(reg_file.is_used(cur_reg));
660 assert(reg_file.is_fixed(cur_reg));
661 assert(reg_file.reg_local_idx(cur_reg) == local_idx());
662 // TODO: can this happen? If so, conditionally emit move.
663 assert(cur_reg != value_reg);
664 compiler->derived()->mov(cur_reg, value_reg, ap.part_size());
665 ap.set_register_valid(true);
666 ap.set_modified(true);
667 return;
668 }
669
670 // Otherwise, take the register.
671 assert(!ap.register_valid() && !ap.stack_valid() &&
672 "attempted to overwrite already initialized ValuePartRef");
673
674 reg_file.mark_used(value_reg, local_idx(), part());
675 reg_file.mark_clobbered(value_reg);
676 ap.set_reg(value_reg);
677 ap.set_register_valid(true);
678 ap.set_modified(true);
679}
680
681template <IRAdaptor Adaptor, typename Derived, CompilerConfig Config>
682typename CompilerBase<Adaptor, Derived, Config>::AsmReg
683 CompilerBase<Adaptor, Derived, Config>::ValuePart::salvage_keep_used(
684 CompilerBase *compiler) noexcept {
685 assert(compiler->may_change_value_state());
686 assert(can_salvage());
687 if (!has_assignment()) {
688 AsmReg reg = state.c.reg;
689 compiler->register_file.unmark_fixed(reg);
690 state.c.reg = AsmReg::make_invalid();
691 return reg;
692 }
693
694 auto ap = assignment();
695 assert(ap.register_valid());
696 auto cur_reg = ap.get_reg();
697
698 unlock(compiler);
699 assert(ap.fixed_assignment() || !compiler->register_file.is_fixed(cur_reg));
700 if (ap.fixed_assignment()) {
701 compiler->register_file.dec_lock_count(cur_reg); // release fixed register
702 }
703
704 ap.set_register_valid(false);
705 ap.set_fixed_assignment(false);
706 return cur_reg;
707}
708
709template <IRAdaptor Adaptor, typename Derived, CompilerConfig Config>
710void CompilerBase<Adaptor, Derived, Config>::ValuePart::reset(
711 CompilerBase *compiler) noexcept {
712 AsmReg reg = state.c.reg;
713 if (!reg.valid()) {
714 return;
715 }
716
717#ifndef NDEBUG
718 // In debug builds, touch assignment to catch cases where the assignment was
719 // already free'ed.
720 assert(!has_assignment() || assignment().modified() || true);
721#endif
722
723 if (!has_assignment()) {
724 if (state.c.owned) {
725 compiler->register_file.unmark_fixed(reg);
726 compiler->register_file.unmark_used(reg);
727 }
728 } else {
729 assert(compiler->may_change_value_state());
730 bool reg_unlocked = compiler->register_file.dec_lock_count(reg);
731 if (reg_unlocked && state.v.owned) {
732 assert(assignment().register_valid());
733 assert(!assignment().fixed_assignment());
734 compiler->register_file.unmark_used(reg);
735 assignment().set_register_valid(false);
736 }
737 }
738
739 state.c.reg = AsmReg::make_invalid();
740}
741
742template <IRAdaptor Adaptor, typename Derived, CompilerConfig Config>
743struct CompilerBase<Adaptor, Derived, Config>::ValuePartRef : ValuePart {
744 CompilerBase *compiler;
745
746 template <typename... Args>
747 ValuePartRef(CompilerBase *compiler, Args &&...args) noexcept
748 : ValuePart(std::forward<Args>(args)...), compiler(compiler) {}
749
750 explicit ValuePartRef(const ValuePartRef &) = delete;
751
752 ValuePartRef(ValuePartRef &&other) noexcept
753 : ValuePart(std::move(other)), compiler(other.compiler) {}
754
755 ~ValuePartRef() noexcept { reset(); }
756
757 ValuePartRef &operator=(const ValuePartRef &) = delete;
758
759 ValuePartRef &operator=(ValuePartRef &&other) noexcept {
760 if (this == &other) {
761 return *this;
762 }
763 reset();
764 ValuePart::operator=(std::move(other));
765 return *this;
766 }
767
768 AsmReg alloc_reg(u64 exclusion_mask = 0) noexcept {
769 return ValuePart::alloc_reg(compiler, exclusion_mask);
770 }
771
772 AsmReg alloc_try_reuse(ValuePart &ref) noexcept {
773 return ValuePart::alloc_try_reuse(compiler, ref);
774 }
775
776 void alloc_specific(AsmReg reg) noexcept {
777 ValuePart::alloc_specific(compiler, reg);
778 }
779
780 AsmReg load_to_reg() noexcept { return ValuePart::load_to_reg(compiler); }
781
782 void load_to_specific(AsmReg reg) noexcept {
783 ValuePart::load_to_specific(compiler, reg);
784 }
785
786 AsmReg reload_into_specific_fixed(AsmReg reg, unsigned size = 0) noexcept {
787 return ValuePart::reload_into_specific_fixed(compiler, reg, size);
788 }
789
790 AsmReg reload_into_specific_fixed(CompilerBase *compiler,
791 AsmReg reg,
792 unsigned size = 0) noexcept {
793 return ValuePart::reload_into_specific_fixed(compiler, reg, size);
794 }
795
796 ValuePartRef get_unowned_ref() noexcept {
797 return ValuePartRef{compiler, ValuePart::get_unowned()};
798 }
799
800 ValuePartRef into_temporary() && noexcept {
801 return ValuePartRef{
802 compiler,
803 std::move(*static_cast<ValuePart *>(this)).into_temporary(compiler)};
804 }
805
806 ValuePartRef into_extended(bool sign, u32 from, u32 to) && noexcept {
807 return ValuePartRef{compiler,
808 std::move(*static_cast<ValuePart *>(this))
809 .into_extended(compiler, sign, from, to)};
810 }
811
812 void lock() noexcept { ValuePart::lock(compiler); }
813 void unlock() noexcept { ValuePart::unlock(compiler); }
814
815 void set_value(ValuePart &&other) noexcept {
816 ValuePart::set_value(compiler, std::move(other));
817 }
818
819 void set_value_reg(AsmReg value_reg) noexcept {
820 ValuePart::set_value_reg(compiler, value_reg);
821 }
822
823 AsmReg salvage() noexcept { return ValuePart::salvage(compiler); }
824
825 void reset() noexcept { ValuePart::reset(compiler); }
826};
827
828} // namespace tpde
CompilerBase(Adaptor *adaptor)
Initialize a CompilerBase, should be called by the derived classes.