Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for VB.NET cached delegate initialization with closures #2844

Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2011-2016 Siegfried Pammer
// Copyright (c) 2011-2016 Siegfried Pammer
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
Expand Down Expand Up @@ -55,6 +55,10 @@ public void Run(Block block, BlockTransformContext context)
{
continue;
}
if (CachedDelegateInitializationVBWithClosure(inst))
{
continue;
}
}
}
}
Expand Down Expand Up @@ -236,5 +240,48 @@ bool CachedDelegateInitializationVB(IfInstruction inst)
inst.ReplaceWith(new StLoc(s, delegateConstruction));
return true;
}

/// <summary>
/// if (comp.o(ldobj delegateType(ldflda CachedAnonMethodDelegate(ldloc closure)) != ldnull)) Block {
/// stloc s(ldobj delegateType(ldflda CachedAnonMethodDelegate(ldloc closure)))
/// } else Block {
/// stloc s(stobj delegateType(ldflda CachedAnonMethodDelegate(ldloc closure), DelegateConstruction))
/// }
/// =>
/// stloc s(DelegateConstruction)
/// </summary>
bool CachedDelegateInitializationVBWithClosure(IfInstruction inst)
{
if (!(inst.TrueInst is Block trueInst && inst.FalseInst is Block falseInst))
return false;
if (trueInst.Instructions.Count != 1 || falseInst.Instructions.Count != 1)
return false;
if (!(trueInst.Instructions[0].MatchStLoc(out var s, out var trueInitValue)
&& falseInst.Instructions[0].MatchStLoc(s, out var falseInitValue)))
{
return false;
}
if (s.Kind != VariableKind.StackSlot || s.StoreCount != 2 || s.LoadCount != 1)
return false;
if (!(falseInitValue is StObj stobj) || !(trueInitValue is LdObj ldobj))
return false;
if (!(stobj.Value is NewObj delegateConstruction))
return false;
if (!stobj.Target.MatchLdFlda(out var target1, out var field1)
|| !ldobj.Target.MatchLdFlda(out var target2, out var field2)
|| !field1.Equals(field2) || !target1.Match(target2).Success)
{
return false;
}
if (!inst.Condition.MatchCompNotEqualsNull(out ILInstruction left))
siegfriedpammer marked this conversation as resolved.
Show resolved Hide resolved
return false;
if (!ldobj.Match(left).Success)
return false;
if (!DelegateConstruction.MatchDelegateConstruction(delegateConstruction, out _, out _, out _, true))
return false;
context.Step("CachedDelegateInitializationVBWithClosure", inst);
inst.ReplaceWith(new StLoc(s, delegateConstruction));
return true;
}
}
}