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