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
|
/* Miscellaneous simulator utilities.
Copyright (C) 1997-2024 Free Software Foundation, Inc.
Contributed by Cygnus Support.
This file is part of GDB, the GNU debugger.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
void
gen_struct (void)
{
printf ("\n");
printf ("typedef struct _test_tuples {\n");
printf (" int line;\n");
printf (" int row;\n");
printf (" int col;\n");
printf (" uint64_t val;\n");
printf (" uint64_t check;\n");
printf ("} test_tuples;\n");
printf ("\n");
printf ("typedef struct _test_spec {\n");
printf (" const char *file;\n");
printf (" const char *macro;\n");
printf (" int nr_rows;\n");
printf (" int nr_cols;\n");
printf (" test_tuples *tuples;\n");
printf ("} test_spec;\n");
}
void
gen_bit (int bitsize,
int msb,
const char *macro,
int nr_bits)
{
int i;
printf ("\n/* Test the %s macro */\n", macro);
printf ("test_tuples %s_tuples[%d] = {\n", macro, nr_bits);
for (i = 0; i < nr_bits; i++)
{
/* compute what we think the value is */
unsigned long long bit = 1;
if (msb == 0)
bit <<= nr_bits - i - 1;
else
bit <<= i;
if (bitsize == 32)
bit &= 0xffffffff; /* truncate it! */
/* write it out */
printf (" { __LINE__, ");
printf ("%d, %2d, ", -1, i);
printf ("%s (%2d), ", macro, i);
printf ("UNSIGNED64 (0x%016llx), ", bit);
printf ("},\n");
}
printf ("};\n");
printf ("\n");
printf ("test_spec %s_test = { __FILE__, \"%s\", 1, %d, %s_tuples, };\n",
macro, macro, nr_bits, macro);
printf ("\n");
}
void
gen_enum (const char *macro,
int nr_bits)
{
int i;
printf ("\n/* Test the %s macro in an enum */\n", macro);
printf ("enum enum_%s {\n", macro);
for (i = 0; i < nr_bits; i++)
{
printf (" elem_%s_%d = %s (%d),\n", macro, i, macro, i);
}
printf ("};\n");
printf ("\n");
}
void
gen_mask (int bitsize,
const char *msb,
const char *macro,
int nr_bits)
{
int l;
int h;
printf ("\n/* Test the %s%s macro */\n", msb, macro);
printf ("test_tuples %s_tuples[%d][%d] = {\n", macro, nr_bits, nr_bits);
for (l = 0; l < nr_bits; l++)
{
printf (" {\n");
for (h = 0; h < nr_bits; h++)
{
printf (" { __LINE__, ");
if ((strcmp (msb, "MS") == 0 && l <= h)
|| (strcmp (msb, "MS") != 0 && l >= h)
|| (strcmp (macro, "") == 0))
{
/* compute the mask */
unsigned long long mask = 0;
int b;
for (b = 0; b < nr_bits; b++)
{
unsigned long long bit = 1;
if (strcmp (msb, "MS") == 0)
{
if ((l <= b && b <= h)
|| (h < l && (b <= h || b >= l)))
bit <<= (nr_bits - b - 1);
else
bit = 0;
}
else
{
if ((l >= b && b >= h)
|| (h > l && (b >= h || b <= l)))
bit <<= b;
else
bit = 0;
}
mask |= bit;
}
if (bitsize == 32)
mask &= 0xffffffff;
printf ("%d, %d, ", l, h);
printf ("%s%s (%2d, %2d), ", msb, macro, l, h);
printf ("UNSIGNED64 (0x%llx), ", mask);
}
else
printf ("-1, -1, ");
printf ("},\n");
}
printf (" },\n");
}
printf ("};\n");
printf ("\n");
printf ("test_spec %s_test = { __FILE__, \"%s%s\", %d, %d, &%s_tuples[0][0], };\n",
macro, msb, macro, nr_bits, nr_bits, macro);
printf ("\n");
}
void
usage (int reason)
{
fprintf (stderr, "Usage:\n");
fprintf (stderr, " bits-gen <nr-bits> <msb> <byte-order>\n");
fprintf (stderr, "Generate a test case for the simulator bit manipulation code\n");
fprintf (stderr, " <nr-bits> = { 32 | 64 }\n");
fprintf (stderr, " <msb> = { 0 | { 31 | 63 } }\n");
fprintf (stderr, " <byte-order> = { big | little }\n");
switch (reason)
{
case 1: fprintf (stderr, "Wrong number of arguments\n");
break;
case 2:
fprintf (stderr, "Invalid <nr-bits> argument\n");
break;
case 3:
fprintf (stderr, "Invalid <msb> argument\n");
break;
case 4:
fprintf (stderr, "Invalid <byte-order> argument\n");
break;
}
exit (1);
}
int
main (int argc, char *argv[])
{
int bitsize;
int msb;
char *ms;
int big_endian;
if (argc != 4)
usage (1);
if (strcmp (argv [1], "32") == 0)
bitsize = 32;
else if (strcmp (argv [1], "64") == 0)
bitsize = 64;
else
usage (2);
if (strcmp (argv [2], "0") == 0)
msb = 0;
else if (strcmp (argv [2], "31") == 0 && bitsize == 32)
msb = 31;
else if (strcmp (argv [2], "63") == 0 && bitsize == 64)
msb = 63;
else
usage (3);
if (msb == 0)
ms = "MS";
else
ms = "LS";
if (strcmp (argv [3], "big") == 0)
big_endian = 1;
else if (strcmp (argv [3], "little") == 0)
big_endian = 0;
else
usage (4);
printf ("#define WITH_TARGET_WORD_BITSIZE %d\n", bitsize);
printf ("#define WITH_TARGET_WORD_MSB %d\n", msb);
printf ("#define WITH_HOST_WORD_BITSIZE %zu\n", sizeof (int) * 8);
printf ("#define WITH_TARGET_BYTE_ORDER %s\n", big_endian ? "BFD_ENDIAN_BIG" : "BFD_ENDIAN_LITTLE");
printf ("\n");
printf ("#define SIM_BITS_INLINE (ALL_H_INLINE)\n");
printf ("\n");
printf ("#define ASSERT(X) do { if (!(X)) abort(); } while (0)\n");
printf ("\n");
printf ("#define PACKAGE \"sim\"\n");
printf ("#include <stdlib.h>\n");
printf ("#include <string.h>\n");
printf ("#include \"sim-basics.h\"\n");
gen_struct ();
printf ("#define DO_BIT_TESTS\n");
gen_bit ( 4, msb, "BIT4", 4);
gen_bit ( 5, msb, "BIT5", 5);
gen_bit ( 8, msb, "BIT8", 8);
gen_bit (10, msb, "BIT10", 10);
gen_bit (16, msb, "BIT16", 16);
gen_bit (32, msb, "BIT32", 32);
gen_bit (64, msb, "BIT64", 64);
gen_bit (bitsize, msb, "BIT", 64);
gen_bit ( 8, 8 - 1, "LSBIT8", 8);
gen_bit (16, 16 - 1, "LSBIT16", 16);
gen_bit (32, 32 - 1, "LSBIT32", 32);
gen_bit (64, 64 - 1, "LSBIT64", 64);
gen_bit (bitsize, bitsize - 1, "LSBIT", 64);
gen_bit ( 8, 0, "MSBIT8", 8);
gen_bit (16, 0, "MSBIT16", 16);
gen_bit (32, 0, "MSBIT32", 32);
gen_bit (64, 0, "MSBIT64", 64);
gen_bit (bitsize, 0, "MSBIT", 64);
printf ("test_spec *(bit_tests[]) = {\n");
printf (" &BIT4_test,\n");
printf (" &BIT5_test,\n");
printf (" &BIT8_test,\n");
printf (" &BIT10_test,\n");
printf (" &BIT16_test,\n");
printf (" &BIT32_test,\n");
printf (" &BIT64_test,\n");
printf (" &BIT_test,\n");
printf (" &LSBIT8_test,\n");
printf (" &LSBIT16_test,\n");
printf (" &LSBIT32_test,\n");
printf (" &LSBIT64_test,\n");
printf (" &LSBIT_test,\n");
printf (" &MSBIT8_test,\n");
printf (" &MSBIT16_test,\n");
printf (" &MSBIT32_test,\n");
printf (" &MSBIT64_test,\n");
printf (" &MSBIT_test,\n");
printf (" 0,\n");
printf ("};\n\n");
gen_enum ("BIT", 64);
gen_enum ("LSBIT", 64);
gen_enum ("MSBIT", 64);
gen_enum ("BIT32", 32);
gen_enum ("LSBIT32", 32);
gen_enum ("MSBIT32", 32);
printf ("#define DO_MASK_TESTS\n");
gen_mask ( 8, ms, "MASK8", 8);
gen_mask (16, ms, "MASK16", 16);
gen_mask (32, ms, "MASK32", 32);
gen_mask (64, ms, "MASK64", 64);
gen_mask (bitsize, ms, "MASK", 64);
printf ("test_spec *(mask_tests[]) = {\n");
printf (" &MASK8_test,\n");
printf (" &MASK16_test,\n");
printf (" &MASK32_test,\n");
printf (" &MASK64_test,\n");
printf (" &MASK_test,\n");
printf (" 0,\n");
printf ("};\n\n");
return 0;
}
|