Skip to content

Commit

Permalink
Use ExprContext.None for fake stack slots used to disable stack sched…
Browse files Browse the repository at this point in the history
…uler optimizations.

Fixes dotnet#7148.
  • Loading branch information
AlekseyTs committed Dec 1, 2015
1 parent 41950e2 commit a85bef7
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 4 deletions.
8 changes: 5 additions & 3 deletions src/Compilers/CSharp/Portable/CodeGen/Optimizer.cs
Expand Up @@ -331,6 +331,7 @@ private bool Includes(int val)
// Foo(x, ref x) <-- x cannot be a stack local as it is used in different contexts.
internal enum ExprContext
{
None,
Sideeffects,
Value,
Address,
Expand Down Expand Up @@ -494,6 +495,7 @@ protected override BoundExpression VisitExpressionWithoutStackGuard(BoundExpress

private void PushEvalStack(BoundExpression result, ExprContext context)
{
Debug.Assert(result != null || context == ExprContext.None);
_evalStack.Add(ValueTuple.Create(result, context));
}

Expand Down Expand Up @@ -879,7 +881,7 @@ public override BoundNode VisitAssignmentOperator(BoundAssignmentOperator node)
if (mayPushReceiver)
{
// push unknown value just to prevent access to stack locals.
PushEvalStack(null, ExprContext.Address);
PushEvalStack(null, ExprContext.None);
}

right = VisitExpression(node.Right, rhsContext);
Expand Down Expand Up @@ -1326,7 +1328,7 @@ public override BoundNode VisitComplexConditionalReceiver(BoundComplexConditiona

var origStack = StackDepth();

PushEvalStack(null, ExprContext.Value);
PushEvalStack(null, ExprContext.None);

var cookie = GetStackStateCookie(); // implicit goto here

Expand Down Expand Up @@ -1446,7 +1448,7 @@ public override BoundNode VisitCatchBlock(BoundCatchBlock node)
if (exceptionSourceOpt != null)
{
// runtime pushes the exception object
PushEvalStack(null, ExprContext.Value);
PushEvalStack(null, ExprContext.None);
_counter++;

// We consume it by writing into the exception source.
Expand Down
Expand Up @@ -144,6 +144,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.CodeGen
End Function

Private Sub PushEvalStack(result As BoundExpression, context As ExprContext)
Debug.Assert(result IsNot Nothing OrElse context = ExprContext.None)
_evalStack.Add(ValueTuple.Create(result, context))
End Sub

Expand Down Expand Up @@ -586,7 +587,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.CodeGen

If mayPushReceiver Then
'push unknown value just to prevent access to stack locals.
PushEvalStack(Nothing, ExprContext.Address)
PushEvalStack(Nothing, ExprContext.None)
End If

right = VisitExpression(node.Right, rhsContext)
Expand Down
44 changes: 44 additions & 0 deletions src/Compilers/VisualBasic/Test/Emit/CodeGen/CodeGenTests.vb
Expand Up @@ -13470,6 +13470,50 @@ End Module
IL_002d: call "Sub System.Console.WriteLine(Integer)"
IL_0032: ret
}
]]>)
End Sub

<Fact, WorkItem(7148, "https://github.com/dotnet/roslyn/issues/7148")>
Public Sub Issue7148()
Dim c = CompileAndVerify(
<compilation>
<file name="a.vb">
Public Class TestClass
Private _rotation As Decimal
Private Sub CalculateDimensions()
_rotation *= 180 / System.Math.PI 'This line causes '"vbc.exe" exited with code -2146232797'
End Sub

Shared Sub Main()
Dim x as New TestClass()
x._rotation = 1
x.CalculateDimensions()
System.Console.WriteLine(x._rotation)
End Sub
End Class
</file>
</compilation>, options:=TestOptions.ReleaseExe,
expectedOutput:="57.2957795130823")

c.VerifyIL("TestClass.CalculateDimensions",
<![CDATA[
{
// Code size 40 (0x28)
.maxstack 3
.locals init (Decimal& V_0)
IL_0000: ldarg.0
IL_0001: ldflda "TestClass._rotation As Decimal"
IL_0006: dup
IL_0007: stloc.0
IL_0008: ldloc.0
IL_0009: ldobj "Decimal"
IL_000e: call "Function System.Convert.ToDouble(Decimal) As Double"
IL_0013: ldc.r8 57.2957795130823
IL_001c: mul
IL_001d: newobj "Sub Decimal..ctor(Double)"
IL_0022: stobj "Decimal"
IL_0027: ret
}
]]>)
End Sub

Expand Down

0 comments on commit a85bef7

Please sign in to comment.