wazabin-qcode 0.4.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
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
//! Token-segment rendering of instructions for rich (colored, clickable) display.
//!
//! [`instruction_segments`] produces the *same bytes* as an instruction's
//! [`Display`](std::fmt::Display) (`InstructionStatement`) — concatenating every
//! returned [`Token`]'s text reproduces the canonical textual IR exactly — while
//! additionally tagging each run with a semantic [`TokenKind`] (for coloring) and
//! an optional [`Link`] (for click-through to the referenced value, function, or
//! block). The textual format stays the single source of truth: a test asserts
//! `concat(segments) == format!("{insn}")` for every mnemonic, so the two can
//! never drift.

use crate::{
    context::{Context, Shared},
    space::Space,
    value::{
        BasicBlock, FunctionBody, LocalBlockId, LocalValueId, QCodeView, ValueId,
        block::BlockId,
        bytes::BytesRef,
        function::FunctionId,
        insn::{Callee, InstructionRef, Mnemonic},
        literal::{LiteralId, LiteralRef, SymbolicRef},
        varnode::Varnode,
    },
};

/// What a token *is*, semantically — drives syntax coloring in a viewer.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TokenKind {
    /// A type name (`i32`, `f64`), the access width of a load/store, or a cast
    /// target width.
    Type,
    /// An instruction-result reference (`%name` / `%tmp…`).
    Variable,
    /// A block parameter (`@name`), or a parameter/argument name on the left of
    /// `=` in a branch/call argument.
    BlockParam,
    /// A varnode / register reference.
    Varnode,
    /// A literal constant (`0x2`, `&<blk>`, `&"str"`) or a bare numeric offset.
    Literal,
    /// A byte-string literal (`b"…"`).
    Bytes,
    /// A reserved word or mnemonic (`load`, `goto`, `call fn`, `zext`, …).
    Keyword,
    /// An operator (`+`, ` = `, ` <- `, ` <$> `, `.`).
    Operator,
    /// Structural punctuation (`(`, `)`, `,`, `:`, `[`, `]`, `;`, spaces).
    Punctuation,
    /// A basic-block label (`<name>`).
    Label,
    /// An aggregate field name (`lhs`, `.val`).
    Field,
    /// A function reference (call/apply/map/scan target, or function used as a
    /// value operand).
    Function,
    /// A memory space name (`ram`, `register`, …).
    Space,
}

/// A click-through target carried by a token, when it names something navigable.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Link {
    Value(ValueId),
    Function(FunctionId),
    Block(BlockId),
}

/// One contiguous run of rendered text with its semantic kind and optional link.
///
/// Concatenating the `text` of every token of an instruction yields exactly the
/// instruction's `Display` output.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Token {
    pub text: String,
    pub kind: TokenKind,
    pub link: Option<Link>,
}

impl Token {
    fn new(text: impl Into<String>, kind: TokenKind, link: Option<Link>) -> Self {
        Token {
            text: text.into(),
            kind,
            link,
        }
    }
}

/// Accumulator with terse push helpers, kept private to this module.
struct Seg<'ctx, 'str, R> {
    view: R,
    out: Vec<Token>,
    marker: std::marker::PhantomData<&'ctx &'str ()>,
}

