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