Skip to content

Commit

Permalink
fix generated code for stobj opcode missing target type (#246)
Browse files Browse the repository at this point in the history
also updates some incorrect expectations related to the same issue
  • Loading branch information
adrianoc committed Aug 12, 2023
1 parent a6b0541 commit 8399517
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion Cecilifier.Core.Tests/Tests/Unit/PointerTypeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ private static IEnumerable<TestCaseData> 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");
}
}
}
46 changes: 45 additions & 1 deletion Cecilifier.Core.Tests/Tests/Unit/RefAssignmentTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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()
{
Expand Down
11 changes: 8 additions & 3 deletions Cecilifier.Core/AST/AssignmentVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,7 @@ bool HandleIndexer(SyntaxNode node, LinkedListNode<string> lastInstructionLoadin
// bellow it.
AddMethodCall(ilVar, propertySymbol.GetMethod);
Context.MoveLinesToEnd(InstructionPrecedingValueToLoad, lastInstructionLoadingRhs);
OpCode opCode = propertySymbol.Type.Stind();
Context.EmitCilInstruction(ilVar, opCode);
EmitIndirectStore(propertySymbol.Type);
}
else
{
Expand All @@ -209,6 +208,12 @@ bool HandleIndexer(SyntaxNode node, LinkedListNode<string> 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);
Expand Down Expand Up @@ -249,7 +254,7 @@ private void MemberAssignment(ITypeSymbol memberType, RefKind memberRefKind, Def
{
if (NeedsIndirectStore(memberType, memberRefKind))
{
Context.EmitCilInstruction(ilVar, memberType.Stind());
EmitIndirectStore(memberType);
}
else
{
Expand Down
3 changes: 1 addition & 2 deletions Cecilifier.Core/Extensions/TypeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 8399517

Please sign in to comment.