Welcome to mirror list, hosted at ThFree Co, Russian Federation.

MethodILDebugView.cs « IL « TypeSystem « Common « tools « coreclr « src - github.com/dotnet/runtime.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 28b9818c3af1ef5b9ced1f641788b8dc661a76a2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Text;

using Internal.TypeSystem;

namespace Internal.IL
{
    internal sealed class MethodILDebugView
    {
        private readonly MethodIL _methodIL;

        public MethodILDebugView(MethodIL methodIL)
        {
            _methodIL = methodIL;
        }

        public string Disassembly
        {
            get
            {
                ILDisassembler disasm = new ILDisassembler(_methodIL);

                StringBuilder sb = new StringBuilder();

                MethodDesc owningMethod = _methodIL.OwningMethod;

                sb.Append("// ");
                sb.AppendLine(owningMethod.ToString());
                sb.Append(".method ");
                // TODO: accessibility, specialname, calling conventions etc.
                if (!owningMethod.Signature.IsStatic)
                    sb.Append("instance ");
                disasm.AppendType(sb, owningMethod.Signature.ReturnType);
                sb.Append(' ');
                sb.Append(owningMethod.Name);
                if (owningMethod.HasInstantiation)
                {
                    sb.Append('<');
                    for (int i = 0; i < owningMethod.Instantiation.Length; i++)
                    {
                        if (i != 0)
                            sb.Append(", ");
                        disasm.AppendType(sb, owningMethod.Instantiation[i]);
                    }
                    sb.Append('>');
                }
                sb.Append('(');
                for (int i = 0; i < owningMethod.Signature.Length; i++)
                {
                    if (i != 0)
                        sb.Append(", ");
                    disasm.AppendType(sb, owningMethod.Signature[i]);
                }
                sb.AppendLine(") cil managed");

                sb.AppendLine("{");

                sb.Append("  // Code size: ");
                sb.Append(disasm.CodeSize);
                sb.AppendLine();
                sb.Append("  .maxstack ");
                sb.Append(_methodIL.MaxStack);
                sb.AppendLine();

                LocalVariableDefinition[] locals = _methodIL.GetLocals();
                if (locals != null && locals.Length > 0)
                {
                    sb.Append("  .locals ");
                    if (_methodIL.IsInitLocals)
                        sb.Append("init ");

                    sb.Append('(');

                    for (int i = 0; i < locals.Length; i++)
                    {
                        if (i != 0)
                        {
                            sb.AppendLine(",");
                            sb.Append(' ', 6);
                        }
                        disasm.AppendType(sb, locals[i].Type);
                        sb.Append(' ');
                        if (locals[i].IsPinned)
                            sb.Append("pinned ");
                        sb.Append("V_");
                        sb.Append(i);
                    }
                    sb.AppendLine(")");
                }
                sb.AppendLine();

                const string pad = "  ";

                // TODO: pretty exception regions
                foreach (ILExceptionRegion region in _methodIL.GetExceptionRegions())
                {
                    sb.Append(pad);
                    sb.Append(".try ");
                    ILDisassembler.AppendOffset(sb, region.TryOffset);
                    sb.Append(" to ");
                    ILDisassembler.AppendOffset(sb, region.TryOffset + region.TryLength);

                    switch (region.Kind)
                    {
                        case ILExceptionRegionKind.Catch:
                            sb.Append(" catch ");
                            disasm.AppendType(sb, (TypeDesc)_methodIL.GetObject(region.ClassToken));
                            break;
                        case ILExceptionRegionKind.Fault:
                            sb.Append(" fault");
                            break;
                        case ILExceptionRegionKind.Filter:
                            sb.Append(" filter ");
                            ILDisassembler.AppendOffset(sb, region.FilterOffset);
                            break;
                        case ILExceptionRegionKind.Finally:
                            sb.Append(" finally");
                            break;
                    }

                    sb.Append(" handler ");
                    ILDisassembler.AppendOffset(sb, region.HandlerOffset);
                    sb.Append(" to ");
                    ILDisassembler.AppendOffset(sb, region.HandlerOffset + region.HandlerLength);
                    sb.AppendLine();
                }

                while (disasm.HasNextInstruction)
                {
                    sb.Append(pad);
                    sb.AppendLine(disasm.GetNextInstruction());
                }

                sb.AppendLine("}");

                return sb.ToString();
            }
        }
    }
}