From ac439029aa3d9d00a08959b8bab8edb59658063d Mon Sep 17 00:00:00 2001 From: kudzu_cp <6d05c8c8ef5431987001abfdb2eadc9593ac9498> Date: Sun, 26 Jul 2009 05:19:29 +0000 Subject: [PATCH] --- .../Cosmos.IL2CPU.Profiler/Assembler.cs | 24 ++++++++++++++ .../Cosmos.IL2CPU.Profiler.csproj | 1 + .../IL2PCU/Cosmos.IL2CPU.Profiler/Program.cs | 2 +- source2/IL2PCU/Cosmos.IL2CPU.X86/Assembler.cs | 14 ++++++++ .../Cosmos.IL2CPU.X86.csproj | 1 + source2/IL2PCU/Cosmos.IL2CPU.X86/IL/Ldc_I4.cs | 2 +- source2/IL2PCU/Cosmos.IL2CPU.X86/ILOp.cs | 8 +++-- source2/IL2PCU/Cosmos.IL2CPU/Assembler.cs | 32 ++++--------------- source2/Users/Kudzu/HelloWorld/Program.cs | 2 +- 9 files changed, 55 insertions(+), 31 deletions(-) create mode 100644 source2/IL2PCU/Cosmos.IL2CPU.Profiler/Assembler.cs create mode 100644 source2/IL2PCU/Cosmos.IL2CPU.X86/Assembler.cs diff --git a/source2/IL2PCU/Cosmos.IL2CPU.Profiler/Assembler.cs b/source2/IL2PCU/Cosmos.IL2CPU.Profiler/Assembler.cs new file mode 100644 index 000000000..29041f7de --- /dev/null +++ b/source2/IL2PCU/Cosmos.IL2CPU.Profiler/Assembler.cs @@ -0,0 +1,24 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace Cosmos.IL2CPU.Profiler { + public class Assembler : Cosmos.IL2CPU.Assembler { + + protected override void InitILOps() { + var xDelegate = CreateCtorDelegate(typeof(ILOp)); + // Don't change the type in the foreach to a var, its necessary as it is now + // to typecast it, so we can then recast to an int. + foreach (ILOpCode.Code xCode in Enum.GetValues(typeof(ILOpCode.Code))) { + int xCodeValue = (int)xCode; + if (xCodeValue <= 0xFF) { + mILOpsLo[xCodeValue] = xDelegate; + } else { + mILOpsHi[xCodeValue & 0xFF] = xDelegate; + } + } + } + + } +} diff --git a/source2/IL2PCU/Cosmos.IL2CPU.Profiler/Cosmos.IL2CPU.Profiler.csproj b/source2/IL2PCU/Cosmos.IL2CPU.Profiler/Cosmos.IL2CPU.Profiler.csproj index c3b48d460..8cb4aec40 100644 --- a/source2/IL2PCU/Cosmos.IL2CPU.Profiler/Cosmos.IL2CPU.Profiler.csproj +++ b/source2/IL2PCU/Cosmos.IL2CPU.Profiler/Cosmos.IL2CPU.Profiler.csproj @@ -43,6 +43,7 @@ + diff --git a/source2/IL2PCU/Cosmos.IL2CPU.Profiler/Program.cs b/source2/IL2PCU/Cosmos.IL2CPU.Profiler/Program.cs index fb8bbb06d..011f84bb1 100644 --- a/source2/IL2PCU/Cosmos.IL2CPU.Profiler/Program.cs +++ b/source2/IL2PCU/Cosmos.IL2CPU.Profiler/Program.cs @@ -17,7 +17,7 @@ namespace Cosmos.IL2CPU.Profiler { var xSW = new Stopwatch(); xSW.Start(); - var xAsmblr = new Assembler(typeof(ILOp), true); + var xAsmblr = new Assembler(); var xScanner = new ILScanner(xAsmblr); var xEntryPoint = typeof(Program).GetMethod("ScannerEntryPoint", BindingFlags.NonPublic | BindingFlags.Static); xScanner.Execute(xEntryPoint); diff --git a/source2/IL2PCU/Cosmos.IL2CPU.X86/Assembler.cs b/source2/IL2PCU/Cosmos.IL2CPU.X86/Assembler.cs new file mode 100644 index 000000000..39a3ddba9 --- /dev/null +++ b/source2/IL2PCU/Cosmos.IL2CPU.X86/Assembler.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace Cosmos.IL2CPU.X86 { + public class Assembler : Cosmos.IL2CPU.Assembler { + + protected override void InitILOps() { + InitILOps(typeof(ILOp)); + } + + } +} diff --git a/source2/IL2PCU/Cosmos.IL2CPU.X86/Cosmos.IL2CPU.X86.csproj b/source2/IL2PCU/Cosmos.IL2CPU.X86/Cosmos.IL2CPU.X86.csproj index af257d3bc..4e7f1f427 100644 --- a/source2/IL2PCU/Cosmos.IL2CPU.X86/Cosmos.IL2CPU.X86.csproj +++ b/source2/IL2PCU/Cosmos.IL2CPU.X86/Cosmos.IL2CPU.X86.csproj @@ -49,6 +49,7 @@ + diff --git a/source2/IL2PCU/Cosmos.IL2CPU.X86/IL/Ldc_I4.cs b/source2/IL2PCU/Cosmos.IL2CPU.X86/IL/Ldc_I4.cs index a0cac243e..8edbb3bdb 100644 --- a/source2/IL2PCU/Cosmos.IL2CPU.X86/IL/Ldc_I4.cs +++ b/source2/IL2PCU/Cosmos.IL2CPU.X86/IL/Ldc_I4.cs @@ -11,7 +11,7 @@ namespace Cosmos.IL2CPU.X86.IL { public override void Execute(uint aMethodUID) { new CPU.Push { DestinationValue = ((OpInt)OpCode).Value }; - ((CPU.Assembler)Indy.IL2CPU.Assembler.Assembler.CurrentInstance.Peek()).StackContents.Push(new StackContent(4, typeof(int))); + Asmblr.StackContents.Push(new StackContent(4, typeof(int))); } } diff --git a/source2/IL2PCU/Cosmos.IL2CPU.X86/ILOp.cs b/source2/IL2PCU/Cosmos.IL2CPU.X86/ILOp.cs index bb6ac809a..c91cc7481 100644 --- a/source2/IL2PCU/Cosmos.IL2CPU.X86/ILOp.cs +++ b/source2/IL2PCU/Cosmos.IL2CPU.X86/ILOp.cs @@ -2,13 +2,17 @@ using System.Collections.Generic; using System.Linq; using System.Text; +using CPU = Indy.IL2CPU.Assembler.X86; namespace Cosmos.IL2CPU.X86 { public abstract class ILOp : Cosmos.IL2CPU.ILOp { - protected ILOp(ILOpCode aOpCode):base(aOpCode) - { + + protected ILOp(ILOpCode aOpCode):base(aOpCode) { + Asmblr = ((CPU.Assembler)Indy.IL2CPU.Assembler.Assembler.CurrentInstance.Peek()); } + protected readonly CPU.Assembler Asmblr; + //TODO: remove this when all descendants implement this public override void Execute(UInt32 aMethodUID) { } diff --git a/source2/IL2PCU/Cosmos.IL2CPU/Assembler.cs b/source2/IL2PCU/Cosmos.IL2CPU/Assembler.cs index 5607649e4..286969666 100644 --- a/source2/IL2PCU/Cosmos.IL2CPU/Assembler.cs +++ b/source2/IL2PCU/Cosmos.IL2CPU/Assembler.cs @@ -5,21 +5,14 @@ using System.Reflection.Emit; using System.Text; namespace Cosmos.IL2CPU { - public class Assembler { + public abstract class Assembler { protected delegate ILOp ILOpCreateDelegate(ILOpCode aOpCode); protected ILOpCreateDelegate[] mILOpsLo = new ILOpCreateDelegate[256]; protected ILOpCreateDelegate[] mILOpsHi = new ILOpCreateDelegate[256]; - public Assembler(Type aAssemblerBaseOp) : this(aAssemblerBaseOp, false) { - } - - public Assembler(Type aAssemblerBaseOp, bool aSingleILOp) { - if (aSingleILOp) { - LoadILOp(aAssemblerBaseOp); - } else { - LoadILOps(aAssemblerBaseOp); - } + public Assembler() { + InitILOps(); } public void ProcessMethod(UInt32 aMethodUID, List aOpCodes) { @@ -36,6 +29,7 @@ namespace Cosmos.IL2CPU { } } + // http://blogs.msdn.com/haibo_luo/archive/2005/11/17/494009.aspx protected ILOpCreateDelegate CreateCtorDelegate(Type aType) { var xMethod = new DynamicMethod("", typeof(ILOp), new Type[] { typeof(ILOpCode) }, typeof(ILScanner).Module); var xGen = xMethod.GetILGenerator(); @@ -45,23 +39,9 @@ namespace Cosmos.IL2CPU { return (ILOpCreateDelegate)xMethod.CreateDelegate(typeof(ILOpCreateDelegate)); } - protected void LoadILOp(Type aAssemblerBaseOp) { - // http://blogs.msdn.com/haibo_luo/archive/2005/11/17/494009.aspx - // - var xDelegate = CreateCtorDelegate(aAssemblerBaseOp); - // Don't change the type in the foreach to a var, its necessary as it is now - // to typecast it, so we can then recast to an int. - foreach (ILOpCode.Code xCode in Enum.GetValues(typeof(ILOpCode.Code))) { - int xCodeValue = (int)xCode; - if (xCodeValue <= 0xFF) { - mILOpsLo[xCodeValue] = xDelegate; - } else { - mILOpsHi[xCodeValue & 0xFF] = xDelegate; - } - } - } + protected abstract void InitILOps(); - protected void LoadILOps(Type aAssemblerBaseOp) { + protected void InitILOps(Type aAssemblerBaseOp) { foreach (var xType in aAssemblerBaseOp.Assembly.GetExportedTypes()) { if (xType.IsSubclassOf(aAssemblerBaseOp)) { var xAttrib = (OpCodeAttribute)xType.GetCustomAttributes(typeof(OpCodeAttribute), false)[0]; diff --git a/source2/Users/Kudzu/HelloWorld/Program.cs b/source2/Users/Kudzu/HelloWorld/Program.cs index 1885f2816..f7a22b390 100644 --- a/source2/Users/Kudzu/HelloWorld/Program.cs +++ b/source2/Users/Kudzu/HelloWorld/Program.cs @@ -17,7 +17,7 @@ namespace HelloWorld { //TODO: Move new build logic into new sort. // Build stuff is all UI, launching QEMU, making ISO etc. // IL2CPU should only contain scanning and assembling of binary files - var xAsmblr = new Cosmos.IL2CPU.Assembler(typeof(Cosmos.IL2CPU.X86.ILOp)); + var xAsmblr = new Cosmos.IL2CPU.X86.Assembler(); var xScanner = new ILScanner(xAsmblr); var xEntryPoint = typeof(Program).GetMethod("Init", BindingFlags.Public | BindingFlags.Static);