sim: convert to bfd_endian
[deliverable/binutils-gdb.git] / sim / testsuite / common / bits-gen.c
index 5feb5a0a6d6ed060565ebf526c7b19d6770c0732..6e7dd75b93c8f910452a5f25b8b4111edc9192ed 100644 (file)
@@ -1,22 +1,21 @@
 /* Miscellaneous simulator utilities.
-   Copyright (C) 1997 Free Software Foundation, Inc.
+   Copyright (C) 1997-2016 Free Software Foundation, Inc.
    Contributed by Cygnus Support.
 
 This file is part of GDB, the GNU debugger.
 
 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
-the Free Software Foundation; either version 2, or (at your option)
-any later version.
+the Free Software Foundation; either version 3 of the License, or
+(at your option) any later version.
 
 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
-You should have received a copy of the GNU General Public License along
-with this program; if not, write to the Free Software Foundation, Inc.,
-59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
+You should have received a copy of the GNU General Public License
+along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
 
 
 #include <stdio.h>
@@ -52,33 +51,51 @@ gen_bit (int bitsize,
 {
   int i;
 
-  printf ("\n/* Test the BIT%s macro */\n", macro);
-  printf ("test_tuples bit%s_tuples[%d] = {\n", macro, nr_bits);
+  printf ("\n/* Test the %s macro */\n", macro);
+  printf ("test_tuples %s_tuples[%d] = {\n", macro, nr_bits);
   for (i = 0; i < nr_bits; i++)
     {
       /* compute what we think the value is */
-      long long bit = 1;
+      unsigned long long bit = 1;
       if (msb == 0)
        bit <<= nr_bits - i - 1;
       else
        bit <<= i;
       if (bitsize == 32)
-       bit = (long) bit;
+       bit = (unsigned) bit; /* truncate it! */
       /* write it out */
       printf ("  { __LINE__, ");
-      printf ("%d, %d, ", -1, i);
-      printf ("BIT%s (%2d), ", macro, i);
-      printf ("0x%08lx%08lxLL, ", (long) (bit >> 32), (long) bit);
+      printf ("%d, %2d, ", -1, i);
+      printf ("%s (%2d), ", macro, i);
+      printf ("UNSIGNED64 (0x%08lx%08lx), ",
+             (long) (bit >> 32), (long) bit);
       printf ("},\n");
     }
   printf ("};\n");
   printf ("\n");
-  printf ("test_spec bit%s_test = { __FILE__, \"BIT%s\", 1, %d, bit%s_tuples, };\n",
+  printf ("test_spec %s_test = { __FILE__, \"%s\", 1, %d, %s_tuples, };\n",
          macro, macro, nr_bits, macro);
   printf ("\n");
 }
 
 
