This commit is contained in:
mterwoord_cp 2010-07-27 16:55:22 +00:00
parent 16fc8313ce
commit d3bec07e55
10 changed files with 375 additions and 41 deletions

View file

@ -78,16 +78,16 @@
<Reference Include="System.Xml" /> <Reference Include="System.Xml" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="CecilExtensions.cs" /> <None Include="CecilExtensions.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Reader.cs" /> <None Include="Reader.cs" />
<Compile Include="Reader.MonoHelpers.cs" /> <None Include="Reader.MonoHelpers.cs" />
<Compile Include="Reader.ScanLog.cs" /> <None Include="Reader.ScanLog.cs" />
<Compile Include="ReaderWithPlugSupport.cs" /> <None Include="ReaderWithPlugSupport.cs" />
<Compile Include="_QueueItems\QueuedPointerType.cs" /> <None Include="_QueueItems\QueuedPointerType.cs" />
<Compile Include="_QueueItems\QueuedArrayType.cs" /> <None Include="_QueueItems\QueuedArrayType.cs" />
<Compile Include="_QueueItems\QueuedType.cs" /> <None Include="_QueueItems\QueuedType.cs" />
<Compile Include="_QueueItems\QueuedMethod.cs" /> <None Include="_QueueItems\QueuedMethod.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\Cosmos.IL2CPU.Plugs\Cosmos.IL2CPU.Plugs.csproj"> <ProjectReference Include="..\Cosmos.IL2CPU.Plugs\Cosmos.IL2CPU.Plugs.csproj">

View file

