1 /* Miscellaneous simulator utilities.
2    Copyright (C) 1997, 2007, 2008, 2009, 2010, 2011
3    Free Software Foundation, Inc.
4    Contributed by Cygnus Support.
5 
6 This file is part of GDB, the GNU debugger.
7 
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
12 
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 GNU General Public License for more details.
17 
18 You should have received a copy of the GNU General Public License
19 along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
20 
21 
22 #include <stdio.h>
23 
24 
25 void
gen_struct(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
gen_bit(int bitsize,int msb,const char * macro,int nr_bits)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)
66 	bit = (unsigned) bit; /* truncate it! */
67       /* write it out */
68       printf ("  { __LINE__, ");
69       printf ("%d, %2d, ", -1, i);
70       printf ("%s (%2d), ", macro, i);
71       printf ("UNSIGNED64 (0x%08lx%08lx), ",
72 	      (long) (bit >> 32), (long) bit);
73       printf ("},\n");
74     }
75   printf ("};\n");
76   printf ("\n");
77   printf ("test_spec %s_test = { __FILE__, \"%s\", 1, %d, %s_tuples, };\n",
78 	  macro, macro, nr_bits, macro);
79   printf ("\n");
80 }
81 
82 
83 void
gen_enum(const char * macro,int nr_bits)84 gen_enum (const char *macro,
85 	  int nr_bits)
86 {
87   int i;
88 
89   printf ("\n/* Test the %s macro in an enum */\n", macro);
90   printf ("enum enum_%s {\n", macro);
91   for (i = 0; i < nr_bits; i++)
92     {
93       printf ("  elem_%s_%d = %s (%d),\n", macro, i, macro, i);
94     }
95   printf ("};\n");
96   printf ("\n");
97 }
98 
99 
100 void
gen_mask(int bitsize,const char * msb,const char * macro,int nr_bits)101 gen_mask (int bitsize,
102 	  const char *msb,
103 	  const char *macro,
104 	  int nr_bits)
105 {
106   int l;
107   int h;
108   printf ("\n/* Test the %s%s macro */\n", msb, macro);
109   printf ("test_tuples %s_tuples[%d][%d] = {\n", macro, nr_bits, nr_bits);
110   for (l = 0; l < nr_bits; l++)
111     {
112       printf ("  {\n");
113       for (h = 0; h < nr_bits; h++)
114 	{
115 	  printf ("    { __LINE__, ");
116 	  if ((strcmp (msb, "MS") == 0 && l <= h)
117 	      || (strcmp (msb, "MS") != 0 && l >= h)
118 	      || (strcmp (macro, "") == 0))
119 	    {
120 	      /* compute the mask */
121 	      unsigned long long mask = 0;
122 	      int b;
123 	      for (b = 0; b < nr_bits; b++)
124 		{
125 		  unsigned long long bit = 1;
126 		  if (strcmp (msb, "MS") == 0)
127 		    {
128 		      if ((l <= b && b <= h)
129 			  || (h < l && (b <= h || b >= l)))
130 			bit <<= (nr_bits - b - 1);
131 		      else
132 			bit = 0;
133 		    }
134 		  else
135 		    {
136 		      if ((l >= b && b >= h)
137 			  || (h > l && (b >= h || b <= l)))
138 			bit <<= b;
139 		      else
140 			bit = 0;
141 		    }
142 		  mask |= bit;
143 		}
144 	      if (bitsize == 32)
145 		mask = (unsigned long) mask;
146 	      printf ("%d, %d, ", l, h);
147 	      printf ("%s%s (%2d, %2d), ", msb, macro, l, h);
148 	      printf ("UNSIGNED64 (0x%08lx%08lx), ",
149 		      (long) (mask >> 32), (long) mask);
150 	    }
151 	  else
152 	    printf ("-1, -1, ");
153 	  printf ("},\n");
154 	}
155       printf ("  },\n");
156     }
157   printf ("};\n");
158   printf ("\n");
159   printf ("test_spec %s_test = { __FILE__, \"%s%s\", %d, %d, &%s_tuples[0][0], };\n",
160 	  macro, msb, macro, nr_bits, nr_bits, macro);
161   printf ("\n");
162 }
163 
164 
165 void
usage(int reason)166 usage (int reason)
167 {
168   fprintf (stderr, "Usage:\n");
169   fprintf (stderr, "  bits-gen <nr-bits> <msb> <byte-order>\n");
170   fprintf (stderr, "Generate a test case for the simulator bit manipulation code\n");
171   fprintf (stderr, "  <nr-bits> = { 32 | 64 }\n");
172   fprintf (stderr, "  <msb> = { 0 | { 31 | 63 } }\n");
173   fprintf (stderr, "  <byte-order> = { big | little }\n");
174 
175   switch (reason)
176     {
177     case 1: fprintf (stderr, "Wrong number of arguments\n");
178       break;
179     case 2:
180       fprintf (stderr, "Invalid <nr-bits> argument\n");
181       break;
182     case 3:
183       fprintf (stderr, "Invalid <msb> argument\n");
184       break;
185     case 4:
186       fprintf (stderr, "Invalid <byte-order> argument\n");
187       break;
188     default:
189     }
190 
191   exit (1);
192 }
193 
194 
195 
196 int
main(argc,argv)197 main (argc, argv)
198      int argc;
199      char **argv;
200 {
201   int bitsize;
202   int msb;
203   char *ms;
204   int big_endian;
205 
206   if (argc != 4)
207     usage (1);
208 
209   if (strcmp (argv [1], "32") == 0)
210     bitsize = 32;
211   else if (strcmp (argv [1], "64") == 0)
212     bitsize = 64;
213   else
214     usage (2);
215 
216   if (strcmp (argv [2], "0") == 0)
217     msb = 0;
218   else if (strcmp (argv [2], "31") == 0 && bitsize == 32)
219     msb = 31;
220   else if (strcmp (argv [2], "63") == 0 && bitsize == 64)
221     msb = 63;
222   else
223     usage (3);
224   if (msb == 0)
225     ms = "MS";
226   else
227     ms = "LS";
228 
229   if (strcmp (argv [3], "big") == 0)
230     big_endian = 1;
231   else if (strcmp (argv [3], "little") == 0)
232     big_endian = 0;
233   else
234     usage (4);
235 
236   printf ("#define WITH_TARGET_WORD_BITSIZE %d\n", bitsize);
237   printf ("#define WITH_TARGET_WORD_MSB %d\n", msb);
238   printf ("#define WITH_HOST_WORD_BITSIZE %d\n", sizeof (int) * 8);
239   printf ("#define WITH_TARGET_BYTE_ORDER %s\n", big_endian ? "BIG_ENDIAN" : "LITTLE_ENDIAN");
240   printf ("\n");
241   printf ("#define SIM_BITS_INLINE (ALL_H_INLINE)\n");
242   printf ("\n");
243   printf ("#define ASSERT(X) do { if (!(X)) abort(); } while (0)\n");
244   printf ("\n");
245   printf ("#include \"sim-basics.h\"\n");
246 
247   gen_struct ();
248 
249 
250 
251   printf ("#define DO_BIT_TESTS\n");
252   gen_bit ( 4, msb, "BIT4",  4);
253   gen_bit ( 5, msb, "BIT5",  5);
254   gen_bit ( 8, msb, "BIT8",  8);
255   gen_bit (10, msb, "BIT10", 10);
256   gen_bit (16, msb, "BIT16", 16);
257   gen_bit (32, msb, "BIT32", 32);
258   gen_bit (64, msb, "BIT64", 64);
259   gen_bit (bitsize, msb, "BIT", 64);
260 
261   gen_bit ( 8,  8 - 1, "LSBIT8",   8);
262   gen_bit (16, 16 - 1, "LSBIT16", 16);
263   gen_bit (32, 32 - 1, "LSBIT32", 32);
264   gen_bit (64, 64 - 1, "LSBIT64", 64);
265   gen_bit (bitsize, bitsize - 1, "LSBIT",   64);
266 
267   gen_bit ( 8,  0, "MSBIT8",   8);
268   gen_bit (16,  0, "MSBIT16", 16);
269   gen_bit (32,  0, "MSBIT32", 32);
270   gen_bit (64,  0, "MSBIT64", 64);
271   gen_bit (bitsize,  0, "MSBIT",   64);
272 
273   printf ("test_spec *(bit_tests[]) = {\n");
274   printf ("  &BIT4_test,\n");
275   printf ("  &BIT5_test,\n");
276   printf ("  &BIT8_test,\n");
277   printf ("  &BIT10_test,\n");
278   printf ("  &BIT16_test,\n");
279   printf ("  &BIT32_test,\n");
280   printf ("  &BIT64_test,\n");
281   printf ("  &BIT_test,\n");
282   printf ("  &LSBIT8_test,\n");
283   printf ("  &LSBIT16_test,\n");
284   printf ("  &LSBIT32_test,\n");
285   printf ("  &LSBIT64_test,\n");
286   printf ("  &LSBIT_test,\n");
287   printf ("  &MSBIT8_test,\n");
288   printf ("  &MSBIT16_test,\n");
289   printf ("  &MSBIT32_test,\n");
290   printf ("  &MSBIT64_test,\n");
291   printf ("  &MSBIT_test,\n");
292   printf ("  0,\n");
293   printf ("};\n\n");
294 
295   gen_enum ("BIT",   64);
296   gen_enum ("LSBIT", 64);
297   gen_enum ("MSBIT", 64);
298   gen_enum ("BIT32",   32);
299   gen_enum ("LSBIT32", 32);
300   gen_enum ("MSBIT32", 32);
301 
302   printf ("#define DO_MASK_TESTS\n");
303   gen_mask ( 8, ms, "MASK8",  8);
304   gen_mask (16, ms, "MASK16", 16);
305   gen_mask (32, ms, "MASK32", 32);
306   gen_mask (64, ms, "MASK64", 64);
307   gen_mask (bitsize, ms, "MASK", 64);
308 
309   printf ("test_spec *(mask_tests[]) = {\n");
310   printf ("  &MASK8_test,\n");
311   printf ("  &MASK16_test,\n");
312   printf ("  &MASK32_test,\n");
313   printf ("  &MASK64_test,\n");
314   printf ("  &MASK_test,\n");
315   printf ("  0,\n");
316   printf ("};\n\n");
317 
318   return 0;
319 }
320