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