From d672fe068a068ba5ddedf73736545b9ec4bc5b2e Mon Sep 17 00:00:00 2001 From: Alexey Andreev Date: Tue, 26 Jul 2016 22:06:40 +0300 Subject: [PATCH] Continue developing decompiler --- .../java/org/teavm/common/GraphUtils.java | 30 +++ .../teavm/wasm/decompile/ContextUtils.java | 39 ---- .../wasm/decompile/DecompileSupport.java | 161 +++++++++++++ .../teavm/wasm/decompile/WasmDecompiler.java | 211 +++++++++++++----- 4 files changed, 344 insertions(+), 97 deletions(-) delete mode 100644 core/src/main/java/org/teavm/wasm/decompile/ContextUtils.java create mode 100644 core/src/main/java/org/teavm/wasm/decompile/DecompileSupport.java diff --git a/core/src/main/java/org/teavm/common/GraphUtils.java b/core/src/main/java/org/teavm/common/GraphUtils.java index c9fdee2f1..d0e0862dc 100644 --- a/core/src/main/java/org/teavm/common/GraphUtils.java +++ b/core/src/main/java/org/teavm/common/GraphUtils.java @@ -165,6 +165,36 @@ public final class GraphUtils { return graph.build(); } + public static int[] dfs(Graph graph) { + int[] result = new int[graph.size()]; + int[] state = new int[graph.size()]; + int[] stack = new int[graph.size() * 2]; + int top = 0; + stack[top++] = 0; + int index = graph.size(); + + while (top > 0) { + int node = stack[--top]; + switch (state[node]) { + case 0: + state[node] = 1; + stack[top++] = node; + for (int successor : graph.outgoingEdges(node)) { + if (state[successor] == 0) { + stack[top++] = node; + } + } + break; + case 1: + result[node] = --index; + state[node] = 2; + break; + } + } + + return result; + } + public static void splitIrreducibleGraph(Graph graph, int[] weights, GraphSplittingBackend backend) { new IrreducibleGraphConverter().convertToReducible(graph, weights, backend); } diff --git a/core/src/main/java/org/teavm/wasm/decompile/ContextUtils.java b/core/src/main/java/org/teavm/wasm/decompile/ContextUtils.java deleted file mode 100644 index 0edf17df1..000000000 --- a/core/src/main/java/org/teavm/wasm/decompile/ContextUtils.java +++ /dev/null @@ -1,39 +0,0 @@ -/* - * Copyright 2016 Alexey Andreev. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.teavm.wasm.decompile; - -import com.carrotsearch.hppc.IntObjectMap; -import com.carrotsearch.hppc.IntObjectOpenHashMap; - -public final class ContextUtils { - private ContextUtils() { - } - - public static void withLabels(Context context, IntObjectMap