wazabin-qcode 0.1.1

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
use crate::value::{
    LocalValueId,
    function::FunctionId,
    insn::{
        Apply, Assert, BadInsn, Binary, Branch, BranchInd, CBranch, Call, CallInd, Carry, Extract,
        FloatToFloat, FloatToInt, Gep, IntToFloat, IntrinsicApp, IsFloatNaN, Load, LzCount, Map,
        PCodeOp, PopCount, Range, Return, ReturnValue, SBorrow, SCarry, Scan, Sext, Store, Switch,
        TailCall, Tuple, Unary, Zext,
    },
};
use smallvec::SmallVec;

/// Operand list returned by [`MnemonicKind::args`]. Inline-stores up to two
/// operands (covering every fixed-arity instruction — binops, casts, loads,
/// flags, …), so the pervasive per-instruction operand walks in the analysis
/// passes don't heap-allocate. Variable-arity ops (calls, tuples, `scan`) spill
/// to the heap only when they exceed two operands.
pub type Args = SmallVec<[LocalValueId; 2]>;

/// Implemented by each concrete instruction type.
///
/// Provides the common interface that [`Mnemonic`] dispatches to: a short
/// opcode string, argument enumeration, and terminator status.
pub trait MnemonicKind {
    /// Short textual opcode, e.g. `"load"`, `"int_add"`, `"branch"`.
    fn opcode(&self) -> &'static str;

    // TODO: replace with a visitor pattern to avoid the need for this method
    /// Returns the [`LocalValueId`]s of all operands consumed by this instruction.
    fn args(&self) -> Args;

    /// Returns `true` if this instruction ends a basic block.
    ///
    /// Terminators are: [`Branch`], [`CBranch`], [`BranchInd`], [`Switch`], [`Call`],
    /// [`CallInd`], and [`Return`].
    fn is_terminator(&self) -> bool {
        false
    }
}

/// The operation performed by an [`Instruction`](crate::value::Instruction).
///
/// `Mnemonic` is a closed enum over all supported IR operations.  It is
/// `#[non_exhaustive]` so that new operations can be added without requiring
/// downstream crates to update exhaustive match arms.
///
/// # Categories
///
/// | Variants | Category |
/// |---|---|
/// | [`Load`], [`Store`] | Memory access |
/// | [`Branch`], [`CBranch`], [`BranchInd`], [`Switch`], [`Call`], [`CallInd`], [`Return`], [`ReturnValue`], [`BadInsn`] | Control flow (terminators) |
/// | [`Unop`](Mnemonic::Unop) | Unary integer/float/bool operations |
/// | [`Binop`](Mnemonic::Binop) | Binary integer/float/bool operations |
/// | [`Zext`], [`Sext`], [`Range`], [`IntToFloat`], [`FloatToInt`], [`FloatToFloat`] | Type casts and bit extraction |
/// | [`IsFloatNaN`], [`PopCount`], [`LzCount`], [`Carry`], [`SCarry`], [`SBorrow`] | Bit/flag operations |
/// | [`PCodeOp`] | User-defined or architecture-specific operation |
/// | [`Intrinsic`](crate::value::insn::intrinsic::Intrinsic) | Pure named intrinsic function (e.g. `rol`, `ror`) |
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
pub enum Mnemonic {
    /// Load a value from a memory space.
    Load(Load),
    /// Store a value to a memory space.
    Store(Store),
    /// Unconditional direct branch to a static target block.
    Branch(Branch),
    /// Conditional branch: taken when the condition operand is non-zero.
    CBranch(CBranch),
    /// Unconditional indirect branch to a dynamically-computed address.
    BranchInd(BranchInd),
    /// Multi-way dispatch on an integer scrutinee — a resolved jump table.
    Switch(Switch),
    /// Direct call to a known function.
    Call(Call),
    /// Tail call: a function-level transfer of control to another function's
    /// entry (thunk / tail jump). Carries a [`FunctionId`], never a foreign
    /// block — see [`TailCall`].
    TailCall(TailCall),
    /// Value-level application of a pure lambda function.
    Apply(Apply),
    /// Indirect call through a computed function pointer.
    CallInd(CallInd),
    /// Return from the current function.
    Return(Return),
    /// Value return from a lambda function.
    ReturnValue(ReturnValue),
    /// Bytes that do not decode to a valid instruction. A terminator with no
    /// successors (see [`BadInsn`]).
    BadInsn(BadInsn),
    /// A unary integer, float, or boolean operation.
    Unop(Unary),
    /// A binary integer, float, or boolean operation.
    Binop(Binary),
    /// Extract a contiguous byte range from a value.
    Range(Range),
    /// Convert an integer to a floating-point value.
    IntToFloat(IntToFloat),
    /// Convert a floating-point value to a different float width.
    FloatToFloat(FloatToFloat),
    /// Convert a floating-point value to an integer (truncate toward zero).
    FloatToInt(FloatToInt),
    /// Zero-extend a value to a wider integer.
    Zext(Zext),
    /// Sign-extend a value to a wider integer.
    Sext(Sext),
    /// Test whether a floating-point value is NaN.
    IsFloatNaN(IsFloatNaN),
    /// Count the number of set bits (population count / Hamming weight).
    PopCount(PopCount),
    /// Count leading zero bits.
    LzCount(LzCount),
    /// Unsigned addition carry-out flag.
    Carry(Carry),
    /// Signed addition carry-out (overflow) flag.
    SCarry(SCarry),
    /// Signed subtraction borrow flag.
    SBorrow(SBorrow),
    /// Assert when resolving an execution trace
    Assert(Assert),
    /// A user-defined or architecture-specific p-code operation.
    PCodeOp(PCodeOp),
    /// A pure named intrinsic function (e.g. `rol`, `ror`). Categorically pure:
    /// no memory or observable side effects.
    Intrinsic(IntrinsicApp),
    /// Build an aggregate (tuple) value from ordered fields.
    Tuple(Tuple),
    /// Project a single field out of an aggregate value.
    Extract(Extract),
    /// Compute the address of a struct field (typed, named pointer arithmetic).
    Gep(Gep),
    /// Total element-wise map over an array value (a projectable loop).
    Map(Map),
    /// Total left-scan (prefix fold) over an array value: a projectable loop
    /// whose per-element write depends on the previous iteration's result.
    Scan(Scan),
}

