From f8249a5ef046febf759f48a39464358fbc13ea3c Mon Sep 17 00:00:00 2001 From: HoLLy Date: Sat, 23 Nov 2019 22:23:24 +0100 Subject: [PATCH] Fix blocks not being printed when wrapped in AssignmentExpressions --- WasmLib/Decompilation/GenericDecompiler.cs | 8 ++++---- .../Decompilation/SourceCode/AssignmentExpression.cs | 5 ++++- WasmLib/Decompilation/SourceCode/GenericExpression.cs | 2 +- WasmLib/Decompilation/SourceCode/IHasBlocks.cs | 10 ++++++++++ 4 files changed, 19 insertions(+), 6 deletions(-) create mode 100644 WasmLib/Decompilation/SourceCode/IHasBlocks.cs diff --git a/WasmLib/Decompilation/GenericDecompiler.cs b/WasmLib/Decompilation/GenericDecompiler.cs index 92701d4..857c37d 100644 --- a/WasmLib/Decompilation/GenericDecompiler.cs +++ b/WasmLib/Decompilation/GenericDecompiler.cs @@ -141,17 +141,17 @@ namespace WasmLib.Decompilation } // TODO: support comments - if (expression is GenericExpression ge && ge.Block1 != null) { + if (expression is IHasBlocks blocks && blocks.Block1 != null) { output.WriteLine(new string('\t', tabCount) + stringRepresentation + " {"); - OutputAsCode(ge.Block1, output, tabCount + 1, varCounts); + OutputAsCode(blocks.Block1, output, tabCount + 1, varCounts); - if (ge.Block2 == null) { + if (blocks.Block2 == null) { output.WriteLine(new string('\t', tabCount) + "}"); } else { output.WriteLine(new string('\t', tabCount) + "} else {"); - OutputAsCode(ge.Block2, output, tabCount + 1, varCounts); + OutputAsCode(blocks.Block2, output, tabCount + 1, varCounts); output.WriteLine(new string('\t', tabCount) + "}"); } } diff --git a/WasmLib/Decompilation/SourceCode/AssignmentExpression.cs b/WasmLib/Decompilation/SourceCode/AssignmentExpression.cs index c9ee174..71e4a18 100644 --- a/WasmLib/Decompilation/SourceCode/AssignmentExpression.cs +++ b/WasmLib/Decompilation/SourceCode/AssignmentExpression.cs @@ -1,13 +1,16 @@ +using Rivers; using WasmLib.FileFormat; using WasmLib.Utils; namespace WasmLib.Decompilation.SourceCode { - public class AssignmentExpression : IExpression + public class AssignmentExpression : IExpression, IHasBlocks { public IExpression BaseExpression { get; } public VariableReferenceExpression Reference { get; } public string Name { get; } + public Graph? Block1 => (BaseExpression as IHasBlocks)?.Block1; + public Graph? Block2 => (BaseExpression as IHasBlocks)?.Block2; public AssignmentExpression(IExpression baseExpression, ValueKind type, int index) { diff --git a/WasmLib/Decompilation/SourceCode/GenericExpression.cs b/WasmLib/Decompilation/SourceCode/GenericExpression.cs index 6021dd5..128cf5f 100644 --- a/WasmLib/Decompilation/SourceCode/GenericExpression.cs +++ b/WasmLib/Decompilation/SourceCode/GenericExpression.cs @@ -5,7 +5,7 @@ using WasmLib.Utils; namespace WasmLib.Decompilation.SourceCode { - public class GenericExpression : IExpression + public class GenericExpression : IExpression, IHasBlocks { public IntermediateInstruction BaseInstruction { get; } public IExpression[]? Parameters { get; } diff --git a/WasmLib/Decompilation/SourceCode/IHasBlocks.cs b/WasmLib/Decompilation/SourceCode/IHasBlocks.cs new file mode 100644 index 0000000..38c32ce --- /dev/null +++ b/WasmLib/Decompilation/SourceCode/IHasBlocks.cs @@ -0,0 +1,10 @@ +using Rivers; + +namespace WasmLib.Decompilation.SourceCode +{ + public interface IHasBlocks + { + Graph? Block1 { get; } + Graph? Block2 { get; } + } +} \ No newline at end of file