1 #include <limits.h>
2 #include <float.h>
3 
4 #include "libgccjit.h"
5 
6 #include "harness.h"
7 
8 static void
make_test_of_constant(gcc_jit_context * ctxt,gcc_jit_type * type,gcc_jit_rvalue * rvalue,const char * funcname)9 make_test_of_constant (gcc_jit_context *ctxt,
10                        gcc_jit_type *type,
11                        gcc_jit_rvalue *rvalue,
12                        const char *funcname)
13 {
14   /* Make a test function of the form:
15        T funcname (void)
16        {
17 	  return VALUE;
18        }
19      and return a debug dump of VALUE so that
20      the caller can sanity-check the debug dump implementation.
21   */
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);
31 }
32 
33 /**********************************************************************
34  Tests of gcc_jit_context_new_rvalue_from_int.
35  **********************************************************************/
36 
37 static const char *
make_test_of_int_constant(gcc_jit_context * ctxt,gcc_jit_type * type,int value,const char * funcname)38 make_test_of_int_constant (gcc_jit_context *ctxt,
39 			   gcc_jit_type *type,
40 			   int value,
41 			   const char *funcname)
42 {
43   /* Make a test function of the form:
44        int funcname (void)
45        {
46 	  return VALUE;
47        }
48      and return a debug dump of VALUE so that
49      the caller can sanity-check the debug dump implementation.
50   */
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));
56 }
57 
58 static void
make_tests_of_int_constants(gcc_jit_context * ctxt)59 make_tests_of_int_constants (gcc_jit_context *ctxt)
60 {
61   gcc_jit_type *int_type =
62     gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_INT);
63 
64   CHECK_STRING_VALUE (
65     make_test_of_int_constant (ctxt,
66 			       int_type,
67 			       0,
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");
78 }
79 
80 static void
verify_int_constants(gcc_jit_result * result)81 verify_int_constants (gcc_jit_result *result)
82 {
83   typedef int (*test_fn) (void);
84 
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);
90 
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);
96 
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);
102 }
103 
104 /**********************************************************************
105  Tests of gcc_jit_context_new_rvalue_from_long.
106  **********************************************************************/
107 
108 static const char *
make_test_of_long_constant(gcc_jit_context * ctxt,gcc_jit_type * type,long value,const char * funcname)109 make_test_of_long_constant (gcc_jit_context *ctxt,
110 			   gcc_jit_type *type,
111 			   long value,
112 			   const char *funcname)
113 {
114   /* Make a test function of the form:
115        long funcname (void)
116        {
117 	  return VALUE;
118        }
119      and return a debug dump of VALUE so that
120      the caller can sanity-check the debug dump implementation.
121   */
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));
127 }
128 
129 static void
make_tests_of_long_constants(gcc_jit_context * ctxt)130 make_tests_of_long_constants (gcc_jit_context *ctxt)
131 {
132   gcc_jit_type *long_type =
133     gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_LONG);
134 
135   CHECK_STRING_VALUE (
136     make_test_of_long_constant (ctxt,
137 			       long_type,
138 			       0,
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");
149 }
150 
151 static void
verify_long_constants(gcc_jit_result * result)152 verify_long_constants (gcc_jit_result *result)
153 {
154   typedef long (*test_fn) (void);
155 
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);
161 
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);
167 
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);
173 }
174 
175 /**********************************************************************
176  Tests of gcc_jit_context_new_rvalue_from_double.
177  **********************************************************************/
178 
179 static const char *
make_test_of_double_constant(gcc_jit_context * ctxt,gcc_jit_type * type,double value,const char * funcname)180 make_test_of_double_constant (gcc_jit_context *ctxt,
181 			   gcc_jit_type *type,
182 			   double value,
183 			   const char *funcname)
184 {
185   /* Make a test function of the form:
186        double funcname (void)
187        {
188 	  return VALUE;
189        }
190      and return a debug dump of VALUE so that
191      the caller can sanity-check the debug dump implementation.
192   */
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));
198 }
199 
200 static void
make_tests_of_double_constants(gcc_jit_context * ctxt)201 make_tests_of_double_constants (gcc_jit_context *ctxt)
202 {
203   gcc_jit_type *double_type =
204     gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_DOUBLE);
205 
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");
222 }
223 
224 static void
verify_double_constants(gcc_jit_result * result)225 verify_double_constants (gcc_jit_result *result)
226 {
227   typedef double (*test_fn) (void);
228 
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);
234 
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);
240 
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);
246 
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);
252 }
253 
254 /**********************************************************************
255  Tests of gcc_jit_context_new_rvalue_from_ptr.
256  **********************************************************************/
257 
258 static const char *
make_test_of_ptr_constant(gcc_jit_context * ctxt,gcc_jit_type * type,void * value,const char * funcname)259 make_test_of_ptr_constant (gcc_jit_context *ctxt,
260 			   gcc_jit_type *type,
261 			   void *value,
262 			   const char *funcname)
263 {
264   /* Make a test function of the form:
265        void *funcname (void)
266        {
267 	  return VALUE;
268        }
269      and return a debug dump of VALUE so that
270      the caller can sanity-check the debug dump implementation.
271   */
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));
277 }
278 
279 static void
make_tests_of_ptr_constants(gcc_jit_context * ctxt)280 make_tests_of_ptr_constants (gcc_jit_context *ctxt)
281 {
282   gcc_jit_type *ptr_type =
283     gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_VOID_PTR);
284 
285   CHECK_STRING_VALUE (
286     make_test_of_ptr_constant (ctxt,
287 			       ptr_type,
288 			       0,
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");
297 }
298 
299 static void
verify_ptr_constants(gcc_jit_result * result)300 verify_ptr_constants (gcc_jit_result *result)
301 {
302   typedef void *(*test_fn) (void);
303 
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);
309 
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);
315 }
316 
317 /**********************************************************************
318  Code for harness
319  **********************************************************************/
320 
321 void
create_code(gcc_jit_context * ctxt,void * user_data)322 create_code (gcc_jit_context *ctxt, void *user_data)
323 {
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);
328 }
329 
330 void
verify_code(gcc_jit_context * ctxt,gcc_jit_result * result)331 verify_code (gcc_jit_context *ctxt, gcc_jit_result *result)
332 {
333   CHECK_NON_NULL (result);
334 
335   verify_int_constants (result);
336   verify_long_constants (result);
337   verify_double_constants (result);
338   verify_ptr_constants (result);
339 }
340