impl Mnemonic {
    /// The unresolved direct-callee slot carried by this mnemonic, if any.
    pub fn minted_callee_slot(&self) -> Option<u32> {
        match self {
            Self::Call(call) => call.target.minted(),
            Self::TailCall(call) => call.target.minted(),
            Self::Apply(apply) => apply.target.minted(),
            Self::Map(map) => map.body.minted(),
            Self::Scan(scan) => scan.body.minted(),
            _ => None,
        }
    }

    /// Resolve one pass-local direct-callee placeholder to its installed
    /// function ID. Returns whether this mnemonic contained that placeholder.
    pub fn resolve_minted_callee(&mut self, slot: u32, real: FunctionId) -> bool {
        let callee = match self {
            Self::Call(call) => Some(&mut call.target),
            Self::TailCall(call) => Some(&mut call.target),
            Self::Apply(apply) => Some(&mut apply.target),
            Self::Map(map) => Some(&mut map.body),
            Self::Scan(scan) => Some(&mut scan.body),
            _ => None,
        };
        let Some(callee) = callee else {
            return false;
        };
        if *callee != super::Callee::Minted(slot) {
            return false;
        }
        *callee = super::Callee::Real(real);
        true
    }

    fn as_kind(&self) -> &dyn MnemonicKind {
        match self {
            Mnemonic::Load(m) => m,
            Mnemonic::Store(m) => m,
            Mnemonic::Branch(m) => m,
            Mnemonic::CBranch(m) => m,
            Mnemonic::BranchInd(m) => m,
            Mnemonic::Switch(m) => m,
            Mnemonic::Call(m) => m,
            Mnemonic::TailCall(m) => m,
            Mnemonic::Apply(m) => m,
            Mnemonic::CallInd(m) => m,
            Mnemonic::Return(m) => m,
            Mnemonic::ReturnValue(m) => m,
            Mnemonic::BadInsn(m) => m,
            Mnemonic::Range(m) => m,
            Mnemonic::Unop(m) => m,
            Mnemonic::Binop(m) => m,
            Mnemonic::IsFloatNaN(m) => m,
            Mnemonic::IntToFloat(m) => m,
            Mnemonic::FloatToFloat(m) => m,
            Mnemonic::FloatToInt(m) => m,
            Mnemonic::Zext(m) => m,
            Mnemonic::Sext(m) => m,
            Mnemonic::PopCount(m) => m,
            Mnemonic::LzCount(m) => m,
            Mnemonic::Carry(m) => m,
            Mnemonic::SCarry(m) => m,
            Mnemonic::SBorrow(m) => m,
            Mnemonic::Assert(m) => m,
            Mnemonic::PCodeOp(m) => m,
            Mnemonic::Intrinsic(m) => m,
            Mnemonic::Tuple(m) => m,
            Mnemonic::Extract(m) => m,
            Mnemonic::Gep(m) => m,
            Mnemonic::Map(m) => m,
            Mnemonic::Scan(m) => m,
        }
    }

