[llvm] Fix passing vtypes received by value as arguments, they need to be copied...
[mono-project.git] / mono / mini / objects.cs
blob6477ee96e69e250296873f428a21ef2a25330ab0
1 using System;
2 using System.Text;
3 using System.Reflection;
4 using System.Collections.Generic;
5 using System.Runtime.InteropServices;
6 using System.Runtime.CompilerServices;
8 /*
9 * Regression tests for the mono JIT.
11 * Each test needs to be of the form:
13 * static int test_<result>_<name> ();
15 * where <result> is an integer (the value that needs to be returned by
16 * the method to make it pass.
17 * <name> is a user-displayed name used to identify the test.
19 * The tests can be driven in two ways:
20 * *) running the program directly: Main() uses reflection to find and invoke
21 * the test methods (this is useful mostly to check that the tests are correct)
22 * *) with the --regression switch of the jit (this is the preferred way since
23 * all the tests will be run with optimizations on and off)
25 * The reflection logic could be moved to a .dll since we need at least another
26 * regression test file written in IL code to have better control on how
27 * the IL code looks.
30 #if __MOBILE__
31 namespace ObjectTests
33 #endif
35 struct Simple {
36 public int a;
37 public byte b;
38 public short c;
39 public long d;
42 struct Small {
43 public byte b1;
44 public byte b2;
47 // Size=2, Align=1
48 struct Foo {
49 bool b1;
50 bool b2;
53 struct Large {
54 int one;
55 int two;
56 long three;
57 long four;
58 int five;
59 long six;
60 int seven;
61 long eight;
62 long nine;
63 long ten;
65 public void populate ()
67 one = 1; two = 2;
68 three = 3; four = 4;
69 five = 5; six = 6;
70 seven = 7; eight = 8;
71 nine = 9; ten = 10;
73 public bool check ()
75 return one == 1 && two == 2 &&
76 three == 3 && four == 4 &&
77 five == 5 && six == 6 &&
78 seven == 7 && eight == 8 &&
79 nine == 9 && ten == 10;
83 class Sample {
84 public int a;
85 public Sample (int v) {
86 a = v;
90 [StructLayout ( LayoutKind.Explicit )]
91 struct StructWithBigOffsets {
92 [ FieldOffset(10000) ] public byte b;
93 [ FieldOffset(10001) ] public sbyte sb;
94 [ FieldOffset(11000) ] public short s;
95 [ FieldOffset(11002) ] public ushort us;
96 [ FieldOffset(12000) ] public uint i;
97 [ FieldOffset(12004) ] public int si;
98 [ FieldOffset(13000) ] public long l;
99 [ FieldOffset(14000) ] public float f;
100 [ FieldOffset(15000) ] public double d;
103 enum SampleEnum {
109 struct Alpha {
110 public long a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v;
113 struct Beta {
114 public Alpha a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v;
117 struct Gamma {
118 public Beta a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v;
121 class Tests {
123 #if !__MOBILE__
124 public static int Main (string[] args) {
125 return TestDriver.RunTests (typeof (Tests), args);
127 #endif
129 public static int test_0_return () {
130 Simple s;
131 s.a = 1;
132 s.b = 2;
133 s.c = (short)(s.a + s.b);
134 s.d = 4;
135 return s.a - 1;
138 public static int test_0_string_access () {
139 string s = "Hello";
140 if (s [1] != 'e')
141 return 1;
142 return 0;
145 public static int test_0_string_virtual_call () {
146 string s = "Hello";
147 string s2 = s.ToString ();
148 if (s2 [1] != 'e')
149 return 1;
150 return 0;
153 public static int test_0_iface_call () {
154 string s = "Hello";
155 object o = ((ICloneable)s).Clone ();
156 return 0;
159 public static int test_5_newobj () {
160 Sample s = new Sample (5);
161 return s.a;
164 public static int test_4_box () {
165 object obj = 4;
166 return (int)obj;
169 public static int test_0_enum_unbox () {
170 SampleEnum x = SampleEnum.A;
171 object o = x;
173 int res = 1;
175 res = (int)o;
177 return res;
180 static Simple get_simple (int v) {
181 Simple r = new Simple ();
182 r.a = v;
183 r.b = (byte)(v + 1);
184 r.c = (short)(v + 2);
185 r.d = v + 3;
187 return r;
190 public static int test_3_return_struct () {
191 Simple v = get_simple (1);
193 if (v.a != 1)
194 return 0;
195 if (v.b != 2)
196 return 0;
197 if (v.c != 3)
198 return 0;
199 if (v.d != 4)
200 return 0;
201 return 3;
204 public virtual Simple v_get_simple (int v)
206 return get_simple (v);
209 public static int test_2_return_struct_virtual () {
210 Tests t = new Tests ();
211 Simple v = t.v_get_simple (2);
213 if (v.a != 2)
214 return 0;
215 if (v.b != 3)
216 return 0;
217 if (v.c != 4)
218 return 0;
219 if (v.d != 5)
220 return 0;
221 return 2;
224 static int receive_simple (int a, Simple v, int b) {
225 if (v.a != 1)
226 return 1;
227 if (v.b != 2)
228 return 2;
229 if (v.c != 3)
230 return 3;
231 if (v.d != 4)
232 return 4;
233 if (a != 7)
234 return 5;
235 if (b != 9)
236 return 6;
237 return 0;
240 public static int test_5_pass_struct () {
241 Simple v = get_simple (1);
242 if (receive_simple (7, v, 9) != 0)
243 return 0;
244 if (receive_simple (7, get_simple (1), 9) != 0)
245 return 1;
246 return 5;
249 static Simple s_v;
250 public static int test_5_pass_static_struct () {
251 s_v = get_simple (1);
252 if (receive_simple (7, s_v, 9) != 0)
253 return 0;
254 return 5;
257 // Test alignment of small structs
259 static Small get_small (byte v) {
260 Small r = new Small ();
262 r.b1 = v;
263 r.b2 = (byte)(v + 1);
265 return r;
268 static Small return_small (Small s) {
269 return s;
272 static int receive_small (int a, Small v, int b) {
273 if (v.b1 != 1)
274 return 1;
275 if (v.b2 != 2)
276 return 2;
277 return 0;
280 static int receive_small_sparc_many_args (int a, int a2, int a3, int a4, int a5, int a6, Small v, int b) {
281 if (v.b1 != 1)
282 return 1;
283 if (v.b2 != 2)
284 return 2;
285 return 0;
288 public static int test_5_pass_small_struct () {
289 Small v = get_small (1);
290 if (receive_small (7, v, 9) != 0)
291 return 0;
292 if (receive_small (7, get_small (1), 9) != 0)
293 return 1;
294 if (receive_small_sparc_many_args (1, 2, 3, 4, 5, 6, v, 9) != 0)
295 return 2;
296 v = return_small (v);
297 if (v.b1 != 1)
298 return 3;
299 if (v.b2 != 2)
300 return 4;
301 return 5;
304 // 64-bits, 32-bit aligned
305 struct struct1 {
306 public int a;
307 public int b;
310 static int check_struct1(struct1 x) {
311 if (x.a != 1)
312 return 1;
313 if (x.b != 2)
314 return 2;
315 return 0;
318 static int pass_struct1(int a, int b, struct1 x) {
319 if (a != 3)
320 return 3;
321 if (b != 4)
322 return 4;
323 return check_struct1(x);
326 static int pass_struct1(int a, struct1 x) {
327 if (a != 3)
328 return 3;
329 return check_struct1(x);
332 static int pass_struct1(struct1 x) {
333 return check_struct1(x);
336 public static int test_0_struct1_args () {
337 int r;
338 struct1 x;
340 x.a = 1;
341 x.b = 2;
342 if ((r = check_struct1(x)) != 0)
343 return r;
344 if ((r = pass_struct1(x)) != 0)
345 return r + 10;
346 if ((r = pass_struct1(3, x)) != 0)
347 return r + 20;
348 if ((r = pass_struct1(3, 4, x)) != 0)
349 return r + 30;
350 return 0;
353 // 64-bits, 64-bit aligned
354 struct struct2 {
355 public long a;
358 static int check_struct2(struct2 x) {
359 if (x.a != 1)
360 return 1;
361 return 0;
364 static int pass_struct2(int a, int b, int c, struct2 x) {
365 if (a != 3)
366 return 3;
367 if (b != 4)
368 return 4;
369 if (c != 5)
370 return 5;
371 return check_struct2(x);
374 static int pass_struct2(int a, int b, struct2 x) {
375 if (a != 3)
376 return 3;
377 if (b != 4)
378 return 4;
379 return check_struct2(x);
382 static int pass_struct2(int a, struct2 x) {
383 if (a != 3)
384 return 3;
385 return check_struct2(x);
388 static int pass_struct2(struct2 x) {
389 return check_struct2(x);
392 public static int test_0_struct2_args () {
393 int r;
394 struct2 x;
396 x.a = 1;
397 if ((r = check_struct2(x)) != 0)
398 return r;
399 if ((r = pass_struct2(x)) != 0)
400 return r + 10;
401 if ((r = pass_struct2(3, x)) != 0)
402 return r + 20;
403 if ((r = pass_struct2(3, 4, x)) != 0)
404 return r + 30;
405 if ((r = pass_struct2(3, 4, 5, x)) != 0)
406 return r + 40;
407 return 0;
410 // 128 bits
411 struct Struct3 {
412 public long i, j, k, l;
415 static int pass_struct3 (int i, int j, int k, int l, int m, int n, int o, int p, Struct3 s, int q) {
416 if (s.i + s.j + s.k + s.l != 10)
417 return 1;
418 else
419 return 0;
422 public static int test_0_struct3_args () {
423 Struct3 s = new Struct3 ();
424 s.i = 1;
425 s.j = 2;
426 s.k = 3;
427 s.l = 4;
429 return pass_struct3 (1, 2, 3, 4, 5, 6, 7, 8, s, 9);
432 // Struct with unaligned size on 64 bit machines
433 struct Struct4 {
434 public int i, j, k, l, m;
435 public int i1, i2, i3, i4, i5, i6;
438 static int pass_struct4 (Struct4 s) {
439 if (s.i + s.j + s.k + s.l + s.m != 15)
440 return 1;
441 else
442 return 0;
445 public static int test_0_struct4_args () {
446 Struct4 s = new Struct4 ();
447 s.i = 1;
448 s.j = 2;
449 s.k = 3;
450 s.l = 4;
451 s.m = 5;
453 return pass_struct4 (s);
458 struct AStruct {
459 public int i;
461 public AStruct (int i) {
462 this.i = i;
465 public override int GetHashCode () {
466 return i;
470 // Test that vtypes are unboxed during a virtual call
471 public static int test_44_unbox_trampoline () {
472 AStruct s = new AStruct (44);
473 object o = s;
474 return o.GetHashCode ();
477 public static int test_0_unbox_trampoline2 () {
478 int i = 12;
479 object o = i;
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;
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 ();
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;
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;
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;
534 return 0;
537 class TestRegA {
539 long buf_start;
540 int buf_length, buf_offset;
542 public TestRegA () {
543 buf_start = 0;
544 buf_length = 0;
545 buf_offset = 0;
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;
556 return buf_start;
561 public static int test_0_seektest () {
562 TestRegA t = new TestRegA ();
563 return (int)t.Seek (0);
566 class Super : ICloneable {
567 public virtual object Clone () {
568 return null;
571 class Duper: Super {
574 public static int test_0_null_cast () {
575 object o = null;
577 Super s = (Super)o;
579 return 0;
582 public static int test_0_super_cast () {
583 Duper d = new Duper ();
584 Super sup = d;
585 Object o = d;
587 if (!(o is Super))
588 return 1;
589 try {
590 d = (Duper)sup;
591 } catch {
592 return 2;
594 if (!(d is Object))
595 return 3;
596 try {
597 d = (Duper)(object)sup;
598 } catch {
599 return 4;
601 return 0;
604 public static int test_0_super_cast_array () {
605 Duper[] d = new Duper [0];
606 Super[] sup = d;
607 Object[] o = d;
609 if (!(o is Super[]))
610 return 1;
611 try {
612 d = (Duper[])sup;
613 } catch {
614 return 2;
616 if (!(d is Object[]))
617 return 3;
618 try {
619 d = (Duper[])(object[])sup;
620 } catch {
621 return 4;
623 return 0;
626 public static int test_0_multi_array_cast () {
627 Duper[,] d = new Duper [1, 1];
628 object[,] o = d;
630 try {
631 o [0, 0] = new Super ();
632 return 1;
634 catch (ArrayTypeMismatchException) {
637 return 0;
640 public static int test_0_vector_array_cast () {
641 Array arr1 = Array.CreateInstance (typeof (int), new int[] {1}, new int[] {0});
642 Array arr2 = Array.CreateInstance (typeof (int), new int[] {1}, new int[] {10});
643 Array arr5 = Array.CreateInstance (typeof (string), new int[] {1}, new int[] {10});
645 if (arr1.GetType () != typeof (int[]))
646 return 1;
648 if (arr2.GetType () == typeof (int[]))
649 return 2;
651 int[] b;
653 b = (int[])arr1;
655 try {
656 b = (int[])arr2;
657 return 3;
659 catch (InvalidCastException) {
662 if (arr2 is int[])
663 return 4;
664 var as_object_arr = arr5 as object [];
665 if (as_object_arr != null)
666 return 5;
668 int [,] [] arr3 = new int [1, 1] [];
669 object o = arr3;
670 int [,] [] arr4 = (int [,] [])o;
672 return 0;
675 public static int test_0_enum_array_cast () {
676 TypeCode[] tc = new TypeCode [0];
677 object[] oa;
678 ValueType[] vta;
679 int[] inta;
680 Array a = tc;
681 bool ok;
683 if (a is object[])
684 return 1;
685 if (a is ValueType[])
686 return 2;
687 if (a is Enum[])
688 return 3;
689 try {
690 ok = false;
691 oa = (object[])a;
692 } catch {
693 ok = true;
695 if (!ok)
696 return 4;
697 try {
698 ok = false;
699 vta = (ValueType[])a;
700 } catch {
701 ok = true;
703 if (!ok)
704 return 5;
705 try {
706 ok = true;
707 inta = (int[])a;
708 } catch {
709 ok = false;
711 if (!ok)
712 return 6;
713 return 0;
716 public static int test_0_more_cast_corner_cases () {
717 ValueType[] vta = new ValueType [0];
718 Enum[] ea = new Enum [0];
719 Array a = vta;
720 object[] oa;
721 bool ok;
723 if (!(a is object[]))
724 return 1;
725 if (!(a is ValueType[]))
726 return 2;
727 if (a is Enum[])
728 return 3;
729 a = ea;
730 if (!(a is object[]))
731 return 4;
732 if (!(a is ValueType[]))
733 return 5;
734 if (!(a is Enum[]))
735 return 6;
737 try {
738 ok = true;
739 oa = (object[])a;
740 } catch {
741 ok = false;
743 if (!ok)
744 return 7;
746 try {
747 ok = true;
748 oa = (Enum[])a;
749 } catch {
750 ok = false;
752 if (!ok)
753 return 8;
755 try {
756 ok = true;
757 oa = (ValueType[])a;
758 } catch {
759 ok = false;
761 if (!ok)
762 return 9;
764 a = vta;
765 try {
766 ok = true;
767 oa = (object[])a;
768 } catch {
769 ok = false;
771 if (!ok)
772 return 10;
774 try {
775 ok = true;
776 oa = (ValueType[])a;
777 } catch {
778 ok = false;
780 if (!ok)
781 return 11;
783 try {
784 ok = false;
785 vta = (Enum[])a;
786 } catch {
787 ok = true;
789 if (!ok)
790 return 12;
792 object arr = new int [10];
793 if (arr is IList<int?>)
794 return 13;
796 return 0;
799 public static int test_0_cast_iface_array () {
800 object o = new ICloneable [0];
801 object o2 = new Duper [0];
802 object t;
803 bool ok;
805 if (!(o is object[]))
806 return 1;
807 if (!(o2 is ICloneable[]))
808 return 2;
810 try {
811 ok = true;
812 t = (object[])o;
813 } catch {
814 ok = false;
816 if (!ok)
817 return 3;
819 try {
820 ok = true;
821 t = (ICloneable[])o2;
822 } catch {
823 ok = false;
825 if (!ok)
826 return 4;
828 try {
829 ok = true;
830 t = (ICloneable[])o;
831 } catch {
832 ok = false;
834 if (!ok)
835 return 5;
837 if (!(o is ICloneable[]))
838 return 6;
840 /* add tests for interfaces that 'inherit' interfaces */
841 return 0;
844 private static int[] daysmonthleap = { 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
846 private static int AbsoluteDays (int year, int month, int day)
848 int temp = 0, m = 1;
849 int[] days = daysmonthleap;
850 while (m < month)
851 temp += days[m++];
852 return ((day-1) + temp + (365* (year-1)) + ((year-1)/4) - ((year-1)/100) + ((year-1)/400));
855 public static int test_719162_complex_div () {
856 int adays = AbsoluteDays (1970, 1, 1);
857 return adays;
860 delegate int GetIntDel ();
862 static int return4 () {
863 return 4;
866 int return5 () {
867 return 5;
870 public static int test_2_static_delegate () {
871 GetIntDel del = new GetIntDel (return4);
872 int v = del ();
873 if (v != 4)
874 return 0;
875 return 2;
878 public static int test_2_instance_delegate () {
879 Tests t = new Tests ();
880 GetIntDel del = new GetIntDel (t.return5);
881 int v = del ();
882 if (v != 5)
883 return 0;
884 return 2;
887 class InstanceDelegateTest {
888 public int a;
890 public int return_field () {
891 return a;
895 public static int test_2_instance_delegate_with_field () {
896 InstanceDelegateTest t = new InstanceDelegateTest () { a = 1337 };
897 GetIntDel del = new GetIntDel (t.return_field);
898 int v = del ();
899 if (v != 1337)
900 return 0;
901 return 2;
904 interface IFaceVirtualDel {
905 int return_field ();
908 struct VtypeVirtualDelStruct : IFaceVirtualDel {
909 public int f;
910 public int return_field_nonvirt () {
911 return f;
913 public int return_field () {
914 return f;
918 public static int test_42_vtype_delegate () {
919 var s = new VtypeVirtualDelStruct () { f = 42 };
920 Func<int> f = s.return_field_nonvirt;
921 return f ();
924 public static int test_42_vtype_virtual_delegate () {
925 IFaceVirtualDel s = new VtypeVirtualDelStruct () { f = 42 };
926 Func<int> f = s.return_field;
927 return f ();
930 public static int test_1_store_decimal () {
931 decimal[,] a = {{1}};
933 if (a[0,0] != 1m)
934 return 0;
935 return 1;
938 public static int test_2_intptr_stobj () {
939 System.IntPtr [] arr = { new System.IntPtr () };
941 if (arr [0] != (System.IntPtr)0)
942 return 1;
943 return 2;
946 static int llmult (int a, int b, int c, int d) {
947 return a + b + c + d;
951 * Test that evaluation of complex arguments does not overwrite the
952 * arguments already in outgoing registers.
954 public static int test_155_regalloc () {
955 int a = 10;
956 int b = 10;
958 int c = 0;
959 int d = 0;
960 int[] arr = new int [5];
962 return llmult (arr [c + d], 150, 5, 0);
965 static bool large_struct_test (Large a, Large b, Large c, Large d)
967 if (!a.check ()) return false;
968 if (!b.check ()) return false;
969 if (!c.check ()) return false;
970 if (!d.check ()) return false;
971 return true;
974 public static int test_2_large_struct_pass ()
976 Large a, b, c, d;
977 a = new Large ();
978 b = new Large ();
979 c = new Large ();
980 d = new Large ();
981 a.populate ();
982 b.populate ();
983 c.populate ();
984 d.populate ();
985 if (large_struct_test (a, b, c, d))
986 return 2;
987 return 0;
990 public static unsafe int test_0_pin_string () {
991 string x = "xxx";
992 fixed (char *c = x) {
993 if (*c != 'x')
994 return 1;
996 return 0;
999 public static int my_flags;
1000 public static int test_0_and_cmp_static ()
1003 /* various forms of test [mem], imm */
1005 my_flags = 0x01020304;
1007 if ((my_flags & 0x01020304) == 0)
1008 return 1;
1010 if ((my_flags & 0x00000304) == 0)
1011 return 2;
1013 if ((my_flags & 0x00000004) == 0)
1014 return 3;
1016 if ((my_flags & 0x00000300) == 0)
1017 return 4;
1019 if ((my_flags & 0x00020000) == 0)
1020 return 5;
1022 if ((my_flags & 0x01000000) == 0)
1023 return 6;
1025 return 0;
1028 static byte b;
1029 public static int test_0_byte_compares ()
1031 b = 0xff;
1032 if (b == -1)
1033 return 1;
1034 b = 0;
1035 if (!(b < System.Byte.MaxValue))
1036 return 2;
1038 if (!(b <= System.Byte.MaxValue))
1039 return 3;
1041 return 0;
1044 static Nullable<bool> s_nullb;
1045 static AStruct s_struct1;
1047 /* test if VES uses correct sizes for value type write to static field */
1048 public static int test_0_static_nullable_bool () {
1049 s_struct1 = new AStruct (0x1337dead);
1050 s_nullb = true;
1051 /* make sure that the write to s_nullb didn't smash the value after it */
1052 if (s_struct1.i != 0x1337dead)
1053 return 2;
1054 return 0;
1057 public static int test_71_long_shift_right () {
1058 ulong value = 38654838087;
1059 int x = 0;
1060 byte [] buffer = new byte [1];
1061 buffer [x] = ((byte)(value >> x));
1062 return buffer [x];
1065 static long x;
1066 public static int test_0_addsub_mem ()
1068 x = 0;
1069 x += 5;
1071 if (x != 5)
1072 return 1;
1074 x -= 10;
1076 if (x != -5)
1077 return 2;
1079 return 0;
1082 static ulong y;
1083 public static int test_0_sh32_mem ()
1085 y = 0x0102130405060708;
1086 y >>= 32;
1088 if (y != 0x01021304)
1089 return 1;
1091 y = 0x0102130405060708;
1092 y <<= 32;
1094 if (y != 0x0506070800000000)
1095 return 2;
1097 x = 0x0102130405060708;
1098 x <<= 32;
1100 if (x != 0x0506070800000000)
1101 return 2;
1103 return 0;
1107 static uint dum_de_dum = 1;
1108 public static int test_0_long_arg_opt ()
1110 return Foo (0x1234567887654321, dum_de_dum);
1113 static int Foo (ulong x, ulong y)
1115 if (x != 0x1234567887654321)
1116 return 1;
1118 if (y != 1)
1119 return 2;
1121 return 0;
1124 public static int test_0_long_ret_opt ()
1126 ulong x = X ();
1127 if (x != 0x1234567887654321)
1128 return 1;
1129 ulong y = Y ();
1130 if (y != 1)
1131 return 2;
1133 return 0;
1136 static ulong X ()
1138 return 0x1234567887654321;
1141 static ulong Y ()
1143 return dum_de_dum;
1146 /* from bug# 71515 */
1147 static int counter = 0;
1148 static bool WriteStuff () {
1149 counter = 10;
1150 return true;
1152 public static int test_0_cond_branch_side_effects () {
1153 counter = 5;
1154 if (WriteStuff()) {
1156 if (counter == 10)
1157 return 0;
1158 return 1;
1161 // bug #74992
1162 public static int arg_only_written (string file_name, int[]
1163 ncells ) {
1164 if (file_name == null)
1165 return 1;
1167 ncells = foo ();
1168 bar (ncells [0]);
1170 return 0;
1173 public static int[] foo () {
1174 return new int [3];
1177 public static void bar (int i) {
1181 public static int test_0_arg_only_written ()
1183 return arg_only_written ("md.in", null);
1186 static long position = 0;
1188 public static int test_4_static_inc_long () {
1190 int count = 4;
1192 position = 0;
1194 position += count;
1196 return (int)position;
1199 struct FooStruct {
1201 public FooStruct (long l) {
1205 public static int test_0_calls_opcode_emulation () {
1206 // Test that emulated opcodes do not clobber arguments already in
1207 // out registers
1208 checked {
1209 long val = 10000;
1210 new FooStruct (val * 10000);
1212 return 0;
1215 public static int test_0_intrins_string_length () {
1216 string s = "ABC";
1218 return (s.Length == 3) ? 0 : 1;
1221 public static int test_0_intrins_string_chars () {
1222 string s = "ABC";
1224 return (s [0] == 'A' && s [1] == 'B' && s [2] == 'C') ? 0 : 1;
1227 public static int test_0_intrins_object_gettype () {
1228 object o = 1;
1230 return (o.GetType () == typeof (int)) ? 0 : 1;
1233 public static int test_0_intrins_object_gethashcode () {
1234 object o = new Object ();
1236 return (o.GetHashCode () == o.GetHashCode ()) ? 0 : 1;
1239 class FooClass {
1242 public static int test_0_intrins_object_ctor () {
1243 object o = new FooClass ();
1245 return (o != null) ? 0 : 1;
1248 public static int test_0_intrins_array_rank () {
1249 int[,] a = new int [10, 10];
1251 return (a.Rank == 2) ? 0 : 1;
1254 public static int test_0_intrins_array_length () {
1255 int[,] a = new int [10, 10];
1256 Array a2 = a;
1258 return (a2.Length == 100) ? 0 : 1;
1261 public static int test_0_intrins_runtimehelpers_offset_to_string_data () {
1262 int i = RuntimeHelpers.OffsetToStringData;
1264 return i - i;
1267 public static int test_0_intrins_string_setchar () {
1268 StringBuilder sb = new StringBuilder ("ABC");
1270 sb [1] = 'D';
1272 return sb.ToString () == "ADC" ? 0 : 1;
1275 enum FlagsEnum {
1276 None = 0,
1277 A = 1,
1278 B = 2,
1279 C = 4
1282 public static int test_0_intrins_enum_hasflag () {
1283 var e = FlagsEnum.A | FlagsEnum.B;
1285 if (!e.HasFlag (FlagsEnum.A))
1286 return 1;
1287 if (!e.HasFlag (FlagsEnum.A | FlagsEnum.B))
1288 return 2;
1289 if (!e.HasFlag (FlagsEnum.None))
1290 return 3;
1291 if (e.HasFlag (FlagsEnum.C))
1292 return 4;
1293 return 0;
1296 public class Bar {
1297 bool allowLocation = true;
1298 Foo f = new Foo ();
1301 public static int test_0_regress_78990_unaligned_structs () {
1302 new Bar ();
1304 return 0;
1307 public static unsafe int test_97_negative_index () {
1308 char[] arr = new char[] {'a', 'b'};
1309 fixed (char *p = arr) {
1310 char *i = p + 2;
1311 char a = i[-2];
1312 return a;
1316 /* bug #82281 */
1317 public static int test_0_unsigned_right_shift_imm0 () {
1318 uint temp = 0;
1319 byte[] data = new byte[256];
1320 for (int i = 0; i < 1; i ++)
1321 temp = (uint)(data[temp >> 24] | data[temp >> 0]);
1322 return 0;
1325 class Foo2 {
1326 public virtual int foo () {
1327 return 0;
1331 sealed class Bar2 : Foo2 {
1332 public override int foo () {
1333 return 0;
1337 public static int test_0_abcrem_check_this_removal () {
1338 Bar2 b = new Bar2 ();
1340 // The check_this generated here by the JIT should be removed
1341 b.foo ();
1343 return 0;
1346 static int invoke_twice (Bar2 b) {
1347 b.foo ();
1348 // The check_this generated here by the JIT should be removed
1349 b.foo ();
1351 return 0;
1354 public static int test_0_abcrem_check_this_removal2 () {
1355 Bar2 b = new Bar2 ();
1357 invoke_twice (b);
1359 return 0;
1362 /* #346563 */
1363 public static int test_0_array_access_64_bit () {
1364 int[] arr2 = new int [10];
1365 for (int i = 0; i < 10; ++i)
1366 arr2 [i] = i;
1367 string s = "ABCDEFGH";
1369 byte[] arr = new byte [4];
1370 arr [0] = 252;
1371 arr [1] = 255;
1372 arr [2] = 255;
1373 arr [3] = 255;
1375 int len = arr [0] | (arr [1] << 8) | (arr [2] << 16) | (arr [3] << 24);
1376 int len2 = - (len + 2);
1378 // Test array and string access with a 32 bit value whose upper 32 bits are
1379 // undefined
1380 // len2 = 3
1381 if (arr2 [len2] != 2)
1382 return 1;
1383 if (s [len2] != 'C')
1384 return 2;
1385 return 0;
1388 public static float return_float () {
1389 return 1.4e-45f;
1392 #if !NO_BITCODE
1393 [Category ("!BITCODE")] // bug #59953
1394 public static int test_0_float_return_spill () {
1395 // The return value of return_float () is spilled because of the
1396 // boxing call
1397 object o = return_float ();
1398 float f = return_float ();
1399 return (float)o == f ? 0 : 1;
1401 #endif
1403 class R4Holder {
1404 public static float pi = 3.14f;
1406 public float float_field;
1409 public static int test_0_ldsfld_soft_float () {
1410 if (R4Holder.pi == 3.14f)
1411 return 0;
1412 else
1413 return 1;
1416 public static int test_0_ldfld_stfld_soft_float () {
1417 R4Holder h = new R4Holder ();
1418 h.float_field = 3.14f;
1420 if (h.float_field == 3.14f)
1421 return 0;
1422 else
1423 return 1;
1426 class R4HolderRemote : MarshalByRefObject {
1427 public static float pi = 3.14f;
1429 public float float_field;
1432 public static int test_0_ldfld_stfld_soft_float_remote () {
1433 R4HolderRemote h = new R4HolderRemote ();
1434 h.float_field = 3.14f;
1436 if (h.float_field == 3.14f)
1437 return 0;
1438 else
1439 return 1;
1442 public static int test_0_locals_soft_float () {
1443 float f = 0.0f;
1445 f = 3.14f;
1447 if (f == 3.14f)
1448 return 0;
1449 else
1450 return 1;
1453 struct AStruct2 {
1454 public int i;
1455 public int j;
1458 static float pass_vtype_return_float (AStruct2 s) {
1459 return s.i + s.j == 6 ? 1.0f : -1.0f;
1462 public static int test_0_vtype_arg_soft_float () {
1463 return pass_vtype_return_float (new AStruct2 () { i = 2, j = 4 }) > 0.0 ? 0 : 1;
1466 static int range_check_strlen (int i, string s) {
1467 if (i < 0 || i > s.Length)
1468 return 1;
1469 else
1470 return 0;
1473 public static int test_0_range_check_opt () {
1474 if (range_check_strlen (0, "A") != 0)
1475 return 1;
1476 if (range_check_strlen (1, "A") != 0)
1477 return 2;
1478 if (range_check_strlen (2, "A") != 1)
1479 return 3;
1480 if (range_check_strlen (-100, "A") != 1)
1481 return 4;
1482 return 0;
1485 static int test_0_array_get_set_soft_float () {
1486 float[,] arr = new float [2, 2];
1487 arr [0, 0] = 256f;
1488 return arr [0, 0] == 256f ? 0 : 1;
1491 //repro for #506915
1492 struct Bug506915 { public int val; }
1493 static int test_2_ldobj_stobj_optization ()
1495 int i = 99;
1496 var a = new Bug506915 ();
1497 var b = new Bug506915 ();
1498 if (i.GetHashCode () == 99)
1499 i = 44;
1500 var array = new Bug506915 [2];
1501 array [0].val = 2;
1502 array [1] = (i == 0) ? a : array [0];
1504 return array [1].val;
1507 /* mcs can't compile this (#646744) */
1508 #if FALSE
1509 static void InitMe (out Gamma noMercyWithTheStack) {
1510 noMercyWithTheStack = new Gamma ();
1513 static int FunNoInline () {
1514 int x = 99;
1515 if (x > 344 && x < 22)
1516 return 333;
1517 return x;
1520 static float DoNothingButDontInline (float a, int b) {
1521 if (b > 0)
1522 return a;
1523 else if (b < 0 && b > 10)
1524 return 444.0f;
1525 return a;
1529 * The local register allocator emits loadr8_membase and storer8_membase
1530 * to do spilling. This code is generated after mono_arch_lowering_pass so
1531 * mono_arch_output_basic_block must know how to deal with big offsets.
1532 * This only happens because the call in middle forces the temp for "(float)obj"
1533 * to be spilled.
1535 public static int test_0_float_load_and_store_with_big_offset ()
1537 object obj = 1.0f;
1538 Gamma noMercyWithTheStack;
1539 float res;
1541 InitMe (out noMercyWithTheStack);
1543 res = DoNothingButDontInline ((float)obj, FunNoInline ());
1545 if (!(res == 1.0f))
1546 return 1;
1547 return 0;
1549 #endif
1551 struct VTypePhi {
1552 public int i;
1555 static int vtype_phi (VTypePhi v1, VTypePhi v2, bool first) {
1556 VTypePhi v = first ? v1 : v2;
1558 return v.i;
1561 static int test_0_vtype_phi ()
1563 VTypePhi v1 = new VTypePhi () { i = 1 };
1564 VTypePhi v2 = new VTypePhi () { i = 2 };
1566 if (vtype_phi (v1, v2, true) != 1)
1567 return 1;
1568 if (vtype_phi (v1, v2, false) != 2)
1569 return 2;
1571 return 0;
1574 [MethodImplAttribute (MethodImplOptions.NoInlining)]
1575 static void UseValue (int index)
1579 [MethodImplAttribute (MethodImplOptions.NoInlining)]
1580 static bool IsFalse ()
1582 return false;
1585 static int test_0_llvm_moving_faulting_loads ()
1587 int[] indexes = null;
1589 if (IsFalse ()) {
1590 indexes = new int[0];
1593 while (IsFalse ()) {
1594 UseValue (indexes[0]);
1595 UseValue (indexes[0]);
1598 return 0;
1601 public static bool flag;
1603 class B {
1605 internal static B[] d;
1607 static B () {
1608 flag = true;
1612 [MethodImplAttribute (MethodImplOptions.NoInlining)]
1613 static int regress_679467_inner () {
1614 if (flag == true)
1615 return 1;
1616 var o = B.d;
1617 var o2 = B.d;
1618 return 0;
1622 * FIXME: This fails with AOT #703317.
1625 static int test_0_multiple_cctor_calls_regress_679467 () {
1626 flag = false;
1627 return regress_679467_inner ();
1631 static int test_0_char_ctor () {
1632 string s = new String (new char[] { 'A', 'B' }, 0, 1);
1633 return 0;
1636 static object mInstance = null;
1638 [MethodImpl(MethodImplOptions.Synchronized)]
1639 public static object getInstance() {
1640 if (mInstance == null)
1641 mInstance = new object();
1642 return mInstance;
1645 static int test_0_synchronized () {
1646 getInstance ();
1647 return 0;
1650 struct BStruct {
1651 public Type t;
1654 class Del<T> {
1655 public static BStruct foo () {
1656 return new BStruct () { t = typeof (T) };
1660 delegate BStruct ADelegate ();
1662 static int test_0_regress_10601 () {
1663 var act = (ADelegate)(Del<string>.foo);
1664 BStruct b = act ();
1665 if (b.t != typeof (string))
1666 return 1;
1667 return 0;
1670 static int test_0_regress_11058 () {
1671 int foo = -252674008;
1672 int foo2 = (int)(foo ^ 0xF0F0F0F0); // = 28888
1673 var arr = new byte[foo2].Length;
1674 return 0;
1677 public static void do_throw () {
1678 throw new Exception ();
1681 [MethodImplAttribute (MethodImplOptions.NoInlining)]
1682 static void empty () {
1685 // #11297
1686 public static int test_0_llvm_inline_throw () {
1687 try {
1688 empty ();
1689 } catch (Exception) {
1690 do_throw ();
1693 return 0;
1696 enum ByteEnum : byte {
1697 Zero = 0
1700 struct BugStruct {
1701 public ByteEnum f1;
1702 public ByteEnum f2;
1703 public ByteEnum f3;
1704 public byte f4;
1705 public byte f5;
1706 public byte f6;
1707 public byte f7;
1710 public static int test_0_14217 () {
1711 t_14217_inner (new BugStruct ());
1712 return 0;
1715 [MethodImplAttribute (MethodImplOptions.NoInlining)]
1716 static void t_14217_inner (BugStruct bug) {
1719 [StructLayout(LayoutKind.Sequential)]
1720 public struct EmptyStruct {
1723 class EmptyClass {
1724 public static EmptyStruct s;
1727 // #20349
1728 static int test_0_empty_struct_as_static () {
1729 var s = EmptyClass.s;
1730 return 0;
1733 // #25487
1734 static int test_0_int_to_r4 () {
1735 return int_to_r4_inner (255);
1738 static int int_to_r4_inner (int value1) {
1739 int sub = -value1;
1740 float mult = sub * 1f;
1741 if (mult != -255.0f)
1742 return 1;
1743 else
1744 return 0;
1747 struct HFA4D {
1748 public double a, b, c, d;
1751 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) {
1752 return s.a + s.b + s.c + s.d;
1755 static int test_0_arm64_hfa_on_stack () {
1756 var s = new HFA4D () { a = 1.0, b = 2.0, c = 3.0, d = 4.0 };
1757 var res = arm64_hfa_on_stack_inner (1, 2, 3, 4, 5, 6, 7, 8, s);
1758 return res == 10.0 ? 0 : 1;
1761 class MulOvfClass {
1762 [MethodImplAttribute (MethodImplOptions.NoInlining)]
1763 public unsafe void EncodeIntoBuffer(char* value, int valueLength, char* buffer, int bufferLength) {
1767 static unsafe int test_0_mul_ovf_regress_36052 () {
1768 var p = new MulOvfClass ();
1770 string typeName = typeof(int).Name;
1771 int bufferSize = 45;
1773 fixed (char* value = typeName) {
1774 char* buffer = stackalloc char[bufferSize];
1775 p.EncodeIntoBuffer(value, typeName.Length, buffer, bufferSize);
1777 return 0;
1780 struct Struct16 {
1781 public int a, b, c, d;
1784 [MethodImplAttribute (MethodImplOptions.NoInlining)]
1785 static int pass_struct16 (object o0, object o2, object o3, object o4, object o5, object o6, object o7, Struct16 o8) {
1786 // This disables LLVM
1787 try {
1788 } catch {
1790 return o8.a;
1793 [MethodImplAttribute (MethodImplOptions.NoInlining)]
1794 static int pass_struct16 (object o0, object o2, object o3, object o6, object o7, Struct16 o8) {
1795 return pass_struct16 (o0, o2, null, o3, null, o6, o7, o8);
1798 public static int test_42_pass_16byte_struct_split () {
1799 return pass_struct16 (null, null, null, null, null, new Struct16 () { a = 42 });
1802 public interface IComparer2
1804 Type foo<T> ();
1807 public class AClass : IComparer2 {
1808 public Type foo<T> () {
1809 return typeof(T);
1813 public static int test_0_delegate_to_virtual_generic_on_ifaces () {
1814 IComparer2 c = new AClass ();
1816 Func<Type> f = c.foo<string>;
1817 return f () == typeof(string) ? 0 : 1;
1820 public enum ByteEnum2 : byte {
1821 High = 142
1824 [MethodImplAttribute (MethodImplOptions.NoInlining)]
1825 public static int enum_arg_zero_extend (ByteEnum2 b) {
1826 return (int)b;
1829 public static int test_142_byte_enum_arg_zero_extend () {
1830 return enum_arg_zero_extend (ByteEnum2.High);
1833 enum Mine { One, Two }
1835 public static int test_0_enum_gethashcode_opt () {
1836 int sum = 0;
1837 for (int i = 0; i < 1000000; ++i)
1838 sum += Mine.Two.GetHashCode();
1840 return 0;
1843 public static int test_0_typedref () {
1844 int i = 5;
1845 System.TypedReference r = __makeref(i);
1846 System.Type t = __reftype(r);
1848 if (t != typeof (int))
1849 return 1;
1850 int j = __refvalue(r, int);
1851 if (j != 5)
1852 return 2;
1854 try {
1855 object o = __refvalue (r, object);
1856 } catch (InvalidCastException) {
1859 return 0;
1862 enum FooEnum { Bar }
1863 //https://github.com/mono/mono/issues/6666
1864 public static int test_0_bad_unbox_nullable_of_enum () {
1865 try {
1866 var enumValue = FooEnum.Bar;
1867 object value = (int)enumValue;
1868 var res = (FooEnum?)value; // Should throw
1869 } catch (InvalidCastException) {
1870 return 0;
1872 return 1;
1875 //https://github.com/mono/mono/issues/6666
1876 public static int test_0_unbox_nullable_of_enum () {
1877 try {
1878 var enumValue = FooEnum.Bar;
1879 object value = (object)enumValue;
1880 var res = (FooEnum?)value; // Should not throw
1881 } catch (InvalidCastException) {
1882 return 1;
1884 return 0;
1887 static void decode (out sbyte v) {
1888 byte tmp = 134;
1889 v = (sbyte)tmp;
1892 // gh #6414
1893 public static int test_0_alias_analysis_sign_extend () {
1894 sbyte t;
1895 decode (out t);
1897 return t == -122 ? 0 : 1;
1900 public interface IFoo
1902 int MyInt { get; }
1905 public class IFooImpl : IFoo
1907 public int MyInt => 0;
1910 //gh 6266
1911 public static int test_0_store_to_magic_iface_array ()
1913 ICollection<IFoo> arr1 = new IFooImpl[1] { new IFooImpl() };
1914 ICollection<IFoo> arr2 = new IFooImpl[1] { new IFooImpl() };
1916 ICollection<IFoo>[] a2d = new ICollection<IFoo>[2] {
1917 arr1,
1918 arr2,
1921 return 0;
1924 static volatile bool abool;
1926 public static unsafe int test_0_stind_r4_float32_stack_merge () {
1927 Single* dataPtr = stackalloc Single[4];
1928 abool = true;
1929 dataPtr[0] = abool ? 1.0f : 2.0f;
1930 return dataPtr [0] == 1.0f ? 0 : 1;
1933 class AClass1 {
1936 class BClass1 : AClass1 {
1939 class CClass1 {
1942 public static int test_0_array_of_magic_iface () {
1943 // Need to make this an object otherwise csc removes the cast
1944 object d = new [] { new [] { new BClass1 () } };
1945 if (!(d is IList<AClass1> []))
1946 return 1;
1947 if (d is IList<CClass1> [])
1948 return 2;
1949 var e2 = (IList<AClass1> []) d;
1950 return 0;
1953 class SimpleContainer {
1954 public Simple simple1;
1955 public Simple simple2;
1957 public static Simple constsimple;
1959 public int SetFields () {
1960 constsimple.a = 0x1337;
1961 simple1 = simple2 = constsimple;
1962 return simple1.a - simple2.a;
1966 public static int test_0_dup_vtype () {
1967 return new SimpleContainer ().SetFields ();
1970 public struct Vec3 {
1971 public int X, Y, Z;
1973 [MethodImplAttribute (MethodImplOptions.NoInlining)]
1974 public Vec3(int x, int y, int z) {
1975 X = x;
1976 Y = y;
1977 Z = z;
1981 [MethodImplAttribute (MethodImplOptions.NoInlining)]
1982 public static int gh_11378_inner_1 (Vec3 p1, Vec3 p2) {
1983 p1.X -= p2.X;
1984 p1.Y -= p2.Y;
1985 p1.Z -= p2.Z;
1987 return (int)p2.Y;
1990 [MethodImplAttribute (MethodImplOptions.NoInlining)]
1991 public static int gh_11378_inner_2 (Vec3 c, Vec3 pos) {
1992 return gh_11378_inner_1 (pos, c);
1995 static int gh_11378_inner_3 (Vec3 c) {
1996 var c2 = c;
1997 return gh_11378_inner_2 (c, c2);
2000 public static int test_2_gh_11378 () {
2001 return gh_11378_inner_3 (new Vec3(0, 2, -20));
2006 #if __MOBILE__
2008 #endif