Exemplo n.º 1
0
        public static void WriteTo(this Instruction instruction, MethodDef method, CilBody body, ITextOutput writer)
        {
            writer.WriteDefinition(dnlibExtensions.OffsetToString(instruction.Offset), instruction, true);
            writer.Write(": ");
            writer.WriteReference(instruction.OpCode.Name, instruction.OpCode, true);
            if (instruction.Operand != null)
            {
                writer.Write(' ');
                if (instruction.OpCode == OpCodes.Ldtoken)
                {
                    if (dnlibExtensions.IsMethod(instruction.Operand))
                    {
                        writer.WriteKeyword("method ");
                    }
                    else if (dnlibExtensions.IsField(instruction.Operand))
                    {
                        writer.WriteKeyword("field ");
                    }
                }
                WriteOperand(writer, instruction.Operand);
            }
            else if (method != null && body != null)
            {
                switch (instruction.OpCode.Code)
                {
                case Code.Ldloc_0:
                case Code.Ldloc_1:
                case Code.Ldloc_2:
                case Code.Ldloc_3:
                    writer.WriteComment("  // ");
                    var local = instruction.GetLocal(body.Variables);
                    if (local != null)
                    {
                        WriteOperand(writer, local);
                    }
                    break;

                case Code.Ldarg_0:
                case Code.Ldarg_1:
                case Code.Ldarg_2:
                case Code.Ldarg_3:
                    writer.WriteComment("  // ");
                    var arg = instruction.GetParameter(method.Parameters);
                    if (arg != null)
                    {
                        WriteOperand(writer, arg);
                    }
                    break;
                }
            }
        }