@ -11,25 +11,24 @@ namespace ReflectionToEcmaCil.Tests
{ {
public class BaseTest public class BaseTest
{ {
protected void AssertCompilationSame(string refName, Type baseTYpe) protected void AssertCompilationSame(string refName, Type baseType)
{ {
//var xReader = new Reader(); var xReader = new Reader();
//var xResult = xReader.Execute(baseTYpe.Assembly.Location); var xResult = xReader.Execute(baseType.Assembly.Location);
//string xActualOutput; string xActualOutput;
//using (var xStringWriter = new StringWriter()) using (var xStringWriter = new StringWriter())
//{ {
// using (var xXmlOut = XmlWriter.Create(xStringWriter)) using (var xXmlOut = XmlWriter.Create(xStringWriter))
// { {
// Dump.DumpTypes(xResult, xXmlOut); Dump.DumpTypes(xResult, xXmlOut);
// xXmlOut.Flush(); xXmlOut.Flush();
// xStringWriter.Flush(); xStringWriter.Flush();
// xActualOutput = xStringWriter.ToString(); xActualOutput = xStringWriter.ToString();
// } }
//} }
//var xExpectedOutput = ReadAllTextFromStream(typeof(BaseTest).Assembly.GetManifestResourceStream(typeof(BaseTest).Namespace + "." + refName + ".xml")); var xExpectedOutput = ReadAllTextFromStream(typeof(BaseTest).Assembly.GetManifestResourceStream(typeof(BaseTest).Namespace + "." + refName + ".xml"));
//Assert.AreEqual(xExpectedOutput, xActualOutput); Assert.AreEqual(xExpectedOutput, xActualOutput);
Assert.Fail("Not implemented yet!");
} }
private static string ReadAllTextFromStream(Stream aStream) private static string ReadAllTextFromStream(Stream aStream)

View file

@ -2,10 +2,17 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using NUnit.Framework;
namespace ReflectionToEcmaCil.Tests namespace ReflectionToEcmaCil.Tests
{ {
public class SimpleMethodsTest: BaseTest [TestFixture]
public class SimpleMethodsTestsTest : BaseTest
{ {
[Test]
public void DoTest()
{
AssertCompilationSame("SimpleMethodsTests", typeof(SimpleMethodsTest.Program));
}
} }
} }

View file

@ -0,0 +1,133 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
namespace ReflectionToEcmaCil
{
public static class Extensions
{
public static string GetFullName(this MethodBase aMethod)
{
if (aMethod == null)
{
throw new ArgumentNullException("aMethod");
}
var xBuilder = new StringBuilder(256);
var xParts = aMethod.ToString().Split(' ');
var xParts2 = xParts.Skip(1).ToArray();
var xMethodInfo = aMethod as System.Reflection.MethodInfo;
if (xMethodInfo != null)
{
xBuilder.Append(GetFullName(xMethodInfo.ReturnType));
}
else
{
var xCtor = aMethod as ConstructorInfo;
if (xCtor != null)
{
xBuilder.Append(typeof(void).FullName);
}
else
{
xBuilder.Append(xParts[0]);
}
}
xBuilder.Append(" ");
xBuilder.Append(GetFullName(aMethod.DeclaringType));
xBuilder.Append(".");
xBuilder.Append(aMethod.Name);
if (aMethod.IsGenericMethod || aMethod.IsGenericMethodDefinition)
{
var xGenArgs = aMethod.GetGenericArguments();
if (xGenArgs.Length > 0)
{
xBuilder.Append("<");
for (int i = 0; i < xGenArgs.Length - 1; i++)
{
xBuilder.Append(GetFullName(xGenArgs[i]));
xBuilder.Append(", ");
}
xBuilder.Append(GetFullName(xGenArgs.Last()));
xBuilder.Append(">");
}
}
xBuilder.Append("(");
var xParams = aMethod.GetParameters();
for (var i = 0; i < xParams.Length; i++)
{
if (xParams[i].Name == "aThis" && i == 0)
{
continue;
}
xBuilder.Append(GetFullName(xParams[i].ParameterType));
if (i < (xParams.Length - 1))
{
xBuilder.Append(", ");
}
}
xBuilder.Append(")");
return String.Intern(xBuilder.ToString());
}
private static string GetFullName(this Type aType)
{
if (aType.IsGenericParameter)
{
return aType.Name;
}
var xSB = new StringBuilder();
if (aType.IsArray)
{
xSB.Append(aType.GetElementType().GetFullName());
xSB.Append("[");
int xRank = aType.GetArrayRank();
while (xRank > 1)
{
xSB.Append(",");
xRank--;
}
xSB.Append("]");
return xSB.ToString();
}
if (aType.IsByRef && aType.HasElementType)
{
return "&" + aType.GetElementType().GetFullName();
}
if (aType.IsGenericType)
{
xSB.Append(aType.GetGenericTypeDefinition().FullName);
}
else
{
xSB.Append(aType.FullName);
}
if (aType.ContainsGenericParameters)
{
xSB.Append("<");
var xArgs = aType.GetGenericArguments();
for (int i = 0; i < xArgs.Length - 1; i++)
{
xSB.Append(GetFullName(xArgs[i]));
xSB.Append(", ");
} if (xArgs.Length == 0) { Console.Write(""); }
xSB.Append(GetFullName(xArgs.Last()));
xSB.Append(">");
}
return xSB.ToString();
}
public static string GetFullName(this FieldInfo aField)
{
return aField.FieldType.GetFullName() + " " + aField.DeclaringType.GetFullName() + "." + aField.Name;
//var xSB = new StringBuilder(aField.FieldType.FullName.Length + 1 + aField.DeclaringType.FullName.Length + 1 + aField.Name);
//xSB.Append(aField.FieldType.FullName);
//xSB.Append(" ");
//xSB.Append(aField.DeclaringType.FullName);
//xSB.Append(".");
//xSB.Append(aField.Name);
//return String.Intern(xSB.ToString());
}
}
}

View file

@ -7,17 +7,77 @@ namespace ReflectionToEcmaCil
{ {
public class ILStreamPositionReader public class ILStreamPositionReader
{ {
private static readonly byte[] InstructionLengthsLow;
private static readonly byte[] InstructionLengthsHigh;
static ILStreamPositionReader()
{
#region init InstructionLengthsLow
InstructionLengthsLow = new byte[256]{
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1,
1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
4, 8, 4, 8, 0, 0, 0, 4, 4, 4, 0, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 4, 4, 4, 4, 4, 4, 4, 4,
4, 4, 4, 4, 4, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4,
4, 4, 4, 4, 4, 4, 0, 0, 0, 4, 0, 4, 4, 4, 4, 4,
4, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 4, 0, 4,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 4, 4, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 4, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0,
4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 1, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
#endregion
#region init InstructionLengthsHigh
InstructionLengthsHigh = new byte[256]{
0, 0, 0, 0, 0, 0, 4, 4, 0, 2, 2, 2, 2, 2, 2, 0,
0, 0, 1, 0, 0, 4, 4, 0, 0, 0, 0, 0, 4, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
};
#endregion
}
/// <summary> /// <summary>
/// .Key contains stream index, .Value contains instruction (logical) index /// .Key contains stream index, .Value contains instruction (logical) index
/// </summary> /// </summary>
public static IEnumerable<KeyValuePair<int, int>> GetIndexes(byte[] stream) public static IEnumerable<KeyValuePair<int, int>> GetIndexes(byte[] stream)
{ {
int xCurrentIndex = 0;
int xCurrentPosition = 0; int xCurrentPosition = 0;
yield return new KeyValuePair<int, int>(0, 0); yield return new KeyValuePair<int, int>(0, 0);
while (xCurrentPosition < stream.Length) while (xCurrentPosition < stream.Length)
{ {
var xOp = stream[xCurrentPosition];
if (xOp == 0xFE)
{
xCurrentPosition++;
xOp = stream[xCurrentPosition];
xCurrentPosition += InstructionLengthsHigh[xOp];
}
else
{
xCurrentPosition += InstructionLengthsLow[xOp];
}
xCurrentPosition++;
xCurrentIndex++;
yield return new KeyValuePair<int, int>(xCurrentPosition, xCurrentIndex);
} }
} }
} }

View file

@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.IO; using System.IO;
using System.Reflection;
namespace ReflectionToEcmaCil namespace ReflectionToEcmaCil
{ {
@ -18,6 +19,103 @@ namespace ReflectionToEcmaCil
} }
protected Dictionary<object, List<LogItem>> mLogMap; protected Dictionary<object, List<LogItem>> mLogMap;
public void EnableLogging(string aPathname)
{
mLogMap = new Dictionary<object, List<LogItem>>();
mMapPathname = aPathname;
mLogEnabled = true;
}
public void WriteScanMap()
{
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)
{
if (!xBookmarks.ContainsKey(xItem.Item))
{
xBookmarks.Add(xItem.Item, xBookmark);
xBookmark++;
}
}
}
using (mLogWriter = new StreamWriter(mMapPathname, false))
{
mLogWriter.WriteLine("<html><body>");
foreach (var xList in mLogMap)
{
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() + "\"></a>");
}
int xHref;
if (!xBookmarks.TryGetValue(xList.Key, out xHref))
{
xHref = -1;
}
mLogWriter.Write("<p>");
if (xHref >= 0)
{
mLogWriter.WriteLine("<a href=\"#Item" + xHref.ToString() + "\">");
}
if (xList.Key == null)
{
mLogWriter.WriteLine("Unspecified Source");
}
else
{
mLogWriter.WriteLine(LogItemText(xList.Key));
}
if (xHref >= 0)
{
mLogWriter.Write("</a>");
}
mLogWriter.WriteLine("</a></p>");
mLogWriter.WriteLine("<ul>");
foreach (var xItem in xList.Value)
{
mLogWriter.Write("<li>" + LogItemText(xItem.Item) + "</li>");
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.ToString();
}
else if (aItem is Type)
{
var x = (Type)aItem;
return "Type: " + x.FullName;
}
else
{
return "Other: " + aItem.ToString();
}
}
private void LogMapPoint(object aSrc, string aSrcType, object aItem) private void LogMapPoint(object aSrc, string aSrcType, object aItem)
{ {

View file

@ -231,5 +231,32 @@ namespace ReflectionToEcmaCil
//} //}
} }
} }
private void ScanArrayType(QueuedArrayType aArrayType, EcmaCil.ArrayTypeMeta aArrayMeta)
{
aArrayMeta.Dimensions = aArrayType.ArrayType.GetArrayRank();
// todo: fix?
// foreach (ArrayDimension xDimension in aType.ArrayType.Dimensions)
//{
// if (xDimension.LowerBound != 0 || xDimension.UpperBound != 0)
// {
// throw new Exception("Arrays with limited dimensions not supported");
// }
//}
if (aArrayType.ArrayType.GetArrayRank() != 1)
{
throw new Exception("Multidimensional arrays not yet supported!");
}
#if DEBUG
var xSB = new StringBuilder();
xSB.Append(aArrayMeta.ElementType.ToString());
xSB.Append("[");
xSB.Append(new String(',', aArrayMeta.Dimensions - 1));
xSB.Append("]");
aArrayMeta.Data[EcmaCil.DataIds.DebugMetaId] = xSB.ToString();
#endif
}
} }
} }

