[change] 小幅重构生成Native2Managed及AdjustorThunk的代码,消除重复代码

main
walon 2023-08-31 19:22:07 +08:00
parent 58c7073073
commit 47e43b0afa
1 changed files with 13 additions and 17 deletions

View File

@ -555,7 +555,7 @@ static void __M2N_{method.CreateCallSigName()}(const MethodInfo* method, uint16_
return s.ToString(); return s.ToString();
} }
public string GenerateCopyArgumentToInterpreterStack(List<ParamInfo> paramInfos, bool adjustorThunk) public string GenerateCopyArgumentToInterpreterStack(List<ParamInfo> paramInfos)
{ {
StringBuilder s = new StringBuilder(); StringBuilder s = new StringBuilder();
int index = 0; int index = 0;
@ -565,11 +565,11 @@ static void __M2N_{method.CreateCallSigName()}(const MethodInfo* method, uint16_
{ {
if (param.Type.NeedExpandValue()) if (param.Type.NeedExpandValue())
{ {
s.AppendLine($"\targs[__ARG_OFFSET_{index}__].u64 = {(index == 0 && adjustorThunk ? $"(uint64_t)((uintptr_t)__arg{index} + sizeof(Il2CppObject))" : $"__arg{index}")};"); s.AppendLine($"\targs[__ARG_OFFSET_{index}__].u64 = __arg{index};");
} }
else else
{ {
s.AppendLine($"\t*({param.Type.GetTypeName()}*)(args + __ARG_OFFSET_{index}__) = {(index == 0 && adjustorThunk ? $"(uintptr_t)__arg{index} + sizeof(Il2CppObject)" : $"__arg{index}")};"); s.AppendLine($"\t*({param.Type.GetTypeName()}*)(args + __ARG_OFFSET_{index}__) = __arg{index};");
} }
} }
else else
@ -581,33 +581,29 @@ static void __M2N_{method.CreateCallSigName()}(const MethodInfo* method, uint16_
return s.ToString(); return s.ToString();
} }
public void GenerateNative2ManagedMethod(MethodDesc method, List<string> lines) private void GenerateNative2ManagedMethod0(MethodDesc method, bool adjustorThunk, List<string> lines)
{ {
string paramListStr = string.Join(", ", method.ParamInfos.Select(p => $"{p.Type.GetTypeName()} __arg{p.Index}").Concat(new string[] { "const MethodInfo* method" })); string paramListStr = string.Join(", ", method.ParamInfos.Select(p => $"{p.Type.GetTypeName()} __arg{p.Index}").Concat(new string[] { "const MethodInfo* method" }));
lines.Add($@" lines.Add($@"
static {method.ReturnInfo.Type.GetTypeName()} __N2M_{method.CreateCallSigName()}({paramListStr}) static {method.ReturnInfo.Type.GetTypeName()} __N2M_{(adjustorThunk ? "AdjustorThunk_" : "")}{method.CreateCallSigName()}({paramListStr})
{{ {{
{(adjustorThunk ? "__arg0 += sizeof(Il2CppObject);" : "")}
{GenerateArgumentSizeAndOffset(method.ParamInfos)} {GenerateArgumentSizeAndOffset(method.ParamInfos)}
StackObject args[__TOTAL_ARG_SIZE__]; StackObject args[__TOTAL_ARG_SIZE__];
{GenerateCopyArgumentToInterpreterStack(method.ParamInfos, false)} {GenerateCopyArgumentToInterpreterStack(method.ParamInfos)}
{(method.ReturnInfo.IsVoid ? "Interpreter::Execute(method, args, nullptr);" : $"{method.ReturnInfo.Type.GetTypeName()} ret; Interpreter::Execute(method, args, &ret); return ret;")} {(method.ReturnInfo.IsVoid ? "Interpreter::Execute(method, args, nullptr);" : $"{method.ReturnInfo.Type.GetTypeName()} ret; Interpreter::Execute(method, args, &ret); return ret;")}
}} }}
"); ");
} }
public void GenerateNative2ManagedMethod(MethodDesc method, List<string> lines)
{
GenerateNative2ManagedMethod0(method, false, lines);
}
public void GenerateAdjustThunkMethod(MethodDesc method, List<string> lines) public void GenerateAdjustThunkMethod(MethodDesc method, List<string> lines)
{ {
string paramListStr = string.Join(", ", method.ParamInfos.Select(p => $"{p.Type.GetTypeName()} __arg{p.Index}").Concat(new string[] { "const MethodInfo* method" })); GenerateNative2ManagedMethod0(method, true, lines);
lines.Add($@"
static {method.ReturnInfo.Type.GetTypeName()} __N2M_AdjustorThunk_{method.CreateCallSigName()}({paramListStr})
{{
{GenerateArgumentSizeAndOffset(method.ParamInfos)}
StackObject args[__TOTAL_ARG_SIZE__];
{GenerateCopyArgumentToInterpreterStack(method.ParamInfos, true)}
{(method.ReturnInfo.IsVoid ? "Interpreter::Execute(method, args, nullptr);" : $"{method.ReturnInfo.Type.GetTypeName()} ret; Interpreter::Execute(method, args, &ret); return ret;")}
}}
");
} }
} }
} }