    pub fn opcode(&self) -> &'static str {
        self.as_kind().opcode()
    }

    pub fn is_terminator(&self) -> bool {
        self.as_kind().is_terminator()
    }

    /// Whether an instruction must be kept even if its result has no users:
    /// it writes memory, transfers control, calls, asserts, or invokes an
    /// opaque p-code op. This is the single source of truth shared by DCE
    /// (which must not delete these) and the emitter (which must always print
    /// them); keep the two in agreement by routing both through here.
    pub fn has_side_effects(&self) -> bool {
        matches!(
            self,
            Mnemonic::Store(_)
                | Mnemonic::Call(_)
                | Mnemonic::CallInd(_)
                | Mnemonic::PCodeOp(_)
                | Mnemonic::Assert(_)
        ) || self.is_terminator()
    }

    /// The callee of a direct [`Call`] or body of a [`Map`], or `None`
    /// (including indirect [`CallInd`] calls, whose target is not statically
    /// known). Used to maintain the reverse call graph.
    pub fn call_target(&self) -> Option<FunctionId> {
        match self {
            Mnemonic::Call(call) => call.target.real(),
            Mnemonic::TailCall(tc) => tc.target.real(),
            Mnemonic::Apply(apply) => apply.target.real(),
            Mnemonic::Map(map) => map.body.real(),
            Mnemonic::Scan(scan) => scan.body.real(),
            _ => None,
        }
    }

    /// The statically-known CFG target blocks this mnemonic branches to — the
    /// `Branch` target and both `CBranch` arms — as bare body-local indices. Empty
    /// for non-branch or indirect terminators (a `BranchInd` resolves to computed
    /// addresses, not a static block). Strict IR locality (context-split ruling 2)
    /// guarantees each target lives in this terminator's own arena, so a caller
    /// with the owning function in hand recovers the full `BlockId` via
    /// `BlockId::new(func, local)`.
    pub fn target_blocks(&self) -> smallvec::SmallVec<[crate::value::LocalBlockId; 2]> {
        match self {
            Mnemonic::Branch(b) => smallvec::smallvec![b.target],
            Mnemonic::CBranch(c) => smallvec::smallvec![c.success_block, c.failure_block],
            // Every arm plus the default, if it has one: a resolved dispatch
            // knows all of its successors statically.
            Mnemonic::Switch(s) => s
                .cases
                .iter()
                .map(|case| case.target)
                .chain(s.default)
                .collect(),
            _ => smallvec::SmallVec::new(),
        }
    }

    pub fn args(&self) -> Args {
        self.as_kind().args()
    }

    /// Replace every occurrence of `old` with `new` in this instruction's operands.
    pub fn replace_value(&mut self, old: LocalValueId, new: LocalValueId) {
        match self {
            Mnemonic::Load(m) => {
                if m.ptr == old {
                    m.ptr = new;
                }
            }
            Mnemonic::Store(m) => {
                if m.ptr == old {
                    m.ptr = new;
                }
                if m.src == old {
                    m.src = new;
                }
            }
            Mnemonic::CBranch(m) => {
                if m.condition == old {
                    m.condition = new;
                }
                m.success_args.iter_mut().for_each(|a| {
                    if *a == old {
                        *a = new;
                    }
                });
                m.failure_args.iter_mut().for_each(|a| {
                    if *a == old {
                        *a = new;
                    }
                });
            }
            Mnemonic::BranchInd(m) => {
                if m.ptr == old {
                    m.ptr = new;
                }
            }
            Mnemonic::Switch(m) => {
                if m.scrutinee == old {
                    m.scrutinee = new;
                }
                for case in m.cases.iter_mut() {
                    case.args.iter_mut().for_each(|a| {
                        if *a == old {
                            *a = new;
                        }
                    });
                }
                m.default_args.iter_mut().for_each(|a| {
                    if *a == old {
                        *a = new;
                    }
                });
            }
            Mnemonic::Call(m) => {
                m.args.iter_mut().for_each(|a| {
                    if *a == old {
                        *a = new;
                    }
                });
            }
            Mnemonic::TailCall(m) => {
                m.args.iter_mut().for_each(|a| {
                    if *a == old {
                        *a = new;
                    }
                });
            }
            Mnemonic::Apply(m) => {
                m.args.iter_mut().for_each(|a| {
                    if *a == old {
                        *a = new;
                    }
                });
            }
            Mnemonic::CallInd(m) => {
                if m.ptr == old {
                    m.ptr = new;
                }
                m.args.iter_mut().for_each(|a| {
                    if *a == old {
                        *a = new;
                    }
                });
            }
            Mnemonic::Return(m) => {
                if m.ptr == old {
                    m.ptr = new;
                }
                if let Some(v) = m.value.as_mut()
                    && *v == old
                {
                    *v = new;
                }
            }
            Mnemonic::ReturnValue(m) => {
                if m.value == old {
                    m.value = new;
                }
            }
            // No operands to rewrite.
            Mnemonic::BadInsn(_) => {}
            Mnemonic::Unop(m) => {
                if m.src == old {
                    m.src = new;
                }
            }
            Mnemonic::Binop(m) => {
                if m.lhs == old {
                    m.lhs = new;
                }
                if m.rhs == old {
                    m.rhs = new;
                }
            }
            Mnemonic::Range(m) => {
                if m.src == old {
                    m.src = new;
                }
            }
            Mnemonic::Zext(m) => {
                if m.src == old {
                    m.src = new;
                }
            }
            Mnemonic::Sext(m) => {
                if m.src == old {
                    m.src = new;
                }
            }
            Mnemonic::IntToFloat(m) => {
                if m.src == old {
                    m.src = new;
                }
            }
            Mnemonic::FloatToFloat(m) => {
                if m.src == old {
                    m.src = new;
                }
            }
            Mnemonic::FloatToInt(m) => {
                if m.src == old {
                    m.src = new;
                }
            }
            Mnemonic::IsFloatNaN(m) => {
                if m.src == old {
                    m.src = new;
                }
            }
            Mnemonic::PopCount(m) => {
                if m.src == old {
                    m.src = new;
                }
            }
            Mnemonic::LzCount(m) => {
                if m.src == old {
                    m.src = new;
                }
            }
            Mnemonic::Carry(m) => {
                if m.lhs == old {
                    m.lhs = new;
                }
                if m.rhs == old {
                    m.rhs = new;
                }
            }
            Mnemonic::SCarry(m) => {
                if m.lhs == old {
                    m.lhs = new;
                }
                if m.rhs == old {
                    m.rhs = new;
                }
            }
            Mnemonic::SBorrow(m) => {
                if m.lhs == old {
                    m.lhs = new;
                }
                if m.rhs == old {
                    m.rhs = new;
                }
            }
            Mnemonic::PCodeOp(m) => {
                m.args.iter_mut().for_each(|a| {
                    if *a == old {
                        *a = new;
                    }
                });
                if let Some(v) = m.dst.as_mut()
                    && *v == old
                {
                    *v = new;
                }
            }
            Mnemonic::Branch(m) => {
                m.args.iter_mut().for_each(|a| {
                    if *a == old {
                        *a = new;
                    }
                });
            }
            Mnemonic::Intrinsic(m) => {
                m.args.iter_mut().for_each(|a| {
                    if *a == old {
                        *a = new;
                    }
                });
            }
            Mnemonic::Tuple(m) => {
                m.fields.iter_mut().for_each(|a| {
                    if *a == old {
                        *a = new;
                    }
                });
            }
            Mnemonic::Assert(m) => {
                if m.condition == old {
                    m.condition = new;
                }
            }
            Mnemonic::Extract(m) => {
                if m.agg == old {
                    m.agg = new;
                }
            }
            Mnemonic::Gep(m) => {
                if m.base == old {
                    m.base = new;
                }
            }
            Mnemonic::Map(m) => {
                // `body` is a function symbol, not a value operand — left intact.
                if m.src == old {
                    m.src = new;
                }
                m.captures.iter_mut().for_each(|a| {
                    if *a == old {
                        *a = new;
                    }
                });
            }
            Mnemonic::Scan(m) => {
                // `body` is a function symbol, not a value operand — left intact.
                if m.init == old {
                    m.init = new;
                }
                if m.src == old {
                    m.src = new;
                }
                m.captures.iter_mut().for_each(|a| {
                    if *a == old {
                        *a = new;
                    }
                });
            }
        }
    }
}