From 1945e6cb44a1e8ee70d794ad6e716738e951843c Mon Sep 17 00:00:00 2001
From: Alexey Andreev <konsoletyper@gmail.com>
Date: Fri, 8 May 2015 20:54:18 +0300
Subject: [PATCH] Print bytecode listing of a method when decompiler fails to
 decompile

---
 .../java/org/teavm/javascript/Decompiler.java | 20 ++++++++++++++++---
 1 file changed, 17 insertions(+), 3 deletions(-)

diff --git a/teavm-core/src/main/java/org/teavm/javascript/Decompiler.java b/teavm-core/src/main/java/org/teavm/javascript/Decompiler.java
index ca3ba41be..290cbc094 100644
--- a/teavm-core/src/main/java/org/teavm/javascript/Decompiler.java
+++ b/teavm-core/src/main/java/org/teavm/javascript/Decompiler.java
@@ -66,6 +66,7 @@ import org.teavm.model.TryCatchBlock;
 import org.teavm.model.ValueType;
 import org.teavm.model.Variable;
 import org.teavm.model.util.AsyncProgramSplitter;
+import org.teavm.model.util.ListingBuilder;
 import org.teavm.model.util.ProgramUtils;
 
 /**
@@ -260,7 +261,13 @@ public class Decompiler {
         Program program = method.getProgram();
         int[] targetBlocks = new int[program.basicBlockCount()];
         Arrays.fill(targetBlocks, -1);
-        methodNode.setBody(getRegularMethodStatement(program, targetBlocks, false).getStatement());
+        try {
+            methodNode.setBody(getRegularMethodStatement(program, targetBlocks, false).getStatement());
+        } catch (RuntimeException e) {
+            StringBuilder sb = new StringBuilder("Error decompiling method " + method.getReference() + ":\n");
+            sb.append(new ListingBuilder().buildListing(program, "  "));
+            throw new DecompilationException(sb.toString(), e);
+        }
         for (int i = 0; i < program.variableCount(); ++i) {
             methodNode.getVariables().add(program.variableAt(i).getRegister());
         }
@@ -311,8 +318,15 @@ public class Decompiler {
         AsyncProgramSplitter splitter = new AsyncProgramSplitter(classSource, splitMethods);
         splitter.split(method.getProgram());
         for (int i = 0; i < splitter.size(); ++i) {
-            AsyncMethodPart part = getRegularMethodStatement(splitter.getProgram(i), splitter.getBlockSuccessors(i),
-                    i > 0);
+            AsyncMethodPart part;
+            try {
+                part = getRegularMethodStatement(splitter.getProgram(i), splitter.getBlockSuccessors(i), i > 0);
+            } catch (RuntimeException e) {
+                StringBuilder sb = new StringBuilder("Error decompiling method " + method.getReference() +
+                        " part " + i + ":\n");
+                sb.append(new ListingBuilder().buildListing(splitter.getProgram(i), "  "));
+                throw new DecompilationException(sb.toString(), e);
+            }
             node.getBody().add(part);
         }
         Program program = method.getProgram();