Revert "Revert "Merge branch 'master' of https://github.com/mono/mono""
[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 public static class Tests4 {
48         static Tests4 () {
49         }
50 }
51
52 public class AAttribute : Attribute {
53         public int afield;
54 }
55
56 public class BAttribute : AAttribute {
57         public int bfield;
58 }
59
60 [DebuggerDisplay ("Tests", Name="FOO", Target=typeof (int))]
61 [DebuggerTypeProxy (typeof (Tests))]
62 [BAttribute (afield = 1, bfield = 2)]
63 public class Tests2 {
64         [DebuggerBrowsableAttribute (DebuggerBrowsableState.Collapsed)]
65         public int field_j;
66         public static int static_field_j;
67
68         [DebuggerBrowsableAttribute (DebuggerBrowsableState.Collapsed)]
69         public int AProperty {
70                 get {
71                         return 0;
72                 }
73         }
74
75         public void invoke () {
76         }
77 }
78
79 public struct AStruct {
80         public int i;
81         public string s;
82         public byte k;
83         public IntPtr j;
84
85         [MethodImplAttribute (MethodImplOptions.NoInlining)]
86         public int foo (int val) {
87                 return val;
88         }
89
90         [MethodImplAttribute (MethodImplOptions.NoInlining)]
91         public static int static_foo (int val) {
92                 return val;
93         }
94
95         [MethodImplAttribute (MethodImplOptions.NoInlining)]
96         public int invoke_return_int () {
97                 return i;
98         }
99
100         [MethodImplAttribute (MethodImplOptions.NoInlining)]
101         public static int invoke_static () {
102                 return 5;
103         }
104
105         [MethodImplAttribute (MethodImplOptions.NoInlining)]
106         public IntPtr invoke_return_intptr () {
107                 return j;
108         }
109 }
110
111 public class GClass<T> {
112         public T field;
113         public static T static_field;
114
115         [MethodImplAttribute (MethodImplOptions.NoInlining)]
116         public GClass () {
117         }
118
119         [MethodImplAttribute (MethodImplOptions.NoInlining)]
120         public void bp<T2> () {
121         }
122 }
123
124 public struct GStruct<T> {
125         public T i;
126
127         public int j;
128
129         [MethodImplAttribute (MethodImplOptions.NoInlining)]
130         public int invoke_return_int () {
131                 return j;
132         }
133 }
134
135 public struct NestedStruct {
136         NestedInner nested1, nested2;
137 }
138
139 public struct NestedInner {
140 }
141
142 interface ITest
143 {
144         void Foo ();
145         void Bar ();
146 }
147
148 interface ITest<T>
149 {
150         void Foo ();
151         void Bar ();
152 }
153
154 class TestIfaces : ITest
155 {
156         void ITest.Foo () {
157         }
158
159         void ITest.Bar () {
160         }
161
162         TestIfaces<int> Baz () {
163                 return null;
164         }
165 }
166
167 class TestIfaces<T> : ITest<T>
168 {
169         void ITest<T>.Foo () {
170         }
171
172         void ITest<T>.Bar () {
173         }
174 }
175
176 public interface ITest2
177 {
178         int invoke_iface ();
179 }
180
181 public class Tests : TestsBase, ITest2
182 {
183 #pragma warning disable 0414
184         int field_i;
185         string field_s;
186         AnEnum field_enum;
187         bool field_bool1, field_bool2;
188         char field_char;
189         byte field_byte;
190         sbyte field_sbyte;
191         short field_short;
192         ushort field_ushort;
193         long field_long;
194         ulong field_ulong;
195         float field_float;
196         double field_double;
197         Thread field_class;
198         IntPtr field_intptr;
199         int? field_nullable;
200         static int static_i = 55;
201         static string static_s = "A";
202         public const int literal_i = 56;
203         public const string literal_s = "B";
204         public object child;
205         public AStruct field_struct;
206         public object field_boxed_struct;
207         public GStruct<int> generic_field_struct;
208         public KeyValuePair<int, object> boxed_struct_field;
209         [ThreadStatic]
210         public static int tls_i;
211         public static bool is_attached = Debugger.IsAttached;
212         public NestedStruct nested_struct;
213
214 #pragma warning restore 0414
215
216         public class NestedClass {
217         }
218
219         public int IntProperty {
220                 get {
221                         return field_i;
222                 }
223                 set {
224                         field_i = value;
225                 }
226         }
227
228         public int ReadOnlyProperty {
229                 get {
230                         return field_i;
231                 }
232         }
233
234         public int this [int index] {
235                 get {
236                         return field_i;
237                 }
238         }
239
240         public static void wait_one ()
241         {
242                 ManualResetEvent evt = new ManualResetEvent (false);
243                 evt.WaitOne ();
244         }
245
246         public static int Main (String[] args) {
247                 tls_i = 42;
248
249                 if (args.Length > 0 && args [0] == "suspend-test")
250                         /* This contains an infinite loop, so execute it conditionally */
251                         suspend ();
252                 if (args.Length >0 && args [0] == "unhandled-exception") {
253                         unhandled_exception ();
254                         return 0;
255                 }
256                 if (args.Length >0 && args [0] == "unhandled-exception-endinvoke") {
257                         unhandled_exception_endinvoke ();
258                         return 0;
259                 }
260                 if (args.Length >0 && args [0] == "unhandled-exception-user") {
261                         unhandled_exception_user ();
262                         return 0;
263                 }
264                 if (args.Length >0 && args [0] == "wait-one") {
265                         wait_one ();
266                         return 0;
267                 }
268                 breakpoints ();
269                 single_stepping ();
270                 arguments ();
271                 objects ();
272                 objrefs ();
273                 vtypes ();
274                 locals ();
275                 line_numbers ();
276                 type_info ();
277                 assembly_load ();
278                 invoke ();
279                 exceptions ();
280                 exception_filter ();
281                 threads ();
282                 dynamic_methods ();
283                 user ();
284                 type_load ();
285                 regress ();
286                 gc_suspend ();
287                 set_ip ();
288                 if (args.Length > 0 && args [0] == "domain-test")
289                         /* This takes a lot of time, so execute it conditionally */
290                         domains ();
291                 if (args.Length > 0 && args [0] == "ref-emit-test")
292                         ref_emit ();
293                 if (args.Length > 0 && args [0] == "frames-in-native")
294                         frames_in_native ();
295                 if (args.Length > 0 && args [0] == "invoke-single-threaded")
296                         new Tests ().invoke_single_threaded ();
297                 new Tests ().evaluate_method ();
298                 return 3;
299         }
300
301         public static void breakpoints () {
302                 /* Call these early so it is JITted by the time a breakpoint is placed on it */
303                 bp3 ();
304                 bp7<int> ();
305                 bp7<string> ();
306
307                 bp1 ();
308                 bp2 ();
309                 bp3 ();
310                 bp4 ();
311                 bp4 ();
312                 bp4 ();
313                 bp5 ();
314                 bp6<string> (new GClass <int> ());
315                 bp7<int> ();
316                 bp7<string> ();
317         }
318
319         [MethodImplAttribute (MethodImplOptions.NoInlining)]
320         public static void bp1 () {
321         }
322
323         [MethodImplAttribute (MethodImplOptions.NoInlining)]
324         public static void bp2 () {
325         }
326
327         [MethodImplAttribute (MethodImplOptions.NoInlining)]
328         public static void bp3 () {
329         }
330
331         [MethodImplAttribute (MethodImplOptions.NoInlining)]
332         public static void bp4 () {
333         }
334
335         [MethodImplAttribute (MethodImplOptions.NoInlining)]
336         public static void bp5 () {
337         }
338
339         [MethodImplAttribute (MethodImplOptions.NoInlining)]
340         public static void bp6<T> (GClass<int> gc) {
341                 gc.bp<int> ();
342         }
343
344         [MethodImplAttribute (MethodImplOptions.NoInlining)]
345         public static void bp7<T> () {
346         }
347
348         [MethodImplAttribute (MethodImplOptions.NoInlining)]
349         public static void single_stepping () {
350                 bool b = true;
351                 ss1 ();
352                 ss2 ();
353                 ss3 ();
354                 ss3_2 ();
355                 ss4 ();
356                 ss5 (new int [] { 1, 2, 3 }, new Func<int, bool> (is_even));
357                 try {
358                         ss6 (b);
359                 } catch {
360                 }
361                 ss7 ();
362                 ss_nested ();
363                 ss_regress_654694 ();
364                 ss_step_through ();
365                 ss_non_user_code ();
366                 ss_recursive (1);
367                 ss_fp_clobber ();
368         }
369
370         [MethodImplAttribute (MethodImplOptions.NoInlining)]
371         public static void ss1 () {
372         }
373
374         [MethodImplAttribute (MethodImplOptions.NoInlining)]
375         public static void ss2 () {
376         }
377
378         [MethodImplAttribute (MethodImplOptions.NoInlining)]
379         public static int ss3 () {
380                 int sum = 0;
381
382                 for (int i = 0; i < 10; ++i)
383                         sum += i;
384
385                 return sum;
386         }
387
388         [MethodImplAttribute (MethodImplOptions.NoInlining)]
389         public static void ss3_2 () {
390                 ss3_2_2 ();
391         }
392
393         [MethodImplAttribute (MethodImplOptions.NoInlining)]
394         public static void ss3_2_2 () {
395         }
396
397         [MethodImplAttribute (MethodImplOptions.NoInlining)]
398         public static int ss4 () {
399                 ss1 (); ss1 ();
400                 ss2 ();
401                 return 0;
402         }
403
404         [MethodImplAttribute (MethodImplOptions.NoInlining)]
405         public static void ss5 (int[] arr, Func<int, bool> selector) {
406                 // Call into linq which calls back into this assembly
407                 arr.Count (selector);
408         }
409
410         [MethodImplAttribute (MethodImplOptions.NoInlining)]
411         public static void ss6 (bool b) {
412                 if (b) {
413                         ss6_2 ();
414                         throw new Exception ();
415                 }
416         }
417
418         [MethodImplAttribute (MethodImplOptions.NoInlining)]
419         public static void ss6_2 () {
420         }
421
422         [MethodImplAttribute (MethodImplOptions.NoInlining)]
423         public static void ss7 () {
424                 try {
425                         ss7_2 ();
426                         ss7_3 ();
427                 } catch {
428                 }
429                 ss7_2 ();
430         }
431
432         [MethodImplAttribute (MethodImplOptions.NoInlining)]
433         public static void ss7_2 () {
434         }
435
436         [MethodImplAttribute (MethodImplOptions.NoInlining)]
437         public static void ss7_3 () {
438                 throw new Exception ();
439         }
440
441         [MethodImplAttribute (MethodImplOptions.NoInlining)]
442         public static void ss_nested () {
443                 ss_nested_1 (ss_nested_2 ());
444                 ss_nested_1 (ss_nested_2 ());
445                 ss_nested_3 ();
446         }
447
448         [MethodImplAttribute (MethodImplOptions.NoInlining)]
449         public static void ss_nested_1 (int i) {
450         }
451
452         [MethodImplAttribute (MethodImplOptions.NoInlining)]
453         public static int ss_nested_2 () {
454                 return 0;
455         }
456
457         [MethodImplAttribute (MethodImplOptions.NoInlining)]
458         public static void ss_nested_3 () {
459         }
460
461         [MethodImplAttribute (MethodImplOptions.NoInlining)]
462         public static void ss_step_through () {
463                 step_through_1 ();
464                 StepThroughClass.step_through_2 ();
465                 step_through_3 ();
466         }
467
468         [DebuggerStepThrough]
469         [MethodImplAttribute (MethodImplOptions.NoInlining)]
470         public static void step_through_1 () {
471         }
472
473         [DebuggerStepThrough]
474         class StepThroughClass {
475                 [MethodImplAttribute (MethodImplOptions.NoInlining)]
476                 public static void step_through_2 () {
477                 }
478         }
479
480         [DebuggerStepThrough]
481         [MethodImplAttribute (MethodImplOptions.NoInlining)]
482         public static void step_through_3 () {
483         }
484
485         [MethodImplAttribute (MethodImplOptions.NoInlining)]
486         public static void ss_non_user_code () {
487                 non_user_code_1 ();
488                 StepNonUserCodeClass.non_user_code_2 ();
489                 non_user_code_3 ();
490         }
491
492         [DebuggerNonUserCode]
493         [MethodImplAttribute (MethodImplOptions.NoInlining)]
494         public static void non_user_code_1 () {
495         }
496
497         [DebuggerNonUserCode]
498         class StepNonUserCodeClass {
499                 [MethodImplAttribute (MethodImplOptions.NoInlining)]
500                 public static void non_user_code_2 () {
501                 }
502         }
503
504         [DebuggerNonUserCode]
505         [MethodImplAttribute (MethodImplOptions.NoInlining)]
506         public static void non_user_code_3 () {
507         }
508
509         [MethodImplAttribute (MethodImplOptions.NoInlining)]
510         public static void ss_recursive (int n) {
511                 if (n == 10)
512                         return;
513                 ss_recursive (n + 1);
514         }
515
516         [MethodImplAttribute (MethodImplOptions.NoInlining)]
517         public static void ss_fp_clobber () {
518                 double v = ss_fp_clobber_1 (5.0);
519                 ss_fp_clobber_2 (v);
520         }
521
522         [MethodImplAttribute (MethodImplOptions.NoInlining)]
523         public static double ss_fp_clobber_1 (double d) {
524                 return d + 2.0;
525         }
526
527         [MethodImplAttribute (MethodImplOptions.NoInlining)]
528         public static void ss_fp_clobber_2 (double d) {
529         }
530
531         [MethodImplAttribute (MethodImplOptions.NoInlining)]
532         public static bool is_even (int i) {
533                 return i % 2 == 0;
534         }
535
536         /*
537                 lock (static_s) {
538                         Console.WriteLine ("HIT!");
539                 }
540                 return 0;
541         }
542         */
543
544         [MethodImplAttribute (MethodImplOptions.NoInlining)]
545         public static void arguments () {
546                 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));
547                 int i = 42;
548                 arg2 ("FOO", null, "BLA", ref i, new GClass <int> { field = 42 }, new object ());
549                 Tests t = new Tests () { field_i = 42, field_s = "S" };
550                 t.arg3 ("BLA");
551         }
552
553         [MethodImplAttribute (MethodImplOptions.NoInlining)]
554         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) {
555                 return (int)(sb + b + (bl ? 0 : 1) + s + us + (int)c + i + ui + l + (long)ul + f + d + (int)ip + (int)uip);
556         }
557
558         [MethodImplAttribute (MethodImplOptions.NoInlining)]
559         public static string arg2 (string s, string s3, object o, ref int i, GClass <int> gc, object o2) {
560                 return s + (s3 != null ? "" : "") + o + i + gc.field + o2;
561         }
562
563         [MethodImplAttribute (MethodImplOptions.NoInlining)]
564         public object arg3 (string s) {
565                 return s + s + s + s + this;
566         }
567
568         [MethodImplAttribute (MethodImplOptions.NoInlining)]
569         public static void objects () {
570                 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), field_nullable = null };
571                 t.o1 (new Tests2 () { field_j = 43 }, new GClass <int> { field = 42 }, new GClass <string> { field = "FOO" });
572                 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 });
573         }
574
575         [MethodImplAttribute (MethodImplOptions.NoInlining)]
576         public object o1 (Tests2 t, GClass <int> gc1, GClass <string> gc2) {
577                 if (t == null || gc1 == null || gc2 == null)
578                         return null;
579                 else
580                         return this;
581         }
582
583         [MethodImplAttribute (MethodImplOptions.NoInlining)]
584         public static string o2 (string[] s2, int[] s3, int[,] s4, int[,] s5, IList<int> s6) {
585                 return s2 [0] + s3 [0] + s4 [0, 0] + s6 [0];
586         }
587
588         [MethodImplAttribute (MethodImplOptions.NoInlining)]
589         public static void objrefs () {
590                 Tests t = new Tests () {};
591                 set_child (t);
592                 t.objrefs1 ();
593                 t.child = null;
594                 GC.Collect ();
595                 objrefs2 ();
596         }
597
598         [MethodImplAttribute (MethodImplOptions.NoInlining)]
599         public static void set_child (Tests t) {
600                 t.child = new Tests ();
601         }
602
603         [MethodImplAttribute (MethodImplOptions.NoInlining)]
604         public void objrefs1 () {
605         }
606
607         [MethodImplAttribute (MethodImplOptions.NoInlining)]
608         public static void objrefs2 () {
609         }
610
611         public static void vtypes () {
612                 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 }, boxed_struct_field = new KeyValuePair<int, object> (1, (long)42 ) };
613                 AStruct s = new AStruct { i = 44, s = "T", k = 45 };
614                 AStruct[] arr = new AStruct[] { 
615                         new AStruct () { i = 1, s = "S1" },
616                         new AStruct () { i = 2, s = "S2" } };
617                 t.vtypes1 (s, arr);
618                 vtypes2 (s);
619                 vtypes3 (s);
620         }
621
622         [MethodImplAttribute (MethodImplOptions.NoInlining)]
623         public object vtypes1 (AStruct s, AStruct[] arr) {
624                 if (arr != null)
625                         return this;
626                 else
627                         return null;
628         }
629
630         [MethodImplAttribute (MethodImplOptions.NoInlining)]
631         public static void vtypes2 (AStruct s) {
632                 s.foo (5);
633         }
634
635         [MethodImplAttribute (MethodImplOptions.NoInlining)]
636         public static void vtypes3 (AStruct s) {
637                 AStruct.static_foo (5);
638         }
639
640         [MethodImplAttribute (MethodImplOptions.NoInlining)]
641         public static void locals () {
642                 string s = null;
643                 var astruct = new AStruct () { i = 42 };
644                 locals1 (null);
645                 locals2<string> (null, 5, "ABC", ref s, ref astruct);
646                 locals3 ();
647                 locals6 ();
648                 locals7<int> (22);
649         }
650
651         [MethodImplAttribute (MethodImplOptions.NoInlining)]
652         static void locals11 (double a, ref double b) {
653         }
654
655         [MethodImplAttribute (MethodImplOptions.NoInlining)]
656         public static void locals1 (string[] args) {
657                 long foo = 42;
658
659                 double ri = 1;
660                 locals11 (b: ref ri, a: ri);
661
662                 for (int j = 0; j < 10; ++j) {
663                         foo ++;
664                 }
665         }
666
667         [MethodImplAttribute (MethodImplOptions.NoInlining)]
668 #if NET_4_5
669         [StateMachine (typeof (int))]
670 #endif
671         public static void locals2<T> (string[] args, int arg, T t, ref string rs, ref AStruct astruct) {
672                 long i = 42;
673                 string s = "AB";
674
675                 for (int j = 0; j < 10; ++j) {
676                         if (s != null)
677                                 i ++;
678                         if (t != null)
679                                 i ++;
680                         astruct = new AStruct ();
681                 }
682                 rs = "A";
683         }
684
685
686         [MethodImplAttribute (MethodImplOptions.NoInlining)]
687         public static void locals3 () {
688                 string s = "B";
689                 s.ToString ();
690
691                 {
692                         long i = 42;
693                         i ++;
694                         locals4 ();
695                 }
696                 {
697                         string i = "A";
698                         i.ToString ();
699                         locals5 ();
700                 }
701                 {
702                         long j = 42;
703                         j ++;
704                 }
705         }
706
707         [MethodImplAttribute (MethodImplOptions.NoInlining)]
708         public static void locals4 () {
709         }
710
711         [MethodImplAttribute (MethodImplOptions.NoInlining)]
712         public static void locals5 () {
713         }
714
715         [MethodImplAttribute (MethodImplOptions.NoInlining)]
716         public static void locals6 () {
717                 int i = 0;
718                 int j = 0;
719                 for (i = 0; i < 10; ++i)
720                         j ++;
721                 sbyte sb = 0;
722                 for (i = 0; i < 10; ++i)
723                         sb ++;
724                 locals6_1 ();
725                 locals6_2 (j);
726                 locals6_3 ();
727                 locals6_4 (j);
728                 locals6_5 ();
729                 locals6_6 (sb);
730         }
731
732         [MethodImplAttribute (MethodImplOptions.NoInlining)]
733         public static void locals6_1 () {
734         }
735
736         [MethodImplAttribute (MethodImplOptions.NoInlining)]
737         public static void locals6_2 (int arg) {
738         }
739
740         [MethodImplAttribute (MethodImplOptions.NoInlining)]
741         public static void locals6_3 () {
742                 // Clobber all registers
743                 int sum = 0, i, j, k, l, m;
744                 for (i = 0; i < 100; ++i)
745                         sum ++;
746                 for (j = 0; j < 100; ++j)
747                         sum ++;
748                 for (k = 0; k < 100; ++k)
749                         sum ++;
750                 for (l = 0; l < 100; ++l)
751                         sum ++;
752                 for (m = 0; m < 100; ++m)
753                         sum ++;
754         }
755
756         [MethodImplAttribute (MethodImplOptions.NoInlining)]
757         public static void locals6_4 (int arg) {
758         }
759
760         [MethodImplAttribute (MethodImplOptions.NoInlining)]
761         public static void locals6_5 () {
762         }
763
764         [MethodImplAttribute (MethodImplOptions.NoInlining)]
765         public static void locals6_6 (int arg) {
766         }
767
768         [MethodImplAttribute (MethodImplOptions.NoInlining)]
769         public static void locals7<T> (T arg) {
770                 T t = arg;
771                 T t2 = t;
772         }
773
774         [MethodImplAttribute (MethodImplOptions.NoInlining)]
775         public static void line_numbers () {
776                 LineNumbers.ln1 ();
777         }
778
779         [MethodImplAttribute (MethodImplOptions.NoInlining)]
780         public static void suspend () {
781                 long i = 5;
782
783                 while (true) {
784                         i ++;
785                 }
786         }
787
788         [MethodImplAttribute (MethodImplOptions.NoInlining)]
789         public static void type_info () {
790                 Tests t = new Tests () { field_i = 42, field_s = "S", base_field_i = 43, base_field_s = "T", field_enum = AnEnum.B };
791                 t.ti1 (new Tests2 () { field_j = 43 }, new GClass <int> { field = 42 }, new GClass <string> { field = "FOO" });
792                 int val = 0;
793                 unsafe {
794                         AStruct s = new AStruct () { i = 42, s = "S", k = 43 };
795
796                         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);
797                 }
798         }
799
800         [MethodImplAttribute (MethodImplOptions.NoInlining)]
801         public object ti1 (Tests2 t, GClass <int> gc1, GClass <string> gc2) {
802                 if (t == null || gc1 == null || gc2 == null)
803                         return null;
804                 else
805                         return this;
806         }
807
808         [MethodImplAttribute (MethodImplOptions.NoInlining)]
809         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) {
810                 return s2 [0] + s3 [0] + s4 [0, 0];
811         }
812
813         [MethodImplAttribute (MethodImplOptions.NoInlining)]
814         public static void assembly_load () {
815                 assembly_load_2 ();
816         }
817
818         [MethodImplAttribute (MethodImplOptions.NoInlining)]
819         public static void assembly_load_2 () {
820                 // This will load System.dll while holding the loader lock
821                 new Foo ();
822         }
823
824         [MethodImplAttribute (MethodImplOptions.NoInlining)]
825         public static void invoke () {
826                 new Tests ().invoke1 (new Tests2 (), new AStruct () { i = 42, j = (IntPtr)43 }, new GStruct<int> { j = 42 });
827                 new Tests ().invoke_ex ();
828         }
829
830         [MethodImplAttribute (MethodImplOptions.NoInlining)]
831         public void invoke1 (Tests2 t, AStruct s, GStruct<int> g) {
832                 invoke2 ();
833         }
834
835         [MethodImplAttribute (MethodImplOptions.NoInlining)]
836         public void invoke2 () {
837         }
838
839         [MethodImplAttribute (MethodImplOptions.NoInlining)]
840         public void invoke_ex () {
841                 invoke_ex_inner ();
842         }
843
844         [MethodImplAttribute (MethodImplOptions.NoInlining)]
845         public void invoke_ex_inner () {
846                 try {
847                         throw new Exception ();
848                 } catch {
849                 }
850         }
851
852         int counter;
853
854         [MethodImplAttribute (MethodImplOptions.NoInlining)]
855         public void invoke_single_threaded () {
856                 // Spawn a thread incrementing a counter
857                 bool finished = false;
858
859                 new Thread (delegate () {
860                                 while (!finished)
861                                         counter ++;
862                 }).Start ();
863
864                 Thread.Sleep (100);
865
866                 invoke_single_threaded_2 ();
867
868                 finished = true;
869         }
870
871         [MethodImplAttribute (MethodImplOptions.NoInlining)]
872         public void invoke_single_threaded_2 () {
873         }
874
875         public void invoke_return_void () {
876         }
877
878         public string invoke_return_ref () {
879                 return "ABC";
880         }
881
882         public object invoke_return_null () {
883                 return null;
884         }
885
886         public int invoke_return_primitive () {
887                 return 42;
888         }
889
890         public int? invoke_return_nullable () {
891                 return 42;
892         }
893
894         public int? invoke_return_nullable_null () {
895                 return null;
896         }
897
898         public void invoke_type_load () {
899                 new Class3 ();
900         }
901
902         class Class3 {
903         }
904
905         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) {
906                 return ub + sb + ss + us + i + ui + l + (long)ul + (int)c + (b ? 1 : 0) + (int)f + (int)d;
907         }
908
909         public int invoke_pass_primitive2 (bool b) {
910                 return b ? 1 : 0;
911         }
912
913         public string invoke_pass_ref (string s) {
914                 return s;
915         }
916
917         public static string invoke_static_pass_ref (string s) {
918                 return s;
919         }
920
921         public static void invoke_static_return_void () {
922         }
923
924         public static void invoke_throws () {
925                 throw new Exception ();
926         }
927
928         public int invoke_iface () {
929                 return 42;
930         }
931
932         [MethodImplAttribute (MethodImplOptions.NoInlining)]
933         public static void exceptions () {
934                 try {
935                         throw new OverflowException ();
936                 } catch (Exception) {
937                 }
938                 try {
939                         throw new OverflowException ();
940                 } catch (Exception) {
941                 }
942                 try {
943                         throw new ArgumentException ();
944                 } catch (Exception) {
945                 }
946                 try {
947                         throw new OverflowException ();
948                 } catch (Exception) {
949                 }
950                 // no subclasses
951                 try {
952                         throw new OverflowException ();
953                 } catch (Exception) {
954                 }
955                 try {
956                         throw new Exception ();
957                 } catch (Exception) {
958                 }
959
960                 object o = null;
961                 try {
962                         o.GetType ();
963                 } catch (Exception) {
964                 }
965
966                 try {
967                         exceptions2 ();
968                 } catch (Exception) {
969                 }
970         }
971
972         [MethodImplAttribute (MethodImplOptions.NoInlining)]
973         public static void unhandled_exception () {
974                 ThreadPool.QueueUserWorkItem (delegate {
975                                 throw new InvalidOperationException ();
976                         });
977                 Thread.Sleep (10000);
978         }
979
980         [MethodImplAttribute (MethodImplOptions.NoInlining)]
981         public static void unhandled_exception_endinvoke_2 () {
982         }
983
984         [MethodImplAttribute (MethodImplOptions.NoInlining)]
985         public static void unhandled_exception_endinvoke () {
986                         Action action = new Action (() => 
987                         {
988                                 throw new Exception ("thrown");
989                         });
990                         action.BeginInvoke ((ar) => {
991                                 try {
992                                         action.EndInvoke (ar);
993                                 } catch (Exception ex) {
994                                         //Console.WriteLine (ex);
995                                 }
996                         }, null);
997                 Thread.Sleep (1000);
998                 unhandled_exception_endinvoke_2 ();
999         }
1000
1001         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1002         public static void unhandled_exception_user () {
1003 #if NET_4_5
1004                 System.Threading.Tasks.Task.Factory.StartNew (() => {
1005                                 Throw ();
1006                         });
1007                 Thread.Sleep (10000);
1008 #endif
1009         }
1010
1011         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1012         public static void Throw () {
1013                 throw new Exception ();
1014         }
1015
1016         internal static Delegate create_filter_delegate (Delegate dlg, MethodInfo filter_method)
1017         {
1018                 if (dlg == null)
1019                         throw new ArgumentNullException ();
1020                 if (dlg.Target != null)
1021                         throw new ArgumentException ();
1022                 if (dlg.Method == null)
1023                         throw new ArgumentException ();
1024
1025                 var ret_type = dlg.Method.ReturnType;
1026                 var param_types = dlg.Method.GetParameters ().Select (x => x.ParameterType).ToArray ();
1027
1028                 var dynamic = new DynamicMethod (Guid.NewGuid ().ToString (), ret_type, param_types, typeof (object), true);
1029                 var ig = dynamic.GetILGenerator ();
1030
1031                 LocalBuilder retval = null;
1032                 if (ret_type != typeof (void))
1033                         retval = ig.DeclareLocal (ret_type);
1034
1035                 var label = ig.BeginExceptionBlock ();
1036
1037                 for (int i = 0; i < param_types.Length; i++)
1038                         ig.Emit (OpCodes.Ldarg, i);
1039                 ig.Emit (OpCodes.Call, dlg.Method);
1040
1041                 if (retval != null)
1042                         ig.Emit (OpCodes.Stloc, retval);
1043
1044                 ig.Emit (OpCodes.Leave, label);
1045
1046                 ig.BeginExceptFilterBlock ();
1047
1048                 ig.Emit (OpCodes.Call, filter_method);
1049
1050                 ig.BeginCatchBlock (null);
1051
1052                 ig.Emit (OpCodes.Pop);
1053
1054                 ig.EndExceptionBlock ();
1055
1056                 if (retval != null)
1057                         ig.Emit (OpCodes.Ldloc, retval);
1058
1059                 ig.Emit (OpCodes.Ret);
1060
1061                 return dynamic.CreateDelegate (dlg.GetType ());
1062         }
1063
1064         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1065         static void exception_filter_method () {
1066                 throw new InvalidOperationException ();
1067         }
1068
1069         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1070         static int exception_filter_filter (Exception exc) {
1071                 return 1;
1072         }
1073
1074         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1075         public static void exception_filter () {
1076                 var method = typeof (Tests).GetMethod (
1077                         "exception_filter_method", BindingFlags.NonPublic | BindingFlags.Static);
1078                 var filter_method = typeof (Tests).GetMethod (
1079                         "exception_filter_filter", BindingFlags.NonPublic | BindingFlags.Static);
1080
1081                 var dlg = Delegate.CreateDelegate (typeof (Action), method);
1082
1083                 var wrapper = (Action) create_filter_delegate (dlg, filter_method);
1084
1085                 wrapper ();
1086         }
1087
1088         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1089         public static bool return_true () {
1090                 return true;
1091         }
1092
1093         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1094         public static void exceptions2 () {
1095                 if (return_true ())
1096                         throw new Exception ();
1097                 Console.WriteLine ();
1098         }
1099
1100         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1101         public static void threads () {
1102                 Thread t = new Thread (delegate () {});
1103
1104                 t.Start ();
1105                 t.Join ();
1106         }
1107
1108         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1109         public static void domains () {
1110                 AppDomain domain = AppDomain.CreateDomain ("domain");
1111
1112                 CrossDomain o = (CrossDomain)domain.CreateInstanceAndUnwrap (
1113                                    typeof (CrossDomain).Assembly.FullName, "CrossDomain");
1114
1115                 domains_2 (o, new CrossDomain ());
1116
1117                 o.invoke_2 ();
1118
1119                 o.invoke ();
1120
1121                 o.invoke_2 ();
1122
1123                 AppDomain.Unload (domain);
1124
1125                 domains_3 ();
1126         }
1127
1128         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1129         public static void domains_2 (object o, object o2) {
1130         }
1131
1132         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1133         public static void domains_3 () {
1134         }
1135
1136         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1137         public static void invoke_in_domain () {
1138         }
1139
1140         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1141         public static void invoke_in_domain_2 () {
1142         }
1143
1144         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1145         public static void dynamic_methods () {
1146                 var m = new DynamicMethod ("dyn_method", typeof (void), new Type []  { typeof (int) }, typeof (object).Module);
1147                 var ig = m.GetILGenerator ();
1148
1149                 ig.Emit (OpCodes.Ldstr, "FOO");
1150                 ig.Emit (OpCodes.Call, typeof (Tests).GetMethod ("dyn_call"));
1151                 ig.Emit (OpCodes.Ret);
1152
1153                 var del = (Action<int>)m.CreateDelegate (typeof (Action<int>));
1154
1155                 del (0);
1156         }
1157
1158         public static void dyn_call (string s) {
1159         }
1160
1161         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1162         public static void ref_emit () {
1163                 AssemblyName assemblyName = new AssemblyName ();
1164                 assemblyName.Name = "foo";
1165
1166                 AssemblyBuilder assembly =
1167                         Thread.GetDomain ().DefineDynamicAssembly (
1168                                                                                                            assemblyName, AssemblyBuilderAccess.RunAndSave);
1169
1170                 ModuleBuilder module = assembly.DefineDynamicModule ("foo.dll");
1171
1172                 TypeBuilder tb = module.DefineType ("foo", TypeAttributes.Public, typeof (object));
1173                 MethodBuilder mb = tb.DefineMethod ("ref_emit_method", MethodAttributes.Public|MethodAttributes.Static, CallingConventions.Standard, typeof (void), new Type [] { });
1174                 ILGenerator ig = mb.GetILGenerator ();
1175                 ig.Emit (OpCodes.Ldstr, "FOO");
1176                 ig.Emit (OpCodes.Call, typeof (Tests).GetMethod ("ref_emit_call"));
1177                 ig.Emit (OpCodes.Ret);
1178
1179                 Type t = tb.CreateType ();
1180
1181                 t.GetMethod ("ref_emit_method").Invoke (null, null);
1182         }
1183
1184         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1185         public static void ref_emit_call (string s) {
1186         }
1187
1188         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1189         public static void frames_in_native () {
1190                 Thread.Sleep (500);
1191                 var evt = new ManualResetEvent (false);
1192                 
1193                 object mon = new object ();
1194                 ThreadPool.QueueUserWorkItem (delegate {
1195                                 frames_in_native_2 ();
1196                                 evt.Set ();
1197                         });
1198                 evt.WaitOne ();
1199         }
1200
1201         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1202         static void frames_in_native_2 () {
1203                 frames_in_native_3 ();
1204         }
1205
1206         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1207         static void frames_in_native_3 () {
1208         }
1209
1210         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1211         public static void string_call (string s) {
1212         }
1213
1214         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1215         public static void ss_regress_654694 () {
1216                 if (true) {
1217                         string h = "hi";
1218                         string_call (h);
1219                 }
1220         }
1221
1222         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1223         public static void user () {
1224                 Debugger.Break ();
1225
1226                 Debugger.Log (5, Debugger.IsLogging () ? "A" : "", "B");
1227         }
1228
1229         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1230         public static void type_load () {
1231                 type_load_2 ();
1232         }
1233
1234         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1235         static void type_load_2 () {
1236                 var c1 = new Dictionary<int, int> ();
1237                 c1.ToString ();
1238                 var c = new TypeLoadClass ();
1239                 c.ToString ();
1240                 var c2 = new TypeLoadClass2 ();
1241                 c2.ToString ();
1242         }
1243
1244         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1245         public static void regress () {
1246                 regress_2755 (DateTime.Now);
1247         }
1248
1249         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1250         public static unsafe void regress_2755 (DateTime d) {
1251                 int* buffer = stackalloc int [128];
1252
1253                 regress_2755_2 ();
1254
1255                 int sum = 0;
1256                 for (int i = 0; i < 128; ++i)
1257                         sum += buffer [i];
1258
1259                 regress_2755_3 (sum);
1260         }
1261
1262         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1263         public static void regress_2755_2 () {
1264         }
1265
1266         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1267         public static void regress_2755_3 (int sum) {
1268         }
1269
1270         static object gc_suspend_field;
1271
1272         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1273         static unsafe void set_gc_suspend_field () {
1274                 set_gc_suspend_field_2 ();
1275                 // Clear stack
1276                 int* buffer = stackalloc int [4096];
1277         }
1278
1279         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1280         static void set_gc_suspend_field_2 () {
1281                 gc_suspend_field = new object ();
1282         }
1283
1284         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1285         static void gc_suspend_1 () {
1286         }
1287
1288         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1289         public static void gc_suspend_invoke () {
1290                 gc_suspend_field = null;
1291                 GC.Collect ();
1292                 GC.WaitForPendingFinalizers ();
1293         }
1294
1295         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1296         public static void gc_suspend () {
1297                 set_gc_suspend_field ();
1298                 gc_suspend_1 ();
1299         }
1300
1301         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1302         public static void generic_method<T> () where T : class {
1303         }
1304
1305         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1306         public void evaluate_method_2 () {
1307         }
1308
1309         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1310         public void evaluate_method () {
1311                 field_i = 42;
1312                 evaluate_method_2 ();
1313         }
1314
1315         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1316         static void set_ip_1 () {
1317         }
1318
1319         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1320         static void set_ip_2 () {
1321         }
1322
1323         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1324         public static void set_ip () {
1325                 int i, j;
1326
1327                 i = 1;
1328                 set_ip_1 ();
1329                 i = 5;
1330                 j = 5;
1331                 set_ip_2 ();
1332         }
1333 }
1334
1335 class TypeLoadClass {
1336 }
1337
1338 class TypeLoadClass2 {
1339 }
1340
1341 public class CrossDomain : MarshalByRefObject
1342 {
1343         public void invoke () {
1344                 Tests.invoke_in_domain ();
1345         }
1346
1347         public void invoke_2 () {
1348                 Tests.invoke_in_domain_2 ();
1349         }
1350
1351         public int invoke_3 () {
1352                 return 42;
1353         }
1354 }       
1355
1356 public class Foo
1357 {
1358         public ProcessStartInfo info;
1359 }
1360
1361 // Class used for line number info testing, don't change its layout
1362 public class LineNumbers
1363 {
1364         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1365         public static void ln1 () {
1366                 // Column 3
1367                 ln2 ();
1368                 ln3 ();
1369         }
1370
1371         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1372         public static void ln2 () {
1373         }
1374
1375         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1376         public static void ln3 () {
1377 #pragma warning disable 0219
1378                 int i = 5;
1379 #pragma warning restore 0219
1380                 #line 55 "FOO"
1381         }
1382 }