/// <summary> /// Initializes a new instance of the <see cref="SimMethodCompiler" /> class. /// </summary> /// <param name="compiler">The compiler.</param> /// <param name="method">The method.</param> /// <param name="simAdapter">The sim adapter.</param> /// <param name="basicBlocks">The basic blocks.</param> /// <param name="instructionSet">The instruction set.</param> public SimMethodCompiler(SimCompiler compiler, MosaMethod method, ISimAdapter simAdapter, BasicBlocks basicBlocks, InstructionSet instructionSet) : base(compiler, method, basicBlocks, instructionSet) { var compilerOptions = Compiler.CompilerOptions; // Populate the pipeline Pipeline.Add(new IMethodCompilerStage[] { new CILDecodingStage(), new BasicBlockBuilderStage(), new StackSetupStage(), new ExceptionPrologueStage(), new OperandAssignmentStage(), //new SingleUseMarkerStage(), //new OperandUsageAnalyzerStage(), new StaticAllocationResolutionStage(), new CILTransformationStage(), new ConvertCompoundMoveStage(), //new CheckIROperandCountStage(), (compilerOptions.EnableSSA) ? new PromoteLocalVariablesStage() : null, (compilerOptions.EnableSSA) ? new EdgeSplitStage() : null, (compilerOptions.EnableSSA) ? new PhiPlacementStage() : null, (compilerOptions.EnableSSA) ? new EnterSSAStage() : null, (compilerOptions.EnableSSA && compilerOptions.EnableSSAOptimizations) ? new SSAOptimizations() : null, (compilerOptions.EnableSSA) ? new LeaveSSA() : null, (compilerOptions.EnableSSA) ? new ConvertCompoundMoveStage() : null, new PlatformStubStage(), //new CheckPlatformOperandCountStage(), new PlatformEdgeSplitStage(), new GreedyRegisterAllocatorStage(), new StackLayoutStage(), new EmptyBlockRemovalStage(), new BlockOrderingStage(), new SimCodeGeneratorStage(simAdapter), }); }
public override object GetResult(ISimAdapter simAdapter, MosaType type) { var x86 = simAdapter.SimCPU as CPUx86; if (type.IsI1) return (object)(sbyte)x86.EAX.Value; else if (type.IsI2) return (object)(short)x86.EAX.Value; else if (type.IsI4) return (object)(int)x86.EAX.Value; else if (type.IsI8) return (object)(long)(((ulong)x86.EAX.Value) | ((ulong)x86.EDX.Value << 32)); else if (type.IsU1) return (object)(byte)x86.EAX.Value; else if (type.IsU2) return (object)(ushort)x86.EAX.Value; else if (type.IsU4) return (object)(uint)x86.EAX.Value; else if (type.IsU8) return (object)(ulong)(((ulong)x86.EAX.Value) | ((ulong)x86.EDX.Value << 32)); else if (type.IsChar) return (object)(char)x86.EAX.Value; else if (type.IsBoolean) return (object)(bool)(x86.EAX.Value != 0); else if (type.IsR4) return (object)(float)x86.XMM0.Value.LowF; else if (type.IsR8) return (object)(double)x86.XMM0.Value.Low; else if (type.IsVoid) return null; return null; }
/// <summary> /// Compiles the specified type system. /// </summary> /// <param name="typeSystem">The type system.</param> /// <param name="typeLayout">The type layout.</param> /// <param name="compilerTrace">The compiler trace.</param> /// <param name="compilerOptions">The compiler options.</param> /// <param name="architecture">The architecture.</param> /// <param name="simAdapter">The sim adapter.</param> /// <param name="linker">The linker.</param> /// <returns></returns> public static SimCompiler Compile(TypeSystem typeSystem, MosaTypeLayout typeLayout, CompilerTrace compilerTrace, CompilerOptions compilerOptions, BaseArchitecture architecture, ISimAdapter simAdapter, BaseLinker linker) { var compiler = new SimCompiler(architecture, typeSystem, typeLayout, linker, compilerOptions, compilerTrace, simAdapter); compiler.Compile(); return compiler; }
/// <summary> /// Initializes a new instance of the <see cref="SimLinkerSection" /> class. /// </summary> /// <param name="kind">The kind of the section.</param> /// <param name="name">The name.</param> /// <param name="address">The address.</param> /// <param name="size">The size.</param> /// <param name="simAdapter">The sim adapter.</param> public SimLinkerSection(SectionKind kind, string name, uint address, uint size, ISimAdapter simAdapter) : base(kind, name, 0) { Memory = new byte[size]; VirtualAddress = address; simAdapter.SimCPU.AddMemory(address, size, 1); stream = new MemoryStream(Memory, true); }
/// <summary> /// Compiles the specified type system. /// </summary> /// <param name="typeSystem">The type system.</param> /// <param name="typeLayout">The type layout.</param> /// <param name="internalTrace">The internal trace.</param> /// <param name="enabledSSA">if set to <c>true</c> [enabled ssa].</param> /// <param name="architecture">The architecture.</param> /// <param name="simAdapter">The sim adapter.</param> /// <param name="linker">The linker.</param> /// <returns></returns> public static SimCompiler Compile(TypeSystem typeSystem, MosaTypeLayout typeLayout, IInternalTrace internalTrace, bool enabledSSA, BaseArchitecture architecture, ISimAdapter simAdapter, ILinker linker) { CompilerOptions compilerOptions = new CompilerOptions(); compilerOptions.EnableSSA = enabledSSA; compilerOptions.EnableSSAOptimizations = enabledSSA; SimCompiler compiler = new SimCompiler(architecture, typeSystem, typeLayout, linker, compilerOptions, internalTrace, simAdapter); compiler.Compile(); return compiler; }
/// <summary> /// Initializes a new instance of the <see cref="SimLinker" /> class. /// </summary> /// <param name="simAdapter">The sim adapter.</param> public SimLinker(ISimAdapter simAdapter) { this.SimAdapter = simAdapter; AddSection(new SimLinkerSection(SectionKind.BSS, "BSS", 0x400000, 0x200000, simAdapter)); AddSection(new SimLinkerSection(SectionKind.Data, "Data", 0x600000, 0x200000, simAdapter)); AddSection(new SimLinkerSection(SectionKind.ROData, "ReadOnlyData", 0x800000, 0x200000, simAdapter)); AddSection(new SimLinkerSection(SectionKind.Text, "Text", 0xA00000, 0x200000, simAdapter)); LoadSectionAlignment = 1; SectionAlignment = 1; Endianness = Endianness.Little; // FIXME: assumes x86 }
/// <summary> /// Prevents a default instance of the <see cref="SimCompiler" /> class from being created. /// </summary> /// <param name="architecture">The compiler target architecture.</param> /// <param name="typeSystem">The type system.</param> /// <param name="typeLayout">The type layout.</param> /// <param name="linker">The linker.</param> /// <param name="compilerOptions">The compiler options.</param> /// <param name="internalTrace">The internal trace.</param> /// <param name="simAdapter">The sim adapter.</param> public SimCompiler(BaseArchitecture architecture, TypeSystem typeSystem, MosaTypeLayout typeLayout, ILinker linker, CompilerOptions compilerOptions, IInternalTrace internalTrace, ISimAdapter simAdapter) : base(architecture, typeSystem, typeLayout, new CompilationScheduler(typeSystem, true), internalTrace, linker, compilerOptions) { this.simAdapter = simAdapter; // Build the assembly compiler pipeline Pipeline.Add(new ICompilerStage[] { new PlugStage(), new MethodCompilerSchedulerStage(), new TypeInitializerSchedulerStage(), new SimPowerUpStage(), new TypeLayoutStage(), new MetadataStage(), new LinkerFinalizationStage(), }); architecture.ExtendCompilerPipeline(Pipeline); }
/// <summary> /// Initializes a new instance of the <see cref="SimMethodCompiler" /> class. /// </summary> /// <param name="compiler">The compiler.</param> /// <param name="method">The method.</param> /// <param name="simAdapter">The sim adapter.</param> /// <param name="basicBlocks">The basic blocks.</param> /// <param name="threadID">The thread identifier.</param> public SimMethodCompiler(SimCompiler compiler, MosaMethod method, ISimAdapter simAdapter, BasicBlocks basicBlocks, int threadID) : base(compiler, method, basicBlocks, threadID) { var compilerOptions = Compiler.CompilerOptions; // Populate the pipeline Pipeline.Add(new IMethodCompilerStage[] { new CILDecodingStage(), new ExceptionPrologueStage(), new OperandAssignmentStage(), new StackSetupStage(), new ProtectedRegionStage(), new StaticAllocationResolutionStage(), new CILTransformationStage(), new ConvertCompoundStage(), new UnboxValueTypeStage(), new ExceptionStage(), (compilerOptions.EnableInlinedMethods) ? new InlineStage() : null, (compilerOptions.EnableVariablePromotion) ? new PromoteTempVariablesStage() : null, (compilerOptions.EnableSSA) ? new EdgeSplitStage() : null, (compilerOptions.EnableSSA) ? new PhiPlacementStage() : null, (compilerOptions.EnableSSA) ? new EnterSSAStage() : null, (compilerOptions.EnableSparseConditionalConstantPropagation && compilerOptions.EnableSSA) ? new SparseConditionalConstantPropagationStage() : null, (compilerOptions.EnableOptimizations) ? new IROptimizationStage() : null, (compilerOptions.EnableSSA) ? new LeaveSSA() : null, new IRCleanup(), (compilerOptions.EnableInlinedMethods) ? new InlineEvaluationStage() : null, new PlatformStubStage(), new PlatformEdgeSplitStage(), new GreedyRegisterAllocatorStage(), new StackLayoutStage(), new EmptyBlockRemovalStage(), new BlockOrderingStage(), new SimCodeGeneratorStage(simAdapter), new ProtectedRegionLayoutStage(), }); }
public abstract void ResetSimulation(ISimAdapter simAdapter);
public SimLinkerFinalizationStage(ISimAdapter simAdapter) { this.simAdapter = simAdapter; }
public override void InitializeSimulation(ISimAdapter simAdapter) { //simAdapter.SimCPU.AddInstruction(StopEIP, new SimInstruction(Opcode.Nop, 1)); }
private void WriteStackValue(ISimAdapter simAdapter, ulong value) { var x86 = simAdapter.SimCPU as CPUx86; x86.ESP.Value = x86.ESP.Value - 8; x86.Write64(x86.ESP.Value, value); }
public override void ResetSimulation(ISimAdapter simAdapter) { simAdapter.SimCPU.Monitor.ClearBreakPoints(); simAdapter.SimCPU.Monitor.AddBreakPoint(StopEIP); simAdapter.SimCPU.Tick = 0; simAdapter.SimCPU.LastException = null; simAdapter.SimCPU.Monitor.Stop = false; var x86 = simAdapter.SimCPU as CPUx86; x86.Reset(); // Set start of stack x86.ESP.Value = 0x00080000; x86.EBP.Value = x86.ESP.Value; simAdapter.SimCPU.Write8(StopEIP, 0x90); }
public override void InitializeSimulation(ISimAdapter simAdapter) { simAdapter.SimCPU.AddMemory(0x00000000, 0x000A0000, 1); // First 640kb simAdapter.SimCPU.Monitor.AddBreakPoint(StopEIP); }
public override void ResetSimulation(ISimAdapter simAdapter) { var x86 = simAdapter.SimCPU as CPUx86; x86.Reset(); // Start of stack x86.ESP.Value = 0x00080000; x86.EBP.Value = x86.ESP.Value; }
public override void PrepareToExecuteMethod(ISimAdapter simAdapter, ulong address) { var x86 = simAdapter.SimCPU as CPUx86; x86.EIP.Value = (uint)address; // push the return address on stack WriteStackValue(simAdapter, StopEIP); }
public override void PopulateStack(ISimAdapter simAdapter, params object[] parameters) { for (int i = parameters.Length - 1; i >= 0; i--) { PopulateStack(simAdapter, parameters[i]); } }
public abstract object GetResult(ISimAdapter simAdapter, MosaType type);
public abstract void PrepareToExecuteMethod(ISimAdapter simAdapter, ulong address, params object[] parameters);
public abstract void PopulateStack(ISimAdapter simAdapter, params object[] parameters);
public abstract void PrepareToExecuteMethod(ISimAdapter simAdapter, ulong address);
public void PopulateStack(ISimAdapter simAdapter, object parameter) { if ((parameter == null) || !(parameter is ValueType)) { throw new InvalidProgramException(); } if (parameter is Boolean) { WriteStackValue(simAdapter, (bool)parameter ? 1 : (uint)0); } else if (parameter is Char) { WriteStackValue(simAdapter, (char)parameter); } else if (parameter is SByte) { WriteStackValue(simAdapter, (uint)(sbyte)parameter); } else if (parameter is Int16) { WriteStackValue(simAdapter, (uint)(short)parameter); } else if (parameter is int) { WriteStackValue(simAdapter, (uint)(int)parameter); } else if (parameter is Byte) { WriteStackValue(simAdapter, (byte)parameter); } else if (parameter is UInt16) { WriteStackValue(simAdapter, (ushort)parameter); } else if (parameter is UInt32) { WriteStackValue(simAdapter, (uint)parameter); } else if (parameter is UInt64) { WriteStackValue(simAdapter, (uint)((ulong)parameter >> 32)); WriteStackValue(simAdapter, (uint)(ulong)parameter); } else if (parameter is Int64) { WriteStackValue(simAdapter, (uint)((long)parameter >> 32)); WriteStackValue(simAdapter, (uint)(long)parameter); } else if (parameter is Single) { var b = BitConverter.GetBytes((float)parameter); var u = BitConverter.ToUInt32(b, 0); WriteStackValue(simAdapter, u); } else if (parameter is Double) { var b = BitConverter.GetBytes((double)parameter); var u = BitConverter.ToUInt64(b, 0); WriteStackValue(simAdapter, u); } //else if (parameter is UIntPtr) { WriteStackValue(simAdapter, (uint)parameter); } //else if (parameter is IntPtr) { WriteStackValue(simAdapter, (uint)parameter); } else { throw new InvalidProgramException(); } }
/// <summary> /// Prevents a default instance of the <see cref="SimCompiler" /> class from being created. /// </summary> /// <param name="compiler">The compiler.</param> /// <param name="simAdapter">The sim adapter.</param> public SimCompiler(ISimAdapter simAdapter) { this.simAdapter = simAdapter; }
public SimCodeGeneratorStage(ISimAdapter simAdapter) : base(true) { this.simAdapter = simAdapter; }
public override void PrepareToExecuteMethod(ISimAdapter simAdapter, ulong address, params object[] parameters) { var x86 = simAdapter.SimCPU as CPUx86; // Clear last exception x86.LastException = null; // Clear registers x86.EFLAGS.Value = 0; x86.EAX.Value = 0; x86.EBX.Value = 0; x86.ECX.Value = 0; x86.EDX.Value = 0; x86.ESI.Value = 0; // Set start of stack x86.ESP.Value = 0x00080000; x86.EBP.Value = x86.ESP.Value; // Write NOP instruction to 0x01000 simAdapter.SimCPU.Write8(StopEIP, 0x90); // Set starting address x86.EIP.Value = (uint)address; for (int i = parameters.Length - 1; i >= 0; i--) { PopulateStack(simAdapter, parameters[i]); } // Put return address on stack WriteStackValue(simAdapter, StopEIP); // Clear stop simAdapter.SimCPU.Monitor.Stop = false; }
/// <summary> /// Initializes a new instance of the <see cref="SimLinker" /> class. /// </summary> /// <param name="simAdapter">The sim adapter.</param> public SimLinker(ISimAdapter simAdapter) { this.simAdapter = simAdapter; }
private void WriteStackValue(ISimAdapter simAdapter, uint value) { var x86 = simAdapter.SimCPU as CPUx86; x86.ESP.Value = x86.ESP.Value - 4; x86.Write32(x86.ESP.Value, value); }
public abstract void InitializeSimulation(ISimAdapter simAdapter);
public void PopulateStack(ISimAdapter simAdapter, object parameter) { if ((parameter == null) || !(parameter is ValueType)) { throw new InvalidProgramException(); } if (parameter is Boolean) { WriteStackValue(simAdapter, (bool)parameter ? (uint)1 : (uint)0); } else if (parameter is Char) { WriteStackValue(simAdapter, (uint)(char)parameter); } else if (parameter is SByte) { WriteStackValue(simAdapter, (uint)(sbyte)parameter); } else if (parameter is Int16) { WriteStackValue(simAdapter, (uint)(short)parameter); } else if (parameter is Int32) { WriteStackValue(simAdapter, (uint)(int)parameter); } else if (parameter is Byte) { WriteStackValue(simAdapter, (uint)(byte)parameter); } else if (parameter is UInt16) { WriteStackValue(simAdapter, (uint)(ushort)parameter); } else if (parameter is UInt32) { WriteStackValue(simAdapter, (uint)(uint)parameter); } else if (parameter is UInt64) { WriteStackValue(simAdapter, (uint)((ulong)parameter >> 32)); WriteStackValue(simAdapter, (uint)(ulong)parameter); } else if (parameter is Int64) { WriteStackValue(simAdapter, (uint)((long)parameter >> 32)); WriteStackValue(simAdapter, (uint)(long)parameter); } else if (parameter is Single) { var b = BitConverter.GetBytes((float)parameter); var u = BitConverter.ToUInt32(b, 0); WriteStackValue(simAdapter, u); } else if (parameter is Double) { var b = BitConverter.GetBytes((double)parameter); var u = BitConverter.ToUInt64(b, 0); WriteStackValue(simAdapter, u); } //else if (parameter is UIntPtr) { WriteStackValue(simAdapter, (uint)parameter); } //else if (parameter is IntPtr) { WriteStackValue(simAdapter, (uint)parameter); } else { throw new InvalidProgramException(); } }
public abstract void PopulateStack(ISimAdapter simAdapter, object parameter);