示例#1
0
        private string GetField(IField field)
        {
            IDecompilerOutput output;
            var stringBuilder = new StringBuilder();
            var writer        = new StringWriter(stringBuilder);

            output = new TextWriterDecompilerOutput(writer);

            DisassemblerHelpers.WriteFieldTo(field, output);

            return(output.ToString());
        }
        public override string ToString()
        {
            StringWriter writer = new StringWriter();

            switch (NodeType)
            {
            case ControlFlowNodeType.Normal:
                writer.Write("Block #{0}", BlockIndex);
                if (Start != null)
                {
                    writer.Write(": IL_{0:x4}", Start.Offset);
                }
                if (End != null)
                {
                    writer.Write(" to IL_{0:x4}", End.GetEndOffset());
                }
                break;

            case ControlFlowNodeType.CatchHandler:
            case ControlFlowNodeType.FinallyOrFaultHandler:
                writer.Write("Block #{0}: {1}: ", BlockIndex, NodeType);
                DisassemblerHelpers.WriteTo(ExceptionHandler, new PlainTextOutput(writer));
                break;

            default:
                writer.Write("Block #{0}: {1}", BlockIndex, NodeType);
                break;
            }
//			if (ImmediateDominator != null) {
//				writer.WriteLine();
//				writer.Write("ImmediateDominator: #{0}", ImmediateDominator.BlockIndex);
//			}
            if (DominanceFrontier != null && DominanceFrontier.Any())
            {
                writer.WriteLine();
                writer.Write("DominanceFrontier: " + string.Join(",", DominanceFrontier.OrderBy(d => d.BlockIndex).Select(d => d.BlockIndex.ToString())));
            }
            foreach (Instruction inst in Instructions)
            {
                writer.WriteLine();
                DisassemblerHelpers.WriteTo(inst, new PlainTextOutput(writer));
            }
            if (UserData != null)
            {
                writer.WriteLine();
                writer.Write(UserData.ToString());
            }
            return(writer.ToString());
        }
示例#3
0
        public override void WriteTo(ITextOutput output)
        {
            if (Operand is ILVariable && ((ILVariable)Operand).IsGenerated)
            {
                if (Code == ILCode.Stloc && this.InferredType == null)
                {
                    output.Write(((ILVariable)Operand).Name);

                    output.Write(" = ");
                    Arguments.First().WriteTo(output);
                    return;
                }
                else if (Code == ILCode.Ldloc)
                {
                    output.Write(((ILVariable)Operand).Name);
                    if (this.InferredType != null)
                    {
                        output.Write(':');
                        this.InferredType.WriteTo(output, ILNameSyntax.ShortTypeName);
                        if (this.ExpectedType != null && this.ExpectedType.FullName != this.InferredType.FullName)
                        {
                            output.Write("[exp:");
                            this.ExpectedType.WriteTo(output, ILNameSyntax.ShortTypeName);
                            output.Write(']');
                        }
                    }
                    return;
                }
            }

            if (this.Prefixes != null)
            {
                foreach (var prefix in this.Prefixes)
                {
                    output.Write(prefix.Code.GetName());
                    output.Write(". ");
                }
            }

            output.Write(Code.GetName());
            if (this.InferredType != null)
            {
                output.Write(':');
                this.InferredType.WriteTo(output, ILNameSyntax.ShortTypeName);
                if (this.ExpectedType != null && this.ExpectedType.FullName != this.InferredType.FullName)
                {
                    output.Write("[exp:");
                    this.ExpectedType.WriteTo(output, ILNameSyntax.ShortTypeName);
                    output.Write(']');
                }
            }
            else if (this.ExpectedType != null)
            {
                output.Write("[exp:");
                this.ExpectedType.WriteTo(output, ILNameSyntax.ShortTypeName);
                output.Write(']');
            }
            output.Write('(');
            bool first = true;

            if (Operand != null)
            {
                if (Operand is ILLabel)
                {
                    output.WriteReference(((ILLabel)Operand).Name, Operand);
                }
                else if (Operand is ILLabel[])
                {
                    ILLabel[] labels = (ILLabel[])Operand;
                    for (int i = 0; i < labels.Length; i++)
                    {
                        if (i > 0)
                        {
                            output.Write(", ");
                        }
                        output.WriteReference(labels[i].Name, labels[i]);
                    }
                }
                else if (Operand is MethodReference)
                {
                    MethodReference method = (MethodReference)Operand;
                    if (method.DeclaringType != null)
                    {
                        method.DeclaringType.WriteTo(output, ILNameSyntax.ShortTypeName);
                        output.Write("::");
                    }
                    output.WriteReference(method.Name, method);
                }
                else if (Operand is FieldReference)
                {
                    FieldReference field = (FieldReference)Operand;
                    field.DeclaringType.WriteTo(output, ILNameSyntax.ShortTypeName);
                    output.Write("::");
                    output.WriteReference(field.Name, field);
                }
                else
                {
                    DisassemblerHelpers.WriteOperand(output, Operand);
                }
                first = false;
            }
            foreach (ILExpression arg in this.Arguments)
            {
                if (!first)
                {
                    output.Write(", ");
                }
                arg.WriteTo(output);
                first = false;
            }
            output.Write(')');
        }