impl<'ctx, 'str: 'ctx, R> Seg<'ctx, 'str, R>
where
    R: QCodeView<'ctx, 'str>,
{
    fn push(&mut self, text: impl Into<String>, kind: TokenKind, link: Option<Link>) {
        self.out.push(Token::new(text, kind, link));
    }

    fn kw(&mut self, text: &str) {
        self.push(text, TokenKind::Keyword, None);
    }

    fn op(&mut self, text: impl Into<String>) {
        self.push(text, TokenKind::Operator, None);
    }

    fn punct(&mut self, text: &str) {
        self.push(text, TokenKind::Punctuation, None);
    }

    /// The `<ty> ` prefix shared by every typed operand. Mirrors
    /// `write!(f, "{} ", type_name)` in `ValueRef`'s `Display`.
    fn ty(&mut self, type_id: crate::types::TypeId) {
        self.push(
            format!("{} ", self.view.shared().types.type_name(type_id)),
            TokenKind::Type,
            None,
        );
    }

    /// A value operand, rendered exactly as `ValueRef`'s `Display`: `<ty> <atom>`
    /// for scalars (instruction, block param, literal, bytes, varnode), and bare
    /// for functions/blocks.
    fn value(&mut self, id: ValueId) {
        let link = Some(Link::Value(id));
        // Note: instruction / block-param operands are never foreign. They are
        // stored as bare body-local ids and qualified with their reader's own
        // `func`, so a cross-function data operand is unrepresentable — no guard
        // is needed (or wanted: one would mask a mis-qualification). Only the
        // *absolute* ids below (blocks, and symbolic block literals) can name
        // another function.
        match id {
            ValueId::Instruction(iid) => {
                let r = self.view.insn_ref(iid);
                self.ty(r.type_id());
                self.push(instruction_atom(self.view, iid), TokenKind::Variable, link);
            }
            ValueId::BlockParam(pid) => {
                let r = self.view.param_ref(pid);
                self.ty(r.type_id());
                self.push(
                    block_param_atom(self.view, pid),
                    TokenKind::BlockParam,
                    link,
                );
            }
            ValueId::Literal(lid) => {
                let r = LiteralRef::from_id(self.view.shared(), lid);
                self.ty(r.type_id());
                // The literal *atom* is rendered from the whole `&Context`, so a
                // symbolic block/function literal resolves its target name (a
                // `&Shared`-backed `LiteralRef` cannot — context-split 5b-ii #1).
                self.push(literal_atom_view(self.view, lid), TokenKind::Literal, link);
            }
            ValueId::Bytes(bid) => {
                let r = BytesRef::from_id(self.view.shared(), bid);
                self.ty(r.type_id());
                self.push(r.to_string(), TokenKind::Bytes, link);
            }
            ValueId::Varnode(vid) => {
                let r = Varnode::from_id(self.view.shared(), vid);
                self.push(format!("i{} ", r.size() * 8), TokenKind::Type, None);
                self.push(r.to_string(), TokenKind::Varnode, link);
            }
            ValueId::Poison(pid) => {
                let ty = self.view.shared().values.poisons[pid].type_id;
                self.ty(ty);
                self.push("poison".to_string(), TokenKind::Literal, link);
            }
            ValueId::Temp(id) => {
                let r = self.view.temp_ref(id);
                self.push(format!("i{} ", r.size() * 8), TokenKind::Type, None);
                self.push(r.to_string(), TokenKind::Varnode, link);
            }
            ValueId::Function(fid) => {
                let name = self.view.interface(fid).name.to_string();
                self.push(
                    format!("<{name}>"),
                    TokenKind::Function,
                    Some(Link::Function(fid)),
                );
            }
            ValueId::BasicBlock(bid) => {
                // A block used as a value renders via the block's own `Display`
                // (never `ValueRef`'s, which routes back here — that would recurse).
                // A block in another function (a transient during discovery) is
                // unreadable through a function-scoped view, so render its id
                // instead of resolving the foreign body's block text.
                let text = if self.view.owner().is_some_and(|o| o != bid.func) {
                    format!("<{bid}>")
                } else {
                    self.view.block_ref(bid).to_string()
                };
                self.push(text, TokenKind::Label, Some(Link::Block(bid)));
            }
        }
    }

    /// A value operand printed *bare* (no type prefix) for instruction results,
    /// matching `fmt_bare_value` used by `extract`/`gep`. Non-instruction values
    /// fall back to the regular typed rendering.
    fn bare_value(&mut self, id: ValueId) {
        match id {
            ValueId::Instruction(iid) => {
                self.push(
                    instruction_atom(self.view, iid),
                    TokenKind::Variable,
                    Some(Link::Value(id)),
                );
            }
            other => self.value(other),
        }
    }

    /// A direct branch/cbranch target: `<name @p=arg …>`. Mirrors
    /// `fmt_branch_target`. The `target` is a bare body-local index; `func` is the
    /// terminator's owning function (strict IR locality ⇒ the target lives in that
    /// same arena), used to recover the full [`BlockId`].
    fn branch_target(&mut self, func: FunctionId, target: LocalBlockId, args: &[LocalValueId]) {
        let target = BlockId::new(func, target);
        let block = self.view.block_ref(target);
        let name = block.name().unwrap_or("unnamed");
        self.push(
            format!("<{name}"),
            TokenKind::Label,
            Some(Link::Block(target)),
        );

        let params = block.params().collect::<Vec<_>>();
        for (i, &arg) in args.iter().enumerate() {
            self.punct(" ");
            match params.get(i) {
                Some(param) => self.push(param.to_string(), TokenKind::BlockParam, None),
                None => self.push(format!("@arg{i}"), TokenKind::BlockParam, None),
            }
            self.op("=");
            self.value(arg.qualify(func));
        }

        self.push(">", TokenKind::Label, None);
    }
}

