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 */
63 struct mode_data *component; /* mode of components */
64 struct mode_data *wider; /* next wider mode */
66 const char *file; /* file and line of definition, */
67 unsigned int line; /* for error reporting */
70 static struct mode_data *known_modes[MAX_MODE_CLASS];
71 static unsigned int n_modes[MAX_MODE_CLASS];
72 static struct mode_data *void_mode;
74 static const struct mode_data blank_mode = {
75 0, "<unknown>", MAX_MODE_CLASS,
81 /* Mode class operations. */
82 static enum mode_class
83 complex_class (enum mode_class class)
87 case MODE_INT: return MODE_COMPLEX_INT;
88 case MODE_FLOAT: return MODE_COMPLEX_FLOAT;
90 error ("no complex class for class %s", mode_class_names[class]);
95 static enum mode_class
96 vector_class (enum mode_class class)
100 case MODE_INT: return MODE_VECTOR_INT;
101 case MODE_FLOAT: return MODE_VECTOR_FLOAT;
103 error ("no vector class for class %s", mode_class_names[class]);
108 static struct mode_data *
109 find_mode (enum mode_class class, const char *name)
113 for (m = known_modes[class]; m; m = m->next)
114 if (!strcmp (name, m->name))
120 static struct mode_data *
121 new_mode (enum mode_class class, const char *name,
122 const char *file, unsigned int line)
126 m = find_mode (class, name);
129 error ("%s:%d: duplicate definition of mode \"%s\"",
130 trim_filename (file), line, name);
131 error ("%s:%d: previous definition here", m->file, m->line);
135 m = xmalloc (sizeof (struct mode_data));
136 memcpy (m, &blank_mode, sizeof (struct mode_data));
140 m->file = trim_filename (file);
143 m->next = known_modes[class];
144 known_modes[class] = m;
149 #define for_all_modes(C, M) \
150 for (C = 0; C < MAX_MODE_CLASS; C++) \
151 for (M = known_modes[C]; M; M = M->next)
154 /* Diagnose failure to meet expectations in a partially filled out
156 enum requirement { SET, UNSET, OPTIONAL };
158 #define validate_field_(mname, fname, req, val, unset, file, line) do { \
163 error ("%s:%d: (%s) field %s must be set", \
164 file, line, mname, fname); \
168 error ("%s:%d: (%s) field %s must not be set", \
169 file, line, mname, fname); \
175 #define validate_field(M, F) \
176 validate_field_(M->name, #F, r_##F, M->F, blank_mode.F, M->file, M->line)
179 validate_mode (struct mode_data *m,
180 enum requirement r_bitsize,
181 enum requirement r_bytesize,
182 enum requirement r_component,
183 enum requirement r_ncomponents)
185 validate_field (m, bitsize);
186 validate_field (m, bytesize);
187 validate_field (m, component);
188 validate_field (m, ncomponents);
190 #undef validate_field
191 #undef validate_field_
193 /* Given a partially-filled-out mode structure, figure out what we can
194 and fill the rest of it in; die if it isn't enough. */
196 complete_mode (struct mode_data *m)
198 unsigned int alignment;
202 error ("%s:%d: mode with no name", m->file, m->line);
205 if (m->class == MAX_MODE_CLASS)
207 error ("%s:%d: %smode has no mode class", m->file, m->line, m->name);
214 /* Nothing more need be said. */
215 if (!strcmp (m->name, "VOID"))
218 validate_mode (m, UNSET, UNSET, UNSET, UNSET);
227 /* Again, nothing more need be said. For historical reasons,
228 the size of a CC mode is four units. */
229 validate_mode (m, UNSET, UNSET, UNSET, UNSET);
238 /* A scalar mode must have a byte size, may have a bit size,
239 and must not have components. */
240 validate_mode (m, OPTIONAL, SET, UNSET, UNSET);
246 case MODE_PARTIAL_INT:
247 /* A partial integer mode uses ->component to say what the
248 corresponding full-size integer mode is, and may also
249 specify a bit size. */
250 validate_mode (m, OPTIONAL, UNSET, SET, UNSET);
252 m->bytesize = m->component->bytesize;
255 m->component = 0; /* ??? preserve this */
258 case MODE_COMPLEX_INT:
259 case MODE_COMPLEX_FLOAT:
260 /* Complex modes should have a component indicated, but no more. */
261 validate_mode (m, UNSET, UNSET, SET, UNSET);
263 if (m->component->bitsize != (unsigned int)-1)
264 m->bitsize = 2 * m->component->bitsize;
265 m->bytesize = 2 * m->component->bytesize;
268 case MODE_VECTOR_INT:
269 case MODE_VECTOR_FLOAT:
270 /* Vector modes should have a component and a number of components. */
271 validate_mode (m, UNSET, UNSET, SET, SET);
272 if (m->component->bitsize != (unsigned int)-1)
273 m->bitsize = m->ncomponents * m->component->bitsize;
274 m->bytesize = m->ncomponents * m->component->bytesize;
281 /* If not already specified, the mode alignment defaults to the largest
282 power of two that divides the size of the object. Complex types are
283 not more aligned than their contents. */
284 if (m->class == MODE_COMPLEX_INT || m->class == MODE_COMPLEX_FLOAT)
285 alignment = m->component->bytesize;
287 alignment = m->bytesize;
289 m->alignment = alignment & (~alignment + 1);
293 complete_all_modes (void)
302 /* For each mode in class CLASS, construct a corresponding complex mode. */
303 #define COMPLEX_MODES(C) make_complex_modes(MODE_##C, __FILE__, __LINE__)
305 make_complex_modes (enum mode_class class,
306 const char *file, unsigned int line)
311 enum mode_class cclass = complex_class (class);
313 if (cclass == MODE_RANDOM)
316 for (m = known_modes[class]; m; m = m->next)
318 /* Skip BImode. FIXME: BImode probably shouldn't be MODE_INT. */
322 if (strlen (m->name) >= sizeof buf)
324 error ("%s:%d:mode name \"%s\" is too long",
325 m->file, m->line, m->name);
329 /* Float complex modes are named SCmode, etc.
330 Int complex modes are named CSImode, etc.
331 This inconsistency should be eliminated. */
332 if (class == MODE_FLOAT)
335 strncpy (buf, m->name, sizeof buf);
336 p = strchr (buf, 'F');
339 error ("%s:%d: float mode \"%s\" has no 'F'",
340 m->file, m->line, m->name);
347 snprintf (buf, sizeof buf, "C%s", m->name);
349 c = new_mode (cclass, xstrdup (buf), file, line);
354 /* For all modes in class CLASS, construct vector modes of width
355 WIDTH, having as many components as necessary. */
356 #define VECTOR_MODES(C, W) make_vector_modes(MODE_##C, W, __FILE__, __LINE__)
358 make_vector_modes (enum mode_class class, unsigned int width,
359 const char *file, unsigned int line)
364 unsigned int ncomponents;
365 enum mode_class vclass = vector_class (class);
367 if (vclass == MODE_RANDOM)
370 for (m = known_modes[class]; m; m = m->next)
372 /* Do not construct vector modes with only one element, or
373 vector modes where the element size doesn't divide the full
375 ncomponents = width / m->bytesize;
378 if (width % m->bytesize)
381 /* Skip QFmode and BImode. FIXME: this special case should
383 if (class == MODE_FLOAT && m->bytesize == 1)
385 if (class == MODE_INT && m->bitsize == 1)
388 if ((size_t)snprintf (buf, sizeof buf, "V%u%s", ncomponents, m->name)
391 error ("%s:%d: mode name \"%s\" is too long",
392 m->file, m->line, m->name);
396 v = new_mode (vclass, xstrdup (buf), file, line);
398 v->ncomponents = ncomponents;
404 #define _SPECIAL_MODE(C, N) make_special_mode(MODE_##C, #N, __FILE__, __LINE__)
405 #define RANDOM_MODE(N) _SPECIAL_MODE (RANDOM, N)
406 #define CC_MODE(N) _SPECIAL_MODE (CC, N)
409 make_special_mode (enum mode_class class, const char *name,
410 const char *file, unsigned int line)
412 new_mode (class, name, file, line);
415 #define _SCALAR_MODE(C, N, B, Y) \
416 make_scalar_mode (MODE_##C, #N, B, Y, __FILE__, __LINE__)
418 #define INT_MODE(N, Y) _SCALAR_MODE (INT, N, -1, Y)
419 #define FRACTIONAL_INT_MODE(N, B, Y) _SCALAR_MODE (INT, N, B, Y)
420 #define FLOAT_MODE(N, Y) _SCALAR_MODE (FLOAT, N, -1, Y)
421 #define FRACTIONAL_FLOAT_MODE(N, B, Y) _SCALAR_MODE (FLOAT, N, B, Y)
424 make_scalar_mode (enum mode_class class, const char *name,
425 unsigned int bitsize, unsigned int bytesize,
426 const char *file, unsigned int line)
428 struct mode_data *m = new_mode (class, name, file, line);
429 m->bytesize = bytesize;
430 m->bitsize = bitsize;
433 /* Partial integer modes are specified by relation to a full integer mode.
434 For now, we do not attempt to narrow down their bit sizes. */
435 #define PARTIAL_INT_MODE(M) \
436 make_partial_integer_mode (#M, "P" #M, -1, __FILE__, __LINE__)
437 static void ATTRIBUTE_UNUSED
438 make_partial_integer_mode (const char *base, const char *name,
439 unsigned int bitsize,
440 const char *file, unsigned int line)
443 struct mode_data *component = find_mode (MODE_INT, base);
446 error ("%s:%d: no mode \"%s\" in class INT", file, line, name);
450 m = new_mode (MODE_PARTIAL_INT, name, file, line);
451 m->bitsize = bitsize;
452 m->component = component;
455 /* A single vector mode can be specified by naming its component
456 mode and the number of components. */
457 #define VECTOR_MODE(C, M, N) \
458 make_vector_mode (MODE_##C, #M, N, __FILE__, __LINE__);
459 static void ATTRIBUTE_UNUSED
460 make_vector_mode (enum mode_class bclass,
462 unsigned int ncomponents,
463 const char *file, unsigned int line)
466 enum mode_class vclass = vector_class (bclass);
467 struct mode_data *component = find_mode (bclass, base);
470 if (vclass == MODE_RANDOM)
474 error ("%s:%d: no mode \"%s\" in class %s",
475 file, line, base, mode_class_names[bclass] + 5);
479 if ((size_t)snprintf (namebuf, sizeof namebuf, "V%u%s",
480 ncomponents, base) >= sizeof namebuf)
482 error ("%s:%d: mode name \"%s\" is too long",
487 v = new_mode (vclass, xstrdup (namebuf), file, line);
488 v->ncomponents = ncomponents;
489 v->component = component;
496 #include "machmode.def"
501 /* Sort a list of modes into the order needed for the WIDER field:
502 major sort by bitsize, minor sort by component bitsize.
505 QI < HI < SI < DI < TI
506 V4QI < V2HI < V8QI < V4HI < V2SI.
508 If the bitsize is not set, sort by the bytesize. A mode with
509 bitsize set gets sorted before a mode without bitsize set, if
510 they have the same bytesize; this is the right thing because
511 the bitsize must always be smaller than the bytesize * BITS_PER_UNIT.
512 We don't have to do anything special to get this done -- an unset
513 bitsize shows up as (unsigned int)-1, i.e. UINT_MAX. */
515 cmp_modes (const void *a, const void *b)
517 struct mode_data *m = *(struct mode_data **)a;
518 struct mode_data *n = *(struct mode_data **)b;
520 if (m->bytesize > n->bytesize)
522 else if (m->bytesize < n->bytesize)
525 if (m->bitsize > n->bitsize)
527 else if (m->bitsize < n->bitsize)
530 if (!m->component && !n->component)
533 if (m->component->bytesize > n->component->bytesize)
535 else if (m->component->bytesize < n->component->bytesize)
538 if (m->component->bitsize > n->component->bitsize)
540 else if (m->component->bitsize < n->component->bitsize)
547 calc_wider_mode (void)
551 struct mode_data **sortbuf;
552 unsigned int max_n_modes = 0;
555 for (c = 0; c < MAX_MODE_CLASS; c++)
556 max_n_modes = MAX (max_n_modes, n_modes[c]);
558 sortbuf = alloca (max_n_modes * sizeof (struct mode_data *));
560 for (c = 0; c < MAX_MODE_CLASS; c++)
562 /* "wider" is not meaningful for MODE_RANDOM and MODE_CC.
563 However, we want these in textual order, and we have
564 precisely the reverse. */
565 if (c == MODE_RANDOM || c == MODE_CC)
567 struct mode_data *prev, *next;
569 for (prev = 0, m = known_modes[c]; m; m = next)
571 m->wider = void_mode;
573 /* this is nreverse */
578 known_modes[c] = prev;
585 for (i = 0, m = known_modes[c]; m; i++, m = m->next)
588 qsort (sortbuf, i, sizeof (struct mode_data *), cmp_modes);
591 for (j = 0; j < i; j++)
592 sortbuf[j]->next = sortbuf[j]->wider = sortbuf[j + 1];
595 known_modes[c] = sortbuf[0];
600 /* Output routines. */
602 #define tagged_printf(FMT, ARG, TAG) do { \
604 printf (" " FMT ",%n", ARG, &count_); \
605 printf ("%*s/* %s */\n", 27 - count_, "", TAG); \
608 #define print_decl(TYPE, NAME, ASIZE) \
609 printf ("\nconst %s %s[%s] =\n{\n", TYPE, NAME, ASIZE);
611 #define print_closer() puts ("};")
614 emit_insn_modes_h (void)
617 struct mode_data *m, *first, *last;
619 printf ("/* Generated automatically from machmode.def%s%s\n",
620 HAVE_EXTRA_MODES ? " and " : "",
626 #ifndef GCC_INSN_MODES_H\n\
627 #define GCC_INSN_MODES_H\n\
629 enum machine_mode\n{");
631 for (c = 0; c < MAX_MODE_CLASS; c++)
632 for (m = known_modes[c]; m; m = m->next)
635 printf (" %smode,%n", m->name, &count_);
636 printf ("%*s/* %s:%d */\n", 27 - count_, "",
637 trim_filename (m->file), m->line);
640 puts (" MAX_MACHINE_MODE,\n");
642 for (c = 0; c < MAX_MODE_CLASS; c++)
644 first = known_modes[c];
646 for (m = first; m; last = m, m = m->next)
649 /* Don't use BImode for MIN_MODE_INT, since otherwise the middle
650 end will try to use it for bitfields in structures and the
651 like, which we do not want. Only the target md file should
652 generate BImode widgets. */
653 if (first && first->bitsize == 1)
657 printf (" MIN_%s = %smode,\n MAX_%s = %smode,\n\n",
658 mode_class_names[c], first->name,
659 mode_class_names[c], last->name);
661 printf (" MIN_%s = %smode,\n MAX_%s = %smode,\n\n",
662 mode_class_names[c], void_mode->name,
663 mode_class_names[c], void_mode->name);
667 NUM_MACHINE_MODES = MAX_MACHINE_MODE\n\
670 #endif /* insn-modes.h */");
674 emit_insn_modes_c_header (void)
676 printf ("/* Generated automatically from machmode.def%s%s\n",
677 HAVE_EXTRA_MODES ? " and " : "",
683 #define GENERATOR_FILE /* This inhibits insn-flags.h and\n\
684 insn-constants.h, which don't exist yet. */\n\
685 #include \"config.h\"\n\
686 #include \"system.h\"\n\
687 #include \"coretypes.h\"\n\
689 #include \"machmode.h\"");
693 emit_mode_name (void)
698 print_decl ("char *const", "mode_name", "NUM_MACHINE_MODES");
701 printf (" \"%s\",\n", m->name);
707 emit_mode_class (void)
712 print_decl ("unsigned char", "mode_class", "NUM_MACHINE_MODES");
715 tagged_printf ("%s", mode_class_names[m->class], m->name);
721 emit_mode_bitsize (void)
726 print_decl ("unsigned short", "mode_bitsize", "NUM_MACHINE_MODES");
729 if (m->bitsize != (unsigned int)-1)
730 tagged_printf ("%u", m->bitsize, m->name);
732 tagged_printf ("%u*BITS_PER_UNIT", m->bytesize, m->name);
738 emit_mode_size (void)
743 print_decl ("unsigned char", "mode_size", "NUM_MACHINE_MODES");
746 tagged_printf ("%u", m->bytesize, m->name);
752 emit_mode_unit_size (void)
757 print_decl ("unsigned char", "mode_unit_size", "NUM_MACHINE_MODES");
762 ? m->component->bytesize : m->bytesize,
769 emit_mode_wider (void)
774 print_decl ("unsigned char", "mode_wider", "NUM_MACHINE_MODES");
777 tagged_printf ("%smode",
778 m->wider ? m->wider->name : void_mode->name,
785 emit_mode_mask (void)
790 print_decl ("unsigned HOST_WIDE_INT", "mode_mask_array",
791 "NUM_MACHINE_MODES");
793 #define MASK(m) \\\n\
794 ((m) >= HOST_BITS_PER_WIDE_INT) \\\n\
795 ? ~(unsigned HOST_WIDE_INT) 0 \\\n\
796 : ((unsigned HOST_WIDE_INT) 1 << (m)) - 1\n");
799 if (m->bitsize != (unsigned int)-1)
800 tagged_printf ("MASK (%u)", m->bitsize, m->name);
802 tagged_printf ("MASK (%u*BITS_PER_UNIT)", m->bytesize, m->name);
804 puts ("#undef MASK");
809 emit_mode_inner (void)
814 print_decl ("unsigned char", "mode_inner", "NUM_MACHINE_MODES");
817 tagged_printf ("%smode",
818 m->component ? m->component->name : void_mode->name,
825 emit_mode_base_align (void)
830 print_decl ("unsigned char", "mode_base_align", "NUM_MACHINE_MODES");
833 tagged_printf ("%u", m->alignment, m->name);
839 emit_class_narrowest_mode (void)
843 print_decl ("unsigned char", "class_narrowest_mode", "MAX_MODE_CLASS");
845 for (c = 0; c < MAX_MODE_CLASS; c++)
846 /* Bleah, all this to get the comment right for MIN_MODE_INT. */
847 tagged_printf ("MIN_%s", mode_class_names[c],
849 ? (known_modes[c]->bitsize != 1
850 ? known_modes[c]->name
851 : (known_modes[c]->next
852 ? known_modes[c]->next->name
860 emit_insn_modes_c (void)
862 emit_insn_modes_c_header ();
865 emit_mode_bitsize ();
867 emit_mode_unit_size ();
871 emit_mode_base_align ();
872 emit_class_narrowest_mode ();
875 /* Master control. */
877 main(int argc, char **argv)
884 else if (argc == 2 && !strcmp (argv[1], "-h"))
888 error ("usage: %s [-h] > file", progname);
889 return FATAL_EXIT_CODE;
893 complete_all_modes ();
896 return FATAL_EXIT_CODE;
901 emit_insn_modes_h ();
903 emit_insn_modes_c ();
905 if (fflush (stdout) || fclose (stdout))
906 return FATAL_EXIT_CODE;
907 return SUCCESS_EXIT_CODE;