Exemplo n.º 2
0
        public void WriteComment(CommentType commentType, string content)
        {
            switch (commentType)
            {
            case CommentType.SingleLine:
                output.WriteComment("//");
                output.WriteComment(content);
                output.WriteLine();
                break;

            case CommentType.MultiLine:
                output.WriteComment("/*");
                output.WriteComment(content);
                output.WriteComment("*/");
                break;

            case CommentType.Documentation:
                bool isLastLine = !(nodeStack.Peek().NextSibling is Comment);
                if (!inDocumentationComment && !isLastLine)
                {
                    inDocumentationComment = true;
                    output.MarkFoldStart("///" + content, true);
                }
                output.WriteComment("///");
                output.WriteComment(content);
                if (inDocumentationComment && isLastLine)
                {
                    inDocumentationComment = false;
                    output.MarkFoldEnd();
                }
                output.WriteLine();
                break;

            default:
                output.Write(content);
                break;
            }
        }
		public static void WriteTo(this Instruction instruction, MethodDef method, CilBody body, ITextOutput writer)
		{
			writer.WriteDefinition(dnlibExtensions.OffsetToString(instruction.Offset), instruction, true);
			writer.Write(": ");
			writer.WriteReference(instruction.OpCode.Name, instruction.OpCode, true);
			if (instruction.Operand != null) {
				writer.Write(' ');
				if (instruction.OpCode == OpCodes.Ldtoken) {
					if (dnlibExtensions.IsMethod(instruction.Operand))
						writer.WriteKeyword("method ");
					else if (dnlibExtensions.IsField(instruction.Operand))
						writer.WriteKeyword("field ");
				}
				WriteOperand(writer, instruction.Operand);
			}
			else if (method != null && body != null) {
				switch (instruction.OpCode.Code) {
					case Code.Ldloc_0:
					case Code.Ldloc_1:
					case Code.Ldloc_2:
					case Code.Ldloc_3:
						writer.WriteComment("  // ");
						var local = instruction.GetLocal(body.Variables);
						if (local != null)
							WriteOperand(writer, local);
						break;

					case Code.Ldarg_0:
					case Code.Ldarg_1:
					case Code.Ldarg_2:
					case Code.Ldarg_3:
						writer.WriteComment("  // ");
						var arg = instruction.GetParameter(method.Parameters);
						if (arg != null)
							WriteOperand(writer, arg);
						break;
				}
			}
		}
		public static void WriteOperand(ITextOutput writer, object operand)
		{
			if (operand == null)
				throw new ArgumentNullException("operand");
			
			Instruction targetInstruction = operand as Instruction;
			if (targetInstruction != null) {
				WriteOffsetReference(writer, targetInstruction);
				return;
			}
			
			Instruction[] targetInstructions = operand as Instruction[];
			if (targetInstructions != null) {
				WriteLabelList(writer, targetInstructions);
				return;
			}
			
			Local local = operand as Local;
			if (local != null) {
				if (string.IsNullOrEmpty(local.Name))
					writer.WriteReference("[" + local.Index.ToString() + "]", local, true);
				else
					writer.WriteReference(Escape(local.Name), local, true);
				return;
			}
			
			Parameter param = operand as Parameter;
			if (param != null) {
				if (string.IsNullOrEmpty(param.Name))
					writer.WriteReference("[" + param.Index.ToString() + "]", param, true);
				else
					writer.WriteReference(Escape(param.Name), param, true);
				return;
			}
			
			IMethod methodRef = operand as IMethod;
			if (methodRef != null && dnlibExtensions.IsMethod(methodRef)) {
				methodRef.WriteTo(writer);
				writer.WriteComment(" // 0x" + methodRef.MDToken.Raw.ToString("x8"));
				return;
			}
			
			ITypeDefOrRef typeRef = operand as ITypeDefOrRef;
			if (typeRef != null) {
				typeRef.WriteTo(writer, ILNameSyntax.TypeName);
				writer.WriteComment(" // 0x" + typeRef.MDToken.Raw.ToString("x8"));
				return;
			}

			IField fieldRef = operand as IField;
			if (fieldRef != null && dnlibExtensions.IsField(fieldRef)) {
				fieldRef.WriteTo(writer);
				writer.WriteComment(" // 0x" + fieldRef.MDToken.Raw.ToString("x8"));
				return;
			}
			
			string s = operand as string;
			if (s != null) {
				writer.WriteLiteral("\"" + NRefactory.CSharp.CSharpOutputVisitor.ConvertString(s) + "\"");
			} else if (operand is char) {
				writer.WriteLiteral(((int)(char)operand).ToString());
			} else if (operand is float) {
				float val = (float)operand;
				if (val == 0) {
					if (1 / val == float.NegativeInfinity) {
						// negative zero is a special case
						writer.WriteLiteral("-");
					}
					writer.WriteLiteral("0.0");
				} else if (float.IsInfinity(val) || float.IsNaN(val)) {
					byte[] data = BitConverter.GetBytes(val);
					writer.Write('(');
					for (int i = 0; i < data.Length; i++) {
						if (i > 0)
							writer.WriteLiteral(" ");
						writer.WriteLiteral(data[i].ToString("X2"));
					}
					writer.Write(')');
				} else {
					writer.WriteLiteral(val.ToString("R", System.Globalization.CultureInfo.InvariantCulture));
				}
			} else if (operand is double) {
				double val = (double)operand;
				if (val == 0) {
					if (1 / val == double.NegativeInfinity) {
						// negative zero is a special case
						writer.WriteLiteral("-");
					}
					writer.WriteLiteral("0.0");
				} else if (double.IsInfinity(val) || double.IsNaN(val)) {
					byte[] data = BitConverter.GetBytes(val);
					writer.Write('(');
					for (int i = 0; i < data.Length; i++) {
						if (i > 0)
							writer.WriteLiteral(" ");
						writer.WriteLiteral(data[i].ToString("X2"));
					}
					writer.Write(')');
				} else {
					writer.WriteLiteral(val.ToString("R", System.Globalization.CultureInfo.InvariantCulture));
				}
			} else if (operand is bool) {
				writer.WriteLiteral((bool)operand ? "true" : "false");
			} else {
				s = ToInvariantCultureString(operand);
				writer.WriteLiteral(s);
			}
		}
Exemplo n.º 5
0
 public static void WriteLineComment(this ITextOutput output, string format, params object[] args)
 {
     output.WriteComment(string.Format(format, args));
     output.WriteLine();
 }
