Skip to content

Commit

Permalink
0.5: Revert inline
Browse files Browse the repository at this point in the history
  • Loading branch information
oleg-st committed Jun 25, 2021
1 parent ba94437 commit 1f15271
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
6 changes: 6 additions & 0 deletions src/InlineMethod.Fody/Helper/OpCodeHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -316,5 +316,11 @@ public static IEnumerable<Instruction> GetTargets(Instruction instruction)
}
}
}

public static void ReplaceInstruction(Instruction target, Instruction source)
{
target.OpCode = source.OpCode;
target.Operand = source.Operand;
}
}
}
20 changes: 16 additions & 4 deletions src/InlineMethod.Fody/InlineMethodWeaver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -287,12 +287,24 @@ public void Finish()
private bool CanInline => _pushInstruction != null && _variableDefinition == null &&
(OpCodeHelper.IsLoadConst(_pushInstruction) || OpCodeHelper.IsLoadArg(_pushInstruction) || OpCodeHelper.IsLoadLoc(_pushInstruction));

private readonly List<Instruction> _inlinedInstructions = new List<Instruction>();

private VariableDefinition GetVariableDefinition()
{
if (_variableDefinition == null)
{
_variableDefinition = new VariableDefinition(_inlineMethodWeaver._parameters[_paramIndex].ParameterType);
_inlineMethodWeaver._parentMethod.Body.Variables.Add(_variableDefinition);

// revert inline
if (_inlinedInstructions.Count > 0)
{
foreach (var inlinedInstruction in _inlinedInstructions)
{
OpCodeHelper.ReplaceInstruction(inlinedInstruction, OpCodeHelper.CreateLoadLoc(_variableDefinition));
}
_inlinedInstructions.Clear();
}
}

return _variableDefinition;
Expand All @@ -302,9 +314,7 @@ private void ProcessDeferred()
{
if (_deferredInstruction != null)
{
var newInstruction = GetLdArgInstruction();
_deferredInstruction.OpCode = newInstruction.OpCode;
_deferredInstruction.Operand = newInstruction.Operand;
OpCodeHelper.ReplaceInstruction(_deferredInstruction, GetLdArgInstruction());
_deferredInstruction = null;
}
}
Expand All @@ -319,7 +329,9 @@ private Instruction GetLdArgInstruction()
{
if (CanInline)
{
return OpCodeHelper.Clone(_pushInstruction);
var ldArgInstruction = OpCodeHelper.Clone(_pushInstruction);
_inlinedInstructions.Add(ldArgInstruction);
return ldArgInstruction;
}
return OpCodeHelper.CreateLoadLoc(GetVariableDefinition());
}
Expand Down
2 changes: 1 addition & 1 deletion src/InlineMethod/InlineMethod.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<PackageOutputPath>$(SolutionDir)/../nugets</PackageOutputPath>
<PackageProjectUrl>https://github.com/oleg-st/InlineMethod.Fody</PackageProjectUrl>
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
<Version>0.4</Version>
<Version>0.5</Version>
</PropertyGroup>

<ItemGroup>
Expand Down

0 comments on commit 1f15271

Please sign in to comment.