/// The bare atom for an instruction result: `%name` or `%tmp<id>`.
fn instruction_atom<'ctx, 'str: 'ctx>(
    view: impl QCodeView<'ctx, 'str>,
    id: crate::value::InstructionId,
) -> String {
    match view.instruction(id).name.as_deref() {
        Some(name) => format!("%{name}"),
        None => format!("%tmp{:x}", usize::from(id.local)),
    }
}

/// The bare atom for a block parameter: `@name` or `@param<id>`.
fn block_param_atom<'ctx, 'str: 'ctx>(
    view: impl QCodeView<'ctx, 'str>,
    id: crate::value::BlockParamId,
) -> String {
    let r = view.param_ref(id);
    match r.name() {
        Some(name) => format!("@{name}"),
        None => format!("@param{:x}", usize::from(id.local)),
    }
}

/// Render an instruction as colored, linkable tokens. Concatenating the tokens'
/// text equals the instruction's `Display` (`as_statement()`) output.
pub fn instruction_segments<'ctx, 'str: 'ctx, R>(insn: &InstructionRef<'str, 'ctx, R>) -> Vec<Token>
where
    R: QCodeView<'ctx, 'str>,
{
    let view = insn.view;
    let mut seg = Seg {
        view,
        out: Vec::new(),
        marker: std::marker::PhantomData,
    };

    // LHS: `<ty> %name = ` (mirrors `InstructionStatement` + `InstructionRef`'s
    // inherent `fmt`). Terminators and other size-0 instructions have no LHS.
    if insn.size() != 0 {
        seg.ty(insn.type_id());
        seg.push(
            instruction_atom(view, insn.id),
            TokenKind::Variable,
            Some(Link::Value(ValueId::Instruction(insn.id))),
        );
        seg.op(" = ");
    }

    match insn.mnemonic() {
        Mnemonic::Tuple(t) => tuple_with_type(&mut seg, insn.id.func, t, insn.type_id()),
        m => mnemonic_segments(&mut seg, insn.id.func, m),
    }

    seg.out
}

fn tuple_with_type<'ctx, 'str: 'ctx>(
    seg: &mut Seg<'ctx, 'str, impl QCodeView<'ctx, 'str>>,
    func: FunctionId,
    t: &crate::value::insn::Tuple,
    type_id: crate::types::TypeId,
) {
    seg.kw("pack");
    seg.punct("(");
    for (i, &field) in t.fields.iter().enumerate() {
        if i > 0 {
            seg.punct(", ");
        }
        let name = seg
            .view
            .shared()
            .types
            .field_name(type_id, i)
            .map(str::to_owned)
            .unwrap_or_else(|| format!("field{}", i + 1));
        seg.push(name, TokenKind::Field, None);
        seg.op("=");
        seg.value(field.qualify(func));
    }
    seg.punct(");");
}

