diff --git a/source/Indy.IL2CPU.Core/Engine.cs b/source/Indy.IL2CPU.Core/Engine.cs index 1af109adf..5761bfa31 100644 --- a/source/Indy.IL2CPU.Core/Engine.cs +++ b/source/Indy.IL2CPU.Core/Engine.cs @@ -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"); } } } diff --git a/source/Indy.IL2CPU.Core/OpCodeMap.cs b/source/Indy.IL2CPU.Core/OpCodeMap.cs index 42e6fdc88..1b6c3593b 100644 --- a/source/Indy.IL2CPU.Core/OpCodeMap.cs +++ b/source/Indy.IL2CPU.Core/OpCodeMap.cs @@ -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 mMap = new SortedList(); + protected SortedList mMap = new SortedList(); 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]; } diff --git a/source/Indy.IL2CPU.IL/ILReader.cs b/source/Indy.IL2CPU.IL/ILReader.cs deleted file mode 100644 index 9225d0d05..000000000 --- a/source/Indy.IL2CPU.IL/ILReader.cs +++ /dev/null @@ -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; - } - } -} \ No newline at end of file diff --git a/source/Indy.IL2CPU.IL/Indy.IL2CPU.IL.csproj b/source/Indy.IL2CPU.IL/Indy.IL2CPU.IL.csproj index 533583571..15fe0ba69 100644 --- a/source/Indy.IL2CPU.IL/Indy.IL2CPU.IL.csproj +++ b/source/Indy.IL2CPU.IL/Indy.IL2CPU.IL.csproj @@ -35,24 +35,22 @@ 4 + + False + ..\Mono.Cecil.dll + 3.5 - - 3.5 - - - 3.5 - - - + + diff --git a/source/Indy.IL2CPU.IL/Ldstr.cs b/source/Indy.IL2CPU.IL/Ldstr.cs new file mode 100644 index 000000000..cc1fdb350 --- /dev/null +++ b/source/Indy.IL2CPU.IL/Ldstr.cs @@ -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); + } + } +} \ No newline at end of file diff --git a/source/Indy.IL2CPU.IL/LdstrOp.cs b/source/Indy.IL2CPU.IL/LdstrOp.cs deleted file mode 100644 index 650e87bbc..000000000 --- a/source/Indy.IL2CPU.IL/LdstrOp.cs +++ /dev/null @@ -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) { - } - - } -} \ No newline at end of file diff --git a/source/Indy.IL2CPU.IL/Noop.cs b/source/Indy.IL2CPU.IL/Noop.cs index 494767b17..a7c0a434e 100644 --- a/source/Indy.IL2CPU.IL/Noop.cs +++ b/source/Indy.IL2CPU.IL/Noop.cs @@ -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"); } } diff --git a/source/Indy.IL2CPU.IL/Op.cs b/source/Indy.IL2CPU.IL/Op.cs index c1cfce5cb..a88708083 100644 --- a/source/Indy.IL2CPU.IL/Op.cs +++ b/source/Indy.IL2CPU.IL/Op.cs @@ -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); } } diff --git a/source/Indy.IL2CPU.IL/OpCodeAttribute.cs b/source/Indy.IL2CPU.IL/OpCodeAttribute.cs new file mode 100644 index 000000000..647d874cc --- /dev/null +++ b/source/Indy.IL2CPU.IL/OpCodeAttribute.cs @@ -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; + } + } + } +} \ No newline at end of file