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