Skip to content

Commit

Permalink
(#354) CodeGen: proper FuncPtr conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
ForNeVeR committed Nov 19, 2023
1 parent 10d1cd7 commit 193c090
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,13 @@
IL_0001: ret

System.Int32 <Module>::main()
Locals:
Cesium.Runtime.FuncPtr`1<System.Func`1<System.Int32>> V_0
IL_0000: ldloca V_0
IL_0004: ldftn System.Int32 <Module>::myFunc()
IL_000a: conv.i
IL_000b: call System.Int32 Test::Func(Cesium.Runtime.FuncPtr`1<System.Func`1<System.Int32>>)
IL_0010: ldc.i4.1
IL_0011: sub
IL_0012: ret
IL_0000: ldftn System.Int32 <Module>::myFunc()
IL_0006: conv.i
IL_0007: newobj System.Void Cesium.Runtime.FuncPtr`1<System.Func`1<System.Int32>>::.ctor(System.Void*)
IL_000c: call System.Int32 Test::Func(Cesium.Runtime.FuncPtr`1<System.Func`1<System.Int32>>)
IL_0011: ldc.i4.1
IL_0012: sub
IL_0013: ret

System.Int32 <Module>::<SyntheticEntrypoint>()
Locals:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,13 @@
IL_0001: ret

System.Int32 <Module>::main()
Locals:
Cesium.Runtime.FuncPtr`1<System.Func`1<System.Int32>> V_0
IL_0000: ldloca V_0
IL_0004: ldftn System.Int32 <Module>::myFunc()
IL_000a: conv.i
IL_000b: call System.Int32 Test::Func(Cesium.Runtime.FuncPtr`1<System.Func`1<System.Int32>>)
IL_0010: ldc.i4.1
IL_0011: sub
IL_0012: ret
IL_0000: ldftn System.Int32 <Module>::myFunc()
IL_0006: conv.i
IL_0007: newobj System.Void Cesium.Runtime.FuncPtr`1<System.Func`1<System.Int32>>::.ctor(System.Void*)
IL_000c: call System.Int32 Test::Func(Cesium.Runtime.FuncPtr`1<System.Func`1<System.Int32>>)
IL_0011: ldc.i4.1
IL_0012: sub
IL_0013: ret

System.Int32 <Module>::<SyntheticEntrypoint>()
Locals:
Expand Down
17 changes: 4 additions & 13 deletions Cesium.CodeGen/Ir/Types/InteropType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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:
Expand Down

0 comments on commit 193c090

Please sign in to comment.