1 #include <stdlib.h>
2 #include <stdio.h>
3 
4 #include "libgccjit.h"
5 
6 #include "harness.h"
7 
8 /* Reproducer for PR jit/66779.
9 
10    Inject the equivalent of:
11      T FUNCNAME (T i, T j, T k)
12      {
13        bool comp0 = i & 0x40;
14        bool comp1 = (j == k);
15        if (comp0 && comp1)
16 	 return 7;
17        else
18 	 return 22;
19      }
20    for some type T; this was segfaulting during the expansion to RTL
21    due to missing handling for some machine modes in
22    jit_langhook_type_for_mode.  */
23 
24 void
create_fn(gcc_jit_context * ctxt,const char * funcname,enum gcc_jit_types jit_type)25 create_fn (gcc_jit_context *ctxt,
26 	   const char *funcname,
27 	   enum gcc_jit_types jit_type)
28 {
29   gcc_jit_type *the_type =
30     gcc_jit_context_get_type (ctxt, jit_type);
31   gcc_jit_type *t_bool =
32     gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_BOOL);
33   gcc_jit_param *param_i =
34     gcc_jit_context_new_param (ctxt, NULL, the_type, "i");
35   gcc_jit_param *param_j =
36     gcc_jit_context_new_param (ctxt, NULL, the_type, "j");
37   gcc_jit_param *param_k =
38     gcc_jit_context_new_param (ctxt, NULL, the_type, "k");
39   gcc_jit_param *params[3] = {
40     param_i,
41     param_j,
42     param_k
43   };
44   gcc_jit_function *func =
45     gcc_jit_context_new_function (ctxt, NULL,
46 				  GCC_JIT_FUNCTION_EXPORTED,
47 				  the_type,
48 				  funcname,
49 				  3, params,
50 				  0);
51   gcc_jit_block *b_entry = gcc_jit_function_new_block (func, "entry");
52   gcc_jit_block *b_on_true = gcc_jit_function_new_block (func, "on_true");
53   gcc_jit_block *b_on_false = gcc_jit_function_new_block (func, "on_false");
54 
55   gcc_jit_lvalue *comp0 =
56     gcc_jit_function_new_local (func, NULL, t_bool, "comp0");
57 
58   gcc_jit_block_add_assignment (
59     b_entry, NULL,
60     comp0,
61     gcc_jit_context_new_comparison (
62       ctxt, NULL,
63       GCC_JIT_COMPARISON_NE,
64       gcc_jit_context_new_binary_op (
65 	ctxt, NULL,
66 	GCC_JIT_BINARY_OP_BITWISE_AND,
67 	the_type,
68 	gcc_jit_param_as_rvalue (param_i),
69 	gcc_jit_context_new_rvalue_from_int (ctxt, the_type, 0x40)),
70       gcc_jit_context_zero (ctxt, the_type)));
71 
72   gcc_jit_lvalue *comp1 =
73     gcc_jit_function_new_local (func, NULL, t_bool, "comp1");
74 
75   gcc_jit_block_add_assignment (
76     b_entry, NULL,
77     comp1,
78     gcc_jit_context_new_comparison (ctxt, NULL,
79 				    GCC_JIT_COMPARISON_EQ,
80 				    gcc_jit_param_as_rvalue (param_j),
81 				    gcc_jit_param_as_rvalue (param_k)));
82 
83  gcc_jit_rvalue *cond =
84    gcc_jit_context_new_binary_op (ctxt, NULL,
85 				  GCC_JIT_BINARY_OP_LOGICAL_AND,
86 				  t_bool,
87 				  gcc_jit_lvalue_as_rvalue (comp0),
88 				  gcc_jit_lvalue_as_rvalue (comp1));
89 
90   gcc_jit_block_end_with_conditional (b_entry, NULL,
91 				      cond,
92 				      b_on_true,
93 				      b_on_false);
94 
95   gcc_jit_block_end_with_return (
96     b_on_true, NULL,
97     gcc_jit_context_new_rvalue_from_int (ctxt, the_type, 7));
98 
99   gcc_jit_block_end_with_return (
100     b_on_false, NULL,
101     gcc_jit_context_new_rvalue_from_int (ctxt, the_type, 22));
102 }
103 
104 void
create_code(gcc_jit_context * ctxt,void * user_data)105 create_code (gcc_jit_context *ctxt, void *user_data)
106 {
107   create_fn (ctxt, "pr66779_signed_char", GCC_JIT_TYPE_SIGNED_CHAR);
108   create_fn (ctxt, "pr66779_unsigned_char", GCC_JIT_TYPE_UNSIGNED_CHAR);
109 
110   create_fn (ctxt, "pr66779_short", GCC_JIT_TYPE_SHORT);
111   create_fn (ctxt, "pr66779_unsigned_short", GCC_JIT_TYPE_UNSIGNED_SHORT);
112 
113   create_fn (ctxt, "pr66779_int", GCC_JIT_TYPE_INT);
114   create_fn (ctxt, "pr66779_unsigned_int", GCC_JIT_TYPE_UNSIGNED_INT);
115 
116   create_fn (ctxt, "pr66779_long", GCC_JIT_TYPE_LONG);
117   create_fn (ctxt, "pr66779_unsigned_long", GCC_JIT_TYPE_UNSIGNED_LONG);
118 
119   create_fn (ctxt, "pr66779_long_long",
120 	     GCC_JIT_TYPE_LONG_LONG);
121   create_fn (ctxt, "pr66779_unsigned_long_long",
122 	     GCC_JIT_TYPE_UNSIGNED_LONG_LONG);
123 
124   create_fn (ctxt, "pr66779_size_t", GCC_JIT_TYPE_SIZE_T);
125 }
126 
127 extern void
verify_code(gcc_jit_context * ctxt,gcc_jit_result * result)128 verify_code (gcc_jit_context *ctxt, gcc_jit_result *result)
129 {
130   typedef int (*fn_type) (int, int, int);
131   CHECK_NON_NULL (result);
132   /* Sanity-check the "int" case.  */
133   fn_type fn =
134     (fn_type)gcc_jit_result_get_code (result, "pr66779_int");
135   CHECK_NON_NULL (fn);
136   CHECK_VALUE (fn (0, 0, 0), 22);
137   CHECK_VALUE (fn (0, 0, 1), 22);
138   CHECK_VALUE (fn (0x40, 0, 0), 7);
139   CHECK_VALUE (fn (0x40, 0, 1), 22);
140   CHECK_VALUE (fn (0x40, 1, 1), 7);
141   CHECK_VALUE (fn (0x3f, 0, 0), 22);
142   CHECK_VALUE (fn (0x3f, 1, 1), 22);
143 }
144