Skip to content

Commit

Permalink
Even more comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Miepee committed Jun 3, 2024
1 parent d79c096 commit 7a123fa
Show file tree
Hide file tree
Showing 12 changed files with 212 additions and 107 deletions.
13 changes: 13 additions & 0 deletions .idea/.idea.Underanalyzer/.idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions .idea/.idea.Underanalyzer/.idea/encodings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/.idea.Underanalyzer/.idea/indexLayout.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/.idea.Underanalyzer/.idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Underanalyzer/Decompiler/AST/ASTBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections.Generic;
using Underanalyzer.Decompiler.ControlFlow;
using Underanalyzer.Decompiler.Warnings;

namespace Underanalyzer.Decompiler.AST;

Expand Down
101 changes: 70 additions & 31 deletions Underanalyzer/Decompiler/DecompileContext.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
using System;
/*
This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/

using System;
using System.Collections.Generic;
using Underanalyzer.Decompiler.ControlFlow;
using Underanalyzer.Decompiler.Macros;
Expand All @@ -11,22 +17,22 @@ namespace Underanalyzer.Decompiler;
public class DecompileContext
{
/// <summary>
/// The game context this decompile context belongs to.
/// The game context this <see cref="DecompileContext"/> belongs to.
/// </summary>
public IGameContext GameContext { get; }

/// <summary>
/// The specific code entry within the game this decompile context belongs to.
/// The specific code entry within the game this <see cref="DecompileContext"/> belongs to.
/// </summary>
public IGMCode Code { get; private set; }

/// <summary>
/// The decompilation settings to be used for this decompile context in its operation.
/// The decompilation settings to be used for this <see cref="DecompileContext"/> in its operation.
/// </summary>/
public IDecompileSettings Settings { get; private set; }

/// <summary>
/// Any warnings produced throughout the decompilation process.
/// A list of warnings produced throughout the decompilation process.
/// </summary>
public List<IDecompileWarning> Warnings { get; } = new();

Expand All @@ -35,43 +41,63 @@ public class DecompileContext
internal bool GMLv2 { get => GameContext.UsingGMLv2; }

// Data structures used (and re-used) for decompilation, as well as tests
internal List<Block> Blocks { get; set; }
internal Dictionary<int, Block> BlocksByAddress { get; set; }
internal List<Fragment> FragmentNodes { get; set; }
internal List<Loop> LoopNodes { get; set; }
internal List<Block> ShortCircuitBlocks { get; set; }
internal List<ShortCircuit> ShortCircuitNodes { get; set; }
internal List<StaticInit> StaticInitNodes { get; set; }
internal List<TryCatch> TryCatchNodes { get; set; }
internal List<Nullish> NullishNodes { get; set; }
internal List<BinaryBranch> BinaryBranchNodes { get; set; }
internal HashSet<IControlFlowNode> SwitchEndNodes { get; set; }
internal List<Switch.SwitchDetectionData> SwitchData { get; set; }
internal HashSet<Block> SwitchContinueBlocks { get; set; }
internal HashSet<Block> SwitchIgnoreJumpBlocks { get; set; }
internal List<Switch> SwitchNodes { get; set; }
internal Dictionary<Block, Loop> BlockSurroundingLoops { get; set; }
internal Dictionary<Block, int> BlockAfterLimits { get; set; }
internal List<GMEnum> EnumDeclarations { get; set; } = new();
internal Dictionary<string, GMEnum> NameToEnumDeclaration { get; set; } = new();
internal GMEnum UnknownEnumDeclaration { get; set; } = null;
// See about changing these to not be nullable?
internal List<Block>? Blocks { get; set; }
internal Dictionary<int, Block>? BlocksByAddress { get; set; }
internal List<Fragment>? FragmentNodes { get; set; }
internal List<Loop>? LoopNodes { get; set; }
internal List<Block>? ShortCircuitBlocks { get; set; }
internal List<ShortCircuit>? ShortCircuitNodes { get; set; }
internal List<StaticInit>? StaticInitNodes { get; set; }
internal List<TryCatch>? TryCatchNodes { get; set; }
internal List<Nullish>? NullishNodes { get; set; }
internal List<BinaryBranch>? BinaryBranchNodes { get; set; }
internal HashSet<IControlFlowNode>? SwitchEndNodes { get; set; }
internal List<Switch.SwitchDetectionData>? SwitchData { get; set; }
internal HashSet<Block>? SwitchContinueBlocks { get; set; }
internal HashSet<Block>? SwitchIgnoreJumpBlocks { get; set; }
internal List<Switch>? SwitchNodes { get; set; }
internal Dictionary<Block, Loop>? BlockSurroundingLoops { get; set; }
internal Dictionary<Block, int>? BlockAfterLimits { get; set; }
internal List<GMEnum>? EnumDeclarations { get; set; } = new();
internal Dictionary<string, GMEnum>? NameToEnumDeclaration { get; set; } = new();
internal GMEnum? UnknownEnumDeclaration { get; set; } = null;
internal int UnknownEnumReferenceCount { get; set; } = 0;

public DecompileContext(IGameContext gameContext, IGMCode code, IDecompileSettings settings = null)
/// <summary>
/// Initializes a new instance of the <see cref="DecompileContext"/> class.
/// </summary>
/// <param name="gameContext">The game context.</param>
/// <param name="code">The code entry.</param>
/// <param name="settings">The decompilation settings that should be used.</param>
public DecompileContext(IGameContext gameContext, IGMCode code, IDecompileSettings settings)
{
GameContext = gameContext;
Code = code;
Settings = settings ?? new DecompileSettings();
Settings = settings;
}

/// <summary>
/// <inheritdoc cref="DecompileContext(IGameContext, IGMCode, IDecompileSettings)"/>
/// </summary>
/// <param name="gameContext"><inheritdoc cref="DecompileContext(IGameContext, IGMCode, IDecompileSettings)"/></param>
/// <param name="code"><see cref="DecompileContext(IGameContext, IGMCode, IDecompileSettings)"/></param>
public DecompileContext(IGameContext gameContext, IGMCode code) : this(gameContext, code, new DecompileSettings())
{ }


// Constructor used for control flow tests
internal DecompileContext(IGMCode code)
{
Code = code;
GameContext = new Mock.GameContextMock();
Settings = new DecompileSettings();
}

// Solely decompiles control flow from the code entry
/// <summary>
/// Solely decompiles control flow from the code entry .
/// </summary>
/// <exception cref="DecompilerException">When a decompiler error occured.</exception>
private void DecompileControlFlow()
{
try
Expand All @@ -93,13 +119,18 @@ private void DecompileControlFlow()
{
throw new DecompilerException($"Decompiler error during control flow analysis: {ex.Message}", ex);
}
// Should probably throw something else, 'cause this should basically never happen.
catch (Exception ex)
{
throw new DecompilerException($"Unexpected exception thrown in decompiler during control flow analysis: {ex.Message}", ex);
}
}

// Decompiles the AST from the code entry4
/// <summary>
/// Decompiles the AST from the code entry.
/// </summary>
/// <returns>The AST</returns>
/// <exception cref="DecompilerException">When a decompiler error occured.</exception>
private AST.IStatementNode DecompileAST()
{
try
Expand All @@ -110,13 +141,19 @@ private AST.IStatementNode DecompileAST()
{
throw new DecompilerException($"Decompiler error during AST building: {ex.Message}", ex);
}
// See in DecompileControlFlow
catch (Exception ex)
{
throw new DecompilerException($"Unexpected exception thrown in decompiler during AST building: {ex.Message}", ex);
}
}

// Decompiles the AST from the code entry

/// <summary>
/// Cleans up a given AST.
/// </summary>
/// <param name="ast">The AST that should be cleaned up.</param>
/// <returns>A new cleaned AST.</returns>
/// <exception cref="DecompilerException">When a decompiler error occured.</exception>
private AST.IStatementNode CleanupAST(AST.IStatementNode ast)
{
try
Expand All @@ -142,6 +179,7 @@ private AST.IStatementNode CleanupAST(AST.IStatementNode ast)
/// <summary>
/// Decompiles the code entry, and returns the AST output.
/// </summary>
/// <returns>The AST.</returns>
public AST.IStatementNode DecompileToAST()
{
DecompileControlFlow();
Expand All @@ -152,6 +190,7 @@ public AST.IStatementNode DecompileToAST()
/// <summary>
/// Decompiles the code entry, and returns the string output.
/// </summary>
/// <returns>The decompiled code.</returns>
public string DecompileToString()
{
AST.IStatementNode ast = DecompileToAST();
Expand Down
10 changes: 9 additions & 1 deletion Underanalyzer/Decompiler/DecompileSettings.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
namespace Underanalyzer.Decompiler;
/*
This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/

namespace Underanalyzer.Decompiler;

/// <summary>
/// Describes the necessary settings properties for the decompiler.
/// </summary>
public interface IDecompileSettings
{
// TODO: more settings :3. Also do some better phrasing for some of these.

/// <summary>
/// String used to indent, e.g. tabs or some amount of spaces generally.
/// </summary>
Expand Down
Loading

0 comments on commit 7a123fa

Please sign in to comment.