View file

@ -61,12 +61,12 @@ namespace ReflectionToEcmaCil
ScanMethod(xMethod, mMethods[xMethod]); ScanMethod(xMethod, mMethods[xMethod]);
continue; continue;
} }
//if (xItem is QueuedArrayType) if (xItem is QueuedArrayType)
//{ {
// var xType = (QueuedArrayType)xItem; var xType = (QueuedArrayType)xItem;
// ScanArrayType(xType, mArrayTypes[xType]); ScanArrayType(xType, mArrayTypes[xType]);
// continue; continue;
//} }
//if (xItem is QueuedPointerType) //if (xItem is QueuedPointerType)
//{ //{
// var xType = (QueuedPointerType)xItem; // var xType = (QueuedPointerType)xItem;

View file

@ -61,8 +61,11 @@
<Reference Include="System.Core"> <Reference Include="System.Core">
<RequiredTargetFramework>3.5</RequiredTargetFramework> <RequiredTargetFramework>3.5</RequiredTargetFramework>
</Reference> </Reference>
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="Extensions.cs" />
<Compile Include="ILStreamPositionReader.cs" /> <Compile Include="ILStreamPositionReader.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Reader.cs" /> <Compile Include="Reader.cs" />

View file

@ -5,24 +5,31 @@ using System.Text;
using EcmaCil; using EcmaCil;
using System.Xml; using System.Xml;
using System.Diagnostics; using System.Diagnostics;
using System.IO;
namespace TestApp namespace TestApp
{ {
class Program class Program
{ {
public static void Main(string[] args) public static void Main(string[] args)
{
try
{ {
using (var xReader = new ReflectionToEcmaCil.Reader()) using (var xReader = new ReflectionToEcmaCil.Reader())
{ {
try
{
xReader.EnableLogging(Path.ChangeExtension(typeof(SimpleMethodsTest.Program).Assembly.Location, "html"));
var xResult = xReader.Execute(typeof(SimpleMethodsTest.Program).Assembly.Location); var xResult = xReader.Execute(typeof(SimpleMethodsTest.Program).Assembly.Location);
//xReader. using (var xXmlOut = XmlWriter.Create(Path.ChangeExtension(typeof(SimpleMethodsTest.Program).Assembly.Location, "out")))
{
Dump.DumpTypes(xResult, xXmlOut);
xXmlOut.Flush();
} }
} }
catch (Exception E) catch (Exception E)
{ {
Console.WriteLine("Error: " + E.ToString()); Console.WriteLine("Error: " + E.ToString());
xReader.WriteScanMap();
}
} }
//using (var xReader = new MonoCecilToEcmaCil1.ReaderWithPlugSupport()) //using (var xReader = new MonoCecilToEcmaCil1.ReaderWithPlugSupport())
//{ //{