BindingFlags.Public needed here as Exception.HResult is now public in .NET 4.5. This...
[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                 breakpoints ();
219                 single_stepping ();
220                 arguments ();
221                 objects ();
222                 objrefs ();
223                 vtypes ();
224                 locals ();
225                 line_numbers ();
226                 type_info ();
227                 assembly_load ();
228                 invoke ();
229                 exceptions ();
230                 exception_filter ();
231                 threads ();
232                 dynamic_methods ();
233                 user ();
234                 type_load ();
235                 regress ();
236                 if (args.Length > 0 && args [0] == "domain-test")
237                         /* This takes a lot of time, so execute it conditionally */
238                         domains ();
239                 if (args.Length > 0 && args [0] == "ref-emit-test")
240                         ref_emit ();
241                 if (args.Length > 0 && args [0] == "frames-in-native")
242                         frames_in_native ();
243                 if (args.Length >0 && args [0] == "invoke-single-threaded")
244                         new Tests ().invoke_single_threaded ();
245                 return 3;
246         }
247
248         public static void breakpoints () {
249                 /* Call these early so it is JITted by the time a breakpoint is placed on it */
250                 bp3 ();
251                 bp7<int> ();
252                 bp7<string> ();
253
254                 bp1 ();
255                 bp2 ();
256                 bp3 ();
257                 bp4 ();
258                 bp4 ();
259                 bp4 ();
260                 bp5 ();
261                 bp6<string> (new GClass <int> ());
262                 bp7<int> ();
263                 bp7<string> ();
264         }
265
266         [MethodImplAttribute (MethodImplOptions.NoInlining)]
267         public static void bp1 () {
268         }
269
270         [MethodImplAttribute (MethodImplOptions.NoInlining)]
271         public static void bp2 () {
272         }
273
274         [MethodImplAttribute (MethodImplOptions.NoInlining)]
275         public static void bp3 () {
276         }
277
278         [MethodImplAttribute (MethodImplOptions.NoInlining)]
279         public static void bp4 () {
280         }
281
282         [MethodImplAttribute (MethodImplOptions.NoInlining)]
283         public static void bp5 () {
284         }
285
286         [MethodImplAttribute (MethodImplOptions.NoInlining)]
287         public static void bp6<T> (GClass<int> gc) {
288                 gc.bp<int> ();
289         }
290
291         [MethodImplAttribute (MethodImplOptions.NoInlining)]
292         public static void bp7<T> () {
293         }
294
295         [MethodImplAttribute (MethodImplOptions.NoInlining)]
296         public static void single_stepping () {
297                 bool b = true;
298                 ss1 ();
299                 ss2 ();
300                 ss3 ();
301                 ss3_2 ();
302                 ss4 ();
303                 ss5 (new int [] { 1, 2, 3 }, new Func<int, bool> (is_even));
304                 try {
305                         ss6 (b);
306                 } catch {
307                 }
308                 ss_regress_654694 ();
309         }
310
311         [MethodImplAttribute (MethodImplOptions.NoInlining)]
312         public static void ss1 () {
313         }
314
315         [MethodImplAttribute (MethodImplOptions.NoInlining)]
316         public static void ss2 () {
317         }
318
319         [MethodImplAttribute (MethodImplOptions.NoInlining)]
320         public static int ss3 () {
321                 int sum = 0;
322
323                 for (int i = 0; i < 10; ++i)
324                         sum += i;
325
326                 return sum;
327         }
328
329         [MethodImplAttribute (MethodImplOptions.NoInlining)]
330         public static void ss3_2 () {
331                 ss3_2_2 ();
332         }
333
334         [MethodImplAttribute (MethodImplOptions.NoInlining)]
335         public static void ss3_2_2 () {
336         }
337
338         [MethodImplAttribute (MethodImplOptions.NoInlining)]
339         public static int ss4 () {
340                 ss1 (); ss1 ();
341                 ss2 ();
342                 return 0;
343         }
344
345         [MethodImplAttribute (MethodImplOptions.NoInlining)]
346         public static void ss5 (int[] arr, Func<int, bool> selector) {
347                 // Call into linq which calls back into this assembly
348                 arr.Count (selector);
349         }
350
351         [MethodImplAttribute (MethodImplOptions.NoInlining)]
352         public static void ss6 (bool b) {
353                 if (b) {
354                         ss7 ();
355                         throw new Exception ();
356                 }
357         }
358
359         [MethodImplAttribute (MethodImplOptions.NoInlining)]
360         public static void ss7 () {
361         }
362
363         [MethodImplAttribute (MethodImplOptions.NoInlining)]
364         public static bool is_even (int i) {
365                 return i % 2 == 0;
366         }
367
368         /*
369                 lock (static_s) {
370                         Console.WriteLine ("HIT!");
371                 }
372                 return 0;
373         }
374         */
375
376         [MethodImplAttribute (MethodImplOptions.NoInlining)]
377         public static void arguments () {
378                 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));
379                 int i = 42;
380                 arg2 ("FOO", null, "BLA", ref i, new GClass <int> { field = 42 }, new object ());
381                 Tests t = new Tests () { field_i = 42, field_s = "S" };
382                 t.arg3 ("BLA");
383         }
384
385         [MethodImplAttribute (MethodImplOptions.NoInlining)]
386         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) {
387                 return (int)(sb + b + (bl ? 0 : 1) + s + us + (int)c + i + ui + l + (long)ul + f + d + (int)ip + (int)uip);
388         }
389
390         [MethodImplAttribute (MethodImplOptions.NoInlining)]
391         public static string arg2 (string s, string s3, object o, ref int i, GClass <int> gc, object o2) {
392                 return s + (s3 != null ? "" : "") + o + i + gc.field + o2;
393         }
394
395         [MethodImplAttribute (MethodImplOptions.NoInlining)]
396         public object arg3 (string s) {
397                 return s + s + s + s + this;
398         }
399
400         [MethodImplAttribute (MethodImplOptions.NoInlining)]
401         public static void objects () {
402                 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) };
403                 t.o1 (new Tests2 () { field_j = 43 }, new GClass <int> { field = 42 }, new GClass <string> { field = "FOO" });
404                 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 });
405         }
406
407         [MethodImplAttribute (MethodImplOptions.NoInlining)]
408         public object o1 (Tests2 t, GClass <int> gc1, GClass <string> gc2) {
409                 if (t == null || gc1 == null || gc2 == null)
410                         return null;
411                 else
412                         return this;
413         }
414
415         [MethodImplAttribute (MethodImplOptions.NoInlining)]
416         public static string o2 (string[] s2, int[] s3, int[,] s4, int[,] s5, IList<int> s6) {
417                 return s2 [0] + s3 [0] + s4 [0, 0] + s6 [0];
418         }
419
420         [MethodImplAttribute (MethodImplOptions.NoInlining)]
421         public static void objrefs () {
422                 Tests t = new Tests () {};
423                 set_child (t);
424                 t.objrefs1 ();
425                 t.child = null;
426                 GC.Collect ();
427                 objrefs2 ();
428         }
429
430         [MethodImplAttribute (MethodImplOptions.NoInlining)]
431         public static void set_child (Tests t) {
432                 t.child = new Tests ();
433         }
434
435         [MethodImplAttribute (MethodImplOptions.NoInlining)]
436         public void objrefs1 () {
437         }
438
439         [MethodImplAttribute (MethodImplOptions.NoInlining)]
440         public static void objrefs2 () {
441         }
442
443         public static void vtypes () {
444                 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 }};
445                 AStruct s = new AStruct { i = 44, s = "T", k = 45 };
446                 AStruct[] arr = new AStruct[] { 
447                         new AStruct () { i = 1, s = "S1" },
448                         new AStruct () { i = 2, s = "S2" } };
449                 t.vtypes1 (s, arr);
450                 vtypes2 (s);
451                 vtypes3 (s);
452         }
453
454         [MethodImplAttribute (MethodImplOptions.NoInlining)]
455         public object vtypes1 (AStruct s, AStruct[] arr) {
456                 if (arr != null)
457                         return this;
458                 else
459                         return null;
460         }
461
462         [MethodImplAttribute (MethodImplOptions.NoInlining)]
463         public static void vtypes2 (AStruct s) {
464                 s.foo (5);
465         }
466
467         [MethodImplAttribute (MethodImplOptions.NoInlining)]
468         public static void vtypes3 (AStruct s) {
469                 AStruct.static_foo (5);
470         }
471
472         [MethodImplAttribute (MethodImplOptions.NoInlining)]
473         public static void locals () {
474                 string s = null;
475                 locals1 (null);
476                 locals2<string> (null, 5, "ABC", ref s);
477                 locals3 ();
478                 locals6 ();
479         }
480
481         [MethodImplAttribute (MethodImplOptions.NoInlining)]
482         public static void locals1 (string[] args) {
483                 long foo = 42;
484
485                 for (int j = 0; j < 10; ++j) {
486                         foo ++;
487                 }
488         }
489
490         [MethodImplAttribute (MethodImplOptions.NoInlining)]
491         public static void locals2<T> (string[] args, int arg, T t, ref string rs) {
492                 long i = 42;
493                 string s = "AB";
494
495                 for (int j = 0; j < 10; ++j) {
496                         if (s != null)
497                                 i ++;
498                         if (t != null)
499                                 i ++;
500                 }
501                 rs = "A";
502         }
503
504         [MethodImplAttribute (MethodImplOptions.NoInlining)]
505         public static void locals3 () {
506                 string s = "B";
507                 s.ToString ();
508
509                 {
510                         long i = 42;
511                         i ++;
512                         locals4 ();
513                 }
514                 {
515                         string i = "A";
516                         i.ToString ();
517                         locals5 ();
518                 }
519                 {
520                         long j = 42;
521                         j ++;
522                 }
523         }
524
525         [MethodImplAttribute (MethodImplOptions.NoInlining)]
526         public static void locals4 () {
527         }
528
529         [MethodImplAttribute (MethodImplOptions.NoInlining)]
530         public static void locals5 () {
531         }
532
533         [MethodImplAttribute (MethodImplOptions.NoInlining)]
534         public static void locals6 () {
535                 int i = 0;
536                 int j = 0;
537                 for (i = 0; i < 10; ++i)
538                         j ++;
539                 sbyte sb = 0;
540                 for (i = 0; i < 10; ++i)
541                         sb ++;
542                 locals6_1 ();
543                 locals6_2 (j);
544                 locals6_3 ();
545                 locals6_4 (j);
546                 locals6_5 ();
547                 locals6_6 (sb);
548         }
549
550         [MethodImplAttribute (MethodImplOptions.NoInlining)]
551         public static void locals6_1 () {
552         }
553
554         [MethodImplAttribute (MethodImplOptions.NoInlining)]
555         public static void locals6_2 (int arg) {
556         }
557
558         [MethodImplAttribute (MethodImplOptions.NoInlining)]
559         public static void locals6_3 () {
560                 // Clobber all registers
561                 int sum = 0, i, j, k, l, m;
562                 for (i = 0; i < 100; ++i)
563                         sum ++;
564                 for (j = 0; j < 100; ++j)
565                         sum ++;
566                 for (k = 0; k < 100; ++k)
567                         sum ++;
568                 for (l = 0; l < 100; ++l)
569                         sum ++;
570                 for (m = 0; m < 100; ++m)
571                         sum ++;
572         }
573
574         [MethodImplAttribute (MethodImplOptions.NoInlining)]
575         public static void locals6_4 (int arg) {
576         }
577
578         [MethodImplAttribute (MethodImplOptions.NoInlining)]
579         public static void locals6_5 () {
580         }
581
582         [MethodImplAttribute (MethodImplOptions.NoInlining)]
583         public static void locals6_6 (int arg) {
584         }
585
586         [MethodImplAttribute (MethodImplOptions.NoInlining)]
587         public static void line_numbers () {
588                 LineNumbers.ln1 ();
589         }
590
591         [MethodImplAttribute (MethodImplOptions.NoInlining)]
592         public static void suspend () {
593                 long i = 5;
594
595                 while (true) {
596                         i ++;
597                 }
598         }
599
600         [MethodImplAttribute (MethodImplOptions.NoInlining)]
601         public static void type_info () {
602                 Tests t = new Tests () { field_i = 42, field_s = "S", base_field_i = 43, base_field_s = "T", field_enum = AnEnum.B };
603                 t.ti1 (new Tests2 () { field_j = 43 }, new GClass <int> { field = 42 }, new GClass <string> { field = "FOO" });
604                 int val = 0;
605                 unsafe {
606                         AStruct s = new AStruct () { i = 42, s = "S", k = 43 };
607
608                         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);
609                 }
610         }
611
612         [MethodImplAttribute (MethodImplOptions.NoInlining)]
613         public object ti1 (Tests2 t, GClass <int> gc1, GClass <string> gc2) {
614                 if (t == null || gc1 == null || gc2 == null)
615                         return null;
616                 else
617                         return this;
618         }
619
620         [MethodImplAttribute (MethodImplOptions.NoInlining)]
621         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) {
622                 return s2 [0] + s3 [0] + s4 [0, 0];
623         }
624
625         [MethodImplAttribute (MethodImplOptions.NoInlining)]
626         public static void assembly_load () {
627                 assembly_load_2 ();
628         }
629
630         [MethodImplAttribute (MethodImplOptions.NoInlining)]
631         public static void assembly_load_2 () {
632                 // This will load System.dll while holding the loader lock
633                 new Foo ();
634         }
635
636         [MethodImplAttribute (MethodImplOptions.NoInlining)]
637         public static void invoke () {
638                 new Tests ().invoke1 (new Tests2 (), new AStruct () { i = 42, j = (IntPtr)43 }, new GStruct<int> { j = 42 });
639                 new Tests ().invoke_ex ();
640         }
641
642         [MethodImplAttribute (MethodImplOptions.NoInlining)]
643         public void invoke1 (Tests2 t, AStruct s, GStruct<int> g) {
644                 invoke2 ();
645         }
646
647         [MethodImplAttribute (MethodImplOptions.NoInlining)]
648         public void invoke2 () {
649         }
650
651         [MethodImplAttribute (MethodImplOptions.NoInlining)]
652         public void invoke_ex () {
653                 invoke_ex_inner ();
654         }
655
656         [MethodImplAttribute (MethodImplOptions.NoInlining)]
657         public void invoke_ex_inner () {
658                 try {
659                         throw new Exception ();
660                 } catch {
661                 }
662         }
663
664         int counter;
665
666         [MethodImplAttribute (MethodImplOptions.NoInlining)]
667         public void invoke_single_threaded () {
668                 // Spawn a thread incrementing a counter
669                 bool finished = false;
670
671                 new Thread (delegate () {
672                                 while (!finished)
673                                         counter ++;
674                 }).Start ();
675
676                 Thread.Sleep (100);
677
678                 invoke_single_threaded_2 ();
679
680                 finished = true;
681         }
682
683         [MethodImplAttribute (MethodImplOptions.NoInlining)]
684         public void invoke_single_threaded_2 () {
685         }
686
687         public void invoke_return_void () {
688         }
689
690         public string invoke_return_ref () {
691                 return "ABC";
692         }
693
694         public object invoke_return_null () {
695                 return null;
696         }
697
698         public int invoke_return_primitive () {
699                 return 42;
700         }
701
702         public int? invoke_return_nullable () {
703                 return 42;
704         }
705
706         public int? invoke_return_nullable_null () {
707                 return null;
708         }
709
710         public void invoke_type_load () {
711                 new Class3 ();
712         }
713
714         class Class3 {
715         }
716
717         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) {
718                 return ub + sb + ss + us + i + ui + l + (long)ul + (int)c + (b ? 1 : 0) + (int)f + (int)d;
719         }
720
721         public int invoke_pass_primitive2 (bool b) {
722                 return b ? 1 : 0;
723         }
724
725         public string invoke_pass_ref (string s) {
726                 return s;
727         }
728
729         public static string invoke_static_pass_ref (string s) {
730                 return s;
731         }
732
733         public static void invoke_static_return_void () {
734         }
735
736         public static void invoke_throws () {
737                 throw new Exception ();
738         }
739
740         [MethodImplAttribute (MethodImplOptions.NoInlining)]
741         public static void exceptions () {
742                 try {
743                         throw new OverflowException ();
744                 } catch (Exception) {
745                 }
746                 try {
747                         throw new OverflowException ();
748                 } catch (Exception) {
749                 }
750                 try {
751                         throw new ArgumentException ();
752                 } catch (Exception) {
753                 }
754                 try {
755                         throw new OverflowException ();
756                 } catch (Exception) {
757                 }
758
759                 object o = null;
760                 try {
761                         o.GetType ();
762                 } catch (Exception) {
763                 }
764
765                 try {
766                         exceptions2 ();
767                 } catch (Exception) {
768                 }
769         }
770
771         internal static Delegate create_filter_delegate (Delegate dlg, MethodInfo filter_method)
772         {
773                 if (dlg == null)
774                         throw new ArgumentNullException ();
775                 if (dlg.Target != null)
776                         throw new ArgumentException ();
777                 if (dlg.Method == null)
778                         throw new ArgumentException ();
779
780                 var ret_type = dlg.Method.ReturnType;
781                 var param_types = dlg.Method.GetParameters ().Select (x => x.ParameterType).ToArray ();
782
783                 var dynamic = new DynamicMethod (Guid.NewGuid ().ToString (), ret_type, param_types, typeof (object), true);
784                 var ig = dynamic.GetILGenerator ();
785
786                 LocalBuilder retval = null;
787                 if (ret_type != typeof (void))
788                         retval = ig.DeclareLocal (ret_type);
789
790                 var label = ig.BeginExceptionBlock ();
791
792                 for (int i = 0; i < param_types.Length; i++)
793                         ig.Emit (OpCodes.Ldarg, i);
794                 ig.Emit (OpCodes.Call, dlg.Method);
795
796                 if (retval != null)
797                         ig.Emit (OpCodes.Stloc, retval);
798
799                 ig.Emit (OpCodes.Leave, label);
800
801                 ig.BeginExceptFilterBlock ();
802
803                 ig.Emit (OpCodes.Call, filter_method);
804
805                 ig.BeginCatchBlock (null);
806
807                 ig.Emit (OpCodes.Pop);
808
809                 ig.EndExceptionBlock ();
810
811                 if (retval != null)
812                         ig.Emit (OpCodes.Ldloc, retval);
813
814                 ig.Emit (OpCodes.Ret);
815
816                 return dynamic.CreateDelegate (dlg.GetType ());
817         }
818
819         [MethodImplAttribute (MethodImplOptions.NoInlining)]
820         static void exception_filter_method () {
821                 throw new InvalidOperationException ();
822         }
823
824         [MethodImplAttribute (MethodImplOptions.NoInlining)]
825         static int exception_filter_filter (Exception exc) {
826                 return 1;
827         }
828
829         [MethodImplAttribute (MethodImplOptions.NoInlining)]
830         public static void exception_filter () {
831                 var method = typeof (Tests).GetMethod (
832                         "exception_filter_method", BindingFlags.NonPublic | BindingFlags.Static);
833                 var filter_method = typeof (Tests).GetMethod (
834                         "exception_filter_filter", BindingFlags.NonPublic | BindingFlags.Static);
835
836                 var dlg = Delegate.CreateDelegate (typeof (Action), method);
837
838                 var wrapper = (Action) create_filter_delegate (dlg, filter_method);
839
840                 wrapper ();
841         }
842
843         [MethodImplAttribute (MethodImplOptions.NoInlining)]
844         public static bool return_true () {
845                 return true;
846         }
847
848         [MethodImplAttribute (MethodImplOptions.NoInlining)]
849         public static void exceptions2 () {
850                 if (return_true ())
851                         throw new Exception ();
852                 Console.WriteLine ();
853         }
854
855         [MethodImplAttribute (MethodImplOptions.NoInlining)]
856         public static void threads () {
857                 Thread t = new Thread (delegate () {});
858
859                 t.Start ();
860                 t.Join ();
861         }
862
863         [MethodImplAttribute (MethodImplOptions.NoInlining)]
864         public static void domains () {
865                 AppDomain domain = AppDomain.CreateDomain ("domain");
866
867                 CrossDomain o = (CrossDomain)domain.CreateInstanceAndUnwrap (
868                                    typeof (CrossDomain).Assembly.FullName, "CrossDomain");
869
870                 o.invoke_2 ();
871
872                 o.invoke ();
873
874                 o.invoke_2 ();
875
876                 AppDomain.Unload (domain);
877
878                 domains_2 ();
879         }
880
881         [MethodImplAttribute (MethodImplOptions.NoInlining)]
882         public static void domains_2 () {
883         }
884
885         [MethodImplAttribute (MethodImplOptions.NoInlining)]
886         public static void invoke_in_domain () {
887         }
888
889         [MethodImplAttribute (MethodImplOptions.NoInlining)]
890         public static void invoke_in_domain_2 () {
891         }
892
893         [MethodImplAttribute (MethodImplOptions.NoInlining)]
894         public static void dynamic_methods () {
895                 var m = new DynamicMethod ("dyn_method", typeof (void), new Type []  { typeof (int) }, typeof (object).Module);
896                 var ig = m.GetILGenerator ();
897
898                 ig.Emit (OpCodes.Ldstr, "FOO");
899                 ig.Emit (OpCodes.Call, typeof (Tests).GetMethod ("dyn_call"));
900                 ig.Emit (OpCodes.Ret);
901
902                 var del = (Action<int>)m.CreateDelegate (typeof (Action<int>));
903
904                 del (0);
905         }
906
907         public static void dyn_call (string s) {
908         }
909
910         [MethodImplAttribute (MethodImplOptions.NoInlining)]
911         public static void ref_emit () {
912                 AssemblyName assemblyName = new AssemblyName ();
913                 assemblyName.Name = "foo";
914
915                 AssemblyBuilder assembly =
916                         Thread.GetDomain ().DefineDynamicAssembly (
917                                                                                                            assemblyName, AssemblyBuilderAccess.RunAndSave);
918
919                 ModuleBuilder module = assembly.DefineDynamicModule ("foo.dll");
920
921                 TypeBuilder tb = module.DefineType ("foo", TypeAttributes.Public, typeof (object));
922                 MethodBuilder mb = tb.DefineMethod ("ref_emit_method", MethodAttributes.Public|MethodAttributes.Static, CallingConventions.Standard, typeof (void), new Type [] { });
923                 ILGenerator ig = mb.GetILGenerator ();
924                 ig.Emit (OpCodes.Ldstr, "FOO");
925                 ig.Emit (OpCodes.Call, typeof (Tests).GetMethod ("ref_emit_call"));
926                 ig.Emit (OpCodes.Ret);
927
928                 Type t = tb.CreateType ();
929
930                 t.GetMethod ("ref_emit_method").Invoke (null, null);
931         }
932
933         [MethodImplAttribute (MethodImplOptions.NoInlining)]
934         public static void ref_emit_call (string s) {
935         }
936
937         [MethodImplAttribute (MethodImplOptions.NoInlining)]
938         public static void frames_in_native () {
939                 Thread.Sleep (500);
940         }
941
942         [MethodImplAttribute (MethodImplOptions.NoInlining)]
943         public static void string_call (string s) {
944         }
945
946         [MethodImplAttribute (MethodImplOptions.NoInlining)]
947         public static void ss_regress_654694 () {
948                 if (true) {
949                         string h = "hi";
950                         string_call (h);
951                 }
952         }
953
954         [MethodImplAttribute (MethodImplOptions.NoInlining)]
955         public static void user () {
956                 Debugger.Break ();
957
958                 Debugger.Log (5, Debugger.IsLogging () ? "A" : "", "B");
959         }
960
961         [MethodImplAttribute (MethodImplOptions.NoInlining)]
962         public static void type_load () {
963                 type_load_2 ();
964         }
965
966         [MethodImplAttribute (MethodImplOptions.NoInlining)]
967         static void type_load_2 () {
968                 var c1 = new Dictionary<int, int> ();
969                 c1.ToString ();
970                 var c = new TypeLoadClass ();
971                 c.ToString ();
972                 var c2 = new TypeLoadClass2 ();
973                 c2.ToString ();
974         }
975
976         [MethodImplAttribute (MethodImplOptions.NoInlining)]
977         public static void regress () {
978                 regress_2755 (DateTime.Now);
979         }
980
981         [MethodImplAttribute (MethodImplOptions.NoInlining)]
982         public static unsafe void regress_2755 (DateTime d) {
983                 int* buffer = stackalloc int [128];
984
985                 regress_2755_2 ();
986
987                 int sum = 0;
988                 for (int i = 0; i < 128; ++i)
989                         sum += buffer [i];
990
991                 regress_2755_3 (sum);
992         }
993
994         [MethodImplAttribute (MethodImplOptions.NoInlining)]
995         public static void regress_2755_2 () {
996         }
997
998         [MethodImplAttribute (MethodImplOptions.NoInlining)]
999         public static void regress_2755_3 (int sum) {
1000         }
1001 }
1002
1003 class TypeLoadClass {
1004 }
1005
1006 class TypeLoadClass2 {
1007 }
1008
1009 public class CrossDomain : MarshalByRefObject
1010 {
1011         public void invoke () {
1012                 Tests.invoke_in_domain ();
1013         }
1014
1015         public void invoke_2 () {
1016                 Tests.invoke_in_domain_2 ();
1017         }
1018 }       
1019
1020 public class Foo
1021 {
1022         public ProcessStartInfo info;
1023 }
1024
1025 // Class used for line number info testing, don't change its layout
1026 public class LineNumbers
1027 {
1028         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1029         public static void ln1 () {
1030                 ln2 ();
1031                 ln3 ();
1032         }
1033
1034         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1035         public static void ln2 () {
1036         }
1037
1038         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1039         public static void ln3 () {
1040 #pragma warning disable 0219
1041                 int i = 5;
1042 #pragma warning restore 0219
1043                 #line 55 "FOO"
1044         }
1045 }