OSDN Git Service

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