2017-07-25 Richard Biener <rguenther@suse.de>
[official-gcc.git] / gcc / testsuite / jit.dg / test-compound-assignment.c
blob8b82e09808c8b045042c986552d55e6d6f45f0e0
1 #include <stdlib.h>
2 #include <stdio.h>
4 #include "libgccjit.h"
6 #include "harness.h"
8 struct assignable_struct
10 int a;
11 char b;
12 float c;
15 union assignable_union
17 int a;
18 char b;
19 float c;
22 /* Verify that compound assignment works; let's try to inject the
23 equivalent of:
25 struct assignable_struct
26 test_struct_assignment (struct assignable_struct x)
28 struct assignable_struct y, z;
29 y = x;
30 z = y;
31 return z;
34 and the same, for "union assignable_union". */
36 /* Make the type "struct assignable_struct" or "union assignable_union". */
38 static gcc_jit_type *
39 make_type (gcc_jit_context *ctxt, int make_union)
41 gcc_jit_type *t_int =
42 gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_INT);
43 gcc_jit_type *t_char =
44 gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_CHAR);
45 gcc_jit_type *t_float =
46 gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_FLOAT);
48 gcc_jit_field *a =
49 gcc_jit_context_new_field (ctxt,
50 NULL,
51 t_int,
52 "a");
53 gcc_jit_field *b =
54 gcc_jit_context_new_field (ctxt,
55 NULL,
56 t_char,
57 "b");
58 gcc_jit_field *c =
59 gcc_jit_context_new_field (ctxt,
60 NULL,
61 t_float,
62 "c");
63 gcc_jit_field *fields[] = {a, b, c};
64 if (make_union)
65 return gcc_jit_context_new_union_type (ctxt, NULL,
66 "assignable_union",
67 3, fields);
68 else
69 return gcc_jit_struct_as_type (
70 gcc_jit_context_new_struct_type (ctxt, NULL,
71 "assignable_struct",
72 3, fields));
75 static void
76 make_function (gcc_jit_context *ctxt, int make_union, const char *funcname)
78 gcc_jit_type *test_type = make_type (ctxt, make_union);
79 gcc_jit_param *x =
80 gcc_jit_context_new_param (ctxt, NULL,
81 test_type, "x");
82 gcc_jit_function *fn =
83 gcc_jit_context_new_function (ctxt, NULL,
84 GCC_JIT_FUNCTION_EXPORTED,
85 test_type,
86 funcname,
87 1, &x,
88 0);
89 gcc_jit_lvalue *y =
90 gcc_jit_function_new_local (fn, NULL, test_type, "y");
91 gcc_jit_lvalue *z =
92 gcc_jit_function_new_local (fn, NULL, test_type, "z");
93 gcc_jit_block *block =
94 gcc_jit_function_new_block (fn, NULL);
95 gcc_jit_block_add_assignment (block, NULL,
96 y, gcc_jit_param_as_rvalue (x));
97 gcc_jit_block_add_assignment (block, NULL,
98 z, gcc_jit_lvalue_as_rvalue (y));
99 gcc_jit_block_end_with_return (block, NULL,
100 gcc_jit_lvalue_as_rvalue (z));
103 void
104 create_code (gcc_jit_context *ctxt, void *user_data)
106 make_function (ctxt, 0, "test_struct_assignment");
107 make_function (ctxt, 1, "test_union_assignment");
110 static void
111 verify_test_struct_assignment (gcc_jit_result *result)
113 typedef struct assignable_struct (*fn_type) (struct assignable_struct);
114 fn_type test_struct_assignment =
115 (fn_type)gcc_jit_result_get_code (result, "test_struct_assignment");
116 CHECK_NON_NULL (test_struct_assignment);
118 struct assignable_struct s, t;
119 s.a = 500;
120 s.b = 'A';
121 s.c = 1.0;
122 t = test_struct_assignment (s);
123 CHECK_VALUE (t.a, 500);
124 CHECK_VALUE (t.b, 'A');
125 CHECK_VALUE (t.c, 1.0);
128 static void
129 verify_test_union_assignment (gcc_jit_result *result)
131 typedef union assignable_union (*fn_type) (union assignable_union);
132 fn_type test_union_assignment =
133 (fn_type)gcc_jit_result_get_code (result, "test_union_assignment");
134 CHECK_NON_NULL (test_union_assignment);
136 union assignable_union p, q;
138 p.a = 500;
139 q = test_union_assignment (p);
140 CHECK_VALUE (q.a, 500);
142 p.b = 'A';
143 q = test_union_assignment (p);
144 CHECK_VALUE (q.b, 'A');
146 p.c = 1.0;
147 q = test_union_assignment (p);
148 CHECK_VALUE (q.c, 1.0);
151 void
152 verify_code (gcc_jit_context *ctxt, gcc_jit_result *result)
154 CHECK_NON_NULL (result);
155 verify_test_struct_assignment (result);
156 verify_test_union_assignment (result);