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