xref: /netbsd/external/gpl3/gcc/dist/gcc/genmodes.c (revision 5306d544)
1 /* Generate the machine mode enumeration and associated tables.
2    Copyright (C) 2003-2017 Free Software Foundation, Inc.
3 
4 This file is part of GCC.
5 
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.
10 
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.
15 
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/>.  */
19 
20 #include "bconfig.h"
21 #include "system.h"
22 #include "errors.h"
23 
24 /* enum mode_class is normally defined by machmode.h but we can't
25    include that header here.  */
26 #include "mode-classes.def"
27 
28 #define DEF_MODE_CLASS(M) M
29 enum mode_class { MODE_CLASSES, MAX_MODE_CLASS };
30 #undef DEF_MODE_CLASS
31 
32 /* Text names of mode classes, for output.  */
33 #define DEF_MODE_CLASS(M) #M
34 static const char *const mode_class_names[MAX_MODE_CLASS] =
35 {
36   MODE_CLASSES
37 };
38 #undef DEF_MODE_CLASS
39 #undef MODE_CLASSES
40 
41 #ifdef EXTRA_MODES_FILE
42 # define HAVE_EXTRA_MODES 1
43 #else
44 # define HAVE_EXTRA_MODES 0
45 # define EXTRA_MODES_FILE ""
46 #endif
47 
48 /* Data structure for building up what we know about a mode.
49    They're clustered by mode class.  */
50 struct mode_data
51 {
52   struct mode_data *next;	/* next this class - arbitrary order */
53 
54   const char *name;		/* printable mode name -- SI, not SImode */
55   enum mode_class cl;		/* this mode class */
56   unsigned int precision;	/* size in bits, equiv to TYPE_PRECISION */
57   unsigned int bytesize;	/* storage size in addressable units */
58   unsigned int ncomponents;	/* number of subunits */
59   unsigned int alignment;	/* mode alignment */
60   const char *format;		/* floating point format - float modes only */
61 
62   struct mode_data *component;	/* mode of components */
63   struct mode_data *wider;	/* next wider mode */
64 
65   struct mode_data *contained;  /* Pointer to list of modes that have
66 				   this mode as a component.  */
67   struct mode_data *next_cont;  /* Next mode in that list.  */
68 
69   struct mode_data *complex;	/* complex type with mode as component.  */
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 */
75   bool need_bytesize_adj;	/* true if this mode need dynamic size
76 				   adjustment */
77   unsigned int int_n;		/* If nonzero, then __int<INT_N> will be defined */
78 };
79 
80 static struct mode_data *modes[MAX_MODE_CLASS];
81 static unsigned int n_modes[MAX_MODE_CLASS];
82 static struct mode_data *void_mode;
83 
84 static const struct mode_data blank_mode = {
85   0, "<unknown>", MAX_MODE_CLASS,
86   -1U, -1U, -1U, -1U,
87   0, 0, 0, 0, 0, 0,
88   "<unknown>", 0, 0, 0, 0, false, 0
89 };
90 
91 static htab_t modes_by_name;
92 
93 /* Data structure for recording target-specified runtime adjustments
94    to a particular mode.  We support varying the byte size, the
95    alignment, and the floating point format.  */
96 struct mode_adjust
97 {
98   struct mode_adjust *next;
99   struct mode_data *mode;
100   const char *adjustment;
101 
102   const char *file;
103   unsigned int line;
104 };
105 
106 static struct mode_adjust *adj_bytesize;
107 static struct mode_adjust *adj_alignment;
108 static struct mode_adjust *adj_format;
109 static struct mode_adjust *adj_ibit;
110 static struct mode_adjust *adj_fbit;
111 
112 /* Mode class operations.  */
113 static enum mode_class
114 complex_class (enum mode_class c)
115 {
116   switch (c)
117     {
118     case MODE_INT: return MODE_COMPLEX_INT;
119     case MODE_PARTIAL_INT: return MODE_COMPLEX_INT;
120     case MODE_FLOAT: return MODE_COMPLEX_FLOAT;
121     default:
122       error ("no complex class for class %s", mode_class_names[c]);
123       return MODE_RANDOM;
124     }
125 }
126 
127 static enum mode_class
128 vector_class (enum mode_class cl)
129 {
130   switch (cl)
131     {
132     case MODE_INT: return MODE_VECTOR_INT;
133     case MODE_FLOAT: return MODE_VECTOR_FLOAT;
134     case MODE_FRACT: return MODE_VECTOR_FRACT;
135     case MODE_UFRACT: return MODE_VECTOR_UFRACT;
136     case MODE_ACCUM: return MODE_VECTOR_ACCUM;
137     case MODE_UACCUM: return MODE_VECTOR_UACCUM;
138     default:
139       error ("no vector class for class %s", mode_class_names[cl]);
140       return MODE_RANDOM;
141     }
142 }
143 
144 /* Utility routines.  */
145 static inline struct mode_data *
146 find_mode (const char *name)
147 {
148   struct mode_data key;
149 
150   key.name = name;
151   return (struct mode_data *) htab_find (modes_by_name, &key);
152 }
153 
154 static struct mode_data *
155 new_mode (enum mode_class cl, const char *name,
156 	  const char *file, unsigned int line)
157 {
158   struct mode_data *m;
159   static unsigned int count = 0;
160 
161   m = find_mode (name);
162   if (m)
163     {
164       error ("%s:%d: duplicate definition of mode \"%s\"",
165 	     trim_filename (file), line, name);
166       error ("%s:%d: previous definition here", m->file, m->line);
167       return m;
168     }
169 
170   m = XNEW (struct mode_data);
171   memcpy (m, &blank_mode, sizeof (struct mode_data));
172   m->cl = cl;
173   m->name = name;
174   if (file)
175     m->file = trim_filename (file);
176   m->line = line;
177   m->counter = count++;
178 
179   m->next = modes[cl];
180   modes[cl] = m;
181   n_modes[cl]++;
182 
183   *htab_find_slot (modes_by_name, m, INSERT) = m;
184 
185   return m;
186 }
187 
188 static hashval_t
189 hash_mode (const void *p)
190 {
191   const struct mode_data *m = (const struct mode_data *)p;
192   return htab_hash_string (m->name);
193 }
194 
195 static int
196 eq_mode (const void *p, const void *q)
197 {
198   const struct mode_data *a = (const struct mode_data *)p;
199   const struct mode_data *b = (const struct mode_data *)q;
200 
201   return !strcmp (a->name, b->name);
202 }
203 
204 #define for_all_modes(C, M)			\
205   for (C = 0; C < MAX_MODE_CLASS; C++)		\
206     for (M = modes[C]; M; M = M->next)
207 
208 static void ATTRIBUTE_UNUSED
209 new_adjust (const char *name,
210 	    struct mode_adjust **category, const char *catname,
211 	    const char *adjustment,
212 	    enum mode_class required_class_from,
213 	    enum mode_class required_class_to,
214 	    const char *file, unsigned int line)
215 {
216   struct mode_data *mode = find_mode (name);
217   struct mode_adjust *a;
218 
219   file = trim_filename (file);
220 
221   if (!mode)
222     {
223       error ("%s:%d: no mode \"%s\"", file, line, name);
224       return;
225     }
226 
227   if (required_class_from != MODE_RANDOM
228       && (mode->cl < required_class_from || mode->cl > required_class_to))
229     {
230       error ("%s:%d: mode \"%s\" is not among class {%s, %s}",
231 	     file, line, name, mode_class_names[required_class_from] + 5,
232 	     mode_class_names[required_class_to] + 5);
233       return;
234     }
235 
236   for (a = *category; a; a = a->next)
237     if (a->mode == mode)
238       {
239 	error ("%s:%d: mode \"%s\" already has a %s adjustment",
240 	       file, line, name, catname);
241 	error ("%s:%d: previous adjustment here", a->file, a->line);
242 	return;
243       }
244 
245   a = XNEW (struct mode_adjust);
246   a->mode = mode;
247   a->adjustment = adjustment;
248   a->file = file;
249   a->line = line;
250 
251   a->next = *category;
252   *category = a;
253 }
254 
255 /* Diagnose failure to meet expectations in a partially filled out
256    mode structure.  */
257 enum requirement { SET, UNSET, OPTIONAL };
258 
259 #define validate_field_(mname, fname, req, val, unset, file, line) do {	\
260   switch (req)								\
261     {									\
262     case SET:								\
263       if (val == unset)							\
264 	error ("%s:%d: (%s) field %s must be set",			\
265 	       file, line, mname, fname);				\
266       break;								\
267     case UNSET:								\
268       if (val != unset)							\
269 	error ("%s:%d: (%s) field %s must not be set",			\
270 	       file, line, mname, fname);				\
271     case OPTIONAL:							\
272       break;								\
273     }									\
274 } while (0)
275 
276 #define validate_field(M, F) \
277   validate_field_(M->name, #F, r_##F, M->F, blank_mode.F, M->file, M->line)
278 
279 static void
280 validate_mode (struct mode_data *m,
281 	       enum requirement r_precision,
282 	       enum requirement r_bytesize,
283 	       enum requirement r_component,
284 	       enum requirement r_ncomponents,
285 	       enum requirement r_format)
286 {
287   validate_field (m, precision);
288   validate_field (m, bytesize);
289   validate_field (m, component);
290   validate_field (m, ncomponents);
291   validate_field (m, format);
292 }
293 #undef validate_field
294 #undef validate_field_
295 
296 /* Given a partially-filled-out mode structure, figure out what we can
297    and fill the rest of it in; die if it isn't enough.  */
298 static void
299 complete_mode (struct mode_data *m)
300 {
301   unsigned int alignment;
302 
303   if (!m->name)
304     {
305       error ("%s:%d: mode with no name", m->file, m->line);
306       return;
307     }
308   if (m->cl == MAX_MODE_CLASS)
309     {
310       error ("%s:%d: %smode has no mode class", m->file, m->line, m->name);
311       return;
312     }
313 
314   switch (m->cl)
315     {
316     case MODE_RANDOM:
317       /* Nothing more need be said.  */
318       if (!strcmp (m->name, "VOID"))
319 	void_mode = m;
320 
321       validate_mode (m, UNSET, UNSET, UNSET, UNSET, UNSET);
322 
323       m->precision = 0;
324       m->bytesize = 0;
325       m->ncomponents = 0;
326       m->component = 0;
327       break;
328 
329     case MODE_CC:
330       /* Again, nothing more need be said.  For historical reasons,
331 	 the size of a CC mode is four units.  */
332       validate_mode (m, UNSET, UNSET, UNSET, UNSET, UNSET);
333 
334       m->bytesize = 4;
335       m->ncomponents = 1;
336       m->component = 0;
337       break;
338 
339     case MODE_INT:
340     case MODE_POINTER_BOUNDS:
341     case MODE_FLOAT:
342     case MODE_DECIMAL_FLOAT:
343     case MODE_FRACT:
344     case MODE_UFRACT:
345     case MODE_ACCUM:
346     case MODE_UACCUM:
347       /* A scalar mode must have a byte size, may have a bit size,
348 	 and must not have components.   A float mode must have a
349          format.  */
350       validate_mode (m, OPTIONAL, SET, UNSET, UNSET,
351 		     (m->cl == MODE_FLOAT || m->cl == MODE_DECIMAL_FLOAT)
352 		     ? SET : UNSET);
353 
354       m->ncomponents = 1;
355       m->component = 0;
356       break;
357 
358     case MODE_PARTIAL_INT:
359       /* A partial integer mode uses ->component to say what the
360 	 corresponding full-size integer mode is, and may also
361 	 specify a bit size.  */
362       validate_mode (m, OPTIONAL, UNSET, SET, UNSET, UNSET);
363 
364       m->bytesize = m->component->bytesize;
365 
366       m->ncomponents = 1;
367       break;
368 
369     case MODE_COMPLEX_INT:
370     case MODE_COMPLEX_FLOAT:
371       /* Complex modes should have a component indicated, but no more.  */
372       validate_mode (m, UNSET, UNSET, SET, UNSET, UNSET);
373       m->ncomponents = 2;
374       if (m->component->precision != (unsigned int)-1)
375 	m->precision = 2 * m->component->precision;
376       m->bytesize = 2 * m->component->bytesize;
377       break;
378 
379     case MODE_VECTOR_INT:
380     case MODE_VECTOR_FLOAT:
381     case MODE_VECTOR_FRACT:
382     case MODE_VECTOR_UFRACT:
383     case MODE_VECTOR_ACCUM:
384     case MODE_VECTOR_UACCUM:
385       /* Vector modes should have a component and a number of components.  */
386       validate_mode (m, UNSET, UNSET, SET, SET, UNSET);
387       if (m->component->precision != (unsigned int)-1)
388 	m->precision = m->ncomponents * m->component->precision;
389       m->bytesize = m->ncomponents * m->component->bytesize;
390       break;
391 
392     default:
393       gcc_unreachable ();
394     }
395 
396   /* If not already specified, the mode alignment defaults to the largest
397      power of two that divides the size of the object.  Complex types are
398      not more aligned than their contents.  */
399   if (m->cl == MODE_COMPLEX_INT || m->cl == MODE_COMPLEX_FLOAT)
400     alignment = m->component->bytesize;
401   else
402     alignment = m->bytesize;
403 
404   m->alignment = alignment & (~alignment + 1);
405 
406   /* If this mode has components, make the component mode point back
407      to this mode, for the sake of adjustments.  */
408   if (m->component)
409     {
410       m->next_cont = m->component->contained;
411       m->component->contained = m;
412     }
413 }
414 
415 static void
416 complete_all_modes (void)
417 {
418   struct mode_data *m;
419   int cl;
420 
421   for_all_modes (cl, m)
422     complete_mode (m);
423 }
424 
425 /* For each mode in class CLASS, construct a corresponding complex mode.  */
426 #define COMPLEX_MODES(C) make_complex_modes (MODE_##C, __FILE__, __LINE__)
427 static void
428 make_complex_modes (enum mode_class cl,
429 		    const char *file, unsigned int line)
430 {
431   struct mode_data *m;
432   struct mode_data *c;
433   enum mode_class cclass = complex_class (cl);
434 
435   if (cclass == MODE_RANDOM)
436     return;
437 
438   for (m = modes[cl]; m; m = m->next)
439     {
440       char *p, *buf;
441       size_t m_len;
442 
443       /* Skip BImode.  FIXME: BImode probably shouldn't be MODE_INT.  */
444       if (m->precision == 1)
445 	continue;
446 
447       m_len = strlen (m->name);
448       /* The leading "1 +" is in case we prepend a "C" below.  */
449       buf = (char *) xmalloc (1 + m_len + 1);
450 
451       /* Float complex modes are named SCmode, etc.
452 	 Int complex modes are named CSImode, etc.
453          This inconsistency should be eliminated.  */
454       p = 0;
455       if (cl == MODE_FLOAT)
456 	{
457 	  memcpy (buf, m->name, m_len + 1);
458 	  p = strchr (buf, 'F');
459 	  if (p == 0 && strchr (buf, 'D') == 0)
460 	    {
461 	      error ("%s:%d: float mode \"%s\" has no 'F' or 'D'",
462 		     m->file, m->line, m->name);
463 	      free (buf);
464 	      continue;
465 	    }
466 	}
467       if (p != 0)
468 	*p = 'C';
469       else
470 	{
471 	  buf[0] = 'C';
472 	  memcpy (buf + 1, m->name, m_len + 1);
473 	}
474 
475       c = new_mode (cclass, buf, file, line);
476       c->component = m;
477       m->complex = c;
478     }
479 }
480 
481 /* For all modes in class CL, construct vector modes of width
482    WIDTH, having as many components as necessary.  */
483 #define VECTOR_MODES(C, W) make_vector_modes (MODE_##C, W, __FILE__, __LINE__)
484 static void ATTRIBUTE_UNUSED
485 make_vector_modes (enum mode_class cl, unsigned int width,
486 		   const char *file, unsigned int line)
487 {
488   struct mode_data *m;
489   struct mode_data *v;
490   /* Big enough for a 32-bit UINT_MAX plus the text.  */
491   char buf[12];
492   unsigned int ncomponents;
493   enum mode_class vclass = vector_class (cl);
494 
495   if (vclass == MODE_RANDOM)
496     return;
497 
498   for (m = modes[cl]; m; m = m->next)
499     {
500       /* Do not construct vector modes with only one element, or
501 	 vector modes where the element size doesn't divide the full
502 	 size evenly.  */
503       ncomponents = width / m->bytesize;
504       if (ncomponents < 2)
505 	continue;
506       if (width % m->bytesize)
507 	continue;
508 
509       /* Skip QFmode and BImode.  FIXME: this special case should
510 	 not be necessary.  */
511       if (cl == MODE_FLOAT && m->bytesize == 1)
512 	continue;
513       if (cl == MODE_INT && m->precision == 1)
514 	continue;
515 
516       if ((size_t)snprintf (buf, sizeof buf, "V%u%s", ncomponents, m->name)
517 	  >= sizeof buf)
518 	{
519 	  error ("%s:%d: mode name \"%s\" is too long",
520 		 m->file, m->line, m->name);
521 	  continue;
522 	}
523 
524       v = new_mode (vclass, xstrdup (buf), file, line);
525       v->component = m;
526       v->ncomponents = ncomponents;
527     }
528 }
529 
530 /* Input.  */
531 
532 #define _SPECIAL_MODE(C, N) \
533   make_special_mode (MODE_##C, #N, __FILE__, __LINE__)
534 #define RANDOM_MODE(N) _SPECIAL_MODE (RANDOM, N)
535 #define CC_MODE(N) _SPECIAL_MODE (CC, N)
536 
537 static void
538 make_special_mode (enum mode_class cl, const char *name,
539 		   const char *file, unsigned int line)
540 {
541   new_mode (cl, name, file, line);
542 }
543 
544 #define POINTER_BOUNDS_MODE(N, Y) \
545   make_pointer_bounds_mode (#N, Y, __FILE__, __LINE__)
546 
547 static void ATTRIBUTE_UNUSED
548 make_pointer_bounds_mode (const char *name,
549 			  unsigned int bytesize,
550 			  const char *file, unsigned int line)
551 {
552   struct mode_data *m = new_mode (MODE_POINTER_BOUNDS, name, file, line);
553   m->bytesize = bytesize;
554 }
555 
556 
557 #define INT_MODE(N, Y) FRACTIONAL_INT_MODE (N, -1U, Y)
558 #define FRACTIONAL_INT_MODE(N, B, Y) \
559   make_int_mode (#N, B, Y, __FILE__, __LINE__)
560 
561 static void
562 make_int_mode (const char *name,
563 	       unsigned int precision, unsigned int bytesize,
564 	       const char *file, unsigned int line)
565 {
566   struct mode_data *m = new_mode (MODE_INT, name, file, line);
567   m->bytesize = bytesize;
568   m->precision = precision;
569 }
570 
571 #define FRACT_MODE(N, Y, F) \
572 	make_fixed_point_mode (MODE_FRACT, #N, Y, 0, F, __FILE__, __LINE__)
573 
574 #define UFRACT_MODE(N, Y, F) \
575 	make_fixed_point_mode (MODE_UFRACT, #N, Y, 0, F, __FILE__, __LINE__)
576 
577 #define ACCUM_MODE(N, Y, I, F) \
578 	make_fixed_point_mode (MODE_ACCUM, #N, Y, I, F, __FILE__, __LINE__)
579 
580 #define UACCUM_MODE(N, Y, I, F) \
581 	make_fixed_point_mode (MODE_UACCUM, #N, Y, I, F, __FILE__, __LINE__)
582 
583 /* Create a fixed-point mode by setting CL, NAME, BYTESIZE, IBIT, FBIT,
584    FILE, and LINE.  */
585 
586 static void
587 make_fixed_point_mode (enum mode_class cl,
588 		       const char *name,
589 		       unsigned int bytesize,
590 		       unsigned int ibit,
591 		       unsigned int fbit,
592 		       const char *file, unsigned int line)
593 {
594   struct mode_data *m = new_mode (cl, name, file, line);
595   m->bytesize = bytesize;
596   m->ibit = ibit;
597   m->fbit = fbit;
598 }
599 
600 #define FLOAT_MODE(N, Y, F)             FRACTIONAL_FLOAT_MODE (N, -1U, Y, F)
601 #define FRACTIONAL_FLOAT_MODE(N, B, Y, F) \
602   make_float_mode (#N, B, Y, #F, __FILE__, __LINE__)
603 
604 static void
605 make_float_mode (const char *name,
606 		 unsigned int precision, unsigned int bytesize,
607 		 const char *format,
608 		 const char *file, unsigned int line)
609 {
610   struct mode_data *m = new_mode (MODE_FLOAT, name, file, line);
611   m->bytesize = bytesize;
612   m->precision = precision;
613   m->format = format;
614 }
615 
616 #define DECIMAL_FLOAT_MODE(N, Y, F)	\
617 	FRACTIONAL_DECIMAL_FLOAT_MODE (N, -1U, Y, F)
618 #define FRACTIONAL_DECIMAL_FLOAT_MODE(N, B, Y, F)	\
619   make_decimal_float_mode (#N, B, Y, #F, __FILE__, __LINE__)
620 
621 static void
622 make_decimal_float_mode (const char *name,
623 			 unsigned int precision, unsigned int bytesize,
624 			 const char *format,
625 			 const char *file, unsigned int line)
626 {
627   struct mode_data *m = new_mode (MODE_DECIMAL_FLOAT, name, file, line);
628   m->bytesize = bytesize;
629   m->precision = precision;
630   m->format = format;
631 }
632 
633 #define RESET_FLOAT_FORMAT(N, F) \
634   reset_float_format (#N, #F, __FILE__, __LINE__)
635 static void ATTRIBUTE_UNUSED
636 reset_float_format (const char *name, const char *format,
637 		    const char *file, unsigned int line)
638 {
639   struct mode_data *m = find_mode (name);
640   if (!m)
641     {
642       error ("%s:%d: no mode \"%s\"", file, line, name);
643       return;
644     }
645   if (m->cl != MODE_FLOAT && m->cl != MODE_DECIMAL_FLOAT)
646     {
647       error ("%s:%d: mode \"%s\" is not a FLOAT class", file, line, name);
648       return;
649     }
650   m->format = format;
651 }
652 
653 /* __intN support.  */
654 #define INT_N(M,PREC)				\
655   make_int_n (#M, PREC, __FILE__, __LINE__)
656 static void ATTRIBUTE_UNUSED
657 make_int_n (const char *m, int bitsize,
658             const char *file, unsigned int line)
659 {
660   struct mode_data *component = find_mode (m);
661   if (!component)
662     {
663       error ("%s:%d: no mode \"%s\"", file, line, m);
664       return;
665     }
666   if (component->cl != MODE_INT
667       && component->cl != MODE_PARTIAL_INT)
668     {
669       error ("%s:%d: mode \"%s\" is not class INT or PARTIAL_INT", file, line, m);
670       return;
671     }
672   if (component->int_n != 0)
673     {
674       error ("%s:%d: mode \"%s\" already has an intN", file, line, m);
675       return;
676     }
677 
678   component->int_n = bitsize;
679 }
680 
681 /* Partial integer modes are specified by relation to a full integer
682    mode.  */
683 #define PARTIAL_INT_MODE(M,PREC,NAME)				\
684   make_partial_integer_mode (#M, #NAME, PREC, __FILE__, __LINE__)
685 static void ATTRIBUTE_UNUSED
686 make_partial_integer_mode (const char *base, const char *name,
687 			   unsigned int precision,
688 			   const char *file, unsigned int line)
689 {
690   struct mode_data *m;
691   struct mode_data *component = find_mode (base);
692   if (!component)
693     {
694       error ("%s:%d: no mode \"%s\"", file, line, name);
695       return;
696     }
697   if (component->cl != MODE_INT)
698     {
699       error ("%s:%d: mode \"%s\" is not class INT", file, line, name);
700       return;
701     }
702 
703   m = new_mode (MODE_PARTIAL_INT, name, file, line);
704   m->precision = precision;
705   m->component = component;
706 }
707 
708 /* A single vector mode can be specified by naming its component
709    mode and the number of components.  */
710 #define VECTOR_MODE(C, M, N) \
711   make_vector_mode (MODE_##C, #M, N, __FILE__, __LINE__);
712 static void ATTRIBUTE_UNUSED
713 make_vector_mode (enum mode_class bclass,
714 		  const char *base,
715 		  unsigned int ncomponents,
716 		  const char *file, unsigned int line)
717 {
718   struct mode_data *v;
719   enum mode_class vclass = vector_class (bclass);
720   struct mode_data *component = find_mode (base);
721   char namebuf[16];
722 
723   if (vclass == MODE_RANDOM)
724     return;
725   if (component == 0)
726     {
727       error ("%s:%d: no mode \"%s\"", file, line, base);
728       return;
729     }
730   if (component->cl != bclass
731       && (component->cl != MODE_PARTIAL_INT
732 	  || bclass != MODE_INT))
733     {
734       error ("%s:%d: mode \"%s\" is not class %s",
735 	     file, line, base, mode_class_names[bclass] + 5);
736       return;
737     }
738 
739   if ((size_t)snprintf (namebuf, sizeof namebuf, "V%u%s",
740 			ncomponents, base) >= sizeof namebuf)
741     {
742       error ("%s:%d: mode name \"%s\" is too long",
743 	     file, line, base);
744       return;
745     }
746 
747   v = new_mode (vclass, xstrdup (namebuf), file, line);
748   v->ncomponents = ncomponents;
749   v->component = component;
750 }
751 
752 /* Adjustability.  */
753 #define _ADD_ADJUST(A, M, X, C1, C2) \
754   new_adjust (#M, &adj_##A, #A, #X, MODE_##C1, MODE_##C2, __FILE__, __LINE__)
755 
756 #define ADJUST_BYTESIZE(M, X)  _ADD_ADJUST (bytesize, M, X, RANDOM, RANDOM)
757 #define ADJUST_ALIGNMENT(M, X) _ADD_ADJUST (alignment, M, X, RANDOM, RANDOM)
758 #define ADJUST_FLOAT_FORMAT(M, X)    _ADD_ADJUST (format, M, X, FLOAT, FLOAT)
759 #define ADJUST_IBIT(M, X)  _ADD_ADJUST (ibit, M, X, ACCUM, UACCUM)
760 #define ADJUST_FBIT(M, X)  _ADD_ADJUST (fbit, M, X, FRACT, UACCUM)
761 
762 static int bits_per_unit;
763 static int max_bitsize_mode_any_int;
764 
765 static void
766 create_modes (void)
767 {
768 #include "machmode.def"
769 
770   /* So put the default value unless the target needs a non standard
771      value. */
772 #ifdef BITS_PER_UNIT
773   bits_per_unit = BITS_PER_UNIT;
774 #else
775   bits_per_unit = 8;
776 #endif
777 
778 #ifdef MAX_BITSIZE_MODE_ANY_INT
779   max_bitsize_mode_any_int = MAX_BITSIZE_MODE_ANY_INT;
780 #else
781   max_bitsize_mode_any_int = 0;
782 #endif
783 }
784 
785 /* Processing.  */
786 
787 /* Sort a list of modes into the order needed for the WIDER field:
788    major sort by precision, minor sort by component precision.
789 
790    For instance:
791      QI < HI < SI < DI < TI
792      V4QI < V2HI < V8QI < V4HI < V2SI.
793 
794    If the precision is not set, sort by the bytesize.  A mode with
795    precision set gets sorted before a mode without precision set, if
796    they have the same bytesize; this is the right thing because
797    the precision must always be smaller than the bytesize * BITS_PER_UNIT.
798    We don't have to do anything special to get this done -- an unset
799    precision shows up as (unsigned int)-1, i.e. UINT_MAX.  */
800 static int
801 cmp_modes (const void *a, const void *b)
802 {
803   const struct mode_data *const m = *(const struct mode_data *const*)a;
804   const struct mode_data *const n = *(const struct mode_data *const*)b;
805 
806   if (m->bytesize > n->bytesize)
807     return 1;
808   else if (m->bytesize < n->bytesize)
809     return -1;
810 
811   if (m->precision > n->precision)
812     return 1;
813   else if (m->precision < n->precision)
814     return -1;
815 
816   if (!m->component && !n->component)
817     {
818       if (m->counter < n->counter)
819 	return -1;
820       else
821 	return 1;
822     }
823 
824   if (m->component->bytesize > n->component->bytesize)
825     return 1;
826   else if (m->component->bytesize < n->component->bytesize)
827     return -1;
828 
829   if (m->component->precision > n->component->precision)
830     return 1;
831   else if (m->component->precision < n->component->precision)
832     return -1;
833 
834   if (m->counter < n->counter)
835     return -1;
836   else
837     return 1;
838 }
839 
840 static void
841 calc_wider_mode (void)
842 {
843   int c;
844   struct mode_data *m;
845   struct mode_data **sortbuf;
846   unsigned int max_n_modes = 0;
847   unsigned int i, j;
848 
849   for (c = 0; c < MAX_MODE_CLASS; c++)
850     max_n_modes = MAX (max_n_modes, n_modes[c]);
851 
852   /* Allocate max_n_modes + 1 entries to leave room for the extra null
853      pointer assigned after the qsort call below.  */
854   sortbuf = XALLOCAVEC (struct mode_data *, max_n_modes + 1);
855 
856   for (c = 0; c < MAX_MODE_CLASS; c++)
857     {
858       /* "wider" is not meaningful for MODE_RANDOM and MODE_CC.
859 	 However, we want these in textual order, and we have
860 	 precisely the reverse.  */
861       if (c == MODE_RANDOM || c == MODE_CC)
862 	{
863 	  struct mode_data *prev, *next;
864 
865 	  for (prev = 0, m = modes[c]; m; m = next)
866 	    {
867 	      m->wider = void_mode;
868 
869 	      /* this is nreverse */
870 	      next = m->next;
871 	      m->next = prev;
872 	      prev = m;
873 	    }
874 	  modes[c] = prev;
875 	}
876       else
877 	{
878 	  if (!modes[c])
879 	    continue;
880 
881 	  for (i = 0, m = modes[c]; m; i++, m = m->next)
882 	    sortbuf[i] = m;
883 
884 	  qsort (sortbuf, i, sizeof (struct mode_data *), cmp_modes);
885 
886 	  sortbuf[i] = 0;
887 	  for (j = 0; j < i; j++)
888 	    {
889 	      sortbuf[j]->next = sortbuf[j + 1];
890 	      if (c == MODE_PARTIAL_INT)
891 		sortbuf[j]->wider = sortbuf[j]->component;
892 	      else
893 		sortbuf[j]->wider = sortbuf[j]->next;
894 	    }
895 
896 	  modes[c] = sortbuf[0];
897 	}
898     }
899 }
900 
901 /* Output routines.  */
902 
903 #define tagged_printf(FMT, ARG, TAG) do {		\
904   int count_ = printf ("  " FMT ",", ARG);		\
905   printf ("%*s/* %s */\n", 27 - count_, "", TAG);	\
906 } while (0)
907 
908 #define print_decl(TYPE, NAME, ASIZE) \
909   puts ("\nconst " TYPE " " NAME "[" ASIZE "] =\n{");
910 
911 #define print_maybe_const_decl(TYPE, NAME, ASIZE, CATEGORY)	\
912   printf ("\n" TYPE " " NAME "[" ASIZE "] = \n{\n",		\
913 	  adj_##CATEGORY ? "" : "const ")
914 
915 #define print_closer() puts ("};")
916 
917 /* Compute the max bitsize of some of the classes of integers.  It may
918    be that there are needs for the other integer classes, and this
919    code is easy to extend.  */
920 static void
921 emit_max_int (void)
922 {
923   unsigned int max, mmax;
924   struct mode_data *i;
925   int j;
926 
927   puts ("");
928 
929   printf ("#define BITS_PER_UNIT (%d)\n", bits_per_unit);
930 
931   if (max_bitsize_mode_any_int == 0)
932     {
933       for (max = 1, i = modes[MODE_INT]; i; i = i->next)
934 	if (max < i->bytesize)
935 	  max = i->bytesize;
936       mmax = max;
937       for (max = 1, i = modes[MODE_PARTIAL_INT]; i; i = i->next)
938 	if (max < i->bytesize)
939 	  max = i->bytesize;
940       if (max > mmax)
941 	mmax = max;
942       printf ("#define MAX_BITSIZE_MODE_ANY_INT (%d*BITS_PER_UNIT)\n", mmax);
943     }
944   else
945     printf ("#define MAX_BITSIZE_MODE_ANY_INT %d\n", max_bitsize_mode_any_int);
946 
947   mmax = 0;
948   for (j = 0; j < MAX_MODE_CLASS; j++)
949     for (i = modes[j]; i; i = i->next)
950       if (mmax < i->bytesize)
951 	mmax = i->bytesize;
952   printf ("#define MAX_BITSIZE_MODE_ANY_MODE (%d*BITS_PER_UNIT)\n", mmax);
953 }
954 
955 /* Emit mode_size_inline routine into insn-modes.h header.  */
956 static void
957 emit_mode_size_inline (void)
958 {
959   int c;
960   struct mode_adjust *a;
961   struct mode_data *m;
962 
963   /* Size adjustments must be propagated to all containing modes.  */
964   for (a = adj_bytesize; a; a = a->next)
965     {
966       a->mode->need_bytesize_adj = true;
967       for (m = a->mode->contained; m; m = m->next_cont)
968 	m->need_bytesize_adj = true;
969     }
970 
971   printf ("\
972 #ifdef __cplusplus\n\
973 inline __attribute__((__always_inline__))\n\
974 #else\n\
975 extern __inline__ __attribute__((__always_inline__, __gnu_inline__))\n\
976 #endif\n\
977 unsigned short\n\
978 mode_size_inline (machine_mode mode)\n\
979 {\n\
980   extern %sunsigned short mode_size[NUM_MACHINE_MODES];\n\
981   gcc_assert (mode >= 0 && mode < NUM_MACHINE_MODES);\n\
982   switch (mode)\n\
983     {\n", adj_bytesize ? "" : "const ");
984 
985   for_all_modes (c, m)
986     if (!m->need_bytesize_adj)
987       printf ("    case %smode: return %u;\n", m->name, m->bytesize);
988 
989   puts ("\
990     default: return mode_size[mode];\n\
991     }\n\
992 }\n");
993 }
994 
995 /* Emit mode_nunits_inline routine into insn-modes.h header.  */
996 static void
997 emit_mode_nunits_inline (void)
998 {
999   int c;
1000   struct mode_data *m;
1001 
1002   puts ("\
1003 #ifdef __cplusplus\n\
1004 inline __attribute__((__always_inline__))\n\
1005 #else\n\
1006 extern __inline__ __attribute__((__always_inline__, __gnu_inline__))\n\
1007 #endif\n\
1008 unsigned char\n\
1009 mode_nunits_inline (machine_mode mode)\n\
1010 {\n\
1011   extern const unsigned char mode_nunits[NUM_MACHINE_MODES];\n\
1012   gcc_assert (mode >= 0 && mode < NUM_MACHINE_MODES);\n\
1013   switch (mode)\n\
1014     {");
1015 
1016   for_all_modes (c, m)
1017     printf ("    case %smode: return %u;\n", m->name, m->ncomponents);
1018 
1019   puts ("\
1020     default: return mode_nunits[mode];\n\
1021     }\n\
1022 }\n");
1023 }
1024 
1025 /* Emit mode_inner_inline routine into insn-modes.h header.  */
1026 static void
1027 emit_mode_inner_inline (void)
1028 {
1029   int c;
1030   struct mode_data *m;
1031 
1032   puts ("\
1033 #ifdef __cplusplus\n\
1034 inline __attribute__((__always_inline__))\n\
1035 #else\n\
1036 extern __inline__ __attribute__((__always_inline__, __gnu_inline__))\n\
1037 #endif\n\
1038 unsigned char\n\
1039 mode_inner_inline (machine_mode mode)\n\
1040 {\n\
1041   extern const unsigned char mode_inner[NUM_MACHINE_MODES];\n\
1042   gcc_assert (mode >= 0 && mode < NUM_MACHINE_MODES);\n\
1043   switch (mode)\n\
1044     {");
1045 
1046   for_all_modes (c, m)
1047     printf ("    case %smode: return %smode;\n", m->name,
1048 	    c != MODE_PARTIAL_INT && m->component
1049 	    ? m->component->name : m->name);
1050 
1051   puts ("\
1052     default: return mode_inner[mode];\n\
1053     }\n\
1054 }\n");
1055 }
1056 
1057 /* Emit mode_unit_size_inline routine into insn-modes.h header.  */
1058 static void
1059 emit_mode_unit_size_inline (void)
1060 {
1061   int c;
1062   struct mode_data *m;
1063 
1064   puts ("\
1065 #ifdef __cplusplus\n\
1066 inline __attribute__((__always_inline__))\n\
1067 #else\n\
1068 extern __inline__ __attribute__((__always_inline__, __gnu_inline__))\n\
1069 #endif\n\
1070 unsigned char\n\
1071 mode_unit_size_inline (machine_mode mode)\n\
1072 {\n\
1073   extern CONST_MODE_UNIT_SIZE unsigned char mode_unit_size[NUM_MACHINE_MODES];\
1074 \n\
1075   gcc_assert (mode >= 0 && mode < NUM_MACHINE_MODES);\n\
1076   switch (mode)\n\
1077     {");
1078 
1079   for_all_modes (c, m)
1080     {
1081       const char *name = m->name;
1082       struct mode_data *m2 = m;
1083       if (c != MODE_PARTIAL_INT && m2->component)
1084 	m2 = m2->component;
1085       if (!m2->need_bytesize_adj)
1086 	printf ("    case %smode: return %u;\n", name, m2->bytesize);
1087     }
1088 
1089   puts ("\
1090     default: return mode_unit_size[mode];\n\
1091     }\n\
1092 }\n");
1093 }
1094 
1095 /* Emit mode_unit_precision_inline routine into insn-modes.h header.  */
1096 static void
1097 emit_mode_unit_precision_inline (void)
1098 {
1099   int c;
1100   struct mode_data *m;
1101 
1102   puts ("\
1103 #ifdef __cplusplus\n\
1104 inline __attribute__((__always_inline__))\n\
1105 #else\n\
1106 extern __inline__ __attribute__((__always_inline__, __gnu_inline__))\n\
1107 #endif\n\
1108 unsigned short\n\
1109 mode_unit_precision_inline (machine_mode mode)\n\
1110 {\n\
1111   extern const unsigned short mode_unit_precision[NUM_MACHINE_MODES];\n\
1112   gcc_assert (mode >= 0 && mode < NUM_MACHINE_MODES);\n\
1113   switch (mode)\n\
1114     {");
1115 
1116   for_all_modes (c, m)
1117     {
1118       struct mode_data *m2
1119 	= (c != MODE_PARTIAL_INT && m->component) ? m->component : m;
1120       if (m2->precision != (unsigned int)-1)
1121 	printf ("    case %smode: return %u;\n", m->name, m2->precision);
1122       else
1123 	printf ("    case %smode: return %u*BITS_PER_UNIT;\n",
1124 		m->name, m2->bytesize);
1125     }
1126 
1127   puts ("\
1128     default: return mode_unit_precision[mode];\n\
1129     }\n\
1130 }\n");
1131 }
1132 
1133 static void
1134 emit_insn_modes_h (void)
1135 {
1136   int c;
1137   struct mode_data *m, *first, *last;
1138   int n_int_n_ents = 0;
1139 
1140   printf ("/* Generated automatically from machmode.def%s%s\n",
1141 	   HAVE_EXTRA_MODES ? " and " : "",
1142 	   EXTRA_MODES_FILE);
1143 
1144   puts ("\
1145    by genmodes.  */\n\
1146 \n\
1147 #ifndef GCC_INSN_MODES_H\n\
1148 #define GCC_INSN_MODES_H\n\
1149 \n\
1150 enum machine_mode\n{");
1151 
1152   for (c = 0; c < MAX_MODE_CLASS; c++)
1153     for (m = modes[c]; m; m = m->next)
1154       {
1155 	int count_ = printf ("  %smode,", m->name);
1156 	printf ("%*s/* %s:%d */\n", 27 - count_, "",
1157 		 trim_filename (m->file), m->line);
1158 	printf ("#define HAVE_%smode\n", m->name);
1159       }
1160 
1161   puts ("  MAX_MACHINE_MODE,\n");
1162 
1163   for (c = 0; c < MAX_MODE_CLASS; c++)
1164     {
1165       first = modes[c];
1166       last = 0;
1167       for (m = first; m; last = m, m = m->next)
1168 	;
1169 
1170       /* Don't use BImode for MIN_MODE_INT, since otherwise the middle
1171 	 end will try to use it for bitfields in structures and the
1172 	 like, which we do not want.  Only the target md file should
1173 	 generate BImode widgets.  */
1174       if (first && first->precision == 1 && c == MODE_INT)
1175 	first = first->next;
1176 
1177       if (first && last)
1178 	printf ("  MIN_%s = %smode,\n  MAX_%s = %smode,\n\n",
1179 		 mode_class_names[c], first->name,
1180 		 mode_class_names[c], last->name);
1181       else
1182 	printf ("  MIN_%s = %smode,\n  MAX_%s = %smode,\n\n",
1183 		 mode_class_names[c], void_mode->name,
1184 		 mode_class_names[c], void_mode->name);
1185     }
1186 
1187   puts ("\
1188   NUM_MACHINE_MODES = MAX_MACHINE_MODE\n\
1189 };\n");
1190 
1191   /* I can't think of a better idea, can you?  */
1192   printf ("#define CONST_MODE_SIZE%s\n", adj_bytesize ? "" : " const");
1193   printf ("#define CONST_MODE_UNIT_SIZE%s\n", adj_bytesize ? "" : " const");
1194   printf ("#define CONST_MODE_BASE_ALIGN%s\n", adj_alignment ? "" : " const");
1195 #if 0 /* disabled for backward compatibility, temporary */
1196   printf ("#define CONST_REAL_FORMAT_FOR_MODE%s\n", adj_format ? "" :" const");
1197 #endif
1198   printf ("#define CONST_MODE_IBIT%s\n", adj_ibit ? "" : " const");
1199   printf ("#define CONST_MODE_FBIT%s\n", adj_fbit ? "" : " const");
1200   emit_max_int ();
1201 
1202   for_all_modes (c, m)
1203     if (m->int_n)
1204       n_int_n_ents ++;
1205 
1206   printf ("#define NUM_INT_N_ENTS %d\n", n_int_n_ents);
1207 
1208   puts ("\n#if !defined (USED_FOR_TARGET) && GCC_VERSION >= 4001\n");
1209   emit_mode_size_inline ();
1210   emit_mode_nunits_inline ();
1211   emit_mode_inner_inline ();
1212   emit_mode_unit_size_inline ();
1213   emit_mode_unit_precision_inline ();
1214   puts ("#endif /* GCC_VERSION >= 4001 */");
1215 
1216   puts ("\
1217 \n\
1218 #endif /* insn-modes.h */");
1219 }
1220 
1221 static void
1222 emit_insn_modes_c_header (void)
1223 {
1224   printf ("/* Generated automatically from machmode.def%s%s\n",
1225 	   HAVE_EXTRA_MODES ? " and " : "",
1226 	   EXTRA_MODES_FILE);
1227 
1228   puts ("\
1229    by genmodes.  */\n\
1230 \n\
1231 #include \"config.h\"\n\
1232 #include \"system.h\"\n\
1233 #include \"coretypes.h\"\n\
1234 #include \"tm.h\"\n\
1235 #include \"machmode.h\"\n\
1236 #include \"real.h\"");
1237 }
1238 
1239 static void
1240 emit_min_insn_modes_c_header (void)
1241 {
1242   printf ("/* Generated automatically from machmode.def%s%s\n",
1243 	   HAVE_EXTRA_MODES ? " and " : "",
1244 	   EXTRA_MODES_FILE);
1245 
1246   puts ("\
1247    by genmodes.  */\n\
1248 \n\
1249 #include \"bconfig.h\"\n\
1250 #include \"system.h\"\n\
1251 #include \"machmode.h\"");
1252 }
1253 
1254 static void
1255 emit_mode_name (void)
1256 {
1257   int c;
1258   struct mode_data *m;
1259 
1260   print_decl ("char *const", "mode_name", "NUM_MACHINE_MODES");
1261 
1262   for_all_modes (c, m)
1263     printf ("  \"%s\",\n", m->name);
1264 
1265   print_closer ();
1266 }
1267 
1268 static void
1269 emit_mode_class (void)
1270 {
1271   int c;
1272   struct mode_data *m;
1273 
1274   print_decl ("unsigned char", "mode_class", "NUM_MACHINE_MODES");
1275 
1276   for_all_modes (c, m)
1277     tagged_printf ("%s", mode_class_names[m->cl], m->name);
1278 
1279   print_closer ();
1280 }
1281 
1282 static void
1283 emit_mode_precision (void)
1284 {
1285   int c;
1286   struct mode_data *m;
1287 
1288   print_decl ("unsigned short", "mode_precision", "NUM_MACHINE_MODES");
1289 
1290   for_all_modes (c, m)
1291     if (m->precision != (unsigned int)-1)
1292       tagged_printf ("%u", m->precision, m->name);
1293     else
1294       tagged_printf ("%u*BITS_PER_UNIT", m->bytesize, m->name);
1295 
1296   print_closer ();
1297 }
1298 
1299 static void
1300 emit_mode_size (void)
1301 {
1302   int c;
1303   struct mode_data *m;
1304 
1305   print_maybe_const_decl ("%sunsigned short", "mode_size",
1306 			  "NUM_MACHINE_MODES", bytesize);
1307 
1308   for_all_modes (c, m)
1309     tagged_printf ("%u", m->bytesize, m->name);
1310 
1311   print_closer ();
1312 }
1313 
1314 static void
1315 emit_mode_nunits (void)
1316 {
1317   int c;
1318   struct mode_data *m;
1319 
1320   print_decl ("unsigned char", "mode_nunits", "NUM_MACHINE_MODES");
1321 
1322   for_all_modes (c, m)
1323     tagged_printf ("%u", m->ncomponents, m->name);
1324 
1325   print_closer ();
1326 }
1327 
1328 static void
1329 emit_mode_wider (void)
1330 {
1331   int c;
1332   struct mode_data *m;
1333 
1334   print_decl ("unsigned char", "mode_wider", "NUM_MACHINE_MODES");
1335 
1336   for_all_modes (c, m)
1337     tagged_printf ("%smode",
1338 		   m->wider ? m->wider->name : void_mode->name,
1339 		   m->name);
1340 
1341   print_closer ();
1342   print_decl ("unsigned char", "mode_2xwider", "NUM_MACHINE_MODES");
1343 
1344   for_all_modes (c, m)
1345     {
1346       struct mode_data * m2;
1347 
1348       for (m2 = m;
1349 	   m2 && m2 != void_mode;
1350 	   m2 = m2->wider)
1351 	{
1352 	  if (m2->bytesize < 2 * m->bytesize)
1353 	    continue;
1354 	  if (m->precision != (unsigned int) -1)
1355 	    {
1356 	      if (m2->precision != 2 * m->precision)
1357 		continue;
1358 	    }
1359 	  else
1360 	    {
1361 	      if (m2->precision != (unsigned int) -1)
1362 		continue;
1363 	    }
1364 
1365 	  /* For vectors we want twice the number of components,
1366 	     with the same element type.  */
1367 	  if (m->cl == MODE_VECTOR_INT
1368 	      || m->cl == MODE_VECTOR_FLOAT
1369 	      || m->cl == MODE_VECTOR_FRACT
1370 	      || m->cl == MODE_VECTOR_UFRACT
1371 	      || m->cl == MODE_VECTOR_ACCUM
1372 	      || m->cl == MODE_VECTOR_UACCUM)
1373 	    {
1374 	      if (m2->ncomponents != 2 * m->ncomponents)
1375 		continue;
1376 	      if (m->component != m2->component)
1377 		continue;
1378 	    }
1379 
1380 	  break;
1381 	}
1382       if (m2 == void_mode)
1383 	m2 = 0;
1384       tagged_printf ("%smode",
1385 		     m2 ? m2->name : void_mode->name,
1386 		     m->name);
1387     }
1388 
1389   print_closer ();
1390 }
1391 
1392 static void
1393 emit_mode_complex (void)
1394 {
1395   int c;
1396   struct mode_data *m;
1397 
1398   print_decl ("unsigned char", "mode_complex", "NUM_MACHINE_MODES");
1399 
1400   for_all_modes (c, m)
1401     tagged_printf ("%smode",
1402 		   m->complex ? m->complex->name : void_mode->name,
1403 		   m->name);
1404 
1405   print_closer ();
1406 }
1407 
1408 static void
1409 emit_mode_mask (void)
1410 {
1411   int c;
1412   struct mode_data *m;
1413 
1414   print_decl ("unsigned HOST_WIDE_INT", "mode_mask_array",
1415 	      "NUM_MACHINE_MODES");
1416   puts ("\
1417 #define MODE_MASK(m)                          \\\n\
1418   ((m) >= HOST_BITS_PER_WIDE_INT)             \\\n\
1419    ? HOST_WIDE_INT_M1U                        \\\n\
1420    : (HOST_WIDE_INT_1U << (m)) - 1\n");
1421 
1422   for_all_modes (c, m)
1423     if (m->precision != (unsigned int)-1)
1424       tagged_printf ("MODE_MASK (%u)", m->precision, m->name);
1425     else
1426       tagged_printf ("MODE_MASK (%u*BITS_PER_UNIT)", m->bytesize, m->name);
1427 
1428   puts ("#undef MODE_MASK");
1429   print_closer ();
1430 }
1431 
1432 static void
1433 emit_mode_inner (void)
1434 {
1435   int c;
1436   struct mode_data *m;
1437 
1438   print_decl ("unsigned char", "mode_inner", "NUM_MACHINE_MODES");
1439 
1440   for_all_modes (c, m)
1441     tagged_printf ("%smode",
1442 		   c != MODE_PARTIAL_INT && m->component
1443 		   ? m->component->name : m->name,
1444 		   m->name);
1445 
1446   print_closer ();
1447 }
1448 
1449 /* Emit mode_unit_size array into insn-modes.c file.  */
1450 static void
1451 emit_mode_unit_size (void)
1452 {
1453   int c;
1454   struct mode_data *m;
1455 
1456   print_maybe_const_decl ("%sunsigned char", "mode_unit_size",
1457 			  "NUM_MACHINE_MODES", bytesize);
1458 
1459   for_all_modes (c, m)
1460     tagged_printf ("%u",
1461 		   c != MODE_PARTIAL_INT && m->component
1462 		   ? m->component->bytesize : m->bytesize, m->name);
1463 
1464   print_closer ();
1465 }
1466 
1467 /* Emit mode_unit_precision array into insn-modes.c file.  */
1468 static void
1469 emit_mode_unit_precision (void)
1470 {
1471   int c;
1472   struct mode_data *m;
1473 
1474   print_decl ("unsigned short", "mode_unit_precision", "NUM_MACHINE_MODES");
1475 
1476   for_all_modes (c, m)
1477     {
1478       struct mode_data *m2 = (c != MODE_PARTIAL_INT && m->component) ?
1479 			     m->component : m;
1480       if (m2->precision != (unsigned int)-1)
1481 	tagged_printf ("%u", m2->precision, m->name);
1482       else
1483 	tagged_printf ("%u*BITS_PER_UNIT", m2->bytesize, m->name);
1484     }
1485 
1486   print_closer ();
1487 }
1488 
1489 
1490 static void
1491 emit_mode_base_align (void)
1492 {
1493   int c;
1494   struct mode_data *m;
1495 
1496   print_maybe_const_decl ("%sunsigned short",
1497 			  "mode_base_align", "NUM_MACHINE_MODES",
1498 			  alignment);
1499 
1500   for_all_modes (c, m)
1501     tagged_printf ("%u", m->alignment, m->name);
1502 
1503   print_closer ();
1504 }
1505 
1506 static void
1507 emit_class_narrowest_mode (void)
1508 {
1509   int c;
1510 
1511   print_decl ("unsigned char", "class_narrowest_mode", "MAX_MODE_CLASS");
1512 
1513   for (c = 0; c < MAX_MODE_CLASS; c++)
1514     /* Bleah, all this to get the comment right for MIN_MODE_INT.  */
1515     tagged_printf ("MIN_%s", mode_class_names[c],
1516 		   modes[c]
1517 		   ? ((c != MODE_INT || modes[c]->precision != 1)
1518 		      ? modes[c]->name
1519 		      : (modes[c]->next
1520 			 ? modes[c]->next->name
1521 			 : void_mode->name))
1522 		   : void_mode->name);
1523 
1524   print_closer ();
1525 }
1526 
1527 static void
1528 emit_real_format_for_mode (void)
1529 {
1530   struct mode_data *m;
1531 
1532   /* The entities pointed to by this table are constant, whether
1533      or not the table itself is constant.
1534 
1535      For backward compatibility this table is always writable
1536      (several targets modify it in TARGET_OPTION_OVERRIDE).   FIXME:
1537      convert all said targets to use ADJUST_FORMAT instead.  */
1538 #if 0
1539   print_maybe_const_decl ("const struct real_format *%s",
1540 			  "real_format_for_mode",
1541 			  "MAX_MODE_FLOAT - MIN_MODE_FLOAT + 1",
1542 			  format);
1543 #else
1544   print_decl ("struct real_format *\n", "real_format_for_mode",
1545 	      "MAX_MODE_FLOAT - MIN_MODE_FLOAT + 1 "
1546 	      "+ MAX_MODE_DECIMAL_FLOAT - MIN_MODE_DECIMAL_FLOAT + 1");
1547 #endif
1548 
1549   /* The beginning of the table is entries for float modes.  */
1550   for (m = modes[MODE_FLOAT]; m; m = m->next)
1551     if (!strcmp (m->format, "0"))
1552       tagged_printf ("%s", m->format, m->name);
1553     else
1554       tagged_printf ("&%s", m->format, m->name);
1555 
1556   /* The end of the table is entries for decimal float modes.  */
1557   for (m = modes[MODE_DECIMAL_FLOAT]; m; m = m->next)
1558     if (!strcmp (m->format, "0"))
1559       tagged_printf ("%s", m->format, m->name);
1560     else
1561       tagged_printf ("&%s", m->format, m->name);
1562 
1563   print_closer ();
1564 }
1565 
1566 static void
1567 emit_mode_adjustments (void)
1568 {
1569   struct mode_adjust *a;
1570   struct mode_data *m;
1571 
1572   puts ("\
1573 \nvoid\
1574 \ninit_adjust_machine_modes (void)\
1575 \n{\
1576 \n  size_t s ATTRIBUTE_UNUSED;");
1577 
1578   /* Size adjustments must be propagated to all containing modes.
1579      A size adjustment forces us to recalculate the alignment too.  */
1580   for (a = adj_bytesize; a; a = a->next)
1581     {
1582       printf ("\n  /* %s:%d */\n  s = %s;\n",
1583 	      a->file, a->line, a->adjustment);
1584       printf ("  mode_size[%smode] = s;\n", a->mode->name);
1585       printf ("  mode_unit_size[%smode] = s;\n", a->mode->name);
1586       printf ("  mode_base_align[%smode] = s & (~s + 1);\n",
1587 	      a->mode->name);
1588 
1589       for (m = a->mode->contained; m; m = m->next_cont)
1590 	{
1591 	  switch (m->cl)
1592 	    {
1593 	    case MODE_COMPLEX_INT:
1594 	    case MODE_COMPLEX_FLOAT:
1595 	      printf ("  mode_size[%smode] = 2*s;\n", m->name);
1596 	      printf ("  mode_unit_size[%smode] = s;\n", m->name);
1597 	      printf ("  mode_base_align[%smode] = s & (~s + 1);\n",
1598 		      m->name);
1599 	      break;
1600 
1601 	    case MODE_VECTOR_INT:
1602 	    case MODE_VECTOR_FLOAT:
1603 	    case MODE_VECTOR_FRACT:
1604 	    case MODE_VECTOR_UFRACT:
1605 	    case MODE_VECTOR_ACCUM:
1606 	    case MODE_VECTOR_UACCUM:
1607 	      printf ("  mode_size[%smode] = %d*s;\n",
1608 		      m->name, m->ncomponents);
1609 	      printf ("  mode_unit_size[%smode] = s;\n", m->name);
1610 	      printf ("  mode_base_align[%smode] = (%d*s) & (~(%d*s)+1);\n",
1611 		      m->name, m->ncomponents, m->ncomponents);
1612 	      break;
1613 
1614 	    default:
1615 	      internal_error (
1616 	      "mode %s is neither vector nor complex but contains %s",
1617 	      m->name, a->mode->name);
1618 	      /* NOTREACHED */
1619 	    }
1620 	}
1621     }
1622 
1623   /* Alignment adjustments propagate too.
1624      ??? This may not be the right thing for vector modes.  */
1625   for (a = adj_alignment; a; a = a->next)
1626     {
1627       printf ("\n  /* %s:%d */\n  s = %s;\n",
1628 	      a->file, a->line, a->adjustment);
1629       printf ("  mode_base_align[%smode] = s;\n", a->mode->name);
1630 
1631       for (m = a->mode->contained; m; m = m->next_cont)
1632 	{
1633 	  switch (m->cl)
1634 	    {
1635 	    case MODE_COMPLEX_INT:
1636 	    case MODE_COMPLEX_FLOAT:
1637 	      printf ("  mode_base_align[%smode] = s;\n", m->name);
1638 	      break;
1639 
1640 	    case MODE_VECTOR_INT:
1641 	    case MODE_VECTOR_FLOAT:
1642 	    case MODE_VECTOR_FRACT:
1643 	    case MODE_VECTOR_UFRACT:
1644 	    case MODE_VECTOR_ACCUM:
1645 	    case MODE_VECTOR_UACCUM:
1646 	      printf ("  mode_base_align[%smode] = %d*s;\n",
1647 		      m->name, m->ncomponents);
1648 	      break;
1649 
1650 	    default:
1651 	      internal_error (
1652 	      "mode %s is neither vector nor complex but contains %s",
1653 	      m->name, a->mode->name);
1654 	      /* NOTREACHED */
1655 	    }
1656 	}
1657     }
1658 
1659   /* Ibit adjustments don't have to propagate.  */
1660   for (a = adj_ibit; a; a = a->next)
1661     {
1662       printf ("\n  /* %s:%d */\n  s = %s;\n",
1663 	      a->file, a->line, a->adjustment);
1664       printf ("  mode_ibit[%smode] = s;\n", a->mode->name);
1665     }
1666 
1667   /* Fbit adjustments don't have to propagate.  */
1668   for (a = adj_fbit; a; a = a->next)
1669     {
1670       printf ("\n  /* %s:%d */\n  s = %s;\n",
1671 	      a->file, a->line, a->adjustment);
1672       printf ("  mode_fbit[%smode] = s;\n", a->mode->name);
1673     }
1674 
1675   /* Real mode formats don't have to propagate anywhere.  */
1676   for (a = adj_format; a; a = a->next)
1677     printf ("\n  /* %s:%d */\n  REAL_MODE_FORMAT (%smode) = %s;\n",
1678 	    a->file, a->line, a->mode->name, a->adjustment);
1679 
1680   puts ("}");
1681 }
1682 
1683 /* Emit ibit for all modes.  */
1684 
1685 static void
1686 emit_mode_ibit (void)
1687 {
1688   int c;
1689   struct mode_data *m;
1690 
1691   print_maybe_const_decl ("%sunsigned char",
1692 			  "mode_ibit", "NUM_MACHINE_MODES",
1693 			  ibit);
1694 
1695   for_all_modes (c, m)
1696     tagged_printf ("%u", m->ibit, m->name);
1697 
1698   print_closer ();
1699 }
1700 
1701 /* Emit fbit for all modes.  */
1702 
1703 static void
1704 emit_mode_fbit (void)
1705 {
1706   int c;
1707   struct mode_data *m;
1708 
1709   print_maybe_const_decl ("%sunsigned char",
1710 			  "mode_fbit", "NUM_MACHINE_MODES",
1711 			  fbit);
1712 
1713   for_all_modes (c, m)
1714     tagged_printf ("%u", m->fbit, m->name);
1715 
1716   print_closer ();
1717 }
1718 
1719 /* Emit __intN for all modes.  */
1720 
1721 static void
1722 emit_mode_int_n (void)
1723 {
1724   int c;
1725   struct mode_data *m;
1726   struct mode_data **mode_sort;
1727   int n_modes = 0;
1728   int i, j;
1729 
1730   print_decl ("int_n_data_t", "int_n_data", "");
1731 
1732   n_modes = 0;
1733   for_all_modes (c, m)
1734     if (m->int_n)
1735       n_modes ++;
1736   mode_sort = XALLOCAVEC (struct mode_data *, n_modes);
1737 
1738   n_modes = 0;
1739   for_all_modes (c, m)
1740     if (m->int_n)
1741       mode_sort[n_modes++] = m;
1742 
1743   /* Yes, this is a bubblesort, but there are at most four (and
1744      usually only 1-2) entries to sort.  */
1745   for (i = 0; i<n_modes - 1; i++)
1746     for (j = i + 1; j < n_modes; j++)
1747       if (mode_sort[i]->int_n > mode_sort[j]->int_n)
1748 	std::swap (mode_sort[i], mode_sort[j]);
1749 
1750   for (i = 0; i < n_modes; i ++)
1751     {
1752       m = mode_sort[i];
1753       printf(" {\n");
1754       tagged_printf ("%u", m->int_n, m->name);
1755       printf ("%smode,", m->name);
1756       printf(" },\n");
1757     }
1758 
1759   print_closer ();
1760 }
1761 
1762 
1763 static void
1764 emit_insn_modes_c (void)
1765 {
1766   emit_insn_modes_c_header ();
1767   emit_mode_name ();
1768   emit_mode_class ();
1769   emit_mode_precision ();
1770   emit_mode_size ();
1771   emit_mode_nunits ();
1772   emit_mode_wider ();
1773   emit_mode_complex ();
1774   emit_mode_mask ();
1775   emit_mode_inner ();
1776   emit_mode_unit_size ();
1777   emit_mode_unit_precision ();
1778   emit_mode_base_align ();
1779   emit_class_narrowest_mode ();
1780   emit_real_format_for_mode ();
1781   emit_mode_adjustments ();
1782   emit_mode_ibit ();
1783   emit_mode_fbit ();
1784   emit_mode_int_n ();
1785 }
1786 
1787 static void
1788 emit_min_insn_modes_c (void)
1789 {
1790   emit_min_insn_modes_c_header ();
1791   emit_mode_name ();
1792   emit_mode_class ();
1793   emit_mode_nunits ();
1794   emit_mode_wider ();
1795   emit_mode_inner ();
1796   emit_class_narrowest_mode ();
1797 }
1798 
1799 /* Master control.  */
1800 int
1801 main (int argc, char **argv)
1802 {
1803   bool gen_header = false, gen_min = false;
1804   progname = argv[0];
1805 
1806   if (argc == 1)
1807     ;
1808   else if (argc == 2 && !strcmp (argv[1], "-h"))
1809     gen_header = true;
1810   else if (argc == 2 && !strcmp (argv[1], "-m"))
1811     gen_min = true;
1812   else
1813     {
1814       error ("usage: %s [-h|-m] > file", progname);
1815       return FATAL_EXIT_CODE;
1816     }
1817 
1818   modes_by_name = htab_create_alloc (64, hash_mode, eq_mode, 0, xcalloc, free);
1819 
1820   create_modes ();
1821   complete_all_modes ();
1822 
1823   if (have_error)
1824     return FATAL_EXIT_CODE;
1825 
1826   calc_wider_mode ();
1827 
1828   if (gen_header)
1829     emit_insn_modes_h ();
1830   else if (gen_min)
1831     emit_min_insn_modes_c ();
1832   else
1833     emit_insn_modes_c ();
1834 
1835   if (fflush (stdout) || fclose (stdout))
1836     return FATAL_EXIT_CODE;
1837   return SUCCESS_EXIT_CODE;
1838 }
1839