aboutsummaryrefslogtreecommitdiff
path: root/gcc/testsuite/jit.dg/test-constants.c
blob: a5092494c5c27e735c511fa282753cce3e0d2672 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
#include <limits.h>
#include <float.h>

#include "libgccjit.h"

#include "harness.h"

static void
make_test_of_constant (gcc_jit_context *ctxt,
                       gcc_jit_type *type,
                       gcc_jit_rvalue *rvalue,
                       const char *funcname)
{
  /* Make a test function of the form:
       T funcname (void)
       {
	  return VALUE;
       }
     and return a debug dump of VALUE so that
     the caller can sanity-check the debug dump implementation.
  */
  gcc_jit_function *test_fn =
    gcc_jit_context_new_function (ctxt, NULL,
				  GCC_JIT_FUNCTION_EXPORTED,
				  type,
				  funcname,
				  0, NULL,
				  0);
  gcc_jit_block *initial = gcc_jit_function_new_block (test_fn, "initial");
  gcc_jit_block_end_with_return (initial, NULL, rvalue);
}

/**********************************************************************
 Tests of gcc_jit_context_new_rvalue_from_int.
 **********************************************************************/

static const char *
make_test_of_int_constant (gcc_jit_context *ctxt,
			   gcc_jit_type *type,
			   int value,
			   const char *funcname)
{
  /* Make a test function of the form:
       int funcname (void)
       {
	  return VALUE;
       }
     and return a debug dump of VALUE so that
     the caller can sanity-check the debug dump implementation.
  */
  gcc_jit_rvalue *rvalue =
    gcc_jit_context_new_rvalue_from_int (ctxt, type, value);
  make_test_of_constant (ctxt, type, rvalue, funcname);
  return gcc_jit_object_get_debug_string (
    gcc_jit_rvalue_as_object (rvalue));
}

static void
make_tests_of_int_constants (gcc_jit_context *ctxt)
{
  gcc_jit_type *int_type =
    gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_INT);

  CHECK_STRING_VALUE (
    make_test_of_int_constant (ctxt,
			       int_type,
			       0,
			       "test_int_constant_0"),
    "(int)0");
  make_test_of_int_constant (ctxt,
                             int_type,
                             INT_MAX,
                             "test_int_constant_INT_MAX");
  make_test_of_int_constant (ctxt,
                             int_type,
                             INT_MIN,
                             "test_int_constant_INT_MIN");
}

static void
verify_int_constants (gcc_jit_result *result)
{
  typedef int (*test_fn) (void);

  test_fn test_int_constant_0 =
    (test_fn)gcc_jit_result_get_code (result,
				      "test_int_constant_0");
  CHECK_NON_NULL (test_int_constant_0);
  CHECK_VALUE (test_int_constant_0 (), 0);

  test_fn test_int_constant_INT_MAX =
    (test_fn)gcc_jit_result_get_code (result,
				      "test_int_constant_INT_MAX");
  CHECK_NON_NULL (test_int_constant_INT_MAX);
  CHECK_VALUE (test_int_constant_INT_MAX (), INT_MAX);

  test_fn test_int_constant_INT_MIN =
    (test_fn)gcc_jit_result_get_code (result,
				      "test_int_constant_INT_MIN");
  CHECK_NON_NULL (test_int_constant_INT_MIN);
  CHECK_VALUE (test_int_constant_INT_MIN (), INT_MIN);
}

/**********************************************************************
 Tests of gcc_jit_context_new_rvalue_from_long.
 **********************************************************************/

static const char *
make_test_of_long_constant (gcc_jit_context *ctxt,
			   gcc_jit_type *type,
			   long value,
			   const char *funcname)
{
  /* Make a test function of the form:
       long funcname (void)
       {
	  return VALUE;
       }
     and return a debug dump of VALUE so that
     the caller can sanity-check the debug dump implementation.
  */
  gcc_jit_rvalue *rvalue =
    gcc_jit_context_new_rvalue_from_long (ctxt, type, value);
  make_test_of_constant (ctxt, type, rvalue, funcname);
  return gcc_jit_object_get_debug_string (
    gcc_jit_rvalue_as_object (rvalue));
}

static void
make_tests_of_long_constants (gcc_jit_context *ctxt)
{
  gcc_jit_type *long_type =
    gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_LONG);

  CHECK_STRING_VALUE (
    make_test_of_long_constant (ctxt,
			       long_type,
			       0,
			       "test_long_constant_0"),
    "(long)0");
  make_test_of_long_constant (ctxt,
                             long_type,
                             LONG_MAX,
                             "test_long_constant_LONG_MAX");
  make_test_of_long_constant (ctxt,
                             long_type,
                             LONG_MIN,
                             "test_long_constant_LONG_MIN");
}

static void
verify_long_constants (gcc_jit_result *result)
{
  typedef long (*test_fn) (void);

  test_fn test_long_constant_0 =
    (test_fn)gcc_jit_result_get_code (result,
				      "test_long_constant_0");
  CHECK_NON_NULL (test_long_constant_0);
  CHECK_VALUE (test_long_constant_0 (), 0);

  test_fn test_long_constant_LONG_MAX =
    (test_fn)gcc_jit_result_get_code (result,
				      "test_long_constant_LONG_MAX");
  CHECK_NON_NULL (test_long_constant_LONG_MAX);
  CHECK_VALUE (test_long_constant_LONG_MAX (), LONG_MAX);

  test_fn test_long_constant_LONG_MIN =
    (test_fn)gcc_jit_result_get_code (result,
				      "test_long_constant_LONG_MIN");
  CHECK_NON_NULL (test_long_constant_LONG_MIN);
  CHECK_VALUE (test_long_constant_LONG_MIN (), LONG_MIN);
}

