wazabin-qcode 0.2.0

Typed SSA-style p-code IR for binary analysis, modelled after Ghidra's p-code
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
//! IR value types and the central [`ValueId`] discriminant.
//!
//! Every piece of IR state is a *value* — a constant, an instruction result,
//! a memory location, a basic block, or a function. Values are stored in a
//! [`Context`] arena and addressed through cheap, `Copy` ID types. To inspect
//! a value you convert its ID into a *reference* type (e.g. [`InstructionRef`],
//! [`BlockRef`]) that borrows the context.
//!
//! # Type Map
//!
//! | ID type            | Reference type     | What it represents          |
//! |--------------------|--------------------|-----------------------------|
//! | [`LiteralId`]      | [`LiteralRef`]     | Integer constant            |
//! | [`InstructionId`]  | [`InstructionRef`] | SSA value                   |
//! | [`VarnodeId`]      | [`VarnodeRef`]     | Named memory location       |
//! | [`BlockId`]        | [`BlockRef`]       | Basic block                 |
//! | [`FunctionId`]     | [`FunctionRef`]    | Lifted or external function |
//!
//! The [`ValueId`] enum unifies all five ID types so that code that works with
//! arbitrary values (e.g. use-def chains, operand lists) can do so without
//! generics.

use crate::{context::Context, space::SpaceRef};
use serde::{Deserialize, Serialize};
use std::fmt::{Debug, Display, Formatter};

/// Declares a composite IR ID: a `{ func: FunctionId, local: LocalX }` pair.
///
/// SSA is intra-function, so every instruction/block/param/edge handle carries
/// the owning function plus a function-local index. Unlike the `Identifier`
/// newtypes these do **not** index a global `Registry` — they route through the
/// owning [`FunctionBody`]'s arena. Derived `Ord` compares `func` then `local`.
#[macro_export]
macro_rules! composite_id {
    ($name:ident, $local:ty) => {
        #[derive(
            Clone,
            Copy,
            PartialEq,
            Eq,
            Hash,
            PartialOrd,
            Ord,
            Default,
            ::serde::Serialize,
            ::serde::Deserialize,
        )]
        pub struct $name {
            pub func: $crate::value::FunctionId,
            pub local: $local,
        }

        impl $name {
            pub const fn new(func: $crate::value::FunctionId, local: $local) -> Self {
                Self { func, local }
            }

            /// Drop the qualifying function and yield the bare body-local index for
            /// in-body storage, asserting (debug builds) that the id belongs to
            /// `func` — the strict-locality tripwire. Use at every storage-flip
            /// write site where the ambient owning function is known.
            #[inline]
            pub fn localize(self, func: $crate::value::FunctionId) -> $local {
                debug_assert_eq!(
                    self.func, func,
                    concat!(stringify!($name), "::localize: foreign id"),
                );
                self.local
            }
        }

        impl ::core::fmt::Debug for $name {
            fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
                write!(
                    f,
                    concat!(stringify!($name), "({}:{})"),
                    self.func, self.local
                )
            }
        }

        impl ::core::fmt::Display for $name {
            fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
                write!(f, "{}:{}", self.func, self.local)
            }
        }
    };
}

pub use block::cfg::LocalBlockId;
pub use block::{BasicBlock, BlockId, BlockMutRef, BlockRef};
pub use block_param::{BlockParam, BlockParamId, BlockParamMutRef, BlockParamRef, LocalParamId};
pub use bytes::{
    Bytes, BytesDisplay, BytesId, BytesRef, StringEncoding, decode_string, escape_decoded,
    render_bytes_literal,
};
pub use function::{
    ArgMemKind, BodyArenaKindStats, BodyArenaStats, DerivedOutput, ExternArg, ExternArgmem,
    ExternInterface, ExternSlot, Footprint, FunctionBody, FunctionEffects, FunctionId,
    FunctionKind, FunctionMutRef, FunctionRef, InterfaceSlot, MemoryChannelState,
    MemoryInterfaceMap, ParamAttrs, RamBase, RamField, RamLocations, RamObject, RamRegion,
    RegisterChannelState, RegisterEffectSets, RegisterInterfaceMap, SlotBase, WrittenSpaces,
    WrittenSpacesState,
};
pub use insn::LocalInsnId;
pub use insn::{Instruction, InstructionId, InstructionRef};
pub use literal::{LiteralId, LiteralRef};
pub use poison::{Poison, PoisonId, PoisonRef};
pub use temp::{
    LocalTempId, LocalTempSpaceId, Temp, TempId, TempRef, TempSpace, TempSpaceId, TempSpaceRef,
};
pub use util::named::{Named, Renameable};
pub use varnode::{Varnode, VarnodeId, VarnodeRef, register::Register, register::RegisterId};