+void
+gen_enum (const char *macro,
+         int nr_bits)
+{
+  int i;
+
+  printf ("\n/* Test the %s macro in an enum */\n", macro);
+  printf ("enum enum_%s {\n", macro);
+  for (i = 0; i < nr_bits; i++)
+    {
+      printf ("  elem_%s_%d = %s (%d),\n", macro, i, macro, i);
+    }
+  printf ("};\n");
+  printf ("\n");
+}
+
+
 void
 gen_mask (int bitsize,
          const char *msb,
@@ -87,8 +104,8 @@ gen_mask (int bitsize,
 {
   int l;
   int h;
-  printf ("\n/* Test the %sMASK%s macro */\n", msb, macro);
-  printf ("test_tuples mask%s_tuples[%d][%d] = {\n", macro, nr_bits, nr_bits);
+  printf ("\n/* Test the %s%s macro */\n", msb, macro);
+  printf ("test_tuples %s_tuples[%d][%d] = {\n", macro, nr_bits, nr_bits);
   for (l = 0; l < nr_bits; l++)
     {
       printf ("  {\n");
@@ -100,11 +117,11 @@ gen_mask (int bitsize,
              || (strcmp (macro, "") == 0))
            {
              /* compute the mask */
-             long long mask = 0;
+             unsigned long long mask = 0;
              int b;
              for (b = 0; b < nr_bits; b++)
                {
-                 long long bit = 1;
+                 unsigned long long bit = 1;
                  if (strcmp (msb, "MS") == 0)
                    {
                      if ((l <= b && b <= h)
@@ -126,8 +143,9 @@ gen_mask (int bitsize,
              if (bitsize == 32)
                mask = (unsigned long) mask;
              printf ("%d, %d, ", l, h);
-             printf ("%sMASK%s (%2d, %2d), ", msb, macro, l, h);
-             printf ("0x%08lx%08lxLL, ", (long) (mask >> 32), (long) mask);
+             printf ("%s%s (%2d, %2d), ", msb, macro, l, h);
+             printf ("UNSIGNED64 (0x%08lx%08lx), ",
+                     (long) (mask >> 32), (long) mask);
            }
          else
            printf ("-1, -1, ");
@@ -137,7 +155,7 @@ gen_mask (int bitsize,
     }
   printf ("};\n");
   printf ("\n");
-  printf ("test_spec mask%s_test = { __FILE__, \"%sMASK%s\", %d, %d, &mask%s_tuples[0][0], };\n",
+  printf ("test_spec %s_test = { __FILE__, \"%s%s\", %d, %d, &%s_tuples[0][0], };\n",
          macro, msb, macro, nr_bits, nr_bits, macro);
   printf ("\n");
 }
@@ -147,10 +165,11 @@ void
 usage (int reason)
 {
   fprintf (stderr, "Usage:\n");
-  fprintf (stderr, "  bits-gen <nr-bits> <msb>\n");
+  fprintf (stderr, "  bits-gen <nr-bits> <msb> <byte-order>\n");
   fprintf (stderr, "Generate a test case for the simulator bit manipulation code\n");
   fprintf (stderr, "  <nr-bits> = { 32 | 64 }\n");
   fprintf (stderr, "  <msb> = { 0 | { 31 | 63 } }\n");
+  fprintf (stderr, "  <byte-order> = { big | little }\n");
 
   switch (reason)
     {
@@ -162,6 +181,9 @@ usage (int reason)
     case 3:
       fprintf (stderr, "Invalid <msb> argument\n");
       break;
+    case 4:
+      fprintf (stderr, "Invalid <byte-order> argument\n");
+      break;
     default:
     }
 
@@ -171,23 +193,23 @@ usage (int reason)
 
 
 int
-main (argc, argv)
-     int argc;
-     char **argv;
+main (int argc, char *argv[])
 {
   int bitsize;
   int msb;
   char *ms;
+  int big_endian;
 
-  /* parse the only argument */
-  if (argc != 3)
+  if (argc != 4)
     usage (1);
+
   if (strcmp (argv [1], "32") == 0)
     bitsize = 32;
   else if (strcmp (argv [1], "64") == 0)
     bitsize = 64;
   else
     usage (2);
+
   if (strcmp (argv [2], "0") == 0)
     msb = 0;
   else if (strcmp (argv [2], "31") == 0 && bitsize == 32)
@@ -201,57 +223,92 @@ main (argc, argv)
   else
     ms = "LS";
 
+  if (strcmp (argv [3], "big") == 0)
+    big_endian = 1;
+  else if (strcmp (argv [3], "little") == 0)
+    big_endian = 0;
+  else
+    usage (4);
+
   printf ("#define WITH_TARGET_WORD_BITSIZE %d\n", bitsize);
   printf ("#define WITH_TARGET_WORD_MSB %d\n", msb);
   printf ("#define WITH_HOST_WORD_BITSIZE %d\n", sizeof (int) * 8);
+  printf ("#define WITH_TARGET_BYTE_ORDER %s\n", big_endian ? "BFD_ENDIAN_BIG" : "BFD_ENDIAN_LITTLE");
   printf ("\n");
-  printf ("#define SIM_BITS_INLINE (INCLUDE_MODULE | INCLUDED_BY_MODULE)\n");
+  printf ("#define SIM_BITS_INLINE (ALL_H_INLINE)\n");
   printf ("\n");
   printf ("#define ASSERT(X) do { if (!(X)) abort(); } while (0)\n");
   printf ("\n");
   printf ("#include \"sim-basics.h\"\n");
-  printf ("#include \"sim-types.h\"\n");
-  printf ("#include \"sim-bits.h\"\n");
 
   gen_struct ();
 
 
 
   printf ("#define DO_BIT_TESTS\n");
-  gen_bit ( 4, msb,  "4",  4);
-  gen_bit ( 5, msb,  "5",  5);
-  gen_bit ( 8, msb,  "8",  8);
-  gen_bit (10, msb, "10", 10);
-  gen_bit (16, msb, "16", 16);
-  gen_bit (32, msb, "32", 32);
-  gen_bit (64, msb, "64", 64);
-  gen_bit (bitsize, msb, "", 64);
+  gen_bit ( 4, msb, "BIT4",  4);
+  gen_bit ( 5, msb, "BIT5",  5);
+  gen_bit ( 8, msb, "BIT8",  8);
+  gen_bit (10, msb, "BIT10", 10);
+  gen_bit (16, msb, "BIT16", 16);
+  gen_bit (32, msb, "BIT32", 32);
+  gen_bit (64, msb, "BIT64", 64);
+  gen_bit (bitsize, msb, "BIT", 64);
+
+  gen_bit ( 8,  8 - 1, "LSBIT8",   8);
+  gen_bit (16, 16 - 1, "LSBIT16", 16);
+  gen_bit (32, 32 - 1, "LSBIT32", 32);
+  gen_bit (64, 64 - 1, "LSBIT64", 64);
+  gen_bit (bitsize, bitsize - 1, "LSBIT",   64);
+
+  gen_bit ( 8,  0, "MSBIT8",   8);
+  gen_bit (16,  0, "MSBIT16", 16);
+  gen_bit (32,  0, "MSBIT32", 32);
+  gen_bit (64,  0, "MSBIT64", 64);
+  gen_bit (bitsize,  0, "MSBIT",   64);
 
   printf ("test_spec *(bit_tests[]) = {\n");
-  printf ("  &bit4_test,\n");
-  printf ("  &bit5_test,\n");
-  printf ("  &bit8_test,\n");
-  printf ("  &bit10_test,\n");
-  printf ("  &bit16_test,\n");
-  printf ("  &bit32_test,\n");
-  printf ("  &bit64_test,\n");
-  printf ("  &bit_test,\n");
+  printf ("  &BIT4_test,\n");
+  printf ("  &BIT5_test,\n");
+  printf ("  &BIT8_test,\n");
+  printf ("  &BIT10_test,\n");
+  printf ("  &BIT16_test,\n");
+  printf ("  &BIT32_test,\n");
+  printf ("  &BIT64_test,\n");
+  printf ("  &BIT_test,\n");
+  printf ("  &LSBIT8_test,\n");
+  printf ("  &LSBIT16_test,\n");
+  printf ("  &LSBIT32_test,\n");
+  printf ("  &LSBIT64_test,\n");
+  printf ("  &LSBIT_test,\n");
+  printf ("  &MSBIT8_test,\n");
+  printf ("  &MSBIT16_test,\n");
+  printf ("  &MSBIT32_test,\n");
+  printf ("  &MSBIT64_test,\n");
+  printf ("  &MSBIT_test,\n");
   printf ("  0,\n");
   printf ("};\n\n");
 
-
+  gen_enum ("BIT",   64);
+  gen_enum ("LSBIT", 64);
+  gen_enum ("MSBIT", 64);
+  gen_enum ("BIT32",   32);
+  gen_enum ("LSBIT32", 32);
+  gen_enum ("MSBIT32", 32);
 
   printf ("#define DO_MASK_TESTS\n");
-  gen_mask (16, ms, "16", 16);
-  gen_mask (32, ms, "32", 32);
-  gen_mask (64, ms, "64", 64);
-  gen_mask (bitsize, ms, "", 64);
+  gen_mask ( 8, ms, "MASK8",  8);
+  gen_mask (16, ms, "MASK16", 16);
+  gen_mask (32, ms, "MASK32", 32);
+  gen_mask (64, ms, "MASK64", 64);
+  gen_mask (bitsize, ms, "MASK", 64);
 
   printf ("test_spec *(mask_tests[]) = {\n");
-  printf ("  &mask16_test,\n");
-  printf ("  &mask32_test,\n");
-  printf ("  &mask64_test,\n");
-  printf ("  &mask_test,\n");
+  printf ("  &MASK8_test,\n");
+  printf ("  &MASK16_test,\n");
+  printf ("  &MASK32_test,\n");
+  printf ("  &MASK64_test,\n");
+  printf ("  &MASK_test,\n");
   printf ("  0,\n");
   printf ("};\n\n");
 
This page took 0.026512 seconds and 4 git commands to generate.