/// <summary>
        /// Initializes a new instance of the <see cref="TestCaseMethodCompiler"/> class.
        /// </summary>
        /// <param name="compiler">The compiler.</param>
        /// <param name="method">The method.</param>
        public TestCaseMethodCompiler(TestCaseCompiler compiler, RuntimeMethod method)
            : base(compiler, method, null)
        {
            // Populate the pipeline
            Pipeline.AddRange(new IMethodCompilerStage[] {
                new CILDecodingStage(),
                new BasicBlockBuilderStage(),
                new ExceptionPrologueStage(),
                new OperandAssignmentStage(),
                new StaticAllocationResolutionStage(),
                new CILTransformationStage(),

                new LocalVariablePromotionStage(),
                new	EdgeSplitStage(),
                new DominanceCalculationStage(),
                new PhiPlacementStage(),
                new EnterSSAStage(),
                new SSAOptimizations(),
                new LeaveSSA(),

                new StackLayoutStage(),
                new PlatformIntrinsicTransformationStage(),
                new PlatformStubStage(),
                new LoopAwareBlockOrderStage(),
                //new SimpleTraceBlockOrderStage(),
                //new ReverseBlockOrderStage(),  // reverse all the basic blocks and see if it breaks anything
                new CodeGenerationStage(),
            });
        }
Пример #2
0
        /// <summary>
        /// Parses the specified attribute blob and instantiates the attribute.
        /// </summary>
        /// <param name="blob">The BLOB.</param>
        /// <param name="attributeCtor">The constructor of the attribute.</param>
        /// <returns>
        /// The fully instantiated and initialized attribute.
        /// </returns>
        public static object[] Parse(byte[] blob, RuntimeMethod attributeCtor)
        {
            if (blob == null)
                throw new ArgumentException(@"Invalid attribute blob token.", @"attributeBlob");

            if (blob.Length == 0)
                return null;

            // Create a binary reader for the blob
            using (var reader = new BinaryReader(new MemoryStream(blob), Encoding.UTF8))
            {
                var prologue = reader.ReadUInt16();
                Debug.Assert(prologue == ATTRIBUTE_BLOB_PROLOGUE, @"Attribute prologue doesn't match.");
                if (prologue != ATTRIBUTE_BLOB_PROLOGUE)
                    throw new ArgumentException(@"Invalid custom attribute blob.", "attributeBlob");

                var parameters = attributeCtor.Parameters.Count;

                object[] args = new object[parameters];
                for (int idx = 0; idx < parameters; idx++)
                    args[idx] = ParseFixedArg(reader, attributeCtor.Signature.Parameters[idx]);

                // Create the attribute instance
                return args;
            }
        }
Пример #3
0
 /// <summary>
 /// Initializes a new instance of the <see cref="RuntimeAttribute"/> class.
 /// </summary>
 /// <param name="typeModule">The type module.</param>
 /// <param name="ctor">The ctor.</param>
 /// <param name="ctorMethod">The ctor method.</param>
 /// <param name="blobIndex">Index of the blob.</param>
 public RuntimeAttribute(ITypeModule typeModule, Token ctor, RuntimeMethod ctorMethod, HeapIndexToken blobIndex)
 {
     this.typeModule = typeModule;
     this.ctorMethod = ctorMethod;
     this.ctor = ctor;
     this.blobIndex = blobIndex;
 }
        public TestCaseMethodCompiler(TestCaseAssemblyCompiler assemblyCompiler, ICompilationSchedulerStage compilationScheduler, RuntimeType type, RuntimeMethod method)
            : base(assemblyCompiler, type, method, null, compilationScheduler)
        {
            // Populate the pipeline
            this.Pipeline.AddRange(new IMethodCompilerStage[] {
                new DecodingStage(),
                new BasicBlockBuilderStage(),
                new ExceptionPrologueStage(),
                new OperandDeterminationStage(),
                new StaticAllocationResolutionStage(),
                new CILTransformationStage(),
                //new CILLeakGuardStage() { MustThrowCompilationException = true },

                new	EdgeSplitStage(),
                new DominanceCalculationStage(),
                new PhiPlacementStage(),
                new EnterSSAStage(),

                new ConstantPropagationStage(ConstantPropagationStage.PropagationStage.PreFolding),
                new ConstantFoldingStage() ,
                new ConstantPropagationStage(ConstantPropagationStage.PropagationStage.PostFolding),

                new LeaveSSA(),

                new StrengthReductionStage(),
                new StackLayoutStage(),
                new PlatformStubStage(),
                //new BlockReductionStage(),
                //new LoopAwareBlockOrderStage(),
                new SimpleTraceBlockOrderStage(),
                //new ReverseBlockOrderStage(),  // reverse all the basic blocks and see if it breaks anything
                //new BasicBlockOrderStage()
                new CodeGenerationStage(),
            });
        }