pub mod block;
pub mod block_param;
pub mod bytes;
pub mod function;
pub mod insn;
pub mod interner;
pub mod literal;
pub mod poison;
pub mod registry;
pub mod temp;
pub mod util;
pub mod varnode;
pub mod view;
pub mod view_mut;

pub use view::{BodyView, ModuleView, QCodeView};
pub use view_mut::QCodeMut;

/// A type-erased handle to any IR value stored in a [`Context`].
///
/// `ValueId` is the "universal pointer" used wherever code must refer to a
/// value without knowing its concrete type at compile time — for example in
/// instruction operand lists, use-def chains, and the address/name maps.
///
/// It is `Copy`, cheap to compare, and contains no borrow of the context.
/// To inspect the value, call `Context::get_value` which returns a
/// [`ValueRef`] tied to the context's lifetime.
///
/// # Exhaustiveness
///
/// `ValueId` is `#[non_exhaustive]`; new variants may be added in future
/// versions without a major semver bump.
#[non_exhaustive]
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, Serialize, Deserialize)]
pub enum ValueId {
    /// A compile-time integer constant, optionally carrying a symbolic label.
    Literal(LiteralId),
    /// A compile-time opaque byte blob wider than a [`Literal`](crate::value::literal::Literal) can hold.
    Bytes(BytesId),
    /// An SSA value produced by an [`Instruction`].
    Instruction(InstructionId),
    /// A control-flow node ([`BasicBlock`]).
    BasicBlock(BlockId),
    /// A typed parameter declared at the entry of a basic block.
    BlockParam(BlockParamId),
    /// A named memory location ([`Varnode`]) such as a register or global.
    Varnode(VarnodeId),
    /// A function-local temporary memory value.
    Temp(TempId),
    /// A lifted or external [`FunctionBody`].
    Function(FunctionId),
    /// A typed **poison** value: a placeholder with undefined bits (argpromote
    /// v2 clobber slots). Never folded by GVN; reading it in the emulator is a
    /// hard error. See [`poison`].
    Poison(PoisonId),
}

