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