Пример #5
0
        public ExplorerMethodCompiler(ExplorerAssemblyCompiler assemblyCompiler, ICompilationSchedulerStage compilationScheduler, RuntimeType type, RuntimeMethod method, CompilerOptions compilerOptions)
            : base(assemblyCompiler, type, method, null, compilationScheduler)
        {
            // Populate the pipeline
            this.Pipeline.AddRange(new IMethodCompilerStage[] {
                new DecodingStage(),
                new BasicBlockBuilderStage(),
                new ExceptionPrologueStage(),
                new OperandDeterminationStage(),
                //new SingleUseMarkerStage(),
                //new OperandUsageAnalyzerStage(),
                new StaticAllocationResolutionStage(),
                new CILTransformationStage(),

                (compilerOptions.EnableSSA) ? new EdgeSplitStage() : null,
                (compilerOptions.EnableSSA) ? new DominanceCalculationStage() : null,
                (compilerOptions.EnableSSA) ? new PhiPlacementStage() : null,
                (compilerOptions.EnableSSA) ? new EnterSSAStage() : null,

                (compilerOptions.EnableSSA) ? new SSAOptimizations() : null,
                //(compilerOptions.EnableSSA) ? new ConstantPropagationStage(ConstantPropagationStage.PropagationStage.PreFolding) : null,
                //(compilerOptions.EnableSSA) ? new ConstantFoldingStage() : null,
                //(compilerOptions.EnableSSA) ? new ConstantPropagationStage(ConstantPropagationStage.PropagationStage.PostFolding) : null,

                (compilerOptions.EnableSSA) ? new LeaveSSA() : null,

                new StrengthReductionStage(),
                new StackLayoutStage(),
                new PlatformStubStage(),
                //new LoopAwareBlockOrderStage(),
                new SimpleTraceBlockOrderStage(),
                //new SimpleRegisterAllocatorStage(),
                new CodeGenerationStage(),
            });
        }
Пример #6
0
        /// <summary>
        /// Initializes a new instance of the <see cref="AotMethodCompiler"/> class.
        /// </summary>
        /// <param name="compiler">The compiler.</param>
        /// <param name="method">The method.</param>
        public AotMethodCompiler(BaseCompiler compiler, RuntimeMethod method)
            : base(compiler, method, null)
        {
            var compilerOptions = compiler.CompilerOptions;

            Pipeline.AddRange(new IMethodCompilerStage[] {
                new CILDecodingStage(),
                new BasicBlockBuilderStage(),
                new ExceptionPrologueStage(),
                new OperandAssignmentStage(),
                new StaticAllocationResolutionStage(),
                new CILTransformationStage(),

                new IRCheckStage(),

                (compilerOptions.EnableSSA && compilerOptions.EnableSSAOptimizations) ? new LocalVariablePromotionStage() : null,
                (compilerOptions.EnableSSA) ? new EdgeSplitStage() : null,
                (compilerOptions.EnableSSA) ? new DominanceCalculationStage() : null,
                (compilerOptions.EnableSSA) ? new PhiPlacementStage() : null,
                (compilerOptions.EnableSSA) ? new EnterSSAStage() : null,
                (compilerOptions.EnableSSA && compilerOptions.EnableSSAOptimizations) ? new SSAOptimizations() : null,
                (compilerOptions.EnableSSA) ? new LeaveSSA() : null,

                new StackLayoutStage(),
                new PlatformIntrinsicTransformationStage(),
                new PlatformStubStage(),
                new LoopAwareBlockOrderStage(),
                //new SimpleTraceBlockOrderStage(),
                //new ReverseBlockOrderStage(),
                //new LocalCSE(),
                new CodeGenerationStage(),
                //new RegisterUsageAnalyzerStage(),
            });
        }