impl ValueId {
    pub fn ty(&self) -> &'static str {
        match self {
            ValueId::Literal(_) => "Literal",
            ValueId::Bytes(_) => "Bytes",
            ValueId::Instruction(_) => "Instruction",
            ValueId::BasicBlock(_) => "BasicBlock",
            ValueId::BlockParam(_) => "BlockParam",
            ValueId::Varnode(_) => "Varnode",
            ValueId::Temp(_) => "Temp",
            ValueId::Function(_) => "Function",
            ValueId::Poison(_) => "Poison",
        }
    }

    /// The function that owns this value's definition, if it is an SSA def
    /// (an [`Instruction`] result or a [`BlockParam`]). Shared values (literals,
    /// bytes, varnodes) and functions/blocks return `None` — they have no single
    /// owning function and their per-function use-lists live in each using
    /// function's reverse-use map (exposed through [`FunctionBody::users_of`]).
    pub fn owning_function(self) -> Option<FunctionId> {
        match self {
            ValueId::Instruction(id) => Some(id.func),
            ValueId::BlockParam(id) => Some(id.func),
            ValueId::Temp(id) => Some(id.func),
            _ => None,
        }
    }

    /// The function whose **name table** owns this value's name, if any. Block,
    /// instruction, and block-param names are function-scoped, so those return
    /// their function; module-scoped values (functions, varnodes, spaces,
    /// literals, bytes) return `None` and use the global name map. Unlike
    /// [`owning_function`](Self::owning_function) this includes blocks (by their
    /// storage function).
    pub fn name_scope_function(self) -> Option<FunctionId> {
        match self {
            ValueId::Instruction(id) => Some(id.func),
            ValueId::BlockParam(id) => Some(id.func),
            ValueId::BasicBlock(id) => Some(id.func),
            ValueId::Temp(id) => Some(id.func),
            _ => None,
        }
    }

    pub fn as_literal(self) -> Option<LiteralId> {
        if let ValueId::Literal(id) = self {
            Some(id)
        } else {
            None
        }
    }

    pub fn as_instruction(self) -> Option<InstructionId> {
        if let ValueId::Instruction(id) = self {
            Some(id)
        } else {
            None
        }
    }

    pub fn as_block(self) -> Option<BlockId> {
        if let ValueId::BasicBlock(id) = self {
            Some(id)
        } else {
            None
        }
    }

    pub fn as_block_param(self) -> Option<BlockParamId> {
        if let ValueId::BlockParam(id) = self {
            Some(id)
        } else {
            None
        }
    }

    pub fn as_bytes(self) -> Option<BytesId> {
        if let ValueId::Bytes(id) = self {
            Some(id)
        } else {
            None
        }
    }

    pub fn is_varnode(self) -> bool {
        matches!(self, ValueId::Varnode(_))
    }

    pub fn as_varnode(self) -> Option<VarnodeId> {
        if let ValueId::Varnode(id) = self {
            Some(id)
        } else {
            None
        }
    }

    pub fn as_temp(self) -> Option<TempId> {
        if let ValueId::Temp(id) = self {
            Some(id)
        } else {
            None
        }
    }

    pub fn as_function(self) -> Option<FunctionId> {
        if let ValueId::Function(id) = self {
            Some(id)
        } else {
            None
        }
    }

    pub fn as_poison(self) -> Option<PoisonId> {
        if let ValueId::Poison(id) = self {
            Some(id)
        } else {
            None
        }
    }

    pub fn is_poison(self) -> bool {
        matches!(self, ValueId::Poison(_))
    }
}

impl From<LiteralId> for ValueId {
    fn from(id: LiteralId) -> Self {
        ValueId::Literal(id)
    }
}

impl From<BytesId> for ValueId {
    fn from(id: BytesId) -> Self {
        ValueId::Bytes(id)
    }
}

impl From<InstructionId> for ValueId {
    fn from(id: InstructionId) -> Self {
        ValueId::Instruction(id)
    }
}

impl From<BlockId> for ValueId {
    fn from(id: BlockId) -> Self {
        ValueId::BasicBlock(id)
    }
}

impl From<BlockParamId> for ValueId {
    fn from(id: BlockParamId) -> Self {
        ValueId::BlockParam(id)
    }
}

impl From<VarnodeId> for ValueId {
    fn from(id: VarnodeId) -> Self {
        ValueId::Varnode(id)
    }
}

impl From<TempId> for ValueId {
    fn from(id: TempId) -> Self {
        ValueId::Temp(id)
    }
}

impl From<FunctionId> for ValueId {
    fn from(id: FunctionId) -> Self {
        ValueId::Function(id)
    }
}

impl From<PoisonId> for ValueId {
    fn from(id: PoisonId) -> Self {
        ValueId::Poison(id)
    }
}

impl ValueId {
    /// A total-order sort key that does not require `Into<usize>` (which the
    /// composite instruction/block/param IDs deliberately lack). The tuple is
    /// `(variant_tag, function-or-0, local-or-global-index)`; global values put
    /// their index in the third slot with function 0.
    pub fn order_key(&self) -> (u8, u32, u32) {
        match *self {
            ValueId::Literal(id) => (0, 0, u32::try_from(usize::from(id)).unwrap_or(u32::MAX)),
            ValueId::Bytes(id) => (1, 0, u32::try_from(usize::from(id)).unwrap_or(u32::MAX)),
            ValueId::Varnode(id) => (2, 0, u32::try_from(usize::from(id)).unwrap_or(u32::MAX)),
            ValueId::Function(id) => (3, 0, u32::try_from(usize::from(id)).unwrap_or(u32::MAX)),
            ValueId::Instruction(id) => {
                (4, usize::from(id.func) as u32, usize::from(id.local) as u32)
            }
            ValueId::BasicBlock(id) => {
                (5, usize::from(id.func) as u32, usize::from(id.local) as u32)
            }
            ValueId::BlockParam(id) => {
                (6, usize::from(id.func) as u32, usize::from(id.local) as u32)
            }
            ValueId::Temp(id) => (7, usize::from(id.func) as u32, usize::from(id.local) as u32),
            ValueId::Poison(id) => (8, 0, u32::try_from(usize::from(id)).unwrap_or(u32::MAX)),
        }
    }
}

