diff --git a/Cesium.CodeGen.Tests/verified/CodeGenNetInteropTests.FuncPtrInterop_architecture=Dynamic.verified.txt b/Cesium.CodeGen.Tests/verified/CodeGenNetInteropTests.FuncPtrInterop_architecture=Dynamic.verified.txt index f287a68d..6df99378 100644 --- a/Cesium.CodeGen.Tests/verified/CodeGenNetInteropTests.FuncPtrInterop_architecture=Dynamic.verified.txt +++ b/Cesium.CodeGen.Tests/verified/CodeGenNetInteropTests.FuncPtrInterop_architecture=Dynamic.verified.txt @@ -6,15 +6,13 @@ IL_0001: ret System.Int32 ::main() - Locals: - Cesium.Runtime.FuncPtr`1> V_0 - IL_0000: ldloca V_0 - IL_0004: ldftn System.Int32 ::myFunc() - IL_000a: conv.i - IL_000b: call System.Int32 Test::Func(Cesium.Runtime.FuncPtr`1>) - IL_0010: ldc.i4.1 - IL_0011: sub - IL_0012: ret + IL_0000: ldftn System.Int32 ::myFunc() + IL_0006: conv.i + IL_0007: newobj System.Void Cesium.Runtime.FuncPtr`1>::.ctor(System.Void*) + IL_000c: call System.Int32 Test::Func(Cesium.Runtime.FuncPtr`1>) + IL_0011: ldc.i4.1 + IL_0012: sub + IL_0013: ret System.Int32 ::() Locals: diff --git a/Cesium.CodeGen.Tests/verified/CodeGenNetInteropTests.FuncPtrInterop_architecture=Wide.verified.txt b/Cesium.CodeGen.Tests/verified/CodeGenNetInteropTests.FuncPtrInterop_architecture=Wide.verified.txt index f287a68d..6df99378 100644 --- a/Cesium.CodeGen.Tests/verified/CodeGenNetInteropTests.FuncPtrInterop_architecture=Wide.verified.txt +++ b/Cesium.CodeGen.Tests/verified/CodeGenNetInteropTests.FuncPtrInterop_architecture=Wide.verified.txt @@ -6,15 +6,13 @@ IL_0001: ret System.Int32 ::main() - Locals: - Cesium.Runtime.FuncPtr`1> V_0 - IL_0000: ldloca V_0 - IL_0004: ldftn System.Int32 ::myFunc() - IL_000a: conv.i - IL_000b: call System.Int32 Test::Func(Cesium.Runtime.FuncPtr`1>) - IL_0010: ldc.i4.1 - IL_0011: sub - IL_0012: ret + IL_0000: ldftn System.Int32 ::myFunc() + IL_0006: conv.i + IL_0007: newobj System.Void Cesium.Runtime.FuncPtr`1>::.ctor(System.Void*) + IL_000c: call System.Int32 Test::Func(Cesium.Runtime.FuncPtr`1>) + IL_0011: ldc.i4.1 + IL_0012: sub + IL_0013: ret System.Int32 ::() Locals: diff --git a/Cesium.CodeGen/Ir/Types/InteropType.cs b/Cesium.CodeGen/Ir/Types/InteropType.cs index eaa93783..058500d3 100644 --- a/Cesium.CodeGen/Ir/Types/InteropType.cs +++ b/Cesium.CodeGen/Ir/Types/InteropType.cs @@ -36,16 +36,12 @@ internal record InteropType(TypeReference UnderlyingType) : IType public void EmitConversion(IEmitScope scope, IExpression expression) { - void EmitExprAndGetPtr() - { - expression.EmitTo(scope); - scope.AddInstruction(OpCodes.Conv_I); // TODO: Should only emit if required. - } + expression.EmitTo(scope); + scope.AddInstruction(OpCodes.Conv_I); // TODO: Should only emit if required. var assemblyContext = scope.AssemblyContext; if (UnderlyingType.FullName == TypeSystemEx.VoidPtrFullTypeName) { - EmitExprAndGetPtr(); scope.AddInstruction(OpCodes.Call, assemblyContext.VoidPtrConverter); return; } @@ -56,18 +52,13 @@ void EmitExprAndGetPtr() switch (parent.FullName) { case TypeSystemEx.CPtrFullTypeName: - EmitExprAndGetPtr(); scope.AddInstruction( OpCodes.Call, assemblyContext.CPtrConverter(typeInstance.GenericArguments.Single())); break; case TypeSystemEx.FuncPtrFullTypeName: - var funcPtrVariable = new VariableDefinition(UnderlyingType); - scope.Method.Body.Variables.Add(funcPtrVariable); - scope.AddInstruction(OpCodes.Ldloca, funcPtrVariable); // TODO: Use common mechanism to efficiently address local variables, use ldloca.s when necessary - EmitExprAndGetPtr(); - Instruction.Create( - OpCodes.Call, + scope.AddInstruction( + OpCodes.Newobj, assemblyContext.FuncPtrConstructor(typeInstance.GenericArguments.Single())); break; default: