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