fn mnemonic_segments<'ctx, 'str: 'ctx>(
    seg: &mut Seg<'ctx, 'str, impl QCodeView<'ctx, 'str>>,
    func: FunctionId,
    m: &Mnemonic,
) {
    use crate::value::insn::Unop;
    match m {
        Mnemonic::Load(l) => {
            seg.kw("load");
            seg.punct("(");
            seg.push(space_name(seg.view, func, l.space), TokenKind::Space, None);
            seg.punct(":");
            seg.push(l.size.to_string(), TokenKind::Type, None);
            seg.punct(", ");
            seg.value(l.ptr.qualify(func));
            seg.punct(");");
        }
        Mnemonic::Store(s) => {
            seg.kw("store");
            seg.punct("(");
            seg.push(space_name(seg.view, func, s.space), TokenKind::Space, None);
            seg.punct(":");
            seg.push(s.size.to_string(), TokenKind::Type, None);
            seg.punct(", ");
            seg.value(s.ptr.qualify(func));
            seg.op(" <- ");
            seg.value(s.src.qualify(func));
            seg.punct(");");
        }
        Mnemonic::Branch(b) => {
            seg.kw("goto ");
            seg.branch_target(func, b.target, &b.args);
            seg.punct(";");
        }
        Mnemonic::BranchInd(b) => {
            seg.kw("goto ");
            seg.punct("[");
            seg.value(b.ptr.qualify(func));
            seg.punct("];");
        }
        Mnemonic::Switch(sw) => {
            seg.kw("switch ");
            seg.value(sw.scrutinee.qualify(func));
            seg.punct(" { ");
            for (i, case) in sw.cases.iter().enumerate() {
                if i > 0 {
                    seg.punct(", ");
                }
                seg.push(format!("{:#x}", case.value), TokenKind::Literal, None);
                seg.op(" => ");
                seg.branch_target(func, case.target, &case.args);
            }
            if let Some(default) = sw.default {
                if !sw.cases.is_empty() {
                    seg.punct(", ");
                }
                seg.kw("default");
                seg.op(" => ");
                seg.branch_target(func, default, &sw.default_args);
            }
            seg.punct(" };");
        }
        Mnemonic::CBranch(cb) => {
            seg.kw("if ");
            seg.value(cb.condition.qualify(func));
            seg.kw(" goto ");
            seg.branch_target(func, cb.success_block, &cb.success_args);
            seg.kw(" else goto ");
            seg.branch_target(func, cb.failure_block, &cb.failure_args);
            seg.punct(";");
        }
        Mnemonic::Apply(a) => {
            seg.kw("apply ");
            let (target, link) = callee_name_link(seg.view, a.target);
            seg.push(target, TokenKind::Function, link);
            seg.punct("(");
            for (i, &arg) in a.args.iter().enumerate() {
                if i > 0 {
                    seg.punct(", ");
                }
                seg.value(arg.qualify(func));
            }
            seg.punct(");");
        }
        Mnemonic::Call(c) => {
            seg.kw("call fn ");
            let (target, link) = callee_name_link(seg.view, c.target);
            seg.push(target, TokenKind::Function, link);
            seg.punct("(");
            for (i, &arg) in c.args.iter().enumerate() {
                if i > 0 {
                    seg.punct(", ");
                }
                let arg_name = c
                    .target
                    .real()
                    .map(|target| call_arg_name(seg.view, target, i))
                    .unwrap_or_else(|| format!("@arg{i}="));
                seg.push(arg_name, TokenKind::BlockParam, None);
                seg.value(arg.qualify(func));
            }
            seg.punct(");");
        }
        Mnemonic::TailCall(tc) => {
            seg.kw("tailcall fn ");
            let (target, link) = callee_name_link(seg.view, tc.target);
            seg.push(target, TokenKind::Function, link);
            seg.punct("(");
            for (i, &arg) in tc.args.iter().enumerate() {
                if i > 0 {
                    seg.punct(", ");
                }
                seg.value(arg.qualify(func));
            }
            seg.punct(");");
        }
        Mnemonic::CallInd(c) => {
            seg.kw("call ");
            seg.punct("[");
            seg.value(c.ptr.qualify(func));
            seg.punct("]");
            if !c.args.is_empty() {
                seg.punct("(");
                for (i, &arg) in c.args.iter().enumerate() {
                    if i > 0 {
                        seg.punct(", ");
                    }
                    seg.value(arg.qualify(func));
                }
                seg.punct(")");
            }
            seg.punct(";");
        }
        // No operands: the marker alone. Kept short so a run of unlifted padding
        // stays readable.
        Mnemonic::BadInsn(_) => {
            seg.kw("badinsn");
            seg.punct(";");
        }
        Mnemonic::Return(r) => match r.value {
            Some(value) => {
                seg.kw("return ");
                seg.value(value.qualify(func));
                seg.kw(" at ");
                seg.value(r.ptr.qualify(func));
                seg.punct(";");
            }
            None => {
                seg.kw("return at ");
                seg.value(r.ptr.qualify(func));
                seg.punct(";");
            }
        },
        Mnemonic::ReturnValue(r) => {
            seg.kw("return ");
            seg.value(r.value.qualify(func));
            seg.punct(";");
        }
        Mnemonic::Unop(u) => match u.op {
            Unop::IntNegate | Unop::IntNot | Unop::FloatNegate => {
                seg.op(format!("{} ", u.op));
                seg.value(u.src.qualify(func));
                seg.punct(";");
            }
            _ => {
                seg.kw(&u.op.to_string());
                seg.punct("(");
                seg.value(u.src.qualify(func));
                seg.punct(");");
            }
        },
        Mnemonic::Binop(b) => {
            seg.value(b.lhs.qualify(func));
            seg.op(format!(" {} ", b.op));
            seg.value(b.rhs.qualify(func));
            seg.punct(";");
        }
        Mnemonic::Zext(z) => cast(seg, func, "zext", 'i', z.size, z.src),
        Mnemonic::Sext(s) => cast(seg, func, "sext", 'i', s.size, s.src),
        Mnemonic::IntToFloat(c) => cast(seg, func, "int2float", 'f', c.size, c.src),
        Mnemonic::FloatToFloat(c) => cast(seg, func, "float2float", 'f', c.size, c.src),
        Mnemonic::FloatToInt(c) => cast(seg, func, "trunc", 'i', c.size, c.src),
        Mnemonic::Range(r) => {
            seg.value(r.src.qualify(func));
            seg.punct("[");
            seg.push(r.start.to_string(), TokenKind::Literal, None);
            seg.punct(":");
            seg.push((r.start + r.size).to_string(), TokenKind::Literal, None);
            seg.punct("];");
        }
        Mnemonic::IsFloatNaN(o) => unary_call(seg, func, "nan", o.src),
        Mnemonic::LzCount(o) => unary_call(seg, func, "lzcount", o.src),
        Mnemonic::PopCount(o) => unary_call(seg, func, "popcount", o.src),
        Mnemonic::Carry(o) => binary_call(seg, func, "carry", o.lhs, o.rhs),
        Mnemonic::SCarry(o) => binary_call(seg, func, "scarry", o.lhs, o.rhs),
        Mnemonic::SBorrow(o) => binary_call(seg, func, "sborrow", o.lhs, o.rhs),
        Mnemonic::Assert(a) => {
            seg.kw("assert ");
            seg.value(a.condition.qualify(func));
            seg.punct(";");
        }
        // `Tuple` is normally routed through `tuple_with_type` (it always has a
        // result type); this bare form mirrors `Tuple`'s own `MnemonicKind::fmt`.
        Mnemonic::Tuple(t) => {
            seg.kw("pack");
            seg.punct("(");
            for (i, &field) in t.fields.iter().enumerate() {
                if i > 0 {
                    seg.punct(", ");
                }
                seg.push(format!("field{}", i + 1), TokenKind::Field, None);
                seg.op("=");
                seg.value(field.qualify(func));
            }
            seg.punct(");");
        }
        Mnemonic::Extract(e) => {
            seg.kw("extract");
            seg.punct("(");
            seg.bare_value(e.agg.qualify(func));
            let name = e
                .field_name_view(seg.view, func)
                .map(str::to_owned)
                .unwrap_or_else(|| format!("field{}", e.index + 1));
            seg.push(format!(".{name}"), TokenKind::Field, None);
            seg.punct(");");
        }
        Mnemonic::Gep(g) => {
            seg.kw("gep");
            seg.punct("(");
            seg.bare_value(g.base.qualify(func));
            match g.field_name_view(seg.view, func) {
                Some(name) => seg.push(format!(".{name}"), TokenKind::Field, None),
                None => {
                    seg.op(" + ");
                    seg.push(format!("{:#x}", g.offset), TokenKind::Literal, None);
                }
            }
            seg.punct(");");
        }
        Mnemonic::Map(map) => {
            let (body, link) = callee_name_link(seg.view, map.body);
            if map.captures.is_empty() {
                seg.push(body, TokenKind::Function, link);
                seg.op(" <$> ");
                seg.value(map.src.qualify(func));
                seg.punct(";");
            } else {
                seg.punct("(");
                seg.push(body, TokenKind::Function, link);
                for &c in &map.captures {
                    seg.punct(" ");
                    seg.value(c.qualify(func));
                }
                seg.op(") <$> ");
                seg.value(map.src.qualify(func));
                seg.punct(";");
            }
        }
        Mnemonic::Scan(scan) => {
            let (body, link) = callee_name_link(seg.view, scan.body);
            let body = match scan.body {
                Callee::Real(_) => format!("@{body}"),
                Callee::Minted(_) => body,
            };
            if scan.captures.is_empty() {
                seg.kw("scanl ");
                seg.push(body, TokenKind::Function, link);
                seg.punct(" ");
                seg.value(scan.init.qualify(func));
                seg.punct(" ");
                seg.value(scan.src.qualify(func));
                seg.punct(";");
            } else {
                seg.kw("scanl ");
                seg.punct("(");
                seg.push(body, TokenKind::Function, link);
                for &c in &scan.captures {
                    seg.punct(" ");
                    seg.value(c.qualify(func));
                }
                seg.punct(") ");
                seg.value(scan.init.qualify(func));
                seg.punct(" ");
                seg.value(scan.src.qualify(func));
                seg.punct(";");
            }
        }
        Mnemonic::PCodeOp(p) => {
            let op = seg.view.shared().pcode_ops[p.id].to_string();
            if let Some(dst) = p.dst {
                seg.value(dst.qualify(func));
                seg.op(" = ");
            }
            seg.kw(&op);
            seg.punct("(");
            for (i, &arg) in p.args.iter().enumerate() {
                if i > 0 {
                    seg.punct(", ");
                }
                seg.value(arg.qualify(func));
            }
            seg.punct(");");
        }
        Mnemonic::Intrinsic(intr) => {
            seg.kw(&format!("${}", intr.id.name()));
            seg.punct("(");
            for (i, &arg) in intr.args.iter().enumerate() {
                if i > 0 {
                    seg.punct(", ");
                }
                seg.value(arg.qualify(func));
            }
            seg.punct(");");
        }
    }
}

