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