Exemplo n.º 6
0
        public static void WriteOperand(ITextOutput writer, object operand)
        {
            if (operand == null)
            {
                throw new ArgumentNullException("operand");
            }

            Instruction targetInstruction = operand as Instruction;

            if (targetInstruction != null)
            {
                WriteOffsetReference(writer, targetInstruction);
                return;
            }

            Instruction[] targetInstructions = operand as Instruction[];
            if (targetInstructions != null)
            {
                WriteLabelList(writer, targetInstructions);
                return;
            }

            Local local = operand as Local;

            if (local != null)
            {
                if (string.IsNullOrEmpty(local.Name))
                {
                    writer.WriteReference("[" + local.Index.ToString() + "]", local, true);
                }
                else
                {
                    writer.WriteReference(Escape(local.Name), local, true);
                }
                return;
            }

            Parameter param = operand as Parameter;

            if (param != null)
            {
                if (string.IsNullOrEmpty(param.Name))
                {
                    writer.WriteReference("[" + param.Index.ToString() + "]", param, true);
                }
                else
                {
                    writer.WriteReference(Escape(param.Name), param, true);
                }
                return;
            }

            IMethod methodRef = operand as IMethod;

            if (methodRef != null && dnlibExtensions.IsMethod(methodRef))
            {
                methodRef.WriteTo(writer);
                writer.WriteComment(" // 0x" + methodRef.MDToken.Raw.ToString("x8"));
                return;
            }

            ITypeDefOrRef typeRef = operand as ITypeDefOrRef;

            if (typeRef != null)
            {
                typeRef.WriteTo(writer, ILNameSyntax.TypeName);
                writer.WriteComment(" // 0x" + typeRef.MDToken.Raw.ToString("x8"));
                return;
            }

            IField fieldRef = operand as IField;

            if (fieldRef != null && dnlibExtensions.IsField(fieldRef))
            {
                fieldRef.WriteTo(writer);
                writer.WriteComment(" // 0x" + fieldRef.MDToken.Raw.ToString("x8"));
                return;
            }

            string s = operand as string;

            if (s != null)
            {
                writer.WriteLiteral("\"" + NRefactory.CSharp.CSharpOutputVisitor.ConvertString(s) + "\"");
            }
            else if (operand is char)
            {
                writer.WriteLiteral(((int)(char)operand).ToString());
            }
            else if (operand is float)
            {
                float val = (float)operand;
                if (val == 0)
                {
                    if (1 / val == float.NegativeInfinity)
                    {
                        // negative zero is a special case
                        writer.WriteLiteral("-");
                    }
                    writer.WriteLiteral("0.0");
                }
                else if (float.IsInfinity(val) || float.IsNaN(val))
                {
                    byte[] data = BitConverter.GetBytes(val);
                    writer.Write('(');
                    for (int i = 0; i < data.Length; i++)
                    {
                        if (i > 0)
                        {
                            writer.WriteLiteral(" ");
                        }
                        writer.WriteLiteral(data[i].ToString("X2"));
                    }
                    writer.Write(')');
                }
                else
                {
                    writer.WriteLiteral(val.ToString("R", System.Globalization.CultureInfo.InvariantCulture));
                }
            }
            else if (operand is double)
            {
                double val = (double)operand;
                if (val == 0)
                {
                    if (1 / val == double.NegativeInfinity)
                    {
                        // negative zero is a special case
                        writer.WriteLiteral("-");
                    }
                    writer.WriteLiteral("0.0");
                }
                else if (double.IsInfinity(val) || double.IsNaN(val))
                {
                    byte[] data = BitConverter.GetBytes(val);
                    writer.Write('(');
                    for (int i = 0; i < data.Length; i++)
                    {
                        if (i > 0)
                        {
                            writer.WriteLiteral(" ");
                        }
                        writer.WriteLiteral(data[i].ToString("X2"));
                    }
                    writer.Write(')');
                }
                else
                {
                    writer.WriteLiteral(val.ToString("R", System.Globalization.CultureInfo.InvariantCulture));
                }
            }
            else if (operand is bool)
            {
                writer.WriteLiteral((bool)operand ? "true" : "false");
            }
            else
            {
                s = ToInvariantCultureString(operand);
                writer.WriteLiteral(s);
            }
        }
        void WriteStructureHeader(ILStructure s)
        {
            switch (s.Type)
            {
            case ILStructureType.Loop:
                output.WriteComment("// loop start");
                if (s.LoopEntryPoint != null)
                {
                    output.WriteComment(" (head: ");
                    DisassemblerHelpers.WriteOffsetReference(output, s.LoopEntryPoint);
                    output.WriteComment(")");
                }
                output.WriteLine();
                break;

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

            case ILStructureType.Handler:
                switch (s.ExceptionHandler.HandlerType)
                {
                case ExceptionHandlerType.Catch:
                case ExceptionHandlerType.Filter:
                    output.WriteKeyword("catch");
                    if (s.ExceptionHandler.CatchType != null)
                    {
                        output.Write(' ');
                        s.ExceptionHandler.CatchType.WriteTo(output, ILNameSyntax.TypeName);
                    }
                    output.WriteLine();
                    break;

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

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

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

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

            default:
                throw new NotSupportedException();
            }
            output.Indent();
        }