Пример #7
0
        void ICompilationScheduler.TrackMethodInvoked(RuntimeMethod method)
        {
            methodsInvoked.AddIfNew(method);

            if (compileAllMethods)
                CompileMethod(method);
        }
Пример #8
0
        /// <summary>
        /// Initializes a new instance of the <see cref="MemberOperand"/> class.
        /// </summary>
        /// <param name="method">The method to reference.</param>
        /// <exception cref="System.ArgumentNullException"><paramref name="method"/> is null.</exception>
        public MemberOperand(RuntimeMethod method)
            : base(null, BuiltInSigType.IntPtr, IntPtr.Zero)
        {
            if (method == null)
                throw new ArgumentNullException(@"method");

            this.member = method;
        }
Пример #9
0
        /// <summary>
        /// Gets the plug.
        /// </summary>
        /// <param name="method">The method.</param>
        /// <returns></returns>
        public RuntimeMethod GetPlugMethod(RuntimeMethod method)
        {
            RuntimeMethod plug = null;

            plugMethods.TryGetValue(method, out plug);

            return plug;
        }
        void ICompilationSchedulerStage.ScheduleMethodForCompilation(RuntimeMethod method)
        {
            if (method == null)
                throw new ArgumentNullException(@"method");

            if (!method.IsGeneric)
            {
                Trace(CompilerEvent.SchedulingMethod, method.ToString());
                methodQueue.Enqueue(method);
            }
        }
Пример #11
0
        /// <summary>
        /// Initializes a new instance of the <see cref="LinkerMethodCompiler"/> class.
        /// </summary>
        /// <param name="assemblyCompiler">The assembly compiler executing this method compiler.</param>
        /// <param name="method">The metadata of the method to compile.</param>
        /// <param name="instructionSet">The instruction set.</param>
        /// <exception cref="System.ArgumentNullException"><paramref name="assemblyCompiler"/>, <paramref name="method"/> or <paramref name="instructionSet"/> is null.</exception>
        public LinkerMethodCompiler(AssemblyCompiler assemblyCompiler, ICompilationSchedulerStage compilationScheduler, RuntimeMethod method, InstructionSet instructionSet)
            : base(assemblyCompiler, method.DeclaringType, method,  instructionSet, compilationScheduler)
        {
            this.CreateBlock(-1, 0);

            this.Pipeline.AddRange(new IMethodCompilerStage[] {
                new SimpleTraceBlockOrderStage(),
                new PlatformStubStage(),
                new CodeGenerationStage(),
            });

            assemblyCompiler.Architecture.ExtendMethodCompilerPipeline(this.Pipeline);
        }
Пример #12
0
        /// <summary>
        /// Initializes a new instance of the <see cref="LinkerMethodCompiler"/> class.
        /// </summary>
        /// <param name="compiler">The assembly compiler executing this method compiler.</param>
        /// <param name="method">The metadata of the method to compile.</param>
        /// <param name="instructionSet">The instruction set.</param>
        public LinkerMethodCompiler(BaseCompiler compiler, RuntimeMethod method, InstructionSet instructionSet)
            : base(compiler, method, instructionSet)
        {
            BasicBlocks.CreateBlock(BasicBlock.PrologueLabel, 0);
            BasicBlocks.AddHeaderBlock(BasicBlocks.PrologueBlock);

            this.Pipeline.AddRange(new IMethodCompilerStage[] {
                new LoopAwareBlockOrderStage(),
                new PlatformStubStage(),
                new CodeGenerationStage(),
            });

            compiler.Architecture.ExtendMethodCompilerPipeline(this.Pipeline);
        }
Пример #13
0
        /// <summary>
        /// Determines if the given method is a method 
        /// of a generic class that uses a generic parameter.
        /// </summary>
        /// <param name="method">The method to check</param>
        /// <returns>True if the method relies upon generic parameters</returns>
        public static bool HasGenericParameters(RuntimeMethod method)
        {
            // Check return type
            if (IsGenericParameter(method.Signature.ReturnType))
                return true;

            // Check parameters
            foreach (SigType parameter in method.Signature.Parameters)
            {
                if (IsGenericParameter(parameter))
                    return true;
            }

            return false;
        }
