Cosmos/source2/IL2PCU/Cosmos.IL2CPU/ILScanner.cs
kudzu_cp 9e0e732327
2009-07-24 14:55:55 +00:00

100 lines
3.3 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
namespace Cosmos.IL2CPU {
public class ILScanner {
//Note: We have both HashSet and List because HashSet.Contains is much faster
// than List.Contains. Also in the future we may remove items from the List
// which have already been processed yet need to keep them in HashSet.
//TODO: When we go threaded, these two should be encapselated into a single
// class with thread safety.
//TODO: These store the MethodBase which also have the IL for the body in memory
// For large asms this could eat lot of RAM. Should convert this to remove
// items from the list after they are processed but keep them in HashSet so we
// know they are already done. Currently HashSet uses a refernece though, so we
// need to hash on some UID instead of the refernce. Do not use strings, they are
// super slow.
private HashSet<MethodBase> mMethodsSet = new HashSet<MethodBase>();
private List<MethodBase> mMethods = new List<MethodBase>();
private HashSet<Type> mTypesSet = new HashSet<Type>();
private List<Type> mTypes = new List<Type>();
protected ILReader mReader;
public ILScanner(Type aAssemblerBaseOp) {
mReader = new ILReader();
}
public void Execute(MethodInfo aEntry) {
// IL Operations implicitly require these types
QueueType(typeof(string));
QueueType(typeof(int));
QueueMethod(aEntry);
// Cannot use foreach, the list changes as we go
for (int i = 0; i < mMethods.Count; i++) {
ScanMethod(mMethods[i]);
}
}
private void ScanMethod(MethodBase aMethodBase) {
if ((aMethodBase.Attributes & MethodAttributes.PinvokeImpl) != 0) {
// pinvoke methods dont have an embedded implementation
return;
} else if (aMethodBase.IsAbstract) {
// abstract methods dont have an implementation
return;
}
var xImplFlags = aMethodBase.GetMethodImplementationFlags();
if ((xImplFlags & MethodImplAttributes.Native) != 0) {
// native implementations cannot be compiled
return;
}
var xOpCodes = mReader.ProcessMethod(aMethodBase);
if (xOpCodes != null) {
foreach (var xOpCode in xOpCodes) {
//InstructionCount++;
//xOpCode.Scan(xReader, this);
}
}
}
public void QueueMethod(MethodBase aMethod) {
if (!mMethodsSet.Contains(aMethod)) {
mMethodsSet.Add(aMethod);
mMethods.Add(aMethod);
QueueType(aMethod.DeclaringType);
}
}
public void QueueType(Type aType) {
if (aType != null) {
if (!mTypesSet.Contains(aType)) {
mTypesSet.Add(aType);
mTypes.Add(aType);
QueueType(aType.BaseType);
foreach (var xMethod in aType.GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)) {
if (xMethod.DeclaringType == aType) {
if (xMethod.IsVirtual) {
QueueMethod(xMethod);
}
}
}
}
}
}
public int MethodCount {
get {
return mMethods.Count;
}
}
}
}