示例#4
0
        public override void WriteTo(ITextOutput output, MemberMapping memberMapping)
        {
            var startLoc = output.Location;

            if (Operand is ILVariable && ((ILVariable)Operand).GeneratedByDecompiler)
            {
                if (Code == ILCode.Stloc && this.InferredType == null)
                {
                    output.WriteReference(((ILVariable)Operand).Name, Operand, ((ILVariable)Operand).IsParameter ? TextTokenKind.Parameter : TextTokenKind.Local);
                    output.WriteSpace();
                    output.Write("=", TextTokenKind.Operator);
                    output.WriteSpace();
                    Arguments.First().WriteTo(output, null);
                    UpdateMemberMapping(memberMapping, startLoc, output.Location, this.GetSelfAndChildrenRecursiveILRanges());
                    return;
                }
                else if (Code == ILCode.Ldloc)
                {
                    output.WriteReference(((ILVariable)Operand).Name, Operand, ((ILVariable)Operand).IsParameter ? TextTokenKind.Parameter : TextTokenKind.Local);
                    if (this.InferredType != null)
                    {
                        output.Write(":", TextTokenKind.Operator);
                        this.InferredType.WriteTo(output, ILNameSyntax.ShortTypeName);
                        if (this.ExpectedType != null && this.ExpectedType.FullName != this.InferredType.FullName)
                        {
                            output.Write("[", TextTokenKind.Operator);
                            output.Write("exp", TextTokenKind.Keyword);
                            output.Write(":", TextTokenKind.Operator);
                            this.ExpectedType.WriteTo(output, ILNameSyntax.ShortTypeName);
                            output.Write("]", TextTokenKind.Operator);
                        }
                    }
                    UpdateMemberMapping(memberMapping, startLoc, output.Location, this.GetSelfAndChildrenRecursiveILRanges());
                    return;
                }
            }

            if (this.Prefixes != null)
            {
                foreach (var prefix in this.Prefixes)
                {
                    output.Write(prefix.Code.GetName() + ".", TextTokenKind.OpCode);
                    output.WriteSpace();
                }
            }

            output.Write(Code.GetName(), TextTokenKind.OpCode);
            if (this.InferredType != null)
            {
                output.Write(":", TextTokenKind.Operator);
                this.InferredType.WriteTo(output, ILNameSyntax.ShortTypeName);
                if (this.ExpectedType != null && this.ExpectedType.FullName != this.InferredType.FullName)
                {
                    output.Write("[", TextTokenKind.Operator);
                    output.Write("exp", TextTokenKind.Keyword);
                    output.Write(":", TextTokenKind.Operator);
                    this.ExpectedType.WriteTo(output, ILNameSyntax.ShortTypeName);
                    output.Write("]", TextTokenKind.Operator);
                }
            }
            else if (this.ExpectedType != null)
            {
                output.Write("[", TextTokenKind.Operator);
                output.Write("exp", TextTokenKind.Keyword);
                output.Write(":", TextTokenKind.Operator);
                this.ExpectedType.WriteTo(output, ILNameSyntax.ShortTypeName);
                output.Write("]", TextTokenKind.Operator);
            }
            output.Write("(", TextTokenKind.Operator);
            bool first = true;

            if (Operand != null)
            {
                if (Operand is ILLabel)
                {
                    output.WriteReference(((ILLabel)Operand).Name, Operand, TextTokenKind.Label);
                }
                else if (Operand is ILLabel[])
                {
                    ILLabel[] labels = (ILLabel[])Operand;
                    for (int i = 0; i < labels.Length; i++)
                    {
                        if (i > 0)
                        {
                            output.Write(",", TextTokenKind.Operator);
                            output.WriteSpace();
                        }
                        output.WriteReference(labels[i].Name, labels[i], TextTokenKind.Label);
                    }
                }
                else if (Operand is IMethod && (Operand as IMethod).MethodSig != null)
                {
                    IMethod method = (IMethod)Operand;
                    if (method.DeclaringType != null)
                    {
                        method.DeclaringType.WriteTo(output, ILNameSyntax.ShortTypeName);
                        output.Write("::", TextTokenKind.Operator);
                    }
                    output.WriteReference(method.Name, method, TextTokenKindUtils.GetTextTokenType(method));
                }
                else if (Operand is IField)
                {
                    IField field = (IField)Operand;
                    field.DeclaringType.WriteTo(output, ILNameSyntax.ShortTypeName);
                    output.Write("::", TextTokenKind.Operator);
                    output.WriteReference(field.Name, field, TextTokenKindUtils.GetTextTokenType(field));
                }
                else if (Operand is ILVariable)
                {
                    var ilvar = (ILVariable)Operand;
                    output.WriteReference(ilvar.Name, Operand, ilvar.IsParameter ? TextTokenKind.Parameter : TextTokenKind.Local);
                }
                else
                {
                    DisassemblerHelpers.WriteOperand(output, Operand);
                }
                first = false;
            }
            foreach (ILExpression arg in this.Arguments)
            {
                if (!first)
                {
                    output.Write(",", TextTokenKind.Operator);
                    output.WriteSpace();
                }
                arg.WriteTo(output, null);
                first = false;
            }
            output.Write(")", TextTokenKind.Operator);
            UpdateMemberMapping(memberMapping, startLoc, output.Location, this.GetSelfAndChildrenRecursiveILRanges());
        }
