Skip to content

Commit 33c9ac3

Browse files
committed
Conversion to the new LLVM IR complete for everything except tracing
Tomorrow, testing and cleanup
1 parent 76e4d0c commit 33c9ac3

File tree

10 files changed

+528
-196
lines changed

10 files changed

+528
-196
lines changed

src/Xamarin.Android.Build.Tasks/Tasks/GeneratePackageManagerJava.cs

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -407,18 +407,9 @@ void AddEnvironment ()
407407
LLVM.IR.LlvmIrModule appConfigModule = appConfigAsmGen.Construct ();
408408

409409
var marshalMethodsState = BuildEngine4.GetRegisteredTaskObjectAssemblyLocal<MarshalMethodsState> (ProjectSpecificTaskObjectKey (GenerateJavaStubs.MarshalMethodsRegisterTaskKey), RegisteredTaskObjectLifetime.Build);
410-
MarshalMethodsNativeAssemblyGenerator marshalMethodsAsmGen;
411410
New.MarshalMethodsNativeAssemblyGenerator marshalMethodsAsmGenNew;
412411

413412
if (enableMarshalMethods) {
414-
marshalMethodsAsmGen = new MarshalMethodsNativeAssemblyGenerator (
415-
assemblyCount,
416-
uniqueAssemblyNames,
417-
marshalMethodsState?.MarshalMethods,
418-
Log,
419-
mmTracingMode
420-
);
421-
422413
marshalMethodsAsmGenNew = new New.MarshalMethodsNativeAssemblyGenerator (
423414
assemblyCount,
424415
uniqueAssemblyNames,
@@ -427,10 +418,8 @@ void AddEnvironment ()
427418
mmTracingMode
428419
);
429420
} else {
430-
marshalMethodsAsmGen = new MarshalMethodsNativeAssemblyGenerator (assemblyCount, uniqueAssemblyNames);
431421
marshalMethodsAsmGenNew = new New.MarshalMethodsNativeAssemblyGenerator (assemblyCount, uniqueAssemblyNames);
432422
}
433-
marshalMethodsAsmGen.Init ();
434423
LLVM.IR.LlvmIrModule marshalMethodsModule = marshalMethodsAsmGenNew.Construct ();
435424