fn cast<'ctx, 'str: 'ctx>(
    seg: &mut Seg<'ctx, 'str, impl QCodeView<'ctx, 'str>>,
    func: FunctionId,
    kw: &str,
    prefix: char,
    size: usize,
    src: LocalValueId,
) {
    seg.kw(kw);
    seg.punct("(");
    seg.push(format!("{prefix}{}", size * 8), TokenKind::Type, None);
    seg.punct(", ");
    seg.value(src.qualify(func));
    seg.punct(");");
}

fn unary_call<'ctx, 'str: 'ctx>(
    seg: &mut Seg<'ctx, 'str, impl QCodeView<'ctx, 'str>>,
    func: FunctionId,
    kw: &str,
    src: LocalValueId,
) {
    seg.kw(kw);
    seg.punct("(");
    seg.value(src.qualify(func));
    seg.punct(");");
}

fn binary_call<'ctx, 'str: 'ctx>(
    seg: &mut Seg<'ctx, 'str, impl QCodeView<'ctx, 'str>>,
    func: FunctionId,
    kw: &str,
    lhs: LocalValueId,
    rhs: LocalValueId,
) {
    seg.kw(kw);
    seg.punct("(");
    seg.value(lhs.qualify(func));
    seg.punct(", ");
    seg.value(rhs.qualify(func));
    seg.punct(");");
}

