changed to use Cecil, added diagnostics support for Ldstr

This commit is contained in:
mterwoord_cp 2007-08-29 14:16:38 +00:00
parent 7aba351080
commit 5eabb4bae1
9 changed files with 62 additions and 86 deletions

View file

@ -5,21 +5,19 @@ using System.Linq;
using System.Reflection;
using System.Text;
using Indy.IL2CPU.IL;
using Mono.Cecil;
using Mono.Cecil.Cil;
namespace Indy.IL2CPU {
public class Engine {
private OpCodeMap mMap = new OpCodeMap();
public void Execute(string assembly) {
Assembly a = Assembly.ReflectionOnlyLoadFrom(assembly);
if (a.EntryPoint == null)
AssemblyDefinition xAD = AssemblyFactory.GetAssembly(assembly);
if (xAD.EntryPoint == null)
throw new NotSupportedException("Libraries are not yet supported!");
ILReader reader = new ILReader(a.EntryPoint.GetMethodBody().GetILAsByteArray());
byte curByte;
while(reader.TryReadByte(out curByte))
{
mMap.GetOpForOpCode(curByte).Process(reader);
foreach (Instruction xInstruction in xAD.EntryPoint.Body.Instructions) {
mMap.GetOpForOpCode(xInstruction.OpCode.Code).Process(xInstruction);
}
Console.WriteLine("Done");
}
}
}

View file

@ -3,24 +3,25 @@ using System.Collections.Generic;
using System.Linq;
using System.Text;
using Indy.IL2CPU.IL;
using Mono.Cecil.Cil;
namespace Indy.IL2CPU {
public class OpCodeMap {
protected SortedList<byte, Op> mMap = new SortedList<byte, Op>();
protected SortedList<Code, Op> mMap = new SortedList<Code, Op>();
public OpCodeMap() {
foreach (Type t in (from item in typeof(Op).Assembly.GetTypes()
where item.IsSubclassOf(typeof(Op))
where item.IsSubclassOf(typeof(Op)) && item.GetCustomAttributes(typeof(OpCodeAttribute), false).Length > 0
select item)) {
Op op = Activator.CreateInstance(t) as Op;
mMap.Add(op.OpCode(), op);
Op xOp = Activator.CreateInstance(t) as Op;
object[] xAttribs = t.GetCustomAttributes(typeof(OpCodeAttribute), false);
mMap.Add(((OpCodeAttribute)xAttribs[0]).OpCode, xOp);
}
}
public Op GetOpForOpCode(byte code)
{
public Op GetOpForOpCode(Code code) {
if (!mMap.ContainsKey(code)) {
throw new NotSupportedException("OpCode '" + code.ToString("X2") + "' not supported!");
throw new NotSupportedException("OpCode '" + code + "' not supported!");
}
return mMap[code];
}

View file

@ -1,39 +0,0 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
namespace Indy.IL2CPU.IL
{
public class ILReader {
private byte[] mILContents;
private int mILIndex = 0;
public ILReader(byte[] aContents)
{
mILContents = aContents;
}
public byte ReadByte()
{
byte result;
if(!TryReadByte(out result))
{
throw new Exception("Couldn't read byte!");
}
return result;
}
public bool TryReadByte(out byte result)
{
if (mILIndex == mILContents.Length)
{
result = 0;
return false;
}
result = mILContents[mILIndex];
mILIndex++;
return true;
}
}
}

View file

@ -35,24 +35,22 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Mono.Cecil, Version=0.5.0.0, Culture=neutral, PublicKeyToken=0738eb9f132ed756">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\Mono.Cecil.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core">
<RequiredTargetFramework>3.5</RequiredTargetFramework>
</Reference>
<Reference Include="System.Xml.Linq">
<RequiredTargetFramework>3.5</RequiredTargetFramework>
</Reference>
<Reference Include="System.Data.DataSetExtensions">
<RequiredTargetFramework>3.5</RequiredTargetFramework>
</Reference>
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="ILReader.cs" />
<Compile Include="LdstrOp.cs" />
<Compile Include="Ldstr.cs" />
<Compile Include="Noop.cs" />
<Compile Include="Op.cs" />
<Compile Include="OpCodeAttribute.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />

View file

@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Mono.Cecil.Cil;
namespace Indy.IL2CPU.IL {
[OpCode(Code.Ldstr)]
public class Ldstr: Op {
public override void Process(Instruction aInstruction)
{
Console.WriteLine("LdStr, string = '{0}'", aInstruction.Operand);
}
}
}

View file

@ -1,16 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Indy.IL2CPU.IL {
public class Ldstr: Op {
public override byte OpCode() {
return 0x72;
}
public override void Process(ILReader aReader) {
}
}
}

View file

@ -3,14 +3,12 @@ using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Mono.Cecil.Cil;
namespace Indy.IL2CPU.IL {
class Noop: Op {
public override byte OpCode() {
return 0;
}
public override void Process(ILReader aReader) {
[OpCode(Code.Nop)]
public class Noop: Op {
public override void Process(Instruction aInstruction) {
Console.WriteLine("NoOp encountered");
}
}

View file

@ -3,10 +3,10 @@ using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Mono.Cecil.Cil;
namespace Indy.IL2CPU.IL {
public abstract class Op {
public abstract byte OpCode();
public abstract void Process(ILReader aReader);
public abstract void Process(Instruction aInstruction);
}
}

View file

@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Mono.Cecil.Cil;
namespace Indy.IL2CPU.IL {
[AttributeUsage(AttributeTargets.Class, Inherited=false, AllowMultiple=false)]
public class OpCodeAttribute: Attribute {
private readonly Code mOpCode;
public OpCodeAttribute(Code aOpCode) {
mOpCode = aOpCode;
}
public Code OpCode {
get {
return mOpCode;
}
}
}
}