Skip to content

Commit d2ecfb9

Browse files
committed
builder/varalloc: read the stored local in dup-conditions; tighten boolean inference
Two related fixes for conditions that re-evaluated a call the store had already performed: - `x = f(); if (x == null)` compiles to dup; astore; ifnull. The builder left the dup'd CALL on the stack, so the branch printed `f()` a second time (`while (reader.readLine() != null)` above a body that read the same stream again — MailcapFile consumed three lines per iteration, AppleScriptEngine two). The store now rewrites the surviving duplicate to a read of the local it just wrote, but only when the next consumer is the conditional branch (constants may sit in between: `dup; istore; iconst_m1; if_icmpeq`). Parameters keep the existing inline-assignment form. - Type inference then un-broke the declaration: a bare `v == 0` test is not evidence of boolean — javac has no distinct test opcode, so `int p = ports[i]; if (p == 0)` looked exactly like a boolean test and was declared `boolean p` (uncompilable: `boolean p = ports[i]`). A boolean inference now also requires 0/1 value-range evidence, and an unambiguous int producer (call result, array element, int field/cast, arithmetic) vetoes it. Ambiguous producers (`v = v & flag`, copies) neither prove nor veto. Verification (rt.jar, 12,609 units): - recompiling the changed classes against rt.jar: 327 files with fewer javac errors (-2,695), 1 already-broken file with +2; no new syntax-bad file (jclassd gate: 1, the asm '()' baseline); - features 59/59 on both structurer paths; corpus spot (jdk8, 30 sources) byte-identical stats (303 methods, 195 equal); - deterministic across parallel and JCDC_THREADS=1 runs; - perf improves (dead call evaluation removed): rt.jar 2.3 s / 686 MB, JDK 28 image 4.7 s / 978 MB; 9.4 s / 427 MB and 23.3 s / 525 MB single-threaded. READMEs refreshed.
1 parent cebb4a9 commit d2ecfb9

4 files changed

Lines changed: 235 additions & 22 deletions

File tree

‎README.md‎

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ fresh Rust implementation.
2525
source corpus in which **100% of decompiled families recompile** with zero
2626
run-mismatches; full `rt.jar` (12,609 classes) renders panic-free and
2727
byte-deterministically on both structurizer pipelines.
28-
- **Fast** — whole `rt.jar` in ~2.4 s and the entire Java 28 runtime image
29-
(27,766 classes) in ~5.1 s on 18 cores, under 1 GB peak RSS (426–947 MB);
28+
- **Fast** — whole `rt.jar` in ~2.3 s and the entire Java 28 runtime image
29+
(27,766 classes) in ~4.7 s on 18 cores, under 1 GB peak RSS (424–978 MB);
3030
parallel by default with deterministic, schedule-independent output.
3131
- **Faithful control flow** — loops/conditions/switch/try-catch-finally
3232
structuring with per-arrival shared-tail copies; source-exact
@@ -48,8 +48,8 @@ cache. Reproduce with [`scripts/bench.sh`](scripts/bench.sh).
4848

4949
| Tool | Wall time | Peak RSS | Units emitted |
5050
|---|---:|---:|---:|
51-
| **jcdc** (parallel — default, 1 worker/core) | **2.4 s** | **694 MB** | 12,609 |
52-
| jcdc (`JCDC_THREADS=1`, single-threaded) | 10.0 s | 426 MB | 12,609 |
51+
| **jcdc** (parallel — default, 1 worker/core) | **2.3 s** | **686 MB** | 12,609 |
52+
| jcdc (`JCDC_THREADS=1`, single-threaded) | 9.4 s | 427 MB | 12,609 |
5353
| Vineflower 1.11.1 | 24.7 s | 8,633 MB | 12,609 |
5454
| CFR 0.152 | 51.9 s | 4,516 MB | 12,609 |
5555
| Procyon 0.6.0 | 108.8 s | 2,218 MB | 12,586 |
@@ -59,8 +59,8 @@ cache. Reproduce with [`scripts/bench.sh`](scripts/bench.sh).
5959