/// The space name as printed by `load`/`store`'s `fmt`: the named space, or a
/// `space: <id>` fallback for an unnamed space.
fn space_name<'ctx, 'str: 'ctx>(
    view: impl QCodeView<'ctx, 'str>,
    func: FunctionId,
    space: crate::space::LocalMemorySpaceId,
) -> String {
    match space.qualify(func) {
        crate::space::MemorySpaceId::Shared(space) => {
            let space_ref = Space::from_id(view.shared(), space);
            match space_ref.name.as_deref() {
                Some(name) => name.to_string(),
                None => format!("space: {space}"),
            }
        }
        // `$tempN` is an explicit body-local space token. The numeric local ID
        // makes the canonical print stable even when a display name is absent,
        // duplicated, or not a qcode identifier; lowering recreates one local
        // space per token in first-use order.
        crate::space::MemorySpaceId::Temp(space) => {
            format!("$temp{}", usize::from(space.local))
        }
    }
}

/// The `@name=` / `@arg<i>=` prefix for a direct-call argument. Mirrors
/// `fmt_call_arg_name`.
fn call_arg_name<'ctx, 'str: 'ctx>(
    view: impl QCodeView<'ctx, 'str>,
    target: FunctionId,
    index: usize,
) -> String {
    // The authoritative name is the callee's root block param, which lives in the
    // callee's *body*. A function-scoped view (a `BodyView`, as used by the
    // pass-fixpoint fingerprint) may not read another function's body at all, so
    // for a foreign callee fall back to the interface-only name — the C-prototype
    // argument name, if any, else the positional form. Purely cosmetic: only the
    // rendered argument label changes, never the operand itself.
    let foreign = view.owner().is_some_and(|owner| owner != target);
    let name = if foreign {
        view.interface(target)
            .signature
            .as_ref()
            .and_then(|s| s.extern_interface.as_ref())
            .and_then(|iface| iface.args.get(index))
            .and_then(|a| a.name.as_ref().map(|n| n.to_string()))
    } else {
        view.function_ref(target).input_arg_name(index)
    };
    match name {
        Some(name) => format!("@{name}="),
        None => format!("@arg{index}="),
    }
}