impl Display for ValueId {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        match *self {
            ValueId::Literal(id) => write!(f, "Literal({})", usize::from(id)),
            ValueId::Bytes(id) => write!(f, "Bytes({})", usize::from(id)),
            ValueId::Varnode(id) => write!(f, "Varnode({})", usize::from(id)),
            ValueId::Function(id) => write!(f, "Function({})", usize::from(id)),
            ValueId::Instruction(id) => write!(f, "Instruction({id})"),
            ValueId::BasicBlock(id) => write!(f, "BasicBlock({id})"),
            ValueId::BlockParam(id) => write!(f, "BlockParam({id})"),
            ValueId::Temp(id) => write!(f, "Temp({id})"),
            ValueId::Poison(id) => write!(f, "Poison({})", usize::from(id)),
        }
    }
}

/// The **body-local** twin of [`ValueId`]: the form an in-body operand holds once
/// storage is localized (stage 6a, ruling 2).
///
/// It mirrors `ValueId` variant-for-variant, but its arena arms
/// (`Instruction`/`BasicBlock`/`BlockParam`) carry a **bare function-local index**
/// (`LocalInsnId`/`LocalBlockId`/`LocalParamId`) with **no** owning `FunctionId` —
/// SSA is intra-function, so the owning function is the ambient body and does not
/// need re-storing on every operand. The shared/module arms
/// (`Literal`/`Bytes`/`Varnode`/`Function`) carry the exact same globally-interned
/// or module ids as `ValueId` (they are already the boundary currency).
///
/// The two forms convert through [`LocalValueId::qualify`] (stamp the ambient
/// `func`) and [`ValueId::localize`] (drop it, asserting it matched). `ValueId`
/// stays the qualified boundary currency (module tables, refs, consumer crates);
/// `LocalValueId` is confined to in-body storage.
#[non_exhaustive]
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, Serialize, Deserialize)]
pub enum LocalValueId {
    /// A compile-time integer constant (module-interned; same id as `ValueId`).
    Literal(LiteralId),
    /// A compile-time opaque byte blob (module-interned; same id as `ValueId`).
    Bytes(BytesId),
    /// An SSA value produced by an [`Instruction`] — bare body-local index.
    Instruction(LocalInsnId),
    /// A control-flow node ([`BasicBlock`]) — bare body-local index.
    BasicBlock(LocalBlockId),
    /// A typed block-entry parameter — bare body-local index.
    BlockParam(LocalParamId),
    /// A named memory location ([`Varnode`]) (module id; same as `ValueId`).
    Varnode(VarnodeId),
    /// A body-owned temporary value — bare body-local index.
    Temp(LocalTempId),
    /// A lifted or external [`FunctionBody`] (module id; same as `ValueId`).
    Function(FunctionId),
    /// A typed poison value (module-interned; same id as `ValueId`).
    Poison(PoisonId),
}

impl LocalValueId {
    /// Qualify a body-local id back into the boundary [`ValueId`] by stamping the
    /// owning function `func` onto the arena arms. Shared/module arms pass through
    /// unchanged.
    pub fn qualify(self, func: FunctionId) -> ValueId {
        match self {
            LocalValueId::Literal(id) => ValueId::Literal(id),
            LocalValueId::Bytes(id) => ValueId::Bytes(id),
            LocalValueId::Varnode(id) => ValueId::Varnode(id),
            LocalValueId::Function(id) => ValueId::Function(id),
            LocalValueId::Poison(id) => ValueId::Poison(id),
            LocalValueId::Instruction(local) => {
                ValueId::Instruction(InstructionId::new(func, local))
            }
            LocalValueId::BasicBlock(local) => ValueId::BasicBlock(BlockId::new(func, local)),
            LocalValueId::BlockParam(local) => ValueId::BlockParam(BlockParamId::new(func, local)),
            LocalValueId::Temp(local) => ValueId::Temp(TempId::new(func, local)),
        }
    }
}

