ILAstLanguage.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // Copyright (c) 2011 AlphaSierraPapa for the SharpDevelop Team
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy of this
  4. // software and associated documentation files (the "Software"), to deal in the Software
  5. // without restriction, including without limitation the rights to use, copy, modify, merge,
  6. // publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
  7. // to whom the Software is furnished to do so, subject to the following conditions:
  8. //
  9. // The above copyright notice and this permission notice shall be included in all copies or
  10. // substantial portions of the Software.
  11. //
  12. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  13. // INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  14. // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
  15. // FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  16. // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  17. // DEALINGS IN THE SOFTWARE.
  18. using System;
  19. using System.Collections.Generic;
  20. using System.Linq;
  21. using ICSharpCode.Decompiler;
  22. using ICSharpCode.Decompiler.Disassembler;
  23. using ICSharpCode.Decompiler.ILAst;
  24. using Mono.Cecil;
  25. namespace ICSharpCode.ILSpy
  26. {
  27. #if DEBUG
  28. /// <summary>
  29. /// Represents the ILAst "language" used for debugging purposes.
  30. /// </summary>
  31. sealed class ILAstLanguage : Language
  32. {
  33. string name;
  34. bool inlineVariables = true;
  35. ILAstOptimizationStep? abortBeforeStep;
  36. public override string Name {
  37. get {
  38. return name;
  39. }
  40. }
  41. public override void DecompileMethod(MethodDefinition method, ITextOutput output, DecompilationOptions options)
  42. {
  43. if (!method.HasBody) {
  44. return;
  45. }
  46. ILAstBuilder astBuilder = new ILAstBuilder();
  47. ILBlock ilMethod = new ILBlock();
  48. DecompilerContext context = new DecompilerContext(method.Module) { CurrentType = method.DeclaringType, CurrentMethod = method };
  49. ilMethod.Body = astBuilder.Build(method, inlineVariables, context);
  50. if (abortBeforeStep != null) {
  51. new ILAstOptimizer().Optimize(context, ilMethod, abortBeforeStep.Value);
  52. }
  53. if (context.CurrentMethodIsAsync)
  54. output.WriteLine("async/await");
  55. var allVariables = ilMethod.GetSelfAndChildrenRecursive<ILExpression>().Select(e => e.Operand as ILVariable)
  56. .Where(v => v != null && !v.IsParameter).Distinct();
  57. foreach (ILVariable v in allVariables) {
  58. output.WriteDefinition(v.Name, v);
  59. if (v.Type != null) {
  60. output.Write(" : ");
  61. if (v.IsPinned)
  62. output.Write("pinned ");
  63. v.Type.WriteTo(output, ILNameSyntax.ShortTypeName);
  64. }
  65. if (v.IsGenerated) {
  66. output.Write(" [generated]");
  67. }
  68. output.WriteLine();
  69. }
  70. output.WriteLine();
  71. foreach (ILNode node in ilMethod.Body) {
  72. node.WriteTo(output);
  73. output.WriteLine();
  74. }
  75. }
  76. internal static IEnumerable<ILAstLanguage> GetDebugLanguages()
  77. {
  78. yield return new ILAstLanguage { name = "ILAst (unoptimized)", inlineVariables = false };
  79. string nextName = "ILAst (variable splitting)";
  80. foreach (ILAstOptimizationStep step in Enum.GetValues(typeof(ILAstOptimizationStep))) {
  81. yield return new ILAstLanguage { name = nextName, abortBeforeStep = step };
  82. nextName = "ILAst (after " + step + ")";
  83. }
  84. }
  85. public override string FileExtension {
  86. get {
  87. return ".il";
  88. }
  89. }
  90. public override string TypeToString(TypeReference t, bool includeNamespace, ICustomAttributeProvider attributeProvider = null)
  91. {
  92. PlainTextOutput output = new PlainTextOutput();
  93. t.WriteTo(output, includeNamespace ? ILNameSyntax.TypeName : ILNameSyntax.ShortTypeName);
  94. return output.ToString();
  95. }
  96. }
  97. #endif
  98. }