/// Render a real function symbol or an unresolved pass-local placeholder.
/// Placeholders deliberately carry no link: they are not installed functions.
fn callee_name_link<'ctx, 'str: 'ctx>(
    view: impl QCodeView<'ctx, 'str>,
    callee: Callee,
) -> (String, Option<Link>) {
    match callee {
        Callee::Real(id) => (
            view.interface(id).name.to_string(),
            Some(Link::Function(id)),
        ),
        Callee::Minted(slot) => (format!("<minted:{slot}>"), None),
    }
}

/// The token stream for a single value operand. Concatenating the token text
/// equals [`ValueRef`](crate::value::ValueRef)'s `Display` — which is implemented
/// by writing these.
pub fn value_tokens(ctx: &Context<'_>, id: ValueId) -> Vec<Token> {
    let mut seg = Seg {
        view: crate::value::ModuleView::new(ctx),
        out: Vec::new(),
        marker: std::marker::PhantomData,
    };
    seg.value(id);
    seg.out
}

/// Provider-generic value rendering used by immutable arena-cluster refs.
pub fn value_tokens_view<'ctx, 'str: 'ctx, R>(view: R, id: ValueId) -> Vec<Token>
where
    R: QCodeView<'ctx, 'str>,
{
    let link = Some(Link::Value(id));
    let shared = view.shared();
    let mut out = Vec::new();
    let mut typed = |type_id, text: String, kind| {
        out.push(Token::new(
            format!("{} ", shared.types.type_name(type_id)),
            TokenKind::Type,
            None,
        ));
        out.push(Token::new(text, kind, link));
    };

    match id {
        ValueId::Instruction(iid) => {
            let insn = view.instruction(iid);
            let atom = insn.name.as_deref().map_or_else(
                || {
                    let local: usize = iid.local.into();
                    format!("%tmp{local:x}")
                },
                |name| format!("%{name}"),
            );
            typed(insn.type_id, atom, TokenKind::Variable);
        }
        ValueId::BlockParam(pid) => {
            let param = view.block_param(pid);
            let atom = param.name.as_deref().map_or_else(
                || {
                    let local: usize = pid.local.into();
                    format!("@param{local:x}")
                },
                |name| format!("@{name}"),
            );
            typed(param.type_id, atom, TokenKind::BlockParam);
        }
        ValueId::Literal(lid) => {
            let literal = &shared.values.literals[lid];
            let atom = literal_atom_view(view, lid);
            typed(literal.type_id, atom, TokenKind::Literal);
        }
        ValueId::Bytes(id) => {
            let value = BytesRef::from_id(shared, id);
            typed(value.type_id(), value.to_string(), TokenKind::Bytes);
        }
        ValueId::Varnode(id) => {
            let value = Varnode::from_id(shared, id);
            out.push(Token::new(
                format!("i{} ", value.size() * 8),
                TokenKind::Type,
                None,
            ));
            out.push(Token::new(value.to_string(), TokenKind::Varnode, link));
        }
        ValueId::Temp(id) => {
            let value = view.temp_ref(id);
            out.push(Token::new(
                format!("i{} ", value.size() * 8),
                TokenKind::Type,
                None,
            ));
            out.push(Token::new(value.to_string(), TokenKind::Varnode, link));
        }
        ValueId::Function(id) => out.push(Token::new(
            format!("<{}>", view.interface(id).name),
            TokenKind::Function,
            Some(Link::Function(id)),
        )),
        ValueId::BasicBlock(id) => out.push(Token::new(
            view.block_ref(id).to_string(),
            TokenKind::Label,
            Some(Link::Block(id)),
        )),
        ValueId::Poison(id) => {
            typed(
                shared.values.poisons[id].type_id,
                "poison".to_string(),
                TokenKind::Literal,
            );
        }
    }
    out
}

