hybridclr_unity/Editor/MethodBridge/PlatformGeneratorUniversal3...

76 lines
3.6 KiB
C#
Raw Normal View History

2022-09-22 08:56:07 +08:00
using dnlib.DotNet;
2022-10-17 12:16:18 +08:00
using HybridCLR.Editor.ABI;
2022-09-22 08:56:07 +08:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
namespace HybridCLR.Editor.MethodBridge
2022-09-22 08:56:07 +08:00
{
2022-10-17 12:16:18 +08:00
public class PlatformGeneratorUniversal32 : PlatformGeneratorBase
2022-09-22 08:56:07 +08:00
{
2022-10-17 12:16:18 +08:00
public override PlatformABI PlatformABI { get; } = PlatformABI.Universal32;
2022-09-22 08:56:07 +08:00
//protected override TypeInfo CreateValueType(TypeSig type, bool returnValue)
//{
// (int typeSize, int typeAligment) = ComputeSizeAndAligmentOfArch32(type);
// int actualAliment = typeAligment <= 4 ? 1 : 8;
// return CreateGeneralValueType(type, typeSize, actualAliment);
//}
2022-10-17 12:16:18 +08:00
public override void GenerateManaged2NativeMethod(MethodDesc method, List<string> lines)
2022-09-22 08:56:07 +08:00
{
string paramListStr = string.Join(", ", method.ParamInfos.Select(p => $"{p.Type.GetTypeName()} __arg{p.Index}").Concat(new string[] { "const MethodInfo* method" }));
2022-10-17 12:16:18 +08:00
string paramNameListStr = string.Join(", ", method.ParamInfos.Select(p => p.Managed2NativeParamValue(this.PlatformABI)).Concat(new string[] { "method" }));
2022-09-22 08:56:07 +08:00
lines.Add($@"
// {method.MethodDef}
static void __M2N_{method.CreateCallSigName()}(const MethodInfo* method, uint16_t* argVarIndexs, StackObject* localVarBase, void* ret)
{{
typedef {method.ReturnInfo.Type.GetTypeName()} (*NativeMethod)({paramListStr});
{(!method.ReturnInfo.IsVoid ? $"*({method.ReturnInfo.Type.GetTypeName()}*)ret = " : "")}((NativeMethod)(method->methodPointerCallByInterp))({paramNameListStr});
2022-09-22 08:56:07 +08:00
}}
");
}
2022-10-17 12:16:18 +08:00
public override void GenerateNative2ManagedMethod(MethodDesc method, List<string> lines)
2022-09-22 08:56:07 +08:00
{
2022-10-17 12:16:18 +08:00
int totalQuadWordNum = method.ParamInfos.Count + method.ReturnInfo.GetParamSlotNum(this.PlatformABI);
2022-09-22 08:56:07 +08:00
string paramListStr = string.Join(", ", method.ParamInfos.Select(p => $"{p.Type.GetTypeName()} __arg{p.Index}").Concat(new string[] { "const MethodInfo* method" }));
lines.Add($@"
// {method.MethodDef}
static {method.ReturnInfo.Type.GetTypeName()} __N2M_{method.CreateCallSigName()}({paramListStr})
{{
2022-10-17 12:16:18 +08:00
StackObject args[{Math.Max(totalQuadWordNum, 1)}] = {{{string.Join(", ", method.ParamInfos.Select(p => p.Native2ManagedParamValue(this.PlatformABI)))} }};
2022-09-22 08:56:07 +08:00
StackObject* ret = {(method.ReturnInfo.IsVoid ? "nullptr" : "args + " + method.ParamInfos.Count)};
Interpreter::Execute(method, args, ret);
{(!method.ReturnInfo.IsVoid ? $"return *({method.ReturnInfo.Type.GetTypeName()}*)ret;" : "")}
}}
");
}
2022-10-17 12:16:18 +08:00
public override void GenerateAdjustThunkMethod(MethodDesc method, List<string> lines)
2022-09-22 08:56:07 +08:00
{
2022-10-17 12:16:18 +08:00
int totalQuadWordNum = method.ParamInfos.Count + method.ReturnInfo.GetParamSlotNum(this.PlatformABI);
2022-09-22 08:56:07 +08:00
string paramListStr = string.Join(", ", method.ParamInfos.Select(p => $"{p.Type.GetTypeName()} __arg{p.Index}").Concat(new string[] { "const MethodInfo* method" }));
lines.Add($@"
// {method.MethodDef}
static {method.ReturnInfo.Type.GetTypeName()} __N2M_AdjustorThunk_{method.CreateCallSigName()}({paramListStr})
{{
2022-10-17 12:16:18 +08:00
StackObject args[{Math.Max(totalQuadWordNum, 1)}] = {{{string.Join(", ", method.ParamInfos.Select(p => (p.Index == 0 ? $"(uint64_t)(*(uint8_t**)&__arg{p.Index} + sizeof(Il2CppObject))" : p.Native2ManagedParamValue(this.PlatformABI))))} }};
2022-09-22 08:56:07 +08:00
StackObject* ret = {(method.ReturnInfo.IsVoid ? "nullptr" : "args + " + method.ParamInfos.Count)};
Interpreter::Execute(method, args, ret);
{(!method.ReturnInfo.IsVoid ? $"return *({method.ReturnInfo.Type.GetTypeName()}*)ret;" : "")}
}}
");
}
}
}