436425
foreach (string abi in SupportedAbis) {
@@ -439,7 +428,6 @@ void AddEnvironment ()
439428
string marshalMethodsBaseAsmFilePath = Path.Combine (EnvironmentOutputDirectory, $"marshal_methods.{targetAbi}");
440429
string environmentLlFilePath = $"{environmentBaseAsmFilePath}.ll";
441430
string marshalMethodsLlFilePath = $"{marshalMethodsBaseAsmFilePath}.ll";
442-
string marshalMethodsLlFilePathNew = $"{marshalMethodsBaseAsmFilePath}-new.ll";
443431
AndroidTargetArch targetArch = GetAndroidTargetArchForAbi (abi);
444432

445433
using (var sw = MemoryStreamPool.Shared.CreateStreamWriter ()) {
@@ -455,20 +443,14 @@ void AddEnvironment ()
455443

456444
using (var sw = MemoryStreamPool.Shared.CreateStreamWriter ()) {
457445
try {
458-
marshalMethodsAsmGenNew.Generate (marshalMethodsModule, targetArch, sw, marshalMethodsLlFilePathNew);
446+
marshalMethodsAsmGenNew.Generate (marshalMethodsModule, targetArch, sw, marshalMethodsLlFilePath);
459447
} catch {
460448
throw;
461449
} finally {
462450
sw.Flush ();
463-
Files.CopyIfStreamChanged (sw.BaseStream, marshalMethodsLlFilePathNew);
451+
Files.CopyIfStreamChanged (sw.BaseStream, marshalMethodsLlFilePath);
464452
}
465453
}
466-
467-
using (var sw = MemoryStreamPool.Shared.CreateStreamWriter ()) {
468-
marshalMethodsAsmGen.Write (targetArch, sw, marshalMethodsLlFilePath);
469-
sw.Flush ();
470-
Files.CopyIfStreamChanged (sw.BaseStream, marshalMethodsLlFilePath);
471-
}
472454
}
473455

474456
void AddEnvironmentVariable (string name, string value)
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Reflection;
4+
5+
namespace Xamarin.Android.Tasks.LLVM.IR;
6+
7+
partial class LlvmIrModule
8+
{
9+
sealed class LlvmIrBufferManager
10+
{
11+
Dictionary<string, ulong> counters;
12+
Dictionary<object, Dictionary<string, string>> bufferVariableNames;
13+
14+
public LlvmIrBufferManager ()
15+
{
16+
counters = new Dictionary<string, ulong> (StringComparer.Ordinal);
17+
}
18+
19+
public string Allocate (StructureInstance structure, StructureMemberInfo smi, ulong size)
20+
{
21+
string baseName = $"_{structure.Info.Name}_{smi.Info.Name}";
22+
23+
if (!counters.TryGetValue (baseName, out ulong count)) {
24+
count = 0;
25+
counters.Add (baseName, count);
26+
} else {
27+
count++;
28+
counters[baseName] = count;
29+
}
30+
31+
return Register (structure, smi, $"{baseName}_{count:x}_{structure.IndexInArray:x}");
32+
}
33+
34+
public string? GetBufferVariableName (StructureInstance structure, StructureMemberInfo smi)
35+
{
36+
if (bufferVariableNames == null || bufferVariableNames.Count == 0) {
37+
return null;
38+
}
39+
40+
if (!bufferVariableNames.TryGetValue (structure.Obj, out Dictionary<string, string> members)) {
41+
return null;
42+
}
43+
44+
if (!members.TryGetValue (MakeUniqueMemberId (structure, smi), out string bufferVariableName)) {
45+
return null;
46+
}
47+
48+
return bufferVariableName;
49+
}
50+
51+
string Register (StructureInstance structure, StructureMemberInfo smi, string bufferVariableName)
52+
{
53+
if (bufferVariableNames == null) {
54+
bufferVariableNames = new Dictionary<object, Dictionary<string, string>> ();
55+
}
56+
57+
if (!bufferVariableNames.TryGetValue (structure.Obj, out Dictionary<string, string> members)) {
58+
members = new Dictionary<string, string> (StringComparer.Ordinal);
59+
bufferVariableNames.Add (structure.Obj, members);
60+
}
61+
62+
members.Add (MakeUniqueMemberId (structure, smi), bufferVariableName);
63+
return bufferVariableName;
64+
}
65+
66+
string MakeUniqueMemberId (StructureInstance structure, StructureMemberInfo smi) => $"{smi.Info.Name}_{structure.IndexInArray}";
67+
}
68+
}

src/Xamarin.Android.Build.Tasks/Utilities/LlvmIrGenerator/LlvmIrFunction.cs

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -196,30 +196,55 @@ interface ILlvmIrSavedFunctionSignatureState {}
196196

197197
class LlvmIrFunctionSignature : IEquatable<LlvmIrFunctionSignature>
198198
{
199+
public sealed class ReturnTypeAttributes
200+
{
201+
public bool? InReg;
202+
public bool? NoUndef;
203+
public bool? SignExt;
204+
public bool? ZeroExt;
205+
206+
public ReturnTypeAttributes ()
207+
{}
208+
209+
public ReturnTypeAttributes (ReturnTypeAttributes other)
210+
{
211+
InReg = other.InReg;
212+
NoUndef = other.NoUndef;
213+
SignExt = other.SignExt;
214+
ZeroExt = other.ZeroExt;
215+
}
216+
}
217+
199218
sealed class SavedSignatureState : ILlvmIrSavedFunctionSignatureState
200219
{
201220
public readonly LlvmIrFunctionSignature Owner;
202221
public readonly IList<ILlvmIrSavedFunctionParameterState> ParameterStates;
222+
public readonly ReturnTypeAttributes ReturnAttributes;
203223

204-
public SavedSignatureState (LlvmIrFunctionSignature owner, IList<ILlvmIrSavedFunctionParameterState> parameterStates)
224+
public SavedSignatureState (LlvmIrFunctionSignature owner, IList<ILlvmIrSavedFunctionParameterState> parameterStates, ReturnTypeAttributes returnAttributes)
205225
{
206226
Owner = owner;
207227
ParameterStates = parameterStates;
228+
ReturnAttributes = returnAttributes;
208229
}
209230
}
210231

232+
ReturnTypeAttributes returnAttributes;
233+
211234
public string Name { get; }
212235
public Type ReturnType { get; }
236+
public ReturnTypeAttributes ReturnAttributes => returnAttributes;
213237
public IList<LlvmIrFunctionParameter> Parameters { get; }
214238

215-
public LlvmIrFunctionSignature (string name, Type returnType, IList<LlvmIrFunctionParameter>? parameters = null)
239+
public LlvmIrFunctionSignature (string name, Type returnType, IList<LlvmIrFunctionParameter>? parameters = null, ReturnTypeAttributes? returnAttributes = null)
216240
{
217241
if (String.IsNullOrEmpty (name)) {
218242
throw new ArgumentException ("must not be null or empty", nameof (name));
219243
}
220244

221245
Name = name;
222246
ReturnType = returnType;
247+
this.returnAttributes = returnAttributes ?? new ReturnTypeAttributes ();
223248
Parameters = parameters ?? new List<LlvmIrFunctionParameter> ();
224249
}
225250

@@ -243,7 +268,9 @@ public ILlvmIrSavedFunctionSignatureState SaveState ()
243268
list.Add (parameter.SaveState ());
244269
}
245270

246-
return new SavedSignatureState (this, list.AsReadOnly ());
271+
var ret = new SavedSignatureState (this, list.AsReadOnly (), returnAttributes);
272+
returnAttributes = new ReturnTypeAttributes (returnAttributes);
273+
return ret;
247274
}
248275

249276
/// <summary>
@@ -264,8 +291,8 @@ public void RestoreState (ILlvmIrSavedFunctionSignatureState savedState)
264291
for (int i = 0; i < oldState.ParameterStates.Count; i++) {
265292
ILlvmIrSavedFunctionParameterState parameterState = oldState.ParameterStates[i];
266293
Parameters[i].RestoreState (parameterState);
267-
268294
}
295+
returnAttributes = new ReturnTypeAttributes (oldState.ReturnAttributes);
269296
}
270297

271298
public override int GetHashCode ()
@@ -382,6 +409,7 @@ public SavedFunctionState (LlvmIrFunction owner, ILlvmIrSavedFunctionSignatureSt
382409
public LlvmIrVisibility Visibility { get; set; } = LlvmIrVisibility.Default;
383410
public LlvmIrFunctionBody Body { get; }
384411
public string? Comment { get; set; }
412+
public bool ReturnsValue => Signature.ReturnType != typeof(void);
385413

386414
public LlvmIrFunction (LlvmIrFunctionSignature signature, LlvmIrFunctionAttributeSet? attributeSet = null)
387415
{

0 commit comments

Comments
 (0)