d0b6c668f57dfdbc351d7053a18042bbddcdde2d
[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         public static void locals2<T> (string[] args, int arg, T t, ref string rs) {
516                 long i = 42;
517                 string s = "AB";
518
519                 for (int j = 0; j < 10; ++j) {
520                         if (s != null)
521                                 i ++;
522                         if (t != null)
523                                 i ++;
524                 }
525                 rs = "A";
526         }
527
528         [MethodImplAttribute (MethodImplOptions.NoInlining)]
529         public static void locals3 () {
530                 string s = "B";
531                 s.ToString ();
532
533                 {
534                         long i = 42;
535                         i ++;
536                         locals4 ();
537                 }
538                 {
539                         string i = "A";
540                         i.ToString ();
541                         locals5 ();
542                 }
543                 {
544                         long j = 42;
545                         j ++;
546                 }
547         }
548
549         [MethodImplAttribute (MethodImplOptions.NoInlining)]
550         public static void locals4 () {
551         }
552
553         [MethodImplAttribute (MethodImplOptions.NoInlining)]
554         public static void locals5 () {
555         }
556
557         [MethodImplAttribute (MethodImplOptions.NoInlining)]
558         public static void locals6 () {
559                 int i = 0;
560                 int j = 0;
561                 for (i = 0; i < 10; ++i)
562                         j ++;
563                 sbyte sb = 0;
564                 for (i = 0; i < 10; ++i)
565                         sb ++;
566                 locals6_1 ();
567                 locals6_2 (j);
568                 locals6_3 ();
569                 locals6_4 (j);
570                 locals6_5 ();
571                 locals6_6 (sb);
572         }
573
574         [MethodImplAttribute (MethodImplOptions.NoInlining)]
575         public static void locals6_1 () {
576         }
577
578         [MethodImplAttribute (MethodImplOptions.NoInlining)]
579         public static void locals6_2 (int arg) {
580         }
581
582         [MethodImplAttribute (MethodImplOptions.NoInlining)]
583         public static void locals6_3 () {
584                 // Clobber all registers
585                 int sum = 0, i, j, k, l, m;
586                 for (i = 0; i < 100; ++i)
587                         sum ++;
588                 for (j = 0; j < 100; ++j)
589                         sum ++;
590                 for (k = 0; k < 100; ++k)
591                         sum ++;
592                 for (l = 0; l < 100; ++l)
593                         sum ++;
594                 for (m = 0; m < 100; ++m)
595                         sum ++;
596         }
597
598         [MethodImplAttribute (MethodImplOptions.NoInlining)]
599         public static void locals6_4 (int arg) {
600         }
601
602         [MethodImplAttribute (MethodImplOptions.NoInlining)]
603         public static void locals6_5 () {
604         }
605
606         [MethodImplAttribute (MethodImplOptions.NoInlining)]
607         public static void locals6_6 (int arg) {
608         }
609
610         [MethodImplAttribute (MethodImplOptions.NoInlining)]
611         public static void line_numbers () {
612                 LineNumbers.ln1 ();
613         }
614
615         [MethodImplAttribute (MethodImplOptions.NoInlining)]
616         public static void suspend () {
617                 long i = 5;
618
619                 while (true) {
620                         i ++;
621                 }
622         }
623
624         [MethodImplAttribute (MethodImplOptions.NoInlining)]
625         public static void type_info () {
626                 Tests t = new Tests () { field_i = 42, field_s = "S", base_field_i = 43, base_field_s = "T", field_enum = AnEnum.B };
627                 t.ti1 (new Tests2 () { field_j = 43 }, new GClass <int> { field = 42 }, new GClass <string> { field = "FOO" });
628                 int val = 0;
629                 unsafe {
630                         AStruct s = new AStruct () { i = 42, s = "S", k = 43 };
631
632                         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);
633                 }
634         }
635
636         [MethodImplAttribute (MethodImplOptions.NoInlining)]
637         public object ti1 (Tests2 t, GClass <int> gc1, GClass <string> gc2) {
638                 if (t == null || gc1 == null || gc2 == null)
639                         return null;
640                 else
641                         return this;
642         }
643
644         [MethodImplAttribute (MethodImplOptions.NoInlining)]
645         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) {
646                 return s2 [0] + s3 [0] + s4 [0, 0];
647         }
648
649         [MethodImplAttribute (MethodImplOptions.NoInlining)]
650         public static void assembly_load () {
651                 assembly_load_2 ();
652         }
653
654         [MethodImplAttribute (MethodImplOptions.NoInlining)]
655         public static void assembly_load_2 () {
656                 // This will load System.dll while holding the loader lock
657                 new Foo ();
658         }
659
660         [MethodImplAttribute (MethodImplOptions.NoInlining)]
661         public static void invoke () {
662                 new Tests ().invoke1 (new Tests2 (), new AStruct () { i = 42, j = (IntPtr)43 }, new GStruct<int> { j = 42 });
663                 new Tests ().invoke_ex ();
664         }
665
666         [MethodImplAttribute (MethodImplOptions.NoInlining)]
667         public void invoke1 (Tests2 t, AStruct s, GStruct<int> g) {
668                 invoke2 ();
669         }
670
671         [MethodImplAttribute (MethodImplOptions.NoInlining)]
672         public void invoke2 () {
673         }
674
675         [MethodImplAttribute (MethodImplOptions.NoInlining)]
676         public void invoke_ex () {
677                 invoke_ex_inner ();
678         }
679
680         [MethodImplAttribute (MethodImplOptions.NoInlining)]
681         public void invoke_ex_inner () {
682                 try {
683                         throw new Exception ();
684                 } catch {
685                 }
686         }
687
688         int counter;
689
690         [MethodImplAttribute (MethodImplOptions.NoInlining)]
691         public void invoke_single_threaded () {
692                 // Spawn a thread incrementing a counter
693                 bool finished = false;
694
695                 new Thread (delegate () {
696                                 while (!finished)
697                                         counter ++;
698                 }).Start ();
699
700                 Thread.Sleep (100);
701
702                 invoke_single_threaded_2 ();
703
704                 finished = true;
705         }
706
707         [MethodImplAttribute (MethodImplOptions.NoInlining)]
708         public void invoke_single_threaded_2 () {
709         }
710
711         public void invoke_return_void () {
712         }
713
714         public string invoke_return_ref () {
715                 return "ABC";
716         }
717
718         public object invoke_return_null () {
719                 return null;
720         }
721
722         public int invoke_return_primitive () {
723                 return 42;
724         }
725
726         public int? invoke_return_nullable () {
727                 return 42;
728         }
729
730         public int? invoke_return_nullable_null () {
731                 return null;
732         }
733
734         public void invoke_type_load () {
735                 new Class3 ();
736         }
737
738         class Class3 {
739         }
740
741         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) {
742                 return ub + sb + ss + us + i + ui + l + (long)ul + (int)c + (b ? 1 : 0) + (int)f + (int)d;
743         }
744
745         public int invoke_pass_primitive2 (bool b) {
746                 return b ? 1 : 0;
747         }
748
749         public string invoke_pass_ref (string s) {
750                 return s;
751         }
752
753         public static string invoke_static_pass_ref (string s) {
754                 return s;
755         }
756
757         public static void invoke_static_return_void () {
758         }
759
760         public static void invoke_throws () {
761                 throw new Exception ();
762         }
763
764         [MethodImplAttribute (MethodImplOptions.NoInlining)]
765         public static void exceptions () {
766                 try {
767                         throw new OverflowException ();
768                 } catch (Exception) {
769                 }
770                 try {
771                         throw new OverflowException ();
772                 } catch (Exception) {
773                 }
774                 try {
775                         throw new ArgumentException ();
776                 } catch (Exception) {
777                 }
778                 try {
779                         throw new OverflowException ();
780                 } catch (Exception) {
781                 }
782
783                 object o = null;
784                 try {
785                         o.GetType ();
786                 } catch (Exception) {
787                 }
788
789                 try {
790                         exceptions2 ();
791                 } catch (Exception) {
792                 }
793         }
794
795         [MethodImplAttribute (MethodImplOptions.NoInlining)]
796         public static void unhandled_exception () {
797                 ThreadPool.QueueUserWorkItem (delegate {
798                                 throw new InvalidOperationException ();
799                         });
800                 Thread.Sleep (10000);
801         }
802
803         internal static Delegate create_filter_delegate (Delegate dlg, MethodInfo filter_method)
804         {
805                 if (dlg == null)
806                         throw new ArgumentNullException ();
807                 if (dlg.Target != null)
808                         throw new ArgumentException ();
809                 if (dlg.Method == null)
810                         throw new ArgumentException ();
811
812                 var ret_type = dlg.Method.ReturnType;
813                 var param_types = dlg.Method.GetParameters ().Select (x => x.ParameterType).ToArray ();
814
815                 var dynamic = new DynamicMethod (Guid.NewGuid ().ToString (), ret_type, param_types, typeof (object), true);
816                 var ig = dynamic.GetILGenerator ();
817
818                 LocalBuilder retval = null;
819                 if (ret_type != typeof (void))
820                         retval = ig.DeclareLocal (ret_type);
821
822                 var label = ig.BeginExceptionBlock ();
823
824                 for (int i = 0; i < param_types.Length; i++)
825                         ig.Emit (OpCodes.Ldarg, i);
826                 ig.Emit (OpCodes.Call, dlg.Method);
827
828                 if (retval != null)
829                         ig.Emit (OpCodes.Stloc, retval);
830
831                 ig.Emit (OpCodes.Leave, label);
832
833                 ig.BeginExceptFilterBlock ();
834
835                 ig.Emit (OpCodes.Call, filter_method);
836
837                 ig.BeginCatchBlock (null);
838
839                 ig.Emit (OpCodes.Pop);
840
841                 ig.EndExceptionBlock ();
842
843                 if (retval != null)
844                         ig.Emit (OpCodes.Ldloc, retval);
845
846                 ig.Emit (OpCodes.Ret);
847
848                 return dynamic.CreateDelegate (dlg.GetType ());
849         }
850
851         [MethodImplAttribute (MethodImplOptions.NoInlining)]
852         static void exception_filter_method () {
853                 throw new InvalidOperationException ();
854         }
855
856         [MethodImplAttribute (MethodImplOptions.NoInlining)]
857         static int exception_filter_filter (Exception exc) {
858                 return 1;
859         }
860
861         [MethodImplAttribute (MethodImplOptions.NoInlining)]
862         public static void exception_filter () {
863                 var method = typeof (Tests).GetMethod (
864                         "exception_filter_method", BindingFlags.NonPublic | BindingFlags.Static);
865                 var filter_method = typeof (Tests).GetMethod (
866                         "exception_filter_filter", BindingFlags.NonPublic | BindingFlags.Static);
867
868                 var dlg = Delegate.CreateDelegate (typeof (Action), method);
869
870                 var wrapper = (Action) create_filter_delegate (dlg, filter_method);
871
872                 wrapper ();
873         }
874
875         [MethodImplAttribute (MethodImplOptions.NoInlining)]
876         public static bool return_true () {
877                 return true;
878         }
879
880         [MethodImplAttribute (MethodImplOptions.NoInlining)]
881         public static void exceptions2 () {
882                 if (return_true ())
883                         throw new Exception ();
884                 Console.WriteLine ();
885         }
886
887         [MethodImplAttribute (MethodImplOptions.NoInlining)]
888         public static void threads () {
889                 Thread t = new Thread (delegate () {});
890
891                 t.Start ();
892                 t.Join ();
893         }
894
895         [MethodImplAttribute (MethodImplOptions.NoInlining)]
896         public static void domains () {
897                 AppDomain domain = AppDomain.CreateDomain ("domain");
898
899                 CrossDomain o = (CrossDomain)domain.CreateInstanceAndUnwrap (
900                                    typeof (CrossDomain).Assembly.FullName, "CrossDomain");
901
902                 domains_2 (o, new CrossDomain ());
903
904                 o.invoke_2 ();
905
906                 o.invoke ();
907
908                 o.invoke_2 ();
909
910                 AppDomain.Unload (domain);
911
912                 domains_3 ();
913         }
914
915         [MethodImplAttribute (MethodImplOptions.NoInlining)]
916         public static void domains_2 (object o, object o2) {
917         }
918
919         [MethodImplAttribute (MethodImplOptions.NoInlining)]
920         public static void domains_3 () {
921         }
922
923         [MethodImplAttribute (MethodImplOptions.NoInlining)]
924         public static void invoke_in_domain () {
925         }
926
927         [MethodImplAttribute (MethodImplOptions.NoInlining)]
928         public static void invoke_in_domain_2 () {
929         }
930
931         [MethodImplAttribute (MethodImplOptions.NoInlining)]
932         public static void dynamic_methods () {
933                 var m = new DynamicMethod ("dyn_method", typeof (void), new Type []  { typeof (int) }, typeof (object).Module);
934                 var ig = m.GetILGenerator ();
935
936                 ig.Emit (OpCodes.Ldstr, "FOO");
937                 ig.Emit (OpCodes.Call, typeof (Tests).GetMethod ("dyn_call"));
938                 ig.Emit (OpCodes.Ret);
939
940                 var del = (Action<int>)m.CreateDelegate (typeof (Action<int>));
941
942                 del (0);
943         }
944
945         public static void dyn_call (string s) {
946         }
947
948         [MethodImplAttribute (MethodImplOptions.NoInlining)]
949         public static void ref_emit () {
950                 AssemblyName assemblyName = new AssemblyName ();
951                 assemblyName.Name = "foo";
952
953                 AssemblyBuilder assembly =
954                         Thread.GetDomain ().DefineDynamicAssembly (
955                                                                                                            assemblyName, AssemblyBuilderAccess.RunAndSave);
956
957                 ModuleBuilder module = assembly.DefineDynamicModule ("foo.dll");
958
959                 TypeBuilder tb = module.DefineType ("foo", TypeAttributes.Public, typeof (object));
960                 MethodBuilder mb = tb.DefineMethod ("ref_emit_method", MethodAttributes.Public|MethodAttributes.Static, CallingConventions.Standard, typeof (void), new Type [] { });
961                 ILGenerator ig = mb.GetILGenerator ();
962                 ig.Emit (OpCodes.Ldstr, "FOO");
963                 ig.Emit (OpCodes.Call, typeof (Tests).GetMethod ("ref_emit_call"));
964                 ig.Emit (OpCodes.Ret);
965
966                 Type t = tb.CreateType ();
967
968                 t.GetMethod ("ref_emit_method").Invoke (null, null);
969         }
970
971         [MethodImplAttribute (MethodImplOptions.NoInlining)]
972         public static void ref_emit_call (string s) {
973         }
974
975         [MethodImplAttribute (MethodImplOptions.NoInlining)]
976         public static void frames_in_native () {
977                 Thread.Sleep (500);
978         }
979
980         [MethodImplAttribute (MethodImplOptions.NoInlining)]
981         public static void string_call (string s) {
982         }
983
984         [MethodImplAttribute (MethodImplOptions.NoInlining)]
985         public static void ss_regress_654694 () {
986                 if (true) {
987                         string h = "hi";
988                         string_call (h);
989                 }
990         }
991
992         [MethodImplAttribute (MethodImplOptions.NoInlining)]
993         public static void user () {
994                 Debugger.Break ();
995
996                 Debugger.Log (5, Debugger.IsLogging () ? "A" : "", "B");
997         }
998
999         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1000         public static void type_load () {
1001                 type_load_2 ();
1002         }
1003
1004         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1005         static void type_load_2 () {
1006                 var c1 = new Dictionary<int, int> ();
1007                 c1.ToString ();
1008                 var c = new TypeLoadClass ();
1009                 c.ToString ();
1010                 var c2 = new TypeLoadClass2 ();
1011                 c2.ToString ();
1012         }
1013
1014         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1015         public static void regress () {
1016                 regress_2755 (DateTime.Now);
1017         }
1018
1019         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1020         public static unsafe void regress_2755 (DateTime d) {
1021                 int* buffer = stackalloc int [128];
1022
1023                 regress_2755_2 ();
1024
1025                 int sum = 0;
1026                 for (int i = 0; i < 128; ++i)
1027                         sum += buffer [i];
1028
1029                 regress_2755_3 (sum);
1030         }
1031
1032         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1033         public static void regress_2755_2 () {
1034         }
1035
1036         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1037         public static void regress_2755_3 (int sum) {
1038         }
1039 }
1040
1041 class TypeLoadClass {
1042 }
1043
1044 class TypeLoadClass2 {
1045 }
1046
1047 public class CrossDomain : MarshalByRefObject
1048 {
1049         public void invoke () {
1050                 Tests.invoke_in_domain ();
1051         }
1052
1053         public void invoke_2 () {
1054                 Tests.invoke_in_domain_2 ();
1055         }
1056
1057         public int invoke_3 () {
1058                 return 42;
1059         }
1060 }       
1061
1062 public class Foo
1063 {
1064         public ProcessStartInfo info;
1065 }
1066
1067 // Class used for line number info testing, don't change its layout
1068 public class LineNumbers
1069 {
1070         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1071         public static void ln1 () {
1072                 // Column 3
1073                 ln2 ();
1074                 ln3 ();
1075         }
1076
1077         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1078         public static void ln2 () {
1079         }
1080
1081         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1082         public static void ln3 () {
1083 #pragma warning disable 0219
1084                 int i = 5;
1085 #pragma warning restore 0219
1086                 #line 55 "FOO"
1087         }
1088 }