Skip to main content

ddc_dec/
lift.rs

1//! Register-machine expression lifting: Dalvik instructions → jdc-core IR.
2//!
3//! The register file is modeled as per-register *views* of values:
4//! * `Live(v)` — the register equals local `v`, whose defining statement was
5//!   already emitted (SSA-ish: every materialization allocates a fresh var,
6//!   so a `Local` reference can never go stale);
7//! * `Pending(e)` — the register holds the expression `e`, whose store is
8//!   deferred so consumers can nest it (`return a + b`);
9//! * `PendingCall(e)` — the value of a just-executed invoke /
10//!   filled-new-array whose statement has not been emitted yet: consumed
11//!   exactly once (a materialization, a term, or a discarded-value
12//!   statement), never duplicated;
13//! * `WideHi` — the high half of a J/D pair.
14//!
15//! At block exit the terminator is built FIRST (terms may carry pending
16//! call expressions: `return foo();`), then unconsumed calls become
17//! statements and impure pendings materialize, so cross-block states only
18//! carry pure values, locals, and open `new`/`new-array` views.
19
20use ddc_dex::insn::{ArithOp, CmpKind, CmpOp, Insn, InsnKind, InvokeKind, Payload};
21use ddc_dex::{CodeItem, DexFile};
22use jdc_core::ir::build::{has_side_effects, BlockResult, SwitchTargets, Term};
23use jdc_core::ir::expr::{AssignOp, BinOp, ConcatPart, ConstVal, Expr, TypeRef, UnOp};
24use jdc_core::ir::stmt::Stmt;
25use jdc_core::types::{JavaType, MethodDescriptor};
26use jdc_core::var::{VarInfo, VarTable};
27
28use crate::DexPool;
29
30pub type BResult<T> = Result<T, String>;
31
32/// Cheap per-method feature flags accumulated by the lifter: which
33/// post-lift passes could possibly match anything. Most methods use none of
34/// these — gating their passes skips full-tree analysis walks.
35#[derive(Clone, Copy, Default, PartialEq)]
36pub struct MethodFlags(u8);
37
38impl MethodFlags {
39    const SB: u8 = 1;
40    const MONITOR: u8 = 2;
41    const CMP: u8 = 4;
42    pub fn none() -> MethodFlags {
43        MethodFlags(0)
44    }
45    pub fn with_sb(self) -> MethodFlags {
46        MethodFlags(self.0 | Self::SB)
47    }
48    pub fn with_monitor(self) -> MethodFlags {
49        MethodFlags(self.0 | Self::MONITOR)
50    }
51    pub fn with_cmp(self) -> MethodFlags {
52        MethodFlags(self.0 | Self::CMP)
53    }
54    pub fn has_sb(self) -> bool {
55        self.0 & Self::SB != 0
56    }
57    pub fn has_monitor(self) -> bool {
58        self.0 & Self::MONITOR != 0
59    }
60    pub fn has_cmp(self) -> bool {
61        self.0 & Self::CMP != 0
62    }
63    pub fn merge(self, o: MethodFlags) -> MethodFlags {
64        MethodFlags(self.0 | o.0)
65    }
66}
67
68/// Everything one method body needs from its DEX image.
69pub struct MethodEnv<'a> {
70    pub pool: &'a DexPool,
71    /// Image index of `dex` in the pool (keys the pool's ref caches).
72    pub di: usize,
73    pub dex: &'a DexFile,
74    pub code: &'a CodeItem,
75    pub class_name: String,
76    pub method_name: std::sync::Arc<str>,
77    pub desc: MethodDescriptor,
78    /// Debug-info local table (empty for release/stripped builds):
79    /// real source names per (register, pc-range).
80    pub debug_locals: Vec<ddc_dex::DebugLocal>,
81    pub is_static: bool,
82    /// Total code units — the CFG owns the insns stream, so the lifter reads
83    /// the extent from here instead of `code.insns` (hollowed after the
84    /// CFG build moved it).
85    pub code_units: u32,
86}
87
88impl<'a> MethodEnv<'a> {
89    pub fn type_name(&self, idx: u32) -> String {
90        self.dex.type_name(idx).to_string()
91    }
92    pub fn java_type(&self, idx: u32) -> JavaType {
93        // Shared per-image parse (was desc-reparse per instruction).
94        (*self.pool.type_java(self.di, idx)).clone()
95    }
96    pub fn type_name_arc(&self, idx: u32) -> std::sync::Arc<str> {
97        self.pool.type_name_arc(self.di, idx)
98    }
99    pub fn field_ref(
100        &self,
101        idx: u32,
102    ) -> (
103        std::sync::Arc<str>,
104        std::sync::Arc<str>,
105        JavaType,
106    ) {
107        let f = self.dex.field(idx);
108        let owner = std::sync::Arc::from(self.dex.class_name(f.class_idx).as_str());
109        let raw = self.dex.string(f.name_idx);
110        // Colliding obfuscated field names (synthetic outer ref renamed
111        // to a one-char name beside a real field) resolve differently per
112        // site in source — run the shared rename registry so every
113        // reference prints the same display name as the declaration.
114        let name = jdc_core::rename::field_display(&owner, raw, self.dex.type_name(f.type_idx))
115            .map(std::sync::Arc::from)
116            .unwrap_or_else(|| std::sync::Arc::from(raw));
117        (owner, name, self.java_type(f.type_idx))
118    }
119    pub fn method_ref(
120        &self,
121        idx: u32,
122    ) -> (
123        std::sync::Arc<str>,
124        std::sync::Arc<str>,
125        std::sync::Arc<MethodDescriptor>,
126    ) {
127        // Was: N param Strings + format! + descriptor RE-PARSE per invoke
128        // (6-10 allocations per call instruction). All three components
129        // are shared table entries now — refcount bumps.
130        let m = self.dex.method(idx);
131        let owner = std::sync::Arc::from(self.dex.class_name(m.class_idx).as_str());
132        let desc = self.pool.proto_desc_parsed(self.di, m.proto_idx);
133        // Sanitizer-collapsed method names (whole `ERROR_中文` families on
134        // weibo render one `ERROR________`) need the shared member-rename
135        // registry so declarations and call sites agree. Cold path only:
136        // clean corpora never rebuild the descriptor string.
137        let name = if jdc_core::rename::member_rename_active() {
138            let raw = self.dex.string(m.name_idx);
139            jdc_core::rename::field_display(&owner, raw, &desc.to_string())
140                .map(std::sync::Arc::from)
141                .unwrap_or_else(|| std::sync::Arc::from(raw))
142        } else {
143            std::sync::Arc::from(self.dex.string(m.name_idx))
144        };
145        (owner, name, desc)
146    }
147}
148
149/// Per-register value view (see module docs).
150#[derive(Debug, Clone, PartialEq)]
151pub enum Reg {
152    Undef,
153    Live(u32),
154    Pending(Expr),
155    PendingCall(Expr),
156    WideHi,
157}
158
159/// Block-exit register state (machine-neutral view plus origin tracking).
160#[derive(Debug, Clone)]
161pub struct OutState {
162    pub regs: Vec<Reg>,
163    /// Per register: the pc of the instruction that produced its value
164    /// (orders merge materializations by original instruction order).
165    pub write_pc: Vec<u32>,
166}
167
168/// The registers an instruction READS as operands (receivers, arguments,
169/// move sources — destination registers excluded). Drives the block
170/// lookahead that decides when an allocation view must materialize.
171fn src_regs(kind: &InsnKind) -> Vec<u16> {
172    let mut out: Vec<u16> = Vec::with_capacity(4);
173    let mut push = |r: u16| out.push(r);
174    match kind {
175        InsnKind::Nop
176        | InsnKind::ReturnVoid
177        | InsnKind::Unknown
178        | InsnKind::NewInstance { .. }
179        | InsnKind::SGet { .. } => {}
180        InsnKind::Move { src, .. }
181        | InsnKind::Un { src, .. }
182        | InsnKind::Return { src }
183        | InsnKind::ArrayLength { src, .. } => push(*src),
184        InsnKind::MonitorEnter { reg }
185        | InsnKind::MonitorExit { reg }
186        | InsnKind::Throw { reg }
187        | InsnKind::CheckCast { reg, .. }
188        | InsnKind::FillArrayData { reg, .. }
189        | InsnKind::PackedSwitch { reg, .. }
190        | InsnKind::SparseSwitch { reg, .. } => push(*reg),
191        InsnKind::MoveResult { .. } | InsnKind::MoveException { .. } => {}
192        InsnKind::Const { .. }
193        | InsnKind::ConstClass { .. }
194        | InsnKind::ConstString { .. }
195        | InsnKind::ConstMethodHandle { .. }
196        | InsnKind::ConstMethodType { .. } => {}
197        InsnKind::InstanceOf { src, .. } => {
198            push(*src);
199        }
200        InsnKind::NewArray { size, .. } => {
201            push(*size);
202        }
203        InsnKind::FilledNewArray { regs, .. } | InsnKind::InvokeCustom { regs, .. } => {
204            for r in regs {
205                push(*r);
206            }
207        }
208        InsnKind::Goto { .. } => {}
209        InsnKind::Cmp { a, b, .. } | InsnKind::Bin { a, b, .. } => {
210            push(*a);
211            push(*b);
212        }
213        InsnKind::BinLit { a, .. } => push(*a),
214        InsnKind::If { a, b, z, .. } => {
215            push(*a);
216            if !*z {
217                push(*b);
218            }
219        }
220        InsnKind::AGet { array, index, .. } => {
221            push(*array);
222            push(*index);
223        }
224        InsnKind::APut {
225            value,
226            array,
227            index,
228            ..
229        } => {
230            push(*value);
231            push(*array);
232            push(*index);
233        }
234        InsnKind::IGet { obj, .. } => {
235            push(*obj);
236        }
237        InsnKind::IPut { value, obj, .. } => {
238            push(*value);
239            push(*obj);
240        }
241        InsnKind::SPut { value, .. } => {
242            push(*value);
243        }
244        InsnKind::Invoke { regs, .. } => {
245            for r in regs {
246                push(*r);
247            }
248        }
249    }
250    out
251}
252
253/// Registers an instruction READS as operands.
254/// Registers an instruction DEFINES (new value generations).
255fn dst_regs(kind: &InsnKind) -> Vec<u16> {
256    let mut out: Vec<u16> = Vec::with_capacity(2);
257    let mut push = |r: u16| out.push(r);
258    match kind {
259        InsnKind::Move { dst, .. }
260        | InsnKind::MoveResult { dst }
261        | InsnKind::MoveException { dst }
262        | InsnKind::Const { dst, .. }
263        | InsnKind::ConstClass { dst, .. }
264        | InsnKind::ConstString { dst, .. }
265        | InsnKind::ConstMethodHandle { dst, .. }
266        | InsnKind::ConstMethodType { dst, .. }
267        | InsnKind::NewInstance { dst, .. }
268        | InsnKind::NewArray { dst, .. }
269        | InsnKind::InstanceOf { dst, .. }
270        | InsnKind::ArrayLength { dst, .. }
271        | InsnKind::Cmp { dst, .. }
272        | InsnKind::AGet { dst, .. }
273        | InsnKind::IGet { dst, .. }
274        | InsnKind::SGet { dst, .. }
275        | InsnKind::Un { dst, .. }
276        | InsnKind::Bin { dst, .. }
277        | InsnKind::BinLit { dst, .. } => push(*dst),
278        _ => {}
279    }
280    out
281}
282
283/// The (pc, reg) reads that are the final read of the register's current
284/// generation: walking the register's events in pc order, a read is
285/// final when no other read of the same register precedes its next WRITE
286/// (a write starts a new generation — the old value is dead there).
287fn compute_final_reads(ins: &[Insn]) -> jdc_core::FxHashSet<(u32, u16)> {
288    use jdc_core::FxHashMap as HashMap;
289    // reg → (pc, is_read) events, pc order (ins is already pc-sorted).
290    let mut events: HashMap<u16, Vec<(u32, bool)>> = HashMap::default();
291    for i in ins {
292        for r in src_regs(&i.kind) {
293            events.entry(r).or_default().push((i.pc, true));
294        }
295        for r in dst_regs(&i.kind) {
296            events.entry(r).or_default().push((i.pc, false));
297        }
298    }
299    let mut out = jdc_core::FxHashSet::default();
300    for (reg, evs) in events {
301        let mut dedup: Vec<(u32, bool)> = Vec::with_capacity(evs.len());
302        for (pc, is_read) in evs {
303            if dedup.last() != Some(&(pc, is_read)) {
304                dedup.push((pc, is_read));
305            }
306        }
307        for (i, &(pc, is_read)) in dedup.iter().enumerate() {
308            if !is_read {
309                continue;
310            }
311            // Final read of the current generation: no other read before
312            // the register's next write.
313            let mut final_ = true;
314            if let Some(&(_, is_read2)) = dedup.get(i + 1) {
315                if is_read2 {
316                    final_ = false;
317                }
318            }
319            if final_ {
320                out.insert((pc, reg));
321            }
322        }
323    }
324    out
325}
326
327pub struct Lifter<'a> {
328    pub env: &'a MethodEnv<'a>,
329    pub vt: &'a mut VarTable,
330    /// Owning block id (stable-var registry key).
331    block_id: usize,
332    /// (block, register) → var id, shared across REBUILDS: a re-lift of
333    /// the same block with the same input reuses the same materialized
334    /// variables, so the output state is id-stable. Without this, every
335    /// rebuild allocated fresh ids, the out-state compared unequal, and
336    /// the worklist cascaded re-queuing until the visit cap (~60
337    /// rebuilds per block, 30M lifts on weibo).
338    stable: &'a mut jdc_core::FxHashMap<(usize, u16, u64), u32>,
339    regs: Vec<Reg>,
340    write_pc: Vec<u32>,
341    stmts: Vec<Stmt>,
342    pending_call: Option<Expr>,
343    code_units: u32,
344    /// Current instruction's pc (drives the allocation materialization
345    /// lookahead below).
346    cur_pc: u32,
347    /// (pc, register) reads that are the FINAL read of the register's
348    /// current value generation: no other read before the register's
349    /// next write. An allocation view (post-fold `New` / `NewArray`)
350    /// may only be inlined at a final read; a register reused for a
351    /// call result and read again (greet → move-result v0 → println(v0))
352    /// is a new generation, not another use of the allocation.
353    final_read: jdc_core::FxHashSet<(u32, u16)>,
354    /// Method-level feature flags, merged in place as features are seen
355    /// (build_block consumes the lifter, so the flags must escape via a
356    /// shared reference rather than a field read afterwards).
357    mflags: &'a mut MethodFlags,
358}
359
360impl<'a> Lifter<'a> {
361    pub fn new(
362        env: &'a MethodEnv<'a>,
363        vt: &'a mut VarTable,
364        in_regs: Vec<Reg>,
365        block_id: usize,
366        stable: &'a mut jdc_core::FxHashMap<(usize, u16, u64), u32>,
367        mflags: &'a mut MethodFlags,
368    ) -> Lifter<'a> {
369        let n = env.code.registers_size as usize;
370        let n = n.max(in_regs.len()).max(1);
371        let mut regs = in_regs;
372        regs.resize(n, Reg::Undef);
373        Lifter {
374            env,
375            vt,
376            regs,
377            write_pc: vec![0; n],
378            stmts: Vec::new(),
379            block_id,
380            stable,
381            pending_call: None,
382            code_units: env.code_units,
383            cur_pc: 0,
384            final_read: jdc_core::FxHashSet::default(),
385            mflags,
386        }
387    }
388
389    // -- variable creation ---------------------------------------------------
390
391    /// Cheap 64-bit type fingerprint for the stable-var key: hashing the
392    /// JavaType itself (string bytes + tuple) ran on EVERY register
393    /// materialization; the fingerprint is one pass with no clones
394    /// (`erased()` deep-clones Array chains). G is unreachable in DEX
395    /// lift (no generic signatures) — a coarse tag suffices.
396    fn ty_key(ty: &TypeRef) -> u64 {
397        use std::hash::{Hash, Hasher};
398        fn jt_key(j: &JavaType, h: &mut jdc_core::fx::FxHasher) {
399            match j {
400                JavaType::Void => 1u64.hash(h),
401                JavaType::Boolean => 2u64.hash(h),
402                JavaType::Byte => 3u64.hash(h),
403                JavaType::Char => 4u64.hash(h),
404                JavaType::Short => 5u64.hash(h),
405                JavaType::Int => 6u64.hash(h),
406                JavaType::Float => 7u64.hash(h),
407                JavaType::Long => 8u64.hash(h),
408                JavaType::Double => 9u64.hash(h),
409                JavaType::Object(n) => {
410                    10u64.hash(h);
411                    n.hash(h);
412                }
413                JavaType::Array(inner) => {
414                    11u64.hash(h);
415                    jt_key(inner, h);
416                }
417            }
418        }
419        let mut h = jdc_core::fx::FxHasher::default();
420        match ty {
421            TypeRef::J(j) => jt_key(j, &mut h),
422            TypeRef::G(_) => 99u64.hash(&mut h),
423        }
424        h.finish()
425    }
426
427    fn fresh_var(&mut self, slot: u16, ty: TypeRef) -> u32 {
428        // Keyed by TYPE as well as (block, slot): d8 reuses one register
429        // for values of different types (`v1: byte[]` then `v1: String`
430        // in the same method — the lab package's Main.run). A Java local
431        // cannot change type — the old (block, slot) key reused one var
432        // and then RETYPED it in place, emitting `byte[] v1 = …; v1 =
433        // Obf.s(v1, 90);` (String into byte[]) and kin all over the
434        // corpus. Distinct types mint distinct vars; rebuild stability
435        // (the worklist-convergence fix) holds because the key is a pure
436        // function of the instruction stream.
437        let key = (self.block_id, slot, Self::ty_key(&ty));
438        if let Some(&id) = self.stable.get(&key) {
439            return id;
440        }
441        // Real source name from the debug-info local table when a range
442        // covers (pc, slot); otherwise the synthetic `vN`. apply_local_
443        // names never renames non-synthetic vars and de-duplicates the
444        // (legal-in-source, illegal-in-flat-Java) same-name scopes.
445        if let Some(name) = self.debug_name(slot, &ty) {
446            // Unify same register+type+name vars across blocks: the debug
447            // table says they are ONE source local, and per-block copies
448            // degrade to `out`/`out2`/`out3` suffix soup after de-dup.
449            // Dalvik registers are method-scoped, so one Java local shared
450            // by every write site is exactly register semantics.
451            let tkey = Self::ty_key(&ty);
452            if let Some(id) = self
453                .vt
454                .vars
455                .iter()
456                .find(|v| {
457                    v.slot == slot
458                        && !v.synthetic_name
459                        && v.name == name
460                        && Self::ty_key(&v.ty) == tkey
461                })
462                .map(|v| v.id)
463            {
464                self.stable.insert(key, id);
465                return id;
466            }
467            let id = self.vt.vars.len() as u32;
468            self.stable.insert(key, id);
469            self.push_var(id, slot, name, ty, false);
470            return id;
471        }
472        let id = self.vt.vars.len() as u32;
473        let name = format!("v{}", id);
474        self.stable.insert(key, id);
475        self.push_var(id, slot, name, ty, true);
476        id
477    }
478
479    fn push_var(&mut self, id: u32, slot: u16, name: String, ty: TypeRef, synthetic: bool) {
480        self.vt.vars.push(VarInfo {
481            id,
482            slot,
483            name,
484            ty,
485            is_param: false,
486            range_start: 0,
487            range_end: u16::MAX,
488            synthetic_name: synthetic,
489        });
490        while self.vt.by_slot.len() <= slot as usize {
491            self.vt.by_slot.push(Vec::new());
492        }
493        self.vt.by_slot[slot as usize].push((0, u16::MAX, id));
494    }
495
496    /// The source-level name covering `(cur_pc, slot)` in the debug
497    /// local table, sanitized into a legal Java identifier. Linear scan:
498    /// the table is empty in release APKs (early-out) and small in
499    /// debug builds.
500    fn debug_name(&self, slot: u16, ty: &TypeRef) -> Option<String> {
501        let locals = &self.env.debug_locals;
502        if locals.is_empty() {
503            return None;
504        }
505        let want = ty.erased();
506        let covers = |pc: u32| {
507            locals.iter().find(|l| {
508                l.reg == slot
509                    && pc >= l.start
510                    && pc < l.end
511                    // Type gate: register reuse mints vars of a DIFFERENT
512                    // type inside the same range (`new-array v0, v0` reads
513                    // the old v0 as an int size inside the new v0's [C
514                    // range) — naming that junk var stole the real name.
515                    && l.ty.as_deref().map(|d| crate::desc_type(d) == want).unwrap_or(true)
516            })
517        };
518        // Materializations at block merges run BEFORE any instruction of
519        // the block (cur_pc is stale/zero) — fall back to the pc where
520        // the register's current value was BORN (write_pc), which is
521        // what the debug range actually describes.
522        let hit = covers(self.cur_pc).or_else(|| {
523            let wpc = *self.write_pc.get(slot as usize)?;
524            if wpc == 0 {
525                None
526            } else {
527                covers(wpc)
528            }
529        })?;
530        let name = crate::classdec::java_ident(&hit.name);
531        if name.is_empty() {
532            return None;
533        }
534        Some(name.into_owned())
535    }
536
537    fn local_expr(&self, v: u32) -> Expr {
538        Expr::Local {
539            var: v,
540            ty: self.vt.var(v).ty.clone(),
541        }
542    }
543
544    // -- register access -----------------------------------------------------
545
546    /// Read for nesting into another expression (clones the value).
547    #[track_caller]
548    fn read_nest(&mut self, r: u16) -> Expr {
549        match self.regs.get(r as usize).cloned().unwrap_or(Reg::Undef) {
550            Reg::Undef | Reg::WideHi => {
551                // Reading a register never written on this path (or beyond
552                // the frame): a fresh (never-assigned) local keeps the
553                // output compilable.
554                let ty = TypeRef::J(JavaType::Int);
555                let v = self.fresh_var(r, ty);
556                if (r as usize) < self.regs.len() {
557                    self.regs[r as usize] = Reg::Live(v);
558                }
559                self.local_expr(v)
560            }
561            Reg::Live(v) => self.local_expr(v),
562            Reg::Pending(e) => {
563                // An allocation view (`NewArray`, or a post-fold `New`)
564                // may be emitted inline only when THIS read is the
565                // register's final one: any later read would re-emission
566                // a FRESH allocation (`sput v0, sparse; aput v2, v0, v1`
567                // printed three distinct `new byte[4]`; a discarded-
568                // result StringBuilder chain printed one `new
569                // StringBuilder()` per append). Materialize when a later
570                // read exists; inline the last use, keeping
571                // `foo(new Bar(...))` nesting. `New { raw: true }` is
572                // always exempt: the constructor fold consumes the raw
573                // view at the invoke-direct site.
574                let alloc = matches!(&e, Expr::NewArray { .. })
575                    || (matches!(&e, Expr::New { raw: false, .. })
576                        && !self.final_read.contains(&(self.cur_pc, r)));
577                if alloc {
578                    let v = self.materialize(r);
579                    self.local_expr(v)
580                } else {
581                    // Inline AND consume: this is the register's final
582                    // read in the block, yet leaving the Pending view in
583                    // place made the block-exit materialization emit the
584                    // SAME allocation a second time (`this.last = new
585                    // Report(..); new Report(..);` — a duplicated
586                    // observable side effect; lab package Guard.report).
587                    // `New { raw: true }` is EXEMPT: the ctor fold reads
588                    // the raw view at the invoke-direct site and must
589                    // still find it (consuming it there broke every
590                    // constructor fold).
591                    if matches!(&e, Expr::NewArray { .. })
592                        || matches!(&e, Expr::New { raw: false, .. })
593                    {
594                        if let Some(slot) = self.regs.get_mut(r as usize) {
595                            *slot = Reg::Undef;
596                        }
597                    }
598                    e
599                }
600            }
601            Reg::PendingCall(_) => {
602                let v = self.materialize(r);
603                self.local_expr(v)
604            }
605        }
606    }
607
608    /// Read for a context that evaluates exactly once (term payloads): the
609    /// value is consumed, not cloned — otherwise the block-exit
610    /// materialization would emit it a second time.
611    fn read_term(&mut self, r: u16) -> Expr {
612        match self.regs.get(r as usize).cloned().unwrap_or(Reg::Undef) {
613            Reg::PendingCall(e) | Reg::Pending(e) => {
614                if (r as usize) < self.regs.len() {
615                    self.regs[r as usize] = Reg::Undef;
616                }
617                self.pending_call = None;
618                e
619            }
620            _ => self.read_nest(r),
621        }
622    }
623
624    fn write(&mut self, r: u16, e: Expr, pc: u32, wide: bool) {
625        if r as usize >= self.regs.len() {
626            return;
627        }
628        self.write_pc[r as usize] = pc;
629        // Loop-carried Pending trees grow multiplicatively across fixpoint
630        // rounds (merge clones the whole tree per side per visit); past
631        // this size the register materializes, which caps every state at
632        // a bounded expression over stable locals.
633        if expr_size(&e) > 32 {
634            self.write_pc[r as usize] = pc;
635            let v = self.materialize_value(r, e);
636            if (r as usize) < self.regs.len() {
637                self.regs[r as usize] = Reg::Live(v);
638            }
639            if wide {
640                self.mark_wide_hi(r + 1, pc);
641            }
642            return;
643        }
644        self.regs[r as usize] = Reg::Pending(e);
645        if wide {
646            self.mark_wide_hi(r + 1, pc);
647        }
648    }
649
650    /// Materialize WITHOUT consuming the register slot's caller contract:
651    /// emit `v = e` and return the var id (used by the size cap).
652    fn materialize_value(&mut self, r: u16, e: Expr) -> u32 {
653        let e = value_of_cmp(&e);
654        let ty = e.type_ref();
655        // fresh_var is type-keyed: a retyped register gets a FRESH var,
656        // so no in-place retyping here (each var keeps its mint type and
657        // its LocalDef label always matches its init).
658        let v = self.fresh_var(r, ty);
659        self.stmts.push(Stmt::LocalDef {
660            var: v,
661            init: Some(e),
662            is_final: false,
663            force_type: true,
664        });
665        v
666    }
667
668    /// Write a value whose evaluation already happened (invoke /
669    /// filled-new-array results): consumed exactly once.
670    fn write_call_result(&mut self, r: u16, e: Expr, pc: u32) {
671        if r as usize >= self.regs.len() {
672            return;
673        }
674        self.write_pc[r as usize] = pc;
675        self.regs[r as usize] = Reg::PendingCall(e);
676    }
677
678    fn mark_wide_hi(&mut self, r: u16, pc: u32) {
679        if (r as usize) < self.regs.len() {
680            self.regs[r as usize] = Reg::WideHi;
681            self.write_pc[r as usize] = pc;
682        }
683    }
684
685    /// Emit `v = expr` and mark the register `Live(v)`.
686    fn materialize(&mut self, r: u16) -> u32 {
687        let cur = self.regs.get(r as usize).cloned().unwrap_or(Reg::Undef);
688        let e = match cur {
689            Reg::Pending(e) | Reg::PendingCall(e) => e,
690            Reg::Live(v) => return v,
691            // Undef (a value flowing in from an unanalyzed path): a fresh
692            // local, NOT Const 0 — `0.new a()` was not even valid Java.
693            _ => {
694                let v = self.fresh_var(r, TypeRef::J(JavaType::Object("java/lang/Object".into())));
695                self.stmts.push(Stmt::LocalDef {
696                    var: v,
697                    init: None,
698                    is_final: false,
699                    force_type: false,
700                });
701                if (r as usize) < self.regs.len() {
702                    self.regs[r as usize] = Reg::Live(v);
703                }
704                return v;
705            }
706        };
707        // cmp sentinels stored as values become library compare calls.
708        let e = value_of_cmp(&e);
709        let ty = e.type_ref();
710        // fresh_var is type-keyed: a retyped register gets a FRESH var,
711        // so no in-place retyping here (each var keeps its mint type and
712        // its LocalDef label always matches its init).
713        let v = self.fresh_var(r, ty);
714        self.stmts.push(Stmt::LocalDef {
715            var: v,
716            init: Some(e),
717            is_final: false,
718            force_type: true,
719        });
720        if (r as usize) < self.regs.len() {
721            self.regs[r as usize] = Reg::Live(v);
722        }
723        v
724    }
725
726    /// Discard an unconsumed call result (invoke without move-result).
727    fn drop_pending_call(&mut self) {
728        if let Some(e) = self.pending_call.take() {
729            let e = value_of_cmp(&e);
730            if has_side_effects(&e) {
731                self.stmts.push(Stmt::ExprStmt(e));
732            }
733        }
734    }
735
736    // -- helpers -------------------------------------------------------------
737
738    fn owner_expr(&self, obj: Expr, cls: &str) -> Option<Box<Expr>> {
739        if cls == self.env.class_name && matches!(obj, Expr::This) {
740            return None;
741        }
742        Some(Box::new(obj))
743    }
744
745    fn arith_op(op: ArithOp) -> BinOp {
746        match op {
747            ArithOp::Add => BinOp::Add,
748            ArithOp::Sub => BinOp::Sub,
749            ArithOp::Mul => BinOp::Mul,
750            ArithOp::Div => BinOp::Div,
751            ArithOp::Rem => BinOp::Rem,
752            ArithOp::And => BinOp::And,
753            ArithOp::Or => BinOp::Or,
754            ArithOp::Xor => BinOp::Xor,
755            ArithOp::Shl => BinOp::Shl,
756            ArithOp::Shr => BinOp::Shr,
757            ArithOp::Ushr => BinOp::Ushr,
758        }
759    }
760
761    fn cmp_op(op: CmpOp) -> BinOp {
762        match op {
763            CmpOp::Eq => BinOp::Eq,
764            CmpOp::Ne => BinOp::Ne,
765            CmpOp::Lt => BinOp::Lt,
766            CmpOp::Ge => BinOp::Ge,
767            CmpOp::Gt => BinOp::Gt,
768            CmpOp::Le => BinOp::Le,
769        }
770    }
771
772    fn cmp_sentinel(kind: CmpKind, l: Expr, r: Expr) -> Expr {
773        let name = match kind {
774            CmpKind::CmplF => "\0cmpl-float",
775            CmpKind::CmpgF => "\0cmpg-float",
776            CmpKind::CmplD => "\0cmpl-double",
777            CmpKind::CmpgD => "\0cmpg-double",
778            CmpKind::CmpJ => "\0cmp-long",
779        };
780        Expr::Invokedynamic {
781            name: name.into(),
782            desc: MethodDescriptor {
783                args: vec![],
784                ret: JavaType::Int,
785            },
786            args: vec![l, r],
787            bsm_text: String::new(),
788            bsm_static_args: vec![],
789        }
790    }
791
792    // -- the main loop ---------------------------------------------------------
793
794    /// Lift one block. `handler_types` gives catch types for ranges this
795    /// block handles (for move-exception typing).
796    pub fn build_block(
797        mut self,
798        ins: &[Insn],
799        handler_types: &[Option<std::sync::Arc<str>>],
800    ) -> BResult<(BlockResult, OutState)> {
801        let payloads = &self.env.code.payloads;
802        // Block-scoped event analysis, unconditionally: `ins` is ONE
803        // BLOCK's slice, but the register generation a read belongs to
804        // spans blocks — a post-fold `New` view created in an earlier
805        // block is READ here, so gating on "this block has no
806        // new-instance/new-array" (d9bbb2a) silently flipped every such
807        // read from inline to materialize: cross-block `foo(new Bar())`
808        // nesting lost, statement counts inflated, and the changed var
809        // structure fed forward_single_use a def-reference cycle
810        // (weixin input/b4: ~300k recursion frames, 64MB stack abort).
811        self.final_read = compute_final_reads(ins);
812        for ins in ins {
813            self.cur_pc = ins.pc;
814            match &ins.kind {
815                InsnKind::Nop => {}
816                InsnKind::Unknown => {
817                    return Err(format!(
818                        "unsupported opcode {:#04x} ({}) at pc {}",
819                        ins.op,
820                        ddc_dex::insn::op_name(ins.op),
821                        ins.pc
822                    ));
823                }
824                InsnKind::Move { dst, src } => {
825                    // Impure / call values are consumed once: materialize the
826                    // source so both registers share one evaluation.
827                    let src_state = self.regs.get(*src as usize).cloned().unwrap_or(Reg::Undef);
828                    if matches!(src_state, Reg::PendingCall(_))
829                        || matches!(&src_state, Reg::Pending(e) if has_side_effects(e))
830                    {
831                        self.materialize(*src);
832                    }
833                    let st = self.regs.get(*src as usize).cloned().unwrap_or(Reg::Undef);
834                    let wide = reg_is_wide(&st, self.vt);
835                    if (*dst as usize) < self.regs.len() {
836                        self.regs[*dst as usize] = st;
837                        self.write_pc[*dst as usize] =
838                            self.write_pc.get(*src as usize).copied().unwrap_or(0);
839                    }
840                    if wide {
841                        self.mark_wide_hi(*dst + 1, ins.pc);
842                    }
843                }
844                InsnKind::MoveResult { dst } => {
845                    if let Some(e) = self.pending_call.take() {
846                        let wide =
847                            matches!(e.type_ref().erased(), JavaType::Long | JavaType::Double);
848                        self.write_call_result(*dst, e, ins.pc);
849                        if wide {
850                            self.mark_wide_hi(*dst + 1, ins.pc);
851                        }
852                    } else {
853                        // move-result without a pending call: fresh var.
854                        let v = self.fresh_var(*dst, TypeRef::J(JavaType::Int));
855                        if (*dst as usize) < self.regs.len() {
856                            self.regs[*dst as usize] = Reg::Live(v);
857                        }
858                    }
859                }
860                InsnKind::MoveException { dst } => {
861                    let ty = handler_types
862                        .iter()
863                        .flatten()
864                        .next()
865                        .map(|t| TypeRef::J(JavaType::Object(t.clone())))
866                        .unwrap_or_else(|| {
867                            TypeRef::J(JavaType::Object("java/lang/Throwable".into()))
868                        });
869                    let v = self.fresh_var(*dst, ty);
870                    self.stmts.push(Stmt::LocalDef {
871                        var: v,
872                        init: None,
873                        is_final: false,
874                        force_type: true,
875                    });
876                    if (*dst as usize) < self.regs.len() {
877                        self.regs[*dst as usize] = Reg::Live(v);
878                    }
879                }
880                InsnKind::ReturnVoid
881                | InsnKind::Return { .. }
882                | InsnKind::Throw { .. }
883                | InsnKind::Goto { .. }
884                | InsnKind::PackedSwitch { .. }
885                | InsnKind::SparseSwitch { .. }
886                | InsnKind::If { .. } => {}
887                InsnKind::Const { dst, val, wide } => {
888                    let e = if *wide {
889                        Expr::Const(ConstVal::Long(*val))
890                    } else {
891                        Expr::Const(ConstVal::Int(*val as i32))
892                    };
893                    self.write(*dst, e, ins.pc, *wide);
894                }
895                InsnKind::ConstString { dst, str_idx } => {
896                    let s = std::sync::Arc::from(self.env.dex.string(*str_idx));
897                    self.write(*dst, Expr::Const(ConstVal::Str(s)), ins.pc, false);
898                }
899                InsnKind::ConstClass { dst, type_idx } => {
900                    let ty = TypeRef::J(self.env.java_type(*type_idx));
901                    self.write(*dst, Expr::Const(ConstVal::ClassLit(ty)), ins.pc, false);
902                }
903                InsnKind::MonitorEnter { reg } => {
904                    let e = self.read_nest(*reg);
905                    *self.mflags = self.mflags.with_monitor();
906                    self.stmts.push(Stmt::MonitorEnter(e));
907                }
908                InsnKind::MonitorExit { reg } => {
909                    let e = self.read_nest(*reg);
910                    *self.mflags = self.mflags.with_monitor();
911                    self.stmts.push(Stmt::MonitorExit(e));
912                }
913                InsnKind::CheckCast { reg, type_idx } => {
914                    let e = self.read_nest(*reg);
915                    let ty = TypeRef::J(self.env.java_type(*type_idx));
916                    self.write(*reg, Expr::Cast { ty, e: Box::new(e) }, ins.pc, false);
917                }
918                InsnKind::InstanceOf { dst, src, type_idx } => {
919                    let e = self.read_nest(*src);
920                    let ty = TypeRef::J(self.env.java_type(*type_idx));
921                    self.write(*dst, Expr::InstanceOf { e: Box::new(e), ty }, ins.pc, false);
922                }
923                InsnKind::ArrayLength { dst, src } => {
924                    let e = self.read_nest(*src);
925                    let length = Expr::Field {
926                        owner: Some(Box::new(e)),
927                        cls: std::sync::Arc::from(""),
928                        name: "length".into(),
929                        ty: TypeRef::J(JavaType::Int),
930                        is_static: false,
931                    };
932                    self.write(*dst, length, ins.pc, false);
933                }
934                InsnKind::NewInstance { dst, type_idx } => {
935                    let cls = self.env.type_name_arc(*type_idx);
936                    let ty = TypeRef::J(JavaType::Object(cls.clone()));
937                    self.write(
938                        *dst,
939                        Expr::New {
940                            cls,
941                            ty,
942                            args: vec![],
943                            raw: true,
944                        },
945                        ins.pc,
946                        false,
947                    );
948                }
949                InsnKind::NewArray {
950                    dst,
951                    size,
952                    type_idx,
953                } => {
954                    let elem_desc = self.env.type_name(*type_idx);
955                    // The type id names the ARRAY type: strip one `[`.
956                    let elem = crate::desc_type(elem_desc.trim_start_matches('['));
957                    let dims = self.read_nest(*size);
958                    self.write(
959                        *dst,
960                        Expr::NewArray {
961                            elem: TypeRef::J(elem),
962                            dims: vec![dims],
963                            trailing_dims: 0,
964                            init: None,
965                        },
966                        ins.pc,
967                        false,
968                    );
969                }
970                InsnKind::FilledNewArray { regs, type_idx } => {
971                    self.drop_pending_call();
972                    let elem_desc = self.env.type_name(*type_idx);
973                    let elem = crate::desc_type(elem_desc.trim_start_matches('['));
974                    let args: Vec<Expr> = regs.iter().map(|&r| self.read_nest(r)).collect();
975                    self.pending_call = Some(Expr::NewArray {
976                        elem: TypeRef::J(elem),
977                        dims: vec![],
978                        trailing_dims: 0,
979                        init: Some(args),
980                    });
981                }
982                InsnKind::FillArrayData { reg, payload_pc } => {
983                    if let Some(Payload::ArrayData {
984                        elem_width,
985                        size,
986                        data,
987                    }) = payloads.get(payload_pc)
988                    {
989                        let elems = payload_consts(*elem_width, *size, data);
990                        let cur = self.regs.get(*reg as usize).cloned().unwrap_or(Reg::Undef);
991                        if let Reg::Pending(Expr::NewArray { elem, init, .. }) = cur {
992                            if init.is_none() {
993                                self.write(
994                                    *reg,
995                                    Expr::NewArray {
996                                        elem,
997                                        dims: vec![Expr::Const(ConstVal::Int(*size as i32))],
998                                        trailing_dims: 0,
999                                        init: Some(elems),
1000                                    },
1001                                    ins.pc,
1002                                    false,
1003                                );
1004                                continue;
1005                            }
1006                        }
1007                        // Not the new-array+fill pattern: element stores.
1008                        let arr = self.read_nest(*reg);
1009                        for (i, e) in elems.into_iter().enumerate() {
1010                            let idx = Expr::Const(ConstVal::Int(i as i32));
1011                            let target = Expr::ArrayIndex {
1012                                array: Box::new(arr.clone()),
1013                                index: Box::new(idx),
1014                            };
1015                            self.stmts.push(Stmt::ExprStmt(Expr::Assign {
1016                                target: Box::new(target),
1017                                op: AssignOp::Plain,
1018                                value: Box::new(e),
1019                            }));
1020                        }
1021                    }
1022                }
1023                InsnKind::Cmp { dst, a, b, kind } => {
1024                    let l = self.read_nest(*a);
1025                    let r = self.read_nest(*b);
1026                    *self.mflags = self.mflags.with_cmp();
1027                    self.write(*dst, Self::cmp_sentinel(*kind, l, r), ins.pc, false);
1028                }
1029                InsnKind::AGet {
1030                    dst, array, index, ..
1031                } => {
1032                    let a = self.read_nest(*array);
1033                    let i = self.read_nest(*index);
1034                    self.write(
1035                        *dst,
1036                        Expr::ArrayIndex {
1037                            array: Box::new(a),
1038                            index: Box::new(i),
1039                        },
1040                        ins.pc,
1041                        false,
1042                    );
1043                }
1044                InsnKind::APut {
1045                    value,
1046                    array,
1047                    index,
1048                    ..
1049                } => {
1050                    self.drop_pending_call();
1051                    let v = self.read_nest(*value);
1052                    let a = self.read_nest(*array);
1053                    let i = self.read_nest(*index);
1054                    let target = Expr::ArrayIndex {
1055                        array: Box::new(a),
1056                        index: Box::new(i),
1057                    };
1058                    self.stmts.push(Stmt::ExprStmt(Expr::Assign {
1059                        target: Box::new(target),
1060                        op: AssignOp::Plain,
1061                        value: Box::new(v),
1062                    }));
1063                }
1064                InsnKind::IGet {
1065                    dst,
1066                    obj,
1067                    field_idx,
1068                } => {
1069                    self.drop_pending_call();
1070                    let (cls, name, ty) = self.env.field_ref(*field_idx);
1071                    let owner = self.read_nest(*obj);
1072                    let owner_opt = self.owner_expr(owner, &cls);
1073                    self.write(
1074                        *dst,
1075                        Expr::Field {
1076                            owner: owner_opt,
1077                            cls,
1078                            name,
1079                            ty: TypeRef::J(ty),
1080                            is_static: false,
1081                        },
1082                        ins.pc,
1083                        false,
1084                    );
1085                }
1086                InsnKind::IPut {
1087                    value,
1088                    obj,
1089                    field_idx,
1090                } => {
1091                    self.drop_pending_call();
1092                    let (cls, name, ty) = self.env.field_ref(*field_idx);
1093                    let v = null_in_obj_ctx(self.read_nest(*value), &ty);
1094                    let owner = self.read_nest(*obj);
1095                    let owner_opt = self.owner_expr(owner, &cls);
1096                    let target = Expr::Field {
1097                        owner: owner_opt,
1098                        cls,
1099                        name,
1100                        ty: TypeRef::J(ty),
1101                        is_static: false,
1102                    };
1103                    self.stmts.push(Stmt::ExprStmt(Expr::Assign {
1104                        target: Box::new(target),
1105                        op: AssignOp::Plain,
1106                        value: Box::new(v),
1107                    }));
1108                }
1109                InsnKind::SGet { dst, field_idx } => {
1110                    self.drop_pending_call();
1111                    let (cls, name, ty) = self.env.field_ref(*field_idx);
1112                    self.write(
1113                        *dst,
1114                        Expr::Field {
1115                            owner: None,
1116                            cls,
1117                            name,
1118                            ty: TypeRef::J(ty),
1119                            is_static: true,
1120                        },
1121                        ins.pc,
1122                        false,
1123                    );
1124                }
1125                InsnKind::SPut { value, field_idx } => {
1126                    self.drop_pending_call();
1127                    let (cls, name, ty) = self.env.field_ref(*field_idx);
1128                    let v = null_in_obj_ctx(self.read_nest(*value), &ty);
1129                    let target = Expr::Field {
1130                        owner: None,
1131                        cls,
1132                        name,
1133                        ty: TypeRef::J(ty),
1134                        is_static: true,
1135                    };
1136                    self.stmts.push(Stmt::ExprStmt(Expr::Assign {
1137                        target: Box::new(target),
1138                        op: AssignOp::Plain,
1139                        value: Box::new(v),
1140                    }));
1141                }
1142                InsnKind::Invoke {
1143                    kind,
1144                    regs,
1145                    method_idx,
1146                } => {
1147                    self.drop_pending_call();
1148                    self.do_invoke(*kind, regs, *method_idx, ins.pc)?;
1149                }
1150                InsnKind::InvokeCustom {
1151                    call_site_idx,
1152                    regs,
1153                } => {
1154                    self.drop_pending_call();
1155                    let e = self.build_invoke_custom(*call_site_idx, regs, ins.pc);
1156                    self.pending_call = Some(e);
1157                }
1158                InsnKind::Un {
1159                    dst,
1160                    src,
1161                    op,
1162                    from: _,
1163                    to,
1164                } => {
1165                    let e = self.read_nest(*src);
1166                    let ne = match op {
1167                        ddc_dex::insn::UnArith::Neg => Expr::Un {
1168                            op: UnOp::Neg,
1169                            e: Box::new(e),
1170                        },
1171                        ddc_dex::insn::UnArith::Not => Expr::Un {
1172                            op: UnOp::BitNot,
1173                            e: Box::new(e),
1174                        },
1175                        ddc_dex::insn::UnArith::Conv => Expr::Cast {
1176                            ty: TypeRef::J(primitive_type(*to)),
1177                            e: Box::new(e),
1178                        },
1179                    };
1180                    let wide = matches!(*to, 'J' | 'D');
1181                    self.write(*dst, ne, ins.pc, wide);
1182                }
1183                InsnKind::Bin { op, dst, a, b, ty } => {
1184                    let l = self.read_nest(*a);
1185                    let r = self.read_nest(*b);
1186                    let jt = primitive_type(*ty);
1187                    let e = Expr::Bin {
1188                        op: Self::arith_op(*op),
1189                        l: Box::new(l),
1190                        r: Box::new(r),
1191                        ty: Some(TypeRef::J(jt)),
1192                    };
1193                    let wide = matches!(*ty, 'J' | 'D');
1194                    self.write(*dst, e, ins.pc, wide);
1195                }
1196                InsnKind::BinLit {
1197                    op,
1198                    dst,
1199                    a,
1200                    lit,
1201                    rsub,
1202                } => {
1203                    let av = self.read_nest(*a);
1204                    let e = if *rsub {
1205                        Expr::Bin {
1206                            op: BinOp::Sub,
1207                            l: Box::new(Expr::Const(ConstVal::Int(*lit))),
1208                            r: Box::new(av),
1209                            ty: Some(TypeRef::J(JavaType::Int)),
1210                        }
1211                    } else {
1212                        Expr::Bin {
1213                            op: Self::arith_op(*op),
1214                            l: Box::new(av),
1215                            r: Box::new(Expr::Const(ConstVal::Int(*lit))),
1216                            ty: Some(TypeRef::J(JavaType::Int)),
1217                        }
1218                    };
1219                    self.write(*dst, e, ins.pc, false);
1220                }
1221                InsnKind::ConstMethodHandle { dst, handle_idx } => {
1222                    self.drop_pending_call();
1223                    let e = self.method_handle_expr(*handle_idx);
1224                    self.write(*dst, e, ins.pc, false);
1225                }
1226                InsnKind::ConstMethodType { dst, proto_idx } => {
1227                    self.drop_pending_call();
1228                    let proto = self.env.dex.proto(*proto_idx);
1229                    let desc = format!(
1230                        "({}){}",
1231                        self.env
1232                            .dex
1233                            .proto_params(*proto_idx)
1234                            .iter()
1235                            .map(|&t| self.env.dex.type_name(t))
1236                            .collect::<Vec<_>>()
1237                            .join(""),
1238                        self.env.dex.type_name(proto.return_type_idx)
1239                    );
1240                    self.write(*dst, Expr::Raw(desc), ins.pc, false);
1241                }
1242            }
1243        }
1244
1245        // Terminator first: terms may consume PendingCall values directly.
1246        let term = self.block_term(ins);
1247
1248        // Exit cleanup: unconsumed call → statement; impure pendings → vars.
1249        // Open `new`/`new-array` views stay pending (their consumer may live
1250        // in a later block).
1251        self.drop_pending_call();
1252        self.materialize_impure_at_exit();
1253
1254        // Move (not clone): the lifter is consumed after build_block, and
1255        // these two clones — regs deep-copies boxed Exprs for any Pending
1256        // states — ran 3.7M times on weibo.
1257        let out = OutState {
1258            regs: std::mem::take(&mut self.regs),
1259            write_pc: std::mem::take(&mut self.write_pc),
1260        };
1261        Ok((
1262            BlockResult {
1263                stmts: std::mem::take(&mut self.stmts),
1264                out_stack: Vec::new(),
1265                term,
1266            },
1267            out,
1268        ))
1269    }
1270
1271    fn do_invoke(
1272        &mut self,
1273        kind: InvokeKind,
1274        regs: &[u16],
1275        method_idx: u32,
1276        pc: u32,
1277    ) -> BResult<()> {
1278        let (cls, name, md) = self.env.method_ref(method_idx);
1279        if cls.as_ref() == "java/lang/StringBuilder" || cls.as_ref() == "java/lang/StringBuffer" {
1280            *self.mflags = self.mflags.with_sb();
1281        }
1282        let is_static = matches!(kind, InvokeKind::Static);
1283        let receiver_reg = if is_static {
1284            None
1285        } else {
1286            regs.first().copied()
1287        };
1288        let arg_regs: Vec<u16> = if is_static {
1289            regs.to_vec()
1290        } else {
1291            regs.iter().skip(1).copied().collect()
1292        };
1293        let mut args: Vec<Expr> = Vec::with_capacity(md.args.len());
1294        for (i, at) in md.args.iter().enumerate() {
1295            let r = arg_regs.get(i).copied().unwrap_or(0);
1296            args.push(null_in_obj_ctx(self.read_nest(r), at));
1297        }
1298        let recv_expr = receiver_reg.map(|r| self.read_nest(r));
1299
1300        // Constructor call: fold `new C` receivers; this/super otherwise.
1301        if name.as_ref() == "<init>" && matches!(kind, InvokeKind::Direct) {
1302            let recv_reg = receiver_reg.unwrap_or(0);
1303            let recv_state = self
1304                .regs
1305                .get(recv_reg as usize)
1306                .cloned()
1307                .unwrap_or(Reg::Undef);
1308            if let Reg::Pending(Expr::New {
1309                raw: true, cls: nc, ..
1310            }) = &recv_state
1311            {
1312                if nc == &cls {
1313                    let ty = TypeRef::J(JavaType::Object(cls.clone()));
1314                    self.write(
1315                        recv_reg,
1316                        Expr::New {
1317                            cls: cls.clone(),
1318                            ty,
1319                            args,
1320                            raw: false,
1321                        },
1322                        pc,
1323                        false,
1324                    );
1325                    return Ok(());
1326                }
1327            }
1328            let owner_expr_v = recv_expr.unwrap_or(Expr::This);
1329            // Normalize the receiver: in an instance method the `this`
1330            // parameter is ALWAYS var 0 (entry_regs pushes it first).
1331            // read_nest hands back Local{var:0}, which the emitter's
1332            // is_this/lost_alloc checks do not recognize — a plain
1333            // `super()` arrived there as a non-this receiver and printed
1334            // `new Object();` (every default ctor in every app).
1335            let owner_expr_v = if !self.env.is_static
1336                && matches!(&owner_expr_v, Expr::Local { var: 0, .. })
1337            {
1338                Expr::This
1339            } else {
1340                owner_expr_v
1341            };
1342            let is_super = cls.as_ref() != self.env.class_name.as_str();
1343            let owner = if matches!(owner_expr_v, Expr::This) && is_super {
1344                None
1345            } else {
1346                Some(Box::new(owner_expr_v))
1347            };
1348            let call = Expr::Method {
1349                owner,
1350                cls,
1351                name: "<init>".into(),
1352                desc: md,
1353                args,
1354                is_static: false,
1355                is_interface: false,
1356                is_special: true,
1357                is_super,
1358                is_dynamic: false,
1359                type_args: vec![],
1360            };
1361            self.stmts.push(Stmt::ExprStmt(call));
1362            return Ok(());
1363        }
1364
1365        let is_interface = matches!(kind, InvokeKind::Interface);
1366        let is_super = match kind {
1367            InvokeKind::Super => true,
1368            InvokeKind::Direct if !is_static => cls.as_ref() != self.env.class_name.as_str(),
1369            _ => false,
1370        };
1371        let owner: Option<Box<Expr>> = match recv_expr {
1372            None => None,
1373            Some(o) => {
1374                if is_super && matches!(o, Expr::This) {
1375                    None
1376                } else {
1377                    self.owner_expr(o, &cls)
1378                }
1379            }
1380        };
1381        let is_dynamic = matches!(kind, InvokeKind::Polymorphic);
1382        let call = Expr::Method {
1383            owner,
1384            cls,
1385            name,
1386            desc: md,
1387            args,
1388            is_static,
1389            is_interface,
1390            is_special: matches!(kind, InvokeKind::Direct),
1391            is_super,
1392            is_dynamic,
1393            type_args: vec![],
1394        };
1395        self.pending_call = Some(call);
1396        Ok(())
1397    }
1398
1399    /// Resolve a method-handle constant into a readable expression
1400    /// (`Cls::name` for method kinds).
1401    fn method_handle_expr(&mut self, handle_idx: u32) -> Expr {
1402        let Some(h) = self.env.dex.method_handle(handle_idx) else {
1403            return Expr::Raw(format!("methodHandle@{}", handle_idx));
1404        };
1405        if h.is_field {
1406            let f = self.env.dex.field(h.target_id);
1407            let cls = self.env.dex.class_name(f.class_idx);
1408            let name = self.env.dex.string(f.name_idx).to_string();
1409            return Expr::Raw(format!("{}::{}", cls.replace('/', "."), name));
1410        }
1411        let m = self.env.dex.method(h.target_id);
1412        let cls = self.env.dex.class_name(m.class_idx);
1413        let name = self.env.dex.string(m.name_idx).to_string();
1414        Expr::Raw(format!("{}::{}", cls.replace('/', "."), name))
1415    }
1416
1417    /// Build the expression for an invoke-custom site.
1418    fn build_invoke_custom(&mut self, cs_idx: u32, regs: &[u16], pc: u32) -> Expr {
1419        let Some(cs) = self.env.dex.call_site(cs_idx).cloned() else {
1420            return Expr::Invokedynamic {
1421                name: format!("call-site#{}", cs_idx),
1422                desc: jdc_core::types::MethodDescriptor {
1423                    args: vec![],
1424                    ret: JavaType::Object("java/lang/Object".into()),
1425                },
1426                args: vec![],
1427                bsm_text: "unresolved".into(),
1428                bsm_static_args: vec![],
1429            };
1430        };
1431        let site_name = self.env.dex.string(cs.name_idx).to_string();
1432        let params: Vec<JavaType> = self
1433            .env
1434            .dex
1435            .proto_params(cs.proto_idx)
1436            .iter()
1437            .map(|&t| self.env.java_type(t))
1438            .collect();
1439        let ret = self
1440            .env
1441            .java_type(self.env.dex.proto(cs.proto_idx).return_type_idx);
1442
1443        let (bs_cls, bs_name) = match self.env.dex.method_handle(cs.bootstrap_handle) {
1444            Some(h) if !h.is_field => {
1445                let m = self.env.dex.method(h.target_id);
1446                (
1447                    self.env.dex.class_name(m.class_idx),
1448                    self.env.dex.string(m.name_idx).to_string(),
1449                )
1450            }
1451            _ => (String::new(), String::new()),
1452        };
1453
1454        // Dynamic arguments (the SAM parameters for lambdas).
1455        let mut args: Vec<Expr> = Vec::with_capacity(params.len().min(regs.len()));
1456        for (i, _) in params.iter().enumerate() {
1457            if let Some(&r) = regs.get(i) {
1458                args.push(self.read_nest(r));
1459            }
1460        }
1461
1462        // StringConcatFactory: fold the recipe into `+`.
1463        if bs_name == "makeConcatWithConstants" {
1464            let recipe = cs
1465                .linker_args
1466                .iter()
1467                .find_map(|v| match v {
1468                    ddc_dex::annotations::EncodedValue::String(s) => {
1469                        Some(self.env.dex.string(*s).to_string())
1470                    }
1471                    _ => None,
1472                })
1473                .unwrap_or_default();
1474            return concat_from_recipe(&recipe, args);
1475        }
1476
1477        // LambdaMetafactory: linker args are [samType (erased),
1478        // implMethod (handle), instantiatedMethodType]. The instantiated
1479        // type carries the source-level SAM parameters.
1480        let mut impl_m = None;
1481        let mut instantiated_proto: Option<u32> = None;
1482        if bs_name == "metafactory" || bs_name == "altMetafactory" {
1483            let mut seen_handle = false;
1484            for lv in &cs.linker_args {
1485                match lv {
1486                    ddc_dex::annotations::EncodedValue::MethodHandle(hi) => {
1487                        if !seen_handle {
1488                            seen_handle = true;
1489                            if let Some(h) = self.env.dex.method_handle(*hi) {
1490                                if !h.is_field {
1491                                    let m = self.env.dex.method(h.target_id);
1492                                    impl_m = Some((
1493                                        self.env.dex.class_name(m.class_idx).replace('/', "."),
1494                                        self.env.dex.string(m.name_idx).to_string(),
1495                                    ));
1496                                }
1497                            }
1498                        }
1499                    }
1500                    ddc_dex::annotations::EncodedValue::MethodType(p) if seen_handle => {
1501                        instantiated_proto = Some(*p);
1502                    }
1503                    _ => {}
1504                }
1505            }
1506        }
1507        let _ = pc;
1508
1509        // Remaining registers are the captured values.
1510        let mut captures = Vec::new();
1511        for &r in regs.iter().skip(params.len().min(regs.len())) {
1512            captures.push(self.read_nest(r));
1513        }
1514
1515        if let Some((impl_cls, impl_name)) = impl_m {
1516            if captures.is_empty() {
1517                // Non-capturing: render real Java syntax. A synthetic
1518                // `lambda$` impl is a lambda; anything else is a method
1519                // reference (`Cls::name`).
1520                if impl_name.starts_with("lambda$") {
1521                    // Source-level parameter count comes from the
1522                    // instantiated SAM type, not the site proto (which is
1523                    // the functional-interface factory view: `()Function`).
1524                    let n_params = match instantiated_proto {
1525                        Some(p) => self.env.dex.proto_params(p).len(),
1526                        None => params.len(),
1527                    };
1528                    let pnames: Vec<String> = (0..n_params).map(|i| format!("a{}", i)).collect();
1529                    return Expr::Raw(format!(
1530                        "({}) -> {}.{}({})",
1531                        pnames.join(", "),
1532                        impl_cls,
1533                        impl_name,
1534                        pnames.join(", ")
1535                    ));
1536                }
1537                return Expr::Raw(format!("{}::{}", impl_cls, impl_name));
1538            }
1539            // Capturing: the captures are real expressions; emit the
1540            // desugared call shape (impl applies SAM params then captures).
1541            let mut call_args = args;
1542            call_args.extend(captures);
1543            return Expr::Invokedynamic {
1544                name: format!("{} -> {}.{}", site_name, impl_cls, impl_name),
1545                desc: jdc_core::types::MethodDescriptor { args: params, ret },
1546                args: call_args,
1547                bsm_text: format!("{}::{}", bs_cls, bs_name),
1548                bsm_static_args: vec![],
1549            };
1550        }
1551
1552        Expr::Invokedynamic {
1553            name: site_name,
1554            desc: jdc_core::types::MethodDescriptor { args: params, ret },
1555            args: captures,
1556            bsm_text: format!("{}::{}", bs_cls, bs_name),
1557            bsm_static_args: vec![],
1558        }
1559    }
1560
1561    fn materialize_impure_at_exit(&mut self) {
1562        let n = self.regs.len();
1563        for r in 0..n {
1564            let st = self.regs[r].clone();
1565            let impure = match &st {
1566                Reg::PendingCall(_) => true,
1567                Reg::Pending(e) => {
1568                    has_side_effects(e)
1569                        && !matches!(e, Expr::New { raw: true, .. })
1570                        && !matches!(e, Expr::NewArray { init: None, .. })
1571                }
1572                _ => false,
1573            };
1574            if impure {
1575                self.materialize(r as u16);
1576            }
1577        }
1578    }
1579
1580    /// Terminator for the block, from its last instruction.
1581    fn block_term(&mut self, ins: &[Insn]) -> Term {
1582        let Some(last) = ins.last() else {
1583            return Term::Goto;
1584        };
1585        match &last.kind {
1586            InsnKind::Goto { .. } => Term::Goto,
1587            InsnKind::ReturnVoid => Term::Return(None),
1588            InsnKind::Return { src } => {
1589                let v = null_in_obj_ctx(self.read_term(*src), &self.env.desc.ret);
1590                Term::Return(Some(v))
1591            }
1592            InsnKind::Throw { reg } => {
1593                // throw is always an object context.
1594                let mut v = self.read_term(*reg);
1595                if let Expr::Const(ConstVal::Int(0)) = v {
1596                    v = Expr::Const(ConstVal::Null);
1597                }
1598                Term::Throw(v)
1599            }
1600            InsnKind::If { op, a, b, z, .. } => {
1601                let cond = if *z {
1602                    let v = self.read_nest(*a);
1603                    let (l, r, realop) = jdc_core::ir::build::unfold_cmp(v, Self::cmp_op(*op));
1604                    Expr::Bin {
1605                        op: realop,
1606                        l: Box::new(l),
1607                        r: Box::new(r),
1608                        ty: Some(TypeRef::J(JavaType::Boolean)),
1609                    }
1610                } else {
1611                    let l = self.read_nest(*a);
1612                    let r = self.read_nest(*b);
1613                    Expr::Bin {
1614                        op: Self::cmp_op(*op),
1615                        l: Box::new(l),
1616                        r: Box::new(r),
1617                        ty: Some(TypeRef::J(JavaType::Boolean)),
1618                    }
1619                };
1620                Term::Cond { cond }
1621            }
1622            InsnKind::PackedSwitch { reg, payload_pc }
1623            | InsnKind::SparseSwitch { reg, payload_pc } => {
1624                let selector = self.read_term(*reg);
1625                let default = Some(last.pc + last.size);
1626                let targets = match self.env.code.payloads.get(payload_pc) {
1627                    Some(Payload::Packed { first_key, targets }) => SwitchTargets::Table {
1628                        low: *first_key,
1629                        targets: targets
1630                            .iter()
1631                            .map(|t| (last.pc as i64 + *t as i64) as u32)
1632                            .collect(),
1633                    },
1634                    Some(Payload::Sparse { pairs }) => SwitchTargets::Lookup {
1635                        pairs: pairs
1636                            .iter()
1637                            .map(|(k, t)| (*k, (last.pc as i64 + *t as i64) as u32))
1638                            .collect(),
1639                    },
1640                    _ => SwitchTargets::Lookup { pairs: vec![] },
1641                };
1642                Term::Switch {
1643                    selector,
1644                    targets,
1645                    default,
1646                }
1647            }
1648            // Fell off the analysis end (payload region after): goto.
1649            _ => {
1650                let next = last.pc + last.size;
1651                if next < self.code_units {
1652                    Term::Fallthrough
1653                } else {
1654                    Term::Goto
1655                }
1656            }
1657        }
1658    }
1659}
1660
1661/// StringConcatFactory recipe → `+` parts: `\u{1}` slots consume the next
1662/// argument, `\u{2}` <len> <chars> emits literal text (rare), other bytes
1663/// are literal.
1664fn concat_from_recipe(recipe: &str, args: Vec<Expr>) -> Expr {
1665    use jdc_core::ir::expr::ConcatPart;
1666    let mut parts: Vec<ConcatPart> = Vec::new();
1667    let mut arg_i = 0usize;
1668    let mut const_buf = String::new();
1669    for c in recipe.chars() {
1670        match c {
1671            '\u{1}' => {
1672                if !const_buf.is_empty() {
1673                    parts.push(ConcatPart::Const(std::mem::take(&mut const_buf)));
1674                }
1675                if arg_i < args.len() {
1676                    parts.push(ConcatPart::Str(args[arg_i].clone()));
1677                    arg_i += 1;
1678                }
1679            }
1680            '\u{2}' => {
1681                // Length-prefixed literal: skip the length char and copy the
1682                // following chars until the next marker.
1683            }
1684            c => const_buf.push(c),
1685        }
1686    }
1687    if !const_buf.is_empty() {
1688        parts.push(ConcatPart::Const(const_buf));
1689    }
1690    Expr::StringConcat(parts)
1691}
1692
1693/// Node count of an expression tree (caps loop-carried growth: a Pending
1694/// tree that grows across fixpoint rounds would otherwise reach GBs — each
1695/// merge clones the whole tree per side per visit).
1696fn expr_size(e: &Expr) -> usize {
1697    let mut n = 0usize;
1698    count_nodes(e, &mut n);
1699    n
1700}
1701
1702fn count_nodes(e: &Expr, n: &mut usize) {
1703    *n += 1;
1704    if *n > 512 {
1705        return; // cap the walk itself
1706    }
1707    match e {
1708        Expr::Un { e, .. } | Expr::Cast { e, .. } | Expr::InstanceOf { e, .. } => count_nodes(e, n),
1709        Expr::Bin { l, r, .. } => {
1710            count_nodes(l, n);
1711            count_nodes(r, n);
1712        }
1713        Expr::Cond { c, t, f } => {
1714            count_nodes(c, n);
1715            count_nodes(t, n);
1716            count_nodes(f, n);
1717        }
1718        Expr::Assign { target, value, .. } => {
1719            count_nodes(target, n);
1720            count_nodes(value, n);
1721        }
1722        Expr::PreIncDec { e, .. } | Expr::PostIncDec { e, .. } => count_nodes(e, n),
1723        Expr::Field { owner: Some(o), .. } => count_nodes(o, n),
1724        Expr::Method {
1725            owner: Some(o),
1726            args,
1727            ..
1728        } => {
1729            count_nodes(o, n);
1730            for a in args {
1731                count_nodes(a, n);
1732            }
1733        }
1734        Expr::ArrayIndex { array, index } => {
1735            count_nodes(array, n);
1736            count_nodes(index, n);
1737        }
1738        Expr::New { args, .. } => {
1739            for a in args {
1740                count_nodes(a, n);
1741            }
1742        }
1743        Expr::NewArray { dims, init, .. } => {
1744            for d in dims {
1745                count_nodes(d, n);
1746            }
1747            if let Some(v) = init {
1748                for x in v {
1749                    count_nodes(x, n);
1750                }
1751            }
1752        }
1753        Expr::NewMultiArray { dims, .. } => {
1754            for d in dims {
1755                count_nodes(d, n);
1756            }
1757        }
1758        Expr::StringConcat(parts) => {
1759            for p in parts {
1760                if let ConcatPart::Str(x) = p {
1761                    count_nodes(x, n);
1762                }
1763            }
1764        }
1765        _ => {}
1766    }
1767}
1768
1769fn reg_is_wide(st: &Reg, vt: &VarTable) -> bool {
1770    match st {
1771        Reg::Pending(e) | Reg::PendingCall(e) => e.type_ref().erased().is_wide(),
1772        Reg::Live(v) => vt.var(*v).ty.erased().is_wide(),
1773        Reg::WideHi => true,
1774        Reg::Undef => false,
1775    }
1776}
1777
1778/// d8 encodes `null` as `const/4 vN, 0`; the instruction kind does not
1779/// distinguish object contexts (return/return-object share one variant),
1780/// so the surrounding DESCRIPTOR type decides: a zero constant flowing
1781/// into a reference context is `null`, not `0` (rendering `return 0;`
1782/// from a String method — lab-package feedback).
1783fn null_in_obj_ctx(mut v: Expr, ctx_ty: &JavaType) -> Expr {
1784    if ctx_ty.is_reference() {
1785        if let Expr::Const(ConstVal::Int(0)) = v {
1786            v = Expr::Const(ConstVal::Null);
1787        }
1788    }
1789    v
1790}
1791
1792/// Turn a stored cmp sentinel into a `Long.compare`-style call.
1793fn value_of_cmp(e: &Expr) -> Expr {
1794    if let Expr::Invokedynamic { name, args, .. } = e {
1795        if name.starts_with('\0') && args.len() == 2 {
1796            let (cls, ty) = match name.as_str() {
1797                "\0cmp-long" => ("java/lang/Long", JavaType::Long),
1798                "\0cmpl-float" | "\0cmpg-float" => ("java/lang/Float", JavaType::Float),
1799                _ => ("java/lang/Double", JavaType::Double),
1800            };
1801            let desc = MethodDescriptor {
1802                args: vec![ty.clone(), ty],
1803                ret: JavaType::Int,
1804            };
1805            return Expr::Method {
1806                owner: None,
1807                cls: cls.into(),
1808                name: "compare".into(),
1809                desc: std::sync::Arc::new(desc),
1810                args: args.clone(),
1811                is_static: true,
1812                is_interface: false,
1813                is_special: false,
1814                is_super: false,
1815                is_dynamic: false,
1816                type_args: vec![],
1817            };
1818        }
1819    }
1820    e.clone()
1821}
1822
1823pub fn primitive_type(c: char) -> JavaType {
1824    match c {
1825        'Z' => JavaType::Boolean,
1826        'B' => JavaType::Byte,
1827        'C' => JavaType::Char,
1828        'S' => JavaType::Short,
1829        'F' => JavaType::Float,
1830        'J' => JavaType::Long,
1831        'D' => JavaType::Double,
1832        _ => JavaType::Int,
1833    }
1834}
1835
1836/// Constant elements from a fill-array-data payload, typed by width (the
1837/// consuming new-array's element type refines them later).
1838fn payload_consts(elem_width: u16, size: u32, data: &[u8]) -> Vec<Expr> {
1839    let n = (size as usize).min(data.len() / elem_width.max(1) as usize);
1840    let mut out = Vec::with_capacity(n);
1841    for i in 0..n {
1842        let off = i * elem_width as usize;
1843        let e = match elem_width {
1844            1 => Expr::Const(ConstVal::Int(data[off] as i8 as i32)),
1845            2 => {
1846                let v = i16::from_le_bytes([data[off], data[off + 1]]);
1847                Expr::Const(ConstVal::Int(v as i32))
1848            }
1849            4 => {
1850                let mut b = [0u8; 4];
1851                b.copy_from_slice(&data[off..off + 4]);
1852                Expr::Const(ConstVal::Int(i32::from_le_bytes(b)))
1853            }
1854            8 => {
1855                let mut b = [0u8; 8];
1856                b.copy_from_slice(&data[off..off + 8]);
1857                Expr::Const(ConstVal::Long(i64::from_le_bytes(b)))
1858            }
1859            _ => Expr::Const(ConstVal::Int(0)),
1860        };
1861        out.push(e);
1862    }
1863    out
1864}
1865
1866/// Entry register state from parameters. Returns the register state.
1867pub fn entry_regs(vt: &mut VarTable, env: &MethodEnv, param_names: &[Option<String>]) -> Vec<Reg> {
1868    let code = env.code;
1869    let n = code.registers_size as usize;
1870    let mut regs = vec![Reg::Undef; n.max(1)];
1871    let ins = code.ins_size.min(code.registers_size) as usize;
1872    let mut reg = n.saturating_sub(ins);
1873    let mut pidx = 0;
1874    if !env.is_static && reg < n {
1875        // `this` occupies the first incoming register.
1876        let id = vt.vars.len() as u32;
1877        vt.vars.push(VarInfo {
1878            id,
1879            slot: reg as u16,
1880            name: "this".into(),
1881            ty: TypeRef::J(JavaType::Object(env.class_name.as_str().into())),
1882            is_param: true,
1883            range_start: 0,
1884            range_end: u16::MAX,
1885            synthetic_name: false,
1886        });
1887        while vt.by_slot.len() <= reg {
1888            vt.by_slot.push(Vec::new());
1889        }
1890        vt.by_slot[reg].push((0, u16::MAX, id));
1891        regs[reg] = Reg::Live(id);
1892        reg += 1;
1893        pidx += 1;
1894    }
1895    for (i, arg) in env.desc.args.iter().enumerate() {
1896        if reg >= n {
1897            break;
1898        }
1899        let dbg = param_names.get(i).and_then(|o| o.clone());
1900        let named = dbg.is_some();
1901        // Debug-info names are ARBITRARY DEX strings — obfuscated apps
1902        // ship params named `25` (reqable a4/e: rendered `25 = 25;`, a
1903        // javac parse error). Run them through the same deterministic
1904        // sanitizer as every other declared identifier.
1905        let name = dbg
1906            .map(|d| crate::classdec::java_ident(&d).into_owned())
1907            .unwrap_or_else(|| format!("p{}", pidx));
1908        let id = vt.vars.len() as u32;
1909        vt.vars.push(VarInfo {
1910            id,
1911            slot: reg as u16,
1912            name,
1913            ty: TypeRef::J(arg.clone()),
1914            is_param: true,
1915            range_start: 0,
1916            range_end: u16::MAX,
1917            synthetic_name: !named,
1918        });
1919        while vt.by_slot.len() <= reg {
1920            vt.by_slot.push(Vec::new());
1921        }
1922        vt.by_slot[reg].push((0, u16::MAX, id));
1923        regs[reg] = Reg::Live(id);
1924        reg += if arg.is_wide() { 2 } else { 1 };
1925        pidx += 1;
1926    }
1927    regs
1928}