Skip to content

Commit 497ad94

Browse files
committed
Initialize static variables and manually add gc roots
1 parent 3c5b229 commit 497ad94

3 files changed

Lines changed: 59 additions & 5 deletions

File tree

il2cpp/GeneratorContext.cs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,13 +267,58 @@ internal class GeneratorContext
267267
public readonly StringGenerator StrGen = new StringGenerator();
268268
private readonly HashSet<string> UsedTypeNames = new HashSet<string>();
269269
private readonly HashSet<string> UsedMethodNames = new HashSet<string>();
270+
private readonly Dictionary<string, List<Tuple<string, bool>>> InitFldsMap = new Dictionary<string, List<Tuple<string, bool>>>();
270271
private uint TypeIDCounter;
271272

272273
public GeneratorContext(TypeManager typeMgr)
273274
{
274275
TypeMgr = typeMgr;
275276
}
276277

278+
public void AddStaticField(string typeName, string sfldName, bool hasRef)
279+
{
280+
if (!InitFldsMap.TryGetValue(typeName, out var nameSet))
281+
{
282+
nameSet = new List<Tuple<string, bool>>();
283+
InitFldsMap.Add(typeName, nameSet);
284+
}
285+
nameSet.Add(new Tuple<string, bool>(sfldName, hasRef));
286+
}
287+
288+
private CompileUnit GenInitUnit(Dictionary<string, string> transMap)
289+
{
290+
CompileUnit unit = new CompileUnit();
291+
unit.Name = "il2cppInit";
292+
293+
CodePrinter prtGC = new CodePrinter();
294+
CodePrinter prtInit = new CodePrinter();
295+
foreach (var kv in InitFldsMap)
296+
{
297+
unit.ImplDepends.Add(transMap[kv.Key]);
298+
299+
foreach (var item in kv.Value)
300+
{
301+
if (item.Item2)
302+
prtGC.AppendFormatLine("IL2CPP_ADD_ROOT({0});", item.Item1);
303+
prtInit.AppendFormatLine("{0} = {{}};", item.Item1);
304+
}
305+
}
306+
307+
prtGC.AppendLine("il2cpp_CommitRoots();");
308+
309+
CodePrinter prtFunc = new CodePrinter();
310+
prtFunc.AppendLine("void il2cpp_InitVariables()\n{");
311+
++prtFunc.Indents;
312+
prtFunc.Append(prtGC.ToString());
313+
prtFunc.Append(prtInit.ToString());
314+
--prtFunc.Indents;
315+
prtFunc.AppendLine("}");
316+
317+
unit.ImplCode = prtFunc.ToString();
318+
319+
return unit;
320+
}
321+
277322
public GenerateResult Generate()
278323
{
279324
var unitMap = new Dictionary<string, CompileUnit>();
@@ -294,6 +339,10 @@ public GenerateResult Generate()
294339
if (strTyX != null)
295340
StrGen.Generate(unitMap, GetTypeID(strTyX));
296341

342+
// 生成初始化单元
343+
var unitInit = GenInitUnit(transMap);
344+
unitMap[unitInit.Name] = unitInit;
345+
297346
return new GenerateResult(this, unitMap.Values.ToList(), transMap);
298347
}
299348

il2cpp/Il2cppContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ public static void SaveToFolder(string folder, List<CompileUnit> units, HashSet<
105105
// 生成编译脚本
106106
StringBuilder sb = new StringBuilder();
107107
sb.AppendLine("@echo off");
108-
sb.AppendFormat("BuildTheCode -addcflags \"-DGC_THREADS\" {0} il2cpp.cpp il2cppICall.cpp", addParams);
108+
sb.AppendFormat("BuildTheCode -addcflags \"-DGC_THREADS\" {0} il2cpp.cpp il2cppICall.cpp il2cppInit.cpp", addParams);
109109
foreach (string unitName in unitNames)
110110
sb.AppendFormat(" {0}.cpp", unitName);
111111
sb.AppendLine();

il2cpp/TypeGenerator.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ public TypeGenerator(GeneratorContext genContext, TypeX tyX)
1919
public CompileUnit Generate()
2020
{
2121
CompileUnit unit = new CompileUnit();
22-
unit.Name = GenContext.GetTypeName(CurrType, false);
22+
string strTypeName = GenContext.GetTypeName(CurrType, false);
23+
unit.Name = strTypeName;
2324

2425
// 重排字段
2526
var fields = LayoutFields(out var sfields);
@@ -61,13 +62,13 @@ public CompileUnit Generate()
6162
unit.DeclDepends.Add(strBaseTypeName);
6263

6364
prtDecl.AppendFormatLine("struct {0} : {1}",
64-
GenContext.GetTypeName(CurrType),
65+
strTypeName,
6566
strBaseTypeName);
6667
}
6768
else
6869
{
6970
prtDecl.AppendFormatLine("struct {0}",
70-
GenContext.GetTypeName(CurrType));
71+
strTypeName);
7172
}
7273

7374
prtDecl.AppendLine("{");
@@ -187,15 +188,19 @@ public CompileUnit Generate()
187188
{
188189
RefValueTypeDecl(unit, sfldX.FieldType);
189190

191+
string sfldName = GenContext.GetFieldName(sfldX);
190192
string fldDecl = string.Format("{0} {1};",
191193
GenContext.GetTypeName(sfldX.FieldType),
192-
GenContext.GetFieldName(sfldX));
194+
sfldName);
193195

194196
prtDecl.AppendFormatLine("// {0} -> {1}",
195197
Helper.EscapeString(sfldX.DeclType.GetNameKey()),
196198
Helper.EscapeString(sfldX.GetReplacedNameKey()));
197199
prtDecl.AppendLine("extern " + fldDecl);
198200
prtImpl.AppendLine(fldDecl);
201+
202+
bool hasRef = GenContext.IsRefOrContainsRef(GenContext.GetTypeBySig(sfldX.FieldType));
203+
GenContext.AddStaticField(strTypeName, sfldName, hasRef);
199204
}
200205

201206
// 生成类型判断函数

0 commit comments

Comments
 (0)