2018-02-09 Sebastian Perta <sebastian.perta@renesas.com>
[official-gcc.git] / gcc / testsuite / jit.dg / test-constants.c
bloba5092494c5c27e735c511fa282753cce3e0d2672
1 #include <limits.h>
2 #include <float.h>
4 #include "libgccjit.h"
6 #include "harness.h"
8 static void
9 make_test_of_constant (gcc_jit_context *ctxt,
10 gcc_jit_type *type,
11 gcc_jit_rvalue *rvalue,
12 const char *funcname)
14 /* Make a test function of the form:
15 T funcname (void)
17 return VALUE;
19 and return a debug dump of VALUE so that
20 the caller can sanity-check the debug dump implementation.
22 gcc_jit_function *test_fn =
23 gcc_jit_context_new_function (ctxt, NULL,
24 GCC_JIT_FUNCTION_EXPORTED,
25 type,
26 funcname,
27 0, NULL,
28 0);
29 gcc_jit_block *initial = gcc_jit_function_new_block (test_fn, "initial");
30 gcc_jit_block_end_with_return (initial, NULL, rvalue);
33 /**********************************************************************
34 Tests of gcc_jit_context_new_rvalue_from_int.
35 **********************************************************************/
37 static const char *
38 make_test_of_int_constant (gcc_jit_context *ctxt,
39 gcc_jit_type *type,
40 int value,
41 const char *funcname)
43 /* Make a test function of the form:
44 int funcname (void)
46 return VALUE;
48 and return a debug dump of VALUE so that
49 the caller can sanity-check the debug dump implementation.
51 gcc_jit_rvalue *rvalue =
52 gcc_jit_context_new_rvalue_from_int (ctxt, type, value);
53 make_test_of_constant (ctxt, type, rvalue, funcname);
54 return gcc_jit_object_get_debug_string (
55 gcc_jit_rvalue_as_object (rvalue));
58 static void
59 make_tests_of_int_constants (gcc_jit_context *ctxt)
61 gcc_jit_type *int_type =
62 gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_INT);
64 CHECK_STRING_VALUE (
65 make_test_of_int_constant (ctxt,
66 int_type,
68 "test_int_constant_0"),
69 "(int)0");
70 make_test_of_int_constant (ctxt,
71 int_type,
72 INT_MAX,
73 "test_int_constant_INT_MAX");
74 make_test_of_int_constant (ctxt,
75 int_type,
76 INT_MIN,
77 "test_int_constant_INT_MIN");
80 static void
81 verify_int_constants (gcc_jit_result *result)
83 typedef int (*test_fn) (void);
85 test_fn test_int_constant_0 =
86 (test_fn)gcc_jit_result_get_code (result,
87 "test_int_constant_0");
88 CHECK_NON_NULL (test_int_constant_0);
89 CHECK_VALUE (test_int_constant_0 (), 0);
91 test_fn test_int_constant_INT_MAX =
92 (test_fn)gcc_jit_result_get_code (result,
93 "test_int_constant_INT_MAX");
94 CHECK_NON_NULL (test_int_constant_INT_MAX);
95 CHECK_VALUE (test_int_constant_INT_MAX (), INT_MAX);
97 test_fn test_int_constant_INT_MIN =
98 (test_fn)gcc_jit_result_get_code (result,
99 "test_int_constant_INT_MIN");
100 CHECK_NON_NULL (test_int_constant_INT_MIN);
101 CHECK_VALUE (test_int_constant_INT_MIN (), INT_MIN);
104 /**********************************************************************
105 Tests of gcc_jit_context_new_rvalue_from_long.
106 **********************************************************************/
108 static const char *
109 make_test_of_long_constant (gcc_jit_context *ctxt,
110 gcc_jit_type *type,
111 long value,
112 const char *funcname)
114 /* Make a test function of the form:
115 long funcname (void)
117 return VALUE;
119 and return a debug dump of VALUE so that
120 the caller can sanity-check the debug dump implementation.
122 gcc_jit_rvalue *rvalue =
123 gcc_jit_context_new_rvalue_from_long (ctxt, type, value);
124 make_test_of_constant (ctxt, type, rvalue, funcname);
125 return gcc_jit_object_get_debug_string (
126 gcc_jit_rvalue_as_object (rvalue));
129 static void
130 make_tests_of_long_constants (gcc_jit_context *ctxt)
132 gcc_jit_type *long_type =
133 gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_LONG);
135 CHECK_STRING_VALUE (
136 make_test_of_long_constant (ctxt,
137 long_type,
139 "test_long_constant_0"),
140 "(long)0");
141 make_test_of_long_constant (ctxt,
142 long_type,
143 LONG_MAX,
144 "test_long_constant_LONG_MAX");
145 make_test_of_long_constant (ctxt,
146 long_type,
147 LONG_MIN,
148 "test_long_constant_LONG_MIN");
151 static void
152 verify_long_constants (gcc_jit_result *result)
154 typedef long (*test_fn) (void);
156 test_fn test_long_constant_0 =
157 (test_fn)gcc_jit_result_get_code (result,
158 "test_long_constant_0");
159 CHECK_NON_NULL (test_long_constant_0);
160 CHECK_VALUE (test_long_constant_0 (), 0);
162 test_fn test_long_constant_LONG_MAX =
163 (test_fn)gcc_jit_result_get_code (result,
164 "test_long_constant_LONG_MAX");
165 CHECK_NON_NULL (test_long_constant_LONG_MAX);
166 CHECK_VALUE (test_long_constant_LONG_MAX (), LONG_MAX);
168 test_fn test_long_constant_LONG_MIN =
169 (test_fn)gcc_jit_result_get_code (result,
170 "test_long_constant_LONG_MIN");
171 CHECK_NON_NULL (test_long_constant_LONG_MIN);
172 CHECK_VALUE (test_long_constant_LONG_MIN (), LONG_MIN);
175 /**********************************************************************
176 Tests of gcc_jit_context_new_rvalue_from_double.
177 **********************************************************************/
179 static const char *
180 make_test_of_double_constant (gcc_jit_context *ctxt,
181 gcc_jit_type *type,
182 double value,
183 const char *funcname)
185 /* Make a test function of the form:
186 double funcname (void)
188 return VALUE;
190 and return a debug dump of VALUE so that
191 the caller can sanity-check the debug dump implementation.
193 gcc_jit_rvalue *rvalue =
194 gcc_jit_context_new_rvalue_from_double (ctxt, type, value);
195 make_test_of_constant (ctxt, type, rvalue, funcname);
196 return gcc_jit_object_get_debug_string (
197 gcc_jit_rvalue_as_object (rvalue));
200 static void
201 make_tests_of_double_constants (gcc_jit_context *ctxt)
203 gcc_jit_type *double_type =
204 gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_DOUBLE);
206 make_test_of_double_constant (ctxt,
207 double_type,
208 0.5,
209 "test_double_constant_0_5");
210 make_test_of_double_constant (ctxt,
211 double_type,
212 1e100,
213 "test_double_constant_1e100");
214 make_test_of_double_constant (ctxt,
215 double_type,
216 DBL_MIN,
217 "test_double_constant_DBL_MIN");
218 make_test_of_double_constant (ctxt,
219 double_type,
220 DBL_MAX,
221 "test_double_constant_DBL_MAX");
224 static void
225 verify_double_constants (gcc_jit_result *result)
227 typedef double (*test_fn) (void);
229 test_fn test_double_constant_0_5 =
230 (test_fn)gcc_jit_result_get_code (result,
231 "test_double_constant_0_5");
232 CHECK_NON_NULL (test_double_constant_0_5);
233 CHECK_VALUE (test_double_constant_0_5 (), 0.5);
235 test_fn test_double_constant_1e100 =
236 (test_fn)gcc_jit_result_get_code (result,
237 "test_double_constant_1e100");
238 CHECK_NON_NULL (test_double_constant_1e100);
239 CHECK_VALUE (test_double_constant_1e100 (), 1e100);
241 test_fn test_double_constant_DBL_MIN =
242 (test_fn)gcc_jit_result_get_code (result,
243 "test_double_constant_DBL_MIN");
244 CHECK_NON_NULL (test_double_constant_DBL_MIN);
245 CHECK_VALUE (test_double_constant_DBL_MIN (), DBL_MIN);
247 test_fn test_double_constant_DBL_MAX =
248 (test_fn)gcc_jit_result_get_code (result,
249 "test_double_constant_DBL_MAX");
250 CHECK_NON_NULL (test_double_constant_DBL_MAX);
251 CHECK_VALUE (test_double_constant_DBL_MAX (), DBL_MAX);
254 /**********************************************************************
255 Tests of gcc_jit_context_new_rvalue_from_ptr.
256 **********************************************************************/
258 static const char *
259 make_test_of_ptr_constant (gcc_jit_context *ctxt,
260 gcc_jit_type *type,
261 void *value,
262 const char *funcname)
264 /* Make a test function of the form:
265 void *funcname (void)
267 return VALUE;
269 and return a debug dump of VALUE so that
270 the caller can sanity-check the debug dump implementation.
272 gcc_jit_rvalue *rvalue =
273 gcc_jit_context_new_rvalue_from_ptr (ctxt, type, value);
274 make_test_of_constant (ctxt, type, rvalue, funcname);
275 return gcc_jit_object_get_debug_string (
276 gcc_jit_rvalue_as_object (rvalue));
279 static void
280 make_tests_of_ptr_constants (gcc_jit_context *ctxt)
282 gcc_jit_type *ptr_type =
283 gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_VOID_PTR);
285 CHECK_STRING_VALUE (
286 make_test_of_ptr_constant (ctxt,
287 ptr_type,
289 "test_ptr_constant_0"),
290 "(void *)NULL");
291 CHECK_STRING_VALUE (
292 make_test_of_ptr_constant (ctxt,
293 ptr_type,
294 (void *)0xdeadbeef,
295 "test_ptr_constant_0xdeadbeef"),
296 "(void *)0xdeadbeef");
299 static void
300 verify_ptr_constants (gcc_jit_result *result)
302 typedef void *(*test_fn) (void);
304 test_fn test_ptr_constant_0 =
305 (test_fn)gcc_jit_result_get_code (result,
306 "test_ptr_constant_0");
307 CHECK_NON_NULL (test_ptr_constant_0);
308 CHECK_VALUE (test_ptr_constant_0 (), 0);
310 test_fn test_ptr_constant_0xdeadbeef =
311 (test_fn)gcc_jit_result_get_code (result,
312 "test_ptr_constant_0xdeadbeef");
313 CHECK_NON_NULL (test_ptr_constant_0xdeadbeef);
314 CHECK_VALUE (test_ptr_constant_0xdeadbeef (), (void *)0xdeadbeef);
317 /**********************************************************************
318 Code for harness
319 **********************************************************************/
321 void
322 create_code (gcc_jit_context *ctxt, void *user_data)
324 make_tests_of_int_constants (ctxt);
325 make_tests_of_long_constants (ctxt);
326 make_tests_of_double_constants (ctxt);
327 make_tests_of_ptr_constants (ctxt);
330 void
331 verify_code (gcc_jit_context *ctxt, gcc_jit_result *result)
333 CHECK_NON_NULL (result);
335 verify_int_constants (result);
336 verify_long_constants (result);
337 verify_double_constants (result);
338 verify_ptr_constants (result);