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