-
Notifications
You must be signed in to change notification settings - Fork 74
Expand file tree
/
Copy pathILScanner.cs
More file actions
1018 lines (914 loc) · 45.7 KB
/
ILScanner.cs
File metadata and controls
1018 lines (914 loc) · 45.7 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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using Cosmos.IL2CPU.Extensions;
using IL2CPU.API;
using IL2CPU.API.Attribs;
using XSharp.Assembler;
using static XSharp.x86.Register;
namespace Cosmos.IL2CPU
{
public class ScannerQueueItem
{
public MemberInfo Item { get; }
public string QueueReason { get; }
public List<string> SourceItems { get; set; }
public ScannerQueueItem(MemberInfo aMemberInfo, string aQueueReason, List<string> aSourceItems = null)
{
Item = aMemberInfo;
QueueReason = aQueueReason;
SourceItems = aSourceItems;
}
public override string ToString()
{
return Item.MemberType + " " + Item.ToString();
}
}
internal class ILScanner : IDisposable
{
public Action<Exception> LogException = null;
public Action<string> LogWarning = null;
protected ILReader mReader;
protected AppAssembler mAsmblr;
// List of asssemblies found during scan. We cannot use the list of loaded
// assemblies because the loaded list includes compilers, etc, and also possibly
// other unused assemblies. So instead we collect a list of assemblies as we scan.
internal List<Assembly> mUsedAssemblies = new List<Assembly>();
protected HashSet<MemberInfo> mItems = new HashSet<MemberInfo>(new MemberInfoComparer());
protected List<object> mItemsList = new List<object>();
// Contains items to be scanned, both types and methods
protected Queue<ScannerQueueItem> mQueue = new Queue<ScannerQueueItem>();
// Virtual methods are nasty and constantly need to be rescanned for
// overriding methods in new types, so we keep track of them separately.
// They are also in the main mItems and mQueue.
protected HashSet<MethodBase> mVirtuals = new HashSet<MethodBase>();
protected IDictionary<MethodBase, uint> mMethodUIDs = new Dictionary<MethodBase, uint>();
protected IDictionary<Type, uint> mTypeUIDs = new Dictionary<Type, uint>();
protected PlugManager mPlugManager = null;
// Logging
// Only use for debugging and profiling.
protected bool mLogEnabled = false;
protected string mMapPathname;
protected TextWriter mLogWriter;
protected struct LogItem
{
public string SrcType;
public object Item;
}
protected Dictionary<object, List<LogItem>> mLogMap;
public ILScanner(AppAssembler aAsmblr, TypeResolver typeResolver, Action<Exception> aLogException, Action<string> aLogWarning)
{
mAsmblr = aAsmblr;
mReader = new ILReader();
LogException = aLogException;
LogWarning = aLogWarning;
mPlugManager = new PlugManager(LogException, LogWarning, typeResolver);
VTablesImplRefs.GetTypeId = GetTypeUID; // we need this to figure out which ids object, valuetype and enum have in the vmt
}
public bool EnableLogging(string aPathname)
{
mLogMap = new Dictionary<object, List<LogItem>>();
mMapPathname = aPathname;
mLogEnabled = true;
// be sure that file could be written, to prevent exception on Dispose call, cause we could not make Task log in it
try
{
File.CreateText(aPathname).Dispose();
}
catch
{
return false;
}
return true;
}
protected void Queue(MemberInfo aItem, object aSrc, string aSrcType, List<string> aSourceItems = null)
{
CompilerHelpers.Debug($"Enqueing: {aItem.DeclaringType?.Name ?? ""}.{aItem.Name} from {aSrc}");
if (aItem == null)
{
throw new ArgumentNullException(nameof(aItem));
}
//TODO: fix this, as each label/symbol should also contain an assembly specifier.
//if ((xMemInfo != null) && (xMemInfo.DeclaringType != null)
// && (xMemInfo.DeclaringType.FullName == "System.ThrowHelper")
// && (xMemInfo.DeclaringType.Assembly.GetName().Name != "mscorlib"))
//{
// System.ThrowHelper exists in MS .NET twice...
// Its an internal class that exists in both mscorlib and system assemblies.
// They are separate types though, so normally the scanner scans both and
// then we get conflicting labels. MS included it twice to make exception
// throwing code smaller. They are internal though, so we cannot
// reference them directly and only via finding them as they come along.
// We find it here, not via QueueType so we only check it here. Later
// we might have to checkin QueueType also.
// So now we accept both types, but emit code for only one. This works
// with the current Yasm assembler as we resolve by name in the assembler.
// However with other assemblers this approach may not work.
// If AssemblerYASM adds assembly name to the label, this will allow
// both to exist as they do in BCL.
// So in the future we might be able to remove this hack, or change
// how it works.
//
// Do nothing
//
//}
/*else*/
if (!mItems.Contains(aItem))
{
if (mLogEnabled)
{
LogMapPoint(aSrc, aSrcType, aItem);
}
mItems.Add(aItem);
mItemsList.Add(aItem);
if (aSourceItems != null)
{
if (aSrc is MethodBase xMethodBaseSrc)
{
// aSrc = xMethodBaseSrc.DeclaringType + "::" + aSrc;
aSourceItems.Add(DataMember.FilterStringForIncorrectChars(LabelName.GetFullName(xMethodBaseSrc)));
}
mQueue.Enqueue(new ScannerQueueItem(aItem, aSrcType, aSourceItems));
}
else
{
mQueue.Enqueue(new ScannerQueueItem(aItem, aSrcType));
}
}
}
public void Execute(MethodBase aStartMethod, IEnumerable<Assembly> plugsAssemblies)
{
if (aStartMethod == null)
{
throw new ArgumentNullException(nameof(aStartMethod));
}
// TODO: Investigate using MS CCI
// Need to check license, as well as in profiler
// http://cciast.codeplex.com/
#region Description
// Methodology
//
// Ok - we've done the scanner enough times to know it needs to be
// documented super well so that future changes won't inadvertently
// break undocumented and unseen requirements.
//
// We've tried many approaches including recursive and additive scanning.
// They typically end up being inefficient, overly complex, or both.
//
// -We would like to scan all types/methods so we can plug them.
// -But we can't scan them until we plug them, because we will scan things
// that plugs would remove/change the paths of.
// -Plugs may also call methods which are also plugged.
// -We cannot resolve plugs ahead of time but must do on the fly during
// scanning.
// -TODO: Because we do on the fly resolution, we need to add explicit
// checking of plug classes and err when public methods are found that
// do not resolve. Maybe we can make a list and mark, or rescan. Can be done
// later or as an optional auditing step.
//
// This why in the past we had repetitive scans.
//
// Now we focus on more passes, but simpler execution. In the end it should
// be eaiser to optmize and yield overall better performance. Most of the
// passes should be low overhead versus an integrated system which often
// would need to reiterate over items multiple times. So we do more loops on
// with less repetitive analysis, instead of fewer loops but more repetition.
//
// -Locate all plug classes
// -Scan from entry point collecting all types and methods while checking
// for and following plugs
// -For each type
// -Include all ancestors
// -Include all static constructors
// -For each virtual method
// -Scan overloads in descendants until IsFinal, IsSealed or end
// -Scan base in ancestors until top or IsAbstract
// -Go to scan types again, until no new ones found.
// -Because the virtual method scanning will add to the list as it goes, maintain
// 2 lists.
// -Known Types and Methods
// -Types and Methods in Queue - to be scanned
// -Finally, do compilation
#endregion Description
mPlugManager.FindPlugImpls(plugsAssemblies);
// Now that we found all plugs, scan them.
// We have to scan them after we find all plugs, because
// plugs can use other plugs
mPlugManager.ScanFoundPlugs();
foreach (var xPlug in mPlugManager.PlugImpls)
{
CompilerHelpers.Debug($"Plug found: '{xPlug.Key.FullName}' in '{xPlug.Key.Assembly.FullName}'");
}
ILOp.PlugManager = mPlugManager;
// Pull in extra implementations, GC etc.
Queue(VTablesImplRefs.IsInstanceRef, null, "Explicit Entry");
Queue(VTablesImplRefs.SetTypeInfoRef, null, "Explicit Entry");
Queue(VTablesImplRefs.SetInterfaceInfoRef, null, "Explicit Entry");
Queue(VTablesImplRefs.SetMethodInfoRef, null, "Explicit Entry");
Queue(VTablesImplRefs.SetInterfaceMethodInfoRef, null, "Explicit Entry");
Queue(VTablesImplRefs.GetMethodAddressForTypeRef, null, "Explicit Entry");
Queue(VTablesImplRefs.GetMethodAddressForInterfaceTypeRef, null, "Explicit Entry");
Queue(VTablesImplRefs.GetDeclaringTypeOfMethodForTypeRef, null, "Explicit Entry");
Queue(GCImplementationRefs.InitRef, null, "Explicit Entry");
Queue(GCImplementationRefs.IncRootCountRef, null, "Explicit Entry");
Queue(GCImplementationRefs.IncRootCountsInStructRef, null, "Explicit Entry");
Queue(GCImplementationRefs.DecRootCountRef, null, "Explicit Entry");
Queue(GCImplementationRefs.DecRootCountsInStructRef, null, "Explicit Entry");
Queue(GCImplementationRefs.AllocNewObjectRef, null, "Explicit Entry");
// for now, to ease runtime exception throwing
Queue(typeof(ExceptionHelper).GetMethod("ThrowNotImplemented", new Type[] { typeof(string) }, null), null, "Explicit Entry");
Queue(typeof(ExceptionHelper).GetMethod("ThrowOverflow", Type.EmptyTypes, null), null, "Explicit Entry");
Queue(typeof(ExceptionHelper).GetMethod("ThrowInvalidOperation", new Type[] { typeof(string) }, null), null, "Explicit Entry");
Queue(typeof(ExceptionHelper).GetMethod("ThrowArgumentOutOfRange", new Type[] { typeof(string) }, null), null, "Explicit Entry");
// register system types:
Queue(typeof(Array), null, "Explicit Entry");
Queue(typeof(Array).Assembly.GetType("System.SZArrayHelper"), null, "Explicit Entry");
Queue(typeof(Array).GetConstructors(BindingFlags.NonPublic | BindingFlags.Instance).First(), null, "Explicit Entry");
Queue(typeof(MulticastDelegate).GetMethod("GetInvocationList"), null, "Explicit Entry");
Queue(ExceptionHelperRefs.CurrentExceptionRef, null, "Explicit Entry");
Queue(ExceptionHelperRefs.ThrowInvalidCastExceptionRef, null, "Explicit Entry");
Queue(ExceptionHelperRefs.ThrowNotFiniteNumberExceptionRef, null, "Explicit Entry");
Queue(ExceptionHelperRefs.ThrowDivideByZeroExceptionRef, null, "Explicit Entry");
Queue(ExceptionHelperRefs.ThrowIndexOutOfRangeException, null, "Explicit Entry");
mAsmblr.ProcessField(typeof(string).GetField("Empty", BindingFlags.Static | BindingFlags.Public));
// Start from entry point of this program
Queue(aStartMethod, null, "Entry Point");
ScanQueue();
UpdateAssemblies();
Assemble();
mAsmblr.EmitEntrypoint(aStartMethod);
}
public void QueueMethod(MethodBase method)
{
Queue(method, null, "Explicit entry via QueueMethod");
}
/// This method changes the opcodes. Changes are:
/// * inserting the ValueUID for method ops.
public void ProcessInstructions(List<ILOpCode> aOpCodes) // to remove -------
{
foreach (var xOpCode in aOpCodes)
{
if (xOpCode is ILOpCodes.OpMethod xOpMethod)
{
mItems.TryGetValue(xOpMethod.Value, out MemberInfo value);
xOpMethod.Value = (MethodBase)(value ?? xOpMethod.Value);
xOpMethod.ValueUID = GetMethodUID(xOpMethod.Value);
}
}
}
public void Dispose()
{
if (mLogEnabled)
{
// Create bookmarks, but also a dictionary that
// we can find the items in
var xBookmarks = new Dictionary<object, int>();
int xBookmark = 0;
foreach (var xList in mLogMap)
{
foreach (var xItem in xList.Value)
{
xBookmarks.Add(xItem.Item, xBookmark);
xBookmark++;
}
}
using (mLogWriter = new StreamWriter(File.OpenWrite(mMapPathname)))
{
mLogWriter.WriteLine("<html><body>");
foreach (var xList in mLogMap)
{
var xLogItemText = LogItemText(xList.Key);
mLogWriter.WriteLine("<hr>");
// Emit bookmarks above source, so when clicking links user doesn't need
// to constantly scroll up.
foreach (var xItem in xList.Value)
{
mLogWriter.WriteLine("<a name=\"Item" + xBookmarks[xItem.Item].ToString() + "_S\"></a>");
}
if (!xBookmarks.TryGetValue(xList.Key, out var xHref))
{
xHref = -1;
}
mLogWriter.Write("<p>");
if (xHref >= 0)
{
mLogWriter.WriteLine("<a href=\"#Item" + xHref.ToString() + "_S\">");
mLogWriter.WriteLine("<a name=\"Item{0}\">", xHref);
}
if (xList.Key == null)
{
mLogWriter.WriteLine("Unspecified Source");
}
else
{
mLogWriter.WriteLine(xLogItemText);
}
if (xHref >= 0)
{
mLogWriter.Write("</a>");
mLogWriter.Write("</a>");
}
mLogWriter.WriteLine("</p>");
mLogWriter.WriteLine("<ul>");
foreach (var xItem in xList.Value)
{
mLogWriter.Write("<li><a href=\"#Item{1}\">{0}</a></li>", LogItemText(xItem.Item), xBookmarks[xItem.Item]);
mLogWriter.WriteLine("<ul>");
mLogWriter.WriteLine("<li>" + xItem.SrcType + "</li>");
mLogWriter.WriteLine("</ul>");
}
mLogWriter.WriteLine("</ul>");
}
mLogWriter.WriteLine("</body></html>");
}
}
}
protected string LogItemText(object aItem)
{
if (aItem is MethodBase)
{
var x = (MethodBase)aItem;
return "Method: " + x.DeclaringType + "." + x.Name + "<br>" + x.GetFullName();
}
if (aItem is Type)
{
var x = (Type)aItem;
return "Type: " + x.FullName;
}
return "Other: " + aItem;
}
protected void ScanMethod(MethodBase aMethod, bool aIsPlug, List<string> sourceItems)
{
CompilerHelpers.Debug($"ILScanner: ScanMethod");
CompilerHelpers.Debug($"Method = '{aMethod}'");
CompilerHelpers.Debug($"IsPlug = '{aIsPlug}'");
var xParams = aMethod.GetParameters();
var xParamTypes = new Type[xParams.Length];
// Dont use foreach, enum generaly keeps order but
// isn't guaranteed.
//string xMethodFullName = LabelName.GetFullName(aMethod);
for (int i = 0; i < xParams.Length; i++)
{
xParamTypes[i] = xParams[i].ParameterType;
Queue(xParamTypes[i], aMethod, "Parameter");
}
var xIsDynamicMethod = aMethod.DeclaringType == null;
// Queue Types directly related to method
if (!aIsPlug)
{
// Don't queue declaring types of plugs
if (!xIsDynamicMethod)
{
// dont queue declaring types of dynamic methods either, those dont have a declaring type
Queue(aMethod.DeclaringType, aMethod, "Declaring Type");
}
}
if (aMethod is MethodInfo)
{
Queue(((MethodInfo)aMethod).ReturnType, aMethod, "Return Type");
}
// Scan virtuals
#region Virtuals scan
if (!xIsDynamicMethod && aMethod.IsVirtual)
{
// For virtuals we need to climb up the type tree
// and find the top base method. We then add that top
// node to the mVirtuals list. We don't need to add the
// types becuase adding DeclaringType will already cause
// all ancestor types to be added.
var xVirtMethod = aMethod;
var xVirtType = aMethod.DeclaringType;
MethodBase xNewVirtMethod;
while (true)
{
xVirtType = xVirtType.BaseType;
if (xVirtType == null)
{
// We've reached object, can't go farther
xNewVirtMethod = null;
}
else
{
xNewVirtMethod = xVirtType.GetMethod(aMethod.Name, xParamTypes);
if (xNewVirtMethod != null)
{
if (!xNewVirtMethod.IsVirtual)
{
// This can happen if a virtual "replaces" a non virtual
// above it that is not virtual.
xNewVirtMethod = null;
}
}
}
// We dont bother to add these to Queue, because we have to do a
// full downlevel scan if its a new base virtual anyways.
if (xNewVirtMethod == null)
{
// If its already in the list, we mark it null
// so we dont do a full downlevel scan.
if (mVirtuals.Contains(xVirtMethod))
{
xVirtMethod = null;
}
break;
}
xVirtMethod = xNewVirtMethod;
}
// New virtual base found, we need to downscan it
// If it was already in mVirtuals, then ScanType will take
// care of new additions.
if (xVirtMethod != null)
{
Queue(xVirtMethod, aMethod, "Virtual Base");
mVirtuals.Add(xVirtMethod);
// List changes as we go, cant be foreach
for (int i = 0; i < mItemsList.Count; i++)
{
if (mItemsList[i] is Type xType && xType != xVirtMethod.DeclaringType && !xType.IsInterface)
{
if (xType.IsSubclassOf(xVirtMethod.DeclaringType))
{
var enumerable = xType.GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)
.Where(method => method.Name == aMethod.Name
&& method.GetParameters().Select(param => param.ParameterType).SequenceEqual(xParamTypes));
// We need to check IsVirtual, a non virtual could
// "replace" a virtual above it?
var xNewMethod = enumerable.FirstOrDefault(m => m.IsVirtual);
while (xNewMethod != null && (xNewMethod.Attributes & MethodAttributes.NewSlot) != 0)
{
xType = xType.BaseType;
xNewMethod = enumerable.Where(m => m.DeclaringType == xType).SingleOrDefault();
}
if (xNewMethod != null)
{
Queue(xNewMethod, aMethod, "Virtual Downscan");
}
}
else if (xVirtMethod.DeclaringType.IsInterface
&& xType.GetInterfaces().Contains(xVirtMethod.DeclaringType)
&& !(xType.BaseType == typeof(Array) && xVirtMethod.DeclaringType.IsGenericType))
{
var xInterfaceMap = xType.GetInterfaceMap(xVirtMethod.DeclaringType);
var xMethodIndex = Array.IndexOf(xInterfaceMap.InterfaceMethods, xVirtMethod);
if (xMethodIndex != -1)
{
var xMethod = xInterfaceMap.TargetMethods[xMethodIndex];
if (xMethod.DeclaringType == xType)
{
Queue(xInterfaceMap.TargetMethods[xMethodIndex], aMethod, "Virtual Downscan");
}
}
}
}
}
}
}
#endregion Virtuals scan
MethodBase xPlug = null;
// Plugs may use plugs, but plugs won't be plugged over themself
var inl = aMethod.GetCustomAttribute<InlineAttribute>();
if (!aIsPlug && !xIsDynamicMethod)
{
// Check to see if method is plugged, if it is we don't scan body
xPlug = mPlugManager.ResolvePlug(aMethod, xParamTypes);
if (xPlug != null)
{
//ScanMethod(xPlug, true, "Plug method");
if (inl == null)
{
Queue(xPlug, aMethod, "Plug method");
}
}
}
if (xPlug == null)
{
bool xNeedsPlug = false;
if ((aMethod.Attributes & MethodAttributes.PinvokeImpl) != 0)
{
// pinvoke methods dont have an embedded implementation
xNeedsPlug = true;
}
else
{
var xImplFlags = aMethod.GetMethodImplementationFlags();
// todo: prob even more
if (xImplFlags.HasFlag(MethodImplAttributes.Native) || xImplFlags.HasFlag(MethodImplAttributes.InternalCall))
{
// native implementations cannot be compiled
xNeedsPlug = true;
}
}
if (xNeedsPlug)
{
throw new Exception("Native code encountered, plug required. Check build output for more information." + Environment.NewLine
+ " DO NOT REPORT THIS AS A BUG." + Environment.NewLine
+ " Please see http://www.gocosmos.org/docs/plugs/missing/" + Environment.NewLine
+ " Need plug for: " + LabelName.GetFullName(aMethod, false) + "(Plug Signature: " + DataMember.FilterStringForIncorrectChars(LabelName.GetFullName(aMethod, false)) + " ). " + Environment.NewLine
+ " Static: " + aMethod.IsStatic + Environment.NewLine
+ " Assembly: " + aMethod.DeclaringType.Assembly.FullName + Environment.NewLine
+ " Called from:" + Environment.NewLine + string.Join("\n", sourceItems.Distinct().ToArray()) + Environment.NewLine);
}
//TODO: As we scan each method, we could update or put in a new list
// that has the resolved plug so we don't have to reresolve it again
// later for compilation.
// Scan the method body for more type and method refs
//TODO: Dont queue new items if they are plugged
// or do we need to queue them with a resolved ref in a new list?
if (inl != null)
{
return; // cancel inline
}
var xOpCodes = mReader.ProcessMethod(aMethod);
if (xOpCodes != null)
{
ProcessInstructions(xOpCodes);
foreach (var xOpCode in xOpCodes)
{
if (xOpCode is ILOpCodes.OpMethod)
{
Queue(((ILOpCodes.OpMethod)xOpCode).Value, aMethod, "Call", sourceItems);
}
else if (xOpCode is ILOpCodes.OpType xOpType)
{
Queue(((ILOpCodes.OpType)xOpCode).Value, aMethod, "OpCode Value");
}
else if (xOpCode is ILOpCodes.OpField xOpField)
{
//TODO: Need to do this? Will we get a ILOpCodes.OpType as well?
Queue(xOpField.Value.DeclaringType, aMethod, "OpCode Value");
if (xOpField.Value.IsStatic)
{
//TODO: Why do we add static fields, but not instance?
// AW: instance fields are "added" always, as part of a type, but for static fields, we need to emit a datamember
Queue(xOpField.Value, aMethod, "OpCode Value");
}
}
else if (xOpCode is ILOpCodes.OpToken xOpToken)
{
if (xOpToken.ValueIsType)
{
Queue(xOpToken.ValueType, aMethod, "OpCode Value");
}
if (xOpToken.ValueIsField)
{
Queue(xOpToken.ValueField.DeclaringType, aMethod, "OpCode Value");
if (xOpToken.ValueField.IsStatic)
{
//TODO: Why do we add static fields, but not instance?
// AW: instance fields are "added" always, as part of a type, but for static fields, we need to emit a datamember
Queue(xOpToken.ValueField, aMethod, "OpCode Value");
}
}
}
}
}
}
}
protected void ScanType(Type aType)
{
CompilerHelpers.Debug($"ILScanner: ScanType");
CompilerHelpers.Debug($"Type = '{aType}'");
// This is a bit overkill, most likely we dont need all these methods
// but I dont see a better way to do it easily
// so for generic interface methods on arrays, we just add all methods
if (aType.Name.Contains("SZArrayImpl"))
{
foreach (var xMethod in aType.GetMethods())
{
Queue(xMethod, aType, "Generic Interface Method");
}
}
if (aType.IsGenericType && new string[] { "IList", "ICollection", "IEnumerable", "IReadOnlyList", "IReadOnlyCollection" }
.Any(i => aType.Name.Contains(i)))
{
Queue(aType.GenericTypeArguments[0].MakeArrayType(), aType, "CallVirt of Generic Interface for Array");
}
// Add immediate ancestor type
// We dont need to crawl up farther, when the BaseType is scanned
// it will add its BaseType, and so on.
if (aType.BaseType != null)
{
Queue(aType.BaseType, aType, "Base Type");
}
// Queue static ctors
// We always need static ctors, else the type cannot
// be created.
foreach (var xCctor in aType.GetConstructors(BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public))
{
if (xCctor.DeclaringType == aType)
{
Queue(xCctor, aType, "Static Constructor");
}
}
if (aType.BaseType == typeof(Array) && !aType.GetElementType().IsPointer)
{
var szArrayHelper = typeof(Array).Assembly.GetType("System.SZArrayHelper"); // We manually add the link to the generic interfaces for an array
foreach (var xMethod in szArrayHelper.GetMethods(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly))
{
Queue(xMethod.MakeGenericMethod(new Type[] { aType.GetElementType() }), aType, "Virtual SzArrayHelper");
}
Queue(typeof(SZArrayImpl<>).MakeGenericType(aType.GetElementType()), aType, "Array");
}
// Scam Fields so that we include those types
foreach (var field in aType.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic))
{
Queue(field.FieldType, aType, "Field Type");
}
// For each new type, we need to scan for possible new virtuals
// in our new type if its a descendant of something in
// mVirtuals.
foreach (var xVirt in mVirtuals)
{
// See if our new type is a subclass of any virt's DeclaringTypes
// If so our new type might have some virtuals
if (aType.IsSubclassOf(xVirt.DeclaringType))
{
var xParams = xVirt.GetParameters();
var xParamTypes = new Type[xParams.Length];
// Dont use foreach, enum generaly keeps order but
// isn't guaranteed.
for (int i = 0; i < xParams.Length; i++)
{
xParamTypes[i] = xParams[i].ParameterType;
}
var xMethod = aType.GetMethod(xVirt.Name, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, xParamTypes, null);
if (xMethod != null)
{
// We need to check IsVirtual, a non virtual could
// "replace" a virtual above it?
if (xMethod.IsVirtual)
{
Queue(xMethod, aType, "Virtual");
}
}
}
else if (!aType.IsGenericParameter && xVirt.DeclaringType.IsInterface && !(aType.BaseType == typeof(Array) && xVirt.DeclaringType.IsGenericType))
{
if (!aType.IsInterface && aType.GetInterfaces().Contains(xVirt.DeclaringType)
&& !(aType.BaseType == typeof(Array) && xVirt.DeclaringType.IsGenericType))
{
var xIntfMapping = aType.GetInterfaceMap(xVirt.DeclaringType);
if (xIntfMapping.InterfaceMethods != null && xIntfMapping.TargetMethods != null)
{
var xIdx = Array.IndexOf(xIntfMapping.InterfaceMethods, xVirt);
if (xIdx != -1)
{
Queue(xIntfMapping.TargetMethods[xIdx], aType, "Virtual");
}
}
}
}
}
foreach (var xInterface in aType.GetInterfaces())
{
Queue(xInterface, aType, "Implemented Interface");
}
}
protected void ScanQueue()
{
while (mQueue.Count > 0)
{
var xItem = mQueue.Dequeue();
CompilerHelpers.Debug($"ILScanner: ScanQueue - '{xItem}'");
// Check for MethodBase first, they are more numerous
// and will reduce compares
if (xItem.Item is MethodBase xMethod)
{
if (xItem.SourceItems == null)
{
xItem.SourceItems = new List<string>();
}
ScanMethod(xMethod, false, xItem.SourceItems);
}
else if (xItem.Item is Type xType)
{
ScanType(xType);
// Methods and fields cant exist without types, so we only update
// mUsedAssemblies in type branch.
if (!mUsedAssemblies.Contains(xType.Assembly))
{
mUsedAssemblies.Add(xType.Assembly);
}
}
else if (xItem.Item is FieldInfo)
{
// todo: static fields need more processing?
}
else
{
throw new Exception("Unknown item found in queue.");
}
}
}
protected void LogMapPoint(object aSrc, string aSrcType, object aItem)
{
// Keys cant be null. If null, we just say ILScanner is the source
if (aSrc == null)
{
aSrc = typeof(ILScanner);
}
var xLogItem = new LogItem
{
SrcType = aSrcType,
Item = aItem
};
if (!mLogMap.TryGetValue(aSrc, out var xList))
{
xList = new List<LogItem>();
mLogMap.Add(aSrc, xList);
}
xList.Add(xLogItem);
}
private MethodInfo GetUltimateBaseMethod(MethodInfo aMethod)
{
var xBaseMethod = aMethod;
while (true)
{
var xBaseDefinition = xBaseMethod.GetBaseDefinition();
if (xBaseDefinition == xBaseMethod)
{
return xBaseMethod;
}
xBaseMethod = xBaseDefinition;
}
}
protected uint GetMethodUID(MethodBase aMethod)
{
if (mMethodUIDs.TryGetValue(aMethod, out var xMethodUID))
{
return xMethodUID;
}
else
{
if (!aMethod.DeclaringType.IsInterface)
{
if (aMethod is MethodInfo xMethodInfo)
{
var xBaseMethod = GetUltimateBaseMethod(xMethodInfo);
if (!mMethodUIDs.TryGetValue(xBaseMethod, out xMethodUID))
{
xMethodUID = (uint)mMethodUIDs.Count;
mMethodUIDs.Add(xBaseMethod, xMethodUID);
}
if (!new MethodBaseComparer().Equals(aMethod, xBaseMethod))
{
mMethodUIDs.Add(aMethod, xMethodUID);
}
return xMethodUID;
}
}
xMethodUID = (uint)mMethodUIDs.Count;
mMethodUIDs.Add(aMethod, xMethodUID);
return xMethodUID;
}
}
protected uint GetTypeUID(Type aType)
{
if (!mItems.Contains(aType))
{
throw new Exception($"Cannot get UID of types which are not queued! Type: {aType.Name}");
}
if (!mTypeUIDs.ContainsKey(aType))
{
var xId = (uint)mTypeUIDs.Count;
mTypeUIDs.Add(aType, xId);
return xId;
}
return mTypeUIDs[aType];
}
protected void UpdateAssemblies()
{
// It would be nice to keep DebugInfo output into assembler only but
// there is so much info that is available in scanner that is needed
// or can be used in a more efficient manner. So we output in both
// scanner and assembler as needed.
mAsmblr.DebugInfo.AddAssemblies(mUsedAssemblies);
}
protected void Assemble()
{
foreach (var xItem in mItems)
{
if (xItem is MethodBase xMethod)
{
var xParams = xMethod.GetParameters();
var xParamTypes = xParams.Select(q => q.ParameterType).ToArray();
var xPlug = mPlugManager.ResolvePlug(xMethod, xParamTypes);
var xMethodType = Il2cpuMethodInfo.TypeEnum.Normal;
Type xPlugAssembler = null;
Il2cpuMethodInfo xPlugInfo = null;
var xMethodInline = xMethod.GetCustomAttribute<InlineAttribute>();
if (xMethodInline != null)
{
// inline assembler, shouldn't come here..
continue;
}
var xMethodIdMethod = mItemsList.IndexOf(xMethod);
if (xMethodIdMethod == -1)
{
throw new Exception("Method not in scanner list!");
}
PlugMethod xPlugAttrib = null;
if (xPlug != null)
{
xMethodType = Il2cpuMethodInfo.TypeEnum.NeedsPlug;
xPlugAttrib = xPlug.GetCustomAttribute<PlugMethod>();
var xInlineAttrib = xPlug.GetCustomAttribute<InlineAttribute>();
var xMethodIdPlug = mItemsList.IndexOf(xPlug);
if (xMethodIdPlug == -1 && xInlineAttrib == null)
{
throw new Exception("Plug method not in scanner list!");
}
if (xPlugAttrib != null && xInlineAttrib == null)
{
xPlugAssembler = xPlugAttrib.Assembler;
xPlugInfo = new Il2cpuMethodInfo(xPlug, (uint)xMethodIdPlug, Il2cpuMethodInfo.TypeEnum.Plug, null, xPlugAssembler);
var xMethodInfo = new Il2cpuMethodInfo(xMethod, (uint)xMethodIdMethod, xMethodType, xPlugInfo);
if (xPlugAttrib.IsWildcard)
{
xPlugInfo.IsWildcard = true;
xPlugInfo.PluggedMethod = xMethodInfo;
var xInstructions = mReader.ProcessMethod(xPlug);
if (xInstructions != null)
{
ProcessInstructions(xInstructions);
mAsmblr.ProcessMethod(xPlugInfo, xInstructions, mPlugManager);
}
}
mAsmblr.GenerateMethodForward(xMethodInfo, xPlugInfo);
}
else
{
if (xInlineAttrib != null)
{
var xMethodID = mItemsList.IndexOf(xItem);
if (xMethodID == -1)
{
throw new Exception("Method not in list!");
}
xPlugInfo = new Il2cpuMethodInfo(xPlug, (uint)xMethodID, Il2cpuMethodInfo.TypeEnum.Plug, null, true);
var xMethodInfo = new Il2cpuMethodInfo(xMethod, (uint)xMethodIdMethod, xMethodType, xPlugInfo);
xPlugInfo.PluggedMethod = xMethodInfo;
var xInstructions = mReader.ProcessMethod(xPlug);
if (xInstructions != null)
{
ProcessInstructions(xInstructions);
mAsmblr.ProcessMethod(xPlugInfo, xInstructions, mPlugManager);
}
mAsmblr.GenerateMethodForward(xMethodInfo, xPlugInfo);
}
else
{
xPlugInfo = new Il2cpuMethodInfo(xPlug, (uint)xMethodIdPlug, Il2cpuMethodInfo.TypeEnum.Plug, null, xPlugAssembler);
var xMethodInfo = new Il2cpuMethodInfo(xMethod, (uint)xMethodIdMethod, xMethodType, xPlugInfo);
mAsmblr.GenerateMethodForward(xMethodInfo, xPlugInfo);
}
}
}
else
{
xPlugAttrib = xMethod.GetCustomAttribute<PlugMethod>();
if (xPlugAttrib != null)
{
if (xPlugAttrib.IsWildcard)
{
continue;
}
if (xPlugAttrib.PlugRequired)
{
throw new Exception(String.Format("Method {0} requires a plug, but none is implemented", xMethod.Name));
}
xPlugAssembler = xPlugAttrib.Assembler;
}
var xMethodInfo = new Il2cpuMethodInfo(xMethod, (uint)xMethodIdMethod, xMethodType, xPlugInfo, xPlugAssembler);
var xInstructions = mReader.ProcessMethod(xMethod);
if (xInstructions != null)
{
ProcessInstructions(xInstructions);
mAsmblr.ProcessMethod(xMethodInfo, xInstructions, mPlugManager);
}
}
}
else if (xItem is FieldInfo)
{
mAsmblr.ProcessField((FieldInfo)xItem);
}
}