impl ValueId {
    /// Localize a qualified id for storage inside `func`'s body, dropping the
    /// owning `FunctionId` from the arena arms. In debug builds this asserts the
    /// id's `func` equals `func` — the strict-locality tripwire (ruling 2): a
    /// foreign operand is a bug and fires loudly here rather than misrouting.
    /// Shared/module arms pass through unchanged.
    pub fn localize(self, func: FunctionId) -> LocalValueId {
        match self {
            ValueId::Literal(id) => LocalValueId::Literal(id),
            ValueId::Bytes(id) => LocalValueId::Bytes(id),
            ValueId::Varnode(id) => LocalValueId::Varnode(id),
            ValueId::Function(id) => LocalValueId::Function(id),
            ValueId::Poison(id) => LocalValueId::Poison(id),
            ValueId::Instruction(id) => {
                debug_assert_eq!(
                    id.func, func,
                    "localize: foreign instruction operand {id:?} in function {func} \
                     (strict IR locality, ruling 2)"
                );
                LocalValueId::Instruction(id.local)
            }
            ValueId::BasicBlock(id) => {
                debug_assert_eq!(
                    id.func, func,
                    "localize: foreign block operand {id:?} in function {func} \
                     (strict IR locality, ruling 2)"
                );
                LocalValueId::BasicBlock(id.local)
            }
            ValueId::BlockParam(id) => {
                debug_assert_eq!(
                    id.func, func,
                    "localize: foreign block-param operand {id:?} in function {func} \
                     (strict IR locality, ruling 2)"
                );
                LocalValueId::BlockParam(id.local)
            }
            ValueId::Temp(id) => LocalValueId::Temp(id.localize(func)),
        }
    }

    /// Drop the owning `FunctionId` from the arena arms **without** a locality
    /// check, using the id's *own* embedded func. Unlike [`localize`](Self::localize)
    /// there is no ambient function to assert against: this is for keying a
    /// per-function body map (e.g. `FunctionBody.users`) by a value that already
    /// carries its own func. Within one body's map every arena key has that
    /// body's func, so stripping it is injective and lookup-stable; shared/module
    /// arms pass through unchanged.
    pub fn strip_func(self) -> LocalValueId {
        match self {
            ValueId::Literal(id) => LocalValueId::Literal(id),
            ValueId::Bytes(id) => LocalValueId::Bytes(id),
            ValueId::Varnode(id) => LocalValueId::Varnode(id),
            ValueId::Function(id) => LocalValueId::Function(id),
            ValueId::Poison(id) => LocalValueId::Poison(id),
            ValueId::Instruction(id) => LocalValueId::Instruction(id.local),
            ValueId::BasicBlock(id) => LocalValueId::BasicBlock(id.local),
            ValueId::BlockParam(id) => LocalValueId::BlockParam(id.local),
            ValueId::Temp(id) => LocalValueId::Temp(id.local),
        }
    }

    /// Localize this id **only if it is function-agnostic** — a shared/module arm
    /// (`Literal`/`Bytes`/`Varnode`/`Function`) that carries no owning function and
    /// so needs no ambient body to convert. Returns `None` for the function-scoped
    /// arena arms (`Instruction`/`BasicBlock`/`BlockParam`/`Temp`), which cannot be
    /// dropped into a *different* body's local space without misrouting. This is the
    /// safe localizer for a value that may legitimately be a constant flowing into a
    /// minted body but must otherwise be remapped through a value map.
    pub fn as_function_agnostic(self) -> Option<LocalValueId> {
        match self {
            ValueId::Literal(id) => Some(LocalValueId::Literal(id)),
            ValueId::Bytes(id) => Some(LocalValueId::Bytes(id)),
            ValueId::Varnode(id) => Some(LocalValueId::Varnode(id)),
            ValueId::Function(id) => Some(LocalValueId::Function(id)),
            ValueId::Poison(id) => Some(LocalValueId::Poison(id)),
            ValueId::Instruction(_)
            | ValueId::BasicBlock(_)
            | ValueId::BlockParam(_)
            | ValueId::Temp(_) => None,
        }
    }
}

