void WriteStructureBody(ILStructure s, HashSet <int> branchTargets, ref BlobReader body)
        {
            bool isFirstInstructionInStructure = true;
            bool prevInstructionWasBranch      = false;
            int  childIndex = 0;

            while (body.RemainingBytes > 0 && body.Offset < s.EndOffset)
            {
                int offset = body.Offset;
                if (childIndex < s.Children.Count && s.Children[childIndex].StartOffset <= offset && offset < s.Children[childIndex].EndOffset)
                {
                    ILStructure child = s.Children[childIndex++];
                    WriteStructureHeader(child);
                    WriteStructureBody(child, branchTargets, ref body);
                    WriteStructureFooter(child);
                }
                else
                {
                    if (!isFirstInstructionInStructure && (prevInstructionWasBranch || branchTargets.Contains(offset)))
                    {
                        output.WriteLine();                         // put an empty line after branches, and in front of branch targets
                    }
                    var currentOpCode = ILParser.DecodeOpCode(ref body);
                    body.Offset = offset;                     // reset IL stream
                    WriteInstruction(output, metadata, s.MethodHandle, ref body);
                    prevInstructionWasBranch = currentOpCode.IsBranch() ||
                                               currentOpCode.IsReturn() ||
                                               currentOpCode == ILOpCode.Throw ||
                                               currentOpCode == ILOpCode.Rethrow ||
                                               currentOpCode == ILOpCode.Switch;
                }
                isFirstInstructionInStructure = false;
            }
        }
        void WriteStructureFooter(ILStructure s)
        {
            output.Unindent();
            switch (s.Type)
            {
            case ILStructureType.Loop:
                output.WriteLine("// end loop");
                break;

            case ILStructureType.Try:
                output.WriteLine("} // end .try");
                break;

            case ILStructureType.Handler:
                output.WriteLine("} // end handler");
                break;

            case ILStructureType.Filter:
                output.WriteLine("} // end filter");
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
        void WriteStructureHeader(ILStructure s)
        {
            switch (s.Type)
            {
            case ILStructureType.Loop:
                output.Write("// loop start");
                if (s.LoopEntryPointOffset >= 0)
                {
                    output.Write(" (head: ");
                    DisassemblerHelpers.WriteOffsetReference(output, s.LoopEntryPointOffset);
                    output.Write(')');
                }
                output.WriteLine();
                break;

            case ILStructureType.Try:
                output.WriteLine(".try");
                output.WriteLine("{");
                break;

            case ILStructureType.Handler:
                switch (s.ExceptionHandler.Kind)
                {
                case ExceptionRegionKind.Catch:
                case ExceptionRegionKind.Filter:
                    output.Write("catch");
                    if (!s.ExceptionHandler.CatchType.IsNil)
                    {
                        output.Write(' ');
                        s.ExceptionHandler.CatchType.WriteTo(s.Module, output, s.GenericContext, ILNameSyntax.TypeName);
                    }
                    output.WriteLine();
                    break;

                case ExceptionRegionKind.Finally:
                    output.WriteLine("finally");
                    break;

                case ExceptionRegionKind.Fault:
                    output.WriteLine("fault");
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }
                output.WriteLine("{");
                break;

            case ILStructureType.Filter:
                output.WriteLine("filter");
                output.WriteLine("{");
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }
            output.Indent();
        }
示例#4
0
        bool AddNestedStructure(ILStructure newStructure)
        {
            // special case: don't consider the loop-like structure of "continue;" statements to be nested loops
            if (this.Type == ILStructureType.Loop && newStructure.Type == ILStructureType.Loop && newStructure.StartOffset == this.StartOffset)
            {
                return(false);
            }

            // use <= for end-offset comparisons because both end and EndOffset are exclusive
            Debug.Assert(StartOffset <= newStructure.StartOffset && newStructure.EndOffset <= EndOffset);
            foreach (ILStructure child in this.Children)
            {
                if (child.StartOffset <= newStructure.StartOffset && newStructure.EndOffset <= child.EndOffset)
                {
                    return(child.AddNestedStructure(newStructure));
                }
                else if (!(child.EndOffset <= newStructure.StartOffset || newStructure.EndOffset <= child.StartOffset))
                {
                    // child and newStructure overlap
                    if (!(newStructure.StartOffset <= child.StartOffset && child.EndOffset <= newStructure.EndOffset))
                    {
                        // Invalid nesting, can't build a tree. -> Don't add the new structure.
                        return(false);
                    }
                }
            }
            // Move existing structures into the new structure:
            for (int i = 0; i < this.Children.Count; i++)
            {
                ILStructure child = this.Children[i];
                if (newStructure.StartOffset <= child.StartOffset && child.EndOffset <= newStructure.EndOffset)
                {
                    this.Children.RemoveAt(i--);
                    newStructure.Children.Add(child);
                }
            }
            // Add the structure here:
            this.Children.Add(newStructure);
            return(true);
        }