OSDN Git Service

PR c++/54652
[pf3gnuchains/gcc-fork.git] / gcc / genmodes.c
1 /* Generate the machine mode enumeration and associated tables.
2    Copyright (C) 2003, 2004, 2005, 2006, 2007, 2010
3    Free Software Foundation, Inc.
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
11
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3.  If not see
19 <http://www.gnu.org/licenses/>.  */
20
21 #include "bconfig.h"
22 #include "system.h"
23 #include "errors.h"
24 #include "hashtab.h"
25
26 /* enum mode_class is normally defined by machmode.h but we can't
27    include that header here.  */
28 #include "mode-classes.def"
29
30 #define DEF_MODE_CLASS(M) M
31 enum mode_class { MODE_CLASSES, MAX_MODE_CLASS };
32 #undef DEF_MODE_CLASS
33
34 /* Text names of mode classes, for output.  */
35 #define DEF_MODE_CLASS(M) #M
36 static const char *const mode_class_names[MAX_MODE_CLASS] =
37 {
38   MODE_CLASSES
39 };
40 #undef DEF_MODE_CLASS
41 #undef MODE_CLASSES
42
43 #ifdef EXTRA_MODES_FILE
44 # define HAVE_EXTRA_MODES 1
45 #else
46 # define HAVE_EXTRA_MODES 0
47 # define EXTRA_MODES_FILE ""
48 #endif
49
50 /* Data structure for building up what we know about a mode.
51    They're clustered by mode class.  */
52 struct mode_data
53 {
54   struct mode_data *next;       /* next this class - arbitrary order */
55
56   const char *name;             /* printable mode name -- SI, not SImode */
57   enum mode_class cl;           /* this mode class */
58   unsigned int precision;       /* size in bits, equiv to TYPE_PRECISION */
59   unsigned int bytesize;        /* storage size in addressable units */
60   unsigned int ncomponents;     /* number of subunits */
61   unsigned int alignment;       /* mode alignment */
62   const char *format;           /* floating point format - float modes only */
63
64   struct mode_data *component;  /* mode of components */
65   struct mode_data *wider;      /* next wider mode */
66
67   struct mode_data *contained;  /* Pointer to list of modes that have
68                                    this mode as a component.  */
69   struct mode_data *next_cont;  /* Next mode in that list.  */
70
71   const char *file;             /* file and line of definition, */
72   unsigned int line;            /* for error reporting */
73   unsigned int counter;         /* Rank ordering of modes */
74   unsigned int ibit;            /* the number of integral bits */
75   unsigned int fbit;            /* the number of fractional bits */
76 };
77
78 static struct mode_data *modes[MAX_MODE_CLASS];
79 static unsigned int n_modes[MAX_MODE_CLASS];
80 static struct mode_data *void_mode;
81
82 static const struct mode_data blank_mode = {
83   0, "<unknown>", MAX_MODE_CLASS,
84   -1U, -1U, -1U, -1U,
85   0, 0, 0, 0, 0,
86   "<unknown>", 0, 0, 0, 0
87 };
88
89 static htab_t modes_by_name;
90
91 /* Data structure for recording target-specified runtime adjustments
92    to a particular mode.  We support varying the byte size, the
93    alignment, and the floating point format.  */
94 struct mode_adjust
95 {
96   struct mode_adjust *next;
97   struct mode_data *mode;
98   const char *adjustment;
99
100   const char *file;
101   unsigned int line;
102 };
103
104 static struct mode_adjust *adj_bytesize;
105 static struct mode_adjust *adj_alignment;
106 static struct mode_adjust *adj_format;
107 static struct mode_adjust *adj_ibit;
108 static struct mode_adjust *adj_fbit;
109
110 /* Mode class operations.  */
111 static enum mode_class
112 complex_class (enum mode_class c)
113 {
114   switch (c)
115     {
116     case MODE_INT: return MODE_COMPLEX_INT;
117     case MODE_FLOAT: return MODE_COMPLEX_FLOAT;
118     default:
119       error ("no complex class for class %s", mode_class_names[c]);
120       return MODE_RANDOM;
121     }
122 }
123
124 static enum mode_class
125 vector_class (enum mode_class cl)
126 {
127   switch (cl)
128     {
129     case MODE_INT: return MODE_VECTOR_INT;
130     case MODE_FLOAT: return MODE_VECTOR_FLOAT;
131     case MODE_FRACT: return MODE_VECTOR_FRACT;
132     case MODE_UFRACT: return MODE_VECTOR_UFRACT;
133     case MODE_ACCUM: return MODE_VECTOR_ACCUM;
134     case MODE_UACCUM: return MODE_VECTOR_UACCUM;
135     default:
136       error ("no vector class for class %s", mode_class_names[cl]);
137       return MODE_RANDOM;
138     }
139 }
140
141 /* Utility routines.  */
142 static inline struct mode_data *
143 find_mode (const char *name)
144 {
145   struct mode_data key;
146
147   key.name = name;
148   return (struct mode_data *) htab_find (modes_by_name, &key);
149 }
150
151 static struct mode_data *
152 new_mode (enum mode_class cl, const char *name,
153           const char *file, unsigned int line)
154 {
155   struct mode_data *m;
156   static unsigned int count = 0;
157
158   m = find_mode (name);
159   if (m)
160     {
161       error ("%s:%d: duplicate definition of mode \"%s\"",
162              trim_filename (file), line, name);
163       error ("%s:%d: previous definition here", m->file, m->line);
164       return m;
165     }
166
167   m = XNEW (struct mode_data);
168   memcpy (m, &blank_mode, sizeof (struct mode_data));
169   m->cl = cl;
170   m->name = name;
171   if (file)
172     m->file = trim_filename (file);
173   m->line = line;
174   m->counter = count++;
175
176   m->next = modes[cl];
177   modes[cl] = m;
178   n_modes[cl]++;
179
180   *htab_find_slot (modes_by_name, m, INSERT) = m;
181
182   return m;
183 }
184
185 static hashval_t
186 hash_mode (const void *p)
187 {
188   const struct mode_data *m = (const struct mode_data *)p;
189   return htab_hash_string (m->name);
190 }
191
192 static int
193 eq_mode (const void *p, const void *q)
194 {
195   const struct mode_data *a = (const struct mode_data *)p;
196   const struct mode_data *b = (const struct mode_data *)q;
197
198   return !strcmp (a->name, b->name);
199 }
200
201 #define for_all_modes(C, M)                     \
202   for (C = 0; C < MAX_MODE_CLASS; C++)          \
203     for (M = modes[C]; M; M = M->next)
204
205 static void ATTRIBUTE_UNUSED
206 new_adjust (const char *name,
207             struct mode_adjust **category, const char *catname,
208             const char *adjustment,
209             enum mode_class required_class_from,
210             enum mode_class required_class_to,
211             const char *file, unsigned int line)
212 {
213   struct mode_data *mode = find_mode (name);
214   struct mode_adjust *a;
215
216   file = trim_filename (file);
217
218   if (!mode)
219     {
220       error ("%s:%d: no mode \"%s\"", file, line, name);
221       return;
222     }
223
224   if (required_class_from != MODE_RANDOM
225       && (mode->cl < required_class_from || mode->cl > required_class_to))
226     {
227       error ("%s:%d: mode \"%s\" is not among class {%s, %s}",
228              file, line, name, mode_class_names[required_class_from] + 5,
229              mode_class_names[required_class_to] + 5);
230       return;
231     }
232
233   for (a = *category; a; a = a->next)
234     if (a->mode == mode)
235       {
236         error ("%s:%d: mode \"%s\" already has a %s adjustment",
237                file, line, name, catname);
238         error ("%s:%d: previous adjustment here", a->file, a->line);
239         return;
240       }
241
242   a = XNEW (struct mode_adjust);
243   a->mode = mode;
244   a->adjustment = adjustment;
245   a->file = file;
246   a->line = line;
247
248   a->next = *category;
249   *category = a;
250 }
251
252 /* Diagnose failure to meet expectations in a partially filled out
253    mode structure.  */
254 enum requirement { SET, UNSET, OPTIONAL };
255
256 #define validate_field_(mname, fname, req, val, unset, file, line) do { \
257   switch (req)                                                          \
258     {                                                                   \
259     case SET:                                                           \
260       if (val == unset)                                                 \
261         error ("%s:%d: (%s) field %s must be set",                      \
262                file, line, mname, fname);                               \
263       break;                                                            \
264     case UNSET:                                                         \
265       if (val != unset)                                                 \
266         error ("%s:%d: (%s) field %s must not be set",                  \
267                file, line, mname, fname);                               \
268     case OPTIONAL:                                                      \
269       break;                                                            \
270     }                                                                   \
271 } while (0)
272
273 #define validate_field(M, F) \
274   validate_field_(M->name, #F, r_##F, M->F, blank_mode.F, M->file, M->line)
275
276 static void
277 validate_mode (struct mode_data *m,
278                enum requirement r_precision,
279                enum requirement r_bytesize,
280                enum requirement r_component,
281                enum requirement r_ncomponents,
282                enum requirement r_format)
283 {
284   validate_field (m, precision);
285   validate_field (m, bytesize);
286   validate_field (m, component);
287   validate_field (m, ncomponents);
288   validate_field (m, format);
289 }
290 #undef validate_field
291 #undef validate_field_
292
293 /* Given a partially-filled-out mode structure, figure out what we can
294    and fill the rest of it in; die if it isn't enough.  */
295 static void
296 complete_mode (struct mode_data *m)
297 {
298   unsigned int alignment;
299
300   if (!m->name)
301     {
302       error ("%s:%d: mode with no name", m->file, m->line);
303       return;
304     }
305   if (m->cl == MAX_MODE_CLASS)
306     {
307       error ("%s:%d: %smode has no mode class", m->file, m->line, m->name);
308       return;
309     }
310
311   switch (m->cl)
312     {
313     case MODE_RANDOM:
314       /* Nothing more need be said.  */
315       if (!strcmp (m->name, "VOID"))
316         void_mode = m;
317
318       validate_mode (m, UNSET, UNSET, UNSET, UNSET, UNSET);
319
320       m->precision = 0;
321       m->bytesize = 0;
322       m->ncomponents = 0;
323       m->component = 0;
324       break;
325
326     case MODE_CC:
327       /* Again, nothing more need be said.  For historical reasons,
328          the size of a CC mode is four units.  */
329       validate_mode (m, UNSET, UNSET, UNSET, UNSET, UNSET);
330
331       m->bytesize = 4;
332       m->ncomponents = 1;
333       m->component = 0;
334       break;
335
336     case MODE_INT:
337     case MODE_FLOAT:
338     case MODE_DECIMAL_FLOAT:
339     case MODE_FRACT:
340     case MODE_UFRACT:
341     case MODE_ACCUM:
342     case MODE_UACCUM:
343       /* A scalar mode must have a byte size, may have a bit size,
344          and must not have components.   A float mode must have a
345          format.  */
346       validate_mode (m, OPTIONAL, SET, UNSET, UNSET,
347                      (m->cl == MODE_FLOAT || m->cl == MODE_DECIMAL_FLOAT)
348                      ? SET : UNSET);
349
350       m->ncomponents = 1;
351       m->component = 0;
352       break;
353
354     case MODE_PARTIAL_INT:
355       /* A partial integer mode uses ->component to say what the
356          corresponding full-size integer mode is, and may also
357          specify a bit size.  */
358       validate_mode (m, OPTIONAL, UNSET, SET, UNSET, UNSET);
359
360       m->bytesize = m->component->bytesize;
361
362       m->ncomponents = 1;
363       m->component = 0;  /* ??? preserve this */
364       break;
365
366     case MODE_COMPLEX_INT:
367     case MODE_COMPLEX_FLOAT:
368       /* Complex modes should have a component indicated, but no more.  */
369       validate_mode (m, UNSET, UNSET, SET, UNSET, UNSET);
370       m->ncomponents = 2;
371       if (m->component->precision != (unsigned int)-1)
372         m->precision = 2 * m->component->precision;
373       m->bytesize = 2 * m->component->bytesize;
374       break;
375
376     case MODE_VECTOR_INT:
377     case MODE_VECTOR_FLOAT:
378     case MODE_VECTOR_FRACT:
379     case MODE_VECTOR_UFRACT:
380     case MODE_VECTOR_ACCUM:
381     case MODE_VECTOR_UACCUM:
382       /* Vector modes should have a component and a number of components.  */
383       validate_mode (m, UNSET, UNSET, SET, SET, UNSET);
384       if (m->component->precision != (unsigned int)-1)
385         m->precision = m->ncomponents * m->component->precision;
386       m->bytesize = m->ncomponents * m->component->bytesize;
387       break;
388
389     default:
390       gcc_unreachable ();
391     }
392
393   /* If not already specified, the mode alignment defaults to the largest
394      power of two that divides the size of the object.  Complex types are
395      not more aligned than their contents.  */
396   if (m->cl == MODE_COMPLEX_INT || m->cl == MODE_COMPLEX_FLOAT)
397     alignment = m->component->bytesize;
398   else
399     alignment = m->bytesize;
400
401   m->alignment = alignment & (~alignment + 1);
402
403   /* If this mode has components, make the component mode point back
404      to this mode, for the sake of adjustments.  */
405   if (m->component)
406     {
407       m->next_cont = m->component->contained;
408       m->component->contained = m;
409     }
410 }
411
412 static void
413 complete_all_modes (void)
414 {
415   struct mode_data *m;
416   int cl;
417
418   for_all_modes (cl, m)
419     complete_mode (m);
420 }
421
422 /* For each mode in class CLASS, construct a corresponding complex mode.  */
423 #define COMPLEX_MODES(C) make_complex_modes(MODE_##C, __FILE__, __LINE__)
424 static void
425 make_complex_modes (enum mode_class cl,
426                     const char *file, unsigned int line)
427 {
428   struct mode_data *m;
429   struct mode_data *c;
430   char buf[8];
431   enum mode_class cclass = complex_class (cl);
432
433   if (cclass == MODE_RANDOM)
434     return;
435
436   for (m = modes[cl]; m; m = m->next)
437     {
438       /* Skip BImode.  FIXME: BImode probably shouldn't be MODE_INT.  */
439       if (m->precision == 1)
440         continue;
441
442       if (strlen (m->name) >= sizeof buf)
443         {
444           error ("%s:%d:mode name \"%s\" is too long",
445                  m->file, m->line, m->name);
446           continue;
447         }
448
449       /* Float complex modes are named SCmode, etc.
450          Int complex modes are named CSImode, etc.
451          This inconsistency should be eliminated.  */
452       if (cl == MODE_FLOAT)
453         {
454           char *p, *q = 0;
455           strncpy (buf, m->name, sizeof buf);
456           p = strchr (buf, 'F');
457           if (p == 0)
458             q = strchr (buf, 'D');
459           if (p == 0 && q == 0)
460             {
461               error ("%s:%d: float mode \"%s\" has no 'F' or 'D'",
462                      m->file, m->line, m->name);
463               continue;
464             }
465
466           if (p != 0)
467             *p = 'C';
468           else
469             snprintf (buf, sizeof buf, "C%s", m->name);
470         }
471       else
472         snprintf (buf, sizeof buf, "C%s", m->name);
473
474       c = new_mode (cclass, xstrdup (buf), file, line);
475       c->component = m;
476     }
477 }
478
479 /* For all modes in class CL, construct vector modes of width
480    WIDTH, having as many components as necessary.  */
481 #define VECTOR_MODES(C, W) make_vector_modes(MODE_##C, W, __FILE__, __LINE__)
482 static void ATTRIBUTE_UNUSED
483 make_vector_modes (enum mode_class cl, unsigned int width,
484                    const char *file, unsigned int line)
485 {
486   struct mode_data *m;
487   struct mode_data *v;
488   char buf[8];
489   unsigned int ncomponents;
490   enum mode_class vclass = vector_class (cl);
491
492   if (vclass == MODE_RANDOM)
493     return;
494
495   for (m = modes[cl]; m; m = m->next)
496     {
497       /* Do not construct vector modes with only one element, or
498          vector modes where the element size doesn't divide the full
499          size evenly.  */
500       ncomponents = width / m->bytesize;
501       if (ncomponents < 2)
502         continue;
503       if (width % m->bytesize)
504         continue;
505
506       /* Skip QFmode and BImode.  FIXME: this special case should
507          not be necessary.  */
508       if (cl == MODE_FLOAT && m->bytesize == 1)
509         continue;
510       if (cl == MODE_INT && m->precision == 1)
511         continue;
512
513       if ((size_t)snprintf (buf, sizeof buf, "V%u%s", ncomponents, m->name)
514           >= sizeof buf)
515         {
516           error ("%s:%d: mode name \"%s\" is too long",
517                  m->file, m->line, m->name);
518           continue;
519         }
520
521       v = new_mode (vclass, xstrdup (buf), file, line);
522       v->component = m;
523       v->ncomponents = ncomponents;
524     }
525 }
526
527 /* Input.  */
528
529 #define _SPECIAL_MODE(C, N) make_special_mode(MODE_##C, #N, __FILE__, __LINE__)
530 #define RANDOM_MODE(N) _SPECIAL_MODE (RANDOM, N)
531 #define CC_MODE(N) _SPECIAL_MODE (CC, N)
532
533 static void
534 make_special_mode (enum mode_class cl, const char *name,
535                    const char *file, unsigned int line)
536 {
537   new_mode (cl, name, file, line);
538 }
539
540 #define INT_MODE(N, Y) FRACTIONAL_INT_MODE (N, -1U, Y)
541 #define FRACTIONAL_INT_MODE(N, B, Y) \
542   make_int_mode (#N, B, Y, __FILE__, __LINE__)
543
544 static void
545 make_int_mode (const char *name,
546                unsigned int precision, unsigned int bytesize,
547                const char *file, unsigned int line)
548 {
549   struct mode_data *m = new_mode (MODE_INT, name, file, line);
550   m->bytesize = bytesize;
551   m->precision = precision;
552 }
553
554 #define FRACT_MODE(N, Y, F) \
555         make_fixed_point_mode (MODE_FRACT, #N, Y, 0, F, __FILE__, __LINE__)
556
557 #define UFRACT_MODE(N, Y, F) \
558         make_fixed_point_mode (MODE_UFRACT, #N, Y, 0, F, __FILE__, __LINE__)
559
560 #define ACCUM_MODE(N, Y, I, F) \
561         make_fixed_point_mode (MODE_ACCUM, #N, Y, I, F, __FILE__, __LINE__)
562
563 #define UACCUM_MODE(N, Y, I, F) \
564         make_fixed_point_mode (MODE_UACCUM, #N, Y, I, F, __FILE__, __LINE__)
565
566 /* Create a fixed-point mode by setting CL, NAME, BYTESIZE, IBIT, FBIT,
567    FILE, and LINE.  */
568
569 static void
570 make_fixed_point_mode (enum mode_class cl,
571                        const char *name,
572                        unsigned int bytesize,
573                        unsigned int ibit,
574                        unsigned int fbit,
575                        const char *file, unsigned int line)
576 {
577   struct mode_data *m = new_mode (cl, name, file, line);
578   m->bytesize = bytesize;
579   m->ibit = ibit;
580   m->fbit = fbit;
581 }
582
583 #define FLOAT_MODE(N, Y, F)             FRACTIONAL_FLOAT_MODE (N, -1U, Y, F)
584 #define FRACTIONAL_FLOAT_MODE(N, B, Y, F) \
585   make_float_mode (#N, B, Y, #F, __FILE__, __LINE__)
586
587 static void
588 make_float_mode (const char *name,
589                  unsigned int precision, unsigned int bytesize,
590                  const char *format,
591                  const char *file, unsigned int line)
592 {
593   struct mode_data *m = new_mode (MODE_FLOAT, name, file, line);
594   m->bytesize = bytesize;
595   m->precision = precision;
596   m->format = format;
597 }
598
599 #define DECIMAL_FLOAT_MODE(N, Y, F)     \
600         FRACTIONAL_DECIMAL_FLOAT_MODE (N, -1U, Y, F)
601 #define FRACTIONAL_DECIMAL_FLOAT_MODE(N, B, Y, F)       \
602   make_decimal_float_mode (#N, B, Y, #F, __FILE__, __LINE__)
603
604 static void
605 make_decimal_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_DECIMAL_FLOAT, name, file, line);
611   m->bytesize = bytesize;
612   m->precision = precision;
613   m->format = format;
614 }
615
616 #define RESET_FLOAT_FORMAT(N, F) \
617   reset_float_format (#N, #F, __FILE__, __LINE__)
618 static void ATTRIBUTE_UNUSED
619 reset_float_format (const char *name, const char *format,
620                     const char *file, unsigned int line)
621 {
622   struct mode_data *m = find_mode (name);
623   if (!m)
624     {
625       error ("%s:%d: no mode \"%s\"", file, line, name);
626       return;
627     }
628   if (m->cl != MODE_FLOAT && m->cl != MODE_DECIMAL_FLOAT)
629     {
630       error ("%s:%d: mode \"%s\" is not a FLOAT class", file, line, name);
631       return;
632     }
633   m->format = format;
634 }
635
636 /* Partial integer modes are specified by relation to a full integer mode.
637    For now, we do not attempt to narrow down their bit sizes.  */
638 #define PARTIAL_INT_MODE(M) \
639   make_partial_integer_mode (#M, "P" #M, -1U, __FILE__, __LINE__)
640 static void ATTRIBUTE_UNUSED
641 make_partial_integer_mode (const char *base, const char *name,
642                            unsigned int precision,
643                            const char *file, unsigned int line)
644 {
645   struct mode_data *m;
646   struct mode_data *component = find_mode (base);
647   if (!component)
648     {
649       error ("%s:%d: no mode \"%s\"", file, line, name);
650       return;
651     }
652   if (component->cl != MODE_INT)
653     {
654       error ("%s:%d: mode \"%s\" is not class INT", file, line, name);
655       return;
656     }
657
658   m = new_mode (MODE_PARTIAL_INT, name, file, line);
659   m->precision = precision;
660   m->component = component;
661 }
662
663 /* A single vector mode can be specified by naming its component
664    mode and the number of components.  */
665 #define VECTOR_MODE(C, M, N) \
666   make_vector_mode (MODE_##C, #M, N, __FILE__, __LINE__);
667 static void ATTRIBUTE_UNUSED
668 make_vector_mode (enum mode_class bclass,
669                   const char *base,
670                   unsigned int ncomponents,
671                   const char *file, unsigned int line)
672 {
673   struct mode_data *v;
674   enum mode_class vclass = vector_class (bclass);
675   struct mode_data *component = find_mode (base);
676   char namebuf[8];
677
678   if (vclass == MODE_RANDOM)
679     return;
680   if (component == 0)
681     {
682       error ("%s:%d: no mode \"%s\"", file, line, base);
683       return;
684     }
685   if (component->cl != bclass
686       && (component->cl != MODE_PARTIAL_INT
687           || bclass != MODE_INT))
688     {
689       error ("%s:%d: mode \"%s\" is not class %s",
690              file, line, base, mode_class_names[bclass] + 5);
691       return;
692     }
693
694   if ((size_t)snprintf (namebuf, sizeof namebuf, "V%u%s",
695                         ncomponents, base) >= sizeof namebuf)
696     {
697       error ("%s:%d: mode name \"%s\" is too long",
698              file, line, base);
699       return;
700     }
701
702   v = new_mode (vclass, xstrdup (namebuf), file, line);
703   v->ncomponents = ncomponents;
704   v->component = component;
705 }
706
707 /* Adjustability.  */
708 #define _ADD_ADJUST(A, M, X, C1, C2) \
709   new_adjust (#M, &adj_##A, #A, #X, MODE_##C1, MODE_##C2, __FILE__, __LINE__)
710
711 #define ADJUST_BYTESIZE(M, X)  _ADD_ADJUST(bytesize, M, X, RANDOM, RANDOM)
712 #define ADJUST_ALIGNMENT(M, X) _ADD_ADJUST(alignment, M, X, RANDOM, RANDOM)
713 #define ADJUST_FLOAT_FORMAT(M, X)    _ADD_ADJUST(format, M, X, FLOAT, FLOAT)
714 #define ADJUST_IBIT(M, X)  _ADD_ADJUST(ibit, M, X, ACCUM, UACCUM)
715 #define ADJUST_FBIT(M, X)  _ADD_ADJUST(fbit, M, X, FRACT, UACCUM)
716
717 static void
718 create_modes (void)
719 {
720 #include "machmode.def"
721 }
722
723 /* Processing.  */
724
725 /* Sort a list of modes into the order needed for the WIDER field:
726    major sort by precision, minor sort by component precision.
727
728    For instance:
729      QI < HI < SI < DI < TI
730      V4QI < V2HI < V8QI < V4HI < V2SI.
731
732    If the precision is not set, sort by the bytesize.  A mode with
733    precision set gets sorted before a mode without precision set, if
734    they have the same bytesize; this is the right thing because
735    the precision must always be smaller than the bytesize * BITS_PER_UNIT.
736    We don't have to do anything special to get this done -- an unset
737    precision shows up as (unsigned int)-1, i.e. UINT_MAX.  */
738 static int
739 cmp_modes (const void *a, const void *b)
740 {
741   const struct mode_data *const m = *(const struct mode_data *const*)a;
742   const struct mode_data *const n = *(const struct mode_data *const*)b;
743
744   if (m->bytesize > n->bytesize)
745     return 1;
746   else if (m->bytesize < n->bytesize)
747     return -1;
748
749   if (m->precision > n->precision)
750     return 1;
751   else if (m->precision < n->precision)
752     return -1;
753
754   if (!m->component && !n->component)
755     {
756       if (m->counter < n->counter)
757         return -1;
758       else
759         return 1;
760     }
761
762   if (m->component->bytesize > n->component->bytesize)
763     return 1;
764   else if (m->component->bytesize < n->component->bytesize)
765     return -1;
766
767   if (m->component->precision > n->component->precision)
768     return 1;
769   else if (m->component->precision < n->component->precision)
770     return -1;
771
772   if (m->counter < n->counter)
773     return -1;
774   else
775     return 1;
776 }
777
778 static void
779 calc_wider_mode (void)
780 {
781   int c;
782   struct mode_data *m;
783   struct mode_data **sortbuf;
784   unsigned int max_n_modes = 0;
785   unsigned int i, j;
786
787   for (c = 0; c < MAX_MODE_CLASS; c++)
788     max_n_modes = MAX (max_n_modes, n_modes[c]);
789
790   /* Allocate max_n_modes + 1 entries to leave room for the extra null
791      pointer assigned after the qsort call below.  */
792   sortbuf = XALLOCAVEC (struct mode_data *, max_n_modes + 1);
793
794   for (c = 0; c < MAX_MODE_CLASS; c++)
795     {
796       /* "wider" is not meaningful for MODE_RANDOM and MODE_CC.
797          However, we want these in textual order, and we have
798          precisely the reverse.  */
799       if (c == MODE_RANDOM || c == MODE_CC)
800         {
801           struct mode_data *prev, *next;
802
803           for (prev = 0, m = modes[c]; m; m = next)
804             {
805               m->wider = void_mode;
806
807               /* this is nreverse */
808               next = m->next;
809               m->next = prev;
810               prev = m;
811             }
812           modes[c] = prev;
813         }
814       else
815         {
816           if (!modes[c])
817             continue;
818
819           for (i = 0, m = modes[c]; m; i++, m = m->next)
820             sortbuf[i] = m;
821
822           qsort (sortbuf, i, sizeof (struct mode_data *), cmp_modes);
823
824           sortbuf[i] = 0;
825           for (j = 0; j < i; j++)
826             sortbuf[j]->next = sortbuf[j]->wider = sortbuf[j + 1];
827
828           modes[c] = sortbuf[0];
829         }
830     }
831 }
832
833 /* Output routines.  */
834
835 #define tagged_printf(FMT, ARG, TAG) do {               \
836   int count_ = printf ("  " FMT ",", ARG);              \
837   printf ("%*s/* %s */\n", 27 - count_, "", TAG);       \
838 } while (0)
839
840 #define print_decl(TYPE, NAME, ASIZE) \
841   puts ("\nconst " TYPE " " NAME "[" ASIZE "] =\n{");
842
843 #define print_maybe_const_decl(TYPE, NAME, ASIZE, CATEGORY)     \
844   printf ("\n" TYPE " " NAME "[" ASIZE "] = \n{\n",             \
845           adj_##CATEGORY ? "" : "const ")
846
847 #define print_closer() puts ("};")
848
849 static void
850 emit_insn_modes_h (void)
851 {
852   int c;
853   struct mode_data *m, *first, *last;
854
855   printf ("/* Generated automatically from machmode.def%s%s\n",
856            HAVE_EXTRA_MODES ? " and " : "",
857            EXTRA_MODES_FILE);
858
859   puts ("\
860    by genmodes.  */\n\
861 \n\
862 #ifndef GCC_INSN_MODES_H\n\
863 #define GCC_INSN_MODES_H\n\
864 \n\
865 enum machine_mode\n{");
866
867   for (c = 0; c < MAX_MODE_CLASS; c++)
868     for (m = modes[c]; m; m = m->next)
869       {
870         int count_ = printf ("  %smode,", m->name);
871         printf ("%*s/* %s:%d */\n", 27 - count_, "",
872                  trim_filename (m->file), m->line);
873       }
874
875   puts ("  MAX_MACHINE_MODE,\n");
876
877   for (c = 0; c < MAX_MODE_CLASS; c++)
878     {
879       first = modes[c];
880       last = 0;
881       for (m = first; m; last = m, m = m->next)
882         ;
883
884       /* Don't use BImode for MIN_MODE_INT, since otherwise the middle
885          end will try to use it for bitfields in structures and the
886          like, which we do not want.  Only the target md file should
887          generate BImode widgets.  */
888       if (first && first->precision == 1)
889         first = first->next;
890
891       if (first && last)
892         printf ("  MIN_%s = %smode,\n  MAX_%s = %smode,\n\n",
893                  mode_class_names[c], first->name,
894                  mode_class_names[c], last->name);
895       else
896         printf ("  MIN_%s = %smode,\n  MAX_%s = %smode,\n\n",
897                  mode_class_names[c], void_mode->name,
898                  mode_class_names[c], void_mode->name);
899     }
900
901   puts ("\
902   NUM_MACHINE_MODES = MAX_MACHINE_MODE\n\
903 };\n");
904
905   /* I can't think of a better idea, can you?  */
906   printf ("#define CONST_MODE_SIZE%s\n", adj_bytesize ? "" : " const");
907   printf ("#define CONST_MODE_BASE_ALIGN%s\n", adj_alignment ? "" : " const");
908 #if 0 /* disabled for backward compatibility, temporary */
909   printf ("#define CONST_REAL_FORMAT_FOR_MODE%s\n", adj_format ? "" :" const");
910 #endif
911   printf ("#define CONST_MODE_IBIT%s\n", adj_ibit ? "" : " const");
912   printf ("#define CONST_MODE_FBIT%s\n", adj_fbit ? "" : " const");
913   puts ("\
914 \n\
915 #endif /* insn-modes.h */");
916 }
917
918 static void
919 emit_insn_modes_c_header (void)
920 {
921   printf ("/* Generated automatically from machmode.def%s%s\n",
922            HAVE_EXTRA_MODES ? " and " : "",
923            EXTRA_MODES_FILE);
924
925   puts ("\
926    by genmodes.  */\n\
927 \n\
928 #include \"config.h\"\n\
929 #include \"system.h\"\n\
930 #include \"coretypes.h\"\n\
931 #include \"tm.h\"\n\
932 #include \"machmode.h\"\n\
933 #include \"real.h\"");
934 }
935
936 static void
937 emit_min_insn_modes_c_header (void)
938 {
939   printf ("/* Generated automatically from machmode.def%s%s\n",
940            HAVE_EXTRA_MODES ? " and " : "",
941            EXTRA_MODES_FILE);
942
943   puts ("\
944    by genmodes.  */\n\
945 \n\
946 #include \"bconfig.h\"\n\
947 #include \"system.h\"\n\
948 #include \"machmode.h\"");
949 }
950
951 static void
952 emit_mode_name (void)
953 {
954   int c;
955   struct mode_data *m;
956
957   print_decl ("char *const", "mode_name", "NUM_MACHINE_MODES");
958
959   for_all_modes (c, m)
960     printf ("  \"%s\",\n", m->name);
961
962   print_closer ();
963 }
964
965 static void
966 emit_mode_class (void)
967 {
968   int c;
969   struct mode_data *m;
970
971   print_decl ("unsigned char", "mode_class", "NUM_MACHINE_MODES");
972
973   for_all_modes (c, m)
974     tagged_printf ("%s", mode_class_names[m->cl], m->name);
975
976   print_closer ();
977 }
978
979 static void
980 emit_mode_precision (void)
981 {
982   int c;
983   struct mode_data *m;
984
985   print_decl ("unsigned short", "mode_precision", "NUM_MACHINE_MODES");
986
987   for_all_modes (c, m)
988     if (m->precision != (unsigned int)-1)
989       tagged_printf ("%u", m->precision, m->name);
990     else
991       tagged_printf ("%u*BITS_PER_UNIT", m->bytesize, m->name);
992
993   print_closer ();
994 }
995
996 static void
997 emit_mode_size (void)
998 {
999   int c;
1000   struct mode_data *m;
1001
1002   print_maybe_const_decl ("%sunsigned char", "mode_size",
1003                           "NUM_MACHINE_MODES", bytesize);
1004
1005   for_all_modes (c, m)
1006     tagged_printf ("%u", m->bytesize, m->name);
1007
1008   print_closer ();
1009 }
1010
1011 static void
1012 emit_mode_nunits (void)
1013 {
1014   int c;
1015   struct mode_data *m;
1016
1017   print_decl ("unsigned char", "mode_nunits", "NUM_MACHINE_MODES");
1018
1019   for_all_modes (c, m)
1020     tagged_printf ("%u", m->ncomponents, m->name);
1021
1022   print_closer ();
1023 }
1024
1025 static void
1026 emit_mode_wider (void)
1027 {
1028   int c;
1029   struct mode_data *m;
1030
1031   print_decl ("unsigned char", "mode_wider", "NUM_MACHINE_MODES");
1032
1033   for_all_modes (c, m)
1034     tagged_printf ("%smode",
1035                    m->wider ? m->wider->name : void_mode->name,
1036                    m->name);
1037
1038   print_closer ();
1039   print_decl ("unsigned char", "mode_2xwider", "NUM_MACHINE_MODES");
1040
1041   for_all_modes (c, m)
1042     {
1043       struct mode_data * m2;
1044
1045       for (m2 = m;
1046            m2 && m2 != void_mode;
1047            m2 = m2->wider)
1048         {
1049           if (m2->bytesize < 2 * m->bytesize)
1050             continue;
1051           if (m->precision != (unsigned int) -1)
1052             {
1053               if (m2->precision != 2 * m->precision)
1054                 continue;
1055             }
1056           else
1057             {
1058               if (m2->precision != (unsigned int) -1)
1059                 continue;
1060             }
1061
1062           /* For vectors we want twice the number of components,
1063              with the same element type.  */
1064           if (m->cl == MODE_VECTOR_INT
1065               || m->cl == MODE_VECTOR_FLOAT
1066               || m->cl == MODE_VECTOR_FRACT
1067               || m->cl == MODE_VECTOR_UFRACT
1068               || m->cl == MODE_VECTOR_ACCUM
1069               || m->cl == MODE_VECTOR_UACCUM)
1070             {
1071               if (m2->ncomponents != 2 * m->ncomponents)
1072                 continue;
1073               if (m->component != m2->component)
1074                 continue;
1075             }
1076
1077           break;
1078         }
1079       if (m2 == void_mode)
1080         m2 = 0;
1081       tagged_printf ("%smode",
1082                      m2 ? m2->name : void_mode->name,
1083                      m->name);
1084     }
1085
1086   print_closer ();
1087 }
1088
1089 static void
1090 emit_mode_mask (void)
1091 {
1092   int c;
1093   struct mode_data *m;
1094
1095   print_decl ("unsigned HOST_WIDE_INT", "mode_mask_array",
1096               "NUM_MACHINE_MODES");
1097   puts ("\
1098 #define MODE_MASK(m)                          \\\n\
1099   ((m) >= HOST_BITS_PER_WIDE_INT)             \\\n\
1100    ? ~(unsigned HOST_WIDE_INT) 0              \\\n\
1101    : ((unsigned HOST_WIDE_INT) 1 << (m)) - 1\n");
1102
1103   for_all_modes (c, m)
1104     if (m->precision != (unsigned int)-1)
1105       tagged_printf ("MODE_MASK (%u)", m->precision, m->name);
1106     else
1107       tagged_printf ("MODE_MASK (%u*BITS_PER_UNIT)", m->bytesize, m->name);
1108
1109   puts ("#undef MODE_MASK");
1110   print_closer ();
1111 }
1112
1113 static void
1114 emit_mode_inner (void)
1115 {
1116   int c;
1117   struct mode_data *m;
1118
1119   print_decl ("unsigned char", "mode_inner", "NUM_MACHINE_MODES");
1120
1121   for_all_modes (c, m)
1122     tagged_printf ("%smode",
1123                    m->component ? m->component->name : void_mode->name,
1124                    m->name);
1125
1126   print_closer ();
1127 }
1128
1129 static void
1130 emit_mode_base_align (void)
1131 {
1132   int c;
1133   struct mode_data *m;
1134
1135   print_maybe_const_decl ("%sunsigned char",
1136                           "mode_base_align", "NUM_MACHINE_MODES",
1137                           alignment);
1138
1139   for_all_modes (c, m)
1140     tagged_printf ("%u", m->alignment, m->name);
1141
1142   print_closer ();
1143 }
1144
1145 static void
1146 emit_class_narrowest_mode (void)
1147 {
1148   int c;
1149
1150   print_decl ("unsigned char", "class_narrowest_mode", "MAX_MODE_CLASS");
1151
1152   for (c = 0; c < MAX_MODE_CLASS; c++)
1153     /* Bleah, all this to get the comment right for MIN_MODE_INT.  */
1154     tagged_printf ("MIN_%s", mode_class_names[c],
1155                    modes[c]
1156                    ? (modes[c]->precision != 1
1157                       ? modes[c]->name
1158                       : (modes[c]->next
1159                          ? modes[c]->next->name
1160                          : void_mode->name))
1161                    : void_mode->name);
1162
1163   print_closer ();
1164 }
1165
1166 static void
1167 emit_real_format_for_mode (void)
1168 {
1169   struct mode_data *m;
1170
1171   /* The entities pointed to by this table are constant, whether
1172      or not the table itself is constant.
1173
1174      For backward compatibility this table is always writable
1175      (several targets modify it in TARGET_OPTION_OVERRIDE).   FIXME:
1176      convert all said targets to use ADJUST_FORMAT instead.  */
1177 #if 0
1178   print_maybe_const_decl ("const struct real_format *%s",
1179                           "real_format_for_mode",
1180                           "MAX_MODE_FLOAT - MIN_MODE_FLOAT + 1",
1181                           format);
1182 #else
1183   print_decl ("struct real_format *\n", "real_format_for_mode",
1184               "MAX_MODE_FLOAT - MIN_MODE_FLOAT + 1 "
1185               "+ MAX_MODE_DECIMAL_FLOAT - MIN_MODE_DECIMAL_FLOAT + 1");
1186 #endif
1187
1188   /* The beginning of the table is entries for float modes.  */
1189   for (m = modes[MODE_FLOAT]; m; m = m->next)
1190     if (!strcmp (m->format, "0"))
1191       tagged_printf ("%s", m->format, m->name);
1192     else
1193       tagged_printf ("&%s", m->format, m->name);
1194
1195   /* The end of the table is entries for decimal float modes.  */
1196   for (m = modes[MODE_DECIMAL_FLOAT]; m; m = m->next)
1197     if (!strcmp (m->format, "0"))
1198       tagged_printf ("%s", m->format, m->name);
1199     else
1200       tagged_printf ("&%s", m->format, m->name);
1201
1202   print_closer ();
1203 }
1204
1205 static void
1206 emit_mode_adjustments (void)
1207 {
1208   struct mode_adjust *a;
1209   struct mode_data *m;
1210
1211   puts ("\
1212 \nvoid\
1213 \ninit_adjust_machine_modes (void)\
1214 \n{\
1215 \n  size_t s ATTRIBUTE_UNUSED;");
1216
1217   /* Size adjustments must be propagated to all containing modes.
1218      A size adjustment forces us to recalculate the alignment too.  */
1219   for (a = adj_bytesize; a; a = a->next)
1220     {
1221       printf ("\n  /* %s:%d */\n  s = %s;\n",
1222               a->file, a->line, a->adjustment);
1223       printf ("  mode_size[%smode] = s;\n", a->mode->name);
1224       printf ("  mode_base_align[%smode] = s & (~s + 1);\n",
1225               a->mode->name);
1226
1227       for (m = a->mode->contained; m; m = m->next_cont)
1228         {
1229           switch (m->cl)
1230             {
1231             case MODE_COMPLEX_INT:
1232             case MODE_COMPLEX_FLOAT:
1233               printf ("  mode_size[%smode] = 2*s;\n", m->name);
1234               printf ("  mode_base_align[%smode] = s & (~s + 1);\n",
1235                       m->name);
1236               break;
1237
1238             case MODE_VECTOR_INT:
1239             case MODE_VECTOR_FLOAT:
1240             case MODE_VECTOR_FRACT:
1241             case MODE_VECTOR_UFRACT:
1242             case MODE_VECTOR_ACCUM:
1243             case MODE_VECTOR_UACCUM:
1244               printf ("  mode_size[%smode] = %d*s;\n",
1245                       m->name, m->ncomponents);
1246               printf ("  mode_base_align[%smode] = (%d*s) & (~(%d*s)+1);\n",
1247                       m->name, m->ncomponents, m->ncomponents);
1248               break;
1249
1250             default:
1251               internal_error (
1252               "mode %s is neither vector nor complex but contains %s",
1253               m->name, a->mode->name);
1254               /* NOTREACHED */
1255             }
1256         }
1257     }
1258
1259   /* Alignment adjustments propagate too.
1260      ??? This may not be the right thing for vector modes.  */
1261   for (a = adj_alignment; a; a = a->next)
1262     {
1263       printf ("\n  /* %s:%d */\n  s = %s;\n",
1264               a->file, a->line, a->adjustment);
1265       printf ("  mode_base_align[%smode] = s;\n", a->mode->name);
1266
1267       for (m = a->mode->contained; m; m = m->next_cont)
1268         {
1269           switch (m->cl)
1270             {
1271             case MODE_COMPLEX_INT:
1272             case MODE_COMPLEX_FLOAT:
1273               printf ("  mode_base_align[%smode] = s;\n", m->name);
1274               break;
1275
1276             case MODE_VECTOR_INT:
1277             case MODE_VECTOR_FLOAT:
1278             case MODE_VECTOR_FRACT:
1279             case MODE_VECTOR_UFRACT:
1280             case MODE_VECTOR_ACCUM:
1281             case MODE_VECTOR_UACCUM:
1282               printf ("  mode_base_align[%smode] = %d*s;\n",
1283                       m->name, m->ncomponents);
1284               break;
1285
1286             default:
1287               internal_error (
1288               "mode %s is neither vector nor complex but contains %s",
1289               m->name, a->mode->name);
1290               /* NOTREACHED */
1291             }
1292         }
1293     }
1294
1295   /* Ibit adjustments don't have to propagate.  */
1296   for (a = adj_ibit; a; a = a->next)
1297     {
1298       printf ("\n  /* %s:%d */\n  s = %s;\n",
1299               a->file, a->line, a->adjustment);
1300       printf ("  mode_ibit[%smode] = s;\n", a->mode->name);
1301     }
1302
1303   /* Fbit adjustments don't have to propagate.  */
1304   for (a = adj_fbit; a; a = a->next)
1305     {
1306       printf ("\n  /* %s:%d */\n  s = %s;\n",
1307               a->file, a->line, a->adjustment);
1308       printf ("  mode_fbit[%smode] = s;\n", a->mode->name);
1309     }
1310
1311   /* Real mode formats don't have to propagate anywhere.  */
1312   for (a = adj_format; a; a = a->next)
1313     printf ("\n  /* %s:%d */\n  REAL_MODE_FORMAT (%smode) = %s;\n",
1314             a->file, a->line, a->mode->name, a->adjustment);
1315
1316   puts ("}");
1317 }
1318
1319 /* Emit ibit for all modes.  */
1320
1321 static void
1322 emit_mode_ibit (void)
1323 {
1324   int c;
1325   struct mode_data *m;
1326
1327   print_maybe_const_decl ("%sunsigned char",
1328                           "mode_ibit", "NUM_MACHINE_MODES",
1329                           ibit);
1330
1331   for_all_modes (c, m)
1332     tagged_printf ("%u", m->ibit, m->name);
1333
1334   print_closer ();
1335 }
1336
1337 /* Emit fbit for all modes.  */
1338
1339 static void
1340 emit_mode_fbit (void)
1341 {
1342   int c;
1343   struct mode_data *m;
1344
1345   print_maybe_const_decl ("%sunsigned char",
1346                           "mode_fbit", "NUM_MACHINE_MODES",
1347                           fbit);
1348
1349   for_all_modes (c, m)
1350     tagged_printf ("%u", m->fbit, m->name);
1351
1352   print_closer ();
1353 }
1354
1355
1356 static void
1357 emit_insn_modes_c (void)
1358 {
1359   emit_insn_modes_c_header ();
1360   emit_mode_name ();
1361   emit_mode_class ();
1362   emit_mode_precision ();
1363   emit_mode_size ();
1364   emit_mode_nunits ();
1365   emit_mode_wider ();
1366   emit_mode_mask ();
1367   emit_mode_inner ();
1368   emit_mode_base_align ();
1369   emit_class_narrowest_mode ();
1370   emit_real_format_for_mode ();
1371   emit_mode_adjustments ();
1372   emit_mode_ibit ();
1373   emit_mode_fbit ();
1374 }
1375
1376 static void
1377 emit_min_insn_modes_c (void)
1378 {
1379   emit_min_insn_modes_c_header ();
1380   emit_mode_name ();
1381   emit_mode_class ();
1382   emit_mode_wider ();
1383   emit_class_narrowest_mode ();
1384 }
1385
1386 /* Master control.  */
1387 int
1388 main (int argc, char **argv)
1389 {
1390   bool gen_header = false, gen_min = false;
1391   progname = argv[0];
1392
1393   if (argc == 1)
1394     ;
1395   else if (argc == 2 && !strcmp (argv[1], "-h"))
1396     gen_header = true;
1397   else if (argc == 2 && !strcmp (argv[1], "-m"))
1398     gen_min = true;
1399   else
1400     {
1401       error ("usage: %s [-h|-m] > file", progname);
1402       return FATAL_EXIT_CODE;
1403     }
1404
1405   modes_by_name = htab_create_alloc (64, hash_mode, eq_mode, 0, xcalloc, free);
1406
1407   create_modes ();
1408   complete_all_modes ();
1409
1410   if (have_error)
1411     return FATAL_EXIT_CODE;
1412
1413   calc_wider_mode ();
1414
1415   if (gen_header)
1416     emit_insn_modes_h ();
1417   else if (gen_min)
1418     emit_min_insn_modes_c ();
1419   else
1420     emit_insn_modes_c ();
1421
1422   if (fflush (stdout) || fclose (stdout))
1423     return FATAL_EXIT_CODE;
1424   return SUCCESS_EXIT_CODE;
1425 }