forked from ForNeVeR/Cesium
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssemblyContext.cs
More file actions
390 lines (328 loc) · 14.6 KB
/
AssemblyContext.cs
File metadata and controls
390 lines (328 loc) · 14.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
// SPDX-FileCopyrightText: 2025 Cesium contributors <https://github.com/ForNeVeR/Cesium>
//
// SPDX-License-Identifier: MIT
using System.Collections;
using System.Diagnostics;
using System.Text;
using Cesium.Ast;
using Cesium.CodeGen.Contexts.Meta;
using Cesium.CodeGen.Contexts.Utilities;
using Cesium.CodeGen.Extensions;
using Cesium.CodeGen.Ir.Declarations;
using Cesium.CodeGen.Ir.Emitting;
using Cesium.CodeGen.Ir.Lowering;
using Cesium.CodeGen.Ir.Types;
using Cesium.Core;
using Mono.Cecil;
using Mono.Cecil.Cil;
using Mono.Cecil.Rocks;
using PointerType = Cesium.CodeGen.Ir.Types.PointerType;
namespace Cesium.CodeGen.Contexts;
public class AssemblyContext
{
internal AssemblyDefinition Assembly { get; }
public TargetArchitectureSet ArchitectureSet { get; }
internal AssemblyDefinition MscorlibAssembly { get; }
internal AssemblyDefinition CesiumRuntimeAssembly { get; }
public ModuleDefinition Module { get; }
public AssemblyDefinition[] ImportAssemblies { get; }
public TypeDefinition GlobalType { get; }
internal Dictionary<string, FunctionInfo> Functions { get; } = new();
private readonly Dictionary<string, VariableInfo> _globalFields = new();
public CompilationOptions CompilationOptions { get; }
public static AssemblyContext Create(
AssemblyNameDefinition name,
CompilationOptions compilationOptions)
{
var assembly = AssemblyDefinition.CreateAssembly(name, "Primary", compilationOptions.ModuleKind);
var module = assembly.MainModule;
var assemblyContext = new AssemblyContext(assembly, module, compilationOptions);
var targetRuntime = compilationOptions.TargetRuntime;
assembly.CustomAttributes.Add(targetRuntime.GetTargetFrameworkAttribute(module));
return assemblyContext;
}
public void EmitTranslationUnit(string name, TranslationUnit translationUnit)
{
var context = new TranslationUnitContext(this, name);
var scope = context.GetInitializerScope();
var nodes = translationUnit.ToIntermediate(scope);
nodes = nodes.Select(node => BlockItemLowering.LowerDeclaration(scope, node)).ToList();
foreach (var node in nodes)
BlockItemEmitting.EmitCode(scope, node);
}
/// <summary>Do final code generation tasks, analogous to linkage.</summary>
/// <remarks>As we link code on the fly, here we only need to check there are no unlinked functions left.</remarks>
public AssemblyDefinition VerifyAndGetAssembly()
{
foreach (var (name, function) in Functions)
{
if (!function.IsDefined)
{
if (function.DllLibraryName == null) throw new CompilationException($"Function {name} not defined.");
var funcDef = function.MethodReference!.Resolve();
funcDef.Attributes = MethodAttributes.Public | MethodAttributes.PInvokeImpl | MethodAttributes.Static | MethodAttributes.HideBySig;
ModuleReference? dll = Module.ModuleReferences.FirstOrDefault(_ => _.Name == function.DllLibraryName);
if (dll == null)
{
dll = new ModuleReference(function.DllLibraryName);
Module.ModuleReferences.Add(dll);
}
string entryPoint = function.DllImportNameStrip != null ? name.Replace(function.DllImportNameStrip, null) : name;
funcDef.PInvokeInfo = new PInvokeInfo(PInvokeAttributes.NoMangle | PInvokeAttributes.SupportsLastError, entryPoint, dll);
function.IsDefined = true;
}
}
FinishGlobalInitializer();
return Assembly;
}
public const string ConstantPoolTypeName = "<ConstantPool>";
private readonly Dictionary<int, TypeReference> _stubTypesPerSize = new();
private readonly Dictionary<ByteArrayWrapper, FieldReference> _dataConstantHolders = new();
private readonly Dictionary<IGeneratedType, TypeReference> _generatedTypes = new();
private readonly Lazy<TypeDefinition> _constantPool;
private MethodDefinition? _globalInitializer;
private readonly TypeReference _runtimeCPtr;
private readonly ConversionMethodCache _cPtrConverterCache;
public MethodReference CPtrConverter(TypeReference argument) =>
_cPtrConverterCache.GetOrImportMethod(argument);
public TypeReference RuntimeVoidPtr { get; }
private readonly Lazy<MethodReference> _voidPtrConverter;
public MethodReference VoidPtrConverter => _voidPtrConverter.Value;
private readonly TypeReference _runtimeFuncPtr;
private readonly ConversionMethodCache _funcPtrConstructorCache;
public MethodReference FuncPtrConstructor(TypeReference argument) =>
_funcPtrConstructorCache.GetOrImportMethod(argument);
private AssemblyContext(
AssemblyDefinition assembly,
ModuleDefinition module,
CompilationOptions compilationOptions)
{
Assembly = assembly;
ArchitectureSet = compilationOptions.TargetArchitectureSet;
Module = module;
CompilationOptions = compilationOptions;
MscorlibAssembly = AssemblyDefinition.ReadAssembly(compilationOptions.CorelibAssembly.Value);
CesiumRuntimeAssembly = AssemblyDefinition.ReadAssembly(compilationOptions.CesiumRuntime.Value);
ImportAssemblies = compilationOptions.ImportAssemblies
.Select(x => AssemblyDefinition.ReadAssembly(x.Value))
.Union([MscorlibAssembly, CesiumRuntimeAssembly])
.Distinct().ToArray();
_constantPool = new(
() =>
{
var type = new TypeDefinition(compilationOptions.Namespace, ConstantPoolTypeName, TypeAttributes.Sealed, module.TypeSystem.Object);
module.Types.Add(type);
return type;
});
if (!string.IsNullOrWhiteSpace(compilationOptions.GlobalClassFqn))
{
var fqnComponents = compilationOptions.GlobalClassFqn.Split('.');
var typeName = fqnComponents[^1];
var typeNamespace = string.Join('.', fqnComponents.SkipLast(1));
GlobalType = new TypeDefinition(
typeNamespace,
typeName,
TypeAttributes.Class
| TypeAttributes.Public
| TypeAttributes.Abstract
| TypeAttributes.Sealed,
module.TypeSystem.Object);
module.Types.Add(GlobalType);
}
else
{
GlobalType = Module.GetType("<Module>");
}
TypeDefinition GetRuntimeType(string typeName) =>
CesiumRuntimeAssembly.GetType(typeName) ??
throw new AssertException($"Could not find type {typeName} in the runtime assembly.");
_runtimeCPtr = Module.ImportReference(GetRuntimeType(TypeSystemEx.CPtrFullTypeName));
_cPtrConverterCache = new ConversionMethodCache(
_runtimeCPtr,
ReturnType: _runtimeCPtr.MakeGenericInstanceType(_runtimeCPtr.GenericParameters.Single()),
"op_Implicit",
Module);
RuntimeVoidPtr = Module.ImportReference(GetRuntimeType(TypeSystemEx.VoidPtrFullTypeName));
_voidPtrConverter = new(() => GetImplicitCastOperator(TypeSystemEx.VoidPtrFullTypeName));
_runtimeFuncPtr = Module.ImportReference(GetRuntimeType(TypeSystemEx.FuncPtrFullTypeName));
_funcPtrConstructorCache = new ConversionMethodCache(
_runtimeFuncPtr,
ReturnType: null,
".ctor",
Module);
_importedActionDelegates = new("System", "Action", Module);
_importedFuncDelegates = new("System", "Func", Module);
MethodReference GetImplicitCastOperator(string typeName)
{
var type = GetRuntimeType(typeName);
return Module.ImportReference(type.Methods.Single(m => m.Name == "op_Implicit"));
}
}
public TypeReference RuntimeCPtr(TypeReference typeReference)
{
return _runtimeCPtr.MakeGenericInstanceType(typeReference);
}
public TypeReference RuntimeFuncPtr(TypeReference delegateTypeReference)
{
return _runtimeFuncPtr.MakeGenericInstanceType(delegateTypeReference);
}
private readonly GenericDelegateTypeCache _importedActionDelegates;
private readonly GenericDelegateTypeCache _importedFuncDelegates;
/// <summary>
/// Resolves a standard delegate type (i.e. an <see cref="Action"/> or a <see cref="Func{TResult}"/>), depending on
/// the return type.
/// </summary>
public TypeReference StandardDelegateType(TypeReference returnType, IEnumerable<TypeReference> arguments)
{
var isAction = returnType == Module.TypeSystem.Void;
var typeArguments = (isAction ? arguments : arguments.Append(returnType)).ToArray();
var typeArgumentCount = typeArguments.Length;
if (typeArgumentCount > 16)
{
throw new WipException(
493,
$"Mapping of function for argument count {typeArgumentCount} is not supported.");
}
var delegateCache = isAction ? _importedActionDelegates : _importedFuncDelegates;
var delegateType = delegateCache.GetDelegateType(typeArguments.Length);
return typeArguments.Length == 0
? delegateType
: delegateType.MakeGenericInstanceType(typeArguments);
}
internal VariableInfo? GetGlobalField(string identifier)
{
if (_globalFields.TryGetValue(identifier, out var value)) return value;
return null;
}
internal void AddAssemblyLevelField(string name, StorageClass storageClass, IType type)
{
if (_globalFields.TryGetValue(name, out var globalField))
{
if (globalField.StorageClass != StorageClass.Extern && storageClass != StorageClass.Extern)
throw new CompilationException($"Cannot add a duplicate global field named \"{name}\".");
return;
}
_globalFields.Add(name, new (storageClass, type, null));
}
public FieldDefinition? ResolveAssemblyLevelField(string name, TranslationUnitContext context)
{
if (!_globalFields.TryGetValue(name, out var type))
{
return null;
}
context.EnsureAnonymousTypeGenerated(type.Type);
return GlobalType.GetOrAddField(context, type.Type, name);
}
/// <summary>Returns either a module static constructor or a static constructor of the global type.</summary>
public MethodDefinition GetGlobalInitializer()
{
if (_globalInitializer != null) return _globalInitializer;
var methodAttributes =
MethodAttributes.Private
| MethodAttributes.HideBySig
| MethodAttributes.SpecialName
| MethodAttributes.RTSpecialName
| MethodAttributes.Static;
_globalInitializer = new MethodDefinition(".cctor", methodAttributes, Module.TypeSystem.Void);
GlobalType.Methods.Add(_globalInitializer);
return _globalInitializer;
}
private void FinishGlobalInitializer()
{
if (_globalInitializer != null)
_globalInitializer.Body.Instructions.Add(Instruction.Create(OpCodes.Ret));
}
public FieldReference GetConstantPoolReference(string stringConstant)
{
var encoding = Encoding.UTF8;
var bufferSize = encoding.GetByteCount(stringConstant) + 1;
var data = new byte[bufferSize];
var writtenBytes = encoding.GetBytes(stringConstant, data);
Debug.Assert(writtenBytes == bufferSize - 1);
return GetConstantPoolReference(data);
}
public FieldReference GetConstantPoolReference(byte[] dataConstant)
{
var wrapper = new ByteArrayWrapper(dataConstant);
if (_dataConstantHolders.TryGetValue(wrapper, out var field))
return field;
var bufferSize = dataConstant.Length;
var type = GetStubType(bufferSize);
field = GenerateFieldForDataConstant(type, dataConstant);
_dataConstantHolders.Add(wrapper, field);
return field;
}
private TypeReference GetStubType(int size)
{
if (_stubTypesPerSize.TryGetValue(size, out var typeRef))
return typeRef;
var stubStructTypeName = $"<ConstantPoolItemType{size}>";
var type = new TypeDefinition(
"",
stubStructTypeName,
TypeAttributes.Sealed | TypeAttributes.ExplicitLayout | TypeAttributes.NestedPrivate,
Module.ImportReference(new TypeReference("System", "ValueType", MscorlibAssembly.MainModule, MscorlibAssembly.MainModule.TypeSystem.CoreLibrary)))
{
PackingSize = 1,
ClassSize = size
};
_constantPool.Value.NestedTypes.Add(type);
_stubTypesPerSize.Add(size, type);
return type;
}
private FieldReference GenerateFieldForDataConstant(
TypeReference stubStructType,
byte[] contentWithTerminatingZero)
{
var number = _dataConstantHolders.Count;
var fieldName = $"ConstDataBuffer{number}";
var field = new FieldDefinition(fieldName, FieldAttributes.Static | FieldAttributes.InitOnly, stubStructType)
{
InitialValue = contentWithTerminatingZero
};
var constantPool = _constantPool.Value;
constantPool.Fields.Add(field);
return field;
}
internal void GenerateType(TranslationUnitContext context, string name, StructType type)
{
if (!_generatedTypes.ContainsKey(type))
{
var typeReference = type.StartEmit(name, context);
_generatedTypes.Add(type, typeReference);
foreach (var member in type.Members)
{
if (member.Type is StructType structType)
{
structType.EmitType(context);
}
if (member.Type is PointerType { Base: StructType structTypePtr })
{
structTypePtr.EmitType(context);
}
}
type.FinishEmit(typeReference, name, context);
}
}
internal TypeReference? GetTypeReference(IGeneratedType type)
{
return _generatedTypes.GetValueOrDefault(type);
}
private struct ByteArrayWrapper
{
private readonly byte[] _value;
private int? _hash;
public ByteArrayWrapper(byte[] value)
{
_value = value;
}
public override bool Equals(object? obj)
{
return obj is ByteArrayWrapper other && _value.AsSpan().SequenceEqual(other._value);
}
public override int GetHashCode()
{
return _hash ??= StructuralComparisons.StructuralEqualityComparer.GetHashCode(_value);
}
}
}