Merge pull request #409 from Alkarex/patch-1
[mono.git] / mcs / class / Mono.Debugger.Soft / Test / dtest-app.cs
1 /*
2  * dtest-app.cs:
3  *
4  *   Application program used by the debugger tests.
5  */
6 using System;
7 using System.Runtime.CompilerServices;
8 using System.Reflection;
9 using System.Reflection.Emit;
10 using System.Diagnostics;
11 using System.Threading;
12 using System.Collections.Generic;
13 using System.Linq;
14
15 public class TestsBase
16 {
17 #pragma warning disable 0414
18 #pragma warning disable 0169
19         public int base_field_i;
20         public string base_field_s;
21         static int base_static_i = 57;
22         static string base_static_s = "C";
23 #pragma warning restore 0414
24 #pragma warning restore 0169
25 }
26
27 public enum AnEnum {
28         A = 0,
29         B= 1
30 }
31
32 public sealed class Tests3 {
33         public static void M1 () {
34         }
35
36         static void M2 () {
37         }
38
39         public void M3 () {
40         }
41
42         void M4 () {
43         }
44
45 }
46
47 [DebuggerDisplay ("Tests", Name="FOO", Target=typeof (int))]
48 [DebuggerTypeProxy (typeof (Tests))]
49 public class Tests2 {
50         [DebuggerBrowsableAttribute (DebuggerBrowsableState.Collapsed)]
51         public int field_j;
52         public static int static_field_j;
53
54         [DebuggerBrowsableAttribute (DebuggerBrowsableState.Collapsed)]
55         public int AProperty {
56                 get {
57                         return 0;
58                 }
59         }
60
61         public void invoke () {
62         }
63 }
64
65 public struct AStruct {
66         public int i;
67         public string s;
68         public byte k;
69         public IntPtr j;
70
71         [MethodImplAttribute (MethodImplOptions.NoInlining)]
72         public int foo (int val) {
73                 return val;
74         }
75
76         [MethodImplAttribute (MethodImplOptions.NoInlining)]
77         public static int static_foo (int val) {
78                 return val;
79         }
80
81         [MethodImplAttribute (MethodImplOptions.NoInlining)]
82         public int invoke_return_int () {
83                 return i;
84         }
85
86         [MethodImplAttribute (MethodImplOptions.NoInlining)]
87         public static int invoke_static () {
88                 return 5;
89         }
90
91         [MethodImplAttribute (MethodImplOptions.NoInlining)]
92         public IntPtr invoke_return_intptr () {
93                 return j;
94         }
95 }
96
97 public class GClass<T> {
98         public T field;
99         public static T static_field;
100
101         [MethodImplAttribute (MethodImplOptions.NoInlining)]
102         public GClass () {
103         }
104
105         [MethodImplAttribute (MethodImplOptions.NoInlining)]
106         public void bp<T2> () {
107         }
108 }
109
110 public struct GStruct<T> {
111         public T i;
112
113         public int j;
114
115         [MethodImplAttribute (MethodImplOptions.NoInlining)]
116         public int invoke_return_int () {
117                 return j;
118         }
119 }
120
121 interface ITest
122 {
123         void Foo ();
124         void Bar ();
125 }
126
127 interface ITest<T>
128 {
129         void Foo ();
130         void Bar ();
131 }
132
133 class TestIfaces : ITest
134 {
135         void ITest.Foo () {
136         }
137
138         void ITest.Bar () {
139         }
140
141         TestIfaces<int> Baz () {
142                 return null;
143         }
144 }
145
146 class TestIfaces<T> : ITest<T>
147 {
148         void ITest<T>.Foo () {
149         }
150
151         void ITest<T>.Bar () {
152         }
153 }
154
155 public class Tests : TestsBase
156 {
157 #pragma warning disable 0414
158         int field_i;
159         string field_s;
160         AnEnum field_enum;
161         bool field_bool1, field_bool2;
162         char field_char;
163         byte field_byte;
164         sbyte field_sbyte;
165         short field_short;
166         ushort field_ushort;
167         long field_long;
168         ulong field_ulong;
169         float field_float;
170         double field_double;
171         Thread field_class;
172         IntPtr field_intptr;
173         int? field_nullable;
174         static int static_i = 55;
175         static string static_s = "A";
176         public const int literal_i = 56;
177         public const string literal_s = "B";
178         public object child;
179         public AStruct field_struct;
180         public object field_boxed_struct;
181         public GStruct<int> generic_field_struct;
182         [ThreadStatic]
183         public static int tls_i;
184         public static bool is_attached = Debugger.IsAttached;
185
186 #pragma warning restore 0414
187
188         public class NestedClass {
189         }
190
191         public int IntProperty {
192                 get {
193                         return field_i;
194                 }
195                 set {
196                         field_i = value;
197                 }
198         }
199
200         public int ReadOnlyProperty {
201                 get {
202                         return field_i;
203                 }
204         }
205
206         public int this [int index] {
207                 get {
208                         return field_i;
209                 }
210         }
211
212         public static int Main (String[] args) {
213                 tls_i = 42;
214
215                 if (args.Length > 0 && args [0] == "suspend-test")
216                         /* This contains an infinite loop, so execute it conditionally */
217                         suspend ();
218                 if (args.Length >0 && args [0] == "unhandled-exception") {
219                         unhandled_exception ();
220                         return 0;
221                 }
222                 breakpoints ();
223                 single_stepping ();
224                 arguments ();
225                 objects ();
226                 objrefs ();
227                 vtypes ();
228                 locals ();
229                 line_numbers ();
230                 type_info ();
231                 assembly_load ();
232                 invoke ();
233                 exceptions ();
234                 exception_filter ();
235                 threads ();
236                 dynamic_methods ();
237                 user ();
238                 type_load ();
239                 regress ();
240                 if (args.Length > 0 && args [0] == "domain-test")
241                         /* This takes a lot of time, so execute it conditionally */
242                         domains ();
243                 if (args.Length > 0 && args [0] == "ref-emit-test")
244                         ref_emit ();
245                 if (args.Length > 0 && args [0] == "frames-in-native")
246                         frames_in_native ();
247                 if (args.Length >0 && args [0] == "invoke-single-threaded")
248                         new Tests ().invoke_single_threaded ();
249                 return 3;
250         }
251
252         public static void breakpoints () {
253                 /* Call these early so it is JITted by the time a breakpoint is placed on it */
254                 bp3 ();
255                 bp7<int> ();
256                 bp7<string> ();
257
258                 bp1 ();
259                 bp2 ();
260                 bp3 ();
261                 bp4 ();
262                 bp4 ();
263                 bp4 ();
264                 bp5 ();
265                 bp6<string> (new GClass <int> ());
266                 bp7<int> ();
267                 bp7<string> ();
268         }
269
270         [MethodImplAttribute (MethodImplOptions.NoInlining)]
271         public static void bp1 () {
272         }
273
274         [MethodImplAttribute (MethodImplOptions.NoInlining)]
275         public static void bp2 () {
276         }
277
278         [MethodImplAttribute (MethodImplOptions.NoInlining)]
279         public static void bp3 () {
280         }
281
282         [MethodImplAttribute (MethodImplOptions.NoInlining)]
283         public static void bp4 () {
284         }
285
286         [MethodImplAttribute (MethodImplOptions.NoInlining)]
287         public static void bp5 () {
288         }
289
290         [MethodImplAttribute (MethodImplOptions.NoInlining)]
291         public static void bp6<T> (GClass<int> gc) {
292                 gc.bp<int> ();
293         }
294
295         [MethodImplAttribute (MethodImplOptions.NoInlining)]
296         public static void bp7<T> () {
297         }
298
299         [MethodImplAttribute (MethodImplOptions.NoInlining)]
300         public static void single_stepping () {
301                 bool b = true;
302                 ss1 ();
303                 ss2 ();
304                 ss3 ();
305                 ss3_2 ();
306                 ss4 ();
307                 ss5 (new int [] { 1, 2, 3 }, new Func<int, bool> (is_even));
308                 try {
309                         ss6 (b);
310                 } catch {
311                 }
312                 ss7 ();
313                 ss_regress_654694 ();
314         }
315
316         [MethodImplAttribute (MethodImplOptions.NoInlining)]
317         public static void ss1 () {
318         }
319
320         [MethodImplAttribute (MethodImplOptions.NoInlining)]
321         public static void ss2 () {
322         }
323
324         [MethodImplAttribute (MethodImplOptions.NoInlining)]
325         public static int ss3 () {
326                 int sum = 0;
327
328                 for (int i = 0; i < 10; ++i)
329                         sum += i;
330
331                 return sum;
332         }
333
334         [MethodImplAttribute (MethodImplOptions.NoInlining)]
335         public static void ss3_2 () {
336                 ss3_2_2 ();
337         }
338
339         [MethodImplAttribute (MethodImplOptions.NoInlining)]
340         public static void ss3_2_2 () {
341         }
342
343         [MethodImplAttribute (MethodImplOptions.NoInlining)]
344         public static int ss4 () {
345                 ss1 (); ss1 ();
346                 ss2 ();
347                 return 0;
348         }
349
350         [MethodImplAttribute (MethodImplOptions.NoInlining)]
351         public static void ss5 (int[] arr, Func<int, bool> selector) {
352                 // Call into linq which calls back into this assembly
353                 arr.Count (selector);
354         }
355
356         [MethodImplAttribute (MethodImplOptions.NoInlining)]
357         public static void ss6 (bool b) {
358                 if (b) {
359                         ss6_2 ();
360                         throw new Exception ();
361                 }
362         }
363
364         [MethodImplAttribute (MethodImplOptions.NoInlining)]
365         public static void ss6_2 () {
366         }
367
368         [MethodImplAttribute (MethodImplOptions.NoInlining)]
369         public static void ss7 () {
370                 try {
371                         ss7_2 ();
372                         ss7_3 ();
373                 } catch {
374                 }
375                 ss7_2 ();
376         }
377
378         [MethodImplAttribute (MethodImplOptions.NoInlining)]
379         public static void ss7_2 () {
380         }
381
382         [MethodImplAttribute (MethodImplOptions.NoInlining)]
383         public static void ss7_3 () {
384                 throw new Exception ();
385         }
386
387         [MethodImplAttribute (MethodImplOptions.NoInlining)]
388         public static bool is_even (int i) {
389                 return i % 2 == 0;
390         }
391
392         /*
393                 lock (static_s) {
394                         Console.WriteLine ("HIT!");
395                 }
396                 return 0;
397         }
398         */
399
400         [MethodImplAttribute (MethodImplOptions.NoInlining)]
401         public static void arguments () {
402                 arg1 (SByte.MaxValue - 5, Byte.MaxValue - 5, true, Int16.MaxValue - 5, UInt16.MaxValue - 5, 'F', Int32.MaxValue - 5, UInt32.MaxValue - 5, Int64.MaxValue - 5, UInt64.MaxValue - 5, 1.2345f, 6.78910, new IntPtr (Int32.MaxValue - 5), new UIntPtr (UInt32.MaxValue - 5));
403                 int i = 42;
404                 arg2 ("FOO", null, "BLA", ref i, new GClass <int> { field = 42 }, new object ());
405                 Tests t = new Tests () { field_i = 42, field_s = "S" };
406                 t.arg3 ("BLA");
407         }
408
409         [MethodImplAttribute (MethodImplOptions.NoInlining)]
410         public static int arg1 (sbyte sb, byte b, bool bl, short s, ushort us, char c, int i, uint ui, long l, ulong ul, float f, double d, IntPtr ip, UIntPtr uip) {
411                 return (int)(sb + b + (bl ? 0 : 1) + s + us + (int)c + i + ui + l + (long)ul + f + d + (int)ip + (int)uip);
412         }
413
414         [MethodImplAttribute (MethodImplOptions.NoInlining)]
415         public static string arg2 (string s, string s3, object o, ref int i, GClass <int> gc, object o2) {
416                 return s + (s3 != null ? "" : "") + o + i + gc.field + o2;
417         }
418
419         [MethodImplAttribute (MethodImplOptions.NoInlining)]
420         public object arg3 (string s) {
421                 return s + s + s + s + this;
422         }
423
424         [MethodImplAttribute (MethodImplOptions.NoInlining)]
425         public static void objects () {
426                 Tests t = new Tests () { field_i = 42, field_bool1 = true, field_bool2 = false, field_char = 'A', field_byte = 129, field_sbyte = -33, field_short = Int16.MaxValue - 5, field_ushort = UInt16.MaxValue - 5, field_long = Int64.MaxValue - 5, field_ulong = UInt64.MaxValue - 5, field_float = 3.14f, field_double = 3.14f, field_s = "S", base_field_i = 43, base_field_s = "T", field_enum = AnEnum.B, field_class = null, field_intptr = new IntPtr (Int32.MaxValue - 5) };
427                 t.o1 (new Tests2 () { field_j = 43 }, new GClass <int> { field = 42 }, new GClass <string> { field = "FOO" });
428                 o2 (new string [] { "BAR", "BAZ" }, new int[] { 42, 43 }, new int [,] { { 1, 2 }, { 3, 4 }}, (int[,])Array.CreateInstance (typeof (int), new int [] { 2, 2}, new int [] { 1, 3}), new int[] { 0 });
429         }
430
431         [MethodImplAttribute (MethodImplOptions.NoInlining)]
432         public object o1 (Tests2 t, GClass <int> gc1, GClass <string> gc2) {
433                 if (t == null || gc1 == null || gc2 == null)
434                         return null;
435                 else
436                         return this;
437         }
438
439         [MethodImplAttribute (MethodImplOptions.NoInlining)]
440         public static string o2 (string[] s2, int[] s3, int[,] s4, int[,] s5, IList<int> s6) {
441                 return s2 [0] + s3 [0] + s4 [0, 0] + s6 [0];
442         }
443
444         [MethodImplAttribute (MethodImplOptions.NoInlining)]
445         public static void objrefs () {
446                 Tests t = new Tests () {};
447                 set_child (t);
448                 t.objrefs1 ();
449                 t.child = null;
450                 GC.Collect ();
451                 objrefs2 ();
452         }
453
454         [MethodImplAttribute (MethodImplOptions.NoInlining)]
455         public static void set_child (Tests t) {
456                 t.child = new Tests ();
457         }
458
459         [MethodImplAttribute (MethodImplOptions.NoInlining)]
460         public void objrefs1 () {
461         }
462
463         [MethodImplAttribute (MethodImplOptions.NoInlining)]
464         public static void objrefs2 () {
465         }
466
467         public static void vtypes () {
468                 Tests t = new Tests () { field_struct = new AStruct () { i = 42, s = "S", k = 43 }, generic_field_struct = new GStruct<int> () { i = 42 }, field_boxed_struct = new AStruct () { i = 42 }};
469                 AStruct s = new AStruct { i = 44, s = "T", k = 45 };
470                 AStruct[] arr = new AStruct[] { 
471                         new AStruct () { i = 1, s = "S1" },
472                         new AStruct () { i = 2, s = "S2" } };
473                 t.vtypes1 (s, arr);
474                 vtypes2 (s);
475                 vtypes3 (s);
476         }
477
478         [MethodImplAttribute (MethodImplOptions.NoInlining)]
479         public object vtypes1 (AStruct s, AStruct[] arr) {
480                 if (arr != null)
481                         return this;
482                 else
483                         return null;
484         }
485
486         [MethodImplAttribute (MethodImplOptions.NoInlining)]
487         public static void vtypes2 (AStruct s) {
488                 s.foo (5);
489         }
490
491         [MethodImplAttribute (MethodImplOptions.NoInlining)]
492         public static void vtypes3 (AStruct s) {
493                 AStruct.static_foo (5);
494         }
495
496         [MethodImplAttribute (MethodImplOptions.NoInlining)]
497         public static void locals () {
498                 string s = null;
499                 locals1 (null);
500                 locals2<string> (null, 5, "ABC", ref s);
501                 locals3 ();
502                 locals6 ();
503         }
504
505         [MethodImplAttribute (MethodImplOptions.NoInlining)]
506         public static void locals1 (string[] args) {
507                 long foo = 42;
508
509                 for (int j = 0; j < 10; ++j) {
510                         foo ++;
511                 }
512         }
513
514         [MethodImplAttribute (MethodImplOptions.NoInlining)]
515         [StateMachine (typeof (int))]
516         public static void locals2<T> (string[] args, int arg, T t, ref string rs) {
517                 long i = 42;
518                 string s = "AB";
519
520                 for (int j = 0; j < 10; ++j) {
521                         if (s != null)
522                                 i ++;
523                         if (t != null)
524                                 i ++;
525                 }
526                 rs = "A";
527         }
528
529         [MethodImplAttribute (MethodImplOptions.NoInlining)]
530         public static void locals3 () {
531                 string s = "B";
532                 s.ToString ();
533
534                 {
535                         long i = 42;
536                         i ++;
537                         locals4 ();
538                 }
539                 {
540                         string i = "A";
541                         i.ToString ();
542                         locals5 ();
543                 }
544                 {
545                         long j = 42;
546                         j ++;
547                 }
548         }
549
550         [MethodImplAttribute (MethodImplOptions.NoInlining)]
551         public static void locals4 () {
552         }
553
554         [MethodImplAttribute (MethodImplOptions.NoInlining)]
555         public static void locals5 () {
556         }
557
558         [MethodImplAttribute (MethodImplOptions.NoInlining)]
559         public static void locals6 () {
560                 int i = 0;
561                 int j = 0;
562                 for (i = 0; i < 10; ++i)
563                         j ++;
564                 sbyte sb = 0;
565                 for (i = 0; i < 10; ++i)
566                         sb ++;
567                 locals6_1 ();
568                 locals6_2 (j);
569                 locals6_3 ();
570                 locals6_4 (j);
571                 locals6_5 ();
572                 locals6_6 (sb);
573         }
574
575         [MethodImplAttribute (MethodImplOptions.NoInlining)]
576         public static void locals6_1 () {
577         }
578
579         [MethodImplAttribute (MethodImplOptions.NoInlining)]
580         public static void locals6_2 (int arg) {
581         }
582
583         [MethodImplAttribute (MethodImplOptions.NoInlining)]
584         public static void locals6_3 () {
585                 // Clobber all registers
586                 int sum = 0, i, j, k, l, m;
587                 for (i = 0; i < 100; ++i)
588                         sum ++;
589                 for (j = 0; j < 100; ++j)
590                         sum ++;
591                 for (k = 0; k < 100; ++k)
592                         sum ++;
593                 for (l = 0; l < 100; ++l)
594                         sum ++;
595                 for (m = 0; m < 100; ++m)
596                         sum ++;
597         }
598
599         [MethodImplAttribute (MethodImplOptions.NoInlining)]
600         public static void locals6_4 (int arg) {
601         }
602
603         [MethodImplAttribute (MethodImplOptions.NoInlining)]
604         public static void locals6_5 () {
605         }
606
607         [MethodImplAttribute (MethodImplOptions.NoInlining)]
608         public static void locals6_6 (int arg) {
609         }
610
611         [MethodImplAttribute (MethodImplOptions.NoInlining)]
612         public static void line_numbers () {
613                 LineNumbers.ln1 ();
614         }
615
616         [MethodImplAttribute (MethodImplOptions.NoInlining)]
617         public static void suspend () {
618                 long i = 5;
619
620                 while (true) {
621                         i ++;
622                 }
623         }
624
625         [MethodImplAttribute (MethodImplOptions.NoInlining)]
626         public static void type_info () {
627                 Tests t = new Tests () { field_i = 42, field_s = "S", base_field_i = 43, base_field_s = "T", field_enum = AnEnum.B };
628                 t.ti1 (new Tests2 () { field_j = 43 }, new GClass <int> { field = 42 }, new GClass <string> { field = "FOO" });
629                 int val = 0;
630                 unsafe {
631                         AStruct s = new AStruct () { i = 42, s = "S", k = 43 };
632
633                         ti2 (new string [] { "BAR", "BAZ" }, new int[] { 42, 43 }, new int [,] { { 1, 2 }, { 3, 4 }}, ref val, (int*)IntPtr.Zero, 5, s, new Tests (), new Tests2 (), new GClass <int> (), AnEnum.B);
634                 }
635         }
636
637         [MethodImplAttribute (MethodImplOptions.NoInlining)]
638         public object ti1 (Tests2 t, GClass <int> gc1, GClass <string> gc2) {
639                 if (t == null || gc1 == null || gc2 == null)
640                         return null;
641                 else
642                         return this;
643         }
644
645         [MethodImplAttribute (MethodImplOptions.NoInlining)]
646         public static unsafe string ti2 (string[] s2, int[] s3, int[,] s4, ref int ri, int* ptr, int i, AStruct s, Tests t, Tests2 t2, GClass<int> g, AnEnum ae) {
647                 return s2 [0] + s3 [0] + s4 [0, 0];
648         }
649
650         [MethodImplAttribute (MethodImplOptions.NoInlining)]
651         public static void assembly_load () {
652                 assembly_load_2 ();
653         }
654
655         [MethodImplAttribute (MethodImplOptions.NoInlining)]
656         public static void assembly_load_2 () {
657                 // This will load System.dll while holding the loader lock
658                 new Foo ();
659         }
660
661         [MethodImplAttribute (MethodImplOptions.NoInlining)]
662         public static void invoke () {
663                 new Tests ().invoke1 (new Tests2 (), new AStruct () { i = 42, j = (IntPtr)43 }, new GStruct<int> { j = 42 });
664                 new Tests ().invoke_ex ();
665         }
666
667         [MethodImplAttribute (MethodImplOptions.NoInlining)]
668         public void invoke1 (Tests2 t, AStruct s, GStruct<int> g) {
669                 invoke2 ();
670         }
671
672         [MethodImplAttribute (MethodImplOptions.NoInlining)]
673         public void invoke2 () {
674         }
675
676         [MethodImplAttribute (MethodImplOptions.NoInlining)]
677         public void invoke_ex () {
678                 invoke_ex_inner ();
679         }
680
681         [MethodImplAttribute (MethodImplOptions.NoInlining)]
682         public void invoke_ex_inner () {
683                 try {
684                         throw new Exception ();
685                 } catch {
686                 }
687         }
688
689         int counter;
690
691         [MethodImplAttribute (MethodImplOptions.NoInlining)]
692         public void invoke_single_threaded () {
693                 // Spawn a thread incrementing a counter
694                 bool finished = false;
695
696                 new Thread (delegate () {
697                                 while (!finished)
698                                         counter ++;
699                 }).Start ();
700
701                 Thread.Sleep (100);
702
703                 invoke_single_threaded_2 ();
704
705                 finished = true;
706         }
707
708         [MethodImplAttribute (MethodImplOptions.NoInlining)]
709         public void invoke_single_threaded_2 () {
710         }
711
712         public void invoke_return_void () {
713         }
714
715         public string invoke_return_ref () {
716                 return "ABC";
717         }
718
719         public object invoke_return_null () {
720                 return null;
721         }
722
723         public int invoke_return_primitive () {
724                 return 42;
725         }
726
727         public int? invoke_return_nullable () {
728                 return 42;
729         }
730
731         public int? invoke_return_nullable_null () {
732                 return null;
733         }
734
735         public void invoke_type_load () {
736                 new Class3 ();
737         }
738
739         class Class3 {
740         }
741
742         public long invoke_pass_primitive (byte ub, sbyte sb, short ss, ushort us, int i, uint ui, long l, ulong ul, char c, bool b, float f, double d) {
743                 return ub + sb + ss + us + i + ui + l + (long)ul + (int)c + (b ? 1 : 0) + (int)f + (int)d;
744         }
745
746         public int invoke_pass_primitive2 (bool b) {
747                 return b ? 1 : 0;
748         }
749
750         public string invoke_pass_ref (string s) {
751                 return s;
752         }
753
754         public static string invoke_static_pass_ref (string s) {
755                 return s;
756         }
757
758         public static void invoke_static_return_void () {
759         }
760
761         public static void invoke_throws () {
762                 throw new Exception ();
763         }
764
765         [MethodImplAttribute (MethodImplOptions.NoInlining)]
766         public static void exceptions () {
767                 try {
768                         throw new OverflowException ();
769                 } catch (Exception) {
770                 }
771                 try {
772                         throw new OverflowException ();
773                 } catch (Exception) {
774                 }
775                 try {
776                         throw new ArgumentException ();
777                 } catch (Exception) {
778                 }
779                 try {
780                         throw new OverflowException ();
781                 } catch (Exception) {
782                 }
783
784                 object o = null;
785                 try {
786                         o.GetType ();
787                 } catch (Exception) {
788                 }
789
790                 try {
791                         exceptions2 ();
792                 } catch (Exception) {
793                 }
794         }
795
796         [MethodImplAttribute (MethodImplOptions.NoInlining)]
797         public static void unhandled_exception () {
798                 ThreadPool.QueueUserWorkItem (delegate {
799                                 throw new InvalidOperationException ();
800                         });
801                 Thread.Sleep (10000);
802         }
803
804         internal static Delegate create_filter_delegate (Delegate dlg, MethodInfo filter_method)
805         {
806                 if (dlg == null)
807                         throw new ArgumentNullException ();
808                 if (dlg.Target != null)
809                         throw new ArgumentException ();
810                 if (dlg.Method == null)
811                         throw new ArgumentException ();
812
813                 var ret_type = dlg.Method.ReturnType;
814                 var param_types = dlg.Method.GetParameters ().Select (x => x.ParameterType).ToArray ();
815
816                 var dynamic = new DynamicMethod (Guid.NewGuid ().ToString (), ret_type, param_types, typeof (object), true);
817                 var ig = dynamic.GetILGenerator ();
818
819                 LocalBuilder retval = null;
820                 if (ret_type != typeof (void))
821                         retval = ig.DeclareLocal (ret_type);
822
823                 var label = ig.BeginExceptionBlock ();
824
825                 for (int i = 0; i < param_types.Length; i++)
826                         ig.Emit (OpCodes.Ldarg, i);
827                 ig.Emit (OpCodes.Call, dlg.Method);
828
829                 if (retval != null)
830                         ig.Emit (OpCodes.Stloc, retval);
831
832                 ig.Emit (OpCodes.Leave, label);
833
834                 ig.BeginExceptFilterBlock ();
835
836                 ig.Emit (OpCodes.Call, filter_method);
837
838                 ig.BeginCatchBlock (null);
839
840                 ig.Emit (OpCodes.Pop);
841
842                 ig.EndExceptionBlock ();
843
844                 if (retval != null)
845                         ig.Emit (OpCodes.Ldloc, retval);
846
847                 ig.Emit (OpCodes.Ret);
848
849                 return dynamic.CreateDelegate (dlg.GetType ());
850         }
851
852         [MethodImplAttribute (MethodImplOptions.NoInlining)]
853         static void exception_filter_method () {
854                 throw new InvalidOperationException ();
855         }
856
857         [MethodImplAttribute (MethodImplOptions.NoInlining)]
858         static int exception_filter_filter (Exception exc) {
859                 return 1;
860         }
861
862         [MethodImplAttribute (MethodImplOptions.NoInlining)]
863         public static void exception_filter () {
864                 var method = typeof (Tests).GetMethod (
865                         "exception_filter_method", BindingFlags.NonPublic | BindingFlags.Static);
866                 var filter_method = typeof (Tests).GetMethod (
867                         "exception_filter_filter", BindingFlags.NonPublic | BindingFlags.Static);
868
869                 var dlg = Delegate.CreateDelegate (typeof (Action), method);
870
871                 var wrapper = (Action) create_filter_delegate (dlg, filter_method);
872
873                 wrapper ();
874         }
875
876         [MethodImplAttribute (MethodImplOptions.NoInlining)]
877         public static bool return_true () {
878                 return true;
879         }
880
881         [MethodImplAttribute (MethodImplOptions.NoInlining)]
882         public static void exceptions2 () {
883                 if (return_true ())
884                         throw new Exception ();
885                 Console.WriteLine ();
886         }
887
888         [MethodImplAttribute (MethodImplOptions.NoInlining)]
889         public static void threads () {
890                 Thread t = new Thread (delegate () {});
891
892                 t.Start ();
893                 t.Join ();
894         }
895
896         [MethodImplAttribute (MethodImplOptions.NoInlining)]
897         public static void domains () {
898                 AppDomain domain = AppDomain.CreateDomain ("domain");
899
900                 CrossDomain o = (CrossDomain)domain.CreateInstanceAndUnwrap (
901                                    typeof (CrossDomain).Assembly.FullName, "CrossDomain");
902
903                 domains_2 (o, new CrossDomain ());
904
905                 o.invoke_2 ();
906
907                 o.invoke ();
908
909                 o.invoke_2 ();
910
911                 AppDomain.Unload (domain);
912
913                 domains_3 ();
914         }
915
916         [MethodImplAttribute (MethodImplOptions.NoInlining)]
917         public static void domains_2 (object o, object o2) {
918         }
919
920         [MethodImplAttribute (MethodImplOptions.NoInlining)]
921         public static void domains_3 () {
922         }
923
924         [MethodImplAttribute (MethodImplOptions.NoInlining)]
925         public static void invoke_in_domain () {
926         }
927
928         [MethodImplAttribute (MethodImplOptions.NoInlining)]
929         public static void invoke_in_domain_2 () {
930         }
931
932         [MethodImplAttribute (MethodImplOptions.NoInlining)]
933         public static void dynamic_methods () {
934                 var m = new DynamicMethod ("dyn_method", typeof (void), new Type []  { typeof (int) }, typeof (object).Module);
935                 var ig = m.GetILGenerator ();
936
937                 ig.Emit (OpCodes.Ldstr, "FOO");
938                 ig.Emit (OpCodes.Call, typeof (Tests).GetMethod ("dyn_call"));
939                 ig.Emit (OpCodes.Ret);
940
941                 var del = (Action<int>)m.CreateDelegate (typeof (Action<int>));
942
943                 del (0);
944         }
945
946         public static void dyn_call (string s) {
947         }
948
949         [MethodImplAttribute (MethodImplOptions.NoInlining)]
950         public static void ref_emit () {
951                 AssemblyName assemblyName = new AssemblyName ();
952                 assemblyName.Name = "foo";
953
954                 AssemblyBuilder assembly =
955                         Thread.GetDomain ().DefineDynamicAssembly (
956                                                                                                            assemblyName, AssemblyBuilderAccess.RunAndSave);
957
958                 ModuleBuilder module = assembly.DefineDynamicModule ("foo.dll");
959
960                 TypeBuilder tb = module.DefineType ("foo", TypeAttributes.Public, typeof (object));
961                 MethodBuilder mb = tb.DefineMethod ("ref_emit_method", MethodAttributes.Public|MethodAttributes.Static, CallingConventions.Standard, typeof (void), new Type [] { });
962                 ILGenerator ig = mb.GetILGenerator ();
963                 ig.Emit (OpCodes.Ldstr, "FOO");
964                 ig.Emit (OpCodes.Call, typeof (Tests).GetMethod ("ref_emit_call"));
965                 ig.Emit (OpCodes.Ret);
966
967                 Type t = tb.CreateType ();
968
969                 t.GetMethod ("ref_emit_method").Invoke (null, null);
970         }
971
972         [MethodImplAttribute (MethodImplOptions.NoInlining)]
973         public static void ref_emit_call (string s) {
974         }
975
976         [MethodImplAttribute (MethodImplOptions.NoInlining)]
977         public static void frames_in_native () {
978                 Thread.Sleep (500);
979         }
980
981         [MethodImplAttribute (MethodImplOptions.NoInlining)]
982         public static void string_call (string s) {
983         }
984
985         [MethodImplAttribute (MethodImplOptions.NoInlining)]
986         public static void ss_regress_654694 () {
987                 if (true) {
988                         string h = "hi";
989                         string_call (h);
990                 }
991         }
992
993         [MethodImplAttribute (MethodImplOptions.NoInlining)]
994         public static void user () {
995                 Debugger.Break ();
996
997                 Debugger.Log (5, Debugger.IsLogging () ? "A" : "", "B");
998         }
999
1000         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1001         public static void type_load () {
1002                 type_load_2 ();
1003         }
1004
1005         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1006         static void type_load_2 () {
1007                 var c1 = new Dictionary<int, int> ();
1008                 c1.ToString ();
1009                 var c = new TypeLoadClass ();
1010                 c.ToString ();
1011                 var c2 = new TypeLoadClass2 ();
1012                 c2.ToString ();
1013         }
1014
1015         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1016         public static void regress () {
1017                 regress_2755 (DateTime.Now);
1018         }
1019
1020         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1021         public static unsafe void regress_2755 (DateTime d) {
1022                 int* buffer = stackalloc int [128];
1023
1024                 regress_2755_2 ();
1025
1026                 int sum = 0;
1027                 for (int i = 0; i < 128; ++i)
1028                         sum += buffer [i];
1029
1030                 regress_2755_3 (sum);
1031         }
1032
1033         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1034         public static void regress_2755_2 () {
1035         }
1036
1037         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1038         public static void regress_2755_3 (int sum) {
1039         }
1040 }
1041
1042 class TypeLoadClass {
1043 }
1044
1045 class TypeLoadClass2 {
1046 }
1047
1048 public class CrossDomain : MarshalByRefObject
1049 {
1050         public void invoke () {
1051                 Tests.invoke_in_domain ();
1052         }
1053
1054         public void invoke_2 () {
1055                 Tests.invoke_in_domain_2 ();
1056         }
1057
1058         public int invoke_3 () {
1059                 return 42;
1060         }
1061 }       
1062
1063 public class Foo
1064 {
1065         public ProcessStartInfo info;
1066 }
1067
1068 // Class used for line number info testing, don't change its layout
1069 public class LineNumbers
1070 {
1071         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1072         public static void ln1 () {
1073                 // Column 3
1074                 ln2 ();
1075                 ln3 ();
1076         }
1077
1078         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1079         public static void ln2 () {
1080         }
1081
1082         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1083         public static void ln3 () {
1084 #pragma warning disable 0219
1085                 int i = 5;
1086 #pragma warning restore 0219
1087                 #line 55 "FOO"
1088         }
1089 }