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