OSDN Git Service

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