Skip to content

Commit

Permalink
(#354) CodeGen: move further on CPtr cast operator
Browse files Browse the repository at this point in the history
  • Loading branch information
ForNeVeR committed Nov 11, 2023
1 parent 6614b32 commit e64f5d5
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 6 deletions.
Empty file.
15 changes: 11 additions & 4 deletions Cesium.CodeGen/Contexts/AssemblyContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ public AssemblyDefinition VerifyAndGetAssembly()
private MethodDefinition? _globalInitializer;

private readonly TypeReference _runtimeCPtr;
private readonly Lazy<MethodReference> _cPtrConverter;
public MethodReference CPtrConverter => _cPtrConverter.Value;

public TypeReference RuntimeVoidPtr { get; }
private readonly TypeReference _runtimeFuncPtr;

Expand Down Expand Up @@ -136,11 +139,15 @@ TypeDefinition GetRuntimeType(string typeName) =>
_importedActionDelegates = new("System", "Action", Module);
_importedFuncDelegates = new("System", "Func", Module);

_voidPtrConverter = new(() =>
_voidPtrConverter = new(() => GetImplicitCastOperator(TypeSystemEx.VoidPtrFullTypeName));
_cPtrConverter = new(() => throw new WipException(WipException.ToDo, "TODO: Make the cast operators specialized by types, introduce a cache or something."));
return;

MethodReference GetImplicitCastOperator(string typeName)
{
var voidPtrType = GetRuntimeType(TypeSystemEx.VoidPtrFullTypeName);
return Module.ImportReference(voidPtrType.Methods.Single(m => m.Name == "op_Implicit"));
});
var type = GetRuntimeType(typeName);
return Module.ImportReference(type.Methods.Single(m => m.Name == "op_Implicit"));
}
}

public TypeReference RuntimeCPtr(TypeReference typeReference)
Expand Down
4 changes: 3 additions & 1 deletion Cesium.CodeGen/Ir/Expressions/TypeCastExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ public void EmitTo(IEmitScope scope)
Add(OpCodes.Conv_I);
else if (TargetType is InteropType iType)
{
if (iType.UnderlyingType.FullName == TypeSystemEx.VoidPtrFullTypeName)
var type = iType.UnderlyingType;
if (type.FullName == TypeSystemEx.VoidPtrFullTypeName
|| type.IsGenericInstance && type.GetElementType().FullName == TypeSystemEx.CPtrFullTypeName)
{
Add(OpCodes.Conv_I); // TODO: Should only emit if required.
scope.Method.Body.Instructions.Add(
Expand Down
4 changes: 3 additions & 1 deletion Cesium.CodeGen/Ir/Types/InteropType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,10 @@ public MethodReference GetConvertCall(AssemblyContext context)
if (UnderlyingType.IsGenericInstance)
{
var parent = UnderlyingType.GetElementType();
if (parent.FullName == TypeSystemEx.CPtrFullTypeName)
return context.CPtrConverter;

throw new WipException(WipException.ToDo, "Cannot import converter methods for CPtr or FuncPtr, yet.");
throw new WipException(WipException.ToDo, "Cannot import the converter method for FuncPtr, yet.");
}

throw new AssertException(
Expand Down

0 comments on commit e64f5d5

Please sign in to comment.