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