Пример #14
0
        public static void Run(IInternalTrace internalLog, IPipelineStage stage, RuntimeMethod method, InstructionSet instructionSet, BasicBlocks basicBlocks)
        {
            if (internalLog == null)
                return;

            if (internalLog.TraceListener == null)
                return;

            if (!internalLog.TraceFilter.IsMatch(method, stage.Name))
                return;

            StringBuilder text = new StringBuilder();

            // Line number
            int index = 1;

            text.AppendLine(String.Format("IR representation of method {0} after stage {1}:", method, stage.Name));
            text.AppendLine();

            if (basicBlocks.Count > 0)
            {
                foreach (BasicBlock block in basicBlocks)
                {
                    text.AppendFormat("Block #{0} - Label L_{1:X4}", index, block.Label);
                    if (basicBlocks.IsHeaderBlock(block))
                        text.Append(" [Header]");
                    text.AppendLine();

                    text.AppendFormat("  Prev: ");
                    text.AppendLine(ListBlocks(block.PreviousBlocks));

                    LogInstructions(text, new Context(instructionSet, block));

                    text.AppendFormat("  Next: ");
                    text.AppendLine(ListBlocks(block.NextBlocks));

                    text.AppendLine();
                    index++;
                }
            }
            else
            {
                LogInstructions(text, new Context(instructionSet, 0));
            }

            internalLog.TraceListener.SubmitInstructionTraceInformation(method, stage.Name, text.ToString());
        }
        private void CompileMethod(RuntimeMethod method)
        {
            Trace(CompilerEvent.CompilingMethod, method.ToString());

            using (IMethodCompiler mc = compiler.CreateMethodCompiler(this, method.DeclaringType, method))
            {
                mc.Compile();

                //try
                //{
                //    mc.Compile();
                //}
                //catch (Exception e)
                //{
                //    HandleCompilationException(e);
                //    throw;
                //}
            }
        }
Пример #16
0
 private static List<RuntimeMethod> RecompileMethods(AssemblyCompiler compiler, List<Token> types, RuntimeMethod method)
 {
     throw new NotImplementedException();
 }
Пример #17
0
 private static void ReinsertMethods(List<RuntimeMethod> methods, RuntimeMethod method, RuntimeType type)
 {
     throw new NotImplementedException();
 }
Пример #18
0
        private int FindOverrideSlot(IList<RuntimeMethod> methodTable, RuntimeMethod method)
        {
            foreach (var baseMethod in methodTable)
            {
                if (baseMethod.Name.Equals(method.Name) && baseMethod.Signature.Matches(method.Signature))
                {
                    return methodTableOffsets[baseMethod];
                }
            }

            throw new InvalidOperationException(@"Failed to find override method slot.");
        }
Пример #19
0
 private static List<Token> GetTokenTypesForMethod(AssemblyCompiler compiler, RuntimeType type, RuntimeMethod method)
 {
     throw new NotImplementedException();
 }
Пример #20
0
        private RuntimeMethod FindInterfaceMethod(RuntimeType type, RuntimeMethod interfaceMethod)
        {
            var cleanInterfaceMethodName = GetCleanMethodName(interfaceMethod.Name);

            foreach (var method in type.Methods)
            {
                string cleanMethodName = GetCleanMethodName(method.Name);

                if (cleanInterfaceMethodName.Equals(cleanMethodName))
                {
                    if (interfaceMethod.Signature.Matches(method.Signature))
                    {
                        return method;
                    }
                }
            }

            if (type.BaseType != null)
            {
                return FindInterfaceMethod(type.BaseType, interfaceMethod);
            }

            throw new InvalidOperationException(@"Failed to find implicit interface implementation for type " + type + " and interface method " + interfaceMethod);
        }
Пример #21
0
        private void ScanExplicitInterfaceImplementations(RuntimeType type, RuntimeType interfaceType, RuntimeMethod[] methodTable)
        {
            //TODO: rewrite so that access directly to metadata is not required, type system should assist instead
            var metadata = type.Module.MetadataModule.Metadata;
            var maxToken = metadata.GetMaxTokenValue(TableType.MethodImpl);

            foreach (var token in new Token(TableType.MethodImpl, 1).Upto(maxToken))
            {
                MethodImplRow row = metadata.ReadMethodImplRow(token);
                if (row.@Class == type.Token)
                {
                    int slot = 0;
                    foreach (var interfaceMethod in interfaceType.Methods)
                    {
                        if (interfaceMethod.Token == row.MethodDeclaration)
                        {
                            methodTable[slot] = FindMethodByToken(type, row.MethodBody);
                        }
                        slot++;
                    }
                }
            }
        }
