| 1 | using System; |
| 2 | using System.IO; |
| 3 | using Mono.Cecil; |
| 4 | using Mono.Cecil.Cil; |
| 5 | |
| 6 | namespace Mono.Cecil.Tests { |
| 7 | |
| 8 | public static class Formatter { |
| 9 | |
| 10 | public static string FormatInstruction (Instruction instruction) |
| 11 | { |
| 12 | var writer = new StringWriter (); |
| 13 | WriteInstruction (writer, instruction); |
| 14 | return writer.ToString (); |
| 15 | } |
| 16 | |
| 17 | public static string FormatMethodBody (MethodDefinition method) |
| 18 | { |
| 19 | var writer = new StringWriter (); |
| 20 | WriteMethodBody (writer, method); |
| 21 | return writer.ToString (); |
| 22 | } |
| 23 | |
| 24 | public static void WriteMethodBody (TextWriter writer, MethodDefinition method) |
| 25 | { |
| 26 | var body = method.Body; |
| 27 | |
| 28 | WriteVariables (writer, body); |
| 29 | |
| 30 | foreach (Instruction instruction in body.Instructions) { |
| 31 | var sequence_point = instruction.SequencePoint; |
| 32 | if (sequence_point != null) { |
| 33 | writer.Write ('\t'); |
| 34 | WriteSequencePoint (writer, sequence_point); |
| 35 | writer.WriteLine (); |
| 36 | } |
| 37 | |
| 38 | writer.Write ('\t'); |
| 39 | WriteInstruction (writer, instruction); |
| 40 | writer.WriteLine (); |
| 41 | } |
| 42 | |
| 43 | WriteExceptionHandlers (writer, body); |
| 44 | } |
| 45 | |
| 46 | static void WriteVariables (TextWriter writer, MethodBody body) |
| 47 | { |
| 48 | var variables = body.Variables; |
| 49 | |
| 50 | writer.Write ('\t'); |
| 51 | writer.Write (".locals {0}(", body.InitLocals ? "init " : string.Empty); |
| 52 | |
| 53 | for (int i = 0; i < variables.Count; i++) { |
| 54 | if (i > 0) |
| 55 | writer.Write (", "); |
| 56 | |
| 57 | var variable = variables [i]; |
| 58 | |
| 59 | writer.Write ("{0} {1}", variable.VariableType, variable); |
| 60 | } |
| 61 | writer.WriteLine (")"); |
| 62 | } |
| 63 | |
| 64 | static void WriteInstruction (TextWriter writer, Instruction instruction) |
| 65 | { |
| 66 | writer.Write (FormatLabel (instruction.Offset)); |
| 67 | writer.Write (": "); |
| 68 | writer.Write (instruction.OpCode.Name); |
| 69 | if (null != instruction.Operand) { |
| 70 | writer.Write (' '); |
| 71 | WriteOperand (writer, instruction.Operand); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | static void WriteSequencePoint (TextWriter writer, SequencePoint sequence_point) |
| 76 | { |
| 77 | writer.Write (".line {0},{1}:{2},{3} '{4}'", |
| 78 | sequence_point.StartLine, |
| 79 | sequence_point.EndLine, |
| 80 | sequence_point.StartColumn, |
| 81 | sequence_point.EndColumn, |
| 82 | sequence_point.Document.Url); |
| 83 | } |
| 84 | |
| 85 | static string FormatLabel (int offset) |
| 86 | { |
| 87 | string label = "000" + offset.ToString ("x"); |
| 88 | return "IL_" + label.Substring (label.Length - 4); |
| 89 | } |
| 90 | |
| 91 | static string FormatLabel (Instruction instruction) |
| 92 | { |
| 93 | return FormatLabel (instruction.Offset); |
| 94 | } |
| 95 | |
| 96 | static void WriteOperand (TextWriter writer, object operand) |
| 97 | { |
| 98 | if (null == operand) throw new ArgumentNullException ("operand"); |
| 99 | |
| 100 | var target = operand as Instruction; |
| 101 | if (null != target) { |
| 102 | writer.Write (FormatLabel (target.Offset)); |
| 103 | return; |
| 104 | } |
| 105 | |
| 106 | var targets = operand as Instruction []; |
| 107 | if (null != targets) { |
| 108 | WriteLabelList (writer, targets); |
| 109 | return; |
| 110 | } |
| 111 | |
| 112 | string s = operand as string; |
| 113 | if (null != s) { |
| 114 | writer.Write ("\"" + s + "\""); |
| 115 | return; |
| 116 | } |
| 117 | |
| 118 | var parameter = operand as ParameterDefinition; |
| 119 | if (parameter != null) { |
| 120 | writer.Write (ToInvariantCultureString (parameter.Sequence)); |
| 121 | return; |
| 122 | } |
| 123 | |
| 124 | s = ToInvariantCultureString (operand); |
| 125 | writer.Write (s); |
| 126 | } |
| 127 | |
| 128 | static void WriteLabelList (TextWriter writer, Instruction [] instructions) |
| 129 | { |
| 130 | writer.Write ("("); |
| 131 | |
| 132 | for (int i = 0; i < instructions.Length; i++) { |
| 133 | if (i != 0) writer.Write (", "); |
| 134 | writer.Write (FormatLabel (instructions [i].Offset)); |
| 135 | } |
| 136 | |
| 137 | writer.Write (")"); |
| 138 | } |
| 139 | |
| 140 | static void WriteExceptionHandlers (TextWriter writer, MethodBody body) |
| 141 | { |
| 142 | if (!body.HasExceptionHandlers) |
| 143 | return; |
| 144 | |
| 145 | foreach (var handler in body.ExceptionHandlers) { |
| 146 | writer.Write ("\t"); |
| 147 | writer.WriteLine (".try {0} to {1} {2} handler {3} to {4}", |
| 148 | FormatLabel (handler.TryStart), |
| 149 | FormatLabel (handler.TryEnd), |
| 150 | FormatHandlerType (handler), |
| 151 | FormatLabel (handler.HandlerStart), |
| 152 | FormatLabel (handler.HandlerEnd)); |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | static string FormatHandlerType (ExceptionHandler handler) |
| 157 | { |
| 158 | var handler_type = handler.HandlerType; |
| 159 | var type = handler_type.ToString ().ToLowerInvariant (); |
| 160 | |
| 161 | switch (handler_type) { |
| 162 | case ExceptionHandlerType.Catch: |
| 163 | return string.Format ("{0} {1}", type, handler.CatchType.FullName); |
| 164 | case ExceptionHandlerType.Filter: |
| 165 | throw new NotImplementedException (); |
| 166 | default: |
| 167 | return type; |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | public static string ToInvariantCultureString (object value) |
| 172 | { |
| 173 | var convertible = value as IConvertible; |
| 174 | return (null != convertible) |
| 175 | ? convertible.ToString (System.Globalization.CultureInfo.InvariantCulture) |
| 176 | : value.ToString (); |
| 177 | } |
| 178 | } |
| 179 | } |