Skip to main content

jdc_core/ir/
build.rs

1//! The front-end → core hand-off: how a basic block ends and what it produced.
2//!
3//! A front-end walks its own instruction stream, splits it into blocks that
4//! match [`crate::cfg::Cfg`], and emits one [`BlockResult`] per block, indexed
5//! by block id. Everything downstream (structuring, conversion, emission)
6//! works only from these values.
7
8use crate::ir::expr::{BinOp, ConcatPart, ConstVal, Expr};
9use crate::ir::stmt::Stmt;
10use crate::types::JavaType;
11
12/// How a basic block ends.
13#[derive(Debug, Clone)]
14pub enum Term {
15    Fallthrough,
16    Goto,
17    /// Jumps to `succ[1]` when `cond` is true; falls through to `succ[0]`.
18    Cond { cond: Expr },
19    /// A multi-way branch. `default` is the machine offset of the default
20    /// arm when the front-end can identify it (JVM: the switch payload's
21    /// default; DEX: the `packed-switch`/`sparse-switch` payload's default);
22    /// `None` means "unknown / not recorded".
23    Switch {
24        selector: Expr,
25        targets: SwitchTargets,
26        default: Option<u32>,
27    },
28    Return(Option<Expr>),
29    Throw(Expr),
30    /// Legacy `jsr` (JVM only; kept so legacy class files can be modelled).
31    Jsr,
32    /// Legacy `ret` (JVM only).
33    Ret,
34}
35
36#[derive(Debug, Clone)]
37pub enum SwitchTargets {
38    /// `targets[i]` is the target for key `low + i`.
39    Table { low: i32, targets: Vec<u32> },
40    /// `(match value, target)` pairs.
41    Lookup { pairs: Vec<(i32, u32)> },
42}
43
44#[derive(Debug, Clone)]
45pub struct BlockResult {
46    /// Statements produced by this block, in order, covering everything up to
47    /// (not including) the terminator.
48    pub stmts: Vec<Stmt>,
49    /// Value-machine leftovers at block exit — for a stack machine, the
50    /// operand stack; for a register machine, normally empty. Only the
51    /// front-end's own merge handling consumes this.
52    pub out_stack: Vec<Expr>,
53    pub term: Term,
54}
55
56#[derive(Debug)]
57pub struct BuildError(pub String);
58
59pub type BResult<T> = Result<T, BuildError>;
60
61/// `Expr::Const(ConstVal::Int(i))`.
62pub fn int_const(i: i32) -> Expr {
63    Expr::Const(ConstVal::Int(i))
64}
65
66/// True if dropping this expression from a discarded-value context would
67/// change semantics (a call, a store, a field read that can trap, ...).
68pub fn has_side_effects(e: &Expr) -> bool {
69    match e {
70        Expr::Const(_) | Expr::Local { .. } | Expr::This | Expr::Raw(_) | Expr::RawT(..) => false,
71        Expr::New { .. } | Expr::Method { .. } | Expr::Invokedynamic { .. } | Expr::Lambda(_)
72        | Expr::AnonNew { .. } => true,
73        Expr::Assign { .. } | Expr::PreIncDec { .. } | Expr::PostIncDec { .. } => true,
74        Expr::Field { owner, .. } => {
75            // getfield can NPE / trigger clinit; keep to be safe unless owner is this
76            owner.is_some()
77        }
78        Expr::ArrayIndex { .. } => true,
79        Expr::Un { e, .. } | Expr::Cast { e, .. } | Expr::InstanceOf { e, .. } => has_side_effects(e),
80        Expr::Bin { l, r, .. } => has_side_effects(l) || has_side_effects(r),
81        Expr::Cond { c, t, f } => has_side_effects(c) || has_side_effects(t) || has_side_effects(f),
82        Expr::NewArray { dims, init, .. } => {
83            dims.iter().any(has_side_effects)
84                || init
85                    .as_ref()
86                    .map(|v| v.iter().any(has_side_effects))
87                    .unwrap_or(false)
88        }
89        Expr::NewMultiArray { dims, .. } => dims.iter().any(has_side_effects),
90        Expr::StringConcat(parts) => parts.iter().any(|p| match p {
91            ConcatPart::Str(e) => has_side_effects(e),
92            ConcatPart::Const(_) => false,
93        }),
94    }
95}
96
97/// If the value is a cmp sentinel, unfold into `(l, r, op)`; otherwise compare
98/// against zero.
99pub fn unfold_cmp(v: Expr, op_if_int: BinOp) -> (Expr, Expr, BinOp) {
100    if let Expr::Invokedynamic { name, args, .. } = &v {
101        if name.starts_with("\u{0}cmp") && args.len() == 2 {
102            let mut args = match v {
103                Expr::Invokedynamic { args, .. } => args,
104                _ => unreachable!(),
105            };
106            let r = args.pop().unwrap();
107            let l = args.pop().unwrap();
108            return (l, r, op_if_int);
109        }
110    }
111    (v, int_const(0), op_if_int)
112}
113
114/// A machine type reference (`[`-prefixed descriptors are array types).
115pub fn class_name_to_type(name: &str) -> JavaType {
116    if name.starts_with('[') {
117        crate::types::parse_field_descriptor(name).unwrap_or(JavaType::Object(name.to_string()))
118    } else {
119        JavaType::Object(name.to_string())
120    }
121}