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