aboutsummaryrefslogtreecommitdiff
path: root/gcc/testsuite/jit.dg/test-arith-overflow.c
blob: ac2fa361089d24d845c05136b0b1f12aa13b8f26 (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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <limits.h>
#include <stdbool.h>

#include "libgccjit.h"

#include "harness.h"

static void
create_overflow_fn (gcc_jit_context *ctxt,
		    gcc_jit_type *type,
		    const char *funcname,
		    const char *builtin_name)
{
  /* Create the equivalent of this C:

       int
       test_overflow_T_OP (T x, T y, bool *ovf)
       {
	 T result;
	 result = x OP y;
	 *ovf = ...; // did overflow happen?
	 return result;
       }

  */
  gcc_jit_type *t_bool =
    gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_BOOL);
  gcc_jit_type *t_bool_star =
    gcc_jit_type_get_pointer (t_bool);

  gcc_jit_param *x =
    gcc_jit_context_new_param (
      ctxt,
      NULL,
      type, "x");
  gcc_jit_param *y =
    gcc_jit_context_new_param (
      ctxt,
      NULL,
      type, "y");
  gcc_jit_param *ovf =
    gcc_jit_context_new_param (
      ctxt,
      NULL,
      t_bool_star, "ovf");
  gcc_jit_param *params[3] = {x, y, ovf};

  gcc_jit_function *func =
    gcc_jit_context_new_function (ctxt,
				  NULL,
				  GCC_JIT_FUNCTION_EXPORTED,
				  type,
				  funcname,
				  3, params, 0);

  gcc_jit_lvalue *result =
    gcc_jit_function_new_local (func, NULL, type, "result");

  gcc_jit_block *b_initial =
    gcc_jit_function_new_block (func, "initial");

  /* The builtins are listed in builtins.def as being variadic, but
     the have these signatures:
       bool __builtin_add_overflow (type1 a, type2 b, type3 *res);
       bool __builtin_sub_overflow (type1 a, type2 b, type3 *res);
       bool __builtin_mul_overflow (type1 a, type2 b, type3 *res);  */

  gcc_jit_function *builtin_fn =
    gcc_jit_context_get_builtin_function (ctxt, builtin_name);

  /* Construct a call of the form:
       (returns bool) __builtin_add_overflow (x, y, &result).  */
  gcc_jit_rvalue *args[3] = {gcc_jit_param_as_rvalue (x),
			     gcc_jit_param_as_rvalue (y),
			     gcc_jit_lvalue_get_address (result, NULL)};
  gcc_jit_rvalue *call =
    gcc_jit_context_new_call (ctxt,
			      NULL,
			      builtin_fn,
			      3, args);

  /* "*ovf = BUILTIN_CALL ();" */
  gcc_jit_block_add_assignment (
    b_initial, NULL,
    gcc_jit_rvalue_dereference (gcc_jit_param_as_rvalue (ovf),
				NULL),
    call);

  /* "return result;" */
  gcc_jit_block_end_with_return (
    b_initial, NULL,
    gcc_jit_lvalue_as_rvalue (result));
}

void
create_code (gcc_jit_context *ctxt, void *user_data)
{
  /* int */
  gcc_jit_type *int_type =
    gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_INT);
  create_overflow_fn (ctxt, int_type,
		      "test_overflow_int_add",
		      "__builtin_add_overflow");
  create_overflow_fn (ctxt, int_type,
		      "test_overflow_int_sub",
		      "__builtin_sub_overflow");
  create_overflow_fn (ctxt, int_type,
		      "test_overflow_int_mul",
		      "__builtin_mul_overflow");

  /* uint */
  gcc_jit_type *uint_type =
    gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_UNSIGNED_INT);
  create_overflow_fn (ctxt, uint_type,
		      "test_overflow_uint_add",
		      "__builtin_add_overflow");
  create_overflow_fn (ctxt, uint_type,
		      "test_overflow_uint_sub",
		      "__builtin_sub_overflow");
  create_overflow_fn (ctxt, uint_type,
		      "test_overflow_uint_mul",
		      "__builtin_mul_overflow");
}