Пример #22
0
        /// <summary>
        /// Sets the invoke target.
        /// </summary>
        /// <param name="ctx">The context.</param>
        /// <param name="compiler">The compiler.</param>
        /// <param name="method">The method.</param>
        private static void SetInvokeTarget(Context ctx, IMethodCompiler compiler, RuntimeMethod method)
        {
            if (method == null)
                throw new ArgumentNullException(@"method");

            // Signature of the call target
            // Number of parameters required for the call

            ctx.InvokeTarget = method;

            // Retrieve the target signature
            MethodSignature signature = ctx.InvokeTarget.Signature;

            // Fix the parameter list
            byte paramCount = (byte)signature.Parameters.Length;
            if (signature.HasThis && !signature.HasExplicitThis)
                paramCount++;

            // Setup operands for parameters and the return value
            if (signature.ReturnType.Type != CilElementType.Void)
            {
                ctx.ResultCount = 1;
                ctx.Result = compiler.CreateTemporary(signature.ReturnType);
            }
            else
                ctx.ResultCount = 0;

            ctx.OperandCount = paramCount;
        }
Пример #23
0
        /// <summary>
        /// Gets the interface table.
        /// </summary>
        /// <param name="type">The type.</param>
        /// <param name="interfaceType">Type of the interface.</param>
        /// <returns></returns>
        RuntimeMethod[] ITypeLayout.GetInterfaceTable(RuntimeType type, RuntimeType interfaceType)
        {
            if (type.Interfaces.Count == 0)
                return null;

            ResolveType(type);

            RuntimeMethod[] methodTable = new RuntimeMethod[interfaceType.Methods.Count];

            // Implicit Interface Methods
            for (int slot = 0; slot < interfaceType.Methods.Count; slot++)
                methodTable[slot] = FindInterfaceMethod(type, interfaceType.Methods[slot]);

            // Explicit Interface Methods
            ScanExplicitInterfaceImplementations(type, interfaceType, methodTable);

            return methodTable;
        }
 bool ITraceFilter.IsMatch(RuntimeMethod method, string stage)
 {
     return IsMatch(method.DeclaringType.Name, method.Name, stage);
 }
Пример #25
0
 public void AddMethod(RuntimeMethod method)
 {
     this.methods.Add(method);
 }
Пример #26
0
 public void CreatePlug(RuntimeMethod plug, RuntimeMethod methodToPlug)
 {
     plugMethods.Add(methodToPlug, plug);
 }
Пример #27
0
 /// <summary>
 /// Adds the method.
 /// </summary>
 /// <param name="method">The method.</param>
 public void AddMethod(RuntimeMethod method)
 {
     if (!methods.Contains(method))
         methods.Add(method);
 }
 public override IMethodCompiler CreateMethodCompiler(ICompilationSchedulerStage schedulerStage, RuntimeType type, RuntimeMethod method)
 {
     IMethodCompiler mc = new TestCaseMethodCompiler(this, schedulerStage, type, method);
     Architecture.ExtendMethodCompilerPipeline(mc.Pipeline);
     return mc;
 }
Пример #29
0
 /// <summary>
 /// Creates a method compiler
 /// </summary>
 /// <param name="method">The method to compile.</param>
 /// <returns>
 /// An instance of a MethodCompilerBase for the given type/method pair.
 /// </returns>
 public override BaseMethodCompiler CreateMethodCompiler(RuntimeMethod method)
 {
     return new TestCaseMethodCompiler(this, method);
 }
Пример #30
0
 /// <summary>
 /// Gets the method table offset.
 /// </summary>
 /// <param name="method">The method.</param>
 /// <returns></returns>
 int ITypeLayout.GetMethodTableOffset(RuntimeMethod method)
 {
     ResolveType(method.DeclaringType);
     return methodTableOffsets[method];
 }