From 8399517bf98a1bf978d9604ba524479c09d06aff Mon Sep 17 00:00:00 2001 From: Adriano Carlos Verona Date: Sat, 12 Aug 2023 16:55:14 -0400 Subject: [PATCH] fix generated code for stobj opcode missing target type (#246) also updates some incorrect expectations related to the same issue --- .../CustomValueTypesInstantiationTests.cs | 2 +- .../Tests/Unit/PointerTypeTests.cs | 2 +- .../Tests/Unit/RefAssignmentTests.cs | 46 ++++++++++++++++++- Cecilifier.Core/AST/AssignmentVisitor.cs | 11 +++-- Cecilifier.Core/Extensions/TypeExtensions.cs | 3 +- 5 files changed, 56 insertions(+), 8 deletions(-) diff --git a/Cecilifier.Core.Tests/Tests/Unit/CustomValueTypesInstantiationTests.cs b/Cecilifier.Core.Tests/Tests/Unit/CustomValueTypesInstantiationTests.cs index 248bf882..a9c9627a 100644 --- a/Cecilifier.Core.Tests/Tests/Unit/CustomValueTypesInstantiationTests.cs +++ b/Cecilifier.Core.Tests/Tests/Unit/CustomValueTypesInstantiationTests.cs @@ -32,7 +32,7 @@ public void SimpleStoreToOutStructParameter(string toStore) Assert.That(result.GeneratedCode.ReadToEnd(), Contains.Substring( @"il_M_3.Emit(OpCodes.Ldarg_1); il_M_3.Emit(OpCodes.Ldarg_2); - il_M_3.Emit(OpCodes.Stobj);")); + il_M_3.Emit(OpCodes.Stobj, st_myStruct_0);")); } [Test] diff --git a/Cecilifier.Core.Tests/Tests/Unit/PointerTypeTests.cs b/Cecilifier.Core.Tests/Tests/Unit/PointerTypeTests.cs index 6f575160..9214fd42 100644 --- a/Cecilifier.Core.Tests/Tests/Unit/PointerTypeTests.cs +++ b/Cecilifier.Core.Tests/Tests/Unit/PointerTypeTests.cs @@ -56,7 +56,7 @@ private static IEnumerable PointerAssignmentTypesToTest() yield return new TestCaseData("ulong", "Stind_I8").SetName("ulong"); yield return new TestCaseData("float", "Stind_R4").SetName("float"); yield return new TestCaseData("double", "Stind_R8").SetName("double"); - yield return new TestCaseData("S", "Stobj").SetName("custom-struct"); + yield return new TestCaseData("S", "Stobj, st_S_0").SetName("custom-struct"); } } } diff --git a/Cecilifier.Core.Tests/Tests/Unit/RefAssignmentTests.cs b/Cecilifier.Core.Tests/Tests/Unit/RefAssignmentTests.cs index 65d10e58..acae2782 100644 --- a/Cecilifier.Core.Tests/Tests/Unit/RefAssignmentTests.cs +++ b/Cecilifier.Core.Tests/Tests/Unit/RefAssignmentTests.cs @@ -57,7 +57,7 @@ public void TestRefParameterAssignment(string assignmentExpression, string expec } [Test] - public void TesRefParameterDeference() + public void TestRefParameterDeference() { var result = RunCecilifier("void M(ref int r) { int i; i = r + 42; }"); var cecilifiedCode = result.GeneratedCode.ReadToEnd(); @@ -74,6 +74,50 @@ public void TesRefParameterDeference() """)); } + [Test] + public void TestAssign_NewInstanceOfValueType_ToParameter() + { + var result = RunCecilifier("void M(ref S s) { s = new S(); } struct S { public S() {} }"); + var cecilifiedCode = result.GeneratedCode.ReadToEnd(); + + Assert.That(cecilifiedCode, Does.Match(""" + //s = new S\(\); + (\s+il_M_\d+\.Emit\(OpCodes\.)Ldarg_1\); + \1Newobj, ctor_S_1\); + \1Stobj, st_S_0\); + """)); + } + + [Test] + public void TestAssign_NewInstanceOfValueType_ByRefIndexer() + { + var result = RunCecilifier(""" + struct S { public S() {} } + class Foo + { + S s; + ref S this[int i] => ref s; + + void DoIt(Foo other) + { + other[0] = new S(); + } + } + """); + + var cecilifiedCode = result.GeneratedCode.ReadToEnd(); + + Assert.That(cecilifiedCode, Does.Match(""" + //other\[0\] = new S\(\); + (\s+il_doIt_\d+\.Emit\(OpCodes\.)Ldarg_1\); + \1Ldc_I4, 0\); + \1Callvirt, m_get_9\); + \1Newobj, ctor_S_1\); + \1Stobj, st_S_0\); + \1Ret\); + """)); + } + [Test] public void TesRefFieldAssignment() { diff --git a/Cecilifier.Core/AST/AssignmentVisitor.cs b/Cecilifier.Core/AST/AssignmentVisitor.cs index 90c203bf..fe73638e 100644 --- a/Cecilifier.Core/AST/AssignmentVisitor.cs +++ b/Cecilifier.Core/AST/AssignmentVisitor.cs @@ -197,8 +197,7 @@ bool HandleIndexer(SyntaxNode node, LinkedListNode lastInstructionLoadin // bellow it. AddMethodCall(ilVar, propertySymbol.GetMethod); Context.MoveLinesToEnd(InstructionPrecedingValueToLoad, lastInstructionLoadingRhs); - OpCode opCode = propertySymbol.Type.Stind(); - Context.EmitCilInstruction(ilVar, opCode); + EmitIndirectStore(propertySymbol.Type); } else { @@ -209,6 +208,12 @@ bool HandleIndexer(SyntaxNode node, LinkedListNode lastInstructionLoadin return true; } + private void EmitIndirectStore(ITypeSymbol typeBeingStored) + { + var indirectStoreOpCode = typeBeingStored.Stind(); + Context.EmitCilInstruction(ilVar, indirectStoreOpCode, indirectStoreOpCode == OpCodes.Stobj ? Context.TypeResolver.Resolve(typeBeingStored.ElementTypeSymbolOf()) : null); + } + private void PropertyAssignment(IdentifierNameSyntax node, IPropertySymbol property) { property.EnsurePropertyExists(Context, node); @@ -249,7 +254,7 @@ private void MemberAssignment(ITypeSymbol memberType, RefKind memberRefKind, Def { if (NeedsIndirectStore(memberType, memberRefKind)) { - Context.EmitCilInstruction(ilVar, memberType.Stind()); + EmitIndirectStore(memberType); } else { diff --git a/Cecilifier.Core/Extensions/TypeExtensions.cs b/Cecilifier.Core/Extensions/TypeExtensions.cs index 6fb7a18c..184a65d0 100644 --- a/Cecilifier.Core/Extensions/TypeExtensions.cs +++ b/Cecilifier.Core/Extensions/TypeExtensions.cs @@ -48,8 +48,7 @@ public static string MakeGenericInstanceType(this string type, string genericTyp INamedTypeSymbol { IsGenericType: true, OriginalDefinition: { ContainingNamespace.Name: "System", Name: "Span" } } ns => ns.TypeArguments[0], IPointerTypeSymbol ptr => ptr.PointedAtType, IArrayTypeSymbol array => array.ElementType, - - _ => throw new ArgumentException($"{type.Name} not supported.", nameof(type)) + _ => type }; public static uint SizeofArrayLikeItemElement(this ITypeSymbol type)