Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
16 changes: 14 additions & 2 deletions src/FastExpressionCompiler/FastExpressionCompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1697,17 +1697,28 @@ public static Result TryCollectInfo(ref CompilerContext context, Expression expr
case ExpressionType.Index:
var indexExpr = (IndexExpression)expr;
var precedingTryCount = context.TryExpressionCount;
var precedingIndexJumps = context.GotoTargets.Count;
foreach (var indexArg in indexExpr.GetArgs())
if ((r = TryCollectInfo(ref context, indexArg, nestedLambda, ref rootNestedLambdas, scopeIdx)) != Result.OK)
return r;
if (context.TryExpressionCount != precedingTryCount)
if (context.TryExpressionCount != precedingTryCount || context.GotoTargets.Count != precedingIndexJumps)
context.MarkComplexArguments(indexExpr);
if (indexExpr.Object == null)
return r;
expr = indexExpr.Object;
enterable = false;
continue;

case ExpressionType.ArrayIndex:
var arrayIndex = (BinaryExpression)expr;
if ((r = TryCollectInfo(ref context, arrayIndex.Left, nestedLambda, ref rootNestedLambdas, scopeIdx)) != Result.OK)
return r;
var precedingArrayJumps = context.GotoTargets.Count;
r = TryCollectInfo(ref context, arrayIndex.Right, nestedLambda, ref rootNestedLambdas, scopeIdx);
if (context.GotoTargets.Count != precedingArrayJumps)
context.MarkComplexArguments(arrayIndex);
return r;

case ExpressionType.Try:
{
++context.TryExpressionCount;
Expand Down Expand Up @@ -2504,7 +2515,8 @@ public static bool TryEmit(Expression expr, ILGenerator il, ref CompilerContext
var arrayFlags = ParentFlags.IndexAccess | ParentFlags.AssignmentRightValue;
if (!TryEmit(arrIndexExpr.Left, il, ref context, arrayFlags))
return false;
var arrayVar = (context.TryExpressionCount != 0) ? EmitStoreLocalVariable(il, arrIndexExpr.Left.Type) : -1;
var arrayVar = context.TryExpressionCount != 0 || context.HasComplexArguments(arrIndexExpr)
? EmitStoreLocalVariable(il, arrIndexExpr.Left.Type) : -1;
if (!TryEmit(arrIndexExpr.Right, il, ref context, arrayFlags))
return false;
if (arrayVar != -1)
Expand Down
2 changes: 2 additions & 0 deletions test/FastExpressionCompiler.TestsRunner.Net472/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ static void RunLightExpressionTests(object state)
t.Run(new LightExpression.UnitTests.QuoteIdentityRegressionTests());
t.Run(new LightExpression.UnitTests.ArrayBoundConversionRegressionTests());
t.Run(new LightExpression.UnitTests.EmptyRuntimeVariablesTests());
t.Run(new LightExpression.UnitTests.IndexJumpOperandTests());
t.Run(new LightExpression.UnitTests.CheckedFloatingArithmeticRegressionTests());
t.Run(new LightExpression.UnitTests.CollectionInitializerExceptionTests());
t.Run(new LightExpression.UnitTests.NestedLambdaValidationTests());
Expand Down Expand Up @@ -198,6 +199,7 @@ static void RunLightExpressionTests(object state)
fecTests.Run(new QuoteIdentityRegressionTests());
fecTests.Run(new ArrayBoundConversionRegressionTests());
fecTests.Run(new EmptyRuntimeVariablesTests());
fecTests.Run(new IndexJumpOperandTests());
fecTests.Run(new CheckedFloatingArithmeticRegressionTests());
fecTests.Run(new CollectionInitializerExceptionTests());
fecTests.Run(new NestedLambdaValidationTests());
Expand Down
2 changes: 2 additions & 0 deletions test/FastExpressionCompiler.TestsRunner/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ public static void Main(string[] args)
lt.Run(new LightExpression.UnitTests.QuoteIdentityRegressionTests());
lt.Run(new LightExpression.UnitTests.ArrayBoundConversionRegressionTests());
lt.Run(new LightExpression.UnitTests.EmptyRuntimeVariablesTests());
lt.Run(new LightExpression.UnitTests.IndexJumpOperandTests());
lt.Run(new LightExpression.UnitTests.CheckedFloatingArithmeticRegressionTests());
lt.Run(new LightExpression.UnitTests.CollectionInitializerExceptionTests());
lt.Run(new LightExpression.UnitTests.NestedLambdaValidationTests());
Expand Down Expand Up @@ -224,6 +225,7 @@ public static void Main(string[] args)
st.Run(new QuoteIdentityRegressionTests());
st.Run(new ArrayBoundConversionRegressionTests());
st.Run(new EmptyRuntimeVariablesTests());
st.Run(new IndexJumpOperandTests());
st.Run(new CheckedFloatingArithmeticRegressionTests());
st.Run(new CollectionInitializerExceptionTests());
st.Run(new NestedLambdaValidationTests());
Expand Down
41 changes: 41 additions & 0 deletions test/FastExpressionCompiler.UnitTests/IndexJumpOperandTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System;
using S = System.Linq.Expressions.Expression;
#if LIGHT_EXPRESSION
namespace FastExpressionCompiler.LightExpression.UnitTests;
#else
namespace FastExpressionCompiler.UnitTests;
#endif

public struct IndexJumpOperandTests : ITestX
{
public sealed class Cell { public int Value=7;public int this[int i] {get=>Value+i;set=>Value=value;} }
public void Run(TestRun t) { Index_jumps_discard_receivers(t); Index_assignment_jumps_do_not_write(t); }
public void Index_jumps_discard_receivers(TestContext t)
{
var leave=S.Parameter(typeof(bool));var target=S.Label(typeof(int));
var index=S.Condition(leave,S.Goto(target,S.Constant(19),typeof(int)),S.Constant(0));
foreach(var arrayKind in new[]{0,1,2,3})
{
System.Linq.Expressions.Expression access;
if(arrayKind==0)access=S.ArrayIndex(S.Constant(new[]{7}),index);
else if(arrayKind==1)access=S.ArrayAccess(S.Constant(new[]{7}),index);
else if(arrayKind==2)access=S.ArrayAccess(S.Constant(new[,]{{7}}),index,S.Constant(0));
else access=S.MakeIndex(S.Constant(new Cell()),typeof(Cell).GetProperty("Item"),new[]{index});
var tree=S.Lambda<Func<bool,int>>(S.Block(access,S.Label(target,S.Constant(8))),leave);
foreach(var flags in new[]{CompilerFlags.Default,CompilerFlags.NoInvocationLambdaInlining,CompilerFlags.EnableDelegateDebugInfo})
{
var fast=tree.FromSysExpression().CompileFast(true,flags);if(!t.IsNotNull(fast))return;
foreach(var input in new[]{false,true})t.AreEqual(tree.Compile()(input),fast(input));
}
}
}
public void Index_assignment_jumps_do_not_write(TestContext t)
{
var leave=S.Parameter(typeof(bool));var cell=S.Parameter(typeof(Cell));var target=S.Label(typeof(int));
var index=S.Condition(leave,S.Goto(target,S.Constant(19),typeof(int)),S.Constant(0));
var tree=S.Lambda<Func<Cell,bool,int>>(S.Block(S.Assign(S.MakeIndex(cell,typeof(Cell).GetProperty("Item"),new[]{index}),S.Constant(11)),S.Label(target,S.Constant(8))),cell,leave);
var fast=tree.FromSysExpression().CompileFast(true);if(!t.IsNotNull(fast))return;
foreach(var input in new[]{false,true})
{var a=new Cell();var b=new Cell();t.AreEqual(tree.Compile()(a,input),fast(b,input));t.AreEqual(a.Value,b.Value);}
}
}
Loading