Commit | Line | Data |
---|---|---|
75faf9ae AC |
1 | /* Miscellaneous simulator utilities. |
2 | Copyright (C) 1997 Free Software Foundation, Inc. | |
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 | |
9 | the Free Software Foundation; either version 2, or (at your option) | |
10 | any later version. | |
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 | ||
17 | You should have received a copy of the GNU General Public License along | |
18 | with this program; if not, write to the Free Software Foundation, Inc., | |
19 | 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ | |
20 | ||
21 | ||
22 | #include <stdio.h> | |
23 | ||
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"); | |
33 | printf (" long long val;\n"); | |
34 | printf (" long long check;\n"); | |
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 BIT%s macro */\n", macro); | |
56 | printf ("test_tuples bit%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 | long long bit = 1; | |
61 | if (msb == 0) | |
62 | bit <<= nr_bits - i - 1; | |
63 | else | |
64 | bit <<= i; | |
65 | if (bitsize == 32) | |
66 | bit = (long) bit; | |
67 | /* write it out */ | |
68 | printf (" { __LINE__, "); | |
69 | printf ("%d, %d, ", -1, i); | |
70 | printf ("BIT%s (%2d), ", macro, i); | |
71 | printf ("0x%08lx%08lxLL, ", (long) (bit >> 32), (long) bit); | |
72 | printf ("},\n"); | |
73 | } | |
74 | printf ("};\n"); | |
75 | printf ("\n"); | |
76 | printf ("test_spec bit%s_test = { __FILE__, \"BIT%s\", 1, %d, bit%s_tuples, };\n", | |
77 | macro, macro, nr_bits, macro); | |
78 | printf ("\n"); | |
79 | } | |
80 | ||
81 | ||
82 | void | |
83 | gen_mask (int bitsize, | |
84 | const char *msb, | |
85 | const char *macro, | |
86 | int nr_bits) | |
87 | { | |
88 | int l; | |
89 | int h; | |
90 | printf ("\n/* Test the %sMASK%s macro */\n", msb, macro); | |
91 | printf ("test_tuples mask%s_tuples[%d][%d] = {\n", macro, nr_bits, nr_bits); | |
92 | for (l = 0; l < nr_bits; l++) | |
93 | { | |
94 | printf (" {\n"); | |
95 | for (h = 0; h < nr_bits; h++) | |
96 | { | |
97 | printf (" { __LINE__, "); | |
98 | if ((strcmp (msb, "MS") == 0 && l <= h) | |
99 | || (strcmp (msb, "MS") != 0 && l >= h) | |
100 | || (strcmp (macro, "") == 0)) | |
101 | { | |
102 | /* compute the mask */ | |
103 | long long mask = 0; | |
104 | int b; | |
105 | for (b = 0; b < nr_bits; b++) | |
106 | { | |
107 | long long bit = 1; | |
108 | if (strcmp (msb, "MS") == 0) | |
109 | { | |
110 | if ((l <= b && b <= h) | |
111 | || (h < l && (b <= h || b >= l))) | |
112 | bit <<= (nr_bits - b - 1); | |
113 | else | |
114 | bit = 0; | |
115 | } | |
116 | else | |
117 | { | |
118 | if ((l >= b && b >= h) | |
119 | || (h > l && (b >= h || b <= l))) | |
120 | bit <<= b; | |
121 | else | |
122 | bit = 0; | |
123 | } | |
124 | mask |= bit; | |
125 | } | |
126 | if (bitsize == 32) | |
127 | mask = (unsigned long) mask; | |
128 | printf ("%d, %d, ", l, h); | |
129 | printf ("%sMASK%s (%2d, %2d), ", msb, macro, l, h); | |
130 | printf ("0x%08lx%08lxLL, ", (long) (mask >> 32), (long) mask); | |
131 | } | |
132 | else | |
133 | printf ("-1, -1, "); | |
134 | printf ("},\n"); | |
135 | } | |
136 | printf (" },\n"); | |
137 | } | |
138 | printf ("};\n"); | |
139 | printf ("\n"); | |
140 | printf ("test_spec mask%s_test = { __FILE__, \"%sMASK%s\", %d, %d, &mask%s_tuples[0][0], };\n", | |
141 | macro, msb, macro, nr_bits, nr_bits, macro); | |
142 | printf ("\n"); | |
143 | } | |
144 | ||
145 | ||
146 | void | |
147 | usage (int reason) | |
148 | { | |
149 | fprintf (stderr, "Usage:\n"); | |
150 | fprintf (stderr, " bits-gen <nr-bits> <msb>\n"); | |
151 | fprintf (stderr, "Generate a test case for the simulator bit manipulation code\n"); | |
152 | fprintf (stderr, " <nr-bits> = { 32 | 64 }\n"); | |
153 | fprintf (stderr, " <msb> = { 0 | { 31 | 63 } }\n"); | |
154 | ||
155 | switch (reason) | |
156 | { | |
157 | case 1: fprintf (stderr, "Wrong number of arguments\n"); | |
158 | break; | |
159 | case 2: | |
160 | fprintf (stderr, "Invalid <nr-bits> argument\n"); | |
161 | break; | |
162 | case 3: | |
163 | fprintf (stderr, "Invalid <msb> argument\n"); | |
164 | break; | |
165 | default: | |
166 | } | |
167 | ||
168 | exit (1); | |
169 | } | |
170 | ||
171 | ||
172 | ||
173 | int | |
174 | main (argc, argv) | |
175 | int argc; | |
176 | char **argv; | |
177 | { | |
178 | int bitsize; | |
179 | int msb; | |
180 | char *ms; | |
181 | ||
182 | /* parse the only argument */ | |
183 | if (argc != 3) | |
184 | usage (1); | |
185 | if (strcmp (argv [1], "32") == 0) | |
186 | bitsize = 32; | |
187 | else if (strcmp (argv [1], "64") == 0) | |
188 | bitsize = 64; | |
189 | else | |
190 | usage (2); | |
191 | if (strcmp (argv [2], "0") == 0) | |
192 | msb = 0; | |
193 | else if (strcmp (argv [2], "31") == 0 && bitsize == 32) | |
194 | msb = 31; | |
195 | else if (strcmp (argv [2], "63") == 0 && bitsize == 64) | |
196 | msb = 63; | |
197 | else | |
198 | usage (3); | |
199 | if (msb == 0) | |
200 | ms = "MS"; | |
201 | else | |
202 | ms = "LS"; | |
203 | ||
204 | printf ("#define WITH_TARGET_WORD_BITSIZE %d\n", bitsize); | |
205 | printf ("#define WITH_TARGET_WORD_MSB %d\n", msb); | |
206 | printf ("#define WITH_HOST_WORD_BITSIZE %d\n", sizeof (int) * 8); | |
207 | printf ("\n"); | |
208 | printf ("#define SIM_BITS_INLINE (INCLUDE_MODULE | INCLUDED_BY_MODULE)\n"); | |
209 | printf ("\n"); | |
210 | printf ("#define ASSERT(X) do { if (!(X)) abort(); } while (0)\n"); | |
211 | printf ("\n"); | |
212 | printf ("#include \"sim-basics.h\"\n"); | |
213 | printf ("#include \"sim-types.h\"\n"); | |
214 | printf ("#include \"sim-bits.h\"\n"); | |
215 | ||
216 | gen_struct (); | |
217 | ||
218 | ||
219 | ||
220 | printf ("#define DO_BIT_TESTS\n"); | |
221 | gen_bit ( 4, msb, "4", 4); | |
222 | gen_bit ( 5, msb, "5", 5); | |
223 | gen_bit ( 8, msb, "8", 8); | |
224 | gen_bit (10, msb, "10", 10); | |
225 | gen_bit (16, msb, "16", 16); | |
226 | gen_bit (32, msb, "32", 32); | |
227 | gen_bit (64, msb, "64", 64); | |
228 | gen_bit (bitsize, msb, "", 64); | |
229 | ||
230 | printf ("test_spec *(bit_tests[]) = {\n"); | |
231 | printf (" &bit4_test,\n"); | |
232 | printf (" &bit5_test,\n"); | |
233 | printf (" &bit8_test,\n"); | |
234 | printf (" &bit10_test,\n"); | |
235 | printf (" &bit16_test,\n"); | |
236 | printf (" &bit32_test,\n"); | |
237 | printf (" &bit64_test,\n"); | |
238 | printf (" &bit_test,\n"); | |
239 | printf (" 0,\n"); | |
240 | printf ("};\n\n"); | |
241 | ||
242 | ||
243 | ||
244 | printf ("#define DO_MASK_TESTS\n"); | |
245 | gen_mask (16, ms, "16", 16); | |
246 | gen_mask (32, ms, "32", 32); | |
247 | gen_mask (64, ms, "64", 64); | |
248 | gen_mask (bitsize, ms, "", 64); | |
249 | ||
250 | printf ("test_spec *(mask_tests[]) = {\n"); | |
251 | printf (" &mask16_test,\n"); | |
252 | printf (" &mask32_test,\n"); | |
253 | printf (" &mask64_test,\n"); | |
254 | printf (" &mask_test,\n"); | |
255 | printf (" 0,\n"); | |
256 | printf ("};\n\n"); | |
257 | ||
258 | return 0; | |
259 | } |