PR c++/60252
[official-gcc.git] / gcc / genmodes.c
blob8cc3cdeeeb3117f654ccbe1b0946a0105702467d
1 /* Generate the machine mode enumeration and associated tables.
2 Copyright (C) 2003-2014 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 #include "bconfig.h"
21 #include "system.h"
22 #include "errors.h"
23 #include "hashtab.h"
25 /* enum mode_class is normally defined by machmode.h but we can't
26 include that header here. */
27 #include "mode-classes.def"
29 #define DEF_MODE_CLASS(M) M
30 enum mode_class { MODE_CLASSES, MAX_MODE_CLASS };
31 #undef DEF_MODE_CLASS
33 /* Text names of mode classes, for output. */
34 #define DEF_MODE_CLASS(M) #M
35 static const char *const mode_class_names[MAX_MODE_CLASS] =
37 MODE_CLASSES
39 #undef DEF_MODE_CLASS
40 #undef MODE_CLASSES
42 #ifdef EXTRA_MODES_FILE
43 # define HAVE_EXTRA_MODES 1
44 #else
45 # define HAVE_EXTRA_MODES 0
46 # define EXTRA_MODES_FILE ""
47 #endif
49 /* Data structure for building up what we know about a mode.
50 They're clustered by mode class. */
51 struct mode_data
53 struct mode_data *next; /* next this class - arbitrary order */
55 const char *name; /* printable mode name -- SI, not SImode */
56 enum mode_class cl; /* this mode class */
57 unsigned int precision; /* size in bits, equiv to TYPE_PRECISION */
58 unsigned int bytesize; /* storage size in addressable units */
59 unsigned int ncomponents; /* number of subunits */
60 unsigned int alignment; /* mode alignment */
61 const char *format; /* floating point format - float modes only */
63 struct mode_data *component; /* mode of components */
64 struct mode_data *wider; /* next wider mode */
66 struct mode_data *contained; /* Pointer to list of modes that have
67 this mode as a component. */
68 struct mode_data *next_cont; /* Next mode in that list. */
70 const char *file; /* file and line of definition, */
71 unsigned int line; /* for error reporting */
72 unsigned int counter; /* Rank ordering of modes */
73 unsigned int ibit; /* the number of integral bits */
74 unsigned int fbit; /* the number of fractional bits */
77 static struct mode_data *modes[MAX_MODE_CLASS];
78 static unsigned int n_modes[MAX_MODE_CLASS];
79 static struct mode_data *void_mode;
81 static const struct mode_data blank_mode = {
82 0, "<unknown>", MAX_MODE_CLASS,
83 -1U, -1U, -1U, -1U,
84 0, 0, 0, 0, 0,
85 "<unknown>", 0, 0, 0, 0
88 static htab_t modes_by_name;
90 /* Data structure for recording target-specified runtime adjustments
91 to a particular mode. We support varying the byte size, the
92 alignment, and the floating point format. */
93 struct mode_adjust
95 struct mode_adjust *next;
96 struct mode_data *mode;
97 const char *adjustment;
99 const char *file;
100 unsigned int line;
103 static struct mode_adjust *adj_bytesize;
104 static struct mode_adjust *adj_alignment;
105 static struct mode_adjust *adj_format;
106 static struct mode_adjust *adj_ibit;
107 static struct mode_adjust *adj_fbit;
109 /* Mode class operations. */
110 static enum mode_class
111 complex_class (enum mode_class c)
113 switch (c)
115 case MODE_INT: return MODE_COMPLEX_INT;
116 case MODE_FLOAT: return MODE_COMPLEX_FLOAT;
117 default:
118 error ("no complex class for class %s", mode_class_names[c]);
119 return MODE_RANDOM;
123 static enum mode_class
124 vector_class (enum mode_class cl)
126 switch (cl)
128 case MODE_INT: return MODE_VECTOR_INT;
129 case MODE_FLOAT: return MODE_VECTOR_FLOAT;
130 case MODE_FRACT: return MODE_VECTOR_FRACT;
131 case MODE_UFRACT: return MODE_VECTOR_UFRACT;
132 case MODE_ACCUM: return MODE_VECTOR_ACCUM;
133 case MODE_UACCUM: return MODE_VECTOR_UACCUM;
134 default:
135 error ("no vector class for class %s", mode_class_names[cl]);
136 return MODE_RANDOM;
140 /* Utility routines. */
141 static inline struct mode_data *
142 find_mode (const char *name)
144 struct mode_data key;
146 key.name = name;
147 return (struct mode_data *) htab_find (modes_by_name, &key);
150 static struct mode_data *
151 new_mode (enum mode_class cl, const char *name,
152 const char *file, unsigned int line)
154 struct mode_data *m;
155 static unsigned int count = 0;
157 m = find_mode (name);
158 if (m)
160 error ("%s:%d: duplicate definition of mode \"%s\"",
161 trim_filename (file), line, name);
162 error ("%s:%d: previous definition here", m->file, m->line);
163 return m;
166 m = XNEW (struct mode_data);
167 memcpy (m, &blank_mode, sizeof (struct mode_data));
168 m->cl = cl;
169 m->name = name;
170 if (file)
171 m->file = trim_filename (file);
172 m->line = line;
173 m->counter = count++;
175 m->next = modes[cl];
176 modes[cl] = m;
177 n_modes[cl]++;
179 *htab_find_slot (modes_by_name, m, INSERT) = m;
181 return m;
184 static hashval_t
185 hash_mode (const void *p)
187 const struct mode_data *m = (const struct mode_data *)p;
188 return htab_hash_string (m->name);
191 static int
192 eq_mode (const void *p, const void *q)
194 const struct mode_data *a = (const struct mode_data *)p;
195 const struct mode_data *b = (const struct mode_data *)q;
197 return !strcmp (a->name, b->name);
200 #define for_all_modes(C, M) \
201 for (C = 0; C < MAX_MODE_CLASS; C++) \
202 for (M = modes[C]; M; M = M->next)
204 static void ATTRIBUTE_UNUSED
205 new_adjust (const char *name,
206 struct mode_adjust **category, const char *catname,
207 const char *adjustment,
208 enum mode_class required_class_from,
209 enum mode_class required_class_to,
210 const char *file, unsigned int line)
212 struct mode_data *mode = find_mode (name);
213 struct mode_adjust *a;
215 file = trim_filename (file);
217 if (!mode)
219 error ("%s:%d: no mode \"%s\"", file, line, name);
220 return;
223 if (required_class_from != MODE_RANDOM
224 && (mode->cl < required_class_from || mode->cl > required_class_to))
226 error ("%s:%d: mode \"%s\" is not among class {%s, %s}",
227 file, line, name, mode_class_names[required_class_from] + 5,
228 mode_class_names[required_class_to] + 5);
229 return;
232 for (a = *category; a; a = a->next)
233 if (a->mode == mode)
235 error ("%s:%d: mode \"%s\" already has a %s adjustment",
236 file, line, name, catname);
237 error ("%s:%d: previous adjustment here", a->file, a->line);
238 return;
241 a = XNEW (struct mode_adjust);
242 a->mode = mode;
243 a->adjustment = adjustment;
244 a->file = file;
245 a->line = line;
247 a->next = *category;
248 *category = a;
251 /* Diagnose failure to meet expectations in a partially filled out
252 mode structure. */
253 enum requirement { SET, UNSET, OPTIONAL };
255 #define validate_field_(mname, fname, req, val, unset, file, line) do { \
256 switch (req) \
258 case SET: \
259 if (val == unset) \
260 error ("%s:%d: (%s) field %s must be set", \
261 file, line, mname, fname); \
262 break; \
263 case UNSET: \
264 if (val != unset) \
265 error ("%s:%d: (%s) field %s must not be set", \
266 file, line, mname, fname); \
267 case OPTIONAL: \
268 break; \
270 } while (0)
272 #define validate_field(M, F) \
273 validate_field_(M->name, #F, r_##F, M->F, blank_mode.F, M->file, M->line)
275 static void
276 validate_mode (struct mode_data *m,
277 enum requirement r_precision,
278 enum requirement r_bytesize,
279 enum requirement r_component,
280 enum requirement r_ncomponents,
281 enum requirement r_format)
283 validate_field (m, precision);
284 validate_field (m, bytesize);
285 validate_field (m, component);
286 validate_field (m, ncomponents);
287 validate_field (m, format);
289 #undef validate_field
290 #undef validate_field_
292 /* Given a partially-filled-out mode structure, figure out what we can
293 and fill the rest of it in; die if it isn't enough. */
294 static void
295 complete_mode (struct mode_data *m)
297 unsigned int alignment;
299 if (!m->name)
301 error ("%s:%d: mode with no name", m->file, m->line);
302 return;
304 if (m->cl == MAX_MODE_CLASS)
306 error ("%s:%d: %smode has no mode class", m->file, m->line, m->name);
307 return;
310 switch (m->cl)
312 case MODE_RANDOM:
313 /* Nothing more need be said. */
314 if (!strcmp (m->name, "VOID"))
315 void_mode = m;
317 validate_mode (m, UNSET, UNSET, UNSET, UNSET, UNSET);
319 m->precision = 0;
320 m->bytesize = 0;
321 m->ncomponents = 0;
322 m->component = 0;
323 break;
325 case MODE_CC:
326 /* Again, nothing more need be said. For historical reasons,
327 the size of a CC mode is four units. */
328 validate_mode (m, UNSET, UNSET, UNSET, UNSET, UNSET);
330 m->bytesize = 4;
331 m->ncomponents = 1;
332 m->component = 0;
333 break;
335 case MODE_INT:
336 case MODE_FLOAT:
337 case MODE_DECIMAL_FLOAT:
338 case MODE_FRACT:
339 case MODE_UFRACT:
340 case MODE_ACCUM:
341 case MODE_UACCUM:
342 /* A scalar mode must have a byte size, may have a bit size,
343 and must not have components. A float mode must have a
344 format. */
345 validate_mode (m, OPTIONAL, SET, UNSET, UNSET,
346 (m->cl == MODE_FLOAT || m->cl == MODE_DECIMAL_FLOAT)
347 ? SET : UNSET);
349 m->ncomponents = 1;
350 m->component = 0;
351 break;
353 case MODE_PARTIAL_INT:
354 /* A partial integer mode uses ->component to say what the
355 corresponding full-size integer mode is, and may also
356 specify a bit size. */
357 validate_mode (m, OPTIONAL, UNSET, SET, UNSET, UNSET);
359 m->bytesize = m->component->bytesize;
361 m->ncomponents = 1;
362 break;
364 case MODE_COMPLEX_INT:
365 case MODE_COMPLEX_FLOAT:
366 /* Complex modes should have a component indicated, but no more. */
367 validate_mode (m, UNSET, UNSET, SET, UNSET, UNSET);
368 m->ncomponents = 2;
369 if (m->component->precision != (unsigned int)-1)
370 m->precision = 2 * m->component->precision;
371 m->bytesize = 2 * m->component->bytesize;
372 break;
374 case MODE_VECTOR_INT:
375 case MODE_VECTOR_FLOAT:
376 case MODE_VECTOR_FRACT:
377 case MODE_VECTOR_UFRACT:
378 case MODE_VECTOR_ACCUM:
379 case MODE_VECTOR_UACCUM:
380 /* Vector modes should have a component and a number of components. */
381 validate_mode (m, UNSET, UNSET, SET, SET, UNSET);
382 if (m->component->precision != (unsigned int)-1)
383 m->precision = m->ncomponents * m->component->precision;
384 m->bytesize = m->ncomponents * m->component->bytesize;
385 break;
387 default:
388 gcc_unreachable ();
391 /* If not already specified, the mode alignment defaults to the largest
392 power of two that divides the size of the object. Complex types are
393 not more aligned than their contents. */
394 if (m->cl == MODE_COMPLEX_INT || m->cl == MODE_COMPLEX_FLOAT)
395 alignment = m->component->bytesize;
396 else
397 alignment = m->bytesize;
399 m->alignment = alignment & (~alignment + 1);
401 /* If this mode has components, make the component mode point back
402 to this mode, for the sake of adjustments. */
403 if (m->component)
405 m->next_cont = m->component->contained;
406 m->component->contained = m;
410 static void
411 complete_all_modes (void)
413 struct mode_data *m;
414 int cl;
416 for_all_modes (cl, m)
417 complete_mode (m);
420 /* For each mode in class CLASS, construct a corresponding complex mode. */
421 #define COMPLEX_MODES(C) make_complex_modes (MODE_##C, __FILE__, __LINE__)
422 static void
423 make_complex_modes (enum mode_class cl,
424 const char *file, unsigned int line)
426 struct mode_data *m;
427 struct mode_data *c;
428 enum mode_class cclass = complex_class (cl);
430 if (cclass == MODE_RANDOM)
431 return;
433 for (m = modes[cl]; m; m = m->next)
435 char *p, *buf;
436 size_t m_len;
438 /* Skip BImode. FIXME: BImode probably shouldn't be MODE_INT. */
439 if (m->precision == 1)
440 continue;
442 m_len = strlen (m->name);
443 /* The leading "1 +" is in case we prepend a "C" below. */
444 buf = (char *) xmalloc (1 + m_len + 1);
446 /* Float complex modes are named SCmode, etc.
447 Int complex modes are named CSImode, etc.
448 This inconsistency should be eliminated. */
449 p = 0;
450 if (cl == MODE_FLOAT)
452 memcpy (buf, m->name, m_len + 1);
453 p = strchr (buf, 'F');
454 if (p == 0 && strchr (buf, 'D') == 0)
456 error ("%s:%d: float mode \"%s\" has no 'F' or 'D'",
457 m->file, m->line, m->name);
458 free (buf);
459 continue;
462 if (p != 0)
463 *p = 'C';
464 else
466 buf[0] = 'C';
467 memcpy (buf + 1, m->name, m_len + 1);
470 c = new_mode (cclass, buf, file, line);
471 c->component = m;
475 /* For all modes in class CL, construct vector modes of width
476 WIDTH, having as many components as necessary. */
477 #define VECTOR_MODES(C, W) make_vector_modes (MODE_##C, W, __FILE__, __LINE__)
478 static void ATTRIBUTE_UNUSED
479 make_vector_modes (enum mode_class cl, unsigned int width,
480 const char *file, unsigned int line)
482 struct mode_data *m;
483 struct mode_data *v;
484 char buf[8];
485 unsigned int ncomponents;
486 enum mode_class vclass = vector_class (cl);
488 if (vclass == MODE_RANDOM)
489 return;
491 for (m = modes[cl]; m; m = m->next)
493 /* Do not construct vector modes with only one element, or
494 vector modes where the element size doesn't divide the full
495 size evenly. */
496 ncomponents = width / m->bytesize;
497 if (ncomponents < 2)
498 continue;
499 if (width % m->bytesize)
500 continue;
502 /* Skip QFmode and BImode. FIXME: this special case should
503 not be necessary. */
504 if (cl == MODE_FLOAT && m->bytesize == 1)
505 continue;
506 if (cl == MODE_INT && m->precision == 1)
507 continue;
509 if ((size_t)snprintf (buf, sizeof buf, "V%u%s", ncomponents, m->name)
510 >= sizeof buf)
512 error ("%s:%d: mode name \"%s\" is too long",
513 m->file, m->line, m->name);
514 continue;
517 v = new_mode (vclass, xstrdup (buf), file, line);
518 v->component = m;
519 v->ncomponents = ncomponents;
523 /* Input. */
525 #define _SPECIAL_MODE(C, N) \
526 make_special_mode (MODE_##C, #N, __FILE__, __LINE__)
527 #define RANDOM_MODE(N) _SPECIAL_MODE (RANDOM, N)
528 #define CC_MODE(N) _SPECIAL_MODE (CC, N)
530 static void
531 make_special_mode (enum mode_class cl, const char *name,
532 const char *file, unsigned int line)
534 new_mode (cl, name, file, line);
537 #define INT_MODE(N, Y) FRACTIONAL_INT_MODE (N, -1U, Y)
538 #define FRACTIONAL_INT_MODE(N, B, Y) \
539 make_int_mode (#N, B, Y, __FILE__, __LINE__)
541 static void
542 make_int_mode (const char *name,
543 unsigned int precision, unsigned int bytesize,
544 const char *file, unsigned int line)
546 struct mode_data *m = new_mode (MODE_INT, name, file, line);
547 m->bytesize = bytesize;
548 m->precision = precision;
551 #define FRACT_MODE(N, Y, F) \
552 make_fixed_point_mode (MODE_FRACT, #N, Y, 0, F, __FILE__, __LINE__)
554 #define UFRACT_MODE(N, Y, F) \
555 make_fixed_point_mode (MODE_UFRACT, #N, Y, 0, F, __FILE__, __LINE__)
557 #define ACCUM_MODE(N, Y, I, F) \
558 make_fixed_point_mode (MODE_ACCUM, #N, Y, I, F, __FILE__, __LINE__)
560 #define UACCUM_MODE(N, Y, I, F) \
561 make_fixed_point_mode (MODE_UACCUM, #N, Y, I, F, __FILE__, __LINE__)
563 /* Create a fixed-point mode by setting CL, NAME, BYTESIZE, IBIT, FBIT,
564 FILE, and LINE. */
566 static void
567 make_fixed_point_mode (enum mode_class cl,
568 const char *name,
569 unsigned int bytesize,
570 unsigned int ibit,
571 unsigned int fbit,
572 const char *file, unsigned int line)
574 struct mode_data *m = new_mode (cl, name, file, line);
575 m->bytesize = bytesize;
576 m->ibit = ibit;
577 m->fbit = fbit;
580 #define FLOAT_MODE(N, Y, F) FRACTIONAL_FLOAT_MODE (N, -1U, Y, F)
581 #define FRACTIONAL_FLOAT_MODE(N, B, Y, F) \
582 make_float_mode (#N, B, Y, #F, __FILE__, __LINE__)
584 static void
585 make_float_mode (const char *name,
586 unsigned int precision, unsigned int bytesize,
587 const char *format,
588 const char *file, unsigned int line)
590 struct mode_data *m = new_mode (MODE_FLOAT, name, file, line);
591 m->bytesize = bytesize;
592 m->precision = precision;
593 m->format = format;
596 #define DECIMAL_FLOAT_MODE(N, Y, F) \
597 FRACTIONAL_DECIMAL_FLOAT_MODE (N, -1U, Y, F)
598 #define FRACTIONAL_DECIMAL_FLOAT_MODE(N, B, Y, F) \
599 make_decimal_float_mode (#N, B, Y, #F, __FILE__, __LINE__)
601 static void
602 make_decimal_float_mode (const char *name,
603 unsigned int precision, unsigned int bytesize,
604 const char *format,
605 const char *file, unsigned int line)
607 struct mode_data *m = new_mode (MODE_DECIMAL_FLOAT, name, file, line);
608 m->bytesize = bytesize;
609 m->precision = precision;
610 m->format = format;
613 #define RESET_FLOAT_FORMAT(N, F) \
614 reset_float_format (#N, #F, __FILE__, __LINE__)
615 static void ATTRIBUTE_UNUSED
616 reset_float_format (const char *name, const char *format,
617 const char *file, unsigned int line)
619 struct mode_data *m = find_mode (name);
620 if (!m)
622 error ("%s:%d: no mode \"%s\"", file, line, name);
623 return;
625 if (m->cl != MODE_FLOAT && m->cl != MODE_DECIMAL_FLOAT)
627 error ("%s:%d: mode \"%s\" is not a FLOAT class", file, line, name);
628 return;
630 m->format = format;
633 /* Partial integer modes are specified by relation to a full integer
634 mode. */
635 #define PARTIAL_INT_MODE(M,PREC,NAME) \
636 make_partial_integer_mode (#M, #NAME, PREC, __FILE__, __LINE__)
637 static void ATTRIBUTE_UNUSED
638 make_partial_integer_mode (const char *base, const char *name,
639 unsigned int precision,
640 const char *file, unsigned int line)
642 struct mode_data *m;
643 struct mode_data *component = find_mode (base);
644 if (!component)
646 error ("%s:%d: no mode \"%s\"", file, line, name);
647 return;
649 if (component->cl != MODE_INT)
651 error ("%s:%d: mode \"%s\" is not class INT", file, line, name);
652 return;
655 m = new_mode (MODE_PARTIAL_INT, name, file, line);
656 m->precision = precision;
657 m->component = component;
660 /* A single vector mode can be specified by naming its component
661 mode and the number of components. */
662 #define VECTOR_MODE(C, M, N) \
663 make_vector_mode (MODE_##C, #M, N, __FILE__, __LINE__);
664 static void ATTRIBUTE_UNUSED
665 make_vector_mode (enum mode_class bclass,
666 const char *base,
667 unsigned int ncomponents,
668 const char *file, unsigned int line)
670 struct mode_data *v;
671 enum mode_class vclass = vector_class (bclass);
672 struct mode_data *component = find_mode (base);
673 char namebuf[16];
675 if (vclass == MODE_RANDOM)
676 return;
677 if (component == 0)
679 error ("%s:%d: no mode \"%s\"", file, line, base);
680 return;
682 if (component->cl != bclass
683 && (component->cl != MODE_PARTIAL_INT
684 || bclass != MODE_INT))
686 error ("%s:%d: mode \"%s\" is not class %s",
687 file, line, base, mode_class_names[bclass] + 5);
688 return;
691 if ((size_t)snprintf (namebuf, sizeof namebuf, "V%u%s",
692 ncomponents, base) >= sizeof namebuf)
694 error ("%s:%d: mode name \"%s\" is too long",
695 file, line, base);
696 return;
699 v = new_mode (vclass, xstrdup (namebuf), file, line);
700 v->ncomponents = ncomponents;
701 v->component = component;
704 /* Adjustability. */
705 #define _ADD_ADJUST(A, M, X, C1, C2) \
706 new_adjust (#M, &adj_##A, #A, #X, MODE_##C1, MODE_##C2, __FILE__, __LINE__)
708 #define ADJUST_BYTESIZE(M, X) _ADD_ADJUST (bytesize, M, X, RANDOM, RANDOM)
709 #define ADJUST_ALIGNMENT(M, X) _ADD_ADJUST (alignment, M, X, RANDOM, RANDOM)
710 #define ADJUST_FLOAT_FORMAT(M, X) _ADD_ADJUST (format, M, X, FLOAT, FLOAT)
711 #define ADJUST_IBIT(M, X) _ADD_ADJUST (ibit, M, X, ACCUM, UACCUM)
712 #define ADJUST_FBIT(M, X) _ADD_ADJUST (fbit, M, X, FRACT, UACCUM)
714 static int bits_per_unit;
715 static int max_bitsize_mode_any_int;
717 static void
718 create_modes (void)
720 #include "machmode.def"
722 /* So put the default value unless the target needs a non standard
723 value. */
724 #ifdef BITS_PER_UNIT
725 bits_per_unit = BITS_PER_UNIT;
726 #else
727 bits_per_unit = 8;
728 #endif
730 #ifdef MAX_BITSIZE_MODE_ANY_INT
731 max_bitsize_mode_any_int = MAX_BITSIZE_MODE_ANY_INT;
732 #else
733 max_bitsize_mode_any_int = 0;
734 #endif
737 /* Processing. */
739 /* Sort a list of modes into the order needed for the WIDER field:
740 major sort by precision, minor sort by component precision.
742 For instance:
743 QI < HI < SI < DI < TI
744 V4QI < V2HI < V8QI < V4HI < V2SI.
746 If the precision is not set, sort by the bytesize. A mode with
747 precision set gets sorted before a mode without precision set, if
748 they have the same bytesize; this is the right thing because
749 the precision must always be smaller than the bytesize * BITS_PER_UNIT.
750 We don't have to do anything special to get this done -- an unset
751 precision shows up as (unsigned int)-1, i.e. UINT_MAX. */
752 static int
753 cmp_modes (const void *a, const void *b)
755 const struct mode_data *const m = *(const struct mode_data *const*)a;
756 const struct mode_data *const n = *(const struct mode_data *const*)b;
758 if (m->bytesize > n->bytesize)
759 return 1;
760 else if (m->bytesize < n->bytesize)
761 return -1;
763 if (m->precision > n->precision)
764 return 1;
765 else if (m->precision < n->precision)
766 return -1;
768 if (!m->component && !n->component)
770 if (m->counter < n->counter)
771 return -1;
772 else
773 return 1;
776 if (m->component->bytesize > n->component->bytesize)
777 return 1;
778 else if (m->component->bytesize < n->component->bytesize)
779 return -1;
781 if (m->component->precision > n->component->precision)
782 return 1;
783 else if (m->component->precision < n->component->precision)
784 return -1;
786 if (m->counter < n->counter)
787 return -1;
788 else
789 return 1;
792 static void
793 calc_wider_mode (void)
795 int c;
796 struct mode_data *m;
797 struct mode_data **sortbuf;
798 unsigned int max_n_modes = 0;
799 unsigned int i, j;
801 for (c = 0; c < MAX_MODE_CLASS; c++)
802 max_n_modes = MAX (max_n_modes, n_modes[c]);
804 /* Allocate max_n_modes + 1 entries to leave room for the extra null
805 pointer assigned after the qsort call below. */
806 sortbuf = XALLOCAVEC (struct mode_data *, max_n_modes + 1);
808 for (c = 0; c < MAX_MODE_CLASS; c++)
810 /* "wider" is not meaningful for MODE_RANDOM and MODE_CC.
811 However, we want these in textual order, and we have
812 precisely the reverse. */
813 if (c == MODE_RANDOM || c == MODE_CC)
815 struct mode_data *prev, *next;
817 for (prev = 0, m = modes[c]; m; m = next)
819 m->wider = void_mode;
821 /* this is nreverse */
822 next = m->next;
823 m->next = prev;
824 prev = m;
826 modes[c] = prev;
828 else
830 if (!modes[c])
831 continue;
833 for (i = 0, m = modes[c]; m; i++, m = m->next)
834 sortbuf[i] = m;
836 qsort (sortbuf, i, sizeof (struct mode_data *), cmp_modes);
838 sortbuf[i] = 0;
839 for (j = 0; j < i; j++)
841 sortbuf[j]->next = sortbuf[j + 1];
842 if (c == MODE_PARTIAL_INT)
843 sortbuf[j]->wider = sortbuf[j]->component;
844 else
845 sortbuf[j]->wider = sortbuf[j]->next;
848 modes[c] = sortbuf[0];
853 /* Output routines. */
855 #define tagged_printf(FMT, ARG, TAG) do { \
856 int count_ = printf (" " FMT ",", ARG); \
857 printf ("%*s/* %s */\n", 27 - count_, "", TAG); \
858 } while (0)
860 #define print_decl(TYPE, NAME, ASIZE) \
861 puts ("\nconst " TYPE " " NAME "[" ASIZE "] =\n{");
863 #define print_maybe_const_decl(TYPE, NAME, ASIZE, CATEGORY) \
864 printf ("\n" TYPE " " NAME "[" ASIZE "] = \n{\n", \
865 adj_##CATEGORY ? "" : "const ")
867 #define print_closer() puts ("};")
869 /* Compute the max bitsize of some of the classes of integers. It may
870 be that there are needs for the other integer classes, and this
871 code is easy to extend. */
872 static void
873 emit_max_int (void)
875 unsigned int max, mmax;
876 struct mode_data *i;
877 int j;
879 puts ("");
881 printf ("#define BITS_PER_UNIT (%d)\n", bits_per_unit);
883 if (max_bitsize_mode_any_int == 0)
885 for (max = 1, i = modes[MODE_INT]; i; i = i->next)
886 if (max < i->bytesize)
887 max = i->bytesize;
888 mmax = max;
889 for (max = 1, i = modes[MODE_PARTIAL_INT]; i; i = i->next)
890 if (max < i->bytesize)
891 max = i->bytesize;
892 if (max > mmax)
893 mmax = max;
894 printf ("#define MAX_BITSIZE_MODE_ANY_INT (%d*BITS_PER_UNIT)\n", mmax);
896 else
897 printf ("#define MAX_BITSIZE_MODE_ANY_INT %d\n", max_bitsize_mode_any_int);
899 mmax = 0;
900 for (j = 0; j < MAX_MODE_CLASS; j++)
901 for (i = modes[j]; i; i = i->next)
902 if (mmax < i->bytesize)
903 mmax = i->bytesize;
904 printf ("#define MAX_BITSIZE_MODE_ANY_MODE (%d*BITS_PER_UNIT)\n", mmax);
907 static void
908 emit_insn_modes_h (void)
910 int c;
911 struct mode_data *m, *first, *last;
913 printf ("/* Generated automatically from machmode.def%s%s\n",
914 HAVE_EXTRA_MODES ? " and " : "",
915 EXTRA_MODES_FILE);
917 puts ("\
918 by genmodes. */\n\
920 #ifndef GCC_INSN_MODES_H\n\
921 #define GCC_INSN_MODES_H\n\
923 enum machine_mode\n{");
925 for (c = 0; c < MAX_MODE_CLASS; c++)
926 for (m = modes[c]; m; m = m->next)
928 int count_ = printf (" %smode,", m->name);
929 printf ("%*s/* %s:%d */\n", 27 - count_, "",
930 trim_filename (m->file), m->line);
933 puts (" MAX_MACHINE_MODE,\n");
935 for (c = 0; c < MAX_MODE_CLASS; c++)
937 first = modes[c];
938 last = 0;
939 for (m = first; m; last = m, m = m->next)
942 /* Don't use BImode for MIN_MODE_INT, since otherwise the middle
943 end will try to use it for bitfields in structures and the
944 like, which we do not want. Only the target md file should
945 generate BImode widgets. */
946 if (first && first->precision == 1 && c == MODE_INT)
947 first = first->next;
949 if (first && last)
950 printf (" MIN_%s = %smode,\n MAX_%s = %smode,\n\n",
951 mode_class_names[c], first->name,
952 mode_class_names[c], last->name);
953 else
954 printf (" MIN_%s = %smode,\n MAX_%s = %smode,\n\n",
955 mode_class_names[c], void_mode->name,
956 mode_class_names[c], void_mode->name);
959 puts ("\
960 NUM_MACHINE_MODES = MAX_MACHINE_MODE\n\
961 };\n");
963 /* I can't think of a better idea, can you? */
964 printf ("#define CONST_MODE_SIZE%s\n", adj_bytesize ? "" : " const");
965 printf ("#define CONST_MODE_BASE_ALIGN%s\n", adj_alignment ? "" : " const");
966 #if 0 /* disabled for backward compatibility, temporary */
967 printf ("#define CONST_REAL_FORMAT_FOR_MODE%s\n", adj_format ? "" :" const");
968 #endif
969 printf ("#define CONST_MODE_IBIT%s\n", adj_ibit ? "" : " const");
970 printf ("#define CONST_MODE_FBIT%s\n", adj_fbit ? "" : " const");
971 emit_max_int ();
972 puts ("\
974 #endif /* insn-modes.h */");
977 static void
978 emit_insn_modes_c_header (void)
980 printf ("/* Generated automatically from machmode.def%s%s\n",
981 HAVE_EXTRA_MODES ? " and " : "",
982 EXTRA_MODES_FILE);
984 puts ("\
985 by genmodes. */\n\
987 #include \"config.h\"\n\
988 #include \"system.h\"\n\
989 #include \"coretypes.h\"\n\
990 #include \"tm.h\"\n\
991 #include \"machmode.h\"\n\
992 #include \"real.h\"");
995 static void
996 emit_min_insn_modes_c_header (void)
998 printf ("/* Generated automatically from machmode.def%s%s\n",
999 HAVE_EXTRA_MODES ? " and " : "",
1000 EXTRA_MODES_FILE);
1002 puts ("\
1003 by genmodes. */\n\
1005 #include \"bconfig.h\"\n\
1006 #include \"system.h\"\n\
1007 #include \"machmode.h\"");
1010 static void
1011 emit_mode_name (void)
1013 int c;
1014 struct mode_data *m;
1016 print_decl ("char *const", "mode_name", "NUM_MACHINE_MODES");
1018 for_all_modes (c, m)
1019 printf (" \"%s\",\n", m->name);
1021 print_closer ();
1024 static void
1025 emit_mode_class (void)
1027 int c;
1028 struct mode_data *m;
1030 print_decl ("unsigned char", "mode_class", "NUM_MACHINE_MODES");
1032 for_all_modes (c, m)
1033 tagged_printf ("%s", mode_class_names[m->cl], m->name);
1035 print_closer ();
1038 static void
1039 emit_mode_precision (void)
1041 int c;
1042 struct mode_data *m;
1044 print_decl ("unsigned short", "mode_precision", "NUM_MACHINE_MODES");
1046 for_all_modes (c, m)
1047 if (m->precision != (unsigned int)-1)
1048 tagged_printf ("%u", m->precision, m->name);
1049 else
1050 tagged_printf ("%u*BITS_PER_UNIT", m->bytesize, m->name);
1052 print_closer ();
1055 static void
1056 emit_mode_size (void)
1058 int c;
1059 struct mode_data *m;
1061 print_maybe_const_decl ("%sunsigned char", "mode_size",
1062 "NUM_MACHINE_MODES", bytesize);
1064 for_all_modes (c, m)
1065 tagged_printf ("%u", m->bytesize, m->name);
1067 print_closer ();
1070 static void
1071 emit_mode_nunits (void)
1073 int c;
1074 struct mode_data *m;
1076 print_decl ("unsigned char", "mode_nunits", "NUM_MACHINE_MODES");
1078 for_all_modes (c, m)
1079 tagged_printf ("%u", m->ncomponents, m->name);
1081 print_closer ();
1084 static void
1085 emit_mode_wider (void)
1087 int c;
1088 struct mode_data *m;
1090 print_decl ("unsigned char", "mode_wider", "NUM_MACHINE_MODES");
1092 for_all_modes (c, m)
1093 tagged_printf ("%smode",
1094 m->wider ? m->wider->name : void_mode->name,
1095 m->name);
1097 print_closer ();
1098 print_decl ("unsigned char", "mode_2xwider", "NUM_MACHINE_MODES");
1100 for_all_modes (c, m)
1102 struct mode_data * m2;
1104 for (m2 = m;
1105 m2 && m2 != void_mode;
1106 m2 = m2->wider)
1108 if (m2->bytesize < 2 * m->bytesize)
1109 continue;
1110 if (m->precision != (unsigned int) -1)
1112 if (m2->precision != 2 * m->precision)
1113 continue;
1115 else
1117 if (m2->precision != (unsigned int) -1)
1118 continue;
1121 /* For vectors we want twice the number of components,
1122 with the same element type. */
1123 if (m->cl == MODE_VECTOR_INT
1124 || m->cl == MODE_VECTOR_FLOAT
1125 || m->cl == MODE_VECTOR_FRACT
1126 || m->cl == MODE_VECTOR_UFRACT
1127 || m->cl == MODE_VECTOR_ACCUM
1128 || m->cl == MODE_VECTOR_UACCUM)
1130 if (m2->ncomponents != 2 * m->ncomponents)
1131 continue;
1132 if (m->component != m2->component)
1133 continue;
1136 break;
1138 if (m2 == void_mode)
1139 m2 = 0;
1140 tagged_printf ("%smode",
1141 m2 ? m2->name : void_mode->name,
1142 m->name);
1145 print_closer ();
1148 static void
1149 emit_mode_mask (void)
1151 int c;
1152 struct mode_data *m;
1154 print_decl ("unsigned HOST_WIDE_INT", "mode_mask_array",
1155 "NUM_MACHINE_MODES");
1156 puts ("\
1157 #define MODE_MASK(m) \\\n\
1158 ((m) >= HOST_BITS_PER_WIDE_INT) \\\n\
1159 ? ~(unsigned HOST_WIDE_INT) 0 \\\n\
1160 : ((unsigned HOST_WIDE_INT) 1 << (m)) - 1\n");
1162 for_all_modes (c, m)
1163 if (m->precision != (unsigned int)-1)
1164 tagged_printf ("MODE_MASK (%u)", m->precision, m->name);
1165 else
1166 tagged_printf ("MODE_MASK (%u*BITS_PER_UNIT)", m->bytesize, m->name);
1168 puts ("#undef MODE_MASK");
1169 print_closer ();
1172 static void
1173 emit_mode_inner (void)
1175 int c;
1176 struct mode_data *m;
1178 print_decl ("unsigned char", "mode_inner", "NUM_MACHINE_MODES");
1180 for_all_modes (c, m)
1181 tagged_printf ("%smode",
1182 c != MODE_PARTIAL_INT && m->component
1183 ? m->component->name : void_mode->name,
1184 m->name);
1186 print_closer ();
1189 static void
1190 emit_mode_base_align (void)
1192 int c;
1193 struct mode_data *m;
1195 print_maybe_const_decl ("%sunsigned char",
1196 "mode_base_align", "NUM_MACHINE_MODES",
1197 alignment);
1199 for_all_modes (c, m)
1200 tagged_printf ("%u", m->alignment, m->name);
1202 print_closer ();
1205 static void
1206 emit_class_narrowest_mode (void)
1208 int c;
1210 print_decl ("unsigned char", "class_narrowest_mode", "MAX_MODE_CLASS");
1212 for (c = 0; c < MAX_MODE_CLASS; c++)
1213 /* Bleah, all this to get the comment right for MIN_MODE_INT. */
1214 tagged_printf ("MIN_%s", mode_class_names[c],
1215 modes[c]
1216 ? ((c != MODE_INT || modes[c]->precision != 1)
1217 ? modes[c]->name
1218 : (modes[c]->next
1219 ? modes[c]->next->name
1220 : void_mode->name))
1221 : void_mode->name);
1223 print_closer ();
1226 static void
1227 emit_real_format_for_mode (void)
1229 struct mode_data *m;
1231 /* The entities pointed to by this table are constant, whether
1232 or not the table itself is constant.
1234 For backward compatibility this table is always writable
1235 (several targets modify it in TARGET_OPTION_OVERRIDE). FIXME:
1236 convert all said targets to use ADJUST_FORMAT instead. */
1237 #if 0
1238 print_maybe_const_decl ("const struct real_format *%s",
1239 "real_format_for_mode",
1240 "MAX_MODE_FLOAT - MIN_MODE_FLOAT + 1",
1241 format);
1242 #else
1243 print_decl ("struct real_format *\n", "real_format_for_mode",
1244 "MAX_MODE_FLOAT - MIN_MODE_FLOAT + 1 "
1245 "+ MAX_MODE_DECIMAL_FLOAT - MIN_MODE_DECIMAL_FLOAT + 1");
1246 #endif
1248 /* The beginning of the table is entries for float modes. */
1249 for (m = modes[MODE_FLOAT]; m; m = m->next)
1250 if (!strcmp (m->format, "0"))
1251 tagged_printf ("%s", m->format, m->name);
1252 else
1253 tagged_printf ("&%s", m->format, m->name);
1255 /* The end of the table is entries for decimal float modes. */
1256 for (m = modes[MODE_DECIMAL_FLOAT]; m; m = m->next)
1257 if (!strcmp (m->format, "0"))
1258 tagged_printf ("%s", m->format, m->name);
1259 else
1260 tagged_printf ("&%s", m->format, m->name);
1262 print_closer ();
1265 static void
1266 emit_mode_adjustments (void)
1268 struct mode_adjust *a;
1269 struct mode_data *m;
1271 puts ("\
1272 \nvoid\
1273 \ninit_adjust_machine_modes (void)\
1274 \n{\
1275 \n size_t s ATTRIBUTE_UNUSED;");
1277 /* Size adjustments must be propagated to all containing modes.
1278 A size adjustment forces us to recalculate the alignment too. */
1279 for (a = adj_bytesize; a; a = a->next)
1281 printf ("\n /* %s:%d */\n s = %s;\n",
1282 a->file, a->line, a->adjustment);
1283 printf (" mode_size[%smode] = s;\n", a->mode->name);
1284 printf (" mode_base_align[%smode] = s & (~s + 1);\n",
1285 a->mode->name);
1287 for (m = a->mode->contained; m; m = m->next_cont)
1289 switch (m->cl)
1291 case MODE_COMPLEX_INT:
1292 case MODE_COMPLEX_FLOAT:
1293 printf (" mode_size[%smode] = 2*s;\n", m->name);
1294 printf (" mode_base_align[%smode] = s & (~s + 1);\n",
1295 m->name);
1296 break;
1298 case MODE_VECTOR_INT:
1299 case MODE_VECTOR_FLOAT:
1300 case MODE_VECTOR_FRACT:
1301 case MODE_VECTOR_UFRACT:
1302 case MODE_VECTOR_ACCUM:
1303 case MODE_VECTOR_UACCUM:
1304 printf (" mode_size[%smode] = %d*s;\n",
1305 m->name, m->ncomponents);
1306 printf (" mode_base_align[%smode] = (%d*s) & (~(%d*s)+1);\n",
1307 m->name, m->ncomponents, m->ncomponents);
1308 break;
1310 default:
1311 internal_error (
1312 "mode %s is neither vector nor complex but contains %s",
1313 m->name, a->mode->name);
1314 /* NOTREACHED */
1319 /* Alignment adjustments propagate too.
1320 ??? This may not be the right thing for vector modes. */
1321 for (a = adj_alignment; a; a = a->next)
1323 printf ("\n /* %s:%d */\n s = %s;\n",
1324 a->file, a->line, a->adjustment);
1325 printf (" mode_base_align[%smode] = s;\n", a->mode->name);
1327 for (m = a->mode->contained; m; m = m->next_cont)
1329 switch (m->cl)
1331 case MODE_COMPLEX_INT:
1332 case MODE_COMPLEX_FLOAT:
1333 printf (" mode_base_align[%smode] = s;\n", m->name);
1334 break;
1336 case MODE_VECTOR_INT:
1337 case MODE_VECTOR_FLOAT:
1338 case MODE_VECTOR_FRACT:
1339 case MODE_VECTOR_UFRACT:
1340 case MODE_VECTOR_ACCUM:
1341 case MODE_VECTOR_UACCUM:
1342 printf (" mode_base_align[%smode] = %d*s;\n",
1343 m->name, m->ncomponents);
1344 break;
1346 default:
1347 internal_error (
1348 "mode %s is neither vector nor complex but contains %s",
1349 m->name, a->mode->name);
1350 /* NOTREACHED */
1355 /* Ibit adjustments don't have to propagate. */
1356 for (a = adj_ibit; a; a = a->next)
1358 printf ("\n /* %s:%d */\n s = %s;\n",
1359 a->file, a->line, a->adjustment);
1360 printf (" mode_ibit[%smode] = s;\n", a->mode->name);
1363 /* Fbit adjustments don't have to propagate. */
1364 for (a = adj_fbit; a; a = a->next)
1366 printf ("\n /* %s:%d */\n s = %s;\n",
1367 a->file, a->line, a->adjustment);
1368 printf (" mode_fbit[%smode] = s;\n", a->mode->name);
1371 /* Real mode formats don't have to propagate anywhere. */
1372 for (a = adj_format; a; a = a->next)
1373 printf ("\n /* %s:%d */\n REAL_MODE_FORMAT (%smode) = %s;\n",
1374 a->file, a->line, a->mode->name, a->adjustment);
1376 puts ("}");
1379 /* Emit ibit for all modes. */
1381 static void
1382 emit_mode_ibit (void)
1384 int c;
1385 struct mode_data *m;
1387 print_maybe_const_decl ("%sunsigned char",
1388 "mode_ibit", "NUM_MACHINE_MODES",
1389 ibit);
1391 for_all_modes (c, m)
1392 tagged_printf ("%u", m->ibit, m->name);
1394 print_closer ();
1397 /* Emit fbit for all modes. */
1399 static void
1400 emit_mode_fbit (void)
1402 int c;
1403 struct mode_data *m;
1405 print_maybe_const_decl ("%sunsigned char",
1406 "mode_fbit", "NUM_MACHINE_MODES",
1407 fbit);
1409 for_all_modes (c, m)
1410 tagged_printf ("%u", m->fbit, m->name);
1412 print_closer ();
1416 static void
1417 emit_insn_modes_c (void)
1419 emit_insn_modes_c_header ();
1420 emit_mode_name ();
1421 emit_mode_class ();
1422 emit_mode_precision ();
1423 emit_mode_size ();
1424 emit_mode_nunits ();
1425 emit_mode_wider ();
1426 emit_mode_mask ();
1427 emit_mode_inner ();
1428 emit_mode_base_align ();
1429 emit_class_narrowest_mode ();
1430 emit_real_format_for_mode ();
1431 emit_mode_adjustments ();
1432 emit_mode_ibit ();
1433 emit_mode_fbit ();
1436 static void
1437 emit_min_insn_modes_c (void)
1439 emit_min_insn_modes_c_header ();
1440 emit_mode_name ();
1441 emit_mode_class ();
1442 emit_mode_wider ();
1443 emit_class_narrowest_mode ();
1446 /* Master control. */
1448 main (int argc, char **argv)
1450 bool gen_header = false, gen_min = false;
1451 progname = argv[0];
1453 if (argc == 1)
1455 else if (argc == 2 && !strcmp (argv[1], "-h"))
1456 gen_header = true;
1457 else if (argc == 2 && !strcmp (argv[1], "-m"))
1458 gen_min = true;
1459 else
1461 error ("usage: %s [-h|-m] > file", progname);
1462 return FATAL_EXIT_CODE;
1465 modes_by_name = htab_create_alloc (64, hash_mode, eq_mode, 0, xcalloc, free);
1467 create_modes ();
1468 complete_all_modes ();
1470 if (have_error)
1471 return FATAL_EXIT_CODE;
1473 calc_wider_mode ();
1475 if (gen_header)
1476 emit_insn_modes_h ();
1477 else if (gen_min)
1478 emit_min_insn_modes_c ();
1479 else
1480 emit_insn_modes_c ();
1482 if (fflush (stdout) || fclose (stdout))
1483 return FATAL_EXIT_CODE;
1484 return SUCCESS_EXIT_CODE;