Merge branch 'master' of github.com:mono/mono
[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                 if (counter == 10)
1083                         return 0;
1084                 return 1;
1085         }
1086
1087         // bug #74992
1088         public static int arg_only_written (string file_name, int[]
1089 ncells ) {
1090                 if (file_name == null)
1091                         return 1;
1092
1093                 ncells = foo ();
1094                 bar (ncells [0]);
1095
1096                 return 0;
1097         }
1098
1099         public static int[] foo () {
1100                 return new int [3];
1101         }
1102
1103         public static void bar (int i) {
1104         }
1105         
1106
1107         public static int test_0_arg_only_written ()
1108         {
1109                 return arg_only_written ("md.in", null);
1110         }               
1111
1112         static long position = 0;
1113
1114         public static int test_4_static_inc_long () {
1115
1116                 int count = 4;
1117
1118                 position = 0;
1119
1120                 position += count;
1121
1122                 return (int)position;
1123         }
1124
1125         struct FooStruct {
1126
1127                 public FooStruct (long l) {
1128                 }
1129         }
1130
1131         public static int test_0_calls_opcode_emulation () {
1132                 // Test that emulated opcodes do not clobber arguments already in
1133                 // out registers
1134                 checked {
1135                         long val = 10000;
1136                         new FooStruct (val * 10000);
1137                 }
1138                 return 0;
1139         }
1140
1141         public static int test_0_intrins_string_length () {
1142                 string s = "ABC";
1143
1144                 return (s.Length == 3) ? 0 : 1;
1145         }
1146
1147         public static int test_0_intrins_string_chars () {
1148                 string s = "ABC";
1149
1150                 return (s [0] == 'A' && s [1] == 'B' && s [2] == 'C') ? 0 : 1;
1151         }
1152
1153         public static int test_0_intrins_object_gettype () {
1154                 object o = 1;
1155
1156                 return (o.GetType () == typeof (int)) ? 0 : 1;
1157         }
1158
1159         public static int test_0_intrins_object_gethashcode () {
1160                 object o = new Object ();
1161
1162                 return (o.GetHashCode () == o.GetHashCode ()) ? 0 : 1;
1163         }
1164
1165         class FooClass {
1166         }
1167
1168         public static int test_0_intrins_object_ctor () {
1169                 object o = new FooClass ();
1170
1171                 return (o != null) ? 0 : 1;
1172         }
1173
1174         public static int test_0_intrins_array_rank () {
1175                 int[,] a = new int [10, 10];
1176
1177                 return (a.Rank == 2) ? 0 : 1;
1178         }
1179
1180         public static int test_0_intrins_array_length () {
1181                 int[,] a = new int [10, 10];
1182                 Array a2 = a;
1183
1184                 return (a2.Length == 100) ? 0 : 1;
1185         }
1186
1187         public static int test_0_intrins_runtimehelpers_offset_to_string_data () {
1188                 int i = RuntimeHelpers.OffsetToStringData;
1189                 
1190                 return i - i;
1191         }
1192
1193         public static int test_0_intrins_string_setchar () {
1194                 StringBuilder sb = new StringBuilder ("ABC");
1195
1196                 sb [1] = 'D';
1197
1198                 return sb.ToString () == "ADC" ? 0 : 1;
1199         }
1200
1201         public class Bar {
1202                 bool allowLocation = true;
1203         Foo f = new Foo ();     
1204         }
1205
1206         public static int test_0_regress_78990_unaligned_structs () {
1207                 new Bar ();
1208
1209                 return 0;
1210         }
1211
1212         public static unsafe int test_97_negative_index () {
1213                 char[] arr = new char[] {'a', 'b'};
1214                 fixed (char *p = arr) {
1215                         char *i = p + 2;
1216                         char a = i[-2];
1217                         return a;
1218                 }
1219         }
1220
1221         /* bug #82281 */
1222         public static int test_0_unsigned_right_shift_imm0 () {
1223                 uint temp = 0;
1224                 byte[] data = new byte[256];
1225                 for (int i = 0; i < 1; i ++)
1226                         temp = (uint)(data[temp >> 24] | data[temp >> 0]);
1227                 return 0;
1228         }
1229
1230         class Foo2 {
1231                 public virtual int foo () {
1232                         return 0;
1233                 }
1234         }
1235
1236         sealed class Bar2 : Foo2 {
1237                 public override int foo () {
1238                         return 0;
1239                 }
1240         }
1241
1242         public static int test_0_abcrem_check_this_removal () {
1243                 Bar2 b = new Bar2 ();
1244
1245                 // The check_this generated here by the JIT should be removed
1246                 b.foo ();
1247
1248                 return 0;
1249         }
1250
1251         static int invoke_twice (Bar2 b) {
1252                 b.foo ();
1253                 // The check_this generated here by the JIT should be removed
1254                 b.foo ();
1255
1256                 return 0;
1257         }
1258
1259         public static int test_0_abcrem_check_this_removal2 () {
1260                 Bar2 b = new Bar2 ();
1261
1262                 invoke_twice (b);
1263
1264                 return 0;
1265         }
1266
1267         /* #346563 */
1268         public static int test_0_array_access_64_bit () {
1269                 int[] arr2 = new int [10];
1270                 for (int i = 0; i < 10; ++i)
1271                         arr2 [i] = i;
1272                 string s = "ABCDEFGH";
1273
1274                 byte[] arr = new byte [4];
1275                 arr [0] = 252;
1276                 arr [1] = 255;
1277                 arr [2] = 255;
1278                 arr [3] = 255;
1279
1280                 int len = arr [0] | (arr [1] << 8) | (arr [2] << 16) | (arr [3] << 24);
1281                 int len2 = - (len + 2);
1282
1283                 // Test array and string access with a 32 bit value whose upper 32 bits are
1284                 // undefined
1285                 // len2 = 3
1286                 if (arr2 [len2] != 2)
1287                         return 1;
1288                 if (s [len2] != 'C')
1289                         return 2;
1290                 return 0;
1291         }
1292
1293         public static float return_float () {
1294                 return 1.4e-45f;
1295         }
1296
1297         public static int test_0_float_return_spill () {
1298                 // The return value of return_float () is spilled because of the
1299                 // boxing call
1300                 object o = return_float ();
1301                 float f = return_float ();
1302                 return (float)o == f ? 0 : 1;
1303         }
1304
1305         class R4Holder {
1306                 public static float pi = 3.14f;
1307
1308                 public float float_field;
1309         }
1310
1311         public static int test_0_ldsfld_soft_float () {
1312                 if (R4Holder.pi == 3.14f)
1313                         return 0;
1314                 else
1315                         return 1;
1316         }
1317
1318         public static int test_0_ldfld_stfld_soft_float () {
1319                 R4Holder h = new R4Holder ();
1320                 h.float_field = 3.14f;
1321
1322                 if (h.float_field == 3.14f)
1323                         return 0;
1324                 else
1325                         return 1;
1326         }
1327
1328         class R4HolderRemote : MarshalByRefObject {
1329                 public static float pi = 3.14f;
1330
1331                 public float float_field;
1332         }
1333
1334         public static int test_0_ldfld_stfld_soft_float_remote () {
1335                 R4HolderRemote h = new R4HolderRemote ();
1336                 h.float_field = 3.14f;
1337
1338                 if (h.float_field == 3.14f)
1339                         return 0;
1340                 else
1341                         return 1;
1342         }
1343
1344         public static int test_0_locals_soft_float () {
1345                 float f = 0.0f;
1346                 
1347                 f = 3.14f;
1348
1349                 if (f == 3.14f)
1350                         return 0;
1351                 else
1352                         return 1;
1353         }
1354
1355         struct AStruct2 {
1356                 public int i;
1357                 public int j;
1358         }
1359
1360         static float pass_vtype_return_float (AStruct2 s) {
1361                 return s.i + s.j == 6 ? 1.0f : -1.0f;
1362         }
1363
1364         public static int test_0_vtype_arg_soft_float () {
1365                 return pass_vtype_return_float (new AStruct2 () { i = 2, j = 4 }) > 0.0 ? 0 : 1;
1366         }
1367
1368         static int range_check_strlen (int i, string s) {
1369                 if (i < 0 || i > s.Length)
1370                         return 1;
1371                 else
1372                         return 0;
1373         }
1374                 
1375         public static int test_0_range_check_opt () {
1376                 if (range_check_strlen (0, "A") != 0)
1377                         return 1;
1378                 if (range_check_strlen (1, "A") != 0)
1379                         return 2;
1380                 if (range_check_strlen (2, "A") != 1)
1381                         return 3;
1382                 if (range_check_strlen (-100, "A") != 1)
1383                         return 4;
1384                 return 0;
1385         }
1386
1387         static int test_0_array_get_set_soft_float () {
1388                 float[,] arr = new float [2, 2];
1389                 arr [0, 0] = 256f;
1390                 return arr [0, 0] == 256f ? 0 : 1;
1391         }
1392
1393         //repro for #506915
1394         struct Bug506915 { public int val; }
1395         static int test_2_ldobj_stobj_optization ()
1396         {
1397                 int i = 99;
1398                 var a = new Bug506915 ();
1399                 var b = new Bug506915 ();
1400                 if (i.GetHashCode () == 99)
1401                         i = 44;
1402                 var array = new Bug506915 [2];
1403                 array [0].val = 2;
1404                 array [1] = (i == 0) ? a : array [0];
1405                 
1406                 return array [1].val;
1407         }
1408
1409         /* mcs can't compile this (#646744) */
1410 #if FALSE
1411         static void InitMe (out Gamma noMercyWithTheStack) {
1412                 noMercyWithTheStack = new Gamma ();
1413         }
1414
1415         static int FunNoInline () {
1416                 int x = 99;
1417                 if (x > 344 && x < 22)
1418                         return 333;
1419                 return x;
1420         }
1421
1422         static float DoNothingButDontInline (float a, int b) {
1423                 if (b > 0)
1424                         return a;
1425                 else if (b < 0 && b > 10)
1426                         return 444.0f;
1427                 return a;
1428         }
1429
1430         /*
1431          * The local register allocator emits loadr8_membase and storer8_membase
1432          * to do spilling. This code is generated after mono_arch_lowering_pass so
1433          * mono_arch_output_basic_block must know how to deal with big offsets.
1434          * This only happens because the call in middle forces the temp for "(float)obj"
1435          * to be spilled.
1436         */
1437         public static int test_0_float_load_and_store_with_big_offset ()
1438         {
1439                 object obj = 1.0f;
1440                 Gamma noMercyWithTheStack;
1441                 float res;
1442
1443                 InitMe (out noMercyWithTheStack);
1444
1445                 res = DoNothingButDontInline ((float)obj, FunNoInline ());
1446
1447                 if (!(res == 1.0f))
1448                         return 1;
1449                 return 0;
1450         }
1451 #endif
1452
1453         struct VTypePhi {
1454                 public int i;
1455         }
1456
1457         static int vtype_phi (VTypePhi v1, VTypePhi v2, bool first) {
1458                 VTypePhi v = first ? v1 : v2;
1459
1460                 return v.i;
1461         }
1462
1463         static int test_0_vtype_phi ()
1464         {
1465                 VTypePhi v1 = new VTypePhi () { i = 1 };
1466                 VTypePhi v2 = new VTypePhi () { i = 2 };
1467
1468                 if (vtype_phi (v1, v2, true) != 1)
1469                         return 1;
1470                 if (vtype_phi (v1, v2, false) != 2)
1471                         return 2;
1472
1473                 return 0;
1474         }
1475
1476         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1477         static void UseValue (int index)
1478         {
1479         }
1480
1481         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1482         static bool IsFalse ()
1483         {
1484                 return false;
1485         }
1486
1487         static int test_0_llvm_moving_faulting_loads ()
1488         {
1489                 int[] indexes = null;
1490
1491                 if (IsFalse ()) {
1492                         indexes = new int[0];
1493                 }
1494                         
1495                 while (IsFalse ()) {
1496                         UseValue (indexes[0]);
1497                         UseValue (indexes[0]);
1498                 }
1499
1500                 return 0;
1501         }
1502
1503         public static bool flag;
1504
1505         class B {
1506
1507                 internal static B[] d;
1508
1509                 static B () {
1510                         flag = true;
1511                 }
1512         }
1513
1514         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1515         static int regress_679467_inner () {
1516                 if (flag == true)
1517                         return 1;
1518                 var o = B.d;
1519                 var o2 = B.d;
1520                 return 0;
1521         }
1522
1523         static int test_0_multiple_cctor_calls_regress_679467 () {
1524                 flag = false;
1525                 return regress_679467_inner ();
1526         }
1527 }
1528