6060
| Tool | Wall time | Peak RSS | Units emitted |
6161
|---|---:|---:|---:|
62-
| **jcdc** (parallel — default) | **5.1 s** | **947 MB** | 15,039 |
63-
| jcdc (`JCDC_THREADS=1`, single-threaded) | 25.0 s | 526 MB | 15,039 |
62+
| **jcdc** (parallel — default) | **4.7 s** | **978 MB** | 15,039 |
63+
| jcdc (`JCDC_THREADS=1`, single-threaded) | 23.3 s | 525 MB | 15,039 |
6464
| CFR 0.152 | 144.7 s | 8,245 MB | 15,012 |
6565
| fernflower (JetBrains) | 497.5 s | 5,108 MB | 15,039 |
6666
| Procyon 0.6.0 | 725.2 s | 6,232 MB | 15,011 |
@@ -76,11 +76,11 @@ entries) with a 16 g heap in 41.5 s — still slower than jcdc's whole-image
7676
run.
7777

7878
Even **single-threaded**, jcdc finishes both workloads faster than every JVM
79-
tool here (10.0 s vs Vineflower's 24.7 s on `rt.jar`), and its whole-run peak
80-
RSS (**426–947 MB** against **2.2–8.7 GB** for the JVM tools) is 2.3×–20×
81-
smaller. With its default parallel pipeline it is ~10× faster than
82-
Vineflower, ~21× CFR, ~45× Procyon and ~86× fernflower on `rt.jar`, and
83-
~28× (CFR) / ~98× (fernflower) / ~142× (Procyon) faster on the Java 28
79+
tool here (9.4 s vs Vineflower's 24.7 s on `rt.jar`), and its whole-run peak
80+
RSS (**424–978 MB** against **2.2–8.7 GB** for the JVM tools) is 2.3×–21×
81+
smaller. With its default parallel pipeline it is ~11× faster than
82+
Vineflower, ~23× CFR, ~48× Procyon and ~93× fernflower on `rt.jar`, and
83+
~31× (CFR) / ~106× (fernflower) / ~154× (Procyon) faster on the Java 28
8484
image. Emitted-unit counts differ slightly between tools: jcdc folds `$`-named
8585
hidden classes into their family file (and emits `module-info.java`), while
8686
Procyon skips `package-info` files (−23 on `rt.jar`). (jcdc runs with

‎README.zh.md‎

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ Java 源码。设计参考了 fernflower/Vineflower、garlic、CFR、Procyon、K
2424
100% 可重编译**、零 run-mismatch;完整 `rt.jar`(12,609 个类)在双结构化
2525
管线下零 panic、逐字节确定。
2626
- **快**——18 核机器上整包 `rt.jar` 约 2.4 秒、Java 28 完整运行时镜像
27-
(27,766 个类)约 5.1 秒,峰值内存低于 1GB(426–947MB);默认并行且输出
27+
(27,766 个类)约 4.7 秒,峰值内存低于 1GB(424–978MB);默认并行且输出
2828
与调度无关(确定性)。
2929
- **控制流忠实**——循环/条件/switch/try-catch-finally 结构化 + per-arrival
3030
共享尾拷贝;短路(`&&`/`||`)与 `assert` 习语源码级还原。
@@ -44,8 +44,8 @@ Java 源码。设计参考了 fernflower/Vineflower、garlic、CFR、Procyon、K
4444

4545
| 工具 | 墙钟时间 | 峰值内存 | 产出文件 |
4646
|---|---:|---:|---:|
47-
| **jcdc**(并行——默认,每核 1 worker) | **2.4 秒** | **694 MB** | 12,609 |
48-
| jcdc(`JCDC_THREADS=1` 单线程) | 10.0 秒 | 426 MB | 12,609 |
47+
| **jcdc**(并行——默认,每核 1 worker) | **2.3 秒** | **686 MB** | 12,609 |
48+
| jcdc(`JCDC_THREADS=1` 单线程) | 9.4 秒 | 427 MB | 12,609 |
4949
| Vineflower 1.11.1 | 24.7 秒 | 8,633 MB | 12,609 |
5050
| CFR 0.152 | 51.9 秒 | 4,516 MB | 12,609 |
5151
| Procyon 0.6.0 | 108.8 秒 | 2,218 MB | 12,586 |
@@ -55,8 +55,8 @@ Java 源码。设计参考了 fernflower/Vineflower、garlic、CFR、Procyon、K
5555

5656
| 工具 | 墙钟时间 | 峰值内存 | 产出文件 |
5757
|---|---:|---:|---:|
58-
| **jcdc**(并行——默认) | **5.1 秒** | **947 MB** | 15,039 |
59-
| jcdc(`JCDC_THREADS=1` 单线程) | 25.0 秒 | 526 MB | 15,039 |
58+
| **jcdc**(并行——默认) | **4.7 秒** | **978 MB** | 15,039 |
59+
| jcdc(`JCDC_THREADS=1` 单线程) | 23.3 秒 | 525 MB | 15,039 |
6060
| CFR 0.152 | 144.7 秒 | 8,245 MB | 15,012 |
6161
| fernflower(JetBrains) | 497.5 秒 | 5,108 MB | 15,039 |
6262
| Procyon 0.6.0 | 725.2 秒 | 6,232 MB | 15,011 |
@@ -70,11 +70,11 @@ Java 源码。设计参考了 fernflower/Vineflower、garlic、CFR、Procyon、K
7070
16g 堆 41.5 秒——但仍慢于 jcdc 跑完整镜像。
7171

7272
即便**单线程**,jcdc 跑完两组负载也比这里所有 Java 工具快(`rt.jar` 上
73-
10.0 秒 vs Vineflower 24.7 秒),整轮峰值内存(**426–947 MB**,对比
74-
Java 工具的 **2.2–8.7 GB**)低 2.3×–20×。默认并行管线下:`rt.jar` 上比
75-
Vineflower 快约 10 倍、比 CFR 快约 21 倍、比 Procyon 快约 45 倍、比
76-
fernflower 快约 86 倍;Java 28 镜像上比 CFR 快约 28 倍、比 fernflower 快
77-
约 98 倍、比 Procyon 快约 142 倍。各工具产出文件数的细微差异来自归类口径:
73+
9.4 秒 vs Vineflower 24.7 秒),整轮峰值内存(**424–978 MB**,对比
74+
Java 工具的 **2.2–8.7 GB**)低 2.3×–21×。默认并行管线下:`rt.jar` 上比
75+
Vineflower 快约 11 倍、比 CFR 快约 23 倍、比 Procyon 快约 48 倍、比
76+
fernflower 快约 93 倍;Java 28 镜像上比 CFR 快约 31 倍、比 fernflower 快
77+
约 106 倍、比 Procyon 快约 154 倍。各工具产出文件数的细微差异来自归类口径:
7878
jcdc 把 `$` 命名的隐藏类折叠进所属族文件(并输出 `module-info.java`),
7979
Procyon 则跳过 `package-info` 文件(`rt.jar` 上少 23 个)。(jcdc 带
8080
`-cp <jar>` 提供完整类型上下文;Java 工具从输入 jar 自行解析。)

‎crates/decompiler/src/builder.rs‎

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,36 @@ thread_local! {
2525
std::cell::RefCell::new(HashSet::new());
2626
}
2727

28+
/// The conditional branch consuming the value a store just wrote, looking
29+
/// past constant pushes (`dup; istore v; iconst_m1; if_icmpeq` is how javac
30+
/// compiles `(v = f()) != -1`). Only constants are skipped: any other
31+
/// instruction could reassign the local and make the stack value stale.
32+
fn cond_branch_ahead(ins: &[Instruction], mut i: usize) -> Option<Opcode> {
33+
loop {
34+
let op = ins.get(i)?.op;
35+
match op {
36+
Opcode::AconstNull
37+
| Opcode::IconstM1
38+
| Opcode::Iconst0
39+
| Opcode::Iconst1
40+
| Opcode::Iconst2
41+
| Opcode::Iconst3
42+
| Opcode::Iconst4
43+
| Opcode::Iconst5
44+
| Opcode::Lconst0
45+
| Opcode::Lconst1
46+
| Opcode::Fconst0
47+
| Opcode::Fconst1
48+
| Opcode::Fconst2
49+
| Opcode::Dconst0
50+
| Opcode::Dconst1
51+
| Opcode::Bipush
52+
| Opcode::Sipush => i += 1,
53+
_ => return Some(op),
54+
}
55+
}
56+
}
57+
2858
fn dup_marks_clear_at_or_above(depth: usize) {
2959
DUP_MARKS.with(|m| m.borrow_mut().retain(|&d| d < depth));
3060
}
@@ -213,6 +243,16 @@ impl<'a> Builder<'a> {
213243
{
214244
continue;
215245
}
246+
if self.localize_dup_condition(
247+
&mut stack,
248+
slot,
249+
in0.pc,
250+
next_pc,
251+
cond_branch_ahead(ins, i),
252+
) {
253+
self.store(&mut stmts, slot, in0.pc, next_pc, val)?;
254+
continue;
255+
}
216256
match self.inline_dup_store(&mut stack, slot, in0.pc, next_pc, val) {
217257
Some(left) => self.store(&mut stmts, slot, in0.pc, next_pc, left)?,
218258
None => {}
@@ -221,6 +261,16 @@ impl<'a> Builder<'a> {
221261
Opcode::Istore0 | Opcode::Istore1 | Opcode::Istore2 | Opcode::Istore3 => {
222262
let slot = (op as u8 - Opcode::Istore0 as u8) as u16;
223263
let val = pop(&mut stack)?;
264+
if self.localize_dup_condition(
265+
&mut stack,
266+
slot,
267+
in0.pc,
268+
next_pc,
269+
cond_branch_ahead(ins, i),
270+
) {
271+
self.store(&mut stmts, slot, in0.pc, next_pc, val)?;
272+
continue;
273+
}
224274
match self.inline_dup_store(&mut stack, slot, in0.pc, next_pc, val) {
225275
Some(left) => self.store(&mut stmts, slot, in0.pc, next_pc, left)?,
226276
None => {}
@@ -229,6 +279,16 @@ impl<'a> Builder<'a> {
229279
Opcode::Lstore0 | Opcode::Lstore1 | Opcode::Lstore2 | Opcode::Lstore3 => {
230280
let slot = (op as u8 - Opcode::Lstore0 as u8) as u16;
231281
let val = pop(&mut stack)?;
282+
if self.localize_dup_condition(
283+
&mut stack,
284+
slot,
285+
in0.pc,
286+
next_pc,
287+
cond_branch_ahead(ins, i),
288+
) {
289+
self.store(&mut stmts, slot, in0.pc, next_pc, val)?;
290+
continue;
291+
}
232292
match self.inline_dup_store(&mut stack, slot, in0.pc, next_pc, val) {
233293
Some(left) => self.store(&mut stmts, slot, in0.pc, next_pc, left)?,
234294
None => {}
@@ -237,6 +297,16 @@ impl<'a> Builder<'a> {
237297
Opcode::Fstore0 | Opcode::Fstore1 | Opcode::Fstore2 | Opcode::Fstore3 => {
238298
let slot = (op as u8 - Opcode::Fstore0 as u8) as u16;
239299
let val = pop(&mut stack)?;
300+
if self.localize_dup_condition(
301+
&mut stack,
302+
slot,
303+
in0.pc,
304+
next_pc,
305+
cond_branch_ahead(ins, i),
306+
) {
307+
self.store(&mut stmts, slot, in0.pc, next_pc, val)?;
308+
continue;
309+
}
240310
match self.inline_dup_store(&mut stack, slot, in0.pc, next_pc, val) {
241311
Some(left) => self.store(&mut stmts, slot, in0.pc, next_pc, left)?,
242312
None => {}
@@ -245,6 +315,16 @@ impl<'a> Builder<'a> {
245315
Opcode::Dstore0 | Opcode::Dstore1 | Opcode::Dstore2 | Opcode::Dstore3 => {
246316
let slot = (op as u8 - Opcode::Dstore0 as u8) as u16;
247317
let val = pop(&mut stack)?;
318+
if self.localize_dup_condition(
319+
&mut stack,
320+
slot,
321+
in0.pc,
322+
next_pc,
323+
cond_branch_ahead(ins, i),
324+
) {
325+
self.store(&mut stmts, slot, in0.pc, next_pc, val)?;
326+
continue;
327+
}
248328
match self.inline_dup_store(&mut stack, slot, in0.pc, next_pc, val) {
249329
Some(left) => self.store(&mut stmts, slot, in0.pc, next_pc, left)?,
250330
None => {}
@@ -257,6 +337,16 @@ impl<'a> Builder<'a> {
257337
if matches!(ins.get(i).map(|x| x.op), Some(Opcode::Monitorenter)) {
258338
continue;
259339
}
340+
if self.localize_dup_condition(
341+
&mut stack,
342+
slot,
343+
in0.pc,
344+
next_pc,
345+
cond_branch_ahead(ins, i),
346+
) {
347+
self.store(&mut stmts, slot, in0.pc, next_pc, val)?;
348+
continue;
349+
}
260350
match self.inline_dup_store(&mut stack, slot, in0.pc, next_pc, val) {
261351
Some(left) => self.store(&mut stmts, slot, in0.pc, next_pc, left)?,
262352
None => {}
@@ -1360,6 +1450,68 @@ impl<'a> Builder<'a> {
13601450
/// `this(-1, .., arguments = Arrays.copyOf(..))` — hoisting the store
13611451
/// to a statement precedes the ctor delegation: "灵活构造器" error).
13621452
/// Returns None when handled, else the untouched value for `store`.
1453+
/// `x = f(); if (x == null)` — javac's dup-arm-store idiom. The store
1454+
/// just wrote the very value the surviving dup holds, so the branch must
1455+
/// read the LOCAL. Leaving the call expression on the stack printed the
1456+
/// call a second time (`while (r.readLine() != null)` beside a body that
1457+
/// read the same stream again — MailcapFile consumed three lines per
1458+
/// iteration instead of one). Only an immediately-following conditional
1459+
/// branch is rewritten: a later use could follow a reassignment of the
1460+
/// local, where the stack value and the local genuinely diverge.
1461+
/// Parameter stores keep the inline-assignment form (`inline_dup_store`).
1462+
fn localize_dup_condition(
1463+
&self,
1464+
stack: &mut Vec<Expr>,
1465+
slot: u16,
1466+
at_pc: u16,
1467+
next_pc: u16,
1468+
next_op: Option<Opcode>,
1469+
) -> bool {
1470+
let consumes = matches!(
1471+
next_op,
1472+
Some(
1473+
Opcode::Ifeq
1474+
| Opcode::IfNe
1475+
| Opcode::IfLt
1476+
| Opcode::IfGe
1477+
| Opcode::IfGt
1478+
| Opcode::IfLe
1479+
| Opcode::IfIcmpEq
1480+
| Opcode::IfIcmpNe
1481+
| Opcode::IfIcmpLt
1482+
| Opcode::IfIcmpGe
1483+
| Opcode::IfIcmpGt
1484+
| Opcode::IfIcmpLe
1485+
| Opcode::IfAcmpeq
1486+
| Opcode::IfAcmpne
1487+
| Opcode::IfNull
1488+
| Opcode::IfNonnull
1489+
)
1490+
);
1491+
if !consumes || stack.is_empty() {
1492+
return false;
1493+
}
1494+
let marked = DUP_MARKS.with(|m| m.borrow_mut().remove(&(stack.len() - 1)));
1495+
if !marked {
1496+
return false;
1497+
}
1498+
let Some(v) = self
1499+
.vt
1500+
.at(slot, next_pc)
1501+
.or_else(|| self.vt.at(slot, at_pc))
1502+
else {
1503+
return false;
1504+
};
1505+
if self.vt.var(v).is_param {
1506+
// Put the decision back for the inline-assignment path.
1507+
DUP_MARKS.with(|m| m.borrow_mut().insert(stack.len() - 1));
1508+
return false;
1509+
}
1510+
let Some(last) = stack.last_mut() else { return false };
1511+
*last = self.local_expr(v);
1512+
true
1513+
}
1514+
13631515
fn inline_dup_store(
13641516
&self,
13651517
stack: &mut Vec<Expr>,

0 commit comments

Comments
 (0)