/// Trait implemented by all typed value reference types.
///
/// Provides a uniform interface to a value's [`ValueId`] and its size in bytes.
/// Terminators and non-data values (blocks, functions) report `size() == 0`.
pub trait Value<'str, 'ctx>: Display {
    /// The context-unique identifier for this value.
    fn id(&self) -> ValueId;

    /// The size of this value's output in bytes, or `0` for non-data values
    /// (terminators, blocks, functions).
    fn size(&self) -> usize;
}

/// A borrowed, type-erased view of any value in a [`Context`].
///
/// `ValueRef` is the runtime-typed counterpart to [`ValueId`]. It is
/// produced by `Context::get_value` and borrows the context for `'ctx`.
/// Use pattern matching to downcast to a concrete reference type.
pub enum ValueRef<'str, 'ctx, R = ModuleView<'ctx, 'str>> {
    Literal(LiteralRef<'str, 'ctx>),
    Bytes(BytesRef<'str, 'ctx>),
    Instruction(InstructionRef<'str, 'ctx, R>),
    BasicBlock(BlockRef<'str, 'ctx, R>),
    BlockParam(BlockParamRef<'str, 'ctx, R>),
    Varnode(VarnodeRef<'str, 'ctx>),
    Temp(TempRef<'str, 'ctx, R>),
    Function(FunctionRef<'str, 'ctx, R>),
    Poison(PoisonRef<'str, 'ctx>),
}

impl<R> Debug for ValueRef<'_, '_, R> {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        match self {
            ValueRef::Literal(_) => f.write_str("Literal"),
            ValueRef::Bytes(_) => f.write_str("Bytes"),
            ValueRef::Instruction(_) => f.write_str("Instruction"),
            ValueRef::BasicBlock(_) => f.write_str("BasicBlock"),
            ValueRef::BlockParam(_) => f.write_str("BlockParam"),
            ValueRef::Varnode(_) => f.write_str("Varnode"),
            ValueRef::Temp(_) => f.write_str("Temp"),
            ValueRef::Function(_) => f.write_str("Function"),
            ValueRef::Poison(_) => f.write_str("Poison"),
        }
    }
}

impl<'str, 'ctx, R> From<LiteralRef<'str, 'ctx>> for ValueRef<'str, 'ctx, R> {
    fn from(lit_ref: LiteralRef<'str, 'ctx>) -> Self {
        ValueRef::Literal(lit_ref)
    }
}

impl<'str, 'ctx, R> From<BytesRef<'str, 'ctx>> for ValueRef<'str, 'ctx, R> {
    fn from(bytes_ref: BytesRef<'str, 'ctx>) -> Self {
        ValueRef::Bytes(bytes_ref)
    }
}

impl<'str, 'ctx, R> From<InstructionRef<'str, 'ctx, R>> for ValueRef<'str, 'ctx, R> {
    fn from(insn_ref: InstructionRef<'str, 'ctx, R>) -> Self {
        ValueRef::Instruction(insn_ref)
    }
}

impl<'str, 'ctx, R> From<BlockRef<'str, 'ctx, R>> for ValueRef<'str, 'ctx, R> {
    fn from(bb_ref: BlockRef<'str, 'ctx, R>) -> Self {
        ValueRef::BasicBlock(bb_ref)
    }
}

impl<'str, 'ctx, R> From<BlockParamRef<'str, 'ctx, R>> for ValueRef<'str, 'ctx, R> {
    fn from(param_ref: BlockParamRef<'str, 'ctx, R>) -> Self {
        ValueRef::BlockParam(param_ref)
    }
}

impl<'str, 'ctx, R> From<VarnodeRef<'str, 'ctx>> for ValueRef<'str, 'ctx, R> {
    fn from(var_ref: VarnodeRef<'str, 'ctx>) -> Self {
        ValueRef::Varnode(var_ref)
    }
}

impl<'str, 'ctx, R> From<TempRef<'str, 'ctx, R>> for ValueRef<'str, 'ctx, R> {
    fn from(temp_ref: TempRef<'str, 'ctx, R>) -> Self {
        ValueRef::Temp(temp_ref)
    }
}

