Merge pull request #4444 from lateralusX/jlorenss/windows-unwind-info
[mono.git] / mono / mini / objects.cs
1 using System;
2 using System.Text;
3 using System.Reflection;
4 using System.Runtime.InteropServices;
5 using System.Runtime.CompilerServices;
6
7 /*
8  * Regression tests for the mono JIT.
9  *
10  * Each test needs to be of the form:
11  *
12  * static int test_<result>_<name> ();
13  *
14  * where <result> is an integer (the value that needs to be returned by
15  * the method to make it pass.
16  * <name> is a user-displayed name used to identify the test.
17  *
18  * The tests can be driven in two ways:
19  * *) running the program directly: Main() uses reflection to find and invoke
20  *      the test methods (this is useful mostly to check that the tests are correct)
21  * *) with the --regression switch of the jit (this is the preferred way since
22  *      all the tests will be run with optimizations on and off)
23  *
24  * The reflection logic could be moved to a .dll since we need at least another
25  * regression test file written in IL code to have better control on how
26  * the IL code looks.
27  */
28
29 #if __MOBILE__
30 namespace ObjectTests
31 {
32 #endif
33
34 struct Simple {
35         public int a;
36         public byte b;
37         public short c;
38         public long d;
39 }
40
41 struct Small {
42         public byte b1;
43         public byte b2;
44 }
45
46 // Size=2, Align=1
47 struct Foo {
48         bool b1;
49         bool b2;
50 }
51
52 struct Large {
53         int one;
54         int two;
55         long three;
56         long four;
57         int five;
58         long six;
59         int seven;
60         long eight;
61         long nine;
62         long ten;
63
64         public void populate ()
65         {
66                 one = 1; two = 2;
67                 three = 3; four = 4;
68                 five = 5; six = 6;
69                 seven = 7; eight = 8;
70                 nine = 9; ten = 10;
71         }
72         public bool check ()
73         {
74                 return one == 1  && two == 2  &&
75                         three == 3  && four == 4  &&
76                         five == 5  && six == 6  &&
77                         seven == 7  && eight == 8  &&
78                         nine == 9  && ten == 10;
79         }
80 }
81
82 class Sample {
83         public int a;
84         public Sample (int v) {
85                 a = v;
86         }
87 }
88
89 [StructLayout ( LayoutKind.Explicit )]
90 struct StructWithBigOffsets {
91                 [ FieldOffset(10000) ] public byte b;
92                 [ FieldOffset(10001) ] public sbyte sb;
93                 [ FieldOffset(11000) ] public short s;
94                 [ FieldOffset(11002) ] public ushort us;
95                 [ FieldOffset(12000) ] public uint i;
96                 [ FieldOffset(12004) ] public int si;
97                 [ FieldOffset(13000) ] public long l;
98                 [ FieldOffset(14000) ] public float f;
99                 [ FieldOffset(15000) ] public double d;
100 }
101
102 enum SampleEnum {
103         A,
104         B,
105         C
106 }
107
108 struct Alpha {
109         public long a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v;
110 }
111
112 struct Beta {
113         public Alpha a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v;
114 }
115
116 struct Gamma {
117         public Beta a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v;
118 }
119
120 class Tests {
121
122 #if !__MOBILE__
123         public static int Main (string[] args) {
124                 return TestDriver.RunTests (typeof (Tests), args);
125         }
126 #endif
127         
128         public static int test_0_return () {
129                 Simple s;
130                 s.a = 1;
131                 s.b = 2;
132                 s.c = (short)(s.a + s.b);
133                 s.d = 4;
134                 return s.a - 1;
135         }
136
137         public static int test_0_string_access () {
138                 string s = "Hello";
139                 if (s [1] != 'e')
140                         return 1;
141                 return 0;
142         }
143
144         public static int test_0_string_virtual_call () {
145                 string s = "Hello";
146                 string s2 = s.ToString ();
147                 if (s2 [1] != 'e')
148                         return 1;
149                 return 0;
150         }
151
152         public static int test_0_iface_call () {
153                 string s = "Hello";
154                 object o = ((ICloneable)s).Clone ();
155                 return 0;
156         }
157
158         public static int test_5_newobj () {
159                 Sample s = new Sample (5);
160                 return s.a;
161         }
162
163         public static int test_4_box () {
164                 object obj = 4;
165                 return (int)obj;
166         }
167
168         public static int test_0_enum_unbox () {
169                 SampleEnum x = SampleEnum.A;
170                 object o = x;
171                 
172                 int res = 1;
173
174                 res = (int)o;
175                 
176                 return res;
177         }
178         
179         static Simple get_simple (int v) {
180                 Simple r = new Simple ();
181                 r.a = v;
182                 r.b = (byte)(v + 1);
183                 r.c = (short)(v + 2);
184                 r.d = v + 3;
185
186                 return r;
187         }
188
189         public static int test_3_return_struct () {
190                 Simple v = get_simple (1);
191
192                 if (v.a != 1)
193                         return 0;
194                 if (v.b != 2)
195                         return 0;
196                 if (v.c != 3)
197                         return 0;
198                 if (v.d != 4)
199                         return 0;
200                 return 3;
201         }
202
203         public virtual Simple v_get_simple (int v)
204         {
205                 return get_simple (v);
206         }
207         
208         public static int test_2_return_struct_virtual () {
209                 Tests t = new Tests ();
210                 Simple v = t.v_get_simple (2);
211
212                 if (v.a != 2)
213                         return 0;
214                 if (v.b != 3)
215                         return 0;
216                 if (v.c != 4)
217                         return 0;
218                 if (v.d != 5)
219                         return 0;
220                 return 2;
221         }
222
223         static int receive_simple (int a, Simple v, int b) {
224                 if (v.a != 1)
225                         return 1;
226                 if (v.b != 2)
227                         return 2;
228                 if (v.c != 3)
229                         return 3;
230                 if (v.d != 4)
231                         return 4;
232                 if (a != 7)
233                         return 5;
234                 if (b != 9)
235                         return 6;
236                 return 0;
237         }
238         
239         public static int test_5_pass_struct () {
240                 Simple v = get_simple (1);
241                 if (receive_simple (7, v, 9) != 0)
242                         return 0;
243                 if (receive_simple (7, get_simple (1), 9) != 0)
244                         return 1;
245                 return 5;
246         }
247
248         static Simple s_v;
249         public static int test_5_pass_static_struct () {
250                 s_v = get_simple (1);
251                 if (receive_simple (7, s_v, 9) != 0)
252                         return 0;
253                 return 5;
254         }
255
256         // Test alignment of small structs
257
258         static Small get_small (byte v) {
259                 Small r = new Small ();
260         
261                 r.b1 = v;
262                 r.b2 = (byte)(v + 1);
263
264                 return r;
265         }
266
267         static Small return_small (Small s) {
268                 return s;
269         }
270
271         static int receive_small (int a, Small v, int b) {
272                 if (v.b1 != 1)
273                         return 1;
274                 if (v.b2 != 2)
275                         return 2;
276                 return 0;
277         }
278
279         static int receive_small_sparc_many_args (int a, int a2, int a3, int a4, int a5, int a6, Small v, int b) {
280                 if (v.b1 != 1)
281                         return 1;
282                 if (v.b2 != 2)
283                         return 2;
284                 return 0;
285         }
286
287         public static int test_5_pass_small_struct () {
288                 Small v = get_small (1);
289                 if (receive_small (7, v, 9) != 0)
290                         return 0;
291                 if (receive_small (7, get_small (1), 9) != 0)
292                         return 1;
293                 if (receive_small_sparc_many_args (1, 2, 3, 4, 5, 6, v, 9) != 0)
294                         return 2;
295                 v = return_small (v);
296                 if (v.b1 != 1)
297                         return 3;
298                 if (v.b2 != 2)
299                         return 4;
300                 return 5;
301         }
302
303         // 64-bits, 32-bit aligned
304         struct struct1 {
305                 public int      a;
306                 public int      b;
307         };
308
309         static int check_struct1(struct1 x) {
310                 if (x.a != 1)
311                         return 1;
312                 if (x.b != 2)
313                         return 2;
314                 return 0;
315         }
316
317         static int pass_struct1(int a, int b, struct1 x) {
318                 if (a != 3)
319                         return 3;
320                 if (b != 4)
321                         return 4;
322                 return check_struct1(x);
323         }
324
325         static int pass_struct1(int a, struct1 x) {
326                 if (a != 3)
327                         return 3;
328                 return check_struct1(x);
329         }
330
331         static int pass_struct1(struct1 x) {
332                 return check_struct1(x);
333         }
334
335         public static int test_0_struct1_args () {
336                 int r;
337                 struct1 x;
338
339                 x.a = 1;
340                 x.b = 2;
341                 if ((r = check_struct1(x)) != 0)
342                         return r;
343                 if ((r = pass_struct1(x)) != 0)
344                         return r + 10;
345                 if ((r = pass_struct1(3, x)) != 0)
346                         return r + 20;
347                 if ((r = pass_struct1(3, 4, x)) != 0)
348                         return r + 30;
349                 return 0;
350         }
351
352         // 64-bits, 64-bit aligned
353         struct struct2 {
354                 public long     a;
355         };
356
357         static int check_struct2(struct2 x) {
358                 if (x.a != 1)
359                         return 1;
360                 return 0;
361         }
362
363         static int pass_struct2(int a, int b, int c, struct2 x) {
364                 if (a != 3)
365                         return 3;
366                 if (b != 4)
367                         return 4;
368                 if (c != 5)
369                         return 5;
370                 return check_struct2(x);
371         }
372
373         static int pass_struct2(int a, int b, struct2 x) {
374                 if (a != 3)
375                         return 3;
376                 if (b != 4)
377                         return 4;
378                 return check_struct2(x);
379         }
380
381         static int pass_struct2(int a, struct2 x) {
382                 if (a != 3)
383                         return 3;
384                 return check_struct2(x);
385         }
386
387         static int pass_struct2(struct2 x) {
388                 return check_struct2(x);
389         }
390
391         public static int test_0_struct2_args () {
392                 int r;
393                 struct2 x;
394
395                 x.a = 1;
396                 if ((r = check_struct2(x)) != 0)
397                         return r;
398                 if ((r = pass_struct2(x)) != 0)
399                         return r + 10;
400                 if ((r = pass_struct2(3, x)) != 0)
401                         return r + 20;
402                 if ((r = pass_struct2(3, 4, x)) != 0)
403                         return r + 30;
404                 if ((r = pass_struct2(3, 4, 5, x)) != 0)
405                         return r + 40;
406                 return 0;
407         }
408
409         // 128 bits
410         struct Struct3 {
411                 public long i, j, k, l;
412         }
413
414         static int pass_struct3 (int i, int j, int k, int l, int m, int n, int o, int p, Struct3 s, int q) {
415                 if (s.i + s.j + s.k + s.l != 10)
416                         return 1;
417                 else
418                         return 0;
419         }
420
421         public static int test_0_struct3_args () {
422                 Struct3 s = new Struct3 ();
423                 s.i = 1;
424                 s.j = 2;
425                 s.k = 3;
426                 s.l = 4;
427
428                 return pass_struct3 (1, 2, 3, 4, 5, 6, 7, 8, s, 9);
429         }
430
431         // Struct with unaligned size on 64 bit machines
432         struct Struct4 {
433         public int i, j, k, l, m;
434                 public int i1, i2, i3, i4, i5, i6;
435         }
436
437         static int pass_struct4 (Struct4 s) {
438                 if (s.i + s.j + s.k + s.l + s.m != 15)
439                         return 1;
440                 else
441                         return 0;
442         }
443
444         public static int test_0_struct4_args () {
445                 Struct4 s = new Struct4 ();
446                 s.i = 1;
447                 s.j = 2;
448                 s.k = 3;
449                 s.l = 4;
450                 s.m = 5;
451
452                 return pass_struct4 (s);
453         }
454
455
456
457         struct AStruct {
458                 public int i;
459
460                 public AStruct (int i) {
461                         this.i = i;
462                 }
463
464                 public override int GetHashCode () {
465                         return i;
466                 }
467         }
468
469         // Test that vtypes are unboxed during a virtual call
470         public static int test_44_unbox_trampoline () {
471                 AStruct s = new AStruct (44);
472                 object o = s;
473                 return o.GetHashCode ();
474         }
475
476         public static int test_0_unbox_trampoline2 () {
477                 int i = 12;
478                 object o = i;
479                         
480                 if (i.ToString () != "12")
481                         return 1;
482                 if (((Int32)o).ToString () != "12")
483                         return 2;
484                 if (o.ToString () != "12")
485                         return 3;
486                 return 0;
487         }
488
489         // Test fields with big offsets
490         public static int test_0_fields_with_big_offsets () {
491                 StructWithBigOffsets s = new StructWithBigOffsets ();
492                 StructWithBigOffsets s2 = new StructWithBigOffsets ();
493
494                 s.b = 0xde;
495                 s.sb = 0xe;
496                 s.s = 0x12de;
497                 s.us = 0x12da;
498                 s.i = 0xdeadbeef;
499                 s.si = 0xcafe;
500                 s.l = 0xcafebabe;
501                 s.f = 3.14F;
502                 s.d = 3.14;
503
504                 s2.b = s.b;
505                 s2.sb = s.sb;
506                 s2.s = s.s;
507                 s2.us = s.us;
508                 s2.i = s.i;
509                 s2.si = s.si;
510                 s2.l = s.l;
511                 s2.f = s.f;
512                 s2.d = s.d;
513
514                 if (s2.b != 0xde)
515                         return 1;
516                 if (s2.s != 0x12de)
517                         return 2;
518                 if (s2.i != 0xdeadbeef)
519                         return 3;
520                 if (s2.l != 0xcafebabe)
521                         return 4;
522                 if (s2.f != 3.14F)
523                         return 5;
524                 if (s2.d != 3.14)
525                         return 6;
526                 if (s2.sb != 0xe)
527                         return 7;
528                 if (s2.us != 0x12da)
529                         return 9;
530                 if (s2.si != 0xcafe)
531                         return 10;
532
533                 return 0;
534         }
535
536         class TestRegA {
537
538                 long buf_start;
539                 int buf_length, buf_offset;
540
541                 public TestRegA () {
542                         buf_start = 0;
543                         buf_length = 0;
544                         buf_offset = 0;
545                 }
546         
547                 public long Seek (long position) {
548                         long pos = position;
549                         /* interaction between the register allocator and
550                          * allocating arguments to registers */
551                         if (pos >= buf_start && pos <= buf_start + buf_length) {
552                                 buf_offset = (int) (pos - buf_start);
553                                 return pos;
554                         }
555                         return buf_start;
556                 }
557
558         }
559
560         public static int test_0_seektest () {
561                 TestRegA t = new TestRegA ();
562                 return (int)t.Seek (0);
563         }
564
565         class Super : ICloneable {
566                 public virtual object Clone () {
567                         return null;
568                 }
569         }
570         class Duper: Super {
571         }
572
573         public static int test_0_null_cast () {
574                 object o = null;
575
576                 Super s = (Super)o;
577
578                 return 0;
579         }
580         
581         public static int test_0_super_cast () {
582                 Duper d = new Duper ();
583                 Super sup = d;
584                 Object o = d;
585
586                 if (!(o is Super))
587                         return 1;
588                 try {
589                         d = (Duper)sup;
590                 } catch {
591                         return 2;
592                 }
593                 if (!(d is Object))
594                         return 3;
595                 try {
596                         d = (Duper)(object)sup;
597                 } catch {
598                         return 4;
599                 }
600                 return 0;
601         }
602
603         public static int test_0_super_cast_array () {
604                 Duper[] d = new Duper [0];
605                 Super[] sup = d;
606                 Object[] o = d;
607
608                 if (!(o is Super[]))
609                         return 1;
610                 try {
611                         d = (Duper[])sup;
612                 } catch {
613                         return 2;
614                 }
615                 if (!(d is Object[]))
616                         return 3;
617                 try {
618                         d = (Duper[])(object[])sup;
619                 } catch {
620                         return 4;
621                 }
622                 return 0;
623         }
624
625         public static int test_0_multi_array_cast () {
626                 Duper[,] d = new Duper [1, 1];
627                 object[,] o = d;
628
629                 try {
630                         o [0, 0] = new Super ();
631                         return 1;
632                 }
633                 catch (ArrayTypeMismatchException) {
634                 }
635
636                 return 0;
637         }
638
639         public static int test_0_vector_array_cast () {
640                 Array arr1 = Array.CreateInstance (typeof (int), new int[] {1}, new int[] {0});
641                 Array arr2 = Array.CreateInstance (typeof (int), new int[] {1}, new int[] {10});
642
643                 if (arr1.GetType () != typeof (int[]))
644                         return 1;
645
646                 if (arr2.GetType () == typeof (int[]))
647                         return 2;
648
649                 int[] b;
650
651                 b = (int[])arr1;
652
653                 try {
654                         b = (int[])arr2;
655                         return 3;
656                 }
657                 catch (InvalidCastException) {
658                 }
659
660                 if (arr2 is int[])
661                         return 4;
662
663                 int [,] [] arr3 = new int [1, 1] [];
664                 object o = arr3;
665                 int [,] [] arr4 = (int [,] [])o;
666
667                 return 0;
668         }
669
670         public static int test_0_enum_array_cast () {
671                 TypeCode[] tc = new TypeCode [0];
672                 object[] oa;
673                 ValueType[] vta;
674                 int[] inta;
675                 Array a = tc;
676                 bool ok;
677
678                 if (a is object[])
679                         return 1;
680                 if (a is ValueType[])
681                         return 2;
682                 if (a is Enum[])
683                         return 3;
684                 try {
685                         ok = false;
686                         oa = (object[])a;
687                 } catch {
688                         ok = true;
689                 }
690                 if (!ok)
691                         return 4;
692                 try {
693                         ok = false;
694                         vta = (ValueType[])a;
695                 } catch {
696                         ok = true;
697                 }
698                 if (!ok)
699                         return 5;
700                 try {
701                         ok = true;
702                         inta = (int[])a;
703                 } catch {
704                         ok = false;
705                 }
706                 if (!ok)
707                         return 6;
708                 return 0;
709         }
710
711         public static int test_0_more_cast_corner_cases () {
712                 ValueType[] vta = new ValueType [0];
713                 Enum[] ea = new Enum [0];
714                 Array a = vta;
715                 object[] oa;
716                 bool ok;
717
718                 if (!(a is object[]))
719                         return 1;
720                 if (!(a is ValueType[]))
721                         return 2;
722                 if (a is Enum[])
723                         return 3;
724                 a = ea;
725                 if (!(a is object[]))
726                         return 4;
727                 if (!(a is ValueType[]))
728                         return 5;
729                 if (!(a is Enum[]))
730                         return 6;
731
732                 try {
733                         ok = true;
734                         oa = (object[])a;
735                 } catch {
736                         ok = false;
737                 }
738                 if (!ok)
739                         return 7;
740         
741                 try {
742                         ok = true;
743                         oa = (Enum[])a;
744                 } catch {
745                         ok = false;
746                 }
747                 if (!ok)
748                         return 8;
749         
750                 try {
751                         ok = true;
752                         oa = (ValueType[])a;
753                 } catch {
754                         ok = false;
755                 }
756                 if (!ok)
757                         return 9;
758
759                 a = vta;
760                 try {
761                         ok = true;
762                         oa = (object[])a;
763                 } catch {
764                         ok = false;
765                 }
766                 if (!ok)
767                         return 10;
768         
769                 try {
770                         ok = true;
771                         oa = (ValueType[])a;
772                 } catch {
773                         ok = false;
774                 }
775                 if (!ok)
776                         return 11;
777         
778                 try {
779                         ok = false;
780                         vta = (Enum[])a;
781                 } catch {
782                         ok = true;
783                 }
784                 if (!ok)
785                         return 12;
786                 return 0;
787         }
788
789         public static int test_0_cast_iface_array () {
790                 object o = new ICloneable [0];
791                 object o2 = new Duper [0];
792                 object t;
793                 bool ok;
794
795                 if (!(o is object[]))
796                         return 1;
797                 if (!(o2 is ICloneable[]))
798                         return 2;
799
800                 try {
801                         ok = true;
802                         t = (object[])o;
803                 } catch {
804                         ok = false;
805                 }
806                 if (!ok)
807                         return 3;
808         
809                 try {
810                         ok = true;
811                         t = (ICloneable[])o2;
812                 } catch {
813                         ok = false;
814                 }
815                 if (!ok)
816                         return 4;
817
818                 try {
819                         ok = true;
820                         t = (ICloneable[])o;
821                 } catch {
822                         ok = false;
823                 }
824                 if (!ok)
825                         return 5;
826
827                 if (!(o is ICloneable[]))
828                         return 6;
829
830                 /* add tests for interfaces that 'inherit' interfaces */
831                 return 0;
832         }
833
834         private static int[] daysmonthleap = { 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
835
836         private static int AbsoluteDays (int year, int month, int day)
837         {
838                 int temp = 0, m = 1;
839                 int[] days = daysmonthleap;
840                 while (m < month)
841                         temp += days[m++];
842                 return ((day-1) + temp + (365* (year-1)) + ((year-1)/4) - ((year-1)/100) + ((year-1)/400));
843         }
844
845         public static int test_719162_complex_div () {
846                 int adays = AbsoluteDays (1970, 1, 1);
847                 return adays;
848         }
849
850         delegate int GetIntDel ();
851
852         static int return4 () {
853                 return 4;
854         }
855
856         int return5 () {
857                 return 5;
858         }
859
860         public static int test_2_static_delegate () {
861                 GetIntDel del = new GetIntDel (return4);
862                 int v = del ();
863                 if (v != 4)
864                         return 0;
865                 return 2;
866         }
867
868         public static int test_2_instance_delegate () {
869                 Tests t = new Tests ();
870                 GetIntDel del = new GetIntDel (t.return5);
871                 int v = del ();
872                 if (v != 5)
873                         return 0;
874                 return 2;
875         }
876
877         class InstanceDelegateTest {
878                 public int a;
879
880                 public int return_field () {
881                         return a;
882                 }
883         }
884
885         public static int test_2_instance_delegate_with_field () {
886                 InstanceDelegateTest t = new InstanceDelegateTest () { a = 1337 };
887                 GetIntDel del = new GetIntDel (t.return_field);
888                 int v = del ();
889                 if (v != 1337)
890                         return 0;
891                 return 2;
892         }
893
894         interface IFaceVirtualDel {
895                 int return_field ();
896         }
897
898         struct VtypeVirtualDelStruct : IFaceVirtualDel {
899                 public int f;
900                 public int return_field_nonvirt () {
901                         return f;
902                 }
903                 public int return_field () {
904                         return f;
905                 }
906         }
907
908         public static int test_42_vtype_delegate () {
909                 var s = new VtypeVirtualDelStruct () { f = 42 };
910                 Func<int> f = s.return_field_nonvirt;
911                 return f ();
912         }
913
914         public static int test_42_vtype_virtual_delegate () {
915                 IFaceVirtualDel s = new VtypeVirtualDelStruct () { f = 42 };
916                 Func<int> f = s.return_field;
917                 return f ();
918         }
919
920         public static int test_1_store_decimal () {
921                 decimal[,] a = {{1}};
922
923                 if (a[0,0] != 1m)
924                         return 0;
925                 return 1;
926         }
927
928         public static int test_2_intptr_stobj () {
929                 System.IntPtr [] arr = { new System.IntPtr () };
930
931                 if (arr [0] != (System.IntPtr)0)
932                         return 1;
933                 return 2;
934         }
935
936         static int llmult (int a, int b, int c, int d) {
937                 return a + b + c + d;
938         }
939
940         /* 
941          * Test that evaluation of complex arguments does not overwrite the
942          * arguments already in outgoing registers.
943          */
944         public static int test_155_regalloc () {
945                 int a = 10;
946                 int b = 10;
947
948                 int c = 0;
949                 int d = 0;
950                 int[] arr = new int [5];
951
952                 return llmult (arr [c + d], 150, 5, 0);
953         }
954
955         static bool large_struct_test (Large a, Large b, Large c, Large d)
956         {
957                 if (!a.check ()) return false;
958                 if (!b.check ()) return false;
959                 if (!c.check ()) return false;
960                 if (!d.check ()) return false;
961                 return true;
962         }
963
964         public static int test_2_large_struct_pass ()
965         {
966                 Large a, b, c, d;
967                 a = new Large ();
968                 b = new Large ();
969                 c = new Large ();
970                 d = new Large ();
971                 a.populate ();
972                 b.populate ();
973                 c.populate ();
974                 d.populate ();
975                 if (large_struct_test (a, b, c, d))
976                         return 2;
977                 return 0;
978         }
979
980         public static unsafe int test_0_pin_string () {
981                 string x = "xxx";
982                 fixed (char *c = x) {
983                         if (*c != 'x')
984                                 return 1;
985                 }
986                 return 0;
987         }
988         
989         public static int my_flags;
990         public static int test_0_and_cmp_static ()
991         {
992                 
993                 /* various forms of test [mem], imm */
994                 
995                 my_flags = 0x01020304;
996                 
997                 if ((my_flags & 0x01020304) == 0)
998                         return 1;
999                 
1000                 if ((my_flags & 0x00000304) == 0)
1001                         return 2;
1002                 
1003                 if ((my_flags & 0x00000004) == 0)
1004                         return 3;
1005                 
1006                 if ((my_flags & 0x00000300) == 0)
1007                         return 4;
1008                 
1009                 if ((my_flags & 0x00020000) == 0)
1010                         return 5;
1011                 
1012                 if ((my_flags & 0x01000000) == 0)
1013                         return 6;
1014                 
1015                 return 0;
1016         }
1017         
1018         static byte b;
1019         public static int test_0_byte_compares ()
1020         {
1021                 b = 0xff;
1022                 if (b == -1)
1023                         return 1;
1024                 b = 0;
1025                 if (!(b < System.Byte.MaxValue))
1026                         return 2;
1027                 
1028                 if (!(b <= System.Byte.MaxValue))
1029                         return 3;
1030                 
1031                 return 0;
1032         }
1033
1034         public static int test_71_long_shift_right () {
1035                 ulong value = 38654838087;
1036                 int x = 0;
1037                 byte [] buffer = new byte [1];
1038                 buffer [x] = ((byte)(value >> x));
1039                 return buffer [x];
1040         }
1041         
1042         static long x;
1043         public static int test_0_addsub_mem ()
1044         {
1045                 x = 0;
1046                 x += 5;
1047                 
1048                 if (x != 5)
1049                         return 1;
1050                 
1051                 x -= 10;
1052                 
1053                 if (x != -5)
1054                         return 2;
1055                 
1056                 return 0;
1057         }
1058         
1059         static ulong y;
1060         public static int test_0_sh32_mem ()
1061         {
1062                 y = 0x0102130405060708;
1063                 y >>= 32;
1064                 
1065                 if (y != 0x01021304)
1066                         return 1;
1067                 
1068                 y = 0x0102130405060708;
1069                 y <<= 32;
1070                 
1071                 if (y != 0x0506070800000000)
1072                         return 2;
1073                 
1074                 x = 0x0102130405060708;
1075                 x <<= 32;
1076                 
1077                 if (x != 0x0506070800000000)
1078                         return 2;
1079                 
1080                 return 0;
1081         }
1082
1083
1084         static uint dum_de_dum = 1;
1085         public static int test_0_long_arg_opt ()
1086         {
1087                 return Foo (0x1234567887654321, dum_de_dum);
1088         }
1089         
1090         static int Foo (ulong x, ulong y)
1091         {
1092                 if (x != 0x1234567887654321)
1093                         return 1;
1094                 
1095                 if (y != 1)
1096                         return 2;
1097                 
1098                 return 0;
1099         }
1100         
1101         public static int test_0_long_ret_opt ()
1102         {
1103                 ulong x = X ();
1104                 if (x != 0x1234567887654321)
1105                         return 1;
1106                 ulong y = Y ();
1107                 if (y != 1)
1108                         return 2;
1109                 
1110                 return 0;
1111         }
1112         
1113         static ulong X ()
1114         {
1115                 return 0x1234567887654321;
1116         }
1117         
1118         static ulong Y ()
1119         {
1120                 return dum_de_dum;
1121         }
1122
1123         /* from bug# 71515 */
1124         static int counter = 0;
1125         static bool WriteStuff () {
1126                 counter = 10;
1127                 return true;
1128         }
1129         public static int test_0_cond_branch_side_effects () {
1130                 counter = 5;
1131                 if (WriteStuff()) {
1132                 }
1133                 if (counter == 10)
1134                         return 0;
1135                 return 1;
1136         }
1137
1138         // bug #74992
1139         public static int arg_only_written (string file_name, int[]
1140 ncells ) {
1141                 if (file_name == null)
1142                         return 1;
1143
1144                 ncells = foo ();
1145                 bar (ncells [0]);
1146
1147                 return 0;
1148         }
1149
1150         public static int[] foo () {
1151                 return new int [3];
1152         }
1153
1154         public static void bar (int i) {
1155         }
1156         
1157
1158         public static int test_0_arg_only_written ()
1159         {
1160                 return arg_only_written ("md.in", null);
1161         }               
1162
1163         static long position = 0;
1164
1165         public static int test_4_static_inc_long () {
1166
1167                 int count = 4;
1168
1169                 position = 0;
1170
1171                 position += count;
1172
1173                 return (int)position;
1174         }
1175
1176         struct FooStruct {
1177
1178                 public FooStruct (long l) {
1179                 }
1180         }
1181
1182         public static int test_0_calls_opcode_emulation () {
1183                 // Test that emulated opcodes do not clobber arguments already in
1184                 // out registers
1185                 checked {
1186                         long val = 10000;
1187                         new FooStruct (val * 10000);
1188                 }
1189                 return 0;
1190         }
1191
1192         public static int test_0_intrins_string_length () {
1193                 string s = "ABC";
1194
1195                 return (s.Length == 3) ? 0 : 1;
1196         }
1197
1198         public static int test_0_intrins_string_chars () {
1199                 string s = "ABC";
1200
1201                 return (s [0] == 'A' && s [1] == 'B' && s [2] == 'C') ? 0 : 1;
1202         }
1203
1204         public static int test_0_intrins_object_gettype () {
1205                 object o = 1;
1206
1207                 return (o.GetType () == typeof (int)) ? 0 : 1;
1208         }
1209
1210         public static int test_0_intrins_object_gethashcode () {
1211                 object o = new Object ();
1212
1213                 return (o.GetHashCode () == o.GetHashCode ()) ? 0 : 1;
1214         }
1215
1216         class FooClass {
1217         }
1218
1219         public static int test_0_intrins_object_ctor () {
1220                 object o = new FooClass ();
1221
1222                 return (o != null) ? 0 : 1;
1223         }
1224
1225         public static int test_0_intrins_array_rank () {
1226                 int[,] a = new int [10, 10];
1227
1228                 return (a.Rank == 2) ? 0 : 1;
1229         }
1230
1231         public static int test_0_intrins_array_length () {
1232                 int[,] a = new int [10, 10];
1233                 Array a2 = a;
1234
1235                 return (a2.Length == 100) ? 0 : 1;
1236         }
1237
1238         public static int test_0_intrins_runtimehelpers_offset_to_string_data () {
1239                 int i = RuntimeHelpers.OffsetToStringData;
1240                 
1241                 return i - i;
1242         }
1243
1244         public static int test_0_intrins_string_setchar () {
1245                 StringBuilder sb = new StringBuilder ("ABC");
1246
1247                 sb [1] = 'D';
1248
1249                 return sb.ToString () == "ADC" ? 0 : 1;
1250         }
1251
1252         public class Bar {
1253                 bool allowLocation = true;
1254         Foo f = new Foo ();     
1255         }
1256
1257         public static int test_0_regress_78990_unaligned_structs () {
1258                 new Bar ();
1259
1260                 return 0;
1261         }
1262
1263         public static unsafe int test_97_negative_index () {
1264                 char[] arr = new char[] {'a', 'b'};
1265                 fixed (char *p = arr) {
1266                         char *i = p + 2;
1267                         char a = i[-2];
1268                         return a;
1269                 }
1270         }
1271
1272         /* bug #82281 */
1273         public static int test_0_unsigned_right_shift_imm0 () {
1274                 uint temp = 0;
1275                 byte[] data = new byte[256];
1276                 for (int i = 0; i < 1; i ++)
1277                         temp = (uint)(data[temp >> 24] | data[temp >> 0]);
1278                 return 0;
1279         }
1280
1281         class Foo2 {
1282                 public virtual int foo () {
1283                         return 0;
1284                 }
1285         }
1286
1287         sealed class Bar2 : Foo2 {
1288                 public override int foo () {
1289                         return 0;
1290                 }
1291         }
1292
1293         public static int test_0_abcrem_check_this_removal () {
1294                 Bar2 b = new Bar2 ();
1295
1296                 // The check_this generated here by the JIT should be removed
1297                 b.foo ();
1298
1299                 return 0;
1300         }
1301
1302         static int invoke_twice (Bar2 b) {
1303                 b.foo ();
1304                 // The check_this generated here by the JIT should be removed
1305                 b.foo ();
1306
1307                 return 0;
1308         }
1309
1310         public static int test_0_abcrem_check_this_removal2 () {
1311                 Bar2 b = new Bar2 ();
1312
1313                 invoke_twice (b);
1314
1315                 return 0;
1316         }
1317
1318         /* #346563 */
1319         public static int test_0_array_access_64_bit () {
1320                 int[] arr2 = new int [10];
1321                 for (int i = 0; i < 10; ++i)
1322                         arr2 [i] = i;
1323                 string s = "ABCDEFGH";
1324
1325                 byte[] arr = new byte [4];
1326                 arr [0] = 252;
1327                 arr [1] = 255;
1328                 arr [2] = 255;
1329                 arr [3] = 255;
1330
1331                 int len = arr [0] | (arr [1] << 8) | (arr [2] << 16) | (arr [3] << 24);
1332                 int len2 = - (len + 2);
1333
1334                 // Test array and string access with a 32 bit value whose upper 32 bits are
1335                 // undefined
1336                 // len2 = 3
1337                 if (arr2 [len2] != 2)
1338                         return 1;
1339                 if (s [len2] != 'C')
1340                         return 2;
1341                 return 0;
1342         }
1343
1344         public static float return_float () {
1345                 return 1.4e-45f;
1346         }
1347
1348         public static int test_0_float_return_spill () {
1349                 // The return value of return_float () is spilled because of the
1350                 // boxing call
1351                 object o = return_float ();
1352                 float f = return_float ();
1353                 return (float)o == f ? 0 : 1;
1354         }
1355
1356         class R4Holder {
1357                 public static float pi = 3.14f;
1358
1359                 public float float_field;
1360         }
1361
1362         public static int test_0_ldsfld_soft_float () {
1363                 if (R4Holder.pi == 3.14f)
1364                         return 0;
1365                 else
1366                         return 1;
1367         }
1368
1369         public static int test_0_ldfld_stfld_soft_float () {
1370                 R4Holder h = new R4Holder ();
1371                 h.float_field = 3.14f;
1372
1373                 if (h.float_field == 3.14f)
1374                         return 0;
1375                 else
1376                         return 1;
1377         }
1378
1379         class R4HolderRemote : MarshalByRefObject {
1380                 public static float pi = 3.14f;
1381
1382                 public float float_field;
1383         }
1384
1385         public static int test_0_ldfld_stfld_soft_float_remote () {
1386                 R4HolderRemote h = new R4HolderRemote ();
1387                 h.float_field = 3.14f;
1388
1389                 if (h.float_field == 3.14f)
1390                         return 0;
1391                 else
1392                         return 1;
1393         }
1394
1395         public static int test_0_locals_soft_float () {
1396                 float f = 0.0f;
1397                 
1398                 f = 3.14f;
1399
1400                 if (f == 3.14f)
1401                         return 0;
1402                 else
1403                         return 1;
1404         }
1405
1406         struct AStruct2 {
1407                 public int i;
1408                 public int j;
1409         }
1410
1411         static float pass_vtype_return_float (AStruct2 s) {
1412                 return s.i + s.j == 6 ? 1.0f : -1.0f;
1413         }
1414
1415         public static int test_0_vtype_arg_soft_float () {
1416                 return pass_vtype_return_float (new AStruct2 () { i = 2, j = 4 }) > 0.0 ? 0 : 1;
1417         }
1418
1419         static int range_check_strlen (int i, string s) {
1420                 if (i < 0 || i > s.Length)
1421                         return 1;
1422                 else
1423                         return 0;
1424         }
1425                 
1426         public static int test_0_range_check_opt () {
1427                 if (range_check_strlen (0, "A") != 0)
1428                         return 1;
1429                 if (range_check_strlen (1, "A") != 0)
1430                         return 2;
1431                 if (range_check_strlen (2, "A") != 1)
1432                         return 3;
1433                 if (range_check_strlen (-100, "A") != 1)
1434                         return 4;
1435                 return 0;
1436         }
1437
1438         static int test_0_array_get_set_soft_float () {
1439                 float[,] arr = new float [2, 2];
1440                 arr [0, 0] = 256f;
1441                 return arr [0, 0] == 256f ? 0 : 1;
1442         }
1443
1444         //repro for #506915
1445         struct Bug506915 { public int val; }
1446         static int test_2_ldobj_stobj_optization ()
1447         {
1448                 int i = 99;
1449                 var a = new Bug506915 ();
1450                 var b = new Bug506915 ();
1451                 if (i.GetHashCode () == 99)
1452                         i = 44;
1453                 var array = new Bug506915 [2];
1454                 array [0].val = 2;
1455                 array [1] = (i == 0) ? a : array [0];
1456                 
1457                 return array [1].val;
1458         }
1459
1460         /* mcs can't compile this (#646744) */
1461 #if FALSE
1462         static void InitMe (out Gamma noMercyWithTheStack) {
1463                 noMercyWithTheStack = new Gamma ();
1464         }
1465
1466         static int FunNoInline () {
1467                 int x = 99;
1468                 if (x > 344 && x < 22)
1469                         return 333;
1470                 return x;
1471         }
1472
1473         static float DoNothingButDontInline (float a, int b) {
1474                 if (b > 0)
1475                         return a;
1476                 else if (b < 0 && b > 10)
1477                         return 444.0f;
1478                 return a;
1479         }
1480
1481         /*
1482          * The local register allocator emits loadr8_membase and storer8_membase
1483          * to do spilling. This code is generated after mono_arch_lowering_pass so
1484          * mono_arch_output_basic_block must know how to deal with big offsets.
1485          * This only happens because the call in middle forces the temp for "(float)obj"
1486          * to be spilled.
1487         */
1488         public static int test_0_float_load_and_store_with_big_offset ()
1489         {
1490                 object obj = 1.0f;
1491                 Gamma noMercyWithTheStack;
1492                 float res;
1493
1494                 InitMe (out noMercyWithTheStack);
1495
1496                 res = DoNothingButDontInline ((float)obj, FunNoInline ());
1497
1498                 if (!(res == 1.0f))
1499                         return 1;
1500                 return 0;
1501         }
1502 #endif
1503
1504         struct VTypePhi {
1505                 public int i;
1506         }
1507
1508         static int vtype_phi (VTypePhi v1, VTypePhi v2, bool first) {
1509                 VTypePhi v = first ? v1 : v2;
1510
1511                 return v.i;
1512         }
1513
1514         static int test_0_vtype_phi ()
1515         {
1516                 VTypePhi v1 = new VTypePhi () { i = 1 };
1517                 VTypePhi v2 = new VTypePhi () { i = 2 };
1518
1519                 if (vtype_phi (v1, v2, true) != 1)
1520                         return 1;
1521                 if (vtype_phi (v1, v2, false) != 2)
1522                         return 2;
1523
1524                 return 0;
1525         }
1526
1527         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1528         static void UseValue (int index)
1529         {
1530         }
1531
1532         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1533         static bool IsFalse ()
1534         {
1535                 return false;
1536         }
1537
1538         static int test_0_llvm_moving_faulting_loads ()
1539         {
1540                 int[] indexes = null;
1541
1542                 if (IsFalse ()) {
1543                         indexes = new int[0];
1544                 }
1545                         
1546                 while (IsFalse ()) {
1547                         UseValue (indexes[0]);
1548                         UseValue (indexes[0]);
1549                 }
1550
1551                 return 0;
1552         }
1553
1554         public static bool flag;
1555
1556         class B {
1557
1558                 internal static B[] d;
1559
1560                 static B () {
1561                         flag = true;
1562                 }
1563         }
1564
1565         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1566         static int regress_679467_inner () {
1567                 if (flag == true)
1568                         return 1;
1569                 var o = B.d;
1570                 var o2 = B.d;
1571                 return 0;
1572         }
1573
1574         /*
1575          * FIXME: This fails with AOT #703317.
1576          */
1577         /*
1578         static int test_0_multiple_cctor_calls_regress_679467 () {
1579                 flag = false;
1580                 return regress_679467_inner ();
1581         }
1582         */
1583
1584         static int test_0_char_ctor () {
1585                 string s = new String (new char[] { 'A', 'B' }, 0, 1);
1586                 return 0;
1587         }
1588
1589         static object mInstance = null;
1590
1591         [MethodImpl(MethodImplOptions.Synchronized)]
1592         public static object getInstance() {
1593                 if (mInstance == null)
1594                         mInstance = new object();
1595                 return mInstance;
1596         }
1597
1598         static int test_0_synchronized () {
1599                 getInstance ();
1600                 return 0;
1601         }
1602
1603         struct BStruct {
1604                 public Type t;
1605         }
1606
1607         class Del<T> {
1608                 public static BStruct foo () {
1609                         return new BStruct () { t = typeof (T) };
1610                 }
1611         }
1612
1613         delegate BStruct ADelegate ();
1614
1615         static int test_0_regress_10601 () {
1616                 var act = (ADelegate)(Del<string>.foo);
1617                 BStruct b = act ();
1618                 if (b.t != typeof (string))
1619                         return 1;
1620                 return 0;
1621         }
1622
1623         static int test_0_regress_11058 () {
1624                 int foo = -252674008;
1625                 int foo2 = (int)(foo ^ 0xF0F0F0F0); // = 28888
1626                 var arr = new byte[foo2].Length;
1627                 return 0;
1628         }
1629
1630         public static void do_throw () {
1631                 throw new Exception ();
1632         }
1633
1634         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1635         static void empty () {
1636         }
1637
1638         // #11297
1639         public static int test_0_llvm_inline_throw () {
1640                 try {
1641                         empty ();
1642                 } catch (Exception) {
1643                         do_throw ();
1644                 }
1645
1646                 return 0;
1647         }
1648
1649         enum ByteEnum : byte {
1650         Zero = 0
1651     }
1652
1653     struct BugStruct {
1654         public ByteEnum f1;
1655         public ByteEnum f2;
1656         public ByteEnum f3;
1657         public byte f4;
1658         public byte f5;
1659         public byte f6;
1660         public byte f7;
1661     }
1662
1663         public static int test_0_14217 () {
1664                 t_14217_inner (new BugStruct ());
1665                 return 0;
1666         }
1667
1668         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1669         static void t_14217_inner (BugStruct bug) {
1670     }
1671
1672         [StructLayout(LayoutKind.Sequential)]
1673         public struct EmptyStruct {
1674         }
1675
1676         class EmptyClass {
1677                 public static EmptyStruct s;
1678         }
1679
1680         // #20349
1681         static int test_0_empty_struct_as_static () {
1682                 var s = EmptyClass.s;
1683                 return 0;
1684         }
1685
1686         // #25487
1687         static int test_0_int_to_r4 () {
1688                 return int_to_r4_inner (255);
1689         }
1690
1691         static int int_to_r4_inner (int value1) {
1692                 int sub = -value1;
1693                 float mult = sub * 1f;
1694                 if (mult != -255.0f)
1695                         return 1;
1696                 else
1697                         return 0;
1698         }
1699
1700         struct HFA4D {
1701                 public double a, b, c, d;
1702         }
1703
1704         static double arm64_hfa_on_stack_inner (double d1, double d2, double d3, double d4, double d5, double d6, double d7, double d8, HFA4D s) {
1705                 return s.a + s.b + s.c + s.d;
1706         }
1707
1708         static int test_0_arm64_hfa_on_stack () {
1709                 var s = new HFA4D () { a = 1.0, b = 2.0, c = 3.0, d = 4.0 };
1710                 var res = arm64_hfa_on_stack_inner (1, 2, 3, 4, 5, 6, 7, 8, s);
1711                 return res == 10.0 ? 0 : 1;
1712         }
1713
1714         class MulOvfClass {
1715                 [MethodImplAttribute (MethodImplOptions.NoInlining)]
1716                 public unsafe void EncodeIntoBuffer(char* value, int valueLength, char* buffer, int bufferLength) {
1717                 }
1718         }
1719
1720         static unsafe int test_0_mul_ovf_regress_36052 () {
1721                 var p = new MulOvfClass ();
1722
1723                 string typeName = typeof(int).Name;
1724                 int bufferSize = 45;
1725
1726                 fixed (char* value = typeName) {
1727                         char* buffer = stackalloc char[bufferSize];
1728                         p.EncodeIntoBuffer(value, typeName.Length, buffer, bufferSize);
1729                 }
1730                 return 0;
1731         }
1732
1733         struct Struct16 {
1734                 public int a, b, c, d;
1735         }
1736
1737         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1738         static int pass_struct16 (object o0, object o2, object o3, object o4, object o5, object o6, object o7, Struct16 o8) {
1739                 // This disables LLVM
1740                 try {
1741                 } catch {
1742                 }
1743                 return o8.a;
1744         }
1745
1746         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1747         static int pass_struct16 (object o0, object o2, object o3, object o6, object o7, Struct16 o8) {
1748                 return pass_struct16 (o0, o2, null, o3, null, o6, o7, o8);
1749         }
1750
1751         public static int test_42_pass_16byte_struct_split () {
1752                 return pass_struct16 (null, null, null, null, null, new Struct16 () { a = 42 });
1753         }
1754
1755         public interface IComparer2
1756         {
1757                 Type foo<T> ();
1758         }
1759
1760         public class AClass : IComparer2 {
1761                 public Type foo<T> () {
1762                         return typeof(T);
1763                 }
1764         }
1765
1766         public static int test_0_delegate_to_virtual_generic_on_ifaces () {
1767                 IComparer2 c = new AClass ();
1768
1769                 Func<Type> f = c.foo<string>;
1770                 return f () == typeof(string) ? 0 : 1;
1771         }
1772
1773         public enum ByteEnum2 : byte {
1774                 High = 142
1775         }
1776
1777         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1778         public static int enum_arg_zero_extend (ByteEnum2 b) {
1779                 return (int)b;
1780         }
1781
1782         public static int test_142_byte_enum_arg_zero_extend () {
1783                 return enum_arg_zero_extend (ByteEnum2.High);
1784         }
1785
1786         enum Mine { One, Two }
1787
1788         public static int test_0_enum_gethashcode_opt () {
1789                 int sum = 0;
1790         for (int i = 0; i < 1000000; ++i)
1791                         sum += Mine.Two.GetHashCode();
1792
1793         return 0;
1794     }
1795 }
1796
1797 #if __MOBILE__
1798 }
1799 #endif