blob: 95815fdd47009b91438285bf3683ed1f37c54909 [file] [log] [blame]
Mike Stump46ae64f2010-01-07 18:58:28 +00001#include <stdio.h>
2#include <stdlib.h>
3
4#define N_FIELDS 7
5#define N_FUNCS 128
Mike Stump72e933e2010-01-12 03:01:18 +00006#define FUNCSPACING 20
7#define N_STRUCTS 180 /* 1280 */
8#define N_BASES 6
9#define COVARIANT 0
Mike Stump46ae64f2010-01-07 18:58:28 +000010
11const char *simple_types[] = { "bool", "char", "short", "int", "float",
12 "double", "long double", "wchar_t", "void *",
13 "char *"
14};
15
16void gl(const char *c) {
17 printf("%s\n", c);
18}
19
20void g(const char *c) {
21 printf("%s", c);
22}
23
24void g(int i) {
25 printf("%d", i);
26}
27
Mike Stump82f0be92010-01-07 20:55:28 +000028int uuid = 0;
Mike Stump46ae64f2010-01-07 18:58:28 +000029char base_present[N_STRUCTS][N_STRUCTS];
Mike Stump3d3744c2010-01-08 19:25:36 +000030char funcs_present[N_STRUCTS][N_FUNCS*FUNCSPACING];
Mike Stump46ae64f2010-01-07 18:58:28 +000031
Mike Stumpcffd5162010-01-12 20:55:39 +000032// The return type for each function when doing covariant testcase generation.
33short ret_types[N_STRUCTS][N_FUNCS*FUNCSPACING];
34
Mike Stump46ae64f2010-01-07 18:58:28 +000035bool is_ambiguous(int s, int base) {
36 for (int i = 0; i < N_STRUCTS; ++i) {
Mike Stump3d3744c2010-01-08 19:25:36 +000037 if ((base_present[base][i] & base_present[s][i]) == 1)
Mike Stump46ae64f2010-01-07 18:58:28 +000038 return true;
39 }
40 return false;
41}
42
43void add_bases(int s, int base) {
44 for (int i = 0; i < N_STRUCTS; ++i)
45 base_present[s][i] |= base_present[base][i];
Mike Stumpcffd5162010-01-12 20:55:39 +000046 if (!COVARIANT)
47 return;
48 for (int i = 0; i < N_FUNCS*FUNCSPACING; ++i) {
49 if (!ret_types[base][i])
50 continue;
51 if (!ret_types[s][i]) {
52 ret_types[s][i] = ret_types[base][i];
53 continue;
54 }
55 if (base_present[ret_types[base][i]][ret_types[s][i]])
56 // If the return type of the function from this base dominates
57 ret_types[s][i] = ret_types[base][i];
58 if (base_present[ret_types[s][i]][ret_types[base][i]])
59 // If a previous base dominates
60 continue;
61 // If neither dominates, we'll use this class.
62 ret_types[s][i] = s;
63 }
Mike Stump46ae64f2010-01-07 18:58:28 +000064}
65
Mike Stump3d3744c2010-01-08 19:25:36 +000066// This contains the class that has the final override for
67// each class, for each function.
68short final_override[N_STRUCTS][N_FUNCS*FUNCSPACING];
69
Mike Stump46ae64f2010-01-07 18:58:28 +000070void gs(int s) {
71 bool polymorphic = false;
72
73 static int bases[N_BASES];
74 int i_bases = random() % (N_BASES*2);
75 if (i_bases >= N_BASES)
76 // PARAM: 1/2 of all clases should have no bases
77 i_bases = 0;
78 int n_bases = 0;
79 bool first_base = true;
80
81 // PARAM: 3/4 of all should be class, the rest are structs
82 if (random() % 4 == 0)
83 g("struct s");
84 else
85 g("class s");
86 g(s);
87 int old_base = -1;
Mike Stump3d3744c2010-01-08 19:25:36 +000088 if (s == 0 || s == 1)
Mike Stump46ae64f2010-01-07 18:58:28 +000089 i_bases = 0;
90 while (i_bases) {
91 --i_bases;
Mike Stump3d3744c2010-01-08 19:25:36 +000092 int base = random() % (s-1) + 1;
Mike Stump46ae64f2010-01-07 18:58:28 +000093 if (!base_present[s][base]) {
94 if (is_ambiguous(s, base))
95 continue;
96 if (first_base) {
97 first_base = false;
98 g(": ");
99 } else
100 g(", ");
101 int base_type = 1;
102 if (random()%8 == 0) {
103 // PARAM: 1/8th the bases are virtual
104 g("virtual ");
Mike Stump3d3744c2010-01-08 19:25:36 +0000105 // We have a vtable and rtti, but technically we're not polymorphic
106 // polymorphic = true;
Mike Stump46ae64f2010-01-07 18:58:28 +0000107 base_type = 3;
108 }
Mike Stump72e933e2010-01-12 03:01:18 +0000109 // PARAM: 1/4 are public, 1/8 are privare, 1/8 are protected, the reset, default
110 int base_protection = 0;
111 if (!COVARIANT)
112 base_protection = random()%8;
113 switch (base_protection) {
Mike Stump46ae64f2010-01-07 18:58:28 +0000114 case 0:
115 case 1:
Mike Stump72e933e2010-01-12 03:01:18 +0000116 g("public "); break;
Mike Stump46ae64f2010-01-07 18:58:28 +0000117 case 2:
118 case 3:
Mike Stump46ae64f2010-01-07 18:58:28 +0000119 case 4:
120 case 5:
Mike Stump72e933e2010-01-12 03:01:18 +0000121 break;
Mike Stump46ae64f2010-01-07 18:58:28 +0000122 case 6:
123 g("private "); break;
124 case 7:
125 g("protected "); break;
126 }
127 g("s");
128 add_bases(s, base);
129 bases[n_bases] = base;
130 base_present[s][base] = base_type;
131 ++n_bases;
132 g(base);
133 old_base = base;
134 }
135 }
136 gl(" {");
137
138 /* Fields */
Mike Stump66d29ec2010-01-12 00:28:59 +0000139 int n_fields = N_FIELDS == 0 ? 0 : random() % (N_FIELDS*4);
Mike Stump46ae64f2010-01-07 18:58:28 +0000140 // PARAM: 3/4 of all structs should have no members
141 if (n_fields >= N_FIELDS)
142 n_fields = 0;
143 for (int i = 0; i < n_fields; ++i) {
144 int t = random() % (sizeof(simple_types) / sizeof(simple_types[0]));
145 g(" "); g(simple_types[t]); g(" field"); g(i); gl(";");
146 }
147
148 /* Virtual functions */
Mike Stump3d3744c2010-01-08 19:25:36 +0000149 static int funcs[N_FUNCS*FUNCSPACING];
Mike Stump82f0be92010-01-07 20:55:28 +0000150 // PARAM: 1/2 of all structs should have no virtual functions
151 int n_funcs = random() % (N_FUNCS*2);
152 if (n_funcs > N_FUNCS)
153 n_funcs = 0;
Mike Stump46ae64f2010-01-07 18:58:28 +0000154 int old_func = -1;
155 for (int i = 0; i < n_funcs; ++i) {
156 int fn = old_func + random() % FUNCSPACING + 1;
157 funcs[i] = fn;
Mike Stumpcffd5162010-01-12 20:55:39 +0000158 int ret_type = 0;
159 if (COVARIANT) {
160 ret_type = random() % s + 1;
161 if (!base_present[s][ret_type]
162 || !base_present[ret_type][ret_types[s][fn]])
163 if (ret_types[s][fn]) {
164 printf(" // Found one for s%d for s%d* fun%d.\n", s,
165 ret_types[s][fn], fn);
166 ret_type = ret_types[s][fn];
167 } else
168 ret_type = s;
169 else
170 printf(" // Wow found one for s%d for fun%d.\n", s, fn);
171 ret_types[s][fn] = ret_type;
172 }
173 if (ret_type) {
174 g(" virtual s"); g(ret_type); g("* fun");
Mike Stump72e933e2010-01-12 03:01:18 +0000175 } else
176 g(" virtual void fun");
177 g(fn); g("(char *t) { mix(\"vfn this offset\", (char *)this - t); mix(\"vfn uuid\", "); g(++uuid);
Mike Stumpcffd5162010-01-12 20:55:39 +0000178 if (ret_type)
Mike Stump72e933e2010-01-12 03:01:18 +0000179 gl("); return 0; }");
180 else
181 gl("); }");
Mike Stump3d3744c2010-01-08 19:25:36 +0000182 funcs_present[s][fn] = 1;
183 final_override[s][fn] = s;
Mike Stump46ae64f2010-01-07 18:58:28 +0000184 old_func = fn;
185 }
186
Mike Stump3d3744c2010-01-08 19:25:36 +0000187 // Add required overriders for correctness
188 for (int i = 0; i < n_bases; ++i) {
189 // For each base
190 int base = bases[i];
191 for (int fn = 0; fn < N_FUNCS*FUNCSPACING; ++fn) {
192 // For each possible function
193 int new_base = final_override[base][fn];
194 if (new_base == 0)
195 // If the base didn't have a final overrider, skip
196 continue;
197
198 int prev_base = final_override[s][fn];
199 if (prev_base == s)
200 // Skip functions defined in this class
201 continue;
202
203 // If we don't want to change the info, skip
204 if (prev_base == new_base)
205 continue;
206
207 if (prev_base == 0) {
208 // record the final override
209 final_override[s][fn] = new_base;
210 continue;
211 }
212
213 if (base_present[prev_base][new_base]) {
214 // The previous base dominates the new base, no update necessary
Mike Stumpcffd5162010-01-12 20:55:39 +0000215 printf(" // No override for fun%d in s%d as s%d dominates s%d.\n",
216 fn, s, prev_base, new_base);
Mike Stump3d3744c2010-01-08 19:25:36 +0000217 continue;
218 }
219
220 if (base_present[new_base][prev_base]) {
221 // The new base dominates the old base, no override necessary
Mike Stumpcffd5162010-01-12 20:55:39 +0000222 printf(" // No override for fun%d in s%d as s%d dominates s%d.\n",
223 fn, s, new_base, prev_base);
Mike Stump3d3744c2010-01-08 19:25:36 +0000224 // record the final override
225 final_override[s][fn] = new_base;
226 continue;
227 }
228
Mike Stumpa8ae9f82010-01-08 19:28:41 +0000229 printf(" // Found we needed override for fun%d in s%d.\n", fn, s);
Mike Stump3d3744c2010-01-08 19:25:36 +0000230
231 // record the final override
232 funcs[n_funcs++] = fn;
233 if (n_funcs == (N_FUNCS*FUNCSPACING-1))
234 abort();
Mike Stumpcffd5162010-01-12 20:55:39 +0000235 int ret_type = 0;
236 if (COVARIANT) {
237 if (!ret_types[s][fn]) {
238 ret_types[s][fn] = ret_type = s;
239 } else {
240 ret_type = ret_types[s][fn];
241 if (ret_type != s)
242 printf(" // Calculated return type in s%d as s%d* fun%d.\n",
243 s, ret_type, fn);
244 }
245 }
246 if (ret_type) {
247 g(" virtual s"); g(ret_type); g("* fun");
Mike Stump72e933e2010-01-12 03:01:18 +0000248 } else
249 g(" virtual void fun");
250 g(fn); g("(char *t) { mix(\"vfn this offset\", (char *)this - t); mix(\"vfn uuid\", "); g(++uuid);
Mike Stumpcffd5162010-01-12 20:55:39 +0000251 if (ret_type)
Mike Stump72e933e2010-01-12 03:01:18 +0000252 gl("); return 0; }");
253 else
254 gl("); }");
Mike Stump3d3744c2010-01-08 19:25:36 +0000255 funcs_present[s][fn] = 1;
256 final_override[s][fn] = s;
257 }
258 }
259
Mike Stump46ae64f2010-01-07 18:58:28 +0000260 gl("public:");
261 gl(" void calc(char *t) {");
262
263 // mix in the type number
Mike Stump365d6382010-01-07 19:39:43 +0000264 g(" mix(\"type num\", "); g(s); gl(");");
Mike Stump46ae64f2010-01-07 18:58:28 +0000265 // mix in the size
Mike Stump365d6382010-01-07 19:39:43 +0000266 g(" mix(\"type size\", sizeof (s"); g(s); gl("));");
Mike Stump46ae64f2010-01-07 18:58:28 +0000267 // mix in the this offset
Mike Stump365d6382010-01-07 19:39:43 +0000268 gl(" mix(\"subobject offset\", (char *)this - t);");
Mike Stump46ae64f2010-01-07 18:58:28 +0000269 if (n_funcs)
270 polymorphic = true;
271 if (polymorphic) {
272 // mix in offset to the complete object under construction
Mike Stump365d6382010-01-07 19:39:43 +0000273 gl(" mix(\"real top v current top\", t - (char *)dynamic_cast<void*>(this));");
Mike Stump46ae64f2010-01-07 18:58:28 +0000274 }
275
276 /* check base layout and overrides */
277 for (int i = 0; i < n_bases; ++i) {
278 g(" calc_s"); g(bases[i]); gl("(t);");
279 }
280
281 if (polymorphic) {
282 /* check dynamic_cast to each direct base */
283 for (int i = 0; i < n_bases; ++i) {
284 g(" if ((char *)dynamic_cast<s"); g(bases[i]); gl("*>(this))");
Mike Stump365d6382010-01-07 19:39:43 +0000285 g(" mix(\"base dyn cast\", t - (char *)dynamic_cast<s"); g(bases[i]); gl("*>(this));");
Mike Stump82f0be92010-01-07 20:55:28 +0000286 g(" else mix(\"no dyncast\", "); g(++uuid); gl(");");
Mike Stump46ae64f2010-01-07 18:58:28 +0000287 }
288 }
289
290 /* check field layout */
291 for (int i = 0; i < n_fields; ++i) {
Mike Stump365d6382010-01-07 19:39:43 +0000292 g(" mix(\"field offset\", (char *)&field"); g(i); gl(" - (char *)this);");
Mike Stump46ae64f2010-01-07 18:58:28 +0000293 }
Mike Stump82f0be92010-01-07 20:55:28 +0000294 if (n_fields == 0) {
295 g(" mix(\"no fields\", "); g(++uuid); gl(");");
296 }
Mike Stump46ae64f2010-01-07 18:58:28 +0000297
298 /* check functions */
299 for (int i = 0; i < n_funcs; ++i) {
300 g(" fun"); g(funcs[i]); gl("(t);");
301 }
Mike Stump82f0be92010-01-07 20:55:28 +0000302 if (n_funcs == 0) {
303 g(" mix(\"no funcs\", "); g(++uuid); gl(");");
304 }
Mike Stump46ae64f2010-01-07 18:58:28 +0000305
306 gl(" }");
307
308 // default ctor
309 g(" s"); g(s); g("() ");
310 first_base = true;
311 for (int i = 0; i < n_bases; ++i) {
312 if (first_base) {
313 g(": ");
314 first_base = false;
315 } else
316 g(", ");
317 g("s"); g(bases[i]); g("((char *)this)");
318 }
319 gl(" { calc((char *)this); }");
320 g(" ~s"); g(s); gl("() { calc((char *)this); }");
321
322 // ctor with this to the complete object
323 g(" s"); g(s); gl("(char *t) { calc(t); }");
324 g(" void calc_s"); g(s); gl("(char *t) { calc(t); }");
325 g("} a"); g(s); gl(";");
326}
327
328main(int argc, char **argv) {
329 unsigned seed = 0;
330 char state[16];
331 if (argc > 1)
332 seed = atol(argv[1]);
333
334 initstate(seed, state, sizeof(state));
335 gl("extern \"C\" int printf(const char *...);");
336 gl("");
337 gl("long long sum;");
Mike Stump365d6382010-01-07 19:39:43 +0000338 gl("void mix(const char *desc, long long i) {");
Mike Stump46ae64f2010-01-07 18:58:28 +0000339 // If this ever becomes too slow, we can remove this after we improve the
340 // mixing function
Mike Stump365d6382010-01-07 19:39:43 +0000341 gl(" printf(\"%s: %lld\\n\", desc, i);");
Mike Stump46ae64f2010-01-07 18:58:28 +0000342 gl(" sum += ((sum ^ i) << 3) + (sum<<1) - i;");
343 gl("}");
Mike Stump46ae64f2010-01-07 18:58:28 +0000344 gl("");
345 // PARAM: Randomly size testcases or large testcases?
346 int n_structs = /* random() % */ N_STRUCTS;
Mike Stump3d3744c2010-01-08 19:25:36 +0000347 for (int i = 1; i < n_structs; ++i)
Mike Stump46ae64f2010-01-07 18:58:28 +0000348 gs(i);
349 gl("int main() {");
350 gl(" printf(\"%llx\\n\", sum);");
351 gl("}");
352 return 0;
353}