示例#5
0
 public void WriteTo(TextWriter writer)
 {
     foreach (Instruction prefix in Prefixes)
     {
         DisassemblerHelpers.WriteTo(prefix, new PlainTextOutput(writer));
         writer.WriteLine();
     }
     if (Instruction != null && Instruction.Offset >= 0)
     {
         writer.Write(CecilExtensions.OffsetToString(Instruction.Offset));
         writer.Write(": ");
     }
     if (Target != null)
     {
         writer.Write(Target.ToString());
         writer.Write(" = ");
     }
     if (IsMoveInstruction)
     {
         writer.Write(Operands[0].ToString());
         if (Instruction != null)
         {
             writer.Write(" (" + Instruction.OpCode.Name + ")");
         }
     }
     else
     {
         if (Instruction == null)
         {
             writer.Write(SpecialOpCode.ToString());
         }
         else
         {
             writer.Write(Instruction.OpCode.Name);
             if (null != Instruction.Operand)
             {
                 writer.Write(' ');
                 DisassemblerHelpers.WriteOperand(new PlainTextOutput(writer), Instruction.Operand);
                 writer.Write(' ');
             }
         }
         if (TypeOperand != null)
         {
             writer.Write(' ');
             writer.Write(TypeOperand.ToString());
             writer.Write(' ');
         }
         if (Operands.Length > 0)
         {
             writer.Write('(');
             for (int i = 0; i < Operands.Length; i++)
             {
                 if (i > 0)
                 {
                     writer.Write(", ");
                 }
                 writer.Write(Operands[i].ToString());
             }
             writer.Write(')');
         }
     }
 }