void
verify_int_overflow_fn (gcc_jit_result *jit_result,
			const char *funcname,
			int x, int y,
			int expected_result,
			bool expected_ovf)
{
  CHECK_NON_NULL (jit_result);
  typedef int (*overflow_fn_type) (int, int, bool *);
  overflow_fn_type fn =
    (overflow_fn_type)gcc_jit_result_get_code (jit_result, funcname);
  CHECK_NON_NULL (fn);

  /* Call the function:  */
  bool actual_ovf = 0;
  int actual_result = fn (x, y, &actual_ovf);
  note ("%s (%d, %d) returned: %d with ovf: %d",
	funcname, x, y, actual_result, actual_ovf);
  CHECK_VALUE (actual_result, expected_result);
  CHECK_VALUE (actual_ovf, expected_ovf);
}

void
verify_uint_overflow_fn (gcc_jit_result *jit_result,
			 const char *funcname,
			 unsigned int x, unsigned int y,
			 unsigned int expected_result,
			 bool expected_ovf)
{
  CHECK_NON_NULL (jit_result);
  typedef unsigned int (*overflow_fn_type) (unsigned int, unsigned int,
					    bool *);
  overflow_fn_type fn =
    (overflow_fn_type)gcc_jit_result_get_code (jit_result, funcname);
  CHECK_NON_NULL (fn);

  /* Call the function:  */
  bool actual_ovf = 0;
  unsigned int actual_result = fn (x, y, &actual_ovf);
  note ("%s (%d, %d) returned: %d with ovf: %d",
	funcname, x, y, actual_result, actual_ovf);
  CHECK_VALUE (actual_result, expected_result);
  CHECK_VALUE (actual_ovf, expected_ovf);
}

void
verify_code (gcc_jit_context *ctxt, gcc_jit_result *result)
{
  verify_int_overflow_fn (result, "test_overflow_int_add",
			  5, 15,
			  20, 0);
  verify_int_overflow_fn (result, "test_overflow_int_add",
			  INT_MAX, 1,
			  INT_MIN, 1);
  verify_int_overflow_fn (result, "test_overflow_int_sub",
			  5, 15,
			  -10, 0);
  verify_int_overflow_fn (result, "test_overflow_int_sub",
			  INT_MIN, 1,
			  INT_MAX, 1);
  verify_int_overflow_fn (result, "test_overflow_int_mul",
			  5, 15,
			  75, 0);
  verify_int_overflow_fn (result, "test_overflow_int_mul",
			  INT_MAX, 1,
			  INT_MAX, 0);
  verify_int_overflow_fn (result, "test_overflow_int_mul",
			  INT_MAX, 2,
			  -2, 1);

  verify_uint_overflow_fn (result, "test_overflow_uint_add",
			   5, 15,
			   20, 0);
  verify_uint_overflow_fn (result, "test_overflow_uint_add",
			   INT_MAX, 1,
			   (((unsigned int)INT_MAX) + 1), 0);
  verify_uint_overflow_fn (result, "test_overflow_uint_add",
			   UINT_MAX, 1,
			   0, 1);
  verify_uint_overflow_fn (result, "test_overflow_uint_sub",
			   5, 15,
			   (UINT_MAX - 9), 1);
  verify_uint_overflow_fn (result, "test_overflow_uint_sub",
			   INT_MIN, 1,
			   ((unsigned int)INT_MIN - 1), 0);
  verify_uint_overflow_fn (result, "test_overflow_uint_mul",
			   5, 15,
			   75, 0);
  verify_uint_overflow_fn (result, "test_overflow_uint_mul",
			   INT_MAX, 1,
			   INT_MAX, 0);
  verify_uint_overflow_fn (result, "test_overflow_uint_mul",
			   INT_MAX, 2,
			   (unsigned int)INT_MAX * 2, 0);
  verify_uint_overflow_fn (result, "test_overflow_uint_mul",
			   UINT_MAX, 2,
			   -2/*(unsigned int)INT_MAX * 2*/, 1);
}