Merge pull request #1991 from esdrubal/seq_test_fix
[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 (), '\0'.ToString () + "A");
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, string s4) {
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 invoke3 () {
913         }
914
915         [MethodImplAttribute (MethodImplOptions.NoInlining)]
916         public void invoke_ex () {
917                 invoke_ex_inner ();
918         }
919
920         [MethodImplAttribute (MethodImplOptions.NoInlining)]
921         public void invoke_ex_inner () {
922                 try {
923                         throw new Exception ();
924                 } catch {
925                 }
926         }
927
928         int counter;
929
930         [MethodImplAttribute (MethodImplOptions.NoInlining)]
931         public void invoke_single_threaded () {
932                 // Spawn a thread incrementing a counter
933                 bool finished = false;
934
935                 new Thread (delegate () {
936                                 while (!finished)
937                                         counter ++;
938                 }).Start ();
939
940                 Thread.Sleep (100);
941
942                 invoke_single_threaded_2 ();
943
944                 finished = true;
945         }
946
947         [MethodImplAttribute (MethodImplOptions.NoInlining)]
948         public void invoke_single_threaded_2 () {
949         }
950
951         public void invoke_return_void () {
952         }
953
954         public string invoke_return_ref () {
955                 return "ABC";
956         }
957
958         public object invoke_return_null () {
959                 return null;
960         }
961
962         public int invoke_return_primitive () {
963                 return 42;
964         }
965
966         public int? invoke_return_nullable () {
967                 return 42;
968         }
969
970         public int? invoke_return_nullable_null () {
971                 return null;
972         }
973
974         public void invoke_type_load () {
975                 new Class3 ();
976         }
977
978         class Class3 {
979         }
980
981         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) {
982                 return ub + sb + ss + us + i + ui + l + (long)ul + (int)c + (b ? 1 : 0) + (int)f + (int)d;
983         }
984
985         public int invoke_pass_primitive2 (bool b) {
986                 return b ? 1 : 0;
987         }
988
989         public string invoke_pass_ref (string s) {
990                 return s;
991         }
992
993         public static string invoke_static_pass_ref (string s) {
994                 return s;
995         }
996
997         public static void invoke_static_return_void () {
998         }
999
1000         public static void invoke_throws () {
1001                 throw new Exception ();
1002         }
1003
1004         public int invoke_iface () {
1005                 return 42;
1006         }
1007
1008         public void invoke_out (out int foo, out int[] arr) {
1009                 foo = 5;
1010                 arr = new int [10];
1011         }
1012
1013         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1014         public static void exceptions () {
1015                 try {
1016                         throw new OverflowException ();
1017                 } catch (Exception) {
1018                 }
1019                 try {
1020                         throw new OverflowException ();
1021                 } catch (Exception) {
1022                 }
1023                 try {
1024                         throw new ArgumentException ();
1025                 } catch (Exception) {
1026                 }
1027                 try {
1028                         throw new OverflowException ();
1029                 } catch (Exception) {
1030                 }
1031                 // no subclasses
1032                 try {
1033                         throw new OverflowException ();
1034                 } catch (Exception) {
1035                 }
1036                 try {
1037                         throw new Exception ();
1038                 } catch (Exception) {
1039                 }
1040
1041                 object o = null;
1042                 try {
1043                         o.GetType ();
1044                 } catch (Exception) {
1045                 }
1046
1047                 try {
1048                         exceptions2 ();
1049                 } catch (Exception) {
1050                 }
1051         }
1052
1053         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1054         public static void unhandled_exception () {
1055                 ThreadPool.QueueUserWorkItem (delegate {
1056                                 throw new InvalidOperationException ();
1057                         });
1058                 Thread.Sleep (10000);
1059         }
1060
1061         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1062         public static void unhandled_exception_endinvoke_2 () {
1063         }
1064
1065         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1066         public static void unhandled_exception_endinvoke () {
1067                         Action action = new Action (() => 
1068                         {
1069                                 throw new Exception ("thrown");
1070                         });
1071                         action.BeginInvoke ((ar) => {
1072                                 try {
1073                                         action.EndInvoke (ar);
1074                                 } catch (Exception ex) {
1075                                         //Console.WriteLine (ex);
1076                                 }
1077                         }, null);
1078                 Thread.Sleep (1000);
1079                 unhandled_exception_endinvoke_2 ();
1080         }
1081
1082         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1083         public static void unhandled_exception_user () {
1084 #if NET_4_5
1085                 System.Threading.Tasks.Task.Factory.StartNew (() => {
1086                                 Throw ();
1087                         });
1088                 Thread.Sleep (10000);
1089 #endif
1090         }
1091
1092         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1093         public static void Throw () {
1094                 throw new Exception ();
1095         }
1096
1097         internal static Delegate create_filter_delegate (Delegate dlg, MethodInfo filter_method)
1098         {
1099                 if (dlg == null)
1100                         throw new ArgumentNullException ();
1101                 if (dlg.Target != null)
1102                         throw new ArgumentException ();
1103                 if (dlg.Method == null)
1104                         throw new ArgumentException ();
1105
1106                 var ret_type = dlg.Method.ReturnType;
1107                 var param_types = dlg.Method.GetParameters ().Select (x => x.ParameterType).ToArray ();
1108
1109                 var dynamic = new DynamicMethod (Guid.NewGuid ().ToString (), ret_type, param_types, typeof (object), true);
1110                 var ig = dynamic.GetILGenerator ();
1111
1112                 LocalBuilder retval = null;
1113                 if (ret_type != typeof (void))
1114                         retval = ig.DeclareLocal (ret_type);
1115
1116                 var label = ig.BeginExceptionBlock ();
1117
1118                 for (int i = 0; i < param_types.Length; i++)
1119                         ig.Emit (OpCodes.Ldarg, i);
1120                 ig.Emit (OpCodes.Call, dlg.Method);
1121
1122                 if (retval != null)
1123                         ig.Emit (OpCodes.Stloc, retval);
1124
1125                 ig.Emit (OpCodes.Leave, label);
1126
1127                 ig.BeginExceptFilterBlock ();
1128
1129                 ig.Emit (OpCodes.Call, filter_method);
1130
1131                 ig.BeginCatchBlock (null);
1132
1133                 ig.Emit (OpCodes.Pop);
1134
1135                 ig.EndExceptionBlock ();
1136
1137                 if (retval != null)
1138                         ig.Emit (OpCodes.Ldloc, retval);
1139
1140                 ig.Emit (OpCodes.Ret);
1141
1142                 return dynamic.CreateDelegate (dlg.GetType ());
1143         }
1144
1145         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1146         static void exception_filter_method () {
1147                 throw new InvalidOperationException ();
1148         }
1149
1150         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1151         static int exception_filter_filter (Exception exc) {
1152                 return 1;
1153         }
1154
1155         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1156         public static void exception_filter () {
1157                 var method = typeof (Tests).GetMethod (
1158                         "exception_filter_method", BindingFlags.NonPublic | BindingFlags.Static);
1159                 var filter_method = typeof (Tests).GetMethod (
1160                         "exception_filter_filter", BindingFlags.NonPublic | BindingFlags.Static);
1161
1162                 var dlg = Delegate.CreateDelegate (typeof (Action), method);
1163
1164                 var wrapper = (Action) create_filter_delegate (dlg, filter_method);
1165
1166                 wrapper ();
1167         }
1168
1169         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1170         public static bool return_true () {
1171                 return true;
1172         }
1173
1174         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1175         public static void exceptions2 () {
1176                 if (return_true ())
1177                         throw new Exception ();
1178                 Console.WriteLine ();
1179         }
1180
1181         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1182         public static void threads () {
1183                 Thread t = new Thread (delegate () {});
1184
1185                 t.Start ();
1186                 t.Join ();
1187         }
1188
1189         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1190         public static void domains () {
1191                 AppDomain domain = AppDomain.CreateDomain ("domain");
1192
1193                 CrossDomain o = (CrossDomain)domain.CreateInstanceAndUnwrap (
1194                                    typeof (CrossDomain).Assembly.FullName, "CrossDomain");
1195
1196                 domains_2 (o, new CrossDomain ());
1197
1198                 o.invoke_2 ();
1199
1200                 o.invoke ();
1201
1202                 o.invoke_2 ();
1203
1204                 AppDomain.Unload (domain);
1205
1206                 domains_3 ();
1207
1208                 typeof (Tests).GetMethod ("called_from_invoke").Invoke (null, null);
1209         }
1210
1211         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1212         public static void called_from_invoke () {
1213         }
1214
1215         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1216         public static void domains_2 (object o, object o2) {
1217         }
1218
1219         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1220         public static void domains_3 () {
1221         }
1222
1223         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1224         public static void invoke_in_domain () {
1225         }
1226
1227         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1228         public static void invoke_in_domain_2 () {
1229         }
1230
1231         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1232         public static void dynamic_methods () {
1233                 var m = new DynamicMethod ("dyn_method", typeof (void), new Type []  { typeof (int) }, typeof (object).Module);
1234                 var ig = m.GetILGenerator ();
1235
1236                 ig.Emit (OpCodes.Ldstr, "FOO");
1237                 ig.Emit (OpCodes.Call, typeof (Tests).GetMethod ("dyn_call"));
1238                 ig.Emit (OpCodes.Ret);
1239
1240                 var del = (Action<int>)m.CreateDelegate (typeof (Action<int>));
1241
1242                 del (0);
1243         }
1244
1245         public static void dyn_call (string s) {
1246         }
1247
1248         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1249         public static void ref_emit () {
1250                 AssemblyName assemblyName = new AssemblyName ();
1251                 assemblyName.Name = "foo";
1252
1253                 AssemblyBuilder assembly =
1254                         Thread.GetDomain ().DefineDynamicAssembly (
1255                                                                                                            assemblyName, AssemblyBuilderAccess.RunAndSave);
1256
1257                 ModuleBuilder module = assembly.DefineDynamicModule ("foo.dll");
1258
1259                 TypeBuilder tb = module.DefineType ("foo", TypeAttributes.Public, typeof (object));
1260                 MethodBuilder mb = tb.DefineMethod ("ref_emit_method", MethodAttributes.Public|MethodAttributes.Static, CallingConventions.Standard, typeof (void), new Type [] { });
1261                 ILGenerator ig = mb.GetILGenerator ();
1262                 ig.Emit (OpCodes.Ldstr, "FOO");
1263                 ig.Emit (OpCodes.Call, typeof (Tests).GetMethod ("ref_emit_call"));
1264                 ig.Emit (OpCodes.Ret);
1265
1266                 Type t = tb.CreateType ();
1267
1268                 t.GetMethod ("ref_emit_method").Invoke (null, null);
1269         }
1270
1271         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1272         public static void ref_emit_call (string s) {
1273         }
1274
1275         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1276         public static void frames_in_native () {
1277                 Thread.Sleep (500);
1278                 var evt = new ManualResetEvent (false);
1279                 
1280                 object mon = new object ();
1281                 ThreadPool.QueueUserWorkItem (delegate {
1282                                 frames_in_native_2 ();
1283                                 evt.Set ();
1284                         });
1285                 evt.WaitOne ();
1286         }
1287
1288         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1289         static void frames_in_native_2 () {
1290                 frames_in_native_3 ();
1291         }
1292
1293         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1294         static void frames_in_native_3 () {
1295         }
1296
1297         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1298         public static void string_call (string s) {
1299         }
1300
1301         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1302         public static void ss_regress_654694 () {
1303                 if (true) {
1304                         string h = "hi";
1305                         string_call (h);
1306                 }
1307         }
1308
1309         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1310         public static void user () {
1311                 Debugger.Break ();
1312
1313                 Debugger.Log (5, Debugger.IsLogging () ? "A" : "", "B");
1314         }
1315
1316         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1317         public static void type_load () {
1318                 type_load_2 ();
1319         }
1320
1321         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1322         static void type_load_2 () {
1323                 var c1 = new Dictionary<int, int> ();
1324                 c1.ToString ();
1325                 var c = new TypeLoadClass ();
1326                 c.ToString ();
1327                 var c2 = new TypeLoadClass2 ();
1328                 c2.ToString ();
1329         }
1330
1331         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1332         public static void regress () {
1333                 regress_2755 (DateTime.Now);
1334         }
1335
1336         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1337         public static unsafe void regress_2755 (DateTime d) {
1338                 int* buffer = stackalloc int [128];
1339
1340                 regress_2755_2 ();
1341
1342                 int sum = 0;
1343                 for (int i = 0; i < 128; ++i)
1344                         sum += buffer [i];
1345
1346                 regress_2755_3 (sum);
1347         }
1348
1349         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1350         public static void regress_2755_2 () {
1351         }
1352
1353         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1354         public static void regress_2755_3 (int sum) {
1355         }
1356
1357         static object gc_suspend_field;
1358
1359         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1360         static unsafe void set_gc_suspend_field () {
1361                 set_gc_suspend_field_2 ();
1362                 // Clear stack
1363                 int* buffer = stackalloc int [4096];
1364         }
1365
1366         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1367         static void set_gc_suspend_field_2 () {
1368                 gc_suspend_field = new object ();
1369         }
1370
1371         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1372         static void gc_suspend_1 () {
1373         }
1374
1375         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1376         public static void gc_suspend_invoke () {
1377                 gc_suspend_field = null;
1378                 GC.Collect ();
1379                 GC.WaitForPendingFinalizers ();
1380         }
1381
1382         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1383         public static void gc_suspend () {
1384                 set_gc_suspend_field ();
1385                 gc_suspend_1 ();
1386         }
1387
1388         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1389         public static void generic_method<T> () where T : class {
1390         }
1391
1392         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1393         public void evaluate_method_2 () {
1394         }
1395
1396         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1397         public void evaluate_method () {
1398                 field_i = 42;
1399                 evaluate_method_2 ();
1400         }
1401
1402         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1403         static void set_ip_1 () {
1404         }
1405
1406         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1407         static void set_ip_2 () {
1408         }
1409
1410         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1411         public static void set_ip () {
1412                 int i = 0, j;
1413
1414                 i ++;
1415                 i ++;
1416                 set_ip_1 ();
1417                 i ++;
1418                 j = 5;
1419                 set_ip_2 ();
1420         }
1421
1422         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1423         public static void step_filters () {
1424                 ClassWithCctor.cctor_filter ();
1425         }
1426
1427         class ClassWithCctor {
1428                 [MethodImplAttribute (MethodImplOptions.NoInlining)]
1429                 static ClassWithCctor () {
1430                         int i = 1;
1431                         int j = 2;
1432                 }
1433
1434                 [MethodImplAttribute (MethodImplOptions.NoInlining)]
1435                 public static void cctor_filter () {
1436                 }
1437         }
1438
1439         public override string virtual_method () {
1440                 return "V2";
1441         }
1442 }
1443
1444 class TypeLoadClass {
1445 }
1446
1447 class TypeLoadClass2 {
1448 }
1449
1450 public class CrossDomain : MarshalByRefObject
1451 {
1452         public void invoke () {
1453                 Tests.invoke_in_domain ();
1454         }
1455
1456         public void invoke_2 () {
1457                 Tests.invoke_in_domain_2 ();
1458         }
1459
1460         public int invoke_3 () {
1461                 return 42;
1462         }
1463 }       
1464
1465 public class Foo
1466 {
1467         public ProcessStartInfo info;
1468 }
1469
1470 // Class used for line number info testing, don't change its layout
1471 public class LineNumbers
1472 {
1473         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1474         public static void ln1 () {
1475                 // Column 3
1476                 ln2 ();
1477                 ln3 ();
1478         }
1479
1480         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1481         public static void ln2 () {
1482         }
1483
1484         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1485         public static void ln3 () {
1486 #pragma warning disable 0219
1487                 int i = 5;
1488 #pragma warning restore 0219
1489                 #line 55 "FOO"
1490         }
1491 }
1492
1493 class LocalReflectClass
1494 {
1495         public static void RunMe ()
1496         {
1497                 var reflectMe = new someClass ();
1498                 reflectMe.someMethod ();
1499         }
1500
1501         class someClass : ContextBoundObject
1502         {
1503                 public object someField;
1504
1505                 public void someMethod ()
1506                 {
1507                 }
1508         }
1509 }
1510
1511