/**********************************************************************
 Tests of gcc_jit_context_new_rvalue_from_double.
 **********************************************************************/

static const char *
make_test_of_double_constant (gcc_jit_context *ctxt,
			   gcc_jit_type *type,
			   double value,
			   const char *funcname)
{
  /* Make a test function of the form:
       double funcname (void)
       {
	  return VALUE;
       }
     and return a debug dump of VALUE so that
     the caller can sanity-check the debug dump implementation.
  */
  gcc_jit_rvalue *rvalue =
    gcc_jit_context_new_rvalue_from_double (ctxt, type, value);
  make_test_of_constant (ctxt, type, rvalue, funcname);
  return gcc_jit_object_get_debug_string (
    gcc_jit_rvalue_as_object (rvalue));
}

static void
make_tests_of_double_constants (gcc_jit_context *ctxt)
{
  gcc_jit_type *double_type =
    gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_DOUBLE);

  make_test_of_double_constant (ctxt,
                                double_type,
                                0.5,
                                "test_double_constant_0_5");
  make_test_of_double_constant (ctxt,
                                double_type,
                                1e100,
                                "test_double_constant_1e100");
  make_test_of_double_constant (ctxt,
                                double_type,
                                DBL_MIN,
                                "test_double_constant_DBL_MIN");
  make_test_of_double_constant (ctxt,
                                double_type,
                                DBL_MAX,
                                "test_double_constant_DBL_MAX");
}

static void
verify_double_constants (gcc_jit_result *result)
{
  typedef double (*test_fn) (void);

  test_fn test_double_constant_0_5 =
    (test_fn)gcc_jit_result_get_code (result,
				      "test_double_constant_0_5");
  CHECK_NON_NULL (test_double_constant_0_5);
  CHECK_VALUE (test_double_constant_0_5 (), 0.5);

  test_fn test_double_constant_1e100 =
    (test_fn)gcc_jit_result_get_code (result,
				      "test_double_constant_1e100");
  CHECK_NON_NULL (test_double_constant_1e100);
  CHECK_VALUE (test_double_constant_1e100 (), 1e100);

  test_fn test_double_constant_DBL_MIN =
    (test_fn)gcc_jit_result_get_code (result,
				      "test_double_constant_DBL_MIN");
  CHECK_NON_NULL (test_double_constant_DBL_MIN);
  CHECK_VALUE (test_double_constant_DBL_MIN (), DBL_MIN);

  test_fn test_double_constant_DBL_MAX =
    (test_fn)gcc_jit_result_get_code (result,
				      "test_double_constant_DBL_MAX");
  CHECK_NON_NULL (test_double_constant_DBL_MAX);
  CHECK_VALUE (test_double_constant_DBL_MAX (), DBL_MAX);
}

/**********************************************************************
 Tests of gcc_jit_context_new_rvalue_from_ptr.
 **********************************************************************/

static const char *
make_test_of_ptr_constant (gcc_jit_context *ctxt,
			   gcc_jit_type *type,
			   void *value,
			   const char *funcname)
{
  /* Make a test function of the form:
       void *funcname (void)
       {
	  return VALUE;
       }
     and return a debug dump of VALUE so that
     the caller can sanity-check the debug dump implementation.
  */
  gcc_jit_rvalue *rvalue =
    gcc_jit_context_new_rvalue_from_ptr (ctxt, type, value);
  make_test_of_constant (ctxt, type, rvalue, funcname);
  return gcc_jit_object_get_debug_string (
    gcc_jit_rvalue_as_object (rvalue));
}

static void
make_tests_of_ptr_constants (gcc_jit_context *ctxt)
{
  gcc_jit_type *ptr_type =
    gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_VOID_PTR);

  CHECK_STRING_VALUE (
    make_test_of_ptr_constant (ctxt,
			       ptr_type,
			       0,
			       "test_ptr_constant_0"),
    "(void *)NULL");
  CHECK_STRING_VALUE (
    make_test_of_ptr_constant (ctxt,
			       ptr_type,
			       (void *)0xdeadbeef,
			       "test_ptr_constant_0xdeadbeef"),
    "(void *)0xdeadbeef");
}

static void
verify_ptr_constants (gcc_jit_result *result)
{
  typedef void *(*test_fn) (void);

  test_fn test_ptr_constant_0 =
    (test_fn)gcc_jit_result_get_code (result,
				      "test_ptr_constant_0");
  CHECK_NON_NULL (test_ptr_constant_0);
  CHECK_VALUE (test_ptr_constant_0 (), 0);

  test_fn test_ptr_constant_0xdeadbeef =
    (test_fn)gcc_jit_result_get_code (result,
				      "test_ptr_constant_0xdeadbeef");
  CHECK_NON_NULL (test_ptr_constant_0xdeadbeef);
  CHECK_VALUE (test_ptr_constant_0xdeadbeef (), (void *)0xdeadbeef);
}

/**********************************************************************
 Code for harness
 **********************************************************************/

void
create_code (gcc_jit_context *ctxt, void *user_data)
{
  make_tests_of_int_constants (ctxt);
  make_tests_of_long_constants (ctxt);
  make_tests_of_double_constants (ctxt);
  make_tests_of_ptr_constants (ctxt);
}

void
verify_code (gcc_jit_context *ctxt, gcc_jit_result *result)
{
  CHECK_NON_NULL (result);

  verify_int_constants (result);
  verify_long_constants (result);
  verify_double_constants (result);
  verify_ptr_constants (result);
}