Debian Codegraph @

external/cecil-legacy/rocks/Mono.Cecil.Rocks/ILParser.cs

raw symbols SCIP
1 //
2 // ILParser.cs
3 //
4 // Author:
5 // Jb Evain ([email protected])
6 //
7 // Copyright (c) 2008 - 2011 Jb Evain
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 //
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 //
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28
29 using System;
30
31 using Mono.Cecil.Cil;
32 using Mono.Collections.Generic;
33
34 namespace Mono.Cecil.Rocks {
35
36 #if INSIDE_ROCKS
37 public
38 #endif
39 interface IILVisitor {
40 void OnInlineNone (OpCode opcode);
41 void OnInlineSByte (OpCode opcode, sbyte value);
42 void OnInlineByte (OpCode opcode, byte value);
43 void OnInlineInt32 (OpCode opcode, int value);
44 void OnInlineInt64 (OpCode opcode, long value);
45 void OnInlineSingle (OpCode opcode, float value);
46 void OnInlineDouble (OpCode opcode, double value);
47 void OnInlineString (OpCode opcode, string value);
48 void OnInlineBranch (OpCode opcode, int offset);
49 void OnInlineSwitch (OpCode opcode, int [] offsets);
50 void OnInlineVariable (OpCode opcode, VariableDefinition variable);
51 void OnInlineArgument (OpCode opcode, ParameterDefinition parameter);
52 void OnInlineSignature (OpCode opcode, CallSite callSite);
53 void OnInlineType (OpCode opcode, TypeReference type);
54 void OnInlineField (OpCode opcode, FieldReference field);
55 void OnInlineMethod (OpCode opcode, MethodReference method);
56 }
57
58 #if INSIDE_ROCKS
59 public
60 #endif
61 static class ILParser {
62
63 class ParseContext {
64 public CodeReader Code { get; set; }
65 public MetadataReader Metadata { get; set; }
66 public Collection<VariableDefinition> Variables { get; set; }
67 public IILVisitor Visitor { get; set; }
68 }
69
70 public static void Parse (MethodDefinition method, IILVisitor visitor)
71 {
72 if (method == null)
73 throw new ArgumentNullException ("method");
74 if (visitor == null)
75 throw new ArgumentNullException ("visitor");
76 if (!method.HasBody || !method.HasImage)
77 throw new ArgumentException ();
78
79 var context = CreateContext (method, visitor);
80 var code = context.Code;
81
82 code.MoveTo (method.RVA);
83
84 var flags = code.ReadByte ();
85
86 switch (flags & 0x3) {
87 case 0x2: // tiny
88 int code_size = flags >> 2;
89 ParseCode (code_size, context);
90 break;
91 case 0x3: // fat
92 code.position--;
93 ParseFatMethod (context);
94 break;
95 default:
96 throw new NotSupportedException ();
97 }
98 }
99
100 static ParseContext CreateContext (MethodDefinition method, IILVisitor visitor)
101 {
102 var code = method.Module.Read (method, (_, reader) => new CodeReader (reader.image.MetadataSection, reader));
103
104 return new ParseContext {
105 Code = code,
106 Metadata = code.reader,
107 Visitor = visitor,
108 };
109 }
110
111 static void ParseFatMethod (ParseContext context)
112 {
113 var code = context.Code;
114
115 code.Advance (4);
116 var code_size = code.ReadInt32 ();
117 var local_var_token = code.ReadToken ();
118
119 if (local_var_token != MetadataToken.Zero)
120 context.Variables = code.ReadVariables (local_var_token);
121
122 ParseCode (code_size, context);
123 }
124
125 static void ParseCode (int code_size, ParseContext context)
126 {
127 var code = context.Code;
128 var metadata = context.Metadata;
129 var visitor = context.Visitor;
130
131 var start = code.position;
132 var end = start + code_size;
133
134 while (code.position < end) {
135 var il_opcode = code.ReadByte ();
136 var opcode = il_opcode != 0xfe
137 ? OpCodes.OneByteOpCode [il_opcode]
138 : OpCodes.TwoBytesOpCode [code.ReadByte ()];
139
140 switch (opcode.OperandType) {
141 case OperandType.InlineNone:
142 visitor.OnInlineNone (opcode);
143 break;
144 case OperandType.InlineSwitch:
145 var length = code.ReadInt32 ();
146 var branches = new int [length];
147 for (int i = 0; i < length; i++)
148 branches [i] = code.ReadInt32 ();
149 visitor.OnInlineSwitch (opcode, branches);
150 break;
151 case OperandType.ShortInlineBrTarget:
152 visitor.OnInlineBranch (opcode, code.ReadSByte ());
153 break;
154 case OperandType.InlineBrTarget:
155 visitor.OnInlineBranch (opcode, code.ReadInt32 ());
156 break;
157 case OperandType.ShortInlineI:
158 if (opcode == OpCodes.Ldc_I4_S)
159 visitor.OnInlineSByte (opcode, code.ReadSByte ());
160 else
161 visitor.OnInlineByte (opcode, code.ReadByte ());
162 break;
163 case OperandType.InlineI:
164 visitor.OnInlineInt32 (opcode, code.ReadInt32 ());
165 break;
166 case OperandType.InlineI8:
167 visitor.OnInlineInt64 (opcode, code.ReadInt64 ());
168 break;
169 case OperandType.ShortInlineR:
170 visitor.OnInlineSingle (opcode, code.ReadSingle ());
171 break;
172 case OperandType.InlineR:
173 visitor.OnInlineDouble (opcode, code.ReadDouble ());
174 break;
175 case OperandType.InlineSig:
176 visitor.OnInlineSignature (opcode, code.GetCallSite (code.ReadToken ()));
177 break;
178 case OperandType.InlineString:
179 visitor.OnInlineString (opcode, code.GetString (code.ReadToken ()));
180 break;
181 case OperandType.ShortInlineArg:
182 visitor.OnInlineArgument (opcode, code.GetParameter (code.ReadByte ()));
183 break;
184 case OperandType.InlineArg:
185 visitor.OnInlineArgument (opcode, code.GetParameter (code.ReadInt16 ()));
186 break;
187 case OperandType.ShortInlineVar:
188 visitor.OnInlineVariable (opcode, GetVariable (context, code.ReadByte ()));
189 break;
190 case OperandType.InlineVar:
191 visitor.OnInlineVariable (opcode, GetVariable (context, code.ReadInt16 ()));
192 break;
193 case OperandType.InlineTok:
194 case OperandType.InlineField:
195 case OperandType.InlineMethod:
196 case OperandType.InlineType:
197 var member = metadata.LookupToken (code.ReadToken ());
198 switch (member.MetadataToken.TokenType) {
199 case TokenType.TypeDef:
200 case TokenType.TypeRef:
201 case TokenType.TypeSpec:
202 visitor.OnInlineType (opcode, (TypeReference) member);
203 break;
204 case TokenType.Method:
205 case TokenType.MethodSpec:
206 visitor.OnInlineMethod (opcode, (MethodReference) member);
207 break;
208 case TokenType.Field:
209 visitor.OnInlineField (opcode, (FieldReference) member);
210 break;
211 case TokenType.MemberRef:
212 var field_ref = member as FieldReference;
213 if (field_ref != null) {
214 visitor.OnInlineField (opcode, field_ref);
215 break;
216 }
217
218 var method_ref = member as MethodReference;
219 if (method_ref != null) {
220 visitor.OnInlineMethod (opcode, method_ref);
221 break;
222 }
223
224 throw new InvalidOperationException ();
225 }
226 break;
227 }
228 }
229 }
230
231 static VariableDefinition GetVariable (ParseContext context, int index)
232 {
233 return context.Variables [index];
234 }
235 }
236 }