/// The rendered *atom* (no `<ty>` prefix) of the literal `id`, resolved against
/// the whole `&Context` so symbolic block/function literals show their target
/// name. This is the full-context twin of `LiteralRef`'s `Display` (which, being
/// `&Shared`-backed, cannot reach body/interface names and falls back to the
/// numeric form). Used by the instruction renderer and the dataflow graph.
/// Concatenating with the type prefix reproduces the pre-narrowing rendering
/// byte-for-byte (context-split stage 5b-ii item #1).
pub fn literal_atom(ctx: &Context<'_>, id: LiteralId) -> String {
    let literal = &ctx.shared.values.literals[id];
    match &literal.symbolic {
        Some(SymbolicRef::Block(bid)) => match BasicBlock::from_id(ctx, *bid).name() {
            Some(name) => format!("&<{}>", name),
            None => format!("&<0x{:x}>", literal.value),
        },
        Some(SymbolicRef::Function(fid)) => {
            format!("&<{}>", FunctionBody::from_id(ctx, *fid).name())
        }
        Some(SymbolicRef::String(s)) => format!("&{:?}", s),
        None if ctx.shared.types.is_bool(literal.type_id) => {
            (if literal.value != 0 { "true" } else { "false" }).to_string()
        }
        None => format!("0x{:x}", literal.value),
    }
}

fn literal_atom_view<'ctx, 'str: 'ctx>(view: impl QCodeView<'ctx, 'str>, id: LiteralId) -> String {
    let shared = view.shared();
    let literal = &shared.values.literals[id];
    match &literal.symbolic {
        // A symbolic block-ref into *another* function (a transient during
        // discovery/jump-table recovery) cannot be name-resolved through a
        // function-scoped `BodyView` — reading the foreign body trips the locality
        // guard — so fall back to the numeric form. A whole-module view (`owner()
        // == None`) resolves the name normally.
        Some(SymbolicRef::Block(id)) if view.owner().is_some_and(|o| o != id.func) => {
            format!("&<0x{:x}>", literal.value)
        }
        Some(SymbolicRef::Block(id)) => view.block_ref(*id).name().map_or_else(
            || format!("&<0x{:x}>", literal.value),
            |name| format!("&<{name}>"),
        ),
        Some(SymbolicRef::Function(id)) => format!("&<{}>", view.interface(*id).name),
        Some(SymbolicRef::String(value)) => format!("&{value:?}"),
        None if shared.types.is_bool(literal.type_id) => {
            (if literal.value != 0 { "true" } else { "false" }).to_string()
        }
        None => format!("0x{:x}", literal.value),
    }
}

/// The token stream for a **shared-leaf** value operand (literal, bytes, varnode),
/// rendered from only the module's [`Shared`] IR state. The `&Shared` twin of
/// [`value_tokens`] for the operands a `&Shared`-backed [`ValueRef`](crate::value::ValueRef) can hold;
/// symbolic block/function literals fall back to the numeric form (their names
/// live in bodies/interfaces, out of a `&Shared`'s reach). Panics on
/// arena-cluster ids, which a shared-leaf ref never carries.
pub fn value_tokens_shared(shared: &Shared<'_>, id: ValueId) -> Vec<Token> {
    let link = Some(Link::Value(id));
    let mut out = Vec::new();
    match id {
        ValueId::Literal(lid) => {
            let r = LiteralRef::from_id(shared, lid);
            out.push(Token::new(
                format!("{} ", shared.types.type_name(r.type_id())),
                TokenKind::Type,
                None,
            ));
            out.push(Token::new(r.to_string(), TokenKind::Literal, link));
        }
        ValueId::Bytes(bid) => {
            let r = BytesRef::from_id(shared, bid);
            out.push(Token::new(
                format!("{} ", shared.types.type_name(r.type_id())),
                TokenKind::Type,
                None,
            ));
            out.push(Token::new(r.to_string(), TokenKind::Bytes, link));
        }
        ValueId::Varnode(vid) => {
            let r = Varnode::from_id(shared, vid);
            out.push(Token::new(
                format!("i{} ", r.size() * 8),
                TokenKind::Type,
                None,
            ));
            out.push(Token::new(r.to_string(), TokenKind::Varnode, link));
        }
        ValueId::Poison(pid) => {
            let ty = shared.values.poisons[pid].type_id;
            out.push(Token::new(
                format!("{} ", shared.types.type_name(ty)),
                TokenKind::Type,
                None,
            ));
            out.push(Token::new("poison".to_string(), TokenKind::Literal, link));
        }
        _ => panic!("value_tokens_shared: not a shared-leaf value id"),
    }
    out
}