xref: /dflybsd-src/contrib/gcc-4.7/gcc/genconstants.c (revision 04febcfb30580676d3e95f58a16c5137ee478b32)
1*e4b17023SJohn Marino /* Generate from machine description:
2*e4b17023SJohn Marino    a series of #define statements, one for each constant named in
3*e4b17023SJohn Marino    a (define_constants ...) pattern.
4*e4b17023SJohn Marino 
5*e4b17023SJohn Marino    Copyright (C) 1987, 1991, 1995, 1998, 1999, 2000, 2001, 2003, 2004,
6*e4b17023SJohn Marino    2007, 2010  Free Software Foundation, Inc.
7*e4b17023SJohn Marino 
8*e4b17023SJohn Marino This file is part of GCC.
9*e4b17023SJohn Marino 
10*e4b17023SJohn Marino GCC is free software; you can redistribute it and/or modify
11*e4b17023SJohn Marino it under the terms of the GNU General Public License as published by
12*e4b17023SJohn Marino the Free Software Foundation; either version 3, or (at your option)
13*e4b17023SJohn Marino any later version.
14*e4b17023SJohn Marino 
15*e4b17023SJohn Marino GCC is distributed in the hope that it will be useful,
16*e4b17023SJohn Marino but WITHOUT ANY WARRANTY; without even the implied warranty of
17*e4b17023SJohn Marino MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*e4b17023SJohn Marino GNU General Public License for more details.
19*e4b17023SJohn Marino 
20*e4b17023SJohn Marino You should have received a copy of the GNU General Public License
21*e4b17023SJohn Marino along with GCC; see the file COPYING3.  If not see
22*e4b17023SJohn Marino <http://www.gnu.org/licenses/>.  */
23*e4b17023SJohn Marino 
24*e4b17023SJohn Marino /* This program does not use gensupport.c because it does not need to
25*e4b17023SJohn Marino    look at insn patterns, only (define_constants), and we want to
26*e4b17023SJohn Marino    minimize dependencies.  */
27*e4b17023SJohn Marino 
28*e4b17023SJohn Marino #include "bconfig.h"
29*e4b17023SJohn Marino #include "system.h"
30*e4b17023SJohn Marino #include "coretypes.h"
31*e4b17023SJohn Marino #include "errors.h"
32*e4b17023SJohn Marino #include "read-md.h"
33*e4b17023SJohn Marino 
34*e4b17023SJohn Marino /* Called via traverse_md_constants; emit a #define for
35*e4b17023SJohn Marino    the current constant definition.  */
36*e4b17023SJohn Marino 
37*e4b17023SJohn Marino static int
print_md_constant(void ** slot,void * info ATTRIBUTE_UNUSED)38*e4b17023SJohn Marino print_md_constant (void **slot, void *info ATTRIBUTE_UNUSED)
39*e4b17023SJohn Marino {
40*e4b17023SJohn Marino   struct md_constant *def = (struct md_constant *) *slot;
41*e4b17023SJohn Marino 
42*e4b17023SJohn Marino   if (!def->parent_enum)
43*e4b17023SJohn Marino     printf ("#define %s %s\n", def->name, def->value);
44*e4b17023SJohn Marino   return 1;
45*e4b17023SJohn Marino }
46*e4b17023SJohn Marino 
47*e4b17023SJohn Marino /* Called via traverse_enums.  Emit an enum definition for
48*e4b17023SJohn Marino    enum_type *SLOT.  */
49*e4b17023SJohn Marino 
50*e4b17023SJohn Marino static int
print_enum_type(void ** slot,void * info ATTRIBUTE_UNUSED)51*e4b17023SJohn Marino print_enum_type (void **slot, void *info ATTRIBUTE_UNUSED)
52*e4b17023SJohn Marino {
53*e4b17023SJohn Marino   struct enum_type *def;
54*e4b17023SJohn Marino   struct enum_value *value;
55*e4b17023SJohn Marino   char *value_name;
56*e4b17023SJohn Marino 
57*e4b17023SJohn Marino   def = (struct enum_type *) *slot;
58*e4b17023SJohn Marino   printf ("\nenum %s {", def->name);
59*e4b17023SJohn Marino   for (value = def->values; value; value = value->next)
60*e4b17023SJohn Marino     {
61*e4b17023SJohn Marino       printf ("\n  %s = %s", value->def->name, value->def->value);
62*e4b17023SJohn Marino       if (value->next)
63*e4b17023SJohn Marino 	putc (',', stdout);
64*e4b17023SJohn Marino     }
65*e4b17023SJohn Marino   printf ("\n};\n");
66*e4b17023SJohn Marino 
67*e4b17023SJohn Marino   /* Define NUM_<enum>_VALUES to be the largest enum value + 1.  */
68*e4b17023SJohn Marino   value_name = ACONCAT (("num_", def->name, "_values", NULL));
69*e4b17023SJohn Marino   upcase_string (value_name);
70*e4b17023SJohn Marino   printf ("#define %s %d\n", value_name, def->num_values);
71*e4b17023SJohn Marino 
72*e4b17023SJohn Marino   /* Declare the array that is generated by genenum.  */
73*e4b17023SJohn Marino   printf ("extern const char *const %s_strings[];\n", def->name);
74*e4b17023SJohn Marino 
75*e4b17023SJohn Marino   return 1;
76*e4b17023SJohn Marino }
77*e4b17023SJohn Marino 
78*e4b17023SJohn Marino int
main(int argc,char ** argv)79*e4b17023SJohn Marino main (int argc, char **argv)
80*e4b17023SJohn Marino {
81*e4b17023SJohn Marino   progname = "genconstants";
82*e4b17023SJohn Marino 
83*e4b17023SJohn Marino   if (!read_md_files (argc, argv, NULL, NULL))
84*e4b17023SJohn Marino     return (FATAL_EXIT_CODE);
85*e4b17023SJohn Marino 
86*e4b17023SJohn Marino   /* Initializing the MD reader has the side effect of loading up
87*e4b17023SJohn Marino      the constants table that we wish to scan.  */
88*e4b17023SJohn Marino 
89*e4b17023SJohn Marino   puts ("/* Generated automatically by the program `genconstants'");
90*e4b17023SJohn Marino   puts ("   from the machine description file `md'.  */\n");
91*e4b17023SJohn Marino   puts ("#ifndef GCC_INSN_CONSTANTS_H");
92*e4b17023SJohn Marino   puts ("#define GCC_INSN_CONSTANTS_H\n");
93*e4b17023SJohn Marino 
94*e4b17023SJohn Marino   traverse_md_constants (print_md_constant, 0);
95*e4b17023SJohn Marino   traverse_enum_types (print_enum_type, 0);
96*e4b17023SJohn Marino 
97*e4b17023SJohn Marino   puts ("\n#endif /* GCC_INSN_CONSTANTS_H */");
98*e4b17023SJohn Marino 
99*e4b17023SJohn Marino   if (ferror (stdout) || fflush (stdout) || fclose (stdout))
100*e4b17023SJohn Marino     return FATAL_EXIT_CODE;
101*e4b17023SJohn Marino 
102*e4b17023SJohn Marino   return SUCCESS_EXIT_CODE;
103*e4b17023SJohn Marino }
104