Fix setting of byref variables in sdb. Fixes #4362.
[mono.git] / mcs / class / Mono.Debugger.Soft / Test / dtest-app.cs
1 /*
2  * dtest-app.cs:
3  *
4  *   Application program used by the debugger tests.
5  */
6 using System;
7 using System.Runtime.CompilerServices;
8 using System.Reflection;
9 using System.Reflection.Emit;
10 using System.Diagnostics;
11 using System.Threading;
12 using System.Collections.Generic;
13 using System.Linq;
14
15 public class TestsBase
16 {
17 #pragma warning disable 0414
18 #pragma warning disable 0169
19         public int base_field_i;
20         public string base_field_s;
21         static int base_static_i = 57;
22         static string base_static_s = "C";
23 #pragma warning restore 0414
24 #pragma warning restore 0169
25 }
26
27 public enum AnEnum {
28         A = 0,
29         B= 1
30 }
31
32 public sealed class Tests3 {
33         public static void M1 () {
34         }
35
36         static void M2 () {
37         }
38
39         public void M3 () {
40         }
41
42         void M4 () {
43         }
44
45 }
46
47 [DebuggerDisplay ("Tests", Name="FOO", Target=typeof (int))]
48 [DebuggerTypeProxy (typeof (Tests))]
49 public class Tests2 {
50         [DebuggerBrowsableAttribute (DebuggerBrowsableState.Collapsed)]
51         public int field_j;
52         public static int static_field_j;
53
54         [DebuggerBrowsableAttribute (DebuggerBrowsableState.Collapsed)]
55         public int AProperty {
56                 get {
57                         return 0;
58                 }
59         }
60
61         public void invoke () {
62         }
63 }
64
65 public struct AStruct {
66         public int i;
67         public string s;
68         public byte k;
69         public IntPtr j;
70
71         [MethodImplAttribute (MethodImplOptions.NoInlining)]
72         public int foo (int val) {
73                 return val;
74         }
75
76         [MethodImplAttribute (MethodImplOptions.NoInlining)]
77         public static int static_foo (int val) {
78                 return val;
79         }
80
81         [MethodImplAttribute (MethodImplOptions.NoInlining)]
82         public int invoke_return_int () {
83                 return i;
84         }
85
86         [MethodImplAttribute (MethodImplOptions.NoInlining)]
87         public static int invoke_static () {
88                 return 5;
89         }
90
91         [MethodImplAttribute (MethodImplOptions.NoInlining)]
92         public IntPtr invoke_return_intptr () {
93                 return j;
94         }
95 }
96
97 public class GClass<T> {
98         public T field;
99         public static T static_field;
100
101         [MethodImplAttribute (MethodImplOptions.NoInlining)]
102         public GClass () {
103         }
104
105         [MethodImplAttribute (MethodImplOptions.NoInlining)]
106         public void bp<T2> () {
107         }
108 }
109
110 public struct GStruct<T> {
111         public T i;
112
113         public int j;
114
115         [MethodImplAttribute (MethodImplOptions.NoInlining)]
116         public int invoke_return_int () {
117                 return j;
118         }
119 }
120
121 interface ITest
122 {
123         void Foo ();
124         void Bar ();
125 }
126
127 interface ITest<T>
128 {
129         void Foo ();
130         void Bar ();
131 }
132
133 class TestIfaces : ITest
134 {
135         void ITest.Foo () {
136         }
137
138         void ITest.Bar () {
139         }
140
141         TestIfaces<int> Baz () {
142                 return null;
143         }
144 }
145
146 class TestIfaces<T> : ITest<T>
147 {
148         void ITest<T>.Foo () {
149         }
150
151         void ITest<T>.Bar () {
152         }
153 }
154
155 public class Tests : TestsBase
156 {
157 #pragma warning disable 0414
158         int field_i;
159         string field_s;
160         AnEnum field_enum;
161         bool field_bool1, field_bool2;
162         char field_char;
163         byte field_byte;
164         sbyte field_sbyte;
165         short field_short;
166         ushort field_ushort;
167         long field_long;
168         ulong field_ulong;
169         float field_float;
170         double field_double;
171         Thread field_class;
172         IntPtr field_intptr;
173         static int static_i = 55;
174         static string static_s = "A";
175         public const int literal_i = 56;
176         public const string literal_s = "B";
177         public object child;
178         public AStruct field_struct;
179         public object field_boxed_struct;
180         public GStruct<int> generic_field_struct;
181         [ThreadStatic]
182         public static int tls_i;
183         public static bool is_attached = Debugger.IsAttached;
184
185 #pragma warning restore 0414
186
187         public class NestedClass {
188         }
189
190         public int IntProperty {
191                 get {
192                         return field_i;
193                 }
194                 set {
195                         field_i = value;
196                 }
197         }
198
199         public int ReadOnlyProperty {
200                 get {
201                         return field_i;
202                 }
203         }
204
205         public int this [int index] {
206                 get {
207                         return field_i;
208                 }
209         }
210
211         public static int Main (String[] args) {
212                 tls_i = 42;
213
214                 if (args.Length > 0 && args [0] == "suspend-test")
215                         /* This contains an infinite loop, so execute it conditionally */
216                         suspend ();
217                 breakpoints ();
218                 single_stepping ();
219                 arguments ();
220                 objects ();
221                 objrefs ();
222                 vtypes ();
223                 locals ();
224                 line_numbers ();
225                 type_info ();
226                 assembly_load ();
227                 invoke ();
228                 exceptions ();
229                 exception_filter ();
230                 threads ();
231                 dynamic_methods ();
232                 user ();
233                 type_load ();
234                 regress ();
235                 if (args.Length > 0 && args [0] == "domain-test")
236                         /* This takes a lot of time, so execute it conditionally */
237                         domains ();
238                 if (args.Length > 0 && args [0] == "ref-emit-test")
239                         ref_emit ();
240                 if (args.Length > 0 && args [0] == "frames-in-native")
241                         frames_in_native ();
242                 if (args.Length >0 && args [0] == "invoke-single-threaded")
243                         new Tests ().invoke_single_threaded ();
244                 return 3;
245         }
246
247         public static void breakpoints () {
248                 /* Call these early so it is JITted by the time a breakpoint is placed on it */
249                 bp3 ();
250                 bp7<int> ();
251                 bp7<string> ();
252
253                 bp1 ();
254                 bp2 ();
255                 bp3 ();
256                 bp4 ();
257                 bp4 ();
258                 bp4 ();
259                 bp5 ();
260                 bp6<string> (new GClass <int> ());
261                 bp7<int> ();
262                 bp7<string> ();
263         }
264
265         [MethodImplAttribute (MethodImplOptions.NoInlining)]
266         public static void bp1 () {
267         }
268
269         [MethodImplAttribute (MethodImplOptions.NoInlining)]
270         public static void bp2 () {
271         }
272
273         [MethodImplAttribute (MethodImplOptions.NoInlining)]
274         public static void bp3 () {
275         }
276
277         [MethodImplAttribute (MethodImplOptions.NoInlining)]
278         public static void bp4 () {
279         }
280
281         [MethodImplAttribute (MethodImplOptions.NoInlining)]
282         public static void bp5 () {
283         }
284
285         [MethodImplAttribute (MethodImplOptions.NoInlining)]
286         public static void bp6<T> (GClass<int> gc) {
287                 gc.bp<int> ();
288         }
289
290         [MethodImplAttribute (MethodImplOptions.NoInlining)]
291         public static void bp7<T> () {
292         }
293
294         [MethodImplAttribute (MethodImplOptions.NoInlining)]
295         public static void single_stepping () {
296                 bool b = true;
297                 ss1 ();
298                 ss2 ();
299                 ss3 ();
300                 ss3_2 ();
301                 ss4 ();
302                 ss5 (new int [] { 1, 2, 3 }, new Func<int, bool> (is_even));
303                 try {
304                         ss6 (b);
305                 } catch {
306                 }
307                 ss_regress_654694 ();
308         }
309
310         [MethodImplAttribute (MethodImplOptions.NoInlining)]
311         public static void ss1 () {
312         }
313
314         [MethodImplAttribute (MethodImplOptions.NoInlining)]
315         public static void ss2 () {
316         }
317
318         [MethodImplAttribute (MethodImplOptions.NoInlining)]
319         public static int ss3 () {
320                 int sum = 0;
321
322                 for (int i = 0; i < 10; ++i)
323                         sum += i;
324
325                 return sum;
326         }
327
328         [MethodImplAttribute (MethodImplOptions.NoInlining)]
329         public static void ss3_2 () {
330                 ss3_2_2 ();
331         }
332
333         [MethodImplAttribute (MethodImplOptions.NoInlining)]
334         public static void ss3_2_2 () {
335         }
336
337         [MethodImplAttribute (MethodImplOptions.NoInlining)]
338         public static int ss4 () {
339                 ss1 (); ss1 ();
340                 ss2 ();
341                 return 0;
342         }
343
344         [MethodImplAttribute (MethodImplOptions.NoInlining)]
345         public static void ss5 (int[] arr, Func<int, bool> selector) {
346                 // Call into linq which calls back into this assembly
347                 arr.Count (selector);
348         }
349
350         [MethodImplAttribute (MethodImplOptions.NoInlining)]
351         public static void ss6 (bool b) {
352                 if (b) {
353                         ss7 ();
354                         throw new Exception ();
355                 }
356         }
357
358         [MethodImplAttribute (MethodImplOptions.NoInlining)]
359         public static void ss7 () {
360         }
361
362         [MethodImplAttribute (MethodImplOptions.NoInlining)]
363         public static bool is_even (int i) {
364                 return i % 2 == 0;
365         }
366
367         /*
368                 lock (static_s) {
369                         Console.WriteLine ("HIT!");
370                 }
371                 return 0;
372         }
373         */
374
375         [MethodImplAttribute (MethodImplOptions.NoInlining)]
376         public static void arguments () {
377                 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));
378                 int i = 42;
379                 arg2 ("FOO", null, "BLA", ref i, new GClass <int> { field = 42 }, new object ());
380                 Tests t = new Tests () { field_i = 42, field_s = "S" };
381                 t.arg3 ("BLA");
382         }
383
384         [MethodImplAttribute (MethodImplOptions.NoInlining)]
385         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) {
386                 return (int)(sb + b + (bl ? 0 : 1) + s + us + (int)c + i + ui + l + (long)ul + f + d + (int)ip + (int)uip);
387         }
388
389         [MethodImplAttribute (MethodImplOptions.NoInlining)]
390         public static string arg2 (string s, string s3, object o, ref int i, GClass <int> gc, object o2) {
391                 return s + (s3 != null ? "" : "") + o + i + gc.field + o2;
392         }
393
394         [MethodImplAttribute (MethodImplOptions.NoInlining)]
395         public object arg3 (string s) {
396                 return s + s + s + s + this;
397         }
398
399         [MethodImplAttribute (MethodImplOptions.NoInlining)]
400         public static void objects () {
401                 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) };
402                 t.o1 (new Tests2 () { field_j = 43 }, new GClass <int> { field = 42 }, new GClass <string> { field = "FOO" });
403                 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 });
404         }
405
406         [MethodImplAttribute (MethodImplOptions.NoInlining)]
407         public object o1 (Tests2 t, GClass <int> gc1, GClass <string> gc2) {
408                 if (t == null || gc1 == null || gc2 == null)
409                         return null;
410                 else
411                         return this;
412         }
413
414         [MethodImplAttribute (MethodImplOptions.NoInlining)]
415         public static string o2 (string[] s2, int[] s3, int[,] s4, int[,] s5, IList<int> s6) {
416                 return s2 [0] + s3 [0] + s4 [0, 0] + s6 [0];
417         }
418
419         [MethodImplAttribute (MethodImplOptions.NoInlining)]
420         public static void objrefs () {
421                 Tests t = new Tests () {};
422                 set_child (t);
423                 t.objrefs1 ();
424                 t.child = null;
425                 GC.Collect ();
426                 objrefs2 ();
427         }
428
429         [MethodImplAttribute (MethodImplOptions.NoInlining)]
430         public static void set_child (Tests t) {
431                 t.child = new Tests ();
432         }
433
434         [MethodImplAttribute (MethodImplOptions.NoInlining)]
435         public void objrefs1 () {
436         }
437
438         [MethodImplAttribute (MethodImplOptions.NoInlining)]
439         public static void objrefs2 () {
440         }
441
442         public static void vtypes () {
443                 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 }};
444                 AStruct s = new AStruct { i = 44, s = "T", k = 45 };
445                 AStruct[] arr = new AStruct[] { 
446                         new AStruct () { i = 1, s = "S1" },
447                         new AStruct () { i = 2, s = "S2" } };
448                 t.vtypes1 (s, arr);
449                 vtypes2 (s);
450                 vtypes3 (s);
451         }
452
453         [MethodImplAttribute (MethodImplOptions.NoInlining)]
454         public object vtypes1 (AStruct s, AStruct[] arr) {
455                 if (arr != null)
456                         return this;
457                 else
458                         return null;
459         }
460
461         [MethodImplAttribute (MethodImplOptions.NoInlining)]
462         public static void vtypes2 (AStruct s) {
463                 s.foo (5);
464         }
465
466         [MethodImplAttribute (MethodImplOptions.NoInlining)]
467         public static void vtypes3 (AStruct s) {
468                 AStruct.static_foo (5);
469         }
470
471         [MethodImplAttribute (MethodImplOptions.NoInlining)]
472         public static void locals () {
473                 string s = null;
474                 locals1 (null);
475                 locals2<string> (null, 5, "ABC", ref s);
476                 locals3 ();
477                 locals6 ();
478         }
479
480         [MethodImplAttribute (MethodImplOptions.NoInlining)]
481         public static void locals1 (string[] args) {
482                 long foo = 42;
483
484                 for (int j = 0; j < 10; ++j) {
485                         foo ++;
486                 }
487         }
488
489         [MethodImplAttribute (MethodImplOptions.NoInlining)]
490         public static void locals2<T> (string[] args, int arg, T t, ref string rs) {
491                 long i = 42;
492                 string s = "AB";
493
494                 for (int j = 0; j < 10; ++j) {
495                         if (s != null)
496                                 i ++;
497                         if (t != null)
498                                 i ++;
499                 }
500                 rs = "A";
501         }
502
503         [MethodImplAttribute (MethodImplOptions.NoInlining)]
504         public static void locals3 () {
505                 string s = "B";
506                 s.ToString ();
507
508                 {
509                         long i = 42;
510                         i ++;
511                         locals4 ();
512                 }
513                 {
514                         string i = "A";
515                         i.ToString ();
516                         locals5 ();
517                 }
518                 {
519                         long j = 42;
520                         j ++;
521                 }
522         }
523
524         [MethodImplAttribute (MethodImplOptions.NoInlining)]
525         public static void locals4 () {
526         }
527
528         [MethodImplAttribute (MethodImplOptions.NoInlining)]
529         public static void locals5 () {
530         }
531
532         [MethodImplAttribute (MethodImplOptions.NoInlining)]
533         public static void locals6 () {
534                 int i = 0;
535                 int j = 0;
536                 for (i = 0; i < 10; ++i)
537                         j ++;
538                 sbyte sb = 0;
539                 for (i = 0; i < 10; ++i)
540                         sb ++;
541                 locals6_1 ();
542                 locals6_2 (j);
543                 locals6_3 ();
544                 locals6_4 (j);
545                 locals6_5 ();
546                 locals6_6 (sb);
547         }
548
549         [MethodImplAttribute (MethodImplOptions.NoInlining)]
550         public static void locals6_1 () {
551         }
552
553         [MethodImplAttribute (MethodImplOptions.NoInlining)]
554         public static void locals6_2 (int arg) {
555         }
556
557         [MethodImplAttribute (MethodImplOptions.NoInlining)]
558         public static void locals6_3 () {
559                 // Clobber all registers
560                 int sum = 0, i, j, k, l, m;
561                 for (i = 0; i < 100; ++i)
562                         sum ++;
563                 for (j = 0; j < 100; ++j)
564                         sum ++;
565                 for (k = 0; k < 100; ++k)
566                         sum ++;
567                 for (l = 0; l < 100; ++l)
568                         sum ++;
569                 for (m = 0; m < 100; ++m)
570                         sum ++;
571         }
572
573         [MethodImplAttribute (MethodImplOptions.NoInlining)]
574         public static void locals6_4 (int arg) {
575         }
576
577         [MethodImplAttribute (MethodImplOptions.NoInlining)]
578         public static void locals6_5 () {
579         }
580
581         [MethodImplAttribute (MethodImplOptions.NoInlining)]
582         public static void locals6_6 (int arg) {
583         }
584
585         [MethodImplAttribute (MethodImplOptions.NoInlining)]
586         public static void line_numbers () {
587                 LineNumbers.ln1 ();
588         }
589
590         [MethodImplAttribute (MethodImplOptions.NoInlining)]
591         public static void suspend () {
592                 long i = 5;
593
594                 while (true) {
595                         i ++;
596                 }
597         }
598
599         [MethodImplAttribute (MethodImplOptions.NoInlining)]
600         public static void type_info () {
601                 Tests t = new Tests () { field_i = 42, field_s = "S", base_field_i = 43, base_field_s = "T", field_enum = AnEnum.B };
602                 t.ti1 (new Tests2 () { field_j = 43 }, new GClass <int> { field = 42 }, new GClass <string> { field = "FOO" });
603                 int val = 0;
604                 unsafe {
605                         AStruct s = new AStruct () { i = 42, s = "S", k = 43 };
606
607                         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);
608                 }
609         }
610
611         [MethodImplAttribute (MethodImplOptions.NoInlining)]
612         public object ti1 (Tests2 t, GClass <int> gc1, GClass <string> gc2) {
613                 if (t == null || gc1 == null || gc2 == null)
614                         return null;
615                 else
616                         return this;
617         }
618
619         [MethodImplAttribute (MethodImplOptions.NoInlining)]
620         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) {
621                 return s2 [0] + s3 [0] + s4 [0, 0];
622         }
623
624         [MethodImplAttribute (MethodImplOptions.NoInlining)]
625         public static void assembly_load () {
626                 assembly_load_2 ();
627         }
628
629         [MethodImplAttribute (MethodImplOptions.NoInlining)]
630         public static void assembly_load_2 () {
631                 // This will load System.dll while holding the loader lock
632                 new Foo ();
633         }
634
635         [MethodImplAttribute (MethodImplOptions.NoInlining)]
636         public static void invoke () {
637                 new Tests ().invoke1 (new Tests2 (), new AStruct () { i = 42, j = (IntPtr)43 }, new GStruct<int> { j = 42 });
638                 new Tests ().invoke_ex ();
639         }
640
641         [MethodImplAttribute (MethodImplOptions.NoInlining)]
642         public void invoke1 (Tests2 t, AStruct s, GStruct<int> g) {
643                 invoke2 ();
644         }
645
646         [MethodImplAttribute (MethodImplOptions.NoInlining)]
647         public void invoke2 () {
648         }
649
650         [MethodImplAttribute (MethodImplOptions.NoInlining)]
651         public void invoke_ex () {
652                 invoke_ex_inner ();
653         }
654
655         [MethodImplAttribute (MethodImplOptions.NoInlining)]
656         public void invoke_ex_inner () {
657                 try {
658                         throw new Exception ();
659                 } catch {
660                 }
661         }
662
663         int counter;
664
665         [MethodImplAttribute (MethodImplOptions.NoInlining)]
666         public void invoke_single_threaded () {
667                 // Spawn a thread incrementing a counter
668                 bool finished = false;
669
670                 new Thread (delegate () {
671                                 while (!finished)
672                                         counter ++;
673                 }).Start ();
674
675                 Thread.Sleep (100);
676
677                 invoke_single_threaded_2 ();
678
679                 finished = true;
680         }
681
682         [MethodImplAttribute (MethodImplOptions.NoInlining)]
683         public void invoke_single_threaded_2 () {
684         }
685
686         public void invoke_return_void () {
687         }
688
689         public string invoke_return_ref () {
690                 return "ABC";
691         }
692
693         public object invoke_return_null () {
694                 return null;
695         }
696
697         public int invoke_return_primitive () {
698                 return 42;
699         }
700
701         public int? invoke_return_nullable () {
702                 return 42;
703         }
704
705         public int? invoke_return_nullable_null () {
706                 return null;
707         }
708
709         public void invoke_type_load () {
710                 new Class3 ();
711         }
712
713         class Class3 {
714         }
715
716         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) {
717                 return ub + sb + ss + us + i + ui + l + (long)ul + (int)c + (b ? 1 : 0) + (int)f + (int)d;
718         }
719
720         public int invoke_pass_primitive2 (bool b) {
721                 return b ? 1 : 0;
722         }
723
724         public string invoke_pass_ref (string s) {
725                 return s;
726         }
727
728         public static string invoke_static_pass_ref (string s) {
729                 return s;
730         }
731
732         public static void invoke_static_return_void () {
733         }
734
735         public static void invoke_throws () {
736                 throw new Exception ();
737         }
738
739         [MethodImplAttribute (MethodImplOptions.NoInlining)]
740         public static void exceptions () {
741                 try {
742                         throw new OverflowException ();
743                 } catch (Exception) {
744                 }
745                 try {
746                         throw new OverflowException ();
747                 } catch (Exception) {
748                 }
749                 try {
750                         throw new ArgumentException ();
751                 } catch (Exception) {
752                 }
753                 try {
754                         throw new OverflowException ();
755                 } catch (Exception) {
756                 }
757
758                 object o = null;
759                 try {
760                         o.GetType ();
761                 } catch (Exception) {
762                 }
763
764                 try {
765                         exceptions2 ();
766                 } catch (Exception) {
767                 }
768         }
769
770         internal static Delegate create_filter_delegate (Delegate dlg, MethodInfo filter_method)
771         {
772                 if (dlg == null)
773                         throw new ArgumentNullException ();
774                 if (dlg.Target != null)
775                         throw new ArgumentException ();
776                 if (dlg.Method == null)
777                         throw new ArgumentException ();
778
779                 var ret_type = dlg.Method.ReturnType;
780                 var param_types = dlg.Method.GetParameters ().Select (x => x.ParameterType).ToArray ();
781
782                 var dynamic = new DynamicMethod (Guid.NewGuid ().ToString (), ret_type, param_types, typeof (object), true);
783                 var ig = dynamic.GetILGenerator ();
784
785                 LocalBuilder retval = null;
786                 if (ret_type != typeof (void))
787                         retval = ig.DeclareLocal (ret_type);
788
789                 var label = ig.BeginExceptionBlock ();
790
791                 for (int i = 0; i < param_types.Length; i++)
792                         ig.Emit (OpCodes.Ldarg, i);
793                 ig.Emit (OpCodes.Call, dlg.Method);
794
795                 if (retval != null)
796                         ig.Emit (OpCodes.Stloc, retval);
797
798                 ig.Emit (OpCodes.Leave, label);
799
800                 ig.BeginExceptFilterBlock ();
801
802                 ig.Emit (OpCodes.Call, filter_method);
803
804                 ig.BeginCatchBlock (null);
805
806                 ig.Emit (OpCodes.Pop);
807
808                 ig.EndExceptionBlock ();
809
810                 if (retval != null)
811                         ig.Emit (OpCodes.Ldloc, retval);
812
813                 ig.Emit (OpCodes.Ret);
814
815                 return dynamic.CreateDelegate (dlg.GetType ());
816         }
817
818         [MethodImplAttribute (MethodImplOptions.NoInlining)]
819         static void exception_filter_method () {
820                 throw new InvalidOperationException ();
821         }
822
823         [MethodImplAttribute (MethodImplOptions.NoInlining)]
824         static int exception_filter_filter (Exception exc) {
825                 return 1;
826         }
827
828         [MethodImplAttribute (MethodImplOptions.NoInlining)]
829         public static void exception_filter () {
830                 var method = typeof (Tests).GetMethod (
831                         "exception_filter_method", BindingFlags.NonPublic | BindingFlags.Static);
832                 var filter_method = typeof (Tests).GetMethod (
833                         "exception_filter_filter", BindingFlags.NonPublic | BindingFlags.Static);
834
835                 var dlg = Delegate.CreateDelegate (typeof (Action), method);
836
837                 var wrapper = (Action) create_filter_delegate (dlg, filter_method);
838
839                 wrapper ();
840         }
841
842         [MethodImplAttribute (MethodImplOptions.NoInlining)]
843         public static bool return_true () {
844                 return true;
845         }
846
847         [MethodImplAttribute (MethodImplOptions.NoInlining)]
848         public static void exceptions2 () {
849                 if (return_true ())
850                         throw new Exception ();
851                 Console.WriteLine ();
852         }
853
854         [MethodImplAttribute (MethodImplOptions.NoInlining)]
855         public static void threads () {
856                 Thread t = new Thread (delegate () {});
857
858                 t.Start ();
859                 t.Join ();
860         }
861
862         [MethodImplAttribute (MethodImplOptions.NoInlining)]
863         public static void domains () {
864                 AppDomain domain = AppDomain.CreateDomain ("domain");
865
866                 CrossDomain o = (CrossDomain)domain.CreateInstanceAndUnwrap (
867                                    typeof (CrossDomain).Assembly.FullName, "CrossDomain");
868
869                 o.invoke_2 ();
870
871                 o.invoke ();
872
873                 o.invoke_2 ();
874
875                 AppDomain.Unload (domain);
876
877                 domains_2 ();
878         }
879
880         [MethodImplAttribute (MethodImplOptions.NoInlining)]
881         public static void domains_2 () {
882         }
883
884         [MethodImplAttribute (MethodImplOptions.NoInlining)]
885         public static void invoke_in_domain () {
886         }
887
888         [MethodImplAttribute (MethodImplOptions.NoInlining)]
889         public static void invoke_in_domain_2 () {
890         }
891
892         [MethodImplAttribute (MethodImplOptions.NoInlining)]
893         public static void dynamic_methods () {
894                 var m = new DynamicMethod ("dyn_method", typeof (void), new Type []  { typeof (int) }, typeof (object).Module);
895                 var ig = m.GetILGenerator ();
896
897                 ig.Emit (OpCodes.Ldstr, "FOO");
898                 ig.Emit (OpCodes.Call, typeof (Tests).GetMethod ("dyn_call"));
899                 ig.Emit (OpCodes.Ret);
900
901                 var del = (Action<int>)m.CreateDelegate (typeof (Action<int>));
902
903                 del (0);
904         }
905
906         public static void dyn_call (string s) {
907         }
908
909         [MethodImplAttribute (MethodImplOptions.NoInlining)]
910         public static void ref_emit () {
911                 AssemblyName assemblyName = new AssemblyName ();
912                 assemblyName.Name = "foo";
913
914                 AssemblyBuilder assembly =
915                         Thread.GetDomain ().DefineDynamicAssembly (
916                                                                                                            assemblyName, AssemblyBuilderAccess.RunAndSave);
917
918                 ModuleBuilder module = assembly.DefineDynamicModule ("foo.dll");
919
920                 TypeBuilder tb = module.DefineType ("foo", TypeAttributes.Public, typeof (object));
921                 MethodBuilder mb = tb.DefineMethod ("ref_emit_method", MethodAttributes.Public|MethodAttributes.Static, CallingConventions.Standard, typeof (void), new Type [] { });
922                 ILGenerator ig = mb.GetILGenerator ();
923                 ig.Emit (OpCodes.Ldstr, "FOO");
924                 ig.Emit (OpCodes.Call, typeof (Tests).GetMethod ("ref_emit_call"));
925                 ig.Emit (OpCodes.Ret);
926
927                 Type t = tb.CreateType ();
928
929                 t.GetMethod ("ref_emit_method").Invoke (null, null);
930         }
931
932         [MethodImplAttribute (MethodImplOptions.NoInlining)]
933         public static void ref_emit_call (string s) {
934         }
935
936         [MethodImplAttribute (MethodImplOptions.NoInlining)]
937         public static void frames_in_native () {
938                 Thread.Sleep (500);
939         }
940
941         [MethodImplAttribute (MethodImplOptions.NoInlining)]
942         public static void string_call (string s) {
943         }
944
945         [MethodImplAttribute (MethodImplOptions.NoInlining)]
946         public static void ss_regress_654694 () {
947                 if (true) {
948                         string h = "hi";
949                         string_call (h);
950                 }
951         }
952
953         [MethodImplAttribute (MethodImplOptions.NoInlining)]
954         public static void user () {
955                 Debugger.Break ();
956
957                 Debugger.Log (5, Debugger.IsLogging () ? "A" : "", "B");
958         }
959
960         [MethodImplAttribute (MethodImplOptions.NoInlining)]
961         public static void type_load () {
962                 type_load_2 ();
963         }
964
965         [MethodImplAttribute (MethodImplOptions.NoInlining)]
966         static void type_load_2 () {
967                 var c1 = new Dictionary<int, int> ();
968                 c1.ToString ();
969                 var c = new TypeLoadClass ();
970                 c.ToString ();
971                 var c2 = new TypeLoadClass2 ();
972                 c2.ToString ();
973         }
974
975         [MethodImplAttribute (MethodImplOptions.NoInlining)]
976         public static void regress () {
977                 regress_2755 (DateTime.Now);
978         }
979
980         [MethodImplAttribute (MethodImplOptions.NoInlining)]
981         public static unsafe void regress_2755 (DateTime d) {
982                 int* buffer = stackalloc int [128];
983
984                 regress_2755_2 ();
985
986                 int sum = 0;
987                 for (int i = 0; i < 128; ++i)
988                         sum += buffer [i];
989
990                 regress_2755_3 (sum);
991         }
992
993         [MethodImplAttribute (MethodImplOptions.NoInlining)]
994         public static void regress_2755_2 () {
995         }
996
997         [MethodImplAttribute (MethodImplOptions.NoInlining)]
998         public static void regress_2755_3 (int sum) {
999         }
1000 }
1001
1002 class TypeLoadClass {
1003 }
1004
1005 class TypeLoadClass2 {
1006 }
1007
1008 public class CrossDomain : MarshalByRefObject
1009 {
1010         public void invoke () {
1011                 Tests.invoke_in_domain ();
1012         }
1013
1014         public void invoke_2 () {
1015                 Tests.invoke_in_domain_2 ();
1016         }
1017 }       
1018
1019 public class Foo
1020 {
1021         public ProcessStartInfo info;
1022 }
1023
1024 // Class used for line number info testing, don't change its layout
1025 public class LineNumbers
1026 {
1027         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1028         public static void ln1 () {
1029                 ln2 ();
1030                 ln3 ();
1031         }
1032
1033         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1034         public static void ln2 () {
1035         }
1036
1037         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1038         public static void ln3 () {
1039 #pragma warning disable 0219
1040                 int i = 5;
1041 #pragma warning restore 0219
1042                 #line 55 "FOO"
1043         }
1044 }