1 /* Miscellaneous simulator utilities.
2 Copyright (C) 1997-2018 Free Software Foundation, Inc.
3 Contributed by Cygnus Support.
5 This file is part of GDB, the GNU debugger.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
28 printf ("typedef struct _test_tuples {\n");
29 printf (" int line;\n");
30 printf (" int row;\n");
31 printf (" int col;\n");
32 printf (" long long val;\n");
33 printf (" long long check;\n");
34 printf ("} test_tuples;\n");
36 printf ("typedef struct _test_spec {\n");
37 printf (" const char *file;\n");
38 printf (" const char *macro;\n");
39 printf (" int nr_rows;\n");
40 printf (" int nr_cols;\n");
41 printf (" test_tuples *tuples;\n");
42 printf ("} test_spec;\n");
54 printf ("\n/* Test the %s macro */\n", macro
);
55 printf ("test_tuples %s_tuples[%d] = {\n", macro
, nr_bits
);
56 for (i
= 0; i
< nr_bits
; i
++)
58 /* compute what we think the value is */
59 unsigned long long bit
= 1;
61 bit
<<= nr_bits
- i
- 1;
65 bit
= (unsigned) bit
; /* truncate it! */
67 printf (" { __LINE__, ");
68 printf ("%d, %2d, ", -1, i
);
69 printf ("%s (%2d), ", macro
, i
);
70 printf ("UNSIGNED64 (0x%08lx%08lx), ",
71 (long) (bit
>> 32), (long) bit
);
76 printf ("test_spec %s_test = { __FILE__, \"%s\", 1, %d, %s_tuples, };\n",
77 macro
, macro
, nr_bits
, macro
);
83 gen_enum (const char *macro
,
88 printf ("\n/* Test the %s macro in an enum */\n", macro
);
89 printf ("enum enum_%s {\n", macro
);
90 for (i
= 0; i
< nr_bits
; i
++)
92 printf (" elem_%s_%d = %s (%d),\n", macro
, i
, macro
, i
);
100 gen_mask (int bitsize
,
107 printf ("\n/* Test the %s%s macro */\n", msb
, macro
);
108 printf ("test_tuples %s_tuples[%d][%d] = {\n", macro
, nr_bits
, nr_bits
);
109 for (l
= 0; l
< nr_bits
; l
++)
112 for (h
= 0; h
< nr_bits
; h
++)
114 printf (" { __LINE__, ");
115 if ((strcmp (msb
, "MS") == 0 && l
<= h
)
116 || (strcmp (msb
, "MS") != 0 && l
>= h
)
117 || (strcmp (macro
, "") == 0))
119 /* compute the mask */
120 unsigned long long mask
= 0;
122 for (b
= 0; b
< nr_bits
; b
++)
124 unsigned long long bit
= 1;
125 if (strcmp (msb
, "MS") == 0)
127 if ((l
<= b
&& b
<= h
)
128 || (h
< l
&& (b
<= h
|| b
>= l
)))
129 bit
<<= (nr_bits
- b
- 1);
135 if ((l
>= b
&& b
>= h
)
136 || (h
> l
&& (b
>= h
|| b
<= l
)))
144 mask
= (unsigned long) mask
;
145 printf ("%d, %d, ", l
, h
);
146 printf ("%s%s (%2d, %2d), ", msb
, macro
, l
, h
);
147 printf ("UNSIGNED64 (0x%08lx%08lx), ",
148 (long) (mask
>> 32), (long) mask
);
158 printf ("test_spec %s_test = { __FILE__, \"%s%s\", %d, %d, &%s_tuples[0][0], };\n",
159 macro
, msb
, macro
, nr_bits
, nr_bits
, macro
);
167 fprintf (stderr
, "Usage:\n");
168 fprintf (stderr
, " bits-gen <nr-bits> <msb> <byte-order>\n");
169 fprintf (stderr
, "Generate a test case for the simulator bit manipulation code\n");
170 fprintf (stderr
, " <nr-bits> = { 32 | 64 }\n");
171 fprintf (stderr
, " <msb> = { 0 | { 31 | 63 } }\n");
172 fprintf (stderr
, " <byte-order> = { big | little }\n");
176 case 1: fprintf (stderr
, "Wrong number of arguments\n");
179 fprintf (stderr
, "Invalid <nr-bits> argument\n");
182 fprintf (stderr
, "Invalid <msb> argument\n");
185 fprintf (stderr
, "Invalid <byte-order> argument\n");
196 main (int argc
, char *argv
[])
206 if (strcmp (argv
[1], "32") == 0)
208 else if (strcmp (argv
[1], "64") == 0)
213 if (strcmp (argv
[2], "0") == 0)
215 else if (strcmp (argv
[2], "31") == 0 && bitsize
== 32)
217 else if (strcmp (argv
[2], "63") == 0 && bitsize
== 64)
226 if (strcmp (argv
[3], "big") == 0)
228 else if (strcmp (argv
[3], "little") == 0)
233 printf ("#define WITH_TARGET_WORD_BITSIZE %d\n", bitsize
);
234 printf ("#define WITH_TARGET_WORD_MSB %d\n", msb
);
235 printf ("#define WITH_HOST_WORD_BITSIZE %d\n", sizeof (int) * 8);
236 printf ("#define WITH_TARGET_BYTE_ORDER %s\n", big_endian
? "BFD_ENDIAN_BIG" : "BFD_ENDIAN_LITTLE");
238 printf ("#define SIM_BITS_INLINE (ALL_H_INLINE)\n");
240 printf ("#define ASSERT(X) do { if (!(X)) abort(); } while (0)\n");
242 printf ("#include \"sim-basics.h\"\n");
248 printf ("#define DO_BIT_TESTS\n");
249 gen_bit ( 4, msb
, "BIT4", 4);
250 gen_bit ( 5, msb
, "BIT5", 5);
251 gen_bit ( 8, msb
, "BIT8", 8);
252 gen_bit (10, msb
, "BIT10", 10);
253 gen_bit (16, msb
, "BIT16", 16);
254 gen_bit (32, msb
, "BIT32", 32);
255 gen_bit (64, msb
, "BIT64", 64);
256 gen_bit (bitsize
, msb
, "BIT", 64);
258 gen_bit ( 8, 8 - 1, "LSBIT8", 8);
259 gen_bit (16, 16 - 1, "LSBIT16", 16);
260 gen_bit (32, 32 - 1, "LSBIT32", 32);
261 gen_bit (64, 64 - 1, "LSBIT64", 64);
262 gen_bit (bitsize
, bitsize
- 1, "LSBIT", 64);
264 gen_bit ( 8, 0, "MSBIT8", 8);
265 gen_bit (16, 0, "MSBIT16", 16);
266 gen_bit (32, 0, "MSBIT32", 32);
267 gen_bit (64, 0, "MSBIT64", 64);
268 gen_bit (bitsize
, 0, "MSBIT", 64);
270 printf ("test_spec *(bit_tests[]) = {\n");
271 printf (" &BIT4_test,\n");
272 printf (" &BIT5_test,\n");
273 printf (" &BIT8_test,\n");
274 printf (" &BIT10_test,\n");
275 printf (" &BIT16_test,\n");
276 printf (" &BIT32_test,\n");
277 printf (" &BIT64_test,\n");
278 printf (" &BIT_test,\n");
279 printf (" &LSBIT8_test,\n");
280 printf (" &LSBIT16_test,\n");
281 printf (" &LSBIT32_test,\n");
282 printf (" &LSBIT64_test,\n");
283 printf (" &LSBIT_test,\n");
284 printf (" &MSBIT8_test,\n");
285 printf (" &MSBIT16_test,\n");
286 printf (" &MSBIT32_test,\n");
287 printf (" &MSBIT64_test,\n");
288 printf (" &MSBIT_test,\n");
292 gen_enum ("BIT", 64);
293 gen_enum ("LSBIT", 64);
294 gen_enum ("MSBIT", 64);
295 gen_enum ("BIT32", 32);
296 gen_enum ("LSBIT32", 32);
297 gen_enum ("MSBIT32", 32);
299 printf ("#define DO_MASK_TESTS\n");
300 gen_mask ( 8, ms
, "MASK8", 8);
301 gen_mask (16, ms
, "MASK16", 16);
302 gen_mask (32, ms
, "MASK32", 32);
303 gen_mask (64, ms
, "MASK64", 64);
304 gen_mask (bitsize
, ms
, "MASK", 64);
306 printf ("test_spec *(mask_tests[]) = {\n");
307 printf (" &MASK8_test,\n");
308 printf (" &MASK16_test,\n");
309 printf (" &MASK32_test,\n");
310 printf (" &MASK64_test,\n");
311 printf (" &MASK_test,\n");