This commit is contained in:
kudzu_cp 2009-07-26 05:19:29 +00:00
parent 1459fd78c3
commit ac439029aa
9 changed files with 55 additions and 31 deletions

View file

@ -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;
}
}
}
}
}

View file

@ -43,6 +43,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Assembler.cs" />
<Compile Include="ILOp.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />

View file

@ -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);

View file

@ -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));
}
}
}

View file

@ -49,6 +49,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Assembler.cs" />
<Compile Include="ILOp.cs" />
<Compile Include="IL\Add.cs" />
<Compile Include="IL\Add_Ovf.cs" />

View file

@ -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)));
}
}

View file

@ -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) {
}

View file

@ -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<ILOpCode> 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];

View file

@ -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);