impl<'str, 'ctx, R> From<FunctionRef<'str, 'ctx, R>> for ValueRef<'str, 'ctx, R> {
    fn from(fn_ref: FunctionRef<'str, 'ctx, R>) -> Self {
        ValueRef::Function(fn_ref)
    }
}

impl<'str, 'ctx, R> From<PoisonRef<'str, 'ctx>> for ValueRef<'str, 'ctx, R> {
    fn from(poison_ref: PoisonRef<'str, 'ctx>) -> Self {
        ValueRef::Poison(poison_ref)
    }
}

impl<'str, 'ctx> ValueRef<'str, 'ctx> {
    pub fn new(id: ValueId, ctx: &'ctx Context<'str>) -> Self {
        ValueRef::from_view(ModuleView::new(ctx), id)
    }

    pub fn from_id(ctx: &'ctx Context<'str>, id: ValueId) -> Self {
        Self::new(id, ctx)
    }
}

impl<'str: 'ctx, 'ctx, R> ValueRef<'str, 'ctx, R>
where
    R: QCodeView<'ctx, 'str>,
{
    pub fn from_view(view: R, id: ValueId) -> Self {
        match id {
            ValueId::Literal(id) => ValueRef::Literal(LiteralRef::from_id(view.shared(), id)),
            ValueId::Bytes(id) => ValueRef::Bytes(BytesRef::from_id(view.shared(), id)),
            ValueId::Varnode(id) => ValueRef::Varnode(Varnode::from_id(view.shared(), id)),
            ValueId::Temp(id) => ValueRef::Temp(TempRef::new(view, id)),
            ValueId::Instruction(id) => ValueRef::Instruction(InstructionRef::new(view, id)),
            ValueId::BasicBlock(id) => ValueRef::BasicBlock(BlockRef::new(view, id)),
            ValueId::BlockParam(id) => ValueRef::BlockParam(BlockParamRef::new(view, id)),
            ValueId::Function(id) => ValueRef::Function(FunctionRef::new(view, id)),
            ValueId::Poison(id) => ValueRef::Poison(PoisonRef::from_id(view.shared(), id)),
        }
    }

    fn inner(&self) -> &dyn Value<'str, 'ctx> {
        match self {
            ValueRef::Literal(r) => r,
            ValueRef::Bytes(r) => r,
            ValueRef::Instruction(r) => r,
            ValueRef::BasicBlock(r) => r,
            ValueRef::BlockParam(r) => r,
            ValueRef::Varnode(r) => r,
            ValueRef::Temp(r) => r,
            ValueRef::Function(r) => r,
            ValueRef::Poison(r) => r,
        }
    }

    pub fn space(&self) -> Option<SpaceRef<'ctx>> {
        match self {
            ValueRef::Varnode(v) => Some(v.space()),
            ValueRef::Instruction(i) => i.space(),
            ValueRef::Temp(_) => None,
            ValueRef::Literal(_)
            | ValueRef::Bytes(_)
            | ValueRef::BasicBlock(_)
            | ValueRef::BlockParam(_)
            | ValueRef::Function(_)
            | ValueRef::Poison(_) => None,
        }
    }

    /// Qualified memory-space provenance. Unlike [`space`](Self::space), this
    /// represents body-local temporary spaces without pretending they are
    /// shared [`Space`](crate::space::Space) values.
    pub fn memory_space(&self) -> Option<crate::space::MemorySpaceId> {
        match self {
            ValueRef::Varnode(v) => Some(crate::space::MemorySpaceId::Shared(v.space().id)),
            ValueRef::Instruction(i) => i.memory_space(),
            ValueRef::Temp(t) => Some(t.memory_space()),
            ValueRef::Literal(_)
            | ValueRef::Bytes(_)
            | ValueRef::BasicBlock(_)
            | ValueRef::BlockParam(_)
            | ValueRef::Function(_)
            | ValueRef::Poison(_) => None,
        }
    }
}

impl<'str: 'ctx, 'ctx, R> Display for ValueRef<'str, 'ctx, R>
where
    R: QCodeView<'ctx, 'str>,
{
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        // A value operand's rendering — `<ty> <atom>` uniformly, bare for value
        // references with no scalar type — is defined once, as tokens, in the
        // instruction `segment` module; `Display` is those tokens concatenated.
        //
        // Shared-leaf refs (literal, bytes, varnode) carry only a `&Shared`, so
        // they render through the `&Shared` token path; the arena-cluster refs
        // route reads through their static `QCodeView` provider (context-split
        // Pin B).
        let tokens = match self {
            ValueRef::Literal(r) => insn::segment::value_tokens_shared(r.ctx, self.id()),
            ValueRef::Bytes(r) => insn::segment::value_tokens_shared(r.ctx, self.id()),
            ValueRef::Varnode(r) => insn::segment::value_tokens_shared(r.ctx, self.id()),
            ValueRef::Poison(r) => insn::segment::value_tokens_shared(r.ctx, self.id()),
            ValueRef::Temp(r) => insn::segment::value_tokens_view(r.view, self.id()),
            ValueRef::Instruction(r) => insn::segment::value_tokens_view(r.view, self.id()),
            ValueRef::BasicBlock(r) => insn::segment::value_tokens_view(r.view, self.id()),
            ValueRef::BlockParam(r) => insn::segment::value_tokens_view(r.view, self.id()),
            ValueRef::Function(r) => insn::segment::value_tokens_view(r.view, self.id()),
        };
        for token in tokens {
            write!(f, "{}", token.text)?;
        }
        Ok(())
    }
}

impl<'str: 'ctx, 'ctx, R> Value<'str, 'ctx> for ValueRef<'str, 'ctx, R>
where
    R: QCodeView<'ctx, 'str>,
{
    fn id(&self) -> ValueId {
        self.inner().id()
    }

    fn size(&self) -> usize {
        self.inner().size()
    }
}

#[cfg(test)]
mod local_value_id_tests {
    use super::*;

    /// Every `ValueId` variant round-trips losslessly through
    /// `localize(func).qualify(func)`, with the arena arms carrying the given
    /// `func` and the shared/module arms ignoring it.
    #[test]
    fn qualify_localize_round_trips_every_variant() {
        let func = FunctionId::from(7usize);
        let other = FunctionId::from(3usize);

        let arena: [ValueId; 4] = [
            ValueId::Instruction(InstructionId::new(func, LocalInsnId::from(2usize))),
            ValueId::BasicBlock(BlockId::new(func, LocalBlockId::from(5usize))),
            ValueId::BlockParam(BlockParamId::new(func, LocalParamId::from(1usize))),
            ValueId::Temp(TempId::new(func, LocalTempId::from(4usize))),
        ];
        for id in arena {
            assert_eq!(id.localize(func).qualify(func), id, "{id:?}");
        }

        // Shared/module arms pass through regardless of the ambient func.
        let shared: [ValueId; 4] = [
            ValueId::Literal(LiteralId::from(0usize)),
            ValueId::Bytes(BytesId::from(0usize)),
            ValueId::Varnode(VarnodeId::from(0usize)),
            ValueId::Function(other),
        ];
        for id in shared {
            assert_eq!(id.localize(func).qualify(func), id, "{id:?}");
            // Any func requalifies a shared arm to itself — it carries no func.
            assert_eq!(id.localize(func).qualify(other), id, "{id:?}");
        }
    }

    /// `localize` asserts the operand's func matches the ambient body (the
    /// strict-locality tripwire). A foreign arena id panics in debug builds.
    #[test]
    #[should_panic(expected = "strict IR locality")]
    #[cfg(debug_assertions)]
    fn localize_rejects_foreign_arena_id() {
        let func = FunctionId::from(7usize);
        let foreign = FunctionId::from(9usize);
        let id = ValueId::Instruction(InstructionId::new(foreign, LocalInsnId::from(0usize)));
        let _ = id.localize(func);
    }

    #[test]
    #[should_panic(expected = "TempId::localize: foreign id")]
    #[cfg(debug_assertions)]
    fn localize_rejects_foreign_temporary_id() {
        let owner = FunctionId::from(7usize);
        let foreign = FunctionId::from(9usize);
        let id = ValueId::Temp(TempId::new(foreign, LocalTempId::from(0usize)));
        let _ = id.localize(owner);
    }
}