Skip to content

Commit

Permalink
Treat pointers as value types (#159)
Browse files Browse the repository at this point in the history
* Treat pointers as value types

Handle generics for pointers too

formatting

Implement requested changes

* Remove unnecessary support for using pointers as type arguments

* Pointers are not exactly value types

* IsValueTypeLike

* Fix for get accessors

* Revert changes to JudgeSpecificsByOriginalType
  • Loading branch information
ds5678 committed Sep 14, 2024
1 parent 18ef1e5 commit 5261733
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 1 deletion.
4 changes: 4 additions & 0 deletions Il2CppInterop.Generator/Extensions/AsmResolverExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ private static Parameter GetArgument(this ILProcessor instructions, int argument

public static bool IsNested(this TypeSignature type) => type.DeclaringType is not null;

public static bool IsPointerLike(this TypeSignature type) => type is PointerTypeSignature or ByReferenceTypeSignature;

public static bool IsValueTypeLike(this TypeSignature type) => type.IsValueType || type.IsPointerLike();

public static bool IsSystemEnum(this GenericParameterConstraint constraint) => constraint.Constraint?.FullName is "System.Enum";

public static bool IsSystemValueType(this GenericParameterConstraint constraint) => constraint.Constraint?.FullName is "System.ValueType";
Expand Down
17 changes: 17 additions & 0 deletions Il2CppInterop.Generator/Extensions/ILGeneratorEx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ public static void EmitObjectStore(this ILProcessor body, TypeSignature original
body.Add(OpCodes.Call, imports.IL2CPP_ManagedStringToIl2Cpp.Value);
body.Add(OpCodes.Call, imports.WriteFieldWBarrier);
}
else if (originalType.IsPointerLike())
{
Debug.Assert(newType.IsPointerLike());
body.AddLoadArgument(argumentIndex);
body.Add(OpCodes.Stobj, newType.ToTypeDefOrRef());
body.Add(OpCodes.Pop);
}
else if (originalType.IsValueType)
{
var typeSpecifics = enclosingType.AssemblyContext.GlobalContext.JudgeSpecificsByOriginalType(originalType);
Expand Down Expand Up @@ -182,6 +189,11 @@ public static void EmitObjectToPointer(this ILProcessor body, TypeSignature orig
body.Add(OpCodes.Conv_I);
}
}
else if (originalType.IsPointerLike())
{
Debug.Assert(newType.IsPointerLike());
body.AddLoadArgument(argumentIndex);
}
else if (originalType.IsValueType)
{
if (newType.IsValueType)
Expand Down Expand Up @@ -285,6 +297,11 @@ public static void EmitPointerToObject(this ILProcessor body, TypeSignature orig
{
// do nothing
}
else if (originalReturnType.IsPointerLike())
{
Debug.Assert(convertedReturnType.IsPointerLike());
body.Add(OpCodes.Ldloc, pointerVariable);
}
else if (originalReturnType.IsValueType)
{
if (convertedReturnType.IsValueType)
Expand Down
3 changes: 3 additions & 0 deletions Il2CppInterop.Generator/Utils/FieldAccessorGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ public static void MakeGetter(FieldDefinition field, FieldRewriteContext fieldCo
getterBody.EmitPointerToObject(fieldContext.OriginalField.Signature!.FieldType, property.Signature.ReturnType,
fieldContext.DeclaringType, local0, !field.IsStatic, false);

if (property.Signature.ReturnType.IsPointerLike())
getterBody.Add(OpCodes.Ldind_I);

getterBody.Add(OpCodes.Ret);

property.GetMethod = getter;
Expand Down
2 changes: 1 addition & 1 deletion Il2CppInterop.Generator/Utils/UnstripGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public static void GenerateInvokerMethodBody(MethodDefinition newMethod, FieldDe
}

body.Add(OpCodes.Call, delegateType.Methods.Single(it => it.Name == "Invoke"));
if (!newMethod.Signature!.ReturnType.IsValueType)
if (!newMethod.Signature!.ReturnType.IsValueTypeLike())
{
var pointerVar = new CilLocalVariable(imports.Module.IntPtr());
newMethod.CilMethodBody.LocalVariables.Add(pointerVar);
Expand Down

0 comments on commit 5261733

Please sign in to comment.