gccrs: add test case to show our query-type system is working
[official-gcc.git] / gcc / genmodes.cc
blob715787b8f4833b4e564237bc02f18c4b200aaa04
1 /* Generate the machine mode enumeration and associated tables.
2 Copyright (C) 2003-2023 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;
117 static struct mode_adjust *adj_precision;
119 /* Mode class operations. */
120 static enum mode_class
121 complex_class (enum mode_class c)
123 switch (c)
125 case MODE_INT: return MODE_COMPLEX_INT;
126 case MODE_PARTIAL_INT: return MODE_COMPLEX_INT;
127 case MODE_FLOAT: return MODE_COMPLEX_FLOAT;
128 default:
129 error ("no complex class for class %s", mode_class_names[c]);
130 return MODE_RANDOM;
134 static enum mode_class
135 vector_class (enum mode_class cl)
137 switch (cl)
139 case MODE_INT: return MODE_VECTOR_INT;
140 case MODE_FLOAT: return MODE_VECTOR_FLOAT;
141 case MODE_FRACT: return MODE_VECTOR_FRACT;
142 case MODE_UFRACT: return MODE_VECTOR_UFRACT;
143 case MODE_ACCUM: return MODE_VECTOR_ACCUM;
144 case MODE_UACCUM: return MODE_VECTOR_UACCUM;
145 default:
146 error ("no vector class for class %s", mode_class_names[cl]);
147 return MODE_RANDOM;
151 /* Utility routines. */
152 static inline struct mode_data *
153 find_mode (const char *name)
155 struct mode_data key;
157 key.name = name;
158 return (struct mode_data *) htab_find (modes_by_name, &key);
161 static struct mode_data *
162 new_mode (enum mode_class cl, const char *name,
163 const char *file, unsigned int line)
165 struct mode_data *m;
166 static unsigned int count = 0;
168 m = find_mode (name);
169 if (m)
171 error ("%s:%d: duplicate definition of mode \"%s\"",
172 trim_filename (file), line, name);
173 error ("%s:%d: previous definition here", m->file, m->line);
174 return m;
177 m = XNEW (struct mode_data);
178 memcpy (m, &blank_mode, sizeof (struct mode_data));
179 m->cl = cl;
180 m->name = name;
181 if (file)
182 m->file = trim_filename (file);
183 m->line = line;
184 m->counter = count++;
186 m->next = modes[cl];
187 modes[cl] = m;
188 n_modes[cl]++;
190 *htab_find_slot (modes_by_name, m, INSERT) = m;
192 return m;
195 static hashval_t
196 hash_mode (const void *p)
198 const struct mode_data *m = (const struct mode_data *)p;
199 return htab_hash_string (m->name);
202 static int
203 eq_mode (const void *p, const void *q)
205 const struct mode_data *a = (const struct mode_data *)p;
206 const struct mode_data *b = (const struct mode_data *)q;
208 return !strcmp (a->name, b->name);
211 #define for_all_modes(C, M) \
212 for (C = 0; C < MAX_MODE_CLASS; C++) \
213 for (M = modes[C]; M; M = M->next)
215 static void ATTRIBUTE_UNUSED
216 new_adjust (const char *name,
217 struct mode_adjust **category, const char *catname,
218 const char *adjustment,
219 enum mode_class required_class_from,
220 enum mode_class required_class_to,
221 const char *file, unsigned int line)
223 struct mode_data *mode = find_mode (name);
224 struct mode_adjust *a;
226 file = trim_filename (file);
228 if (!mode)
230 error ("%s:%d: no mode \"%s\"", file, line, name);
231 return;
234 if (required_class_from != MODE_RANDOM
235 && (mode->cl < required_class_from || mode->cl > required_class_to))
237 error ("%s:%d: mode \"%s\" is not among class {%s, %s}",
238 file, line, name, mode_class_names[required_class_from] + 5,
239 mode_class_names[required_class_to] + 5);
240 return;
243 for (a = *category; a; a = a->next)
244 if (a->mode == mode)
246 error ("%s:%d: mode \"%s\" already has a %s adjustment",
247 file, line, name, catname);
248 error ("%s:%d: previous adjustment here", a->file, a->line);
249 return;
252 a = XNEW (struct mode_adjust);
253 a->mode = mode;
254 a->adjustment = adjustment;
255 a->file = file;
256 a->line = line;
258 a->next = *category;
259 *category = a;
262 /* Diagnose failure to meet expectations in a partially filled out
263 mode structure. */
264 enum requirement { SET, UNSET, OPTIONAL };
266 #define validate_field_(mname, fname, req, val, unset, file, line) do { \
267 switch (req) \
269 case SET: \
270 if (val == unset) \
271 error ("%s:%d: (%s) field %s must be set", \
272 file, line, mname, fname); \
273 break; \
274 case UNSET: \
275 if (val != unset) \
276 error ("%s:%d: (%s) field %s must not be set", \
277 file, line, mname, fname); \
278 case OPTIONAL: \
279 break; \
281 } while (0)
283 #define validate_field(M, F) \
284 validate_field_(M->name, #F, r_##F, M->F, blank_mode.F, M->file, M->line)
286 static void
287 validate_mode (struct mode_data *m,
288 enum requirement r_precision,
289 enum requirement r_bytesize,
290 enum requirement r_component,
291 enum requirement r_ncomponents,
292 enum requirement r_format)
294 validate_field (m, precision);
295 validate_field (m, bytesize);
296 validate_field (m, component);
297 validate_field (m, ncomponents);
298 validate_field (m, format);
300 #undef validate_field
301 #undef validate_field_
303 /* Given a partially-filled-out mode structure, figure out what we can
304 and fill the rest of it in; die if it isn't enough. */
305 static void
306 complete_mode (struct mode_data *m)
308 unsigned int alignment;
310 if (!m->name)
312 error ("%s:%d: mode with no name", m->file, m->line);
313 return;
315 if (m->cl == MAX_MODE_CLASS)
317 error ("%s:%d: %smode has no mode class", m->file, m->line, m->name);
318 return;
321 switch (m->cl)
323 case MODE_RANDOM:
324 /* Nothing more need be said. */
325 if (!strcmp (m->name, "VOID"))
326 void_mode = m;
328 validate_mode (m, UNSET, UNSET, UNSET, UNSET, UNSET);
330 m->precision = 0;
331 m->bytesize = 0;
332 m->ncomponents = 0;
333 m->component = 0;
334 break;
336 case MODE_CC:
337 /* Again, nothing more need be said. For historical reasons,
338 the size of a CC mode is four units. */
339 validate_mode (m, UNSET, UNSET, UNSET, UNSET, UNSET);
341 m->bytesize = 4;
342 m->ncomponents = 1;
343 m->component = 0;
344 break;
346 case MODE_INT:
347 case MODE_FLOAT:
348 case MODE_DECIMAL_FLOAT:
349 case MODE_FRACT:
350 case MODE_UFRACT:
351 case MODE_ACCUM:
352 case MODE_UACCUM:
353 /* A scalar mode must have a byte size, may have a bit size,
354 and must not have components. A float mode must have a
355 format. */
356 validate_mode (m, OPTIONAL, SET, UNSET, UNSET,
357 (m->cl == MODE_FLOAT || m->cl == MODE_DECIMAL_FLOAT)
358 ? SET : UNSET);
360 m->ncomponents = 1;
361 m->component = 0;
362 break;
364 case MODE_OPAQUE:
365 /* Opaque modes have size and precision. */
366 validate_mode (m, OPTIONAL, SET, UNSET, UNSET, UNSET);
368 m->ncomponents = 1;
369 m->component = 0;
370 break;
372 case MODE_PARTIAL_INT:
373 /* A partial integer mode uses ->component to say what the
374 corresponding full-size integer mode is, and may also
375 specify a bit size. */
376 validate_mode (m, OPTIONAL, UNSET, SET, UNSET, UNSET);
378 m->bytesize = m->component->bytesize;
380 m->ncomponents = 1;
381 break;
383 case MODE_COMPLEX_INT:
384 case MODE_COMPLEX_FLOAT:
385 /* Complex modes should have a component indicated, but no more. */
386 validate_mode (m, UNSET, UNSET, SET, UNSET, UNSET);
387 m->ncomponents = 2;
388 if (m->component->precision != (unsigned int)-1)
389 m->precision = 2 * m->component->precision;
390 m->bytesize = 2 * m->component->bytesize;
391 break;
393 case MODE_VECTOR_BOOL:
394 validate_mode (m, UNSET, SET, SET, SET, UNSET);
395 break;
397 case MODE_VECTOR_INT:
398 case MODE_VECTOR_FLOAT:
399 case MODE_VECTOR_FRACT:
400 case MODE_VECTOR_UFRACT:
401 case MODE_VECTOR_ACCUM:
402 case MODE_VECTOR_UACCUM:
403 /* Vector modes should have a component and a number of components. */
404 validate_mode (m, UNSET, UNSET, SET, SET, UNSET);
405 if (m->component->precision != (unsigned int)-1)
406 m->precision = m->ncomponents * m->component->precision;
407 m->bytesize = m->ncomponents * m->component->bytesize;
408 break;
410 default:
411 gcc_unreachable ();
414 /* If not already specified, the mode alignment defaults to the largest
415 power of two that divides the size of the object. Complex types are
416 not more aligned than their contents. */
417 if (m->cl == MODE_COMPLEX_INT || m->cl == MODE_COMPLEX_FLOAT)
418 alignment = m->component->bytesize;
419 else
420 alignment = m->bytesize;
422 m->alignment = alignment & (~alignment + 1);
424 /* If this mode has components, make the component mode point back
425 to this mode, for the sake of adjustments. */
426 if (m->component)
428 m->next_cont = m->component->contained;
429 m->component->contained = m;
433 static void
434 complete_all_modes (void)
436 struct mode_data *m;
437 int cl;
439 for_all_modes (cl, m)
440 complete_mode (m);
443 /* For each mode in class CLASS, construct a corresponding complex mode. */
444 #define COMPLEX_MODES(C) make_complex_modes (MODE_##C, __FILE__, __LINE__)
445 static void
446 make_complex_modes (enum mode_class cl,
447 const char *file, unsigned int line)
449 struct mode_data *m;
450 struct mode_data *c;
451 enum mode_class cclass = complex_class (cl);
453 if (cclass == MODE_RANDOM)
454 return;
456 for (m = modes[cl]; m; m = m->next)
458 char *p, *buf;
459 size_t m_len;
461 /* Skip BImode. FIXME: BImode probably shouldn't be MODE_INT. */
462 if (m->boolean)
463 continue;
465 m_len = strlen (m->name);
466 /* The leading "1 +" is in case we prepend a "C" below. */
467 buf = (char *) xmalloc (1 + m_len + 1);
469 /* Float complex modes are named SCmode, etc.
470 Int complex modes are named CSImode, etc.
471 This inconsistency should be eliminated. */
472 p = 0;
473 if (cl == MODE_FLOAT)
475 memcpy (buf, m->name, m_len + 1);
476 p = strchr (buf, 'F');
477 if (p == 0 && strchr (buf, 'D') == 0)
479 error ("%s:%d: float mode \"%s\" has no 'F' or 'D'",
480 m->file, m->line, m->name);
481 free (buf);
482 continue;
485 if (p != 0)
486 *p = 'C';
487 else
489 buf[0] = 'C';
490 memcpy (buf + 1, m->name, m_len + 1);
493 c = new_mode (cclass, buf, file, line);
494 c->component = m;
495 m->complex = c;
499 /* For all modes in class CL, construct vector modes of width WIDTH,
500 having as many components as necessary. ORDER is the sorting order
501 of the mode, with smaller numbers indicating a higher priority. */
502 #define VECTOR_MODES_WITH_PREFIX(PREFIX, C, W, ORDER) \
503 make_vector_modes (MODE_##C, #PREFIX, W, ORDER, __FILE__, __LINE__)
504 #define VECTOR_MODES(C, W) VECTOR_MODES_WITH_PREFIX (V, C, W, 0)
505 static void ATTRIBUTE_UNUSED
506 make_vector_modes (enum mode_class cl, const char *prefix, unsigned int width,
507 unsigned int order, const char *file, unsigned int line)
509 struct mode_data *m;
510 struct mode_data *v;
511 /* Big enough for a 32-bit UINT_MAX plus the text. */
512 char buf[12];
513 unsigned int ncomponents;
514 enum mode_class vclass = vector_class (cl);
516 if (vclass == MODE_RANDOM)
517 return;
519 for (m = modes[cl]; m; m = m->next)
521 /* Do not construct vector modes with only one element, or
522 vector modes where the element size doesn't divide the full
523 size evenly. */
524 ncomponents = width / m->bytesize;
525 if (ncomponents < 2)
526 continue;
527 if (width % m->bytesize)
528 continue;
530 /* Skip QFmode and BImode. FIXME: this special case should
531 not be necessary. */
532 if (cl == MODE_FLOAT && m->bytesize == 1)
533 continue;
534 if (m->boolean)
535 continue;
537 if ((size_t) snprintf (buf, sizeof buf, "%s%u%s", prefix,
538 ncomponents, m->name) >= sizeof buf)
540 error ("%s:%d: mode name \"%s\" is too long",
541 m->file, m->line, m->name);
542 continue;
545 v = new_mode (vclass, xstrdup (buf), file, line);
546 v->order = order;
547 v->component = m;
548 v->ncomponents = ncomponents;
552 /* Create a vector of booleans called NAME with COUNT elements and
553 BYTESIZE bytes in total. */
554 #define VECTOR_BOOL_MODE(NAME, COUNT, COMPONENT, BYTESIZE) \
555 make_vector_bool_mode (#NAME, COUNT, #COMPONENT, BYTESIZE, \
556 __FILE__, __LINE__)
557 static void ATTRIBUTE_UNUSED
558 make_vector_bool_mode (const char *name, unsigned int count,
559 const char *component, unsigned int bytesize,
560 const char *file, unsigned int line)
562 struct mode_data *m = find_mode (component);
563 if (!m)
565 error ("%s:%d: no mode \"%s\"", file, line, component);
566 return;
569 struct mode_data *v = new_mode (MODE_VECTOR_BOOL, name, file, line);
570 v->component = m;
571 v->ncomponents = count;
572 v->bytesize = bytesize;
575 /* Input. */
577 #define _SPECIAL_MODE(C, N) \
578 make_special_mode (MODE_##C, #N, __FILE__, __LINE__)
579 #define RANDOM_MODE(N) _SPECIAL_MODE (RANDOM, N)
580 #define CC_MODE(N) _SPECIAL_MODE (CC, N)
582 static void
583 make_special_mode (enum mode_class cl, const char *name,
584 const char *file, unsigned int line)
586 new_mode (cl, name, file, line);
589 #define INT_MODE(N, Y) FRACTIONAL_INT_MODE (N, -1U, Y)
590 #define FRACTIONAL_INT_MODE(N, B, Y) \
591 make_int_mode (#N, B, Y, __FILE__, __LINE__)
593 static void
594 make_int_mode (const char *name,
595 unsigned int precision, unsigned int bytesize,
596 const char *file, unsigned int line)
598 struct mode_data *m = new_mode (MODE_INT, name, file, line);
599 m->bytesize = bytesize;
600 m->precision = precision;
603 #define BOOL_MODE(N, B, Y) \
604 make_bool_mode (#N, B, Y, __FILE__, __LINE__)
606 static void
607 make_bool_mode (const char *name,
608 unsigned int precision, unsigned int bytesize,
609 const char *file, unsigned int line)
611 struct mode_data *m = new_mode (MODE_INT, name, file, line);
612 m->bytesize = bytesize;
613 m->precision = precision;
614 m->boolean = true;
617 #define OPAQUE_MODE(N, B) \
618 make_opaque_mode (#N, -1U, B, __FILE__, __LINE__)
620 static void ATTRIBUTE_UNUSED
621 make_opaque_mode (const char *name,
622 unsigned int precision,
623 unsigned int bytesize,
624 const char *file, unsigned int line)
626 struct mode_data *m = new_mode (MODE_OPAQUE, name, file, line);
627 m->bytesize = bytesize;
628 m->precision = precision;
631 #define FRACT_MODE(N, Y, F) \
632 make_fixed_point_mode (MODE_FRACT, #N, Y, 0, F, __FILE__, __LINE__)
634 #define UFRACT_MODE(N, Y, F) \
635 make_fixed_point_mode (MODE_UFRACT, #N, Y, 0, F, __FILE__, __LINE__)
637 #define ACCUM_MODE(N, Y, I, F) \
638 make_fixed_point_mode (MODE_ACCUM, #N, Y, I, F, __FILE__, __LINE__)
640 #define UACCUM_MODE(N, Y, I, F) \
641 make_fixed_point_mode (MODE_UACCUM, #N, Y, I, F, __FILE__, __LINE__)
643 /* Create a fixed-point mode by setting CL, NAME, BYTESIZE, IBIT, FBIT,
644 FILE, and LINE. */
646 static void
647 make_fixed_point_mode (enum mode_class cl,
648 const char *name,
649 unsigned int bytesize,
650 unsigned int ibit,
651 unsigned int fbit,
652 const char *file, unsigned int line)
654 struct mode_data *m = new_mode (cl, name, file, line);
655 m->bytesize = bytesize;
656 m->ibit = ibit;
657 m->fbit = fbit;
660 #define FLOAT_MODE(N, Y, F) FRACTIONAL_FLOAT_MODE (N, -1U, Y, F)
661 #define FRACTIONAL_FLOAT_MODE(N, B, Y, F) \
662 make_float_mode (#N, B, Y, #F, __FILE__, __LINE__)
664 static void
665 make_float_mode (const char *name,
666 unsigned int precision, unsigned int bytesize,
667 const char *format,
668 const char *file, unsigned int line)
670 struct mode_data *m = new_mode (MODE_FLOAT, name, file, line);
671 m->bytesize = bytesize;
672 m->precision = precision;
673 m->format = format;
676 #define DECIMAL_FLOAT_MODE(N, Y, F) \
677 FRACTIONAL_DECIMAL_FLOAT_MODE (N, -1U, Y, F)
678 #define FRACTIONAL_DECIMAL_FLOAT_MODE(N, B, Y, F) \
679 make_decimal_float_mode (#N, B, Y, #F, __FILE__, __LINE__)
681 static void
682 make_decimal_float_mode (const char *name,
683 unsigned int precision, unsigned int bytesize,
684 const char *format,
685 const char *file, unsigned int line)
687 struct mode_data *m = new_mode (MODE_DECIMAL_FLOAT, name, file, line);
688 m->bytesize = bytesize;
689 m->precision = precision;
690 m->format = format;
693 #define RESET_FLOAT_FORMAT(N, F) \
694 reset_float_format (#N, #F, __FILE__, __LINE__)
695 static void ATTRIBUTE_UNUSED
696 reset_float_format (const char *name, const char *format,
697 const char *file, unsigned int line)
699 struct mode_data *m = find_mode (name);
700 if (!m)
702 error ("%s:%d: no mode \"%s\"", file, line, name);
703 return;
705 if (m->cl != MODE_FLOAT && m->cl != MODE_DECIMAL_FLOAT)
707 error ("%s:%d: mode \"%s\" is not a FLOAT class", file, line, name);
708 return;
710 m->format = format;
713 /* __intN support. */
714 #define INT_N(M,PREC) \
715 make_int_n (#M, PREC, __FILE__, __LINE__)
716 static void ATTRIBUTE_UNUSED
717 make_int_n (const char *m, int bitsize,
718 const char *file, unsigned int line)
720 struct mode_data *component = find_mode (m);
721 if (!component)
723 error ("%s:%d: no mode \"%s\"", file, line, m);
724 return;
726 if (component->cl != MODE_INT
727 && component->cl != MODE_PARTIAL_INT)
729 error ("%s:%d: mode \"%s\" is not class INT or PARTIAL_INT", file, line, m);
730 return;
732 if (component->int_n != 0)
734 error ("%s:%d: mode \"%s\" already has an intN", file, line, m);
735 return;
738 component->int_n = bitsize;
741 /* Partial integer modes are specified by relation to a full integer
742 mode. */
743 #define PARTIAL_INT_MODE(M,PREC,NAME) \
744 make_partial_integer_mode (#M, #NAME, PREC, __FILE__, __LINE__)
745 static void ATTRIBUTE_UNUSED
746 make_partial_integer_mode (const char *base, const char *name,
747 unsigned int precision,
748 const char *file, unsigned int line)
750 struct mode_data *m;
751 struct mode_data *component = find_mode (base);
752 if (!component)
754 error ("%s:%d: no mode \"%s\"", file, line, name);
755 return;
757 if (component->cl != MODE_INT)
759 error ("%s:%d: mode \"%s\" is not class INT", file, line, name);
760 return;
763 m = new_mode (MODE_PARTIAL_INT, name, file, line);
764 m->precision = precision;
765 m->component = component;
768 /* A single vector mode can be specified by naming its component
769 mode and the number of components. */
770 #define VECTOR_MODE_WITH_PREFIX(PREFIX, C, M, N, ORDER) \
771 make_vector_mode (MODE_##C, #PREFIX, #M, N, ORDER, __FILE__, __LINE__);
772 #define VECTOR_MODE(C, M, N) VECTOR_MODE_WITH_PREFIX(V, C, M, N, 0);
773 static void ATTRIBUTE_UNUSED
774 make_vector_mode (enum mode_class bclass,
775 const char *prefix,
776 const char *base,
777 unsigned int ncomponents,
778 unsigned int order,
779 const char *file, unsigned int line)
781 struct mode_data *v;
782 enum mode_class vclass = vector_class (bclass);
783 struct mode_data *component = find_mode (base);
784 char namebuf[16];
786 if (vclass == MODE_RANDOM)
787 return;
788 if (component == 0)
790 error ("%s:%d: no mode \"%s\"", file, line, base);
791 return;
793 if (component->cl != bclass
794 && (component->cl != MODE_PARTIAL_INT
795 || bclass != MODE_INT))
797 error ("%s:%d: mode \"%s\" is not class %s",
798 file, line, base, mode_class_names[bclass] + 5);
799 return;
802 if ((size_t)snprintf (namebuf, sizeof namebuf, "%s%u%s", prefix,
803 ncomponents, base) >= sizeof namebuf)
805 error ("%s:%d: mode name \"%s\" is too long",
806 file, line, base);
807 return;
810 v = new_mode (vclass, xstrdup (namebuf), file, line);
811 v->order = order;
812 v->ncomponents = ncomponents;
813 v->component = component;
816 /* Adjustability. */
817 #define _ADD_ADJUST(A, M, X, C1, C2) \
818 new_adjust (#M, &adj_##A, #A, #X, MODE_##C1, MODE_##C2, __FILE__, __LINE__)
820 #define ADJUST_NUNITS(M, X) _ADD_ADJUST (nunits, M, X, RANDOM, RANDOM)
821 #define ADJUST_BYTESIZE(M, X) _ADD_ADJUST (bytesize, M, X, RANDOM, RANDOM)
822 #define ADJUST_ALIGNMENT(M, X) _ADD_ADJUST (alignment, M, X, RANDOM, RANDOM)
823 #define ADJUST_PRECISION(M, X) _ADD_ADJUST (precision, M, X, RANDOM, RANDOM)
824 #define ADJUST_FLOAT_FORMAT(M, X) _ADD_ADJUST (format, M, X, FLOAT, FLOAT)
825 #define ADJUST_IBIT(M, X) _ADD_ADJUST (ibit, M, X, ACCUM, UACCUM)
826 #define ADJUST_FBIT(M, X) _ADD_ADJUST (fbit, M, X, FRACT, UACCUM)
828 static int bits_per_unit;
829 static int max_bitsize_mode_any_int;
830 static int max_bitsize_mode_any_mode;
832 static void
833 create_modes (void)
835 #include "machmode.def"
837 /* So put the default value unless the target needs a non standard
838 value. */
839 #ifdef BITS_PER_UNIT
840 bits_per_unit = BITS_PER_UNIT;
841 #else
842 bits_per_unit = 8;
843 #endif
845 #ifdef MAX_BITSIZE_MODE_ANY_INT
846 max_bitsize_mode_any_int = MAX_BITSIZE_MODE_ANY_INT;
847 #else
848 max_bitsize_mode_any_int = 0;
849 #endif
851 #ifdef MAX_BITSIZE_MODE_ANY_MODE
852 max_bitsize_mode_any_mode = MAX_BITSIZE_MODE_ANY_MODE;
853 #else
854 max_bitsize_mode_any_mode = 0;
855 #endif
858 #ifndef NUM_POLY_INT_COEFFS
859 #define NUM_POLY_INT_COEFFS 1
860 #endif
862 /* Processing. */
864 /* Sort a list of modes into the order needed for the WIDER field:
865 major sort by precision, minor sort by component precision.
867 For instance:
868 QI < HI < SI < DI < TI
869 V4QI < V2HI < V8QI < V4HI < V2SI.
871 If the precision is not set, sort by the bytesize. A mode with
872 precision set gets sorted before a mode without precision set, if
873 they have the same bytesize; this is the right thing because
874 the precision must always be smaller than the bytesize * BITS_PER_UNIT.
875 We don't have to do anything special to get this done -- an unset
876 precision shows up as (unsigned int)-1, i.e. UINT_MAX. */
877 static int
878 cmp_modes (const void *a, const void *b)
880 const struct mode_data *const m = *(const struct mode_data *const*)a;
881 const struct mode_data *const n = *(const struct mode_data *const*)b;
883 if (m->order > n->order)
884 return 1;
885 else if (m->order < n->order)
886 return -1;
888 if (m->bytesize > n->bytesize)
889 return 1;
890 else if (m->bytesize < n->bytesize)
891 return -1;
893 if (m->precision > n->precision)
894 return 1;
895 else if (m->precision < n->precision)
896 return -1;
898 if (!m->component && !n->component)
900 if (m->counter < n->counter)
901 return -1;
902 else
903 return 1;
906 if (m->component->bytesize > n->component->bytesize)
907 return 1;
908 else if (m->component->bytesize < n->component->bytesize)
909 return -1;
911 if (m->component->precision > n->component->precision)
912 return 1;
913 else if (m->component->precision < n->component->precision)
914 return -1;
916 if (m->counter < n->counter)
917 return -1;
918 else
919 return 1;
922 static void
923 calc_wider_mode (void)
925 int c;
926 struct mode_data *m;
927 struct mode_data **sortbuf;
928 unsigned int max_n_modes = 0;
929 unsigned int i, j;
931 for (c = 0; c < MAX_MODE_CLASS; c++)
932 max_n_modes = MAX (max_n_modes, n_modes[c]);
934 /* Allocate max_n_modes + 1 entries to leave room for the extra null
935 pointer assigned after the qsort call below. */
936 sortbuf = XALLOCAVEC (struct mode_data *, max_n_modes + 1);
938 for (c = 0; c < MAX_MODE_CLASS; c++)
940 /* "wider" is not meaningful for MODE_RANDOM and MODE_CC.
941 However, we want these in textual order, and we have
942 precisely the reverse. */
943 if (c == MODE_RANDOM || c == MODE_CC)
945 struct mode_data *prev, *next;
947 for (prev = 0, m = modes[c]; m; m = next)
949 m->wider = void_mode;
951 /* this is nreverse */
952 next = m->next;
953 m->next = prev;
954 prev = m;
956 modes[c] = prev;
958 else
960 if (!modes[c])
961 continue;
963 for (i = 0, m = modes[c]; m; i++, m = m->next)
964 sortbuf[i] = m;
966 (qsort) (sortbuf, i, sizeof (struct mode_data *), cmp_modes);
968 sortbuf[i] = 0;
969 for (j = 0; j < i; j++)
971 sortbuf[j]->next = sortbuf[j + 1];
972 if (c == MODE_PARTIAL_INT)
973 sortbuf[j]->wider = sortbuf[j]->component;
974 else
975 sortbuf[j]->wider = sortbuf[j]->next;
978 modes[c] = sortbuf[0];
983 /* Text to add to the constant part of a poly_int_pod initializer in
984 order to fill out te whole structure. */
985 #if NUM_POLY_INT_COEFFS == 1
986 #define ZERO_COEFFS ""
987 #elif NUM_POLY_INT_COEFFS == 2
988 #define ZERO_COEFFS ", 0"
989 #else
990 #error "Unknown value of NUM_POLY_INT_COEFFS"
991 #endif
993 /* Output routines. */
995 #define tagged_printf(FMT, ARG, TAG) do { \
996 int count_ = printf (" " FMT ",", ARG); \
997 printf ("%*s/* %s */\n", 27 - count_, "", TAG); \
998 } while (0)
1000 #define print_decl(TYPE, NAME, ASIZE) \
1001 puts ("\nconst " TYPE " " NAME "[" ASIZE "] =\n{");
1003 #define print_maybe_const_decl(TYPE, NAME, ASIZE, NEEDS_ADJ) \
1004 printf ("\n" TYPE " " NAME "[" ASIZE "] = \n{\n", \
1005 NEEDS_ADJ ? "" : "const ")
1007 #define print_closer() puts ("};")
1009 /* Compute the max bitsize of some of the classes of integers. It may
1010 be that there are needs for the other integer classes, and this
1011 code is easy to extend. */
1012 static void
1013 emit_max_int (void)
1015 unsigned int max, mmax;
1016 struct mode_data *i;
1017 int j;
1019 puts ("");
1021 printf ("#define BITS_PER_UNIT (%d)\n", bits_per_unit);
1023 if (max_bitsize_mode_any_int == 0)
1025 for (max = 1, i = modes[MODE_INT]; i; i = i->next)
1026 if (max < i->bytesize)
1027 max = i->bytesize;
1028 mmax = max;
1029 for (max = 1, i = modes[MODE_PARTIAL_INT]; i; i = i->next)
1030 if (max < i->bytesize)
1031 max = i->bytesize;
1032 if (max > mmax)
1033 mmax = max;
1034 printf ("#define MAX_BITSIZE_MODE_ANY_INT (%d*BITS_PER_UNIT)\n", mmax);
1036 else
1037 printf ("#define MAX_BITSIZE_MODE_ANY_INT %d\n", max_bitsize_mode_any_int);
1039 if (max_bitsize_mode_any_mode == 0)
1041 mmax = 0;
1042 for (j = 0; j < MAX_MODE_CLASS; j++)
1043 for (i = modes[j]; i; i = i->next)
1044 if (mmax < i->bytesize)
1045 mmax = i->bytesize;
1046 printf ("#define MAX_BITSIZE_MODE_ANY_MODE (%d*BITS_PER_UNIT)\n", mmax);
1048 else
1049 printf ("#define MAX_BITSIZE_MODE_ANY_MODE %d\n",
1050 max_bitsize_mode_any_mode);
1053 /* Emit mode_size_inline routine into insn-modes.h header. */
1054 static void
1055 emit_mode_size_inline (void)
1057 int c;
1058 struct mode_adjust *a;
1059 struct mode_data *m;
1061 /* Size adjustments must be propagated to all containing modes. */
1062 for (a = adj_bytesize; a; a = a->next)
1064 a->mode->need_bytesize_adj = true;
1065 for (m = a->mode->contained; m; m = m->next_cont)
1066 m->need_bytesize_adj = true;
1069 /* Changing the number of units by a factor of X also changes the size
1070 by a factor of X. */
1071 for (mode_adjust *a = adj_nunits; a; a = a->next)
1072 a->mode->need_bytesize_adj = true;
1074 printf ("\
1075 #ifdef __cplusplus\n\
1076 inline __attribute__((__always_inline__))\n\
1077 #else\n\
1078 extern __inline__ __attribute__((__always_inline__, __gnu_inline__))\n\
1079 #endif\n\
1080 poly_uint16\n\
1081 mode_size_inline (machine_mode mode)\n\
1082 {\n\
1083 extern %spoly_uint16_pod mode_size[NUM_MACHINE_MODES];\n\
1084 gcc_assert (mode >= 0 && mode < NUM_MACHINE_MODES);\n\
1085 switch (mode)\n\
1086 {\n", adj_nunits || adj_bytesize ? "" : "const ");
1088 for_all_modes (c, m)
1089 if (!m->need_bytesize_adj)
1090 printf (" case E_%smode: return %u;\n", m->name, m->bytesize);
1092 puts ("\
1093 default: return mode_size[mode];\n\
1094 }\n\
1095 }\n");
1098 /* Emit mode_nunits_inline routine into insn-modes.h header. */
1099 static void
1100 emit_mode_nunits_inline (void)
1102 int c;
1103 struct mode_data *m;
1105 for (mode_adjust *a = adj_nunits; a; a = a->next)
1106 a->mode->need_nunits_adj = true;
1108 printf ("\
1109 #ifdef __cplusplus\n\
1110 inline __attribute__((__always_inline__))\n\
1111 #else\n\
1112 extern __inline__ __attribute__((__always_inline__, __gnu_inline__))\n\
1113 #endif\n\
1114 poly_uint16\n\
1115 mode_nunits_inline (machine_mode mode)\n\
1116 {\n\
1117 extern %spoly_uint16_pod mode_nunits[NUM_MACHINE_MODES];\n\
1118 switch (mode)\n\
1119 {\n", adj_nunits ? "" : "const ");
1121 for_all_modes (c, m)
1122 if (!m->need_nunits_adj)
1123 printf (" case E_%smode: return %u;\n", m->name, m->ncomponents);
1125 puts ("\
1126 default: return mode_nunits[mode];\n\
1127 }\n\
1128 }\n");
1131 /* Emit mode_inner_inline routine into insn-modes.h header. */
1132 static void
1133 emit_mode_inner_inline (void)
1135 int c;
1136 struct mode_data *m;
1138 puts ("\
1139 #ifdef __cplusplus\n\
1140 inline __attribute__((__always_inline__))\n\
1141 #else\n\
1142 extern __inline__ __attribute__((__always_inline__, __gnu_inline__))\n\
1143 #endif\n\
1144 unsigned char\n\
1145 mode_inner_inline (machine_mode mode)\n\
1146 {\n\
1147 extern const unsigned char mode_inner[NUM_MACHINE_MODES];\n\
1148 gcc_assert (mode >= 0 && mode < NUM_MACHINE_MODES);\n\
1149 switch (mode)\n\
1150 {");
1152 for_all_modes (c, m)
1153 printf (" case E_%smode: return E_%smode;\n", m->name,
1154 c != MODE_PARTIAL_INT && m->component
1155 ? m->component->name : m->name);
1157 puts ("\
1158 default: return mode_inner[mode];\n\
1159 }\n\
1160 }\n");
1163 /* Emit mode_unit_size_inline routine into insn-modes.h header. */
1164 static void
1165 emit_mode_unit_size_inline (void)
1167 int c;
1168 struct mode_data *m;
1170 puts ("\
1171 #ifdef __cplusplus\n\
1172 inline __attribute__((__always_inline__))\n\
1173 #else\n\
1174 extern __inline__ __attribute__((__always_inline__, __gnu_inline__))\n\
1175 #endif\n\
1176 unsigned char\n\
1177 mode_unit_size_inline (machine_mode mode)\n\
1178 {\n\
1179 extern CONST_MODE_UNIT_SIZE unsigned char mode_unit_size[NUM_MACHINE_MODES];\
1181 gcc_assert (mode >= 0 && mode < NUM_MACHINE_MODES);\n\
1182 switch (mode)\n\
1183 {");
1185 for_all_modes (c, m)
1187 const char *name = m->name;
1188 struct mode_data *m2 = m;
1189 if (c != MODE_PARTIAL_INT && m2->component)
1190 m2 = m2->component;
1191 if (!m2->need_bytesize_adj)
1192 printf (" case E_%smode: return %u;\n", name, m2->bytesize);
1195 puts ("\
1196 default: return mode_unit_size[mode];\n\
1197 }\n\
1198 }\n");
1201 /* Emit mode_unit_precision_inline routine into insn-modes.h header. */
1202 static void
1203 emit_mode_unit_precision_inline (void)
1205 int c;
1206 struct mode_data *m;
1208 puts ("\
1209 #ifdef __cplusplus\n\
1210 inline __attribute__((__always_inline__))\n\
1211 #else\n\
1212 extern __inline__ __attribute__((__always_inline__, __gnu_inline__))\n\
1213 #endif\n\
1214 unsigned short\n\
1215 mode_unit_precision_inline (machine_mode mode)\n\
1216 {\n\
1217 extern const unsigned short mode_unit_precision[NUM_MACHINE_MODES];\n\
1218 gcc_assert (mode >= 0 && mode < NUM_MACHINE_MODES);\n\
1219 switch (mode)\n\
1220 {");
1222 for_all_modes (c, m)
1224 struct mode_data *m2
1225 = (c != MODE_PARTIAL_INT && m->component) ? m->component : m;
1226 if (m2->precision != (unsigned int)-1)
1227 printf (" case E_%smode: return %u;\n", m->name, m2->precision);
1228 else
1229 printf (" case E_%smode: return %u*BITS_PER_UNIT;\n",
1230 m->name, m2->bytesize);
1233 puts ("\
1234 default: return mode_unit_precision[mode];\n\
1235 }\n\
1236 }\n");
1239 /* Return the best machine mode class for MODE, or null if machine_mode
1240 should be used. */
1242 static const char *
1243 get_mode_class (struct mode_data *mode)
1245 switch (mode->cl)
1247 case MODE_INT:
1248 case MODE_PARTIAL_INT:
1249 return "scalar_int_mode";
1251 case MODE_FRACT:
1252 case MODE_UFRACT:
1253 case MODE_ACCUM:
1254 case MODE_UACCUM:
1255 return "scalar_mode";
1257 case MODE_FLOAT:
1258 case MODE_DECIMAL_FLOAT:
1259 return "scalar_float_mode";
1261 case MODE_COMPLEX_INT:
1262 case MODE_COMPLEX_FLOAT:
1263 return "complex_mode";
1265 default:
1266 return NULL;
1270 static void
1271 emit_insn_modes_h (void)
1273 int c;
1274 struct mode_data *m, *first, *last;
1275 int n_int_n_ents = 0;
1277 printf ("/* Generated automatically from machmode.def%s%s\n",
1278 HAVE_EXTRA_MODES ? " and " : "",
1279 EXTRA_MODES_FILE);
1281 puts ("\
1282 by genmodes. */\n\
1284 #ifndef GCC_INSN_MODES_H\n\
1285 #define GCC_INSN_MODES_H\n\
1287 enum machine_mode\n{");
1289 for (c = 0; c < MAX_MODE_CLASS; c++)
1290 for (m = modes[c]; m; m = m->next)
1292 int count_ = printf (" E_%smode,", m->name);
1293 printf ("%*s/* %s:%d */\n", 27 - count_, "",
1294 trim_filename (m->file), m->line);
1295 printf ("#define HAVE_%smode\n", m->name);
1296 printf ("#ifdef USE_ENUM_MODES\n");
1297 printf ("#define %smode E_%smode\n", m->name, m->name);
1298 printf ("#else\n");
1299 if (const char *mode_class = get_mode_class (m))
1300 printf ("#define %smode (%s ((%s::from_int) E_%smode))\n",
1301 m->name, mode_class, mode_class, m->name);
1302 else
1303 printf ("#define %smode ((void) 0, E_%smode)\n",
1304 m->name, m->name);
1305 printf ("#endif\n");
1308 puts (" MAX_MACHINE_MODE,\n");
1310 for (c = 0; c < MAX_MODE_CLASS; c++)
1312 first = modes[c];
1313 last = 0;
1314 for (m = first; m; last = m, m = m->next)
1317 /* Don't use BImode for MIN_MODE_INT, since otherwise the middle
1318 end will try to use it for bitfields in structures and the
1319 like, which we do not want. Only the target md file should
1320 generate BImode widgets. Since some targets such as ARM/MVE
1321 define boolean modes with multiple bits, handle those too. */
1322 if (first && first->boolean)
1324 struct mode_data *last_bool = first;
1325 printf (" MIN_MODE_BOOL = E_%smode,\n", first->name);
1327 while (first && first->boolean)
1329 last_bool = first;
1330 first = first->next;
1333 printf (" MAX_MODE_BOOL = E_%smode,\n\n", last_bool->name);
1336 if (first && last)
1337 printf (" MIN_%s = E_%smode,\n MAX_%s = E_%smode,\n\n",
1338 mode_class_names[c], first->name,
1339 mode_class_names[c], last->name);
1340 else
1341 printf (" MIN_%s = E_%smode,\n MAX_%s = E_%smode,\n\n",
1342 mode_class_names[c], void_mode->name,
1343 mode_class_names[c], void_mode->name);
1346 puts ("\
1347 NUM_MACHINE_MODES = MAX_MACHINE_MODE\n\
1348 };\n");
1350 /* Define a NUM_* macro for each mode class, giving the number of modes
1351 in the class. */
1352 for (c = 0; c < MAX_MODE_CLASS; c++)
1354 printf ("#define NUM_%s ", mode_class_names[c]);
1355 if (modes[c])
1356 printf ("(MAX_%s - MIN_%s + 1)\n", mode_class_names[c],
1357 mode_class_names[c]);
1358 else
1359 printf ("0\n");
1361 printf ("\n");
1363 /* I can't think of a better idea, can you? */
1364 printf ("#define CONST_MODE_NUNITS%s\n", adj_nunits ? "" : " const");
1365 printf ("#define CONST_MODE_PRECISION%s\n", adj_nunits ? "" : " const");
1366 printf ("#define CONST_MODE_SIZE%s\n",
1367 adj_bytesize || adj_nunits ? "" : " const");
1368 printf ("#define CONST_MODE_UNIT_SIZE%s\n", adj_bytesize ? "" : " const");
1369 printf ("#define CONST_MODE_BASE_ALIGN%s\n", adj_alignment ? "" : " const");
1370 #if 0 /* disabled for backward compatibility, temporary */
1371 printf ("#define CONST_REAL_FORMAT_FOR_MODE%s\n", adj_format ? "" :" const");
1372 #endif
1373 printf ("#define CONST_MODE_IBIT%s\n", adj_ibit ? "" : " const");
1374 printf ("#define CONST_MODE_FBIT%s\n", adj_fbit ? "" : " const");
1375 printf ("#define CONST_MODE_MASK%s\n", adj_nunits ? "" : " const");
1376 emit_max_int ();
1378 for_all_modes (c, m)
1379 if (m->int_n)
1380 n_int_n_ents ++;
1382 printf ("#define NUM_INT_N_ENTS %d\n", n_int_n_ents);
1384 printf ("#define NUM_POLY_INT_COEFFS %d\n", NUM_POLY_INT_COEFFS);
1386 puts ("\
1388 #endif /* insn-modes.h */");
1391 static void
1392 emit_insn_modes_inline_h (void)
1394 printf ("/* Generated automatically from machmode.def%s%s\n",
1395 HAVE_EXTRA_MODES ? " and " : "",
1396 EXTRA_MODES_FILE);
1398 puts ("\
1399 by genmodes. */\n\
1401 #ifndef GCC_INSN_MODES_INLINE_H\n\
1402 #define GCC_INSN_MODES_INLINE_H");
1404 puts ("\n#if !defined (USED_FOR_TARGET) && GCC_VERSION >= 4001\n");
1405 emit_mode_size_inline ();
1406 emit_mode_nunits_inline ();
1407 emit_mode_inner_inline ();
1408 emit_mode_unit_size_inline ();
1409 emit_mode_unit_precision_inline ();
1410 puts ("#endif /* GCC_VERSION >= 4001 */");
1412 puts ("\
1414 #endif /* insn-modes-inline.h */");
1417 static void
1418 emit_insn_modes_c_header (void)
1420 printf ("/* Generated automatically from machmode.def%s%s\n",
1421 HAVE_EXTRA_MODES ? " and " : "",
1422 EXTRA_MODES_FILE);
1424 puts ("\
1425 by genmodes. */\n\
1427 #include \"config.h\"\n\
1428 #include \"system.h\"\n\
1429 #include \"coretypes.h\"\n\
1430 #include \"tm.h\"\n\
1431 #include \"real.h\"");
1434 static void
1435 emit_min_insn_modes_c_header (void)
1437 printf ("/* Generated automatically from machmode.def%s%s\n",
1438 HAVE_EXTRA_MODES ? " and " : "",
1439 EXTRA_MODES_FILE);
1441 puts ("\
1442 by genmodes. */\n\
1444 #include \"bconfig.h\"\n\
1445 #include \"system.h\"\n\
1446 #include \"coretypes.h\"");
1449 static void
1450 emit_mode_name (void)
1452 int c;
1453 struct mode_data *m;
1455 print_decl ("char *const", "mode_name", "NUM_MACHINE_MODES");
1457 for_all_modes (c, m)
1458 printf (" \"%s\",\n", m->name);
1460 print_closer ();
1463 static void
1464 emit_mode_class (void)
1466 int c;
1467 struct mode_data *m;
1469 print_decl ("unsigned char", "mode_class", "NUM_MACHINE_MODES");
1471 for_all_modes (c, m)
1472 tagged_printf ("%s", mode_class_names[m->cl], m->name);
1474 print_closer ();
1477 static void
1478 emit_mode_precision (void)
1480 int c;
1481 struct mode_data *m;
1483 print_maybe_const_decl ("%spoly_uint16_pod", "mode_precision",
1484 "NUM_MACHINE_MODES", adj_nunits);
1486 for_all_modes (c, m)
1487 if (m->precision != (unsigned int)-1)
1488 tagged_printf ("{ %u" ZERO_COEFFS " }", m->precision, m->name);
1489 else
1490 tagged_printf ("{ %u * BITS_PER_UNIT" ZERO_COEFFS " }",
1491 m->bytesize, m->name);
1493 print_closer ();
1496 static void
1497 emit_mode_size (void)
1499 int c;
1500 struct mode_data *m;
1502 print_maybe_const_decl ("%spoly_uint16_pod", "mode_size",
1503 "NUM_MACHINE_MODES", adj_nunits || adj_bytesize);
1505 for_all_modes (c, m)
1506 tagged_printf ("{ %u" ZERO_COEFFS " }", m->bytesize, m->name);
1508 print_closer ();
1511 static void
1512 emit_mode_nunits (void)
1514 int c;
1515 struct mode_data *m;
1517 print_maybe_const_decl ("%spoly_uint16_pod", "mode_nunits",
1518 "NUM_MACHINE_MODES", adj_nunits);
1520 for_all_modes (c, m)
1521 tagged_printf ("{ %u" ZERO_COEFFS " }", m->ncomponents, m->name);
1523 print_closer ();
1526 static void
1527 emit_mode_wider (void)
1529 int c;
1530 struct mode_data *m;
1532 print_decl ("unsigned char", "mode_next", "NUM_MACHINE_MODES");
1534 for_all_modes (c, m)
1535 tagged_printf ("E_%smode",
1536 m->wider ? m->wider->name : void_mode->name,
1537 m->name);
1539 print_closer ();
1540 print_decl ("unsigned char", "mode_wider", "NUM_MACHINE_MODES");
1542 for_all_modes (c, m)
1544 struct mode_data *m2 = 0;
1546 if (m->cl == MODE_INT
1547 || m->cl == MODE_PARTIAL_INT
1548 || m->cl == MODE_FLOAT
1549 || m->cl == MODE_DECIMAL_FLOAT
1550 || m->cl == MODE_COMPLEX_FLOAT
1551 || m->cl == MODE_FRACT
1552 || m->cl == MODE_UFRACT
1553 || m->cl == MODE_ACCUM
1554 || m->cl == MODE_UACCUM)
1555 for (m2 = m->wider; m2 && m2 != void_mode; m2 = m2->wider)
1557 if (m2->bytesize == m->bytesize
1558 && m2->precision == m->precision)
1559 continue;
1560 break;
1563 if (m2 == void_mode)
1564 m2 = 0;
1565 tagged_printf ("E_%smode",
1566 m2 ? m2->name : void_mode->name,
1567 m->name);
1570 print_closer ();
1571 print_decl ("unsigned char", "mode_2xwider", "NUM_MACHINE_MODES");
1573 for_all_modes (c, m)
1575 struct mode_data * m2;
1577 for (m2 = m;
1578 m2 && m2 != void_mode;
1579 m2 = m2->wider)
1581 if (m2->bytesize < 2 * m->bytesize)
1582 continue;
1583 if (m->precision != (unsigned int) -1)
1585 if (m2->precision != 2 * m->precision)
1586 continue;
1588 else
1590 if (m2->precision != (unsigned int) -1)
1591 continue;
1594 /* For vectors we want twice the number of components,
1595 with the same element type. */
1596 if (m->cl == MODE_VECTOR_BOOL
1597 || m->cl == MODE_VECTOR_INT
1598 || m->cl == MODE_VECTOR_FLOAT
1599 || m->cl == MODE_VECTOR_FRACT
1600 || m->cl == MODE_VECTOR_UFRACT
1601 || m->cl == MODE_VECTOR_ACCUM
1602 || m->cl == MODE_VECTOR_UACCUM)
1604 if (m2->ncomponents != 2 * m->ncomponents)
1605 continue;
1606 if (m->component != m2->component)
1607 continue;
1610 break;
1612 if (m2 == void_mode)
1613 m2 = 0;
1614 tagged_printf ("E_%smode",
1615 m2 ? m2->name : void_mode->name,
1616 m->name);
1619 print_closer ();
1622 static void
1623 emit_mode_complex (void)
1625 int c;
1626 struct mode_data *m;
1628 print_decl ("unsigned char", "mode_complex", "NUM_MACHINE_MODES");
1630 for_all_modes (c, m)
1631 tagged_printf ("E_%smode",
1632 m->complex ? m->complex->name : void_mode->name,
1633 m->name);
1635 print_closer ();
1638 static void
1639 emit_mode_mask (void)
1641 int c;
1642 struct mode_data *m;
1644 print_maybe_const_decl ("%sunsigned HOST_WIDE_INT", "mode_mask_array",
1645 "NUM_MACHINE_MODES", adj_nunits);
1646 puts ("\
1647 #define MODE_MASK(m) \\\n\
1648 ((m) >= HOST_BITS_PER_WIDE_INT) \\\n\
1649 ? HOST_WIDE_INT_M1U \\\n\
1650 : (HOST_WIDE_INT_1U << (m)) - 1\n");
1652 for_all_modes (c, m)
1653 if (m->precision != (unsigned int)-1)
1654 tagged_printf ("MODE_MASK (%u)", m->precision, m->name);
1655 else
1656 tagged_printf ("MODE_MASK (%u*BITS_PER_UNIT)", m->bytesize, m->name);
1658 puts ("#undef MODE_MASK");
1659 print_closer ();
1662 static void
1663 emit_mode_inner (void)
1665 int c;
1666 struct mode_data *m;
1668 print_decl ("unsigned char", "mode_inner", "NUM_MACHINE_MODES");
1670 for_all_modes (c, m)
1671 tagged_printf ("E_%smode",
1672 c != MODE_PARTIAL_INT && m->component
1673 ? m->component->name : m->name,
1674 m->name);
1676 print_closer ();
1679 /* Emit mode_unit_size array into insn-modes.cc file. */
1680 static void
1681 emit_mode_unit_size (void)
1683 int c;
1684 struct mode_data *m;
1686 print_maybe_const_decl ("%sunsigned char", "mode_unit_size",
1687 "NUM_MACHINE_MODES", adj_bytesize);
1689 for_all_modes (c, m)
1690 tagged_printf ("%u",
1691 c != MODE_PARTIAL_INT && m->component
1692 ? m->component->bytesize : m->bytesize, m->name);
1694 print_closer ();
1697 /* Emit mode_unit_precision array into insn-modes.cc file. */
1698 static void
1699 emit_mode_unit_precision (void)
1701 int c;
1702 struct mode_data *m;
1704 print_decl ("unsigned short", "mode_unit_precision", "NUM_MACHINE_MODES");
1706 for_all_modes (c, m)
1708 struct mode_data *m2 = (c != MODE_PARTIAL_INT && m->component) ?
1709 m->component : m;
1710 if (m2->precision != (unsigned int)-1)
1711 tagged_printf ("%u", m2->precision, m->name);
1712 else
1713 tagged_printf ("%u*BITS_PER_UNIT", m2->bytesize, m->name);
1716 print_closer ();
1720 static void
1721 emit_mode_base_align (void)
1723 int c;
1724 struct mode_data *m;
1726 print_maybe_const_decl ("%sunsigned short",
1727 "mode_base_align", "NUM_MACHINE_MODES",
1728 adj_alignment);
1730 for_all_modes (c, m)
1731 tagged_printf ("%u", m->alignment, m->name);
1733 print_closer ();
1736 static void
1737 emit_class_narrowest_mode (void)
1739 int c;
1741 print_decl ("unsigned char", "class_narrowest_mode", "MAX_MODE_CLASS");
1743 for (c = 0; c < MAX_MODE_CLASS; c++)
1745 /* Bleah, all this to get the comment right for MIN_MODE_INT. */
1746 struct mode_data *m = modes[c];
1747 while (m && m->boolean)
1748 m = m->next;
1749 const char *comment_name = (m ? m : void_mode)->name;
1751 tagged_printf ("MIN_%s", mode_class_names[c], comment_name);
1754 print_closer ();
1757 static void
1758 emit_real_format_for_mode (void)
1760 struct mode_data *m;
1762 /* The entities pointed to by this table are constant, whether
1763 or not the table itself is constant.
1765 For backward compatibility this table is always writable
1766 (several targets modify it in TARGET_OPTION_OVERRIDE). FIXME:
1767 convert all said targets to use ADJUST_FORMAT instead. */
1768 #if 0
1769 print_maybe_const_decl ("const struct real_format *%s",
1770 "real_format_for_mode",
1771 "MAX_MODE_FLOAT - MIN_MODE_FLOAT + 1",
1772 format);
1773 #else
1774 print_decl ("struct real_format *\n", "real_format_for_mode",
1775 "MAX_MODE_FLOAT - MIN_MODE_FLOAT + 1 "
1776 "+ MAX_MODE_DECIMAL_FLOAT - MIN_MODE_DECIMAL_FLOAT + 1");
1777 #endif
1779 /* The beginning of the table is entries for float modes. */
1780 for (m = modes[MODE_FLOAT]; m; m = m->next)
1781 if (!strcmp (m->format, "0"))
1782 tagged_printf ("%s", m->format, m->name);
1783 else
1784 tagged_printf ("&%s", m->format, m->name);
1786 /* The end of the table is entries for decimal float modes. */
1787 for (m = modes[MODE_DECIMAL_FLOAT]; m; m = m->next)
1788 if (!strcmp (m->format, "0"))
1789 tagged_printf ("%s", m->format, m->name);
1790 else
1791 tagged_printf ("&%s", m->format, m->name);
1793 print_closer ();
1796 static void
1797 emit_mode_adjustments (void)
1799 int c;
1800 struct mode_adjust *a;
1801 struct mode_data *m;
1803 if (adj_nunits)
1804 printf ("\n"
1805 "void\n"
1806 "adjust_mode_mask (machine_mode mode)\n"
1807 "{\n"
1808 " unsigned int precision;\n"
1809 " if (GET_MODE_PRECISION (mode).is_constant (&precision)\n"
1810 " && precision < HOST_BITS_PER_WIDE_INT)\n"
1811 " mode_mask_array[mode] = (HOST_WIDE_INT_1U << precision) - 1;"
1812 "\n"
1813 " else\n"
1814 " mode_mask_array[mode] = HOST_WIDE_INT_M1U;\n"
1815 "}\n");
1817 puts ("\
1818 \nvoid\
1819 \ninit_adjust_machine_modes (void)\
1820 \n{\
1821 \n poly_uint16 ps ATTRIBUTE_UNUSED;\n\
1822 size_t s ATTRIBUTE_UNUSED;");
1824 for (a = adj_nunits; a; a = a->next)
1826 m = a->mode;
1827 printf ("\n"
1828 " {\n"
1829 " /* %s:%d */\n ps = %s;\n",
1830 a->file, a->line, a->adjustment);
1831 printf (" int old_factor = vector_element_size"
1832 " (mode_precision[E_%smode], mode_nunits[E_%smode]);\n",
1833 m->name, m->name);
1834 printf (" mode_precision[E_%smode] = ps * old_factor;\n", m->name);
1835 printf (" if (!multiple_p (mode_precision[E_%smode],"
1836 " BITS_PER_UNIT, &mode_size[E_%smode]))\n", m->name, m->name);
1837 printf (" mode_size[E_%smode] = -1;\n", m->name);
1838 printf (" mode_nunits[E_%smode] = ps;\n", m->name);
1839 printf (" adjust_mode_mask (E_%smode);\n", m->name);
1840 printf (" }\n");
1843 /* Size adjustments must be propagated to all containing modes.
1844 A size adjustment forces us to recalculate the alignment too. */
1845 for (a = adj_bytesize; a; a = a->next)
1847 printf ("\n /* %s:%d */\n", a->file, a->line);
1848 switch (a->mode->cl)
1850 case MODE_VECTOR_BOOL:
1851 case MODE_VECTOR_INT:
1852 case MODE_VECTOR_FLOAT:
1853 case MODE_VECTOR_FRACT:
1854 case MODE_VECTOR_UFRACT:
1855 case MODE_VECTOR_ACCUM:
1856 case MODE_VECTOR_UACCUM:
1857 printf (" ps = %s;\n", a->adjustment);
1858 printf (" s = mode_unit_size[E_%smode];\n", a->mode->name);
1859 break;
1861 default:
1862 printf (" ps = s = %s;\n", a->adjustment);
1863 printf (" mode_unit_size[E_%smode] = s;\n", a->mode->name);
1864 break;
1866 printf (" mode_size[E_%smode] = ps;\n", a->mode->name);
1867 printf (" mode_base_align[E_%smode] = known_alignment (ps);\n",
1868 a->mode->name);
1870 for (m = a->mode->contained; m; m = m->next_cont)
1872 switch (m->cl)
1874 case MODE_COMPLEX_INT:
1875 case MODE_COMPLEX_FLOAT:
1876 printf (" mode_size[E_%smode] = 2*s;\n", m->name);
1877 printf (" mode_unit_size[E_%smode] = s;\n", m->name);
1878 printf (" mode_base_align[E_%smode] = s & (~s + 1);\n",
1879 m->name);
1880 break;
1882 case MODE_VECTOR_BOOL:
1883 /* Changes to BImode should not affect vector booleans. */
1884 break;
1886 case MODE_VECTOR_INT:
1887 case MODE_VECTOR_FLOAT:
1888 case MODE_VECTOR_FRACT:
1889 case MODE_VECTOR_UFRACT:
1890 case MODE_VECTOR_ACCUM:
1891 case MODE_VECTOR_UACCUM:
1892 printf (" mode_size[E_%smode] = %d * ps;\n",
1893 m->name, m->ncomponents);
1894 printf (" mode_unit_size[E_%smode] = s;\n", m->name);
1895 printf (" mode_base_align[E_%smode]"
1896 " = known_alignment (%d * ps);\n",
1897 m->name, m->ncomponents);
1898 break;
1900 default:
1901 internal_error (
1902 "mode %s is neither vector nor complex but contains %s",
1903 m->name, a->mode->name);
1904 /* NOTREACHED */
1909 /* Alignment adjustments propagate too.
1910 ??? This may not be the right thing for vector modes. */
1911 for (a = adj_alignment; a; a = a->next)
1913 printf ("\n /* %s:%d */\n s = %s;\n",
1914 a->file, a->line, a->adjustment);
1915 printf (" mode_base_align[E_%smode] = s;\n", a->mode->name);
1917 for (m = a->mode->contained; m; m = m->next_cont)
1919 switch (m->cl)
1921 case MODE_COMPLEX_INT:
1922 case MODE_COMPLEX_FLOAT:
1923 printf (" mode_base_align[E_%smode] = s;\n", m->name);
1924 break;
1926 case MODE_VECTOR_BOOL:
1927 /* Changes to BImode should not affect vector booleans. */
1928 break;
1930 case MODE_VECTOR_INT:
1931 case MODE_VECTOR_FLOAT:
1932 case MODE_VECTOR_FRACT:
1933 case MODE_VECTOR_UFRACT:
1934 case MODE_VECTOR_ACCUM:
1935 case MODE_VECTOR_UACCUM:
1936 printf (" mode_base_align[E_%smode] = %d*s;\n",
1937 m->name, m->ncomponents);
1938 break;
1940 default:
1941 internal_error (
1942 "mode %s is neither vector nor complex but contains %s",
1943 m->name, a->mode->name);
1944 /* NOTREACHED */
1949 /* Ibit adjustments don't have to propagate. */
1950 for (a = adj_ibit; a; a = a->next)
1952 printf ("\n /* %s:%d */\n s = %s;\n",
1953 a->file, a->line, a->adjustment);
1954 printf (" mode_ibit[E_%smode] = s;\n", a->mode->name);
1957 /* Fbit adjustments don't have to propagate. */
1958 for (a = adj_fbit; a; a = a->next)
1960 printf ("\n /* %s:%d */\n s = %s;\n",
1961 a->file, a->line, a->adjustment);
1962 printf (" mode_fbit[E_%smode] = s;\n", a->mode->name);
1965 /* Real mode formats don't have to propagate anywhere. */
1966 for (a = adj_format; a; a = a->next)
1967 printf ("\n /* %s:%d */\n REAL_MODE_FORMAT (E_%smode) = %s;\n",
1968 a->file, a->line, a->mode->name, a->adjustment);
1970 /* Adjust precision to the actual bits size. */
1971 for (a = adj_precision; a; a = a->next)
1972 switch (a->mode->cl)
1974 case MODE_VECTOR_BOOL:
1975 printf ("\n /* %s:%d. */\n ps = %s;\n", a->file, a->line,
1976 a->adjustment);
1977 printf (" mode_precision[E_%smode] = ps;\n", a->mode->name);
1978 break;
1979 default:
1980 internal_error ("invalid use of ADJUST_PRECISION for mode %s",
1981 a->mode->name);
1982 /* NOTREACHED. */
1985 /* Ensure there is no mode size equals -1. */
1986 for_all_modes (c, m)
1987 printf ("\n gcc_assert (maybe_ne (mode_size[E_%smode], -1));\n",
1988 m->name);
1990 puts ("}");
1993 /* Emit ibit for all modes. */
1995 static void
1996 emit_mode_ibit (void)
1998 int c;
1999 struct mode_data *m;
2001 print_maybe_const_decl ("%sunsigned char",
2002 "mode_ibit", "NUM_MACHINE_MODES",
2003 adj_ibit);
2005 for_all_modes (c, m)
2006 tagged_printf ("%u", m->ibit, m->name);
2008 print_closer ();
2011 /* Emit fbit for all modes. */
2013 static void
2014 emit_mode_fbit (void)
2016 int c;
2017 struct mode_data *m;
2019 print_maybe_const_decl ("%sunsigned char",
2020 "mode_fbit", "NUM_MACHINE_MODES",
2021 adj_fbit);
2023 for_all_modes (c, m)
2024 tagged_printf ("%u", m->fbit, m->name);
2026 print_closer ();
2029 /* Emit __intN for all modes. */
2031 static void
2032 emit_mode_int_n (void)
2034 int c;
2035 struct mode_data *m;
2036 struct mode_data **mode_sort;
2037 int n_modes = 0;
2038 int i, j;
2040 print_decl ("int_n_data_t", "int_n_data", "");
2042 n_modes = 0;
2043 for_all_modes (c, m)
2044 if (m->int_n)
2045 n_modes ++;
2046 mode_sort = XALLOCAVEC (struct mode_data *, n_modes);
2048 n_modes = 0;
2049 for_all_modes (c, m)
2050 if (m->int_n)
2051 mode_sort[n_modes++] = m;
2053 /* Yes, this is a bubblesort, but there are at most four (and
2054 usually only 1-2) entries to sort. */
2055 for (i = 0; i<n_modes - 1; i++)
2056 for (j = i + 1; j < n_modes; j++)
2057 if (mode_sort[i]->int_n > mode_sort[j]->int_n)
2058 std::swap (mode_sort[i], mode_sort[j]);
2060 for (i = 0; i < n_modes; i ++)
2062 m = mode_sort[i];
2063 printf(" {\n");
2064 tagged_printf ("%u", m->int_n, m->name);
2065 printf ("{ E_%smode },", m->name);
2066 printf(" },\n");
2069 print_closer ();
2073 static void
2074 emit_insn_modes_c (void)
2076 emit_insn_modes_c_header ();
2077 emit_mode_name ();
2078 emit_mode_class ();
2079 emit_mode_precision ();
2080 emit_mode_size ();
2081 emit_mode_nunits ();
2082 emit_mode_wider ();
2083 emit_mode_complex ();
2084 emit_mode_mask ();
2085 emit_mode_inner ();
2086 emit_mode_unit_size ();
2087 emit_mode_unit_precision ();
2088 emit_mode_base_align ();
2089 emit_class_narrowest_mode ();
2090 emit_real_format_for_mode ();
2091 emit_mode_adjustments ();
2092 emit_mode_ibit ();
2093 emit_mode_fbit ();
2094 emit_mode_int_n ();
2097 static void
2098 emit_min_insn_modes_c (void)
2100 emit_min_insn_modes_c_header ();
2101 emit_mode_name ();
2102 emit_mode_class ();
2103 emit_mode_nunits ();
2104 emit_mode_wider ();
2105 emit_mode_inner ();
2106 emit_class_narrowest_mode ();
2109 /* Master control. */
2111 main (int argc, char **argv)
2113 bool gen_header = false, gen_inlines = false, gen_min = false;
2114 progname = argv[0];
2116 if (argc == 1)
2118 else if (argc == 2 && !strcmp (argv[1], "-h"))
2119 gen_header = true;
2120 else if (argc == 2 && !strcmp (argv[1], "-i"))
2121 gen_inlines = true;
2122 else if (argc == 2 && !strcmp (argv[1], "-m"))
2123 gen_min = true;
2124 else
2126 error ("usage: %s [-h|-i|-m] > file", progname);
2127 return FATAL_EXIT_CODE;
2130 modes_by_name = htab_create_alloc (64, hash_mode, eq_mode, 0, xcalloc, free);
2132 create_modes ();
2133 complete_all_modes ();
2135 if (have_error)
2136 return FATAL_EXIT_CODE;
2138 calc_wider_mode ();
2140 if (gen_header)
2141 emit_insn_modes_h ();
2142 else if (gen_inlines)
2143 emit_insn_modes_inline_h ();
2144 else if (gen_min)
2145 emit_min_insn_modes_c ();
2146 else
2147 emit_insn_modes_c ();
2149 if (fflush (stdout) || fclose (stdout))
2150 return FATAL_EXIT_CODE;
2151 return SUCCESS_EXIT_CODE;