Commit | Line | Data |
---|---|---|
c906108c | 1 | /* Miscellaneous simulator utilities. |
88b9d363 | 2 | Copyright (C) 1997-2022 Free Software Foundation, Inc. |
c906108c SS |
3 | Contributed by Cygnus Support. |
4 | ||
5 | This file is part of GDB, the GNU debugger. | |
6 | ||
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 | |
4744ac1b JB |
9 | the Free Software Foundation; either version 3 of the License, or |
10 | (at your option) any later version. | |
c906108c SS |
11 | |
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. | |
16 | ||
4744ac1b JB |
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/>. */ | |
c906108c | 19 | |
c906108c | 20 | #include <stdio.h> |
254c3783 MF |
21 | #include <stdlib.h> |
22 | #include <string.h> | |
23 | #include <unistd.h> | |
c906108c SS |
24 | |
25 | void | |
26 | gen_struct (void) | |
27 | { | |
28 | printf ("\n"); | |
29 | printf ("typedef struct _test_tuples {\n"); | |
30 | printf (" int line;\n"); | |
31 | printf (" int row;\n"); | |
32 | printf (" int col;\n"); | |
254c3783 MF |
33 | printf (" unsigned64 val;\n"); |
34 | printf (" unsigned64 check;\n"); | |
c906108c SS |
35 | printf ("} test_tuples;\n"); |
36 | printf ("\n"); | |
37 | printf ("typedef struct _test_spec {\n"); | |
38 | printf (" const char *file;\n"); | |
39 | printf (" const char *macro;\n"); | |
40 | printf (" int nr_rows;\n"); | |
41 | printf (" int nr_cols;\n"); | |
42 | printf (" test_tuples *tuples;\n"); | |
43 | printf ("} test_spec;\n"); | |
44 | } | |
45 | ||
46 | ||
47 | void | |
48 | gen_bit (int bitsize, | |
49 | int msb, | |
50 | const char *macro, | |
51 | int nr_bits) | |
52 | { | |
53 | int i; | |
54 | ||
55 | printf ("\n/* Test the %s macro */\n", macro); | |
56 | printf ("test_tuples %s_tuples[%d] = {\n", macro, nr_bits); | |
57 | for (i = 0; i < nr_bits; i++) | |
58 | { | |
59 | /* compute what we think the value is */ | |
60 | unsigned long long bit = 1; | |
61 | if (msb == 0) | |
62 | bit <<= nr_bits - i - 1; | |
63 | else | |
64 | bit <<= i; | |
65 | if (bitsize == 32) | |
254c3783 | 66 | bit &= 0xffffffff; /* truncate it! */ |
c906108c SS |
67 | /* write it out */ |
68 | printf (" { __LINE__, "); | |
69 | printf ("%d, %2d, ", -1, i); | |
70 | printf ("%s (%2d), ", macro, i); | |
254c3783 | 71 | printf ("UNSIGNED64 (0x%016llx), ", bit); |
c906108c SS |
72 | printf ("},\n"); |
73 | } | |
74 | printf ("};\n"); | |
75 | printf ("\n"); | |
76 | printf ("test_spec %s_test = { __FILE__, \"%s\", 1, %d, %s_tuples, };\n", | |
77 | macro, macro, nr_bits, macro); | |
78 | printf ("\n"); | |
79 | } | |
80 | ||
81 | ||
82 | void | |
83 | gen_enum (const char *macro, | |
84 | int nr_bits) | |
85 | { | |
86 | int i; | |
87 | ||
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++) | |
91 | { | |
92 | printf (" elem_%s_%d = %s (%d),\n", macro, i, macro, i); | |
93 | } | |
94 | printf ("};\n"); | |
95 | printf ("\n"); | |
96 | } | |
97 | ||
98 | ||
99 | void | |
100 | gen_mask (int bitsize, | |
101 | const char *msb, | |
102 | const char *macro, | |
103 | int nr_bits) | |
104 | { | |
105 | int l; | |
106 | int h; | |
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++) | |
110 | { | |
111 | printf (" {\n"); | |
112 | for (h = 0; h < nr_bits; h++) | |
113 | { | |
114 | printf (" { __LINE__, "); | |
115 | if ((strcmp (msb, "MS") == 0 && l <= h) | |
116 | || (strcmp (msb, "MS") != 0 && l >= h) | |
117 | || (strcmp (macro, "") == 0)) | |
118 | { | |
119 | /* compute the mask */ | |
120 | unsigned long long mask = 0; | |
121 | int b; | |
122 | for (b = 0; b < nr_bits; b++) | |
123 | { | |
124 | unsigned long long bit = 1; | |
125 | if (strcmp (msb, "MS") == 0) | |
126 | { | |
127 | if ((l <= b && b <= h) | |
128 | || (h < l && (b <= h || b >= l))) | |
129 | bit <<= (nr_bits - b - 1); | |
130 | else | |
131 | bit = 0; | |
132 | } | |
133 | else | |
134 | { | |
135 | if ((l >= b && b >= h) | |
136 | || (h > l && (b >= h || b <= l))) | |
137 | bit <<= b; | |
138 | else | |
139 | bit = 0; | |
140 | } | |
141 | mask |= bit; | |
142 | } | |
143 | if (bitsize == 32) | |
254c3783 | 144 | mask &= 0xffffffff; |
c906108c SS |
145 | printf ("%d, %d, ", l, h); |
146 | printf ("%s%s (%2d, %2d), ", msb, macro, l, h); | |
254c3783 | 147 | printf ("UNSIGNED64 (0x%llx), ", mask); |
c906108c SS |
148 | } |
149 | else | |
150 | printf ("-1, -1, "); | |
151 | printf ("},\n"); | |
152 | } | |
153 | printf (" },\n"); | |
154 | } | |
155 | printf ("};\n"); | |
156 | printf ("\n"); | |
157 | printf ("test_spec %s_test = { __FILE__, \"%s%s\", %d, %d, &%s_tuples[0][0], };\n", | |
158 | macro, msb, macro, nr_bits, nr_bits, macro); | |
159 | printf ("\n"); | |
160 | } | |
161 | ||
162 | ||
163 | void | |
164 | usage (int reason) | |
165 | { | |
166 | fprintf (stderr, "Usage:\n"); | |
167 | fprintf (stderr, " bits-gen <nr-bits> <msb> <byte-order>\n"); | |
168 | fprintf (stderr, "Generate a test case for the simulator bit manipulation code\n"); | |
169 | fprintf (stderr, " <nr-bits> = { 32 | 64 }\n"); | |
170 | fprintf (stderr, " <msb> = { 0 | { 31 | 63 } }\n"); | |
171 | fprintf (stderr, " <byte-order> = { big | little }\n"); | |
172 | ||
173 | switch (reason) | |
174 | { | |
175 | case 1: fprintf (stderr, "Wrong number of arguments\n"); | |
176 | break; | |
177 | case 2: | |
178 | fprintf (stderr, "Invalid <nr-bits> argument\n"); | |
179 | break; | |
180 | case 3: | |
181 | fprintf (stderr, "Invalid <msb> argument\n"); | |
182 | break; | |
183 | case 4: | |
184 | fprintf (stderr, "Invalid <byte-order> argument\n"); | |
185 | break; | |
c906108c SS |
186 | } |
187 | ||
188 | exit (1); | |
189 | } | |
190 | ||
191 | ||
192 | ||
193 | int | |
1a8a700e | 194 | main (int argc, char *argv[]) |
c906108c SS |
195 | { |
196 | int bitsize; | |
197 | int msb; | |
198 | char *ms; | |
199 | int big_endian; | |
200 | ||
201 | if (argc != 4) | |
202 | usage (1); | |
203 | ||
204 | if (strcmp (argv [1], "32") == 0) | |
205 | bitsize = 32; | |
206 | else if (strcmp (argv [1], "64") == 0) | |
207 | bitsize = 64; | |
208 | else | |
209 | usage (2); | |
210 | ||
211 | if (strcmp (argv [2], "0") == 0) | |
212 | msb = 0; | |
213 | else if (strcmp (argv [2], "31") == 0 && bitsize == 32) | |
214 | msb = 31; | |
215 | else if (strcmp (argv [2], "63") == 0 && bitsize == 64) | |
216 | msb = 63; | |
217 | else | |
218 | usage (3); | |
219 | if (msb == 0) | |
220 | ms = "MS"; | |
221 | else | |
222 | ms = "LS"; | |
1a8a700e | 223 | |
c906108c SS |
224 | if (strcmp (argv [3], "big") == 0) |
225 | big_endian = 1; | |
226 | else if (strcmp (argv [3], "little") == 0) | |
227 | big_endian = 0; | |
228 | else | |
229 | usage (4); | |
1a8a700e | 230 | |
c906108c SS |
231 | printf ("#define WITH_TARGET_WORD_BITSIZE %d\n", bitsize); |
232 | printf ("#define WITH_TARGET_WORD_MSB %d\n", msb); | |
254c3783 | 233 | printf ("#define WITH_HOST_WORD_BITSIZE %zu\n", sizeof (int) * 8); |
1ac72f06 | 234 | printf ("#define WITH_TARGET_BYTE_ORDER %s\n", big_endian ? "BFD_ENDIAN_BIG" : "BFD_ENDIAN_LITTLE"); |
c906108c SS |
235 | printf ("\n"); |
236 | printf ("#define SIM_BITS_INLINE (ALL_H_INLINE)\n"); | |
237 | printf ("\n"); | |
238 | printf ("#define ASSERT(X) do { if (!(X)) abort(); } while (0)\n"); | |
239 | printf ("\n"); | |
254c3783 MF |
240 | printf ("#define PACKAGE \"sim\"\n"); |
241 | printf ("#include <stdlib.h>\n"); | |
242 | printf ("#include <string.h>\n"); | |
c906108c SS |
243 | printf ("#include \"sim-basics.h\"\n"); |
244 | ||
245 | gen_struct (); | |
246 | ||
247 | ||
248 | ||
249 | printf ("#define DO_BIT_TESTS\n"); | |
250 | gen_bit ( 4, msb, "BIT4", 4); | |
251 | gen_bit ( 5, msb, "BIT5", 5); | |
252 | gen_bit ( 8, msb, "BIT8", 8); | |
253 | gen_bit (10, msb, "BIT10", 10); | |
254 | gen_bit (16, msb, "BIT16", 16); | |
255 | gen_bit (32, msb, "BIT32", 32); | |
256 | gen_bit (64, msb, "BIT64", 64); | |
257 | gen_bit (bitsize, msb, "BIT", 64); | |
258 | ||
259 | gen_bit ( 8, 8 - 1, "LSBIT8", 8); | |
260 | gen_bit (16, 16 - 1, "LSBIT16", 16); | |
261 | gen_bit (32, 32 - 1, "LSBIT32", 32); | |
262 | gen_bit (64, 64 - 1, "LSBIT64", 64); | |
263 | gen_bit (bitsize, bitsize - 1, "LSBIT", 64); | |
264 | ||
265 | gen_bit ( 8, 0, "MSBIT8", 8); | |
266 | gen_bit (16, 0, "MSBIT16", 16); | |
267 | gen_bit (32, 0, "MSBIT32", 32); | |
268 | gen_bit (64, 0, "MSBIT64", 64); | |
269 | gen_bit (bitsize, 0, "MSBIT", 64); | |
270 | ||
271 | printf ("test_spec *(bit_tests[]) = {\n"); | |
272 | printf (" &BIT4_test,\n"); | |
273 | printf (" &BIT5_test,\n"); | |
274 | printf (" &BIT8_test,\n"); | |
275 | printf (" &BIT10_test,\n"); | |
276 | printf (" &BIT16_test,\n"); | |
277 | printf (" &BIT32_test,\n"); | |
278 | printf (" &BIT64_test,\n"); | |
279 | printf (" &BIT_test,\n"); | |
280 | printf (" &LSBIT8_test,\n"); | |
281 | printf (" &LSBIT16_test,\n"); | |
282 | printf (" &LSBIT32_test,\n"); | |
283 | printf (" &LSBIT64_test,\n"); | |
284 | printf (" &LSBIT_test,\n"); | |
285 | printf (" &MSBIT8_test,\n"); | |
286 | printf (" &MSBIT16_test,\n"); | |
287 | printf (" &MSBIT32_test,\n"); | |
288 | printf (" &MSBIT64_test,\n"); | |
289 | printf (" &MSBIT_test,\n"); | |
290 | printf (" 0,\n"); | |
291 | printf ("};\n\n"); | |
292 | ||
293 | gen_enum ("BIT", 64); | |
294 | gen_enum ("LSBIT", 64); | |
295 | gen_enum ("MSBIT", 64); | |
296 | gen_enum ("BIT32", 32); | |
297 | gen_enum ("LSBIT32", 32); | |
298 | gen_enum ("MSBIT32", 32); | |
299 | ||
300 | printf ("#define DO_MASK_TESTS\n"); | |
301 | gen_mask ( 8, ms, "MASK8", 8); | |
302 | gen_mask (16, ms, "MASK16", 16); | |
303 | gen_mask (32, ms, "MASK32", 32); | |
304 | gen_mask (64, ms, "MASK64", 64); | |
305 | gen_mask (bitsize, ms, "MASK", 64); | |
306 | ||
307 | printf ("test_spec *(mask_tests[]) = {\n"); | |
308 | printf (" &MASK8_test,\n"); | |
309 | printf (" &MASK16_test,\n"); | |
310 | printf (" &MASK32_test,\n"); | |
311 | printf (" &MASK64_test,\n"); | |
312 | printf (" &MASK_test,\n"); | |
313 | printf (" 0,\n"); | |
314 | printf ("};\n\n"); | |
315 | ||
316 | return 0; | |
317 | } |