Skip -fwhole-program when merging LTO options.
[official-gcc.git] / gcc / genmodes.cc
blob9f0cc9c317dede0729989a868b1c51394b826e19
1 /* Generate the machine mode enumeration and associated tables.
2 Copyright (C) 2003-2022 Free Software Foundation, Inc.
4 This file is part of GCC.
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.
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.
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/>. */
20 #include "bconfig.h"
21 #include "system.h"
22 #include "errors.h"
24 /* enum mode_class is normally defined by machmode.h but we can't
25 include that header here. */
26 #include "mode-classes.def"
28 #define DEF_MODE_CLASS(M) M
29 enum mode_class { MODE_CLASSES, MAX_MODE_CLASS };
30 #undef DEF_MODE_CLASS
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] =
36 MODE_CLASSES
38 #undef DEF_MODE_CLASS
39 #undef MODE_CLASSES
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
48 /* Data structure for building up what we know about a mode.
49 They're clustered by mode class. */
50 struct mode_data
52 struct mode_data *next; /* next this class - arbitrary order */
54 const char *name; /* printable mode name -- SI, not SImode */
55 enum mode_class cl; /* this mode class */
56 unsigned int order; /* top-level sorting order */
57 unsigned int precision; /* size in bits, equiv to TYPE_PRECISION */
58 unsigned int bytesize; /* storage size in addressable units */
59 unsigned int ncomponents; /* number of subunits */
60 unsigned int alignment; /* mode alignment */
61 const char *format; /* floating point format - float modes only */
63 struct mode_data *component; /* mode of components */
64 struct mode_data *wider; /* next wider mode */
66 struct mode_data *contained; /* Pointer to list of modes that have
67 this mode as a component. */
68 struct mode_data *next_cont; /* Next mode in that list. */
70 struct mode_data *complex; /* complex type with mode as component. */
71 const char *file; /* file and line of definition, */
72 unsigned int line; /* for error reporting */
73 unsigned int counter; /* Rank ordering of modes */
74 unsigned int ibit; /* the number of integral bits */
75 unsigned int fbit; /* the number of fractional bits */
76 bool need_nunits_adj; /* true if this mode needs dynamic nunits
77 adjustment */
78 bool need_bytesize_adj; /* true if this mode needs dynamic size
79 adjustment */
80 unsigned int int_n; /* If nonzero, then __int<INT_N> will be defined */
81 bool boolean;
84 static struct mode_data *modes[MAX_MODE_CLASS];
85 static unsigned int n_modes[MAX_MODE_CLASS];
86 static struct mode_data *void_mode;
88 static const struct mode_data blank_mode = {
89 0, "<unknown>", MAX_MODE_CLASS,
90 0, -1U, -1U, -1U, -1U,
91 0, 0, 0, 0, 0, 0,
92 "<unknown>", 0, 0, 0, 0, false, false, 0,
93 false
96 static htab_t modes_by_name;
98 /* Data structure for recording target-specified runtime adjustments
99 to a particular mode. We support varying the byte size, the
100 alignment, and the floating point format. */
101 struct mode_adjust
103 struct mode_adjust *next;
104 struct mode_data *mode;
105 const char *adjustment;
107 const char *file;
108 unsigned int line;
111 static struct mode_adjust *adj_nunits;
112 static struct mode_adjust *adj_bytesize;
113 static struct mode_adjust *adj_alignment;
114 static struct mode_adjust *adj_format;
115 static struct mode_adjust *adj_ibit;
116 static struct mode_adjust *adj_fbit;
118 /* Mode class operations. */
119 static enum mode_class
120 complex_class (enum mode_class c)
122 switch (c)
124 case MODE_INT: return MODE_COMPLEX_INT;
125 case MODE_PARTIAL_INT: return MODE_COMPLEX_INT;
126 case MODE_FLOAT: return MODE_COMPLEX_FLOAT;
127 default:
128 error ("no complex class for class %s", mode_class_names[c]);
129 return MODE_RANDOM;
133 static enum mode_class
134 vector_class (enum mode_class cl)
136 switch (cl)
138 case MODE_INT: return MODE_VECTOR_INT;
139 case MODE_FLOAT: return MODE_VECTOR_FLOAT;
140 case MODE_FRACT: return MODE_VECTOR_FRACT;
141 case MODE_UFRACT: return MODE_VECTOR_UFRACT;
142 case MODE_ACCUM: return MODE_VECTOR_ACCUM;
143 case MODE_UACCUM: return MODE_VECTOR_UACCUM;
144 default:
145 error ("no vector class for class %s", mode_class_names[cl]);
146 return MODE_RANDOM;
150 /* Utility routines. */
151 static inline struct mode_data *
152 find_mode (const char *name)
154 struct mode_data key;
156 key.name = name;
157 return (struct mode_data *) htab_find (modes_by_name, &key);
160 static struct mode_data *
161 new_mode (enum mode_class cl, const char *name,
162 const char *file, unsigned int line)
164 struct mode_data *m;
165 static unsigned int count = 0;
167 m = find_mode (name);
168 if (m)
170 error ("%s:%d: duplicate definition of mode \"%s\"",
171 trim_filename (file), line, name);
172 error ("%s:%d: previous definition here", m->file, m->line);
173 return m;
176 m = XNEW (struct mode_data);
177 memcpy (m, &blank_mode, sizeof (struct mode_data));
178 m->cl = cl;
179 m->name = name;
180 if (file)
181 m->file = trim_filename (file);
182 m->line = line;
183 m->counter = count++;
185 m->next = modes[cl];
186 modes[cl] = m;
187 n_modes[cl]++;
189 *htab_find_slot (modes_by_name, m, INSERT) = m;
191 return m;
194 static hashval_t
195 hash_mode (const void *p)
197 const struct mode_data *m = (const struct mode_data *)p;
198 return htab_hash_string (m->name);
201 static int
202 eq_mode (const void *p, const void *q)
204 const struct mode_data *a = (const struct mode_data *)p;
205 const struct mode_data *b = (const struct mode_data *)q;
207 return !strcmp (a->name, b->name);
210 #define for_all_modes(C, M) \
211 for (C = 0; C < MAX_MODE_CLASS; C++) \
212 for (M = modes[C]; M; M = M->next)
214 static void ATTRIBUTE_UNUSED
215 new_adjust (const char *name,
216 struct mode_adjust **category, const char *catname,
217 const char *adjustment,
218 enum mode_class required_class_from,
219 enum mode_class required_class_to,
220 const char *file, unsigned int line)
222 struct mode_data *mode = find_mode (name);
223 struct mode_adjust *a;
225 file = trim_filename (file);
227 if (!mode)
229 error ("%s:%d: no mode \"%s\"", file, line, name);
230 return;
233 if (required_class_from != MODE_RANDOM
234 && (mode->cl < required_class_from || mode->cl > required_class_to))
236 error ("%s:%d: mode \"%s\" is not among class {%s, %s}",
237 file, line, name, mode_class_names[required_class_from] + 5,
238 mode_class_names[required_class_to] + 5);
239 return;
242 for (a = *category; a; a = a->next)
243 if (a->mode == mode)
245 error ("%s:%d: mode \"%s\" already has a %s adjustment",
246 file, line, name, catname);
247 error ("%s:%d: previous adjustment here", a->file, a->line);
248 return;
251 a = XNEW (struct mode_adjust);
252 a->mode = mode;
253 a->adjustment = adjustment;
254 a->file = file;
255 a->line = line;
257 a->next = *category;
258 *category = a;
261 /* Diagnose failure to meet expectations in a partially filled out
262 mode structure. */
263 enum requirement { SET, UNSET, OPTIONAL };
265 #define validate_field_(mname, fname, req, val, unset, file, line) do { \
266 switch (req) \
268 case SET: \
269 if (val == unset) \
270 error ("%s:%d: (%s) field %s must be set", \
271 file, line, mname, fname); \
272 break; \
273 case UNSET: \
274 if (val != unset) \
275 error ("%s:%d: (%s) field %s must not be set", \
276 file, line, mname, fname); \
277 case OPTIONAL: \
278 break; \
280 } while (0)
282 #define validate_field(M, F) \
283 validate_field_(M->name, #F, r_##F, M->F, blank_mode.F, M->file, M->line)
285 static void
286 validate_mode (struct mode_data *m,
287 enum requirement r_precision,
288 enum requirement r_bytesize,
289 enum requirement r_component,
290 enum requirement r_ncomponents,
291 enum requirement r_format)
293 validate_field (m, precision);
294 validate_field (m, bytesize);
295 validate_field (m, component);
296 validate_field (m, ncomponents);
297 validate_field (m, format);
299 #undef validate_field
300 #undef validate_field_
302 /* Given a partially-filled-out mode structure, figure out what we can
303 and fill the rest of it in; die if it isn't enough. */
304 static void
305 complete_mode (struct mode_data *m)
307 unsigned int alignment;
309 if (!m->name)
311 error ("%s:%d: mode with no name", m->file, m->line);
312 return;
314 if (m->cl == MAX_MODE_CLASS)
316 error ("%s:%d: %smode has no mode class", m->file, m->line, m->name);
317 return;
320 switch (m->cl)
322 case MODE_RANDOM:
323 /* Nothing more need be said. */
324 if (!strcmp (m->name, "VOID"))
325 void_mode = m;
327 validate_mode (m, UNSET, UNSET, UNSET, UNSET, UNSET);
329 m->precision = 0;
330 m->bytesize = 0;
331 m->ncomponents = 0;
332 m->component = 0;
333 break;
335 case MODE_CC:
336 /* Again, nothing more need be said. For historical reasons,
337 the size of a CC mode is four units. */
338 validate_mode (m, UNSET, UNSET, UNSET, UNSET, UNSET);
340 m->bytesize = 4;
341 m->ncomponents = 1;
342 m->component = 0;
343 break;
345 case MODE_INT:
346 case MODE_FLOAT:
347 case MODE_DECIMAL_FLOAT:
348 case MODE_FRACT:
349 case MODE_UFRACT:
350 case MODE_ACCUM:
351 case MODE_UACCUM:
352 /* A scalar mode must have a byte size, may have a bit size,
353 and must not have components. A float mode must have a
354 format. */
355 validate_mode (m, OPTIONAL, SET, UNSET, UNSET,
356 (m->cl == MODE_FLOAT || m->cl == MODE_DECIMAL_FLOAT)
357 ? SET : UNSET);
359 m->ncomponents = 1;
360 m->component = 0;
361 break;
363 case MODE_OPAQUE:
364 /* Opaque modes have size and precision. */
365 validate_mode (m, OPTIONAL, SET, UNSET, UNSET, UNSET);
367 m->ncomponents = 1;
368 m->component = 0;
369 break;
371 case MODE_PARTIAL_INT:
372 /* A partial integer mode uses ->component to say what the
373 corresponding full-size integer mode is, and may also
374 specify a bit size. */
375 validate_mode (m, OPTIONAL, UNSET, SET, UNSET, UNSET);
377 m->bytesize = m->component->bytesize;
379 m->ncomponents = 1;
380 break;
382 case MODE_COMPLEX_INT:
383 case MODE_COMPLEX_FLOAT:
384 /* Complex modes should have a component indicated, but no more. */
385 validate_mode (m, UNSET, UNSET, SET, UNSET, UNSET);
386 m->ncomponents = 2;
387 if (m->component->precision != (unsigned int)-1)
388 m->precision = 2 * m->component->precision;
389 m->bytesize = 2 * m->component->bytesize;
390 break;
392 case MODE_VECTOR_BOOL:
393 validate_mode (m, UNSET, SET, SET, SET, UNSET);
394 break;
396 case MODE_VECTOR_INT:
397 case MODE_VECTOR_FLOAT:
398 case MODE_VECTOR_FRACT:
399 case MODE_VECTOR_UFRACT:
400 case MODE_VECTOR_ACCUM:
401 case MODE_VECTOR_UACCUM:
402 /* Vector modes should have a component and a number of components. */
403 validate_mode (m, UNSET, UNSET, SET, SET, UNSET);
404 if (m->component->precision != (unsigned int)-1)
405 m->precision = m->ncomponents * m->component->precision;
406 m->bytesize = m->ncomponents * m->component->bytesize;
407 break;
409 default:
410 gcc_unreachable ();
413 /* If not already specified, the mode alignment defaults to the largest
414 power of two that divides the size of the object. Complex types are
415 not more aligned than their contents. */
416 if (m->cl == MODE_COMPLEX_INT || m->cl == MODE_COMPLEX_FLOAT)
417 alignment = m->component->bytesize;
418 else
419 alignment = m->bytesize;
421 m->alignment = alignment & (~alignment + 1);
423 /* If this mode has components, make the component mode point back
424 to this mode, for the sake of adjustments. */
425 if (m->component)
427 m->next_cont = m->component->contained;
428 m->component->contained = m;
432 static void
433 complete_all_modes (void)
435 struct mode_data *m;
436 int cl;
438 for_all_modes (cl, m)
439 complete_mode (m);
442 /* For each mode in class CLASS, construct a corresponding complex mode. */
443 #define COMPLEX_MODES(C) make_complex_modes (MODE_##C, __FILE__, __LINE__)
444 static void
445 make_complex_modes (enum mode_class cl,
446 const char *file, unsigned int line)
448 struct mode_data *m;
449 struct mode_data *c;
450 enum mode_class cclass = complex_class (cl);
452 if (cclass == MODE_RANDOM)
453 return;
455 for (m = modes[cl]; m; m = m->next)
457 char *p, *buf;
458 size_t m_len;
460 /* Skip BImode. FIXME: BImode probably shouldn't be MODE_INT. */
461 if (m->boolean)
462 continue;
464 m_len = strlen (m->name);
465 /* The leading "1 +" is in case we prepend a "C" below. */
466 buf = (char *) xmalloc (1 + m_len + 1);
468 /* Float complex modes are named SCmode, etc.
469 Int complex modes are named CSImode, etc.
470 This inconsistency should be eliminated. */
471 p = 0;
472 if (cl == MODE_FLOAT)
474 memcpy (buf, m->name, m_len + 1);
475 p = strchr (buf, 'F');
476 if (p == 0 && strchr (buf, 'D') == 0)
478 error ("%s:%d: float mode \"%s\" has no 'F' or 'D'",
479 m->file, m->line, m->name);
480 free (buf);
481 continue;
484 if (p != 0)
485 *p = 'C';
486 else
488 buf[0] = 'C';
489 memcpy (buf + 1, m->name, m_len + 1);
492 c = new_mode (cclass, buf, file, line);
493 c->component = m;
494 m->complex = c;
498 /* For all modes in class CL, construct vector modes of width WIDTH,
499 having as many components as necessary. ORDER is the sorting order
500 of the mode, with smaller numbers indicating a higher priority. */
501 #define VECTOR_MODES_WITH_PREFIX(PREFIX, C, W, ORDER) \
502 make_vector_modes (MODE_##C, #PREFIX, W, ORDER, __FILE__, __LINE__)
503 #define VECTOR_MODES(C, W) VECTOR_MODES_WITH_PREFIX (V, C, W, 0)
504 static void ATTRIBUTE_UNUSED
505 make_vector_modes (enum mode_class cl, const char *prefix, unsigned int width,
506 unsigned int order, const char *file, unsigned int line)
508 struct mode_data *m;
509 struct mode_data *v;
510 /* Big enough for a 32-bit UINT_MAX plus the text. */
511 char buf[12];
512 unsigned int ncomponents;
513 enum mode_class vclass = vector_class (cl);
515 if (vclass == MODE_RANDOM)
516 return;
518 for (m = modes[cl]; m; m = m->next)
520 /* Do not construct vector modes with only one element, or
521 vector modes where the element size doesn't divide the full
522 size evenly. */
523 ncomponents = width / m->bytesize;
524 if (ncomponents < 2)
525 continue;
526 if (width % m->bytesize)
527 continue;
529 /* Skip QFmode and BImode. FIXME: this special case should
530 not be necessary. */
531 if (cl == MODE_FLOAT && m->bytesize == 1)
532 continue;
533 if (m->boolean)
534 continue;
536 if ((size_t) snprintf (buf, sizeof buf, "%s%u%s", prefix,
537 ncomponents, m->name) >= sizeof buf)
539 error ("%s:%d: mode name \"%s\" is too long",
540 m->file, m->line, m->name);
541 continue;
544 v = new_mode (vclass, xstrdup (buf), file, line);
545 v->order = order;
546 v->component = m;
547 v->ncomponents = ncomponents;
551 /* Create a vector of booleans called NAME with COUNT elements and
552 BYTESIZE bytes in total. */
553 #define VECTOR_BOOL_MODE(NAME, COUNT, COMPONENT, BYTESIZE) \
554 make_vector_bool_mode (#NAME, COUNT, #COMPONENT, BYTESIZE, \
555 __FILE__, __LINE__)
556 static void ATTRIBUTE_UNUSED
557 make_vector_bool_mode (const char *name, unsigned int count,
558 const char *component, unsigned int bytesize,
559 const char *file, unsigned int line)
561 struct mode_data *m = find_mode (component);
562 if (!m)
564 error ("%s:%d: no mode \"%s\"", file, line, component);
565 return;
568 struct mode_data *v = new_mode (MODE_VECTOR_BOOL, name, file, line);
569 v->component = m;
570 v->ncomponents = count;
571 v->bytesize = bytesize;
574 /* Input. */
576 #define _SPECIAL_MODE(C, N) \
577 make_special_mode (MODE_##C, #N, __FILE__, __LINE__)
578 #define RANDOM_MODE(N) _SPECIAL_MODE (RANDOM, N)
579 #define CC_MODE(N) _SPECIAL_MODE (CC, N)
581 static void
582 make_special_mode (enum mode_class cl, const char *name,
583 const char *file, unsigned int line)
585 new_mode (cl, name, file, line);
588 #define INT_MODE(N, Y) FRACTIONAL_INT_MODE (N, -1U, Y)
589 #define FRACTIONAL_INT_MODE(N, B, Y) \
590 make_int_mode (#N, B, Y, __FILE__, __LINE__)
592 static void
593 make_int_mode (const char *name,
594 unsigned int precision, unsigned int bytesize,
595 const char *file, unsigned int line)
597 struct mode_data *m = new_mode (MODE_INT, name, file, line);
598 m->bytesize = bytesize;
599 m->precision = precision;
602 #define BOOL_MODE(N, B, Y) \
603 make_bool_mode (#N, B, Y, __FILE__, __LINE__)
605 static void
606 make_bool_mode (const char *name,
607 unsigned int precision, unsigned int bytesize,
608 const char *file, unsigned int line)
610 struct mode_data *m = new_mode (MODE_INT, name, file, line);
611 m->bytesize = bytesize;
612 m->precision = precision;
613 m->boolean = true;
616 #define OPAQUE_MODE(N, B) \
617 make_opaque_mode (#N, -1U, B, __FILE__, __LINE__)
619 static void ATTRIBUTE_UNUSED
620 make_opaque_mode (const char *name,
621 unsigned int precision,
622 unsigned int bytesize,
623 const char *file, unsigned int line)
625 struct mode_data *m = new_mode (MODE_OPAQUE, name, file, line);
626 m->bytesize = bytesize;
627 m->precision = precision;
630 #define FRACT_MODE(N, Y, F) \
631 make_fixed_point_mode (MODE_FRACT, #N, Y, 0, F, __FILE__, __LINE__)
633 #define UFRACT_MODE(N, Y, F) \
634 make_fixed_point_mode (MODE_UFRACT, #N, Y, 0, F, __FILE__, __LINE__)
636 #define ACCUM_MODE(N, Y, I, F) \
637 make_fixed_point_mode (MODE_ACCUM, #N, Y, I, F, __FILE__, __LINE__)
639 #define UACCUM_MODE(N, Y, I, F) \
640 make_fixed_point_mode (MODE_UACCUM, #N, Y, I, F, __FILE__, __LINE__)
642 /* Create a fixed-point mode by setting CL, NAME, BYTESIZE, IBIT, FBIT,
643 FILE, and LINE. */
645 static void
646 make_fixed_point_mode (enum mode_class cl,
647 const char *name,
648 unsigned int bytesize,
649 unsigned int ibit,
650 unsigned int fbit,
651 const char *file, unsigned int line)
653 struct mode_data *m = new_mode (cl, name, file, line);
654 m->bytesize = bytesize;
655 m->ibit = ibit;
656 m->fbit = fbit;
659 #define FLOAT_MODE(N, Y, F) FRACTIONAL_FLOAT_MODE (N, -1U, Y, F)
660 #define FRACTIONAL_FLOAT_MODE(N, B, Y, F) \
661 make_float_mode (#N, B, Y, #F, __FILE__, __LINE__)
663 static void
664 make_float_mode (const char *name,
665 unsigned int precision, unsigned int bytesize,
666 const char *format,
667 const char *file, unsigned int line)
669 struct mode_data *m = new_mode (MODE_FLOAT, name, file, line);
670 m->bytesize = bytesize;
671 m->precision = precision;
672 m->format = format;
675 #define DECIMAL_FLOAT_MODE(N, Y, F) \
676 FRACTIONAL_DECIMAL_FLOAT_MODE (N, -1U, Y, F)
677 #define FRACTIONAL_DECIMAL_FLOAT_MODE(N, B, Y, F) \
678 make_decimal_float_mode (#N, B, Y, #F, __FILE__, __LINE__)
680 static void
681 make_decimal_float_mode (const char *name,
682 unsigned int precision, unsigned int bytesize,
683 const char *format,
684 const char *file, unsigned int line)
686 struct mode_data *m = new_mode (MODE_DECIMAL_FLOAT, name, file, line);
687 m->bytesize = bytesize;
688 m->precision = precision;
689 m->format = format;
692 #define RESET_FLOAT_FORMAT(N, F) \
693 reset_float_format (#N, #F, __FILE__, __LINE__)
694 static void ATTRIBUTE_UNUSED
695 reset_float_format (const char *name, const char *format,
696 const char *file, unsigned int line)
698 struct mode_data *m = find_mode (name);
699 if (!m)
701 error ("%s:%d: no mode \"%s\"", file, line, name);
702 return;
704 if (m->cl != MODE_FLOAT && m->cl != MODE_DECIMAL_FLOAT)
706 error ("%s:%d: mode \"%s\" is not a FLOAT class", file, line, name);
707 return;
709 m->format = format;
712 /* __intN support. */
713 #define INT_N(M,PREC) \
714 make_int_n (#M, PREC, __FILE__, __LINE__)
715 static void ATTRIBUTE_UNUSED
716 make_int_n (const char *m, int bitsize,
717 const char *file, unsigned int line)
719 struct mode_data *component = find_mode (m);
720 if (!component)
722 error ("%s:%d: no mode \"%s\"", file, line, m);
723 return;
725 if (component->cl != MODE_INT
726 && component->cl != MODE_PARTIAL_INT)
728 error ("%s:%d: mode \"%s\" is not class INT or PARTIAL_INT", file, line, m);
729 return;
731 if (component->int_n != 0)
733 error ("%s:%d: mode \"%s\" already has an intN", file, line, m);
734 return;
737 component->int_n = bitsize;
740 /* Partial integer modes are specified by relation to a full integer
741 mode. */
742 #define PARTIAL_INT_MODE(M,PREC,NAME) \
743 make_partial_integer_mode (#M, #NAME, PREC, __FILE__, __LINE__)
744 static void ATTRIBUTE_UNUSED
745 make_partial_integer_mode (const char *base, const char *name,
746 unsigned int precision,
747 const char *file, unsigned int line)
749 struct mode_data *m;
750 struct mode_data *component = find_mode (base);
751 if (!component)
753 error ("%s:%d: no mode \"%s\"", file, line, name);
754 return;
756 if (component->cl != MODE_INT)
758 error ("%s:%d: mode \"%s\" is not class INT", file, line, name);
759 return;
762 m = new_mode (MODE_PARTIAL_INT, name, file, line);
763 m->precision = precision;
764 m->component = component;
767 /* A single vector mode can be specified by naming its component
768 mode and the number of components. */
769 #define VECTOR_MODE_WITH_PREFIX(PREFIX, C, M, N, ORDER) \
770 make_vector_mode (MODE_##C, #PREFIX, #M, N, ORDER, __FILE__, __LINE__);
771 #define VECTOR_MODE(C, M, N) VECTOR_MODE_WITH_PREFIX(V, C, M, N, 0);
772 static void ATTRIBUTE_UNUSED
773 make_vector_mode (enum mode_class bclass,
774 const char *prefix,
775 const char *base,
776 unsigned int ncomponents,
777 unsigned int order,
778 const char *file, unsigned int line)
780 struct mode_data *v;
781 enum mode_class vclass = vector_class (bclass);
782 struct mode_data *component = find_mode (base);
783 char namebuf[16];
785 if (vclass == MODE_RANDOM)
786 return;
787 if (component == 0)
789 error ("%s:%d: no mode \"%s\"", file, line, base);
790 return;
792 if (component->cl != bclass
793 && (component->cl != MODE_PARTIAL_INT
794 || bclass != MODE_INT))
796 error ("%s:%d: mode \"%s\" is not class %s",
797 file, line, base, mode_class_names[bclass] + 5);
798 return;
801 if ((size_t)snprintf (namebuf, sizeof namebuf, "%s%u%s", prefix,
802 ncomponents, base) >= sizeof namebuf)
804 error ("%s:%d: mode name \"%s\" is too long",
805 file, line, base);
806 return;
809 v = new_mode (vclass, xstrdup (namebuf), file, line);
810 v->order = order;
811 v->ncomponents = ncomponents;
812 v->component = component;
815 /* Adjustability. */
816 #define _ADD_ADJUST(A, M, X, C1, C2) \
817 new_adjust (#M, &adj_##A, #A, #X, MODE_##C1, MODE_##C2, __FILE__, __LINE__)
819 #define ADJUST_NUNITS(M, X) _ADD_ADJUST (nunits, M, X, RANDOM, RANDOM)
820 #define ADJUST_BYTESIZE(M, X) _ADD_ADJUST (bytesize, M, X, RANDOM, RANDOM)
821 #define ADJUST_ALIGNMENT(M, X) _ADD_ADJUST (alignment, M, X, RANDOM, RANDOM)
822 #define ADJUST_FLOAT_FORMAT(M, X) _ADD_ADJUST (format, M, X, FLOAT, FLOAT)
823 #define ADJUST_IBIT(M, X) _ADD_ADJUST (ibit, M, X, ACCUM, UACCUM)
824 #define ADJUST_FBIT(M, X) _ADD_ADJUST (fbit, M, X, FRACT, UACCUM)
826 static int bits_per_unit;
827 static int max_bitsize_mode_any_int;
828 static int max_bitsize_mode_any_mode;
830 static void
831 create_modes (void)
833 #include "machmode.def"
835 /* So put the default value unless the target needs a non standard
836 value. */
837 #ifdef BITS_PER_UNIT
838 bits_per_unit = BITS_PER_UNIT;
839 #else
840 bits_per_unit = 8;
841 #endif
843 #ifdef MAX_BITSIZE_MODE_ANY_INT
844 max_bitsize_mode_any_int = MAX_BITSIZE_MODE_ANY_INT;
845 #else
846 max_bitsize_mode_any_int = 0;
847 #endif
849 #ifdef MAX_BITSIZE_MODE_ANY_MODE
850 max_bitsize_mode_any_mode = MAX_BITSIZE_MODE_ANY_MODE;
851 #else
852 max_bitsize_mode_any_mode = 0;
853 #endif
856 #ifndef NUM_POLY_INT_COEFFS
857 #define NUM_POLY_INT_COEFFS 1
858 #endif
860 /* Processing. */
862 /* Sort a list of modes into the order needed for the WIDER field:
863 major sort by precision, minor sort by component precision.
865 For instance:
866 QI < HI < SI < DI < TI
867 V4QI < V2HI < V8QI < V4HI < V2SI.
869 If the precision is not set, sort by the bytesize. A mode with
870 precision set gets sorted before a mode without precision set, if
871 they have the same bytesize; this is the right thing because
872 the precision must always be smaller than the bytesize * BITS_PER_UNIT.
873 We don't have to do anything special to get this done -- an unset
874 precision shows up as (unsigned int)-1, i.e. UINT_MAX. */
875 static int
876 cmp_modes (const void *a, const void *b)
878 const struct mode_data *const m = *(const struct mode_data *const*)a;
879 const struct mode_data *const n = *(const struct mode_data *const*)b;
881 if (m->order > n->order)
882 return 1;
883 else if (m->order < n->order)
884 return -1;
886 if (m->bytesize > n->bytesize)
887 return 1;
888 else if (m->bytesize < n->bytesize)
889 return -1;
891 if (m->precision > n->precision)
892 return 1;
893 else if (m->precision < n->precision)
894 return -1;
896 if (!m->component && !n->component)
898 if (m->counter < n->counter)
899 return -1;
900 else
901 return 1;
904 if (m->component->bytesize > n->component->bytesize)
905 return 1;
906 else if (m->component->bytesize < n->component->bytesize)
907 return -1;
909 if (m->component->precision > n->component->precision)
910 return 1;
911 else if (m->component->precision < n->component->precision)
912 return -1;
914 if (m->counter < n->counter)
915 return -1;
916 else
917 return 1;
920 static void
921 calc_wider_mode (void)
923 int c;
924 struct mode_data *m;
925 struct mode_data **sortbuf;
926 unsigned int max_n_modes = 0;
927 unsigned int i, j;
929 for (c = 0; c < MAX_MODE_CLASS; c++)
930 max_n_modes = MAX (max_n_modes, n_modes[c]);
932 /* Allocate max_n_modes + 1 entries to leave room for the extra null
933 pointer assigned after the qsort call below. */
934 sortbuf = XALLOCAVEC (struct mode_data *, max_n_modes + 1);
936 for (c = 0; c < MAX_MODE_CLASS; c++)
938 /* "wider" is not meaningful for MODE_RANDOM and MODE_CC.
939 However, we want these in textual order, and we have
940 precisely the reverse. */
941 if (c == MODE_RANDOM || c == MODE_CC)
943 struct mode_data *prev, *next;
945 for (prev = 0, m = modes[c]; m; m = next)
947 m->wider = void_mode;
949 /* this is nreverse */
950 next = m->next;
951 m->next = prev;
952 prev = m;
954 modes[c] = prev;
956 else
958 if (!modes[c])
959 continue;
961 for (i = 0, m = modes[c]; m; i++, m = m->next)
962 sortbuf[i] = m;
964 (qsort) (sortbuf, i, sizeof (struct mode_data *), cmp_modes);
966 sortbuf[i] = 0;
967 for (j = 0; j < i; j++)
969 sortbuf[j]->next = sortbuf[j + 1];
970 if (c == MODE_PARTIAL_INT)
971 sortbuf[j]->wider = sortbuf[j]->component;
972 else
973 sortbuf[j]->wider = sortbuf[j]->next;
976 modes[c] = sortbuf[0];
981 /* Text to add to the constant part of a poly_int_pod initializer in
982 order to fill out te whole structure. */
983 #if NUM_POLY_INT_COEFFS == 1
984 #define ZERO_COEFFS ""
985 #elif NUM_POLY_INT_COEFFS == 2
986 #define ZERO_COEFFS ", 0"
987 #else
988 #error "Unknown value of NUM_POLY_INT_COEFFS"
989 #endif
991 /* Output routines. */
993 #define tagged_printf(FMT, ARG, TAG) do { \
994 int count_ = printf (" " FMT ",", ARG); \
995 printf ("%*s/* %s */\n", 27 - count_, "", TAG); \
996 } while (0)
998 #define print_decl(TYPE, NAME, ASIZE) \
999 puts ("\nconst " TYPE " " NAME "[" ASIZE "] =\n{");
1001 #define print_maybe_const_decl(TYPE, NAME, ASIZE, NEEDS_ADJ) \
1002 printf ("\n" TYPE " " NAME "[" ASIZE "] = \n{\n", \
1003 NEEDS_ADJ ? "" : "const ")
1005 #define print_closer() puts ("};")
1007 /* Compute the max bitsize of some of the classes of integers. It may
1008 be that there are needs for the other integer classes, and this
1009 code is easy to extend. */
1010 static void
1011 emit_max_int (void)
1013 unsigned int max, mmax;
1014 struct mode_data *i;
1015 int j;
1017 puts ("");
1019 printf ("#define BITS_PER_UNIT (%d)\n", bits_per_unit);
1021 if (max_bitsize_mode_any_int == 0)
1023 for (max = 1, i = modes[MODE_INT]; i; i = i->next)
1024 if (max < i->bytesize)
1025 max = i->bytesize;
1026 mmax = max;
1027 for (max = 1, i = modes[MODE_PARTIAL_INT]; i; i = i->next)
1028 if (max < i->bytesize)
1029 max = i->bytesize;
1030 if (max > mmax)
1031 mmax = max;
1032 printf ("#define MAX_BITSIZE_MODE_ANY_INT (%d*BITS_PER_UNIT)\n", mmax);
1034 else
1035 printf ("#define MAX_BITSIZE_MODE_ANY_INT %d\n", max_bitsize_mode_any_int);
1037 if (max_bitsize_mode_any_mode == 0)
1039 mmax = 0;
1040 for (j = 0; j < MAX_MODE_CLASS; j++)
1041 for (i = modes[j]; i; i = i->next)
1042 if (mmax < i->bytesize)
1043 mmax = i->bytesize;
1044 printf ("#define MAX_BITSIZE_MODE_ANY_MODE (%d*BITS_PER_UNIT)\n", mmax);
1046 else
1047 printf ("#define MAX_BITSIZE_MODE_ANY_MODE %d\n",
1048 max_bitsize_mode_any_mode);
1051 /* Emit mode_size_inline routine into insn-modes.h header. */
1052 static void
1053 emit_mode_size_inline (void)
1055 int c;
1056 struct mode_adjust *a;
1057 struct mode_data *m;
1059 /* Size adjustments must be propagated to all containing modes. */
1060 for (a = adj_bytesize; a; a = a->next)
1062 a->mode->need_bytesize_adj = true;
1063 for (m = a->mode->contained; m; m = m->next_cont)
1064 m->need_bytesize_adj = true;
1067 /* Changing the number of units by a factor of X also changes the size
1068 by a factor of X. */
1069 for (mode_adjust *a = adj_nunits; a; a = a->next)
1070 a->mode->need_bytesize_adj = true;
1072 printf ("\
1073 #ifdef __cplusplus\n\
1074 inline __attribute__((__always_inline__))\n\
1075 #else\n\
1076 extern __inline__ __attribute__((__always_inline__, __gnu_inline__))\n\
1077 #endif\n\
1078 poly_uint16\n\
1079 mode_size_inline (machine_mode mode)\n\
1080 {\n\
1081 extern %spoly_uint16_pod mode_size[NUM_MACHINE_MODES];\n\
1082 gcc_assert (mode >= 0 && mode < NUM_MACHINE_MODES);\n\
1083 switch (mode)\n\
1084 {\n", adj_nunits || adj_bytesize ? "" : "const ");
1086 for_all_modes (c, m)
1087 if (!m->need_bytesize_adj)
1088 printf (" case E_%smode: return %u;\n", m->name, m->bytesize);
1090 puts ("\
1091 default: return mode_size[mode];\n\
1092 }\n\
1093 }\n");
1096 /* Emit mode_nunits_inline routine into insn-modes.h header. */
1097 static void
1098 emit_mode_nunits_inline (void)
1100 int c;
1101 struct mode_data *m;
1103 for (mode_adjust *a = adj_nunits; a; a = a->next)
1104 a->mode->need_nunits_adj = true;
1106 printf ("\
1107 #ifdef __cplusplus\n\
1108 inline __attribute__((__always_inline__))\n\
1109 #else\n\
1110 extern __inline__ __attribute__((__always_inline__, __gnu_inline__))\n\
1111 #endif\n\
1112 poly_uint16\n\
1113 mode_nunits_inline (machine_mode mode)\n\
1114 {\n\
1115 extern %spoly_uint16_pod mode_nunits[NUM_MACHINE_MODES];\n\
1116 switch (mode)\n\
1117 {\n", adj_nunits ? "" : "const ");
1119 for_all_modes (c, m)
1120 if (!m->need_nunits_adj)
1121 printf (" case E_%smode: return %u;\n", m->name, m->ncomponents);
1123 puts ("\
1124 default: return mode_nunits[mode];\n\
1125 }\n\
1126 }\n");
1129 /* Emit mode_inner_inline routine into insn-modes.h header. */
1130 static void
1131 emit_mode_inner_inline (void)
1133 int c;
1134 struct mode_data *m;
1136 puts ("\
1137 #ifdef __cplusplus\n\
1138 inline __attribute__((__always_inline__))\n\
1139 #else\n\
1140 extern __inline__ __attribute__((__always_inline__, __gnu_inline__))\n\
1141 #endif\n\
1142 unsigned char\n\
1143 mode_inner_inline (machine_mode mode)\n\
1144 {\n\
1145 extern const unsigned char mode_inner[NUM_MACHINE_MODES];\n\
1146 gcc_assert (mode >= 0 && mode < NUM_MACHINE_MODES);\n\
1147 switch (mode)\n\
1148 {");
1150 for_all_modes (c, m)
1151 printf (" case E_%smode: return E_%smode;\n", m->name,
1152 c != MODE_PARTIAL_INT && m->component
1153 ? m->component->name : m->name);
1155 puts ("\
1156 default: return mode_inner[mode];\n\
1157 }\n\
1158 }\n");
1161 /* Emit mode_unit_size_inline routine into insn-modes.h header. */
1162 static void
1163 emit_mode_unit_size_inline (void)
1165 int c;
1166 struct mode_data *m;
1168 puts ("\
1169 #ifdef __cplusplus\n\
1170 inline __attribute__((__always_inline__))\n\
1171 #else\n\
1172 extern __inline__ __attribute__((__always_inline__, __gnu_inline__))\n\
1173 #endif\n\
1174 unsigned char\n\
1175 mode_unit_size_inline (machine_mode mode)\n\
1176 {\n\
1177 extern CONST_MODE_UNIT_SIZE unsigned char mode_unit_size[NUM_MACHINE_MODES];\
1179 gcc_assert (mode >= 0 && mode < NUM_MACHINE_MODES);\n\
1180 switch (mode)\n\
1181 {");
1183 for_all_modes (c, m)
1185 const char *name = m->name;
1186 struct mode_data *m2 = m;
1187 if (c != MODE_PARTIAL_INT && m2->component)
1188 m2 = m2->component;
1189 if (!m2->need_bytesize_adj)
1190 printf (" case E_%smode: return %u;\n", name, m2->bytesize);
1193 puts ("\
1194 default: return mode_unit_size[mode];\n\
1195 }\n\
1196 }\n");
1199 /* Emit mode_unit_precision_inline routine into insn-modes.h header. */
1200 static void
1201 emit_mode_unit_precision_inline (void)
1203 int c;
1204 struct mode_data *m;
1206 puts ("\
1207 #ifdef __cplusplus\n\
1208 inline __attribute__((__always_inline__))\n\
1209 #else\n\
1210 extern __inline__ __attribute__((__always_inline__, __gnu_inline__))\n\
1211 #endif\n\
1212 unsigned short\n\
1213 mode_unit_precision_inline (machine_mode mode)\n\
1214 {\n\
1215 extern const unsigned short mode_unit_precision[NUM_MACHINE_MODES];\n\
1216 gcc_assert (mode >= 0 && mode < NUM_MACHINE_MODES);\n\
1217 switch (mode)\n\
1218 {");
1220 for_all_modes (c, m)
1222 struct mode_data *m2
1223 = (c != MODE_PARTIAL_INT && m->component) ? m->component : m;
1224 if (m2->precision != (unsigned int)-1)
1225 printf (" case E_%smode: return %u;\n", m->name, m2->precision);
1226 else
1227 printf (" case E_%smode: return %u*BITS_PER_UNIT;\n",
1228 m->name, m2->bytesize);
1231 puts ("\
1232 default: return mode_unit_precision[mode];\n\
1233 }\n\
1234 }\n");
1237 /* Return the best machine mode class for MODE, or null if machine_mode
1238 should be used. */
1240 static const char *
1241 get_mode_class (struct mode_data *mode)
1243 switch (mode->cl)
1245 case MODE_INT:
1246 case MODE_PARTIAL_INT:
1247 return "scalar_int_mode";
1249 case MODE_FRACT:
1250 case MODE_UFRACT:
1251 case MODE_ACCUM:
1252 case MODE_UACCUM:
1253 return "scalar_mode";
1255 case MODE_FLOAT:
1256 case MODE_DECIMAL_FLOAT:
1257 return "scalar_float_mode";
1259 case MODE_COMPLEX_INT:
1260 case MODE_COMPLEX_FLOAT:
1261 return "complex_mode";
1263 default:
1264 return NULL;
1268 static void
1269 emit_insn_modes_h (void)
1271 int c;
1272 struct mode_data *m, *first, *last;
1273 int n_int_n_ents = 0;
1275 printf ("/* Generated automatically from machmode.def%s%s\n",
1276 HAVE_EXTRA_MODES ? " and " : "",
1277 EXTRA_MODES_FILE);
1279 puts ("\
1280 by genmodes. */\n\
1282 #ifndef GCC_INSN_MODES_H\n\
1283 #define GCC_INSN_MODES_H\n\
1285 enum machine_mode\n{");
1287 for (c = 0; c < MAX_MODE_CLASS; c++)
1288 for (m = modes[c]; m; m = m->next)
1290 int count_ = printf (" E_%smode,", m->name);
1291 printf ("%*s/* %s:%d */\n", 27 - count_, "",
1292 trim_filename (m->file), m->line);
1293 printf ("#define HAVE_%smode\n", m->name);
1294 printf ("#ifdef USE_ENUM_MODES\n");
1295 printf ("#define %smode E_%smode\n", m->name, m->name);
1296 printf ("#else\n");
1297 if (const char *mode_class = get_mode_class (m))
1298 printf ("#define %smode (%s ((%s::from_int) E_%smode))\n",
1299 m->name, mode_class, mode_class, m->name);
1300 else
1301 printf ("#define %smode ((void) 0, E_%smode)\n",
1302 m->name, m->name);
1303 printf ("#endif\n");
1306 puts (" MAX_MACHINE_MODE,\n");
1308 for (c = 0; c < MAX_MODE_CLASS; c++)
1310 first = modes[c];
1311 last = 0;
1312 for (m = first; m; last = m, m = m->next)
1315 /* Don't use BImode for MIN_MODE_INT, since otherwise the middle
1316 end will try to use it for bitfields in structures and the
1317 like, which we do not want. Only the target md file should
1318 generate BImode widgets. Since some targets such as ARM/MVE
1319 define boolean modes with multiple bits, handle those too. */
1320 if (first && first->boolean)
1322 struct mode_data *last_bool = first;
1323 printf (" MIN_MODE_BOOL = E_%smode,\n", first->name);
1325 while (first && first->boolean)
1327 last_bool = first;
1328 first = first->next;
1331 printf (" MAX_MODE_BOOL = E_%smode,\n\n", last_bool->name);
1334 if (first && last)
1335 printf (" MIN_%s = E_%smode,\n MAX_%s = E_%smode,\n\n",
1336 mode_class_names[c], first->name,
1337 mode_class_names[c], last->name);
1338 else
1339 printf (" MIN_%s = E_%smode,\n MAX_%s = E_%smode,\n\n",
1340 mode_class_names[c], void_mode->name,
1341 mode_class_names[c], void_mode->name);
1344 puts ("\
1345 NUM_MACHINE_MODES = MAX_MACHINE_MODE\n\
1346 };\n");
1348 /* Define a NUM_* macro for each mode class, giving the number of modes
1349 in the class. */
1350 for (c = 0; c < MAX_MODE_CLASS; c++)
1352 printf ("#define NUM_%s ", mode_class_names[c]);
1353 if (modes[c])
1354 printf ("(MAX_%s - MIN_%s + 1)\n", mode_class_names[c],
1355 mode_class_names[c]);
1356 else
1357 printf ("0\n");
1359 printf ("\n");
1361 /* I can't think of a better idea, can you? */
1362 printf ("#define CONST_MODE_NUNITS%s\n", adj_nunits ? "" : " const");
1363 printf ("#define CONST_MODE_PRECISION%s\n", adj_nunits ? "" : " const");
1364 printf ("#define CONST_MODE_SIZE%s\n",
1365 adj_bytesize || adj_nunits ? "" : " const");
1366 printf ("#define CONST_MODE_UNIT_SIZE%s\n", adj_bytesize ? "" : " const");
1367 printf ("#define CONST_MODE_BASE_ALIGN%s\n", adj_alignment ? "" : " const");
1368 #if 0 /* disabled for backward compatibility, temporary */
1369 printf ("#define CONST_REAL_FORMAT_FOR_MODE%s\n", adj_format ? "" :" const");
1370 #endif
1371 printf ("#define CONST_MODE_IBIT%s\n", adj_ibit ? "" : " const");
1372 printf ("#define CONST_MODE_FBIT%s\n", adj_fbit ? "" : " const");
1373 printf ("#define CONST_MODE_MASK%s\n", adj_nunits ? "" : " const");
1374 emit_max_int ();
1376 for_all_modes (c, m)
1377 if (m->int_n)
1378 n_int_n_ents ++;
1380 printf ("#define NUM_INT_N_ENTS %d\n", n_int_n_ents);
1382 printf ("#define NUM_POLY_INT_COEFFS %d\n", NUM_POLY_INT_COEFFS);
1384 puts ("\
1386 #endif /* insn-modes.h */");
1389 static void
1390 emit_insn_modes_inline_h (void)
1392 printf ("/* Generated automatically from machmode.def%s%s\n",
1393 HAVE_EXTRA_MODES ? " and " : "",
1394 EXTRA_MODES_FILE);
1396 puts ("\
1397 by genmodes. */\n\
1399 #ifndef GCC_INSN_MODES_INLINE_H\n\
1400 #define GCC_INSN_MODES_INLINE_H");
1402 puts ("\n#if !defined (USED_FOR_TARGET) && GCC_VERSION >= 4001\n");
1403 emit_mode_size_inline ();
1404 emit_mode_nunits_inline ();
1405 emit_mode_inner_inline ();
1406 emit_mode_unit_size_inline ();
1407 emit_mode_unit_precision_inline ();
1408 puts ("#endif /* GCC_VERSION >= 4001 */");
1410 puts ("\
1412 #endif /* insn-modes-inline.h */");
1415 static void
1416 emit_insn_modes_c_header (void)
1418 printf ("/* Generated automatically from machmode.def%s%s\n",
1419 HAVE_EXTRA_MODES ? " and " : "",
1420 EXTRA_MODES_FILE);
1422 puts ("\
1423 by genmodes. */\n\
1425 #include \"config.h\"\n\
1426 #include \"system.h\"\n\
1427 #include \"coretypes.h\"\n\
1428 #include \"tm.h\"\n\
1429 #include \"real.h\"");
1432 static void
1433 emit_min_insn_modes_c_header (void)
1435 printf ("/* Generated automatically from machmode.def%s%s\n",
1436 HAVE_EXTRA_MODES ? " and " : "",
1437 EXTRA_MODES_FILE);
1439 puts ("\
1440 by genmodes. */\n\
1442 #include \"bconfig.h\"\n\
1443 #include \"system.h\"\n\
1444 #include \"coretypes.h\"");
1447 static void
1448 emit_mode_name (void)
1450 int c;
1451 struct mode_data *m;
1453 print_decl ("char *const", "mode_name", "NUM_MACHINE_MODES");
1455 for_all_modes (c, m)
1456 printf (" \"%s\",\n", m->name);
1458 print_closer ();
1461 static void
1462 emit_mode_class (void)
1464 int c;
1465 struct mode_data *m;
1467 print_decl ("unsigned char", "mode_class", "NUM_MACHINE_MODES");
1469 for_all_modes (c, m)
1470 tagged_printf ("%s", mode_class_names[m->cl], m->name);
1472 print_closer ();
1475 static void
1476 emit_mode_precision (void)
1478 int c;
1479 struct mode_data *m;
1481 print_maybe_const_decl ("%spoly_uint16_pod", "mode_precision",
1482 "NUM_MACHINE_MODES", adj_nunits);
1484 for_all_modes (c, m)
1485 if (m->precision != (unsigned int)-1)
1486 tagged_printf ("{ %u" ZERO_COEFFS " }", m->precision, m->name);
1487 else
1488 tagged_printf ("{ %u * BITS_PER_UNIT" ZERO_COEFFS " }",
1489 m->bytesize, m->name);
1491 print_closer ();
1494 static void
1495 emit_mode_size (void)
1497 int c;
1498 struct mode_data *m;
1500 print_maybe_const_decl ("%spoly_uint16_pod", "mode_size",
1501 "NUM_MACHINE_MODES", adj_nunits || adj_bytesize);
1503 for_all_modes (c, m)
1504 tagged_printf ("{ %u" ZERO_COEFFS " }", m->bytesize, m->name);
1506 print_closer ();
1509 static void
1510 emit_mode_nunits (void)
1512 int c;
1513 struct mode_data *m;
1515 print_maybe_const_decl ("%spoly_uint16_pod", "mode_nunits",
1516 "NUM_MACHINE_MODES", adj_nunits);
1518 for_all_modes (c, m)
1519 tagged_printf ("{ %u" ZERO_COEFFS " }", m->ncomponents, m->name);
1521 print_closer ();
1524 static void
1525 emit_mode_wider (void)
1527 int c;
1528 struct mode_data *m;
1530 print_decl ("unsigned char", "mode_next", "NUM_MACHINE_MODES");
1532 for_all_modes (c, m)
1533 tagged_printf ("E_%smode",
1534 m->wider ? m->wider->name : void_mode->name,
1535 m->name);
1537 print_closer ();
1538 print_decl ("unsigned char", "mode_wider", "NUM_MACHINE_MODES");
1540 for_all_modes (c, m)
1542 struct mode_data *m2 = 0;
1544 if (m->cl == MODE_INT
1545 || m->cl == MODE_PARTIAL_INT
1546 || m->cl == MODE_FLOAT
1547 || m->cl == MODE_DECIMAL_FLOAT
1548 || m->cl == MODE_COMPLEX_FLOAT
1549 || m->cl == MODE_FRACT
1550 || m->cl == MODE_UFRACT
1551 || m->cl == MODE_ACCUM
1552 || m->cl == MODE_UACCUM)
1553 for (m2 = m->wider; m2 && m2 != void_mode; m2 = m2->wider)
1555 if (m2->bytesize == m->bytesize
1556 && m2->precision == m->precision)
1557 continue;
1558 break;
1561 if (m2 == void_mode)
1562 m2 = 0;
1563 tagged_printf ("E_%smode",
1564 m2 ? m2->name : void_mode->name,
1565 m->name);
1568 print_closer ();
1569 print_decl ("unsigned char", "mode_2xwider", "NUM_MACHINE_MODES");
1571 for_all_modes (c, m)
1573 struct mode_data * m2;
1575 for (m2 = m;
1576 m2 && m2 != void_mode;
1577 m2 = m2->wider)
1579 if (m2->bytesize < 2 * m->bytesize)
1580 continue;
1581 if (m->precision != (unsigned int) -1)
1583 if (m2->precision != 2 * m->precision)
1584 continue;
1586 else
1588 if (m2->precision != (unsigned int) -1)
1589 continue;
1592 /* For vectors we want twice the number of components,
1593 with the same element type. */
1594 if (m->cl == MODE_VECTOR_BOOL
1595 || m->cl == MODE_VECTOR_INT
1596 || m->cl == MODE_VECTOR_FLOAT
1597 || m->cl == MODE_VECTOR_FRACT
1598 || m->cl == MODE_VECTOR_UFRACT
1599 || m->cl == MODE_VECTOR_ACCUM
1600 || m->cl == MODE_VECTOR_UACCUM)
1602 if (m2->ncomponents != 2 * m->ncomponents)
1603 continue;
1604 if (m->component != m2->component)
1605 continue;
1608 break;
1610 if (m2 == void_mode)
1611 m2 = 0;
1612 tagged_printf ("E_%smode",
1613 m2 ? m2->name : void_mode->name,
1614 m->name);
1617 print_closer ();
1620 static void
1621 emit_mode_complex (void)
1623 int c;
1624 struct mode_data *m;
1626 print_decl ("unsigned char", "mode_complex", "NUM_MACHINE_MODES");
1628 for_all_modes (c, m)
1629 tagged_printf ("E_%smode",
1630 m->complex ? m->complex->name : void_mode->name,
1631 m->name);
1633 print_closer ();
1636 static void
1637 emit_mode_mask (void)
1639 int c;
1640 struct mode_data *m;
1642 print_maybe_const_decl ("%sunsigned HOST_WIDE_INT", "mode_mask_array",
1643 "NUM_MACHINE_MODES", adj_nunits);
1644 puts ("\
1645 #define MODE_MASK(m) \\\n\
1646 ((m) >= HOST_BITS_PER_WIDE_INT) \\\n\
1647 ? HOST_WIDE_INT_M1U \\\n\
1648 : (HOST_WIDE_INT_1U << (m)) - 1\n");
1650 for_all_modes (c, m)
1651 if (m->precision != (unsigned int)-1)
1652 tagged_printf ("MODE_MASK (%u)", m->precision, m->name);
1653 else
1654 tagged_printf ("MODE_MASK (%u*BITS_PER_UNIT)", m->bytesize, m->name);
1656 puts ("#undef MODE_MASK");
1657 print_closer ();
1660 static void
1661 emit_mode_inner (void)
1663 int c;
1664 struct mode_data *m;
1666 print_decl ("unsigned char", "mode_inner", "NUM_MACHINE_MODES");
1668 for_all_modes (c, m)
1669 tagged_printf ("E_%smode",
1670 c != MODE_PARTIAL_INT && m->component
1671 ? m->component->name : m->name,
1672 m->name);
1674 print_closer ();
1677 /* Emit mode_unit_size array into insn-modes.cc file. */
1678 static void
1679 emit_mode_unit_size (void)
1681 int c;
1682 struct mode_data *m;
1684 print_maybe_const_decl ("%sunsigned char", "mode_unit_size",
1685 "NUM_MACHINE_MODES", adj_bytesize);
1687 for_all_modes (c, m)
1688 tagged_printf ("%u",
1689 c != MODE_PARTIAL_INT && m->component
1690 ? m->component->bytesize : m->bytesize, m->name);
1692 print_closer ();
1695 /* Emit mode_unit_precision array into insn-modes.cc file. */
1696 static void
1697 emit_mode_unit_precision (void)
1699 int c;
1700 struct mode_data *m;
1702 print_decl ("unsigned short", "mode_unit_precision", "NUM_MACHINE_MODES");
1704 for_all_modes (c, m)
1706 struct mode_data *m2 = (c != MODE_PARTIAL_INT && m->component) ?
1707 m->component : m;
1708 if (m2->precision != (unsigned int)-1)
1709 tagged_printf ("%u", m2->precision, m->name);
1710 else
1711 tagged_printf ("%u*BITS_PER_UNIT", m2->bytesize, m->name);
1714 print_closer ();
1718 static void
1719 emit_mode_base_align (void)
1721 int c;
1722 struct mode_data *m;
1724 print_maybe_const_decl ("%sunsigned short",
1725 "mode_base_align", "NUM_MACHINE_MODES",
1726 adj_alignment);
1728 for_all_modes (c, m)
1729 tagged_printf ("%u", m->alignment, m->name);
1731 print_closer ();
1734 static void
1735 emit_class_narrowest_mode (void)
1737 int c;
1739 print_decl ("unsigned char", "class_narrowest_mode", "MAX_MODE_CLASS");
1741 for (c = 0; c < MAX_MODE_CLASS; c++)
1743 /* Bleah, all this to get the comment right for MIN_MODE_INT. */
1744 struct mode_data *m = modes[c];
1745 while (m && m->boolean)
1746 m = m->next;
1747 const char *comment_name = (m ? m : void_mode)->name;
1749 tagged_printf ("MIN_%s", mode_class_names[c], comment_name);
1752 print_closer ();
1755 static void
1756 emit_real_format_for_mode (void)
1758 struct mode_data *m;
1760 /* The entities pointed to by this table are constant, whether
1761 or not the table itself is constant.
1763 For backward compatibility this table is always writable
1764 (several targets modify it in TARGET_OPTION_OVERRIDE). FIXME:
1765 convert all said targets to use ADJUST_FORMAT instead. */
1766 #if 0
1767 print_maybe_const_decl ("const struct real_format *%s",
1768 "real_format_for_mode",
1769 "MAX_MODE_FLOAT - MIN_MODE_FLOAT + 1",
1770 format);
1771 #else
1772 print_decl ("struct real_format *\n", "real_format_for_mode",
1773 "MAX_MODE_FLOAT - MIN_MODE_FLOAT + 1 "
1774 "+ MAX_MODE_DECIMAL_FLOAT - MIN_MODE_DECIMAL_FLOAT + 1");
1775 #endif
1777 /* The beginning of the table is entries for float modes. */
1778 for (m = modes[MODE_FLOAT]; m; m = m->next)
1779 if (!strcmp (m->format, "0"))
1780 tagged_printf ("%s", m->format, m->name);
1781 else
1782 tagged_printf ("&%s", m->format, m->name);
1784 /* The end of the table is entries for decimal float modes. */
1785 for (m = modes[MODE_DECIMAL_FLOAT]; m; m = m->next)
1786 if (!strcmp (m->format, "0"))
1787 tagged_printf ("%s", m->format, m->name);
1788 else
1789 tagged_printf ("&%s", m->format, m->name);
1791 print_closer ();
1794 static void
1795 emit_mode_adjustments (void)
1797 struct mode_adjust *a;
1798 struct mode_data *m;
1800 if (adj_nunits)
1801 printf ("\n"
1802 "void\n"
1803 "adjust_mode_mask (machine_mode mode)\n"
1804 "{\n"
1805 " unsigned int precision;\n"
1806 " if (GET_MODE_PRECISION (mode).is_constant (&precision)\n"
1807 " && precision < HOST_BITS_PER_WIDE_INT)\n"
1808 " mode_mask_array[mode] = (HOST_WIDE_INT_1U << precision) - 1;"
1809 "\n"
1810 " else\n"
1811 " mode_mask_array[mode] = HOST_WIDE_INT_M1U;\n"
1812 "}\n");
1814 puts ("\
1815 \nvoid\
1816 \ninit_adjust_machine_modes (void)\
1817 \n{\
1818 \n poly_uint16 ps ATTRIBUTE_UNUSED;\n\
1819 size_t s ATTRIBUTE_UNUSED;");
1821 for (a = adj_nunits; a; a = a->next)
1823 m = a->mode;
1824 printf ("\n"
1825 " {\n"
1826 " /* %s:%d */\n ps = %s;\n",
1827 a->file, a->line, a->adjustment);
1828 printf (" int old_factor = vector_element_size"
1829 " (mode_precision[E_%smode], mode_nunits[E_%smode]);\n",
1830 m->name, m->name);
1831 printf (" mode_precision[E_%smode] = ps * old_factor;\n", m->name);
1832 printf (" mode_size[E_%smode] = exact_div (mode_precision[E_%smode],"
1833 " BITS_PER_UNIT);\n", m->name, m->name);
1834 printf (" mode_nunits[E_%smode] = ps;\n", m->name);
1835 printf (" adjust_mode_mask (E_%smode);\n", m->name);
1836 printf (" }\n");
1839 /* Size adjustments must be propagated to all containing modes.
1840 A size adjustment forces us to recalculate the alignment too. */
1841 for (a = adj_bytesize; a; a = a->next)
1843 printf ("\n /* %s:%d */\n", a->file, a->line);
1844 switch (a->mode->cl)
1846 case MODE_VECTOR_BOOL:
1847 case MODE_VECTOR_INT:
1848 case MODE_VECTOR_FLOAT:
1849 case MODE_VECTOR_FRACT:
1850 case MODE_VECTOR_UFRACT:
1851 case MODE_VECTOR_ACCUM:
1852 case MODE_VECTOR_UACCUM:
1853 printf (" ps = %s;\n", a->adjustment);
1854 printf (" s = mode_unit_size[E_%smode];\n", a->mode->name);
1855 break;
1857 default:
1858 printf (" ps = s = %s;\n", a->adjustment);
1859 printf (" mode_unit_size[E_%smode] = s;\n", a->mode->name);
1860 break;
1862 printf (" mode_size[E_%smode] = ps;\n", a->mode->name);
1863 printf (" mode_base_align[E_%smode] = known_alignment (ps);\n",
1864 a->mode->name);
1866 for (m = a->mode->contained; m; m = m->next_cont)
1868 switch (m->cl)
1870 case MODE_COMPLEX_INT:
1871 case MODE_COMPLEX_FLOAT:
1872 printf (" mode_size[E_%smode] = 2*s;\n", m->name);
1873 printf (" mode_unit_size[E_%smode] = s;\n", m->name);
1874 printf (" mode_base_align[E_%smode] = s & (~s + 1);\n",
1875 m->name);
1876 break;
1878 case MODE_VECTOR_BOOL:
1879 /* Changes to BImode should not affect vector booleans. */
1880 break;
1882 case MODE_VECTOR_INT:
1883 case MODE_VECTOR_FLOAT:
1884 case MODE_VECTOR_FRACT:
1885 case MODE_VECTOR_UFRACT:
1886 case MODE_VECTOR_ACCUM:
1887 case MODE_VECTOR_UACCUM:
1888 printf (" mode_size[E_%smode] = %d * ps;\n",
1889 m->name, m->ncomponents);
1890 printf (" mode_unit_size[E_%smode] = s;\n", m->name);
1891 printf (" mode_base_align[E_%smode]"
1892 " = known_alignment (%d * ps);\n",
1893 m->name, m->ncomponents);
1894 break;
1896 default:
1897 internal_error (
1898 "mode %s is neither vector nor complex but contains %s",
1899 m->name, a->mode->name);
1900 /* NOTREACHED */
1905 /* Alignment adjustments propagate too.
1906 ??? This may not be the right thing for vector modes. */
1907 for (a = adj_alignment; a; a = a->next)
1909 printf ("\n /* %s:%d */\n s = %s;\n",
1910 a->file, a->line, a->adjustment);
1911 printf (" mode_base_align[E_%smode] = s;\n", a->mode->name);
1913 for (m = a->mode->contained; m; m = m->next_cont)
1915 switch (m->cl)
1917 case MODE_COMPLEX_INT:
1918 case MODE_COMPLEX_FLOAT:
1919 printf (" mode_base_align[E_%smode] = s;\n", m->name);
1920 break;
1922 case MODE_VECTOR_BOOL:
1923 /* Changes to BImode should not affect vector booleans. */
1924 break;
1926 case MODE_VECTOR_INT:
1927 case MODE_VECTOR_FLOAT:
1928 case MODE_VECTOR_FRACT:
1929 case MODE_VECTOR_UFRACT:
1930 case MODE_VECTOR_ACCUM:
1931 case MODE_VECTOR_UACCUM:
1932 printf (" mode_base_align[E_%smode] = %d*s;\n",
1933 m->name, m->ncomponents);
1934 break;
1936 default:
1937 internal_error (
1938 "mode %s is neither vector nor complex but contains %s",
1939 m->name, a->mode->name);
1940 /* NOTREACHED */
1945 /* Ibit adjustments don't have to propagate. */
1946 for (a = adj_ibit; a; a = a->next)
1948 printf ("\n /* %s:%d */\n s = %s;\n",
1949 a->file, a->line, a->adjustment);
1950 printf (" mode_ibit[E_%smode] = s;\n", a->mode->name);
1953 /* Fbit adjustments don't have to propagate. */
1954 for (a = adj_fbit; a; a = a->next)
1956 printf ("\n /* %s:%d */\n s = %s;\n",
1957 a->file, a->line, a->adjustment);
1958 printf (" mode_fbit[E_%smode] = s;\n", a->mode->name);
1961 /* Real mode formats don't have to propagate anywhere. */
1962 for (a = adj_format; a; a = a->next)
1963 printf ("\n /* %s:%d */\n REAL_MODE_FORMAT (E_%smode) = %s;\n",
1964 a->file, a->line, a->mode->name, a->adjustment);
1966 puts ("}");
1969 /* Emit ibit for all modes. */
1971 static void
1972 emit_mode_ibit (void)
1974 int c;
1975 struct mode_data *m;
1977 print_maybe_const_decl ("%sunsigned char",
1978 "mode_ibit", "NUM_MACHINE_MODES",
1979 adj_ibit);
1981 for_all_modes (c, m)
1982 tagged_printf ("%u", m->ibit, m->name);
1984 print_closer ();
1987 /* Emit fbit for all modes. */
1989 static void
1990 emit_mode_fbit (void)
1992 int c;
1993 struct mode_data *m;
1995 print_maybe_const_decl ("%sunsigned char",
1996 "mode_fbit", "NUM_MACHINE_MODES",
1997 adj_fbit);
1999 for_all_modes (c, m)
2000 tagged_printf ("%u", m->fbit, m->name);
2002 print_closer ();
2005 /* Emit __intN for all modes. */
2007 static void
2008 emit_mode_int_n (void)
2010 int c;
2011 struct mode_data *m;
2012 struct mode_data **mode_sort;
2013 int n_modes = 0;
2014 int i, j;
2016 print_decl ("int_n_data_t", "int_n_data", "");
2018 n_modes = 0;
2019 for_all_modes (c, m)
2020 if (m->int_n)
2021 n_modes ++;
2022 mode_sort = XALLOCAVEC (struct mode_data *, n_modes);
2024 n_modes = 0;
2025 for_all_modes (c, m)
2026 if (m->int_n)
2027 mode_sort[n_modes++] = m;
2029 /* Yes, this is a bubblesort, but there are at most four (and
2030 usually only 1-2) entries to sort. */
2031 for (i = 0; i<n_modes - 1; i++)
2032 for (j = i + 1; j < n_modes; j++)
2033 if (mode_sort[i]->int_n > mode_sort[j]->int_n)
2034 std::swap (mode_sort[i], mode_sort[j]);
2036 for (i = 0; i < n_modes; i ++)
2038 m = mode_sort[i];
2039 printf(" {\n");
2040 tagged_printf ("%u", m->int_n, m->name);
2041 printf ("{ E_%smode },", m->name);
2042 printf(" },\n");
2045 print_closer ();
2049 static void
2050 emit_insn_modes_c (void)
2052 emit_insn_modes_c_header ();
2053 emit_mode_name ();
2054 emit_mode_class ();
2055 emit_mode_precision ();
2056 emit_mode_size ();
2057 emit_mode_nunits ();
2058 emit_mode_wider ();
2059 emit_mode_complex ();
2060 emit_mode_mask ();
2061 emit_mode_inner ();
2062 emit_mode_unit_size ();
2063 emit_mode_unit_precision ();
2064 emit_mode_base_align ();
2065 emit_class_narrowest_mode ();
2066 emit_real_format_for_mode ();
2067 emit_mode_adjustments ();
2068 emit_mode_ibit ();
2069 emit_mode_fbit ();
2070 emit_mode_int_n ();
2073 static void
2074 emit_min_insn_modes_c (void)
2076 emit_min_insn_modes_c_header ();
2077 emit_mode_name ();
2078 emit_mode_class ();
2079 emit_mode_nunits ();
2080 emit_mode_wider ();
2081 emit_mode_inner ();
2082 emit_class_narrowest_mode ();
2085 /* Master control. */
2087 main (int argc, char **argv)
2089 bool gen_header = false, gen_inlines = false, gen_min = false;
2090 progname = argv[0];
2092 if (argc == 1)
2094 else if (argc == 2 && !strcmp (argv[1], "-h"))
2095 gen_header = true;
2096 else if (argc == 2 && !strcmp (argv[1], "-i"))
2097 gen_inlines = true;
2098 else if (argc == 2 && !strcmp (argv[1], "-m"))
2099 gen_min = true;
2100 else
2102 error ("usage: %s [-h|-i|-m] > file", progname);
2103 return FATAL_EXIT_CODE;
2106 modes_by_name = htab_create_alloc (64, hash_mode, eq_mode, 0, xcalloc, free);
2108 create_modes ();
2109 complete_all_modes ();
2111 if (have_error)
2112 return FATAL_EXIT_CODE;
2114 calc_wider_mode ();
2116 if (gen_header)
2117 emit_insn_modes_h ();
2118 else if (gen_inlines)
2119 emit_insn_modes_inline_h ();
2120 else if (gen_min)
2121 emit_min_insn_modes_c ();
2122 else
2123 emit_insn_modes_c ();
2125 if (fflush (stdout) || fclose (stdout))
2126 return FATAL_EXIT_CODE;
2127 return SUCCESS_EXIT_CODE;