From b9200d6e44d2d69ffce02ca83407e7ca6952a3c1 Mon Sep 17 00:00:00 2001 From: mterwoord_cp <7cd3fd84a0151ea055c2f79e4d2eef9576fe9afesxUZAwxD> Date: Wed, 14 Jan 2009 18:35:14 +0000 Subject: [PATCH] --- source/Indy.IL2CPU/Assembler/Assembler.cs | 43 +++++++------------ .../Assembler/BaseAssemblerElement.cs | 4 +- source/Indy.IL2CPU/Assembler/Comment.cs | 5 +-- source/Indy.IL2CPU/Assembler/DataMember.cs | 14 +++--- source/Indy.IL2CPU/Assembler/Instruction.cs | 4 +- source/Indy.IL2CPU/Assembler/Label.cs | 5 +-- .../Assembler/x86/JumpToSegment.cs | 8 ++-- .../Assembler/x86/_Infra/Instruction.cs | 13 +++--- .../x86/_Infra/InstructionWithDestination.cs | 4 +- .../InstructionWithDestinationAndSource.cs | 4 +- source/Indy.IL2CPU/Engine.cs | 10 ++++- 11 files changed, 57 insertions(+), 57 deletions(-) diff --git a/source/Indy.IL2CPU/Assembler/Assembler.cs b/source/Indy.IL2CPU/Assembler/Assembler.cs index f4a9f8097..6852e1585 100644 --- a/source/Indy.IL2CPU/Assembler/Assembler.cs +++ b/source/Indy.IL2CPU/Assembler/Assembler.cs @@ -5,6 +5,7 @@ using System.Linq; using System.Text; using System.Reflection; using System.Threading; +using System.Diagnostics; namespace Indy.IL2CPU.Assembler { public abstract class Assembler : IDisposable { @@ -167,58 +168,50 @@ namespace Indy.IL2CPU.Assembler { int xCurrentIdx = 0; int xIfLevelsToSkip = 0; var xDefines = new List(); - while (xCurrentIdx < mAllAssemblerElements.Count) { - var xCurrentInstruction = mAllAssemblerElements[xCurrentIdx]; + var xNewAssemblerElements = new List(); + foreach (var xCurrentInstruction in mAllAssemblerElements) { var xIfDefined = xCurrentInstruction as IIfDefined; var xEndIfDefined = xCurrentInstruction as IEndIfDefined; var xDefine = xCurrentInstruction as IDefine; var xIfNotDefined = xCurrentInstruction as IIfNotDefined; if (xCurrentInstruction is Comment) { - mAllAssemblerElements.RemoveAt(xCurrentIdx); continue; } - if(xIfDefined!=null){ - if(xIfLevelsToSkip>0){ + if (xIfDefined != null) { + if (xIfLevelsToSkip > 0) { + xIfLevelsToSkip++; + } else if (!xDefines.Contains(xIfDefined.Symbol.ToLowerInvariant())) { xIfLevelsToSkip++; - } else { - if (!xDefines.Contains(xIfDefined.Symbol.ToLowerInvariant())) { - xIfLevelsToSkip++; - } } - mAllAssemblerElements.RemoveAt(xCurrentIdx); continue; } if (xIfNotDefined != null) { if (xIfLevelsToSkip > 0) { xIfLevelsToSkip++; - } else { - if (xDefines.Contains(xIfNotDefined.Symbol.ToLower())) { - xIfLevelsToSkip++; - } + } else if (xDefines.Contains(xIfNotDefined.Symbol.ToLower())) { + xIfLevelsToSkip++; } - mAllAssemblerElements.RemoveAt(xCurrentIdx); continue; } if (xEndIfDefined != null) { if (xIfLevelsToSkip > 0) { - xIfLevelsToSkip--; + xIfLevelsToSkip--; } - mAllAssemblerElements.RemoveAt(xCurrentIdx); continue; } if (xIfLevelsToSkip > 0) { - mAllAssemblerElements.RemoveAt(xCurrentIdx); continue; } if (xDefine != null) { - if (!xDefines.Contains(xDefine.Symbol.ToLowerInvariant())) { - xDefines.Add(xDefine.Symbol.ToLowerInvariant()); + var xSymbol = xDefine.Symbol.ToLowerInvariant(); + if (!xDefines.Contains(xSymbol)) { + xDefines.Add(xSymbol); } - mAllAssemblerElements.RemoveAt(xCurrentIdx); continue; } - xCurrentIdx++; + xNewAssemblerElements.Add(xCurrentInstruction); } + mAllAssemblerElements = xNewAssemblerElements; } internal List mAllAssemblerElements; @@ -237,11 +230,7 @@ namespace Indy.IL2CPU.Assembler { var xCurrentAddresss = aBaseAddress; ulong xSize = 0; foreach (var xItem in mAllAssemblerElements) { - xItem.StartAddress = xCurrentAddresss; - if (!xItem.DetermineSize(this, out xSize)) { - throw new Exception("Element of unknown size encountered."); - } - xCurrentAddresss += xSize; + xItem.UpdateAddress(this, ref xCurrentAddresss); } foreach (var xItem in mAllAssemblerElements) { diff --git a/source/Indy.IL2CPU/Assembler/BaseAssemblerElement.cs b/source/Indy.IL2CPU/Assembler/BaseAssemblerElement.cs index 6bc433ee6..ee6dd324b 100644 --- a/source/Indy.IL2CPU/Assembler/BaseAssemblerElement.cs +++ b/source/Indy.IL2CPU/Assembler/BaseAssemblerElement.cs @@ -19,7 +19,9 @@ namespace Indy.IL2CPU.Assembler { get; } - public abstract bool DetermineSize(Assembler aAssembler, out ulong aSize); + public virtual void UpdateAddress(Assembler aAssembler, ref ulong aAddress) { + StartAddress = aAddress; + } public abstract bool IsComplete(Assembler aAssembler); public abstract byte[] GetData(Assembler aAssembler); diff --git a/source/Indy.IL2CPU/Assembler/Comment.cs b/source/Indy.IL2CPU/Assembler/Comment.cs index adcae1efd..3c86a2a26 100644 --- a/source/Indy.IL2CPU/Assembler/Comment.cs +++ b/source/Indy.IL2CPU/Assembler/Comment.cs @@ -16,9 +16,8 @@ namespace Indy.IL2CPU.Assembler { return "; " + Text; } - public override bool DetermineSize(Assembler aAssembler, out ulong aSize) { - aSize = 0; - return true; + public override void UpdateAddress(Assembler aAssembler, ref ulong aAddress) { + base.UpdateAddress(aAssembler, ref aAddress); } public override bool IsComplete(Assembler aAssembler) { diff --git a/source/Indy.IL2CPU/Assembler/DataMember.cs b/source/Indy.IL2CPU/Assembler/DataMember.cs index 2d3f2b285..9832cdd39 100644 --- a/source/Indy.IL2CPU/Assembler/DataMember.cs +++ b/source/Indy.IL2CPU/Assembler/DataMember.cs @@ -143,18 +143,15 @@ namespace Indy.IL2CPU.Assembler { } } - public override bool DetermineSize(Assembler aAssembler, out ulong aSize) { + public override void UpdateAddress(Assembler aAssembler, ref ulong xAddress) { + base.UpdateAddress(aAssembler, ref xAddress); if (RawDefaultValue != null) { - aSize = (ulong)RawDefaultValue.LongLength; - return true; + xAddress += (ulong)RawDefaultValue.LongLength; } if (UntypedDefaultValue != null) { // TODO: what to do with 64bit target platforms? right now we only support 32bit - aSize = (ulong)(UntypedDefaultValue.LongLength * 4); - return true; + xAddress += (ulong)(UntypedDefaultValue.LongLength * 4); } - aSize = 0; - return false; } public override bool IsComplete(Assembler aAssembler) { @@ -187,6 +184,9 @@ namespace Indy.IL2CPU.Assembler { if (xTheRef == null) { throw new Exception("Reference not found!"); } + if (!xTheRef.ActualAddress.HasValue) { + Console.Write(""); + } xTemp = BitConverter.GetBytes(xTheRef.ActualAddress.Value); } else { if (UntypedDefaultValue[i] is int) { diff --git a/source/Indy.IL2CPU/Assembler/Instruction.cs b/source/Indy.IL2CPU/Assembler/Instruction.cs index b99fb26d2..a58a89753 100644 --- a/source/Indy.IL2CPU/Assembler/Instruction.cs +++ b/source/Indy.IL2CPU/Assembler/Instruction.cs @@ -35,8 +35,8 @@ namespace Indy.IL2CPU.Assembler { } } - public override bool DetermineSize(Assembler aAssembler, out ulong aSize) { - throw new NotImplementedException("Method not implemented for instruction " + this.GetType().FullName.Substring(typeof(Instruction).Namespace.Length+1)); + public override void UpdateAddress(Assembler aAssembler, ref ulong aAddress) { + base.UpdateAddress(aAssembler, ref aAddress); } public override bool IsComplete(Assembler aAssembler) { diff --git a/source/Indy.IL2CPU/Assembler/Label.cs b/source/Indy.IL2CPU/Assembler/Label.cs index f37535399..e3561f482 100644 --- a/source/Indy.IL2CPU/Assembler/Label.cs +++ b/source/Indy.IL2CPU/Assembler/Label.cs @@ -101,9 +101,8 @@ namespace Indy.IL2CPU.Assembler { return true; } - public override bool DetermineSize(Assembler aAssembler, out ulong aSize) { - aSize = 0; - return true; + public override void UpdateAddress(Assembler aAssembler, ref ulong aAddress) { + base.UpdateAddress(aAssembler, ref aAddress); } public override byte[] GetData(Assembler aAssembler) { diff --git a/source/Indy.IL2CPU/Assembler/x86/JumpToSegment.cs b/source/Indy.IL2CPU/Assembler/x86/JumpToSegment.cs index 561c810e7..af53fc6ff 100644 --- a/source/Indy.IL2CPU/Assembler/x86/JumpToSegment.cs +++ b/source/Indy.IL2CPU/Assembler/x86/JumpToSegment.cs @@ -5,7 +5,7 @@ using System.Text; namespace Indy.IL2CPU.Assembler.X86 { [OpCode("jmp")] - public class JumpToSegment: Instruction { + public class JumpToSegment: IL2CPU.Assembler.Instruction { public ElementReference DestinationRef { get; set; @@ -41,9 +41,9 @@ namespace Indy.IL2CPU.Assembler.X86 { return DestinationRef == null || DestinationRef.Resolve(aAssembler, out xAddress); } - public override bool DetermineSize(Indy.IL2CPU.Assembler.Assembler aAssembler, out ulong aSize) { - aSize = 7; - return true; + public override void UpdateAddress(Indy.IL2CPU.Assembler.Assembler aAssembler, ref ulong aAddress) { + base.UpdateAddress(aAssembler, ref aAddress); + aAddress += 7; } public override byte[] GetData(Indy.IL2CPU.Assembler.Assembler aAssembler) { diff --git a/source/Indy.IL2CPU/Assembler/x86/_Infra/Instruction.cs b/source/Indy.IL2CPU/Assembler/x86/_Infra/Instruction.cs index b6f85343e..34b9b6916 100644 --- a/source/Indy.IL2CPU/Assembler/x86/_Infra/Instruction.cs +++ b/source/Indy.IL2CPU/Assembler/x86/_Infra/Instruction.cs @@ -615,18 +615,21 @@ namespace Indy.IL2CPU.Assembler.X86 { return true; } - public override bool DetermineSize(Indy.IL2CPU.Assembler.Assembler aAssembler, out ulong aSize) { + public override void UpdateAddress(Indy.IL2CPU.Assembler.Assembler aAssembler, ref ulong aAddress) { + base.UpdateAddress(aAssembler, ref aAddress); var xInstructionWithDestination = this as IInstructionWithDestination; var xInstructionWithSource = this as IInstructionWithSource; var xInstructionWithSize = this as IInstructionWithSize; InstructionData xInstructionData = null; InstructionData.InstructionEncodingOption xEncodingOption = null; if (!GetEffectiveInstructionInfo(this, xInstructionWithDestination, xInstructionWithSize, xInstructionWithSource, out xInstructionData, out xEncodingOption)) { - return base.DetermineSize(aAssembler, out aSize); + return; } - var xResult = DetermineSize(aAssembler, out aSize, this, xInstructionWithDestination, xInstructionWithSize, xInstructionWithSource, xInstructionData, xEncodingOption); - mDataSize = aSize; - return xResult; + ulong aSize = 0; + if (!DetermineSize(aAssembler, out aSize, this, xInstructionWithDestination, xInstructionWithSize, xInstructionWithSource, xInstructionData, xEncodingOption)) { + throw new Exception("Unable to determine size"); + } + aAddress += aSize; } public override bool IsComplete(Indy.IL2CPU.Assembler.Assembler aAssembler) { diff --git a/source/Indy.IL2CPU/Assembler/x86/_Infra/InstructionWithDestination.cs b/source/Indy.IL2CPU/Assembler/x86/_Infra/InstructionWithDestination.cs index f77fc53e2..66ce1d7c2 100644 --- a/source/Indy.IL2CPU/Assembler/x86/_Infra/InstructionWithDestination.cs +++ b/source/Indy.IL2CPU/Assembler/x86/_Infra/InstructionWithDestination.cs @@ -38,11 +38,11 @@ namespace Indy.IL2CPU.Assembler.X86 { return base.IsComplete(aAssembler); } - public override bool DetermineSize(Indy.IL2CPU.Assembler.Assembler aAssembler, out ulong aSize) { + public override void UpdateAddress(Indy.IL2CPU.Assembler.Assembler aAssembler, ref ulong aAddresss) { if (DestinationRef != null) { DestinationValue = 0xFFFFFFFF; } - return base.DetermineSize(aAssembler, out aSize); + base.UpdateAddress(aAssembler, ref aAddresss); } diff --git a/source/Indy.IL2CPU/Assembler/x86/_Infra/InstructionWithDestinationAndSource.cs b/source/Indy.IL2CPU/Assembler/x86/_Infra/InstructionWithDestinationAndSource.cs index f13869fcf..f6f4accf8 100644 --- a/source/Indy.IL2CPU/Assembler/x86/_Infra/InstructionWithDestinationAndSource.cs +++ b/source/Indy.IL2CPU/Assembler/x86/_Infra/InstructionWithDestinationAndSource.cs @@ -64,11 +64,11 @@ namespace Indy.IL2CPU.Assembler.X86 { return base.IsComplete(aAssembler); } - public override bool DetermineSize(Indy.IL2CPU.Assembler.Assembler aAssembler, out ulong aSize) { + public override void UpdateAddress(Indy.IL2CPU.Assembler.Assembler aAssembler, ref ulong aAddress) { if (SourceRef != null) { SourceValue = 0xFFFFFFFF; } - return base.DetermineSize(aAssembler, out aSize); + base.UpdateAddress(aAssembler, ref aAddress); } public override byte[] GetData(Indy.IL2CPU.Assembler.Assembler aAssembler) { diff --git a/source/Indy.IL2CPU/Engine.cs b/source/Indy.IL2CPU/Engine.cs index c9172a368..75aafb4f1 100644 --- a/source/Indy.IL2CPU/Engine.cs +++ b/source/Indy.IL2CPU/Engine.cs @@ -16,6 +16,7 @@ using System.Collections.ObjectModel; using System.Diagnostics.SymbolStore; using Microsoft.Samples.Debugging.CorSymbolStore; using System.Threading; +using System.Diagnostics; namespace Indy.IL2CPU { public enum DebugMode { None, IL, Source, MLUsingGDB } @@ -324,7 +325,14 @@ namespace Indy.IL2CPU { } finally { if (aUseBinaryEmission) { using (Stream xOutStream = new FileStream(Path.Combine(aOutputDir, "output.bin"), FileMode.Create)) { - mAssembler.FlushBinary(xOutStream, 0x200000); + Stopwatch xSW = new Stopwatch(); + xSW.Start(); + try { + mAssembler.FlushBinary(xOutStream, 0x200000); + } finally { + xSW.Stop(); + Debug.WriteLine(String.Format("Binary Emission took: {0}", xSW.Elapsed)); + } } } else