Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Treat pointers as value types #159

Merged
merged 6 commits into from
Sep 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading