1 /* Miscellaneous simulator utilities. 2 Copyright (C) 1997-2020 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 3 of the License, or 10 (at your option) 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 18 along with this program. If not, see <http://www.gnu.org/licenses/>. */ 19 20 21 #include <stdio.h> 22 23 24 void 25 gen_struct (void) 26 { 27 printf ("\n"); 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"); 35 printf ("\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"); 43 } 44 45 46 void 47 gen_bit (int bitsize, 48 int msb, 49 const char *macro, 50 int nr_bits) 51 { 52 int i; 53 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++) 57 { 58 /* compute what we think the value is */ 59 unsigned long long bit = 1; 60 if (msb == 0) 61 bit <<= nr_bits - i - 1; 62 else 63 bit <<= i; 64 if (bitsize == 32) 65 bit = (unsigned) bit; /* truncate it! */ 66 /* write it out */ 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); 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) 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); 149 } 150 else 151 printf ("-1, -1, "); 152 printf ("},\n"); 153 } 154 printf (" },\n"); 155 } 156 printf ("};\n"); 157 printf ("\n"); 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); 160 printf ("\n"); 161 } 162 163 164 void 165 usage (int reason) 166 { 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"); 173 174 switch (reason) 175 { 176 case 1: fprintf (stderr, "Wrong number of arguments\n"); 177 break; 178 case 2: 179 fprintf (stderr, "Invalid <nr-bits> argument\n"); 180 break; 181 case 3: 182 fprintf (stderr, "Invalid <msb> argument\n"); 183 break; 184 case 4: 185 fprintf (stderr, "Invalid <byte-order> argument\n"); 186 break; 187 default: 188 } 189 190 exit (1); 191 } 192 193 194 195 int 196 main (int argc, char *argv[]) 197 { 198 int bitsize; 199 int msb; 200 char *ms; 201 int big_endian; 202 203 if (argc != 4) 204 usage (1); 205 206 if (strcmp (argv [1], "32") == 0) 207 bitsize = 32; 208 else if (strcmp (argv [1], "64") == 0) 209 bitsize = 64; 210 else 211 usage (2); 212 213 if (strcmp (argv [2], "0") == 0) 214 msb = 0; 215 else if (strcmp (argv [2], "31") == 0 && bitsize == 32) 216 msb = 31; 217 else if (strcmp (argv [2], "63") == 0 && bitsize == 64) 218 msb = 63; 219 else 220 usage (3); 221 if (msb == 0) 222 ms = "MS"; 223 else 224 ms = "LS"; 225 226 if (strcmp (argv [3], "big") == 0) 227 big_endian = 1; 228 else if (strcmp (argv [3], "little") == 0) 229 big_endian = 0; 230 else 231 usage (4); 232 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"); 237 printf ("\n"); 238 printf ("#define SIM_BITS_INLINE (ALL_H_INLINE)\n"); 239 printf ("\n"); 240 printf ("#define ASSERT(X) do { if (!(X)) abort(); } while (0)\n"); 241 printf ("\n"); 242 printf ("#include \"sim-basics.h\"\n"); 243 244 gen_struct (); 245 246 247 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); 257 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); 263 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); 269 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"); 289 printf (" 0,\n"); 290 printf ("};\n\n"); 291 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); 298 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); 305 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"); 312 printf (" 0,\n"); 313 printf ("};\n\n"); 314 315 return 0; 316 } 317