NaCl runtime fixes
[mono.git] / mono / mini / basic-calls.cs
1 using System;
2 using System.Reflection;
3
4 /*
5  * Regression tests for the mono JIT.
6  *
7  * Each test needs to be of the form:
8  *
9  * public static int test_<result>_<name> ();
10  *
11  * where <result> is an integer (the value that needs to be returned by
12  * the method to make it pass.
13  * <name> is a user-displayed name used to identify the test.
14  *
15  * The tests can be driven in two ways:
16  * *) running the program directly: Main() uses reflection to find and invoke
17  *      the test methods (this is useful mostly to check that the tests are correct)
18  * *) with the --regression switch of the jit (this is the preferred way since
19  *      all the tests will be run with optimizations on and off)
20  *
21  * The reflection logic could be moved to a .dll since we need at least another
22  * regression test file written in IL code to have better control on how
23  * the IL code looks.
24  */
25
26 class Tests {
27
28         public static int Main (string[] args) {
29                 return TestDriver.RunTests (typeof (Tests), args);
30         }
31
32         static void dummy () {
33         }
34
35         public static int test_0_return () {
36                 dummy ();
37                 return 0;
38         }
39
40         static int dummy1 () {
41                 return 1;
42         }
43
44         public static int test_2_int_return () {
45                 int r = dummy1 ();
46                 if (r == 1)
47                         return 2;
48                 return 0;
49         }
50
51         static int add1 (int val) {
52                 return val + 1;
53         }
54
55         public static int test_1_int_pass () {
56                 int r = add1 (5);
57                 if (r == 6)
58                         return 1;
59                 return 0;
60         }
61
62         static int add_many (int val, short t, byte b, int da) {
63                 return val + t + b + da;
64         }
65
66         public static int test_1_int_pass_many () {
67                 byte b = 6;
68                 int r = add_many (5, 2, b, 1);
69                 if (r == 14)
70                         return 1;
71                 return 0;
72         }
73
74         unsafe static float GetFloat (byte *ptr) {
75                 return *(float*)ptr;
76         }
77
78         unsafe public static float GetFloat(float value)
79                 {
80                         return GetFloat((byte *)&value);
81                 }
82
83         /* bug #42134 */
84         public static int test_2_inline_saved_arg_type () {
85                 float f = 100.0f;
86                 return GetFloat (f) == f? 2: 1;
87         }
88
89         static int pass_many_types (int a, long b, int c, long d) {
90                 return a + (int)b + c + (int)d;
91         }
92
93         public static int test_5_pass_longs () {
94                 return pass_many_types (1, 2, -5, 7);
95         }
96
97         static int overflow_registers (int a, int b, int c, int d, int e, int f, int g, int h, int i, int j) {
98                 return a+b+c+d+e+f+g+h+i+j;
99         }
100
101         public static int test_55_pass_even_more () {
102                 return overflow_registers (1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
103         }
104
105         static int pass_ints_longs (int a, long b, long c, long d, long e, int f, long g) {
106                 return (int)(a + b + c + d + e + f + g);
107         }
108
109         public static int test_1_sparc_argument_passing () {
110                 // The 4. argument tests split reg/mem argument passing
111                 // The 5. argument tests mem argument passing
112                 // The 7. argument tests passing longs in misaligned memory
113                 // The MaxValues are needed so the MS word of the long is not 0
114                 return pass_ints_longs (1, 2, System.Int64.MaxValue, System.Int64.MinValue, System.Int64.MaxValue, 0, System.Int64.MinValue);
115         }
116
117         static int pass_bytes (byte a, byte b, byte c, byte d, byte e, byte f, byte g) {
118                 return (int)(a + b + c + d + e + f + g);
119         }
120
121         public static int test_21_sparc_byte_argument_passing () {
122                 return pass_bytes (0, 1, 2, 3, 4, 5, 6);
123         }
124
125         static int pass_sbytes (sbyte a, sbyte b, sbyte c, sbyte d, sbyte e, sbyte f, sbyte g) {
126                 return (int)(a + b + c + d + e + f + g);
127         }
128
129         public static int test_21_sparc_sbyte_argument_passing () {
130                 return pass_sbytes (0, 1, 2, 3, 4, 5, 6);
131         }
132
133         static int pass_shorts (short a, short b, short c, short d, short e, short f, short g) {
134                 return (int)(a + b + c + d + e + f + g);
135         }
136
137         public static int test_21_sparc_short_argument_passing () {
138                 return pass_shorts (0, 1, 2, 3, 4, 5, 6);
139         }
140
141         static int pass_floats_doubles (float a, double b, double c, double d, double e, float f, double g) {
142                 return (int)(a + b + c + d + e + f + g);
143         }
144
145         public static int test_721_sparc_float_argument_passing () {
146                 return pass_floats_doubles (100.0f, 101.0, 102.0, 103.0, 104.0, 105.0f, 106.0);
147         }
148
149         static float pass_floats (float a, float b, float c, float d, float e, float f, float g, float h, float i, float j) {
150                 return a + b + c + d + e + f + g + h + i + j;
151         }
152
153         public static int test_55_sparc_float_argument_passing2 () {
154                 return (int)pass_floats (1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f, 9.0f, 10.0f);
155         }
156
157         public static bool is_small (float value) {
158                 double d = (double)value;
159                 double d2 = 7.183757E-41;
160                 return d - d2 < 0.000001;
161         }
162                 
163         public static int test_0_float_argument_passing_precision () {
164                 float f = 7.183757E-41f;
165                 return is_small (f) ? 0 : 1;
166         }
167
168         // The first argument must be passed on a dword aligned stack location
169         static int pass_byref_ints_longs (ref long a, ref int b, ref byte c, ref short d, ref long e, ref int f, ref long g) {
170                 return (int)(a + b + c + d + e + f + g);
171         }
172
173         static int pass_takeaddr_ints_longs (long a, int b, byte c, short d, long e, int f, long g) {
174                 return pass_byref_ints_longs (ref a, ref b, ref c, ref d, ref e, ref f, ref g);
175         }
176
177         // Test that arguments are moved to the stack from incoming registers
178         // when the argument must reside in the stack because its address is taken
179         public static int test_2_sparc_takeaddr_argument_passing () {
180                 return pass_takeaddr_ints_longs (1, 2, 253, -253, System.Int64.MaxValue, 0, System.Int64.MinValue);
181         }
182
183         static int pass_byref_floats_doubles (ref float a, ref double b, ref double c, ref double d, ref double e, ref float f, ref double g) {
184                 return (int)(a + b + c + d + e + f + g);
185         }
186
187         static int pass_takeaddr_floats_doubles (float a, double b, double c, double d, double e, float f, double g) {
188                 return pass_byref_floats_doubles (ref a, ref b, ref c, ref d, ref e, ref f, ref g);
189         }
190
191         public static int test_721_sparc_takeaddr_argument_passing2 () {
192                 return pass_takeaddr_floats_doubles (100.0f, 101.0, 102.0, 103.0, 104.0, 105.0f, 106.0);
193         }
194
195         static void pass_byref_double (out double d) {
196                 d = 5.0;
197         }
198
199         // Test byref double argument passing
200         public static int test_0_sparc_byref_double_argument_passing () {
201                 double d;
202                 pass_byref_double (out d);
203                 return (d == 5.0) ? 0 : 1;
204         }
205
206         static void shift_un_arg (ulong value) {
207                 do {
208                         value = value >> 4;
209                 } while (value != 0);
210         }
211
212         // Test that assignment to long arguments work
213         public static int test_0_long_arg_assign ()
214         {
215                 ulong c = 0x800000ff00000000;
216                         
217                 shift_un_arg (c >> 4);
218
219                 return 0;
220         }
221
222         static unsafe void* ptr_return (void *ptr)
223         {
224                 return ptr;
225         }
226
227         public static unsafe int test_0_ptr_return ()
228         {
229                 void *ptr = new IntPtr (55).ToPointer ();
230
231                 if (ptr_return (ptr) == ptr)
232                         return 0;
233                 else
234                         return 1;
235         }
236
237         static bool isnan (float f) {
238                 return (f != f);
239         }
240
241         public static int test_0_isnan () {
242                 float f = 1.0f;
243                 return isnan (f) ? 1 : 0;
244         }
245
246         static int first_is_zero (int v1, int v2) {
247                 if (v1 != 0)
248                         return -1;
249                 return v2;
250         }
251         public static int test_1_handle_dup_stloc () {
252                 int index = 0;
253                 int val = first_is_zero (index, ++index);
254                 if (val != 1)
255                         return 2;
256                 return 1;
257         }
258
259         static long return_5low () {
260                 return 5;
261         }
262         
263         static long return_5high () {
264                 return 0x500000000;
265         }
266
267         public static int test_3_long_ret () {
268                 long val = return_5low ();
269                 return (int) (val - 2);
270         }
271
272         public static int test_1_long_ret2 () {
273                 long val = return_5high ();
274                 if (val > 0xffffffff)
275                         return 1;
276                 return 0;
277         }
278
279         public static void use_long_arg (ulong l) {
280                 for (int i = 0; i < 10; ++i)
281                         l ++;
282         }
283
284         public static ulong return_long_arg (object o, ulong perm) {
285                 use_long_arg (perm);
286
287         perm = 0x8000000000000FFF;
288
289                 use_long_arg (perm);
290
291                 return perm;
292         }
293
294     public static int test_0_sparc_long_ret_regress_541577 () {
295         ulong perm = 0x8000000000000FFF;
296
297         ulong work = return_long_arg (null, perm);
298
299                 return work == perm ? 0 : 1;
300         }
301
302         static void doit (double value, out long m) {
303                 m = (long) value;
304         }
305
306         public static int test_0_ftol_clobber () {
307                 long m;
308                 doit (1.3, out m);
309                 if (m != 1)
310                         return 2;
311                 return 0;
312         }
313 }
314