Merge pull request #4615 from alexanderkyte/string_error_handling
[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                 Array arr5 = Array.CreateInstance (typeof (string), new int[] {1}, new int[] {10});
643
644                 if (arr1.GetType () != typeof (int[]))
645                         return 1;
646
647                 if (arr2.GetType () == typeof (int[]))
648                         return 2;
649
650                 int[] b;
651
652                 b = (int[])arr1;
653
654                 try {
655                         b = (int[])arr2;
656                         return 3;
657                 }
658                 catch (InvalidCastException) {
659                 }
660
661                 if (arr2 is int[])
662                         return 4;
663                 var as_object_arr = arr5 as object [];
664                 if (as_object_arr != null)
665                         return 5;
666
667                 int [,] [] arr3 = new int [1, 1] [];
668                 object o = arr3;
669                 int [,] [] arr4 = (int [,] [])o;
670
671                 return 0;
672         }
673
674         public static int test_0_enum_array_cast () {
675                 TypeCode[] tc = new TypeCode [0];
676                 object[] oa;
677                 ValueType[] vta;
678                 int[] inta;
679                 Array a = tc;
680                 bool ok;
681
682                 if (a is object[])
683                         return 1;
684                 if (a is ValueType[])
685                         return 2;
686                 if (a is Enum[])
687                         return 3;
688                 try {
689                         ok = false;
690                         oa = (object[])a;
691                 } catch {
692                         ok = true;
693                 }
694                 if (!ok)
695                         return 4;
696                 try {
697                         ok = false;
698                         vta = (ValueType[])a;
699                 } catch {
700                         ok = true;
701                 }
702                 if (!ok)
703                         return 5;
704                 try {
705                         ok = true;
706                         inta = (int[])a;
707                 } catch {
708                         ok = false;
709                 }
710                 if (!ok)
711                         return 6;
712                 return 0;
713         }
714
715         public static int test_0_more_cast_corner_cases () {
716                 ValueType[] vta = new ValueType [0];
717                 Enum[] ea = new Enum [0];
718                 Array a = vta;
719                 object[] oa;
720                 bool ok;
721
722                 if (!(a is object[]))
723                         return 1;
724                 if (!(a is ValueType[]))
725                         return 2;
726                 if (a is Enum[])
727                         return 3;
728                 a = ea;
729                 if (!(a is object[]))
730                         return 4;
731                 if (!(a is ValueType[]))
732                         return 5;
733                 if (!(a is Enum[]))
734                         return 6;
735
736                 try {
737                         ok = true;
738                         oa = (object[])a;
739                 } catch {
740                         ok = false;
741                 }
742                 if (!ok)
743                         return 7;
744         
745                 try {
746                         ok = true;
747                         oa = (Enum[])a;
748                 } catch {
749                         ok = false;
750                 }
751                 if (!ok)
752                         return 8;
753         
754                 try {
755                         ok = true;
756                         oa = (ValueType[])a;
757                 } catch {
758                         ok = false;
759                 }
760                 if (!ok)
761                         return 9;
762
763                 a = vta;
764                 try {
765                         ok = true;
766                         oa = (object[])a;
767                 } catch {
768                         ok = false;
769                 }
770                 if (!ok)
771                         return 10;
772         
773                 try {
774                         ok = true;
775                         oa = (ValueType[])a;
776                 } catch {
777                         ok = false;
778                 }
779                 if (!ok)
780                         return 11;
781         
782                 try {
783                         ok = false;
784                         vta = (Enum[])a;
785                 } catch {
786                         ok = true;
787                 }
788                 if (!ok)
789                         return 12;
790                 return 0;
791         }
792
793         public static int test_0_cast_iface_array () {
794                 object o = new ICloneable [0];
795                 object o2 = new Duper [0];
796                 object t;
797                 bool ok;
798
799                 if (!(o is object[]))
800                         return 1;
801                 if (!(o2 is ICloneable[]))
802                         return 2;
803
804                 try {
805                         ok = true;
806                         t = (object[])o;
807                 } catch {
808                         ok = false;
809                 }
810                 if (!ok)
811                         return 3;
812         
813                 try {
814                         ok = true;
815                         t = (ICloneable[])o2;
816                 } catch {
817                         ok = false;
818                 }
819                 if (!ok)
820                         return 4;
821
822                 try {
823                         ok = true;
824                         t = (ICloneable[])o;
825                 } catch {
826                         ok = false;
827                 }
828                 if (!ok)
829                         return 5;
830
831                 if (!(o is ICloneable[]))
832                         return 6;
833
834                 /* add tests for interfaces that 'inherit' interfaces */
835                 return 0;
836         }
837
838         private static int[] daysmonthleap = { 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
839
840         private static int AbsoluteDays (int year, int month, int day)
841         {
842                 int temp = 0, m = 1;
843                 int[] days = daysmonthleap;
844                 while (m < month)
845                         temp += days[m++];
846                 return ((day-1) + temp + (365* (year-1)) + ((year-1)/4) - ((year-1)/100) + ((year-1)/400));
847         }
848
849         public static int test_719162_complex_div () {
850                 int adays = AbsoluteDays (1970, 1, 1);
851                 return adays;
852         }
853
854         delegate int GetIntDel ();
855
856         static int return4 () {
857                 return 4;
858         }
859
860         int return5 () {
861                 return 5;
862         }
863
864         public static int test_2_static_delegate () {
865                 GetIntDel del = new GetIntDel (return4);
866                 int v = del ();
867                 if (v != 4)
868                         return 0;
869                 return 2;
870         }
871
872         public static int test_2_instance_delegate () {
873                 Tests t = new Tests ();
874                 GetIntDel del = new GetIntDel (t.return5);
875                 int v = del ();
876                 if (v != 5)
877                         return 0;
878                 return 2;
879         }
880
881         class InstanceDelegateTest {
882                 public int a;
883
884                 public int return_field () {
885                         return a;
886                 }
887         }
888
889         public static int test_2_instance_delegate_with_field () {
890                 InstanceDelegateTest t = new InstanceDelegateTest () { a = 1337 };
891                 GetIntDel del = new GetIntDel (t.return_field);
892                 int v = del ();
893                 if (v != 1337)
894                         return 0;
895                 return 2;
896         }
897
898         interface IFaceVirtualDel {
899                 int return_field ();
900         }
901
902         struct VtypeVirtualDelStruct : IFaceVirtualDel {
903                 public int f;
904                 public int return_field_nonvirt () {
905                         return f;
906                 }
907                 public int return_field () {
908                         return f;
909                 }
910         }
911
912         public static int test_42_vtype_delegate () {
913                 var s = new VtypeVirtualDelStruct () { f = 42 };
914                 Func<int> f = s.return_field_nonvirt;
915                 return f ();
916         }
917
918         public static int test_42_vtype_virtual_delegate () {
919                 IFaceVirtualDel s = new VtypeVirtualDelStruct () { f = 42 };
920                 Func<int> f = s.return_field;
921                 return f ();
922         }
923
924         public static int test_1_store_decimal () {
925                 decimal[,] a = {{1}};
926
927                 if (a[0,0] != 1m)
928                         return 0;
929                 return 1;
930         }
931
932         public static int test_2_intptr_stobj () {
933                 System.IntPtr [] arr = { new System.IntPtr () };
934
935                 if (arr [0] != (System.IntPtr)0)
936                         return 1;
937                 return 2;
938         }
939
940         static int llmult (int a, int b, int c, int d) {
941                 return a + b + c + d;
942         }
943
944         /* 
945          * Test that evaluation of complex arguments does not overwrite the
946          * arguments already in outgoing registers.
947          */
948         public static int test_155_regalloc () {
949                 int a = 10;
950                 int b = 10;
951
952                 int c = 0;
953                 int d = 0;
954                 int[] arr = new int [5];
955
956                 return llmult (arr [c + d], 150, 5, 0);
957         }
958
959         static bool large_struct_test (Large a, Large b, Large c, Large d)
960         {
961                 if (!a.check ()) return false;
962                 if (!b.check ()) return false;
963                 if (!c.check ()) return false;
964                 if (!d.check ()) return false;
965                 return true;
966         }
967
968         public static int test_2_large_struct_pass ()
969         {
970                 Large a, b, c, d;
971                 a = new Large ();
972                 b = new Large ();
973                 c = new Large ();
974                 d = new Large ();
975                 a.populate ();
976                 b.populate ();
977                 c.populate ();
978                 d.populate ();
979                 if (large_struct_test (a, b, c, d))
980                         return 2;
981                 return 0;
982         }
983
984         public static unsafe int test_0_pin_string () {
985                 string x = "xxx";
986                 fixed (char *c = x) {
987                         if (*c != 'x')
988                                 return 1;
989                 }
990                 return 0;
991         }
992         
993         public static int my_flags;
994         public static int test_0_and_cmp_static ()
995         {
996                 
997                 /* various forms of test [mem], imm */
998                 
999                 my_flags = 0x01020304;
1000                 
1001                 if ((my_flags & 0x01020304) == 0)
1002                         return 1;
1003                 
1004                 if ((my_flags & 0x00000304) == 0)
1005                         return 2;
1006                 
1007                 if ((my_flags & 0x00000004) == 0)
1008                         return 3;
1009                 
1010                 if ((my_flags & 0x00000300) == 0)
1011                         return 4;
1012                 
1013                 if ((my_flags & 0x00020000) == 0)
1014                         return 5;
1015                 
1016                 if ((my_flags & 0x01000000) == 0)
1017                         return 6;
1018                 
1019                 return 0;
1020         }
1021         
1022         static byte b;
1023         public static int test_0_byte_compares ()
1024         {
1025                 b = 0xff;
1026                 if (b == -1)
1027                         return 1;
1028                 b = 0;
1029                 if (!(b < System.Byte.MaxValue))
1030                         return 2;
1031                 
1032                 if (!(b <= System.Byte.MaxValue))
1033                         return 3;
1034                 
1035                 return 0;
1036         }
1037
1038         public static int test_71_long_shift_right () {
1039                 ulong value = 38654838087;
1040                 int x = 0;
1041                 byte [] buffer = new byte [1];
1042                 buffer [x] = ((byte)(value >> x));
1043                 return buffer [x];
1044         }
1045         
1046         static long x;
1047         public static int test_0_addsub_mem ()
1048         {
1049                 x = 0;
1050                 x += 5;
1051                 
1052                 if (x != 5)
1053                         return 1;
1054                 
1055                 x -= 10;
1056                 
1057                 if (x != -5)
1058                         return 2;
1059                 
1060                 return 0;
1061         }
1062         
1063         static ulong y;
1064         public static int test_0_sh32_mem ()
1065         {
1066                 y = 0x0102130405060708;
1067                 y >>= 32;
1068                 
1069                 if (y != 0x01021304)
1070                         return 1;
1071                 
1072                 y = 0x0102130405060708;
1073                 y <<= 32;
1074                 
1075                 if (y != 0x0506070800000000)
1076                         return 2;
1077                 
1078                 x = 0x0102130405060708;
1079                 x <<= 32;
1080                 
1081                 if (x != 0x0506070800000000)
1082                         return 2;
1083                 
1084                 return 0;
1085         }
1086
1087
1088         static uint dum_de_dum = 1;
1089         public static int test_0_long_arg_opt ()
1090         {
1091                 return Foo (0x1234567887654321, dum_de_dum);
1092         }
1093         
1094         static int Foo (ulong x, ulong y)
1095         {
1096                 if (x != 0x1234567887654321)
1097                         return 1;
1098                 
1099                 if (y != 1)
1100                         return 2;
1101                 
1102                 return 0;
1103         }
1104         
1105         public static int test_0_long_ret_opt ()
1106         {
1107                 ulong x = X ();
1108                 if (x != 0x1234567887654321)
1109                         return 1;
1110                 ulong y = Y ();
1111                 if (y != 1)
1112                         return 2;
1113                 
1114                 return 0;
1115         }
1116         
1117         static ulong X ()
1118         {
1119                 return 0x1234567887654321;
1120         }
1121         
1122         static ulong Y ()
1123         {
1124                 return dum_de_dum;
1125         }
1126
1127         /* from bug# 71515 */
1128         static int counter = 0;
1129         static bool WriteStuff () {
1130                 counter = 10;
1131                 return true;
1132         }
1133         public static int test_0_cond_branch_side_effects () {
1134                 counter = 5;
1135                 if (WriteStuff()) {
1136                 }
1137                 if (counter == 10)
1138                         return 0;
1139                 return 1;
1140         }
1141
1142         // bug #74992
1143         public static int arg_only_written (string file_name, int[]
1144 ncells ) {
1145                 if (file_name == null)
1146                         return 1;
1147
1148                 ncells = foo ();
1149                 bar (ncells [0]);
1150
1151                 return 0;
1152         }
1153
1154         public static int[] foo () {
1155                 return new int [3];
1156         }
1157
1158         public static void bar (int i) {
1159         }
1160         
1161
1162         public static int test_0_arg_only_written ()
1163         {
1164                 return arg_only_written ("md.in", null);
1165         }               
1166
1167         static long position = 0;
1168
1169         public static int test_4_static_inc_long () {
1170
1171                 int count = 4;
1172
1173                 position = 0;
1174
1175                 position += count;
1176
1177                 return (int)position;
1178         }
1179
1180         struct FooStruct {
1181
1182                 public FooStruct (long l) {
1183                 }
1184         }
1185
1186         public static int test_0_calls_opcode_emulation () {
1187                 // Test that emulated opcodes do not clobber arguments already in
1188                 // out registers
1189                 checked {
1190                         long val = 10000;
1191                         new FooStruct (val * 10000);
1192                 }
1193                 return 0;
1194         }
1195
1196         public static int test_0_intrins_string_length () {
1197                 string s = "ABC";
1198
1199                 return (s.Length == 3) ? 0 : 1;
1200         }
1201
1202         public static int test_0_intrins_string_chars () {
1203                 string s = "ABC";
1204
1205                 return (s [0] == 'A' && s [1] == 'B' && s [2] == 'C') ? 0 : 1;
1206         }
1207
1208         public static int test_0_intrins_object_gettype () {
1209                 object o = 1;
1210
1211                 return (o.GetType () == typeof (int)) ? 0 : 1;
1212         }
1213
1214         public static int test_0_intrins_object_gethashcode () {
1215                 object o = new Object ();
1216
1217                 return (o.GetHashCode () == o.GetHashCode ()) ? 0 : 1;
1218         }
1219
1220         class FooClass {
1221         }
1222
1223         public static int test_0_intrins_object_ctor () {
1224                 object o = new FooClass ();
1225
1226                 return (o != null) ? 0 : 1;
1227         }
1228
1229         public static int test_0_intrins_array_rank () {
1230                 int[,] a = new int [10, 10];
1231
1232                 return (a.Rank == 2) ? 0 : 1;
1233         }
1234
1235         public static int test_0_intrins_array_length () {
1236                 int[,] a = new int [10, 10];
1237                 Array a2 = a;
1238
1239                 return (a2.Length == 100) ? 0 : 1;
1240         }
1241
1242         public static int test_0_intrins_runtimehelpers_offset_to_string_data () {
1243                 int i = RuntimeHelpers.OffsetToStringData;
1244                 
1245                 return i - i;
1246         }
1247
1248         public static int test_0_intrins_string_setchar () {
1249                 StringBuilder sb = new StringBuilder ("ABC");
1250
1251                 sb [1] = 'D';
1252
1253                 return sb.ToString () == "ADC" ? 0 : 1;
1254         }
1255
1256         public class Bar {
1257                 bool allowLocation = true;
1258         Foo f = new Foo ();     
1259         }
1260
1261         public static int test_0_regress_78990_unaligned_structs () {
1262                 new Bar ();
1263
1264                 return 0;
1265         }
1266
1267         public static unsafe int test_97_negative_index () {
1268                 char[] arr = new char[] {'a', 'b'};
1269                 fixed (char *p = arr) {
1270                         char *i = p + 2;
1271                         char a = i[-2];
1272                         return a;
1273                 }
1274         }
1275
1276         /* bug #82281 */
1277         public static int test_0_unsigned_right_shift_imm0 () {
1278                 uint temp = 0;
1279                 byte[] data = new byte[256];
1280                 for (int i = 0; i < 1; i ++)
1281                         temp = (uint)(data[temp >> 24] | data[temp >> 0]);
1282                 return 0;
1283         }
1284
1285         class Foo2 {
1286                 public virtual int foo () {
1287                         return 0;
1288                 }
1289         }
1290
1291         sealed class Bar2 : Foo2 {
1292                 public override int foo () {
1293                         return 0;
1294                 }
1295         }
1296
1297         public static int test_0_abcrem_check_this_removal () {
1298                 Bar2 b = new Bar2 ();
1299
1300                 // The check_this generated here by the JIT should be removed
1301                 b.foo ();
1302
1303                 return 0;
1304         }
1305
1306         static int invoke_twice (Bar2 b) {
1307                 b.foo ();
1308                 // The check_this generated here by the JIT should be removed
1309                 b.foo ();
1310
1311                 return 0;
1312         }
1313
1314         public static int test_0_abcrem_check_this_removal2 () {
1315                 Bar2 b = new Bar2 ();
1316
1317                 invoke_twice (b);
1318
1319                 return 0;
1320         }
1321
1322         /* #346563 */
1323         public static int test_0_array_access_64_bit () {
1324                 int[] arr2 = new int [10];
1325                 for (int i = 0; i < 10; ++i)
1326                         arr2 [i] = i;
1327                 string s = "ABCDEFGH";
1328
1329                 byte[] arr = new byte [4];
1330                 arr [0] = 252;
1331                 arr [1] = 255;
1332                 arr [2] = 255;
1333                 arr [3] = 255;
1334
1335                 int len = arr [0] | (arr [1] << 8) | (arr [2] << 16) | (arr [3] << 24);
1336                 int len2 = - (len + 2);
1337
1338                 // Test array and string access with a 32 bit value whose upper 32 bits are
1339                 // undefined
1340                 // len2 = 3
1341                 if (arr2 [len2] != 2)
1342                         return 1;
1343                 if (s [len2] != 'C')
1344                         return 2;
1345                 return 0;
1346         }
1347
1348         public static float return_float () {
1349                 return 1.4e-45f;
1350         }
1351
1352         public static int test_0_float_return_spill () {
1353                 // The return value of return_float () is spilled because of the
1354                 // boxing call
1355                 object o = return_float ();
1356                 float f = return_float ();
1357                 return (float)o == f ? 0 : 1;
1358         }
1359
1360         class R4Holder {
1361                 public static float pi = 3.14f;
1362
1363                 public float float_field;
1364         }
1365
1366         public static int test_0_ldsfld_soft_float () {
1367                 if (R4Holder.pi == 3.14f)
1368                         return 0;
1369                 else
1370                         return 1;
1371         }
1372
1373         public static int test_0_ldfld_stfld_soft_float () {
1374                 R4Holder h = new R4Holder ();
1375                 h.float_field = 3.14f;
1376
1377                 if (h.float_field == 3.14f)
1378                         return 0;
1379                 else
1380                         return 1;
1381         }
1382
1383         class R4HolderRemote : MarshalByRefObject {
1384                 public static float pi = 3.14f;
1385
1386                 public float float_field;
1387         }
1388
1389         public static int test_0_ldfld_stfld_soft_float_remote () {
1390                 R4HolderRemote h = new R4HolderRemote ();
1391                 h.float_field = 3.14f;
1392
1393                 if (h.float_field == 3.14f)
1394                         return 0;
1395                 else
1396                         return 1;
1397         }
1398
1399         public static int test_0_locals_soft_float () {
1400                 float f = 0.0f;
1401                 
1402                 f = 3.14f;
1403
1404                 if (f == 3.14f)
1405                         return 0;
1406                 else
1407                         return 1;
1408         }
1409
1410         struct AStruct2 {
1411                 public int i;
1412                 public int j;
1413         }
1414
1415         static float pass_vtype_return_float (AStruct2 s) {
1416                 return s.i + s.j == 6 ? 1.0f : -1.0f;
1417         }
1418
1419         public static int test_0_vtype_arg_soft_float () {
1420                 return pass_vtype_return_float (new AStruct2 () { i = 2, j = 4 }) > 0.0 ? 0 : 1;
1421         }
1422
1423         static int range_check_strlen (int i, string s) {
1424                 if (i < 0 || i > s.Length)
1425                         return 1;
1426                 else
1427                         return 0;
1428         }
1429                 
1430         public static int test_0_range_check_opt () {
1431                 if (range_check_strlen (0, "A") != 0)
1432                         return 1;
1433                 if (range_check_strlen (1, "A") != 0)
1434                         return 2;
1435                 if (range_check_strlen (2, "A") != 1)
1436                         return 3;
1437                 if (range_check_strlen (-100, "A") != 1)
1438                         return 4;
1439                 return 0;
1440         }
1441
1442         static int test_0_array_get_set_soft_float () {
1443                 float[,] arr = new float [2, 2];
1444                 arr [0, 0] = 256f;
1445                 return arr [0, 0] == 256f ? 0 : 1;
1446         }
1447
1448         //repro for #506915
1449         struct Bug506915 { public int val; }
1450         static int test_2_ldobj_stobj_optization ()
1451         {
1452                 int i = 99;
1453                 var a = new Bug506915 ();
1454                 var b = new Bug506915 ();
1455                 if (i.GetHashCode () == 99)
1456                         i = 44;
1457                 var array = new Bug506915 [2];
1458                 array [0].val = 2;
1459                 array [1] = (i == 0) ? a : array [0];
1460                 
1461                 return array [1].val;
1462         }
1463
1464         /* mcs can't compile this (#646744) */
1465 #if FALSE
1466         static void InitMe (out Gamma noMercyWithTheStack) {
1467                 noMercyWithTheStack = new Gamma ();
1468         }
1469
1470         static int FunNoInline () {
1471                 int x = 99;
1472                 if (x > 344 && x < 22)
1473                         return 333;
1474                 return x;
1475         }
1476
1477         static float DoNothingButDontInline (float a, int b) {
1478                 if (b > 0)
1479                         return a;
1480                 else if (b < 0 && b > 10)
1481                         return 444.0f;
1482                 return a;
1483         }
1484
1485         /*
1486          * The local register allocator emits loadr8_membase and storer8_membase
1487          * to do spilling. This code is generated after mono_arch_lowering_pass so
1488          * mono_arch_output_basic_block must know how to deal with big offsets.
1489          * This only happens because the call in middle forces the temp for "(float)obj"
1490          * to be spilled.
1491         */
1492         public static int test_0_float_load_and_store_with_big_offset ()
1493         {
1494                 object obj = 1.0f;
1495                 Gamma noMercyWithTheStack;
1496                 float res;
1497
1498                 InitMe (out noMercyWithTheStack);
1499
1500                 res = DoNothingButDontInline ((float)obj, FunNoInline ());
1501
1502                 if (!(res == 1.0f))
1503                         return 1;
1504                 return 0;
1505         }
1506 #endif
1507
1508         struct VTypePhi {
1509                 public int i;
1510         }
1511
1512         static int vtype_phi (VTypePhi v1, VTypePhi v2, bool first) {
1513                 VTypePhi v = first ? v1 : v2;
1514
1515                 return v.i;
1516         }
1517
1518         static int test_0_vtype_phi ()
1519         {
1520                 VTypePhi v1 = new VTypePhi () { i = 1 };
1521                 VTypePhi v2 = new VTypePhi () { i = 2 };
1522
1523                 if (vtype_phi (v1, v2, true) != 1)
1524                         return 1;
1525                 if (vtype_phi (v1, v2, false) != 2)
1526                         return 2;
1527
1528                 return 0;
1529         }
1530
1531         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1532         static void UseValue (int index)
1533         {
1534         }
1535
1536         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1537         static bool IsFalse ()
1538         {
1539                 return false;
1540         }
1541
1542         static int test_0_llvm_moving_faulting_loads ()
1543         {
1544                 int[] indexes = null;
1545
1546                 if (IsFalse ()) {
1547                         indexes = new int[0];
1548                 }
1549                         
1550                 while (IsFalse ()) {
1551                         UseValue (indexes[0]);
1552                         UseValue (indexes[0]);
1553                 }
1554
1555                 return 0;
1556         }
1557
1558         public static bool flag;
1559
1560         class B {
1561
1562                 internal static B[] d;
1563
1564                 static B () {
1565                         flag = true;
1566                 }
1567         }
1568
1569         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1570         static int regress_679467_inner () {
1571                 if (flag == true)
1572                         return 1;
1573                 var o = B.d;
1574                 var o2 = B.d;
1575                 return 0;
1576         }
1577
1578         /*
1579          * FIXME: This fails with AOT #703317.
1580          */
1581         /*
1582         static int test_0_multiple_cctor_calls_regress_679467 () {
1583                 flag = false;
1584                 return regress_679467_inner ();
1585         }
1586         */
1587
1588         static int test_0_char_ctor () {
1589                 string s = new String (new char[] { 'A', 'B' }, 0, 1);
1590                 return 0;
1591         }
1592
1593         static object mInstance = null;
1594
1595         [MethodImpl(MethodImplOptions.Synchronized)]
1596         public static object getInstance() {
1597                 if (mInstance == null)
1598                         mInstance = new object();
1599                 return mInstance;
1600         }
1601
1602         static int test_0_synchronized () {
1603                 getInstance ();
1604                 return 0;
1605         }
1606
1607         struct BStruct {
1608                 public Type t;
1609         }
1610
1611         class Del<T> {
1612                 public static BStruct foo () {
1613                         return new BStruct () { t = typeof (T) };
1614                 }
1615         }
1616
1617         delegate BStruct ADelegate ();
1618
1619         static int test_0_regress_10601 () {
1620                 var act = (ADelegate)(Del<string>.foo);
1621                 BStruct b = act ();
1622                 if (b.t != typeof (string))
1623                         return 1;
1624                 return 0;
1625         }
1626
1627         static int test_0_regress_11058 () {
1628                 int foo = -252674008;
1629                 int foo2 = (int)(foo ^ 0xF0F0F0F0); // = 28888
1630                 var arr = new byte[foo2].Length;
1631                 return 0;
1632         }
1633
1634         public static void do_throw () {
1635                 throw new Exception ();
1636         }
1637
1638         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1639         static void empty () {
1640         }
1641
1642         // #11297
1643         public static int test_0_llvm_inline_throw () {
1644                 try {
1645                         empty ();
1646                 } catch (Exception) {
1647                         do_throw ();
1648                 }
1649
1650                 return 0;
1651         }
1652
1653         enum ByteEnum : byte {
1654         Zero = 0
1655     }
1656
1657     struct BugStruct {
1658         public ByteEnum f1;
1659         public ByteEnum f2;
1660         public ByteEnum f3;
1661         public byte f4;
1662         public byte f5;
1663         public byte f6;
1664         public byte f7;
1665     }
1666
1667         public static int test_0_14217 () {
1668                 t_14217_inner (new BugStruct ());
1669                 return 0;
1670         }
1671
1672         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1673         static void t_14217_inner (BugStruct bug) {
1674     }
1675
1676         [StructLayout(LayoutKind.Sequential)]
1677         public struct EmptyStruct {
1678         }
1679
1680         class EmptyClass {
1681                 public static EmptyStruct s;
1682         }
1683
1684         // #20349
1685         static int test_0_empty_struct_as_static () {
1686                 var s = EmptyClass.s;
1687                 return 0;
1688         }
1689
1690         // #25487
1691         static int test_0_int_to_r4 () {
1692                 return int_to_r4_inner (255);
1693         }
1694
1695         static int int_to_r4_inner (int value1) {
1696                 int sub = -value1;
1697                 float mult = sub * 1f;
1698                 if (mult != -255.0f)
1699                         return 1;
1700                 else
1701                         return 0;
1702         }
1703
1704         struct HFA4D {
1705                 public double a, b, c, d;
1706         }
1707
1708         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) {
1709                 return s.a + s.b + s.c + s.d;
1710         }
1711
1712         static int test_0_arm64_hfa_on_stack () {
1713                 var s = new HFA4D () { a = 1.0, b = 2.0, c = 3.0, d = 4.0 };
1714                 var res = arm64_hfa_on_stack_inner (1, 2, 3, 4, 5, 6, 7, 8, s);
1715                 return res == 10.0 ? 0 : 1;
1716         }
1717
1718         class MulOvfClass {
1719                 [MethodImplAttribute (MethodImplOptions.NoInlining)]
1720                 public unsafe void EncodeIntoBuffer(char* value, int valueLength, char* buffer, int bufferLength) {
1721                 }
1722         }
1723
1724         static unsafe int test_0_mul_ovf_regress_36052 () {
1725                 var p = new MulOvfClass ();
1726
1727                 string typeName = typeof(int).Name;
1728                 int bufferSize = 45;
1729
1730                 fixed (char* value = typeName) {
1731                         char* buffer = stackalloc char[bufferSize];
1732                         p.EncodeIntoBuffer(value, typeName.Length, buffer, bufferSize);
1733                 }
1734                 return 0;
1735         }
1736
1737         struct Struct16 {
1738                 public int a, b, c, d;
1739         }
1740
1741         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1742         static int pass_struct16 (object o0, object o2, object o3, object o4, object o5, object o6, object o7, Struct16 o8) {
1743                 // This disables LLVM
1744                 try {
1745                 } catch {
1746                 }
1747                 return o8.a;
1748         }
1749
1750         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1751         static int pass_struct16 (object o0, object o2, object o3, object o6, object o7, Struct16 o8) {
1752                 return pass_struct16 (o0, o2, null, o3, null, o6, o7, o8);
1753         }
1754
1755         public static int test_42_pass_16byte_struct_split () {
1756                 return pass_struct16 (null, null, null, null, null, new Struct16 () { a = 42 });
1757         }
1758
1759         public interface IComparer2
1760         {
1761                 Type foo<T> ();
1762         }
1763
1764         public class AClass : IComparer2 {
1765                 public Type foo<T> () {
1766                         return typeof(T);
1767                 }
1768         }
1769
1770         public static int test_0_delegate_to_virtual_generic_on_ifaces () {
1771                 IComparer2 c = new AClass ();
1772
1773                 Func<Type> f = c.foo<string>;
1774                 return f () == typeof(string) ? 0 : 1;
1775         }
1776
1777         public enum ByteEnum2 : byte {
1778                 High = 142
1779         }
1780
1781         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1782         public static int enum_arg_zero_extend (ByteEnum2 b) {
1783                 return (int)b;
1784         }
1785
1786         public static int test_142_byte_enum_arg_zero_extend () {
1787                 return enum_arg_zero_extend (ByteEnum2.High);
1788         }
1789
1790         enum Mine { One, Two }
1791
1792         public static int test_0_enum_gethashcode_opt () {
1793                 int sum = 0;
1794         for (int i = 0; i < 1000000; ++i)
1795                         sum += Mine.Two.GetHashCode();
1796
1797         return 0;
1798     }
1799 }
1800
1801 #if __MOBILE__
1802 }
1803 #endif