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

ILProcessorTests.cs « Mono.Cecil.Tests « Test - github.com/mono/cecil.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 17b4fb05f218e7903b295188b882e6915d663678 (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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
using System;
using System.Linq;

using Mono.Cecil;
using Mono.Cecil.Cil;

using NUnit.Framework;

namespace Mono.Cecil.Tests {

	[TestFixture]
	public class ILProcessorTests : BaseTestFixture {

		[Test]
		public void Append ()
		{
			var method = CreateTestMethod ();
			var il = method.GetILProcessor ();

			var ret = il.Create (OpCodes.Ret);
			il.Append (ret);

			AssertOpCodeSequence (new [] { OpCodes.Ret }, method);
		}

		[Test]
		public void InsertBefore ()
		{
			var method = CreateTestMethod (OpCodes.Ldloc_0, OpCodes.Ldloc_2, OpCodes.Ldloc_3);
			var il = method.GetILProcessor ();

			var ldloc_2 = method.Instructions.Where (i => i.OpCode == OpCodes.Ldloc_2).First ();

			il.InsertBefore (
				ldloc_2,
				il.Create (OpCodes.Ldloc_1));

			AssertOpCodeSequence (new [] { OpCodes.Ldloc_0, OpCodes.Ldloc_1, OpCodes.Ldloc_2, OpCodes.Ldloc_3 }, method);
		}

		[Test]
		public void InsertAfter ()
		{
			var method = CreateTestMethod (OpCodes.Ldloc_0, OpCodes.Ldloc_2, OpCodes.Ldloc_3);
			var il = method.GetILProcessor ();

			var ldloc_0 = method.Instructions.First ();

			il.InsertAfter (
				ldloc_0,
				il.Create (OpCodes.Ldloc_1));

			AssertOpCodeSequence (new [] { OpCodes.Ldloc_0, OpCodes.Ldloc_1, OpCodes.Ldloc_2, OpCodes.Ldloc_3 }, method);
		}

		[Test]
		public void InsertAfterUsingIndex ()
		{
			var method = CreateTestMethod (OpCodes.Ldloc_0, OpCodes.Ldloc_2, OpCodes.Ldloc_3);
			var il = method.GetILProcessor ();

			il.InsertAfter (
				0,
				il.Create (OpCodes.Ldloc_1));

			AssertOpCodeSequence (new [] { OpCodes.Ldloc_0, OpCodes.Ldloc_1, OpCodes.Ldloc_2, OpCodes.Ldloc_3 }, method);
		}

		[Test]
		public void InsertAfterWithLocalScopes ()
		{
			var method = CreateTestMethodWithLocalScopes ();
			var il = method.GetILProcessor ();

			il.InsertAfter (
				0,
				il.Create (OpCodes.Nop));

			AssertOpCodeSequence (new [] { OpCodes.Ldloc_0, OpCodes.Nop, OpCodes.Ldloc_1, OpCodes.Ldloc_2 }, method);
			var wholeBodyScope = VerifyWholeBodyScope (method);
			AssertLocalScope (wholeBodyScope.Scopes [0], 0, 2);
			AssertLocalScope (wholeBodyScope.Scopes [1], 2, 3);
			AssertLocalScope (wholeBodyScope.Scopes [2], 3, null);
		}

		[Test]
		public void ReplaceUsingIndex ()
		{
			var method = CreateTestMethod (OpCodes.Ldloc_0, OpCodes.Ldloc_2, OpCodes.Ldloc_3);
			var il = method.GetILProcessor ();

			il.Replace (1, il.Create (OpCodes.Nop));

			AssertOpCodeSequence (new [] { OpCodes.Ldloc_0, OpCodes.Nop, OpCodes.Ldloc_3 }, method);
		}

		[Test]
		public void ReplaceWithLocalScopes ()
		{
			var method = CreateTestMethodWithLocalScopes ();
			var il = method.GetILProcessor ();

			// Replace with larger instruction
			var instruction = il.Create (OpCodes.Ldstr, "test");
			instruction.Offset = method.Instructions [1].Offset;
			il.Replace (1, instruction);

			AssertOpCodeSequence (new [] { OpCodes.Ldloc_0, OpCodes.Ldstr, OpCodes.Ldloc_2 }, method);
			var wholeBodyScope = VerifyWholeBodyScope (method);
			AssertLocalScope (wholeBodyScope.Scopes [0], 0, 1);
			AssertLocalScope (wholeBodyScope.Scopes [1], 1, 6); // size of the new instruction is 5 bytes
			AssertLocalScope (wholeBodyScope.Scopes [2], 6, null);
		}

		[Test]
		public void Clear ()
		{
			var method = CreateTestMethod (OpCodes.Ldloc_0, OpCodes.Ldloc_2, OpCodes.Ldloc_3);
			var il = method.GetILProcessor ();

			il.Clear ();

			AssertOpCodeSequence (new OpCode[] { }, method);
		}

		static void AssertOpCodeSequence (OpCode [] expected, MethodBody body)
		{
			var opcodes = body.Instructions.Select (i => i.OpCode).ToArray ();
			Assert.AreEqual (expected.Length, opcodes.Length);

			for (int i = 0; i < opcodes.Length; i++)
				Assert.AreEqual (expected [i], opcodes [i]);
		}

		static MethodBody CreateTestMethod (params OpCode [] opcodes)
		{
			var method = new MethodDefinition {
				Name = "function",
			};

			var il = method.Body.GetILProcessor ();

			foreach (var opcode in opcodes)
				il.Emit (opcode);

			var instructions = method.Body.Instructions;
			int size = 0;
			for (int i = 0; i < instructions.Count; i++) {
				var instruction = instructions [i];
				instruction.Offset = size;
				size += instruction.GetSize ();
			}

			return method.Body;
		}

		static ScopeDebugInformation VerifyWholeBodyScope (MethodBody body)
		{
			var debug_info = body.Method.DebugInformation;
			Assert.IsNotNull (debug_info);
			AssertLocalScope (debug_info.Scope, 0, null);
			return debug_info.Scope;
		}

		static void AssertLocalScope (ScopeDebugInformation scope, int startOffset, int? endOffset)
		{
			Assert.IsNotNull (scope);
			Assert.AreEqual (startOffset, scope.Start.Offset);
			if (endOffset.HasValue)
				Assert.AreEqual (endOffset.Value, scope.End.Offset);
			else
				Assert.IsTrue (scope.End.IsEndOfMethod);
		}

		static MethodBody CreateTestMethodWithLocalScopes ()
		{
			var methodBody = CreateTestMethod (OpCodes.Ldloc_0, OpCodes.Ldloc_1, OpCodes.Ldloc_2);
			var method = methodBody.Method;
			var debug_info = method.DebugInformation;

			var wholeBodyScope = new ScopeDebugInformation () {
				Start = new InstructionOffset (0),
				End = new InstructionOffset ()
			};
			int size = 0;
			var instruction = methodBody.Instructions [0];
			var innerScopeBegining = new ScopeDebugInformation () { 
				Start = new InstructionOffset (size), 
				End = new InstructionOffset (size + instruction.GetSize ())
			};
			size += instruction.GetSize ();
			wholeBodyScope.Scopes.Add (innerScopeBegining);

			instruction = methodBody.Instructions [1];
			var innerScopeMiddle = new ScopeDebugInformation () {
				Start = new InstructionOffset (size),
				End = new InstructionOffset (size + instruction.GetSize ())
			};
			size += instruction.GetSize ();
			wholeBodyScope.Scopes.Add (innerScopeMiddle);

			var innerScopeEnd = new ScopeDebugInformation () {
				Start = new InstructionOffset (size),
				End = new InstructionOffset ()
			};
			wholeBodyScope.Scopes.Add (innerScopeEnd);

			debug_info.Scope = wholeBodyScope;
			return methodBody;
		}
	}
}