1 /* Generate the machine mode enumeration and associated tables.
3 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
26 /* enum mode_class is normally defined by machmode.h but we can't
27 include that header here. */
28 #include "mode-classes.def"
30 #define DEF_MODE_CLASS(M) M
31 enum mode_class { MODE_CLASSES, MAX_MODE_CLASS };
34 /* Text names of mode classes, for output. */
35 #define DEF_MODE_CLASS(M) #M
36 static const char *const mode_class_names[MAX_MODE_CLASS] =
43 #ifdef EXTRA_MODES_FILE
44 # define HAVE_EXTRA_MODES 1
46 # define HAVE_EXTRA_MODES 0
47 # define EXTRA_MODES_FILE ""
50 /* Data structure for building up what we know about a mode.
51 They're clustered by mode class. */
54 struct mode_data *next; /* next this class - arbitrary order */
56 const char *name; /* printable mode name -- SI, not SImode */
57 enum mode_class class; /* this mode class */
58 unsigned int bitsize; /* size in bits, equiv to TYPE_PRECISION */
59 unsigned int bytesize; /* storage size in addressable units */
60 unsigned int ncomponents; /* number of subunits */
61 unsigned int alignment; /* mode alignment */
62 const char *format; /* floating point format - MODE_FLOAT only */
64 struct mode_data *component; /* mode of components */
65 struct mode_data *wider; /* next wider mode */
67 const char *file; /* file and line of definition, */
68 unsigned int line; /* for error reporting */
71 static struct mode_data *known_modes[MAX_MODE_CLASS];
72 static unsigned int n_modes[MAX_MODE_CLASS];
73 static struct mode_data *void_mode;
75 static const struct mode_data blank_mode = {
76 0, "<unknown>", MAX_MODE_CLASS,
82 /* Mode class operations. */
83 static enum mode_class
84 complex_class (enum mode_class class)
88 case MODE_INT: return MODE_COMPLEX_INT;
89 case MODE_FLOAT: return MODE_COMPLEX_FLOAT;
91 error ("no complex class for class %s", mode_class_names[class]);
96 static enum mode_class
97 vector_class (enum mode_class class)
101 case MODE_INT: return MODE_VECTOR_INT;
102 case MODE_FLOAT: return MODE_VECTOR_FLOAT;
104 error ("no vector class for class %s", mode_class_names[class]);
109 static struct mode_data *
110 find_mode (enum mode_class class, const char *name)
114 for (m = known_modes[class]; m; m = m->next)
115 if (!strcmp (name, m->name))
121 static struct mode_data *
122 new_mode (enum mode_class class, const char *name,
123 const char *file, unsigned int line)
127 m = find_mode (class, name);
130 error ("%s:%d: duplicate definition of mode \"%s\"",
131 trim_filename (file), line, name);
132 error ("%s:%d: previous definition here", m->file, m->line);
136 m = xmalloc (sizeof (struct mode_data));
137 memcpy (m, &blank_mode, sizeof (struct mode_data));
141 m->file = trim_filename (file);
144 m->next = known_modes[class];
145 known_modes[class] = m;
150 #define for_all_modes(C, M) \
151 for (C = 0; C < MAX_MODE_CLASS; C++) \
152 for (M = known_modes[C]; M; M = M->next)
155 /* Diagnose failure to meet expectations in a partially filled out
157 enum requirement { SET, UNSET, OPTIONAL };
159 #define validate_field_(mname, fname, req, val, unset, file, line) do { \
164 error ("%s:%d: (%s) field %s must be set", \
165 file, line, mname, fname); \
169 error ("%s:%d: (%s) field %s must not be set", \
170 file, line, mname, fname); \
176 #define validate_field(M, F) \
177 validate_field_(M->name, #F, r_##F, M->F, blank_mode.F, M->file, M->line)
180 validate_mode (struct mode_data *m,
181 enum requirement r_bitsize,
182 enum requirement r_bytesize,
183 enum requirement r_component,
184 enum requirement r_ncomponents,
185 enum requirement r_format)
187 validate_field (m, bitsize);
188 validate_field (m, bytesize);
189 validate_field (m, component);
190 validate_field (m, ncomponents);
191 validate_field (m, format);
193 #undef validate_field
194 #undef validate_field_
196 /* Given a partially-filled-out mode structure, figure out what we can
197 and fill the rest of it in; die if it isn't enough. */
199 complete_mode (struct mode_data *m)
201 unsigned int alignment;
205 error ("%s:%d: mode with no name", m->file, m->line);
208 if (m->class == MAX_MODE_CLASS)
210 error ("%s:%d: %smode has no mode class", m->file, m->line, m->name);
217 /* Nothing more need be said. */
218 if (!strcmp (m->name, "VOID"))
221 validate_mode (m, UNSET, UNSET, UNSET, UNSET, UNSET);
230 /* Again, nothing more need be said. For historical reasons,
231 the size of a CC mode is four units. */
232 validate_mode (m, UNSET, UNSET, UNSET, UNSET, UNSET);
241 /* A scalar mode must have a byte size, may have a bit size,
242 and must not have components. A float mode must have a
244 validate_mode (m, OPTIONAL, SET, UNSET, UNSET,
245 m->class == MODE_FLOAT ? SET : UNSET);
251 case MODE_PARTIAL_INT:
252 /* A partial integer mode uses ->component to say what the
253 corresponding full-size integer mode is, and may also
254 specify a bit size. */
255 validate_mode (m, OPTIONAL, UNSET, SET, UNSET, UNSET);
257 m->bytesize = m->component->bytesize;
260 m->component = 0; /* ??? preserve this */
263 case MODE_COMPLEX_INT:
264 case MODE_COMPLEX_FLOAT:
265 /* Complex modes should have a component indicated, but no more. */
266 validate_mode (m, UNSET, UNSET, SET, UNSET, UNSET);
268 if (m->component->bitsize != (unsigned int)-1)
269 m->bitsize = 2 * m->component->bitsize;
270 m->bytesize = 2 * m->component->bytesize;
273 case MODE_VECTOR_INT:
274 case MODE_VECTOR_FLOAT:
275 /* Vector modes should have a component and a number of components. */
276 validate_mode (m, UNSET, UNSET, SET, SET, UNSET);
277 if (m->component->bitsize != (unsigned int)-1)
278 m->bitsize = m->ncomponents * m->component->bitsize;
279 m->bytesize = m->ncomponents * m->component->bytesize;
286 /* If not already specified, the mode alignment defaults to the largest
287 power of two that divides the size of the object. Complex types are
288 not more aligned than their contents. */
289 if (m->class == MODE_COMPLEX_INT || m->class == MODE_COMPLEX_FLOAT)
290 alignment = m->component->bytesize;
292 alignment = m->bytesize;
294 m->alignment = alignment & (~alignment + 1);
298 complete_all_modes (void)
307 /* For each mode in class CLASS, construct a corresponding complex mode. */
308 #define COMPLEX_MODES(C) make_complex_modes(MODE_##C, __FILE__, __LINE__)
310 make_complex_modes (enum mode_class class,
311 const char *file, unsigned int line)
316 enum mode_class cclass = complex_class (class);
318 if (cclass == MODE_RANDOM)
321 for (m = known_modes[class]; m; m = m->next)
323 /* Skip BImode. FIXME: BImode probably shouldn't be MODE_INT. */
327 if (strlen (m->name) >= sizeof buf)
329 error ("%s:%d:mode name \"%s\" is too long",
330 m->file, m->line, m->name);
334 /* Float complex modes are named SCmode, etc.
335 Int complex modes are named CSImode, etc.
336 This inconsistency should be eliminated. */
337 if (class == MODE_FLOAT)
340 strncpy (buf, m->name, sizeof buf);
341 p = strchr (buf, 'F');
344 error ("%s:%d: float mode \"%s\" has no 'F'",
345 m->file, m->line, m->name);
352 snprintf (buf, sizeof buf, "C%s", m->name);
354 c = new_mode (cclass, xstrdup (buf), file, line);
359 /* For all modes in class CLASS, construct vector modes of width
360 WIDTH, having as many components as necessary. */
361 #define VECTOR_MODES(C, W) make_vector_modes(MODE_##C, W, __FILE__, __LINE__)
363 make_vector_modes (enum mode_class class, unsigned int width,
364 const char *file, unsigned int line)
369 unsigned int ncomponents;
370 enum mode_class vclass = vector_class (class);
372 if (vclass == MODE_RANDOM)
375 for (m = known_modes[class]; m; m = m->next)
377 /* Do not construct vector modes with only one element, or
378 vector modes where the element size doesn't divide the full
380 ncomponents = width / m->bytesize;
383 if (width % m->bytesize)
386 /* Skip QFmode and BImode. FIXME: this special case should
388 if (class == MODE_FLOAT && m->bytesize == 1)
390 if (class == MODE_INT && m->bitsize == 1)
393 if ((size_t)snprintf (buf, sizeof buf, "V%u%s", ncomponents, m->name)
396 error ("%s:%d: mode name \"%s\" is too long",
397 m->file, m->line, m->name);
401 v = new_mode (vclass, xstrdup (buf), file, line);
403 v->ncomponents = ncomponents;
409 #define _SPECIAL_MODE(C, N) make_special_mode(MODE_##C, #N, __FILE__, __LINE__)
410 #define RANDOM_MODE(N) _SPECIAL_MODE (RANDOM, N)
411 #define CC_MODE(N) _SPECIAL_MODE (CC, N)
414 make_special_mode (enum mode_class class, const char *name,
415 const char *file, unsigned int line)
417 new_mode (class, name, file, line);
420 #define INT_MODE(N, Y) FRACTIONAL_INT_MODE (N, -1, Y)
421 #define FRACTIONAL_INT_MODE(N, B, Y) \
422 make_int_mode (#N, B, Y, __FILE__, __LINE__)
425 make_int_mode (const char *name,
426 unsigned int bitsize, unsigned int bytesize,
427 const char *file, unsigned int line)
429 struct mode_data *m = new_mode (MODE_INT, name, file, line);
430 m->bytesize = bytesize;
431 m->bitsize = bitsize;
434 #define FLOAT_MODE(N, Y, F) FRACTIONAL_FLOAT_MODE (N, -1, Y, F)
435 #define FRACTIONAL_FLOAT_MODE(N, B, Y, F) \
436 make_float_mode (#N, B, Y, #F, __FILE__, __LINE__)
439 make_float_mode (const char *name,
440 unsigned int bitsize, unsigned int bytesize,
442 const char *file, unsigned int line)
444 struct mode_data *m = new_mode (MODE_FLOAT, name, file, line);
445 m->bytesize = bytesize;
446 m->bitsize = bitsize;
450 #define RESET_FLOAT_FORMAT(N, F) \
451 reset_float_format (#N, #F, __FILE__, __LINE__)
452 static void ATTRIBUTE_UNUSED
453 reset_float_format (const char *name, const char *format,
454 const char *file, const char *line)
456 struct mode_data *m = find_mode (MODE_FLOAT, name);
459 error ("%s:%d: no mode \"%s\" in class FLOAT", file, line, name);
465 /* Partial integer modes are specified by relation to a full integer mode.
466 For now, we do not attempt to narrow down their bit sizes. */
467 #define PARTIAL_INT_MODE(M) \
468 make_partial_integer_mode (#M, "P" #M, -1, __FILE__, __LINE__)
469 static void ATTRIBUTE_UNUSED
470 make_partial_integer_mode (const char *base, const char *name,
471 unsigned int bitsize,
472 const char *file, unsigned int line)
475 struct mode_data *component = find_mode (MODE_INT, base);
478 error ("%s:%d: no mode \"%s\" in class INT", file, line, name);
482 m = new_mode (MODE_PARTIAL_INT, name, file, line);
483 m->bitsize = bitsize;
484 m->component = component;
487 /* A single vector mode can be specified by naming its component
488 mode and the number of components. */
489 #define VECTOR_MODE(C, M, N) \
490 make_vector_mode (MODE_##C, #M, N, __FILE__, __LINE__);
491 static void ATTRIBUTE_UNUSED
492 make_vector_mode (enum mode_class bclass,
494 unsigned int ncomponents,
495 const char *file, unsigned int line)
498 enum mode_class vclass = vector_class (bclass);
499 struct mode_data *component = find_mode (bclass, base);
502 if (vclass == MODE_RANDOM)
506 error ("%s:%d: no mode \"%s\" in class %s",
507 file, line, base, mode_class_names[bclass] + 5);
511 if ((size_t)snprintf (namebuf, sizeof namebuf, "V%u%s",
512 ncomponents, base) >= sizeof namebuf)
514 error ("%s:%d: mode name \"%s\" is too long",
519 v = new_mode (vclass, xstrdup (namebuf), file, line);
520 v->ncomponents = ncomponents;
521 v->component = component;
528 #include "machmode.def"
533 /* Sort a list of modes into the order needed for the WIDER field:
534 major sort by bitsize, minor sort by component bitsize.
537 QI < HI < SI < DI < TI
538 V4QI < V2HI < V8QI < V4HI < V2SI.
540 If the bitsize is not set, sort by the bytesize. A mode with
541 bitsize set gets sorted before a mode without bitsize set, if
542 they have the same bytesize; this is the right thing because
543 the bitsize must always be smaller than the bytesize * BITS_PER_UNIT.
544 We don't have to do anything special to get this done -- an unset
545 bitsize shows up as (unsigned int)-1, i.e. UINT_MAX. */
547 cmp_modes (const void *a, const void *b)
549 struct mode_data *m = *(struct mode_data **)a;
550 struct mode_data *n = *(struct mode_data **)b;
552 if (m->bytesize > n->bytesize)
554 else if (m->bytesize < n->bytesize)
557 if (m->bitsize > n->bitsize)
559 else if (m->bitsize < n->bitsize)
562 if (!m->component && !n->component)
565 if (m->component->bytesize > n->component->bytesize)
567 else if (m->component->bytesize < n->component->bytesize)
570 if (m->component->bitsize > n->component->bitsize)
572 else if (m->component->bitsize < n->component->bitsize)
579 calc_wider_mode (void)
583 struct mode_data **sortbuf;
584 unsigned int max_n_modes = 0;
587 for (c = 0; c < MAX_MODE_CLASS; c++)
588 max_n_modes = MAX (max_n_modes, n_modes[c]);
590 sortbuf = alloca (max_n_modes * sizeof (struct mode_data *));
592 for (c = 0; c < MAX_MODE_CLASS; c++)
594 /* "wider" is not meaningful for MODE_RANDOM and MODE_CC.
595 However, we want these in textual order, and we have
596 precisely the reverse. */
597 if (c == MODE_RANDOM || c == MODE_CC)
599 struct mode_data *prev, *next;
601 for (prev = 0, m = known_modes[c]; m; m = next)
603 m->wider = void_mode;
605 /* this is nreverse */
610 known_modes[c] = prev;
617 for (i = 0, m = known_modes[c]; m; i++, m = m->next)
620 qsort (sortbuf, i, sizeof (struct mode_data *), cmp_modes);
623 for (j = 0; j < i; j++)
624 sortbuf[j]->next = sortbuf[j]->wider = sortbuf[j + 1];
627 known_modes[c] = sortbuf[0];
632 /* Output routines. */
634 #define tagged_printf(FMT, ARG, TAG) do { \
636 printf (" " FMT ",%n", ARG, &count_); \
637 printf ("%*s/* %s */\n", 27 - count_, "", TAG); \
640 #define print_decl(TYPE, NAME, ASIZE) \
641 printf ("\nconst %s %s[%s] =\n{\n", TYPE, NAME, ASIZE);
643 #define print_closer() puts ("};")
646 emit_insn_modes_h (void)
649 struct mode_data *m, *first, *last;
651 printf ("/* Generated automatically from machmode.def%s%s\n",
652 HAVE_EXTRA_MODES ? " and " : "",
658 #ifndef GCC_INSN_MODES_H\n\
659 #define GCC_INSN_MODES_H\n\
661 enum machine_mode\n{");
663 for (c = 0; c < MAX_MODE_CLASS; c++)
664 for (m = known_modes[c]; m; m = m->next)
667 printf (" %smode,%n", m->name, &count_);
668 printf ("%*s/* %s:%d */\n", 27 - count_, "",
669 trim_filename (m->file), m->line);
672 puts (" MAX_MACHINE_MODE,\n");
674 for (c = 0; c < MAX_MODE_CLASS; c++)
676 first = known_modes[c];
678 for (m = first; m; last = m, m = m->next)
681 /* Don't use BImode for MIN_MODE_INT, since otherwise the middle
682 end will try to use it for bitfields in structures and the
683 like, which we do not want. Only the target md file should
684 generate BImode widgets. */
685 if (first && first->bitsize == 1)
689 printf (" MIN_%s = %smode,\n MAX_%s = %smode,\n\n",
690 mode_class_names[c], first->name,
691 mode_class_names[c], last->name);
693 printf (" MIN_%s = %smode,\n MAX_%s = %smode,\n\n",
694 mode_class_names[c], void_mode->name,
695 mode_class_names[c], void_mode->name);
699 NUM_MACHINE_MODES = MAX_MACHINE_MODE\n\
702 #endif /* insn-modes.h */");
706 emit_insn_modes_c_header (void)
708 printf ("/* Generated automatically from machmode.def%s%s\n",
709 HAVE_EXTRA_MODES ? " and " : "",
715 #include \"config.h\"\n\
716 #include \"system.h\"\n\
717 #include \"coretypes.h\"\n\
719 #include \"machmode.h\"\n\
720 #include \"real.h\"");
724 emit_min_insn_modes_c_header (void)
726 printf ("/* Generated automatically from machmode.def%s%s\n",
727 HAVE_EXTRA_MODES ? " and " : "",
733 #include \"bconfig.h\"\n\
734 #include \"system.h\"\n\
735 #include \"machmode.h\"");
739 emit_mode_name (void)
744 print_decl ("char *const", "mode_name", "NUM_MACHINE_MODES");
747 printf (" \"%s\",\n", m->name);
753 emit_mode_class (void)
758 print_decl ("unsigned char", "mode_class", "NUM_MACHINE_MODES");
761 tagged_printf ("%s", mode_class_names[m->class], m->name);
767 emit_mode_bitsize (void)
772 print_decl ("unsigned short", "mode_bitsize", "NUM_MACHINE_MODES");
775 if (m->bitsize != (unsigned int)-1)
776 tagged_printf ("%u", m->bitsize, m->name);
778 tagged_printf ("%u*BITS_PER_UNIT", m->bytesize, m->name);
784 emit_mode_size (void)
789 print_decl ("unsigned char", "mode_size", "NUM_MACHINE_MODES");
792 tagged_printf ("%u", m->bytesize, m->name);
798 emit_mode_unit_size (void)
803 print_decl ("unsigned char", "mode_unit_size", "NUM_MACHINE_MODES");
808 ? m->component->bytesize : m->bytesize,
815 emit_mode_wider (void)
820 print_decl ("unsigned char", "mode_wider", "NUM_MACHINE_MODES");
823 tagged_printf ("%smode",
824 m->wider ? m->wider->name : void_mode->name,
831 emit_mode_mask (void)
836 print_decl ("unsigned HOST_WIDE_INT", "mode_mask_array",
837 "NUM_MACHINE_MODES");
839 #define MODE_MASK(m) \\\n\
840 ((m) >= HOST_BITS_PER_WIDE_INT) \\\n\
841 ? ~(unsigned HOST_WIDE_INT) 0 \\\n\
842 : ((unsigned HOST_WIDE_INT) 1 << (m)) - 1\n");
845 if (m->bitsize != (unsigned int)-1)
846 tagged_printf ("MODE_MASK (%u)", m->bitsize, m->name);
848 tagged_printf ("MODE_MASK (%u*BITS_PER_UNIT)", m->bytesize, m->name);
850 puts ("#undef MODE_MASK");
855 emit_mode_inner (void)
860 print_decl ("unsigned char", "mode_inner", "NUM_MACHINE_MODES");
863 tagged_printf ("%smode",
864 m->component ? m->component->name : void_mode->name,
871 emit_mode_base_align (void)
876 print_decl ("unsigned char", "mode_base_align", "NUM_MACHINE_MODES");
879 tagged_printf ("%u", m->alignment, m->name);
885 emit_class_narrowest_mode (void)
889 print_decl ("unsigned char", "class_narrowest_mode", "MAX_MODE_CLASS");
891 for (c = 0; c < MAX_MODE_CLASS; c++)
892 /* Bleah, all this to get the comment right for MIN_MODE_INT. */
893 tagged_printf ("MIN_%s", mode_class_names[c],
895 ? (known_modes[c]->bitsize != 1
896 ? known_modes[c]->name
897 : (known_modes[c]->next
898 ? known_modes[c]->next->name
906 emit_real_format_for_mode (void)
910 /* This will produce a table which is not constant, but points to
911 entities that are constant, which is what we want. */
912 print_decl ("struct real_format *\n ", "real_format_for_mode",
913 "MAX_MODE_FLOAT - MIN_MODE_FLOAT + 1");
915 for (m = known_modes[MODE_FLOAT]; m; m = m->next)
916 if (!strcmp (m->format, "0"))
917 tagged_printf ("%s", m->format, m->name);
919 tagged_printf ("&%s", m->format, m->name);
925 emit_insn_modes_c (void)
927 emit_insn_modes_c_header ();
930 emit_mode_bitsize ();
932 emit_mode_unit_size ();
936 emit_mode_base_align ();
937 emit_class_narrowest_mode ();
938 emit_real_format_for_mode ();
942 emit_min_insn_modes_c (void)
944 emit_min_insn_modes_c_header ();
948 emit_class_narrowest_mode ();
951 /* Master control. */
953 main(int argc, char **argv)
955 bool gen_header = false, gen_min = false;
960 else if (argc == 2 && !strcmp (argv[1], "-h"))
962 else if (argc == 2 && !strcmp (argv[1], "-m"))
966 error ("usage: %s [-h|-m] > file", progname);
967 return FATAL_EXIT_CODE;
971 complete_all_modes ();
974 return FATAL_EXIT_CODE;
979 emit_insn_modes_h ();
981 emit_min_insn_modes_c ();
983 emit_insn_modes_c ();
985 if (fflush (stdout) || fclose (stdout))
986 return FATAL_EXIT_CODE;
987 return SUCCESS_EXIT_CODE;