gimple-fold: Use ranges to simplify _chk calls
[official-gcc.git] / gcc / genmodes.c
blobecc8b448406da7024ba024736bd29187ea066710
1 /* Generate the machine mode enumeration and associated tables.
2 Copyright (C) 2003-2021 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 */
83 static struct mode_data *modes[MAX_MODE_CLASS];
84 static unsigned int n_modes[MAX_MODE_CLASS];
85 static struct mode_data *void_mode;
87 static const struct mode_data blank_mode = {
88 0, "<unknown>", MAX_MODE_CLASS,
89 0, -1U, -1U, -1U, -1U,
90 0, 0, 0, 0, 0, 0,
91 "<unknown>", 0, 0, 0, 0, false, false, 0
94 static htab_t modes_by_name;
96 /* Data structure for recording target-specified runtime adjustments
97 to a particular mode. We support varying the byte size, the
98 alignment, and the floating point format. */
99 struct mode_adjust
101 struct mode_adjust *next;
102 struct mode_data *mode;
103 const char *adjustment;
105 const char *file;
106 unsigned int line;
109 static struct mode_adjust *adj_nunits;
110 static struct mode_adjust *adj_bytesize;
111 static struct mode_adjust *adj_alignment;
112 static struct mode_adjust *adj_format;
113 static struct mode_adjust *adj_ibit;
114 static struct mode_adjust *adj_fbit;
116 /* Mode class operations. */
117 static enum mode_class
118 complex_class (enum mode_class c)
120 switch (c)
122 case MODE_INT: return MODE_COMPLEX_INT;
123 case MODE_PARTIAL_INT: return MODE_COMPLEX_INT;
124 case MODE_FLOAT: return MODE_COMPLEX_FLOAT;
125 default:
126 error ("no complex class for class %s", mode_class_names[c]);
127 return MODE_RANDOM;
131 static enum mode_class
132 vector_class (enum mode_class cl)
134 switch (cl)
136 case MODE_INT: return MODE_VECTOR_INT;
137 case MODE_FLOAT: return MODE_VECTOR_FLOAT;
138 case MODE_FRACT: return MODE_VECTOR_FRACT;
139 case MODE_UFRACT: return MODE_VECTOR_UFRACT;
140 case MODE_ACCUM: return MODE_VECTOR_ACCUM;
141 case MODE_UACCUM: return MODE_VECTOR_UACCUM;
142 default:
143 error ("no vector class for class %s", mode_class_names[cl]);
144 return MODE_RANDOM;
148 /* Utility routines. */
149 static inline struct mode_data *
150 find_mode (const char *name)
152 struct mode_data key;
154 key.name = name;
155 return (struct mode_data *) htab_find (modes_by_name, &key);
158 static struct mode_data *
159 new_mode (enum mode_class cl, const char *name,
160 const char *file, unsigned int line)
162 struct mode_data *m;
163 static unsigned int count = 0;
165 m = find_mode (name);
166 if (m)
168 error ("%s:%d: duplicate definition of mode \"%s\"",
169 trim_filename (file), line, name);
170 error ("%s:%d: previous definition here", m->file, m->line);
171 return m;
174 m = XNEW (struct mode_data);
175 memcpy (m, &blank_mode, sizeof (struct mode_data));
176 m->cl = cl;
177 m->name = name;
178 if (file)
179 m->file = trim_filename (file);
180 m->line = line;
181 m->counter = count++;
183 m->next = modes[cl];
184 modes[cl] = m;
185 n_modes[cl]++;
187 *htab_find_slot (modes_by_name, m, INSERT) = m;
189 return m;
192 static hashval_t
193 hash_mode (const void *p)
195 const struct mode_data *m = (const struct mode_data *)p;
196 return htab_hash_string (m->name);
199 static int
200 eq_mode (const void *p, const void *q)
202 const struct mode_data *a = (const struct mode_data *)p;
203 const struct mode_data *b = (const struct mode_data *)q;
205 return !strcmp (a->name, b->name);
208 #define for_all_modes(C, M) \
209 for (C = 0; C < MAX_MODE_CLASS; C++) \
210 for (M = modes[C]; M; M = M->next)
212 static void ATTRIBUTE_UNUSED
213 new_adjust (const char *name,
214 struct mode_adjust **category, const char *catname,
215 const char *adjustment,
216 enum mode_class required_class_from,
217 enum mode_class required_class_to,
218 const char *file, unsigned int line)
220 struct mode_data *mode = find_mode (name);
221 struct mode_adjust *a;
223 file = trim_filename (file);
225 if (!mode)
227 error ("%s:%d: no mode \"%s\"", file, line, name);
228 return;
231 if (required_class_from != MODE_RANDOM
232 && (mode->cl < required_class_from || mode->cl > required_class_to))
234 error ("%s:%d: mode \"%s\" is not among class {%s, %s}",
235 file, line, name, mode_class_names[required_class_from] + 5,
236 mode_class_names[required_class_to] + 5);
237 return;
240 for (a = *category; a; a = a->next)
241 if (a->mode == mode)
243 error ("%s:%d: mode \"%s\" already has a %s adjustment",
244 file, line, name, catname);
245 error ("%s:%d: previous adjustment here", a->file, a->line);
246 return;
249 a = XNEW (struct mode_adjust);
250 a->mode = mode;
251 a->adjustment = adjustment;
252 a->file = file;
253 a->line = line;
255 a->next = *category;
256 *category = a;
259 /* Diagnose failure to meet expectations in a partially filled out
260 mode structure. */
261 enum requirement { SET, UNSET, OPTIONAL };
263 #define validate_field_(mname, fname, req, val, unset, file, line) do { \
264 switch (req) \
266 case SET: \
267 if (val == unset) \
268 error ("%s:%d: (%s) field %s must be set", \
269 file, line, mname, fname); \
270 break; \
271 case UNSET: \
272 if (val != unset) \
273 error ("%s:%d: (%s) field %s must not be set", \
274 file, line, mname, fname); \
275 case OPTIONAL: \
276 break; \
278 } while (0)
280 #define validate_field(M, F) \
281 validate_field_(M->name, #F, r_##F, M->F, blank_mode.F, M->file, M->line)
283 static void
284 validate_mode (struct mode_data *m,
285 enum requirement r_precision,
286 enum requirement r_bytesize,
287 enum requirement r_component,
288 enum requirement r_ncomponents,
289 enum requirement r_format)
291 validate_field (m, precision);
292 validate_field (m, bytesize);
293 validate_field (m, component);
294 validate_field (m, ncomponents);
295 validate_field (m, format);
297 #undef validate_field
298 #undef validate_field_
300 /* Given a partially-filled-out mode structure, figure out what we can
301 and fill the rest of it in; die if it isn't enough. */
302 static void
303 complete_mode (struct mode_data *m)
305 unsigned int alignment;
307 if (!m->name)
309 error ("%s:%d: mode with no name", m->file, m->line);
310 return;
312 if (m->cl == MAX_MODE_CLASS)
314 error ("%s:%d: %smode has no mode class", m->file, m->line, m->name);
315 return;
318 switch (m->cl)
320 case MODE_RANDOM:
321 /* Nothing more need be said. */
322 if (!strcmp (m->name, "VOID"))
323 void_mode = m;
325 validate_mode (m, UNSET, UNSET, UNSET, UNSET, UNSET);
327 m->precision = 0;
328 m->bytesize = 0;
329 m->ncomponents = 0;
330 m->component = 0;
331 break;
333 case MODE_CC:
334 /* Again, nothing more need be said. For historical reasons,
335 the size of a CC mode is four units. */
336 validate_mode (m, UNSET, UNSET, UNSET, UNSET, UNSET);
338 m->bytesize = 4;
339 m->ncomponents = 1;
340 m->component = 0;
341 break;
343 case MODE_INT:
344 case MODE_FLOAT:
345 case MODE_DECIMAL_FLOAT:
346 case MODE_FRACT:
347 case MODE_UFRACT:
348 case MODE_ACCUM:
349 case MODE_UACCUM:
350 /* A scalar mode must have a byte size, may have a bit size,
351 and must not have components. A float mode must have a
352 format. */
353 validate_mode (m, OPTIONAL, SET, UNSET, UNSET,
354 (m->cl == MODE_FLOAT || m->cl == MODE_DECIMAL_FLOAT)
355 ? SET : UNSET);
357 m->ncomponents = 1;
358 m->component = 0;
359 break;
361 case MODE_OPAQUE:
362 /* Opaque modes have size and precision. */
363 validate_mode (m, OPTIONAL, SET, UNSET, UNSET, UNSET);
365 m->ncomponents = 1;
366 m->component = 0;
367 break;
369 case MODE_PARTIAL_INT:
370 /* A partial integer mode uses ->component to say what the
371 corresponding full-size integer mode is, and may also
372 specify a bit size. */
373 validate_mode (m, OPTIONAL, UNSET, SET, UNSET, UNSET);
375 m->bytesize = m->component->bytesize;
377 m->ncomponents = 1;
378 break;
380 case MODE_COMPLEX_INT:
381 case MODE_COMPLEX_FLOAT:
382 /* Complex modes should have a component indicated, but no more. */
383 validate_mode (m, UNSET, UNSET, SET, UNSET, UNSET);
384 m->ncomponents = 2;
385 if (m->component->precision != (unsigned int)-1)
386 m->precision = 2 * m->component->precision;
387 m->bytesize = 2 * m->component->bytesize;
388 break;
390 case MODE_VECTOR_BOOL:
391 validate_mode (m, UNSET, SET, SET, SET, UNSET);
392 break;
394 case MODE_VECTOR_INT:
395 case MODE_VECTOR_FLOAT:
396 case MODE_VECTOR_FRACT:
397 case MODE_VECTOR_UFRACT:
398 case MODE_VECTOR_ACCUM:
399 case MODE_VECTOR_UACCUM:
400 /* Vector modes should have a component and a number of components. */
401 validate_mode (m, UNSET, UNSET, SET, SET, UNSET);
402 if (m->component->precision != (unsigned int)-1)
403 m->precision = m->ncomponents * m->component->precision;
404 m->bytesize = m->ncomponents * m->component->bytesize;
405 break;
407 default:
408 gcc_unreachable ();
411 /* If not already specified, the mode alignment defaults to the largest
412 power of two that divides the size of the object. Complex types are
413 not more aligned than their contents. */
414 if (m->cl == MODE_COMPLEX_INT || m->cl == MODE_COMPLEX_FLOAT)
415 alignment = m->component->bytesize;
416 else
417 alignment = m->bytesize;
419 m->alignment = alignment & (~alignment + 1);
421 /* If this mode has components, make the component mode point back
422 to this mode, for the sake of adjustments. */
423 if (m->component)
425 m->next_cont = m->component->contained;
426 m->component->contained = m;
430 static void
431 complete_all_modes (void)
433 struct mode_data *m;
434 int cl;
436 for_all_modes (cl, m)
437 complete_mode (m);
440 /* For each mode in class CLASS, construct a corresponding complex mode. */
441 #define COMPLEX_MODES(C) make_complex_modes (MODE_##C, __FILE__, __LINE__)
442 static void
443 make_complex_modes (enum mode_class cl,
444 const char *file, unsigned int line)
446 struct mode_data *m;
447 struct mode_data *c;
448 enum mode_class cclass = complex_class (cl);
450 if (cclass == MODE_RANDOM)
451 return;
453 for (m = modes[cl]; m; m = m->next)
455 char *p, *buf;
456 size_t m_len;
458 /* Skip BImode. FIXME: BImode probably shouldn't be MODE_INT. */
459 if (m->precision == 1)
460 continue;
462 m_len = strlen (m->name);
463 /* The leading "1 +" is in case we prepend a "C" below. */
464 buf = (char *) xmalloc (1 + m_len + 1);
466 /* Float complex modes are named SCmode, etc.
467 Int complex modes are named CSImode, etc.
468 This inconsistency should be eliminated. */
469 p = 0;
470 if (cl == MODE_FLOAT)
472 memcpy (buf, m->name, m_len + 1);
473 p = strchr (buf, 'F');
474 if (p == 0 && strchr (buf, 'D') == 0)
476 error ("%s:%d: float mode \"%s\" has no 'F' or 'D'",
477 m->file, m->line, m->name);
478 free (buf);
479 continue;
482 if (p != 0)
483 *p = 'C';
484 else
486 buf[0] = 'C';
487 memcpy (buf + 1, m->name, m_len + 1);
490 c = new_mode (cclass, buf, file, line);
491 c->component = m;
492 m->complex = c;
496 /* For all modes in class CL, construct vector modes of width WIDTH,
497 having as many components as necessary. ORDER is the sorting order
498 of the mode, with smaller numbers indicating a higher priority. */
499 #define VECTOR_MODES_WITH_PREFIX(PREFIX, C, W, ORDER) \
500 make_vector_modes (MODE_##C, #PREFIX, W, ORDER, __FILE__, __LINE__)
501 #define VECTOR_MODES(C, W) VECTOR_MODES_WITH_PREFIX (V, C, W, 0)
502 static void ATTRIBUTE_UNUSED
503 make_vector_modes (enum mode_class cl, const char *prefix, unsigned int width,
504 unsigned int order, const char *file, unsigned int line)
506 struct mode_data *m;
507 struct mode_data *v;
508 /* Big enough for a 32-bit UINT_MAX plus the text. */
509 char buf[12];
510 unsigned int ncomponents;
511 enum mode_class vclass = vector_class (cl);
513 if (vclass == MODE_RANDOM)
514 return;
516 for (m = modes[cl]; m; m = m->next)
518 /* Do not construct vector modes with only one element, or
519 vector modes where the element size doesn't divide the full
520 size evenly. */
521 ncomponents = width / m->bytesize;
522 if (ncomponents < 2)
523 continue;
524 if (width % m->bytesize)
525 continue;
527 /* Skip QFmode and BImode. FIXME: this special case should
528 not be necessary. */
529 if (cl == MODE_FLOAT && m->bytesize == 1)
530 continue;
531 if (cl == MODE_INT && m->precision == 1)
532 continue;
534 if ((size_t) snprintf (buf, sizeof buf, "%s%u%s", prefix,
535 ncomponents, m->name) >= sizeof buf)
537 error ("%s:%d: mode name \"%s\" is too long",
538 m->file, m->line, m->name);
539 continue;
542 v = new_mode (vclass, xstrdup (buf), file, line);
543 v->order = order;
544 v->component = m;
545 v->ncomponents = ncomponents;
549 /* Create a vector of booleans called NAME with COUNT elements and
550 BYTESIZE bytes in total. */
551 #define VECTOR_BOOL_MODE(NAME, COUNT, BYTESIZE) \
552 make_vector_bool_mode (#NAME, COUNT, BYTESIZE, __FILE__, __LINE__)
553 static void ATTRIBUTE_UNUSED
554 make_vector_bool_mode (const char *name, unsigned int count,
555 unsigned int bytesize, const char *file,
556 unsigned int line)
558 struct mode_data *m = find_mode ("BI");
559 if (!m)
561 error ("%s:%d: no mode \"BI\"", file, line);
562 return;
565 struct mode_data *v = new_mode (MODE_VECTOR_BOOL, name, file, line);
566 v->component = m;
567 v->ncomponents = count;
568 v->bytesize = bytesize;
571 /* Input. */
573 #define _SPECIAL_MODE(C, N) \
574 make_special_mode (MODE_##C, #N, __FILE__, __LINE__)
575 #define RANDOM_MODE(N) _SPECIAL_MODE (RANDOM, N)
576 #define CC_MODE(N) _SPECIAL_MODE (CC, N)
578 static void
579 make_special_mode (enum mode_class cl, const char *name,
580 const char *file, unsigned int line)
582 new_mode (cl, name, file, line);
585 #define INT_MODE(N, Y) FRACTIONAL_INT_MODE (N, -1U, Y)
586 #define FRACTIONAL_INT_MODE(N, B, Y) \
587 make_int_mode (#N, B, Y, __FILE__, __LINE__)
589 static void
590 make_int_mode (const char *name,
591 unsigned int precision, unsigned int bytesize,
592 const char *file, unsigned int line)
594 struct mode_data *m = new_mode (MODE_INT, name, file, line);
595 m->bytesize = bytesize;
596 m->precision = precision;
599 #define OPAQUE_MODE(N, B) \
600 make_opaque_mode (#N, -1U, B, __FILE__, __LINE__)
602 static void ATTRIBUTE_UNUSED
603 make_opaque_mode (const char *name,
604 unsigned int precision,
605 unsigned int bytesize,
606 const char *file, unsigned int line)
608 struct mode_data *m = new_mode (MODE_OPAQUE, name, file, line);
609 m->bytesize = bytesize;
610 m->precision = precision;
613 #define FRACT_MODE(N, Y, F) \
614 make_fixed_point_mode (MODE_FRACT, #N, Y, 0, F, __FILE__, __LINE__)
616 #define UFRACT_MODE(N, Y, F) \
617 make_fixed_point_mode (MODE_UFRACT, #N, Y, 0, F, __FILE__, __LINE__)
619 #define ACCUM_MODE(N, Y, I, F) \
620 make_fixed_point_mode (MODE_ACCUM, #N, Y, I, F, __FILE__, __LINE__)
622 #define UACCUM_MODE(N, Y, I, F) \
623 make_fixed_point_mode (MODE_UACCUM, #N, Y, I, F, __FILE__, __LINE__)
625 /* Create a fixed-point mode by setting CL, NAME, BYTESIZE, IBIT, FBIT,
626 FILE, and LINE. */
628 static void
629 make_fixed_point_mode (enum mode_class cl,
630 const char *name,
631 unsigned int bytesize,
632 unsigned int ibit,
633 unsigned int fbit,
634 const char *file, unsigned int line)
636 struct mode_data *m = new_mode (cl, name, file, line);
637 m->bytesize = bytesize;
638 m->ibit = ibit;
639 m->fbit = fbit;
642 #define FLOAT_MODE(N, Y, F) FRACTIONAL_FLOAT_MODE (N, -1U, Y, F)
643 #define FRACTIONAL_FLOAT_MODE(N, B, Y, F) \
644 make_float_mode (#N, B, Y, #F, __FILE__, __LINE__)
646 static void
647 make_float_mode (const char *name,
648 unsigned int precision, unsigned int bytesize,
649 const char *format,
650 const char *file, unsigned int line)
652 struct mode_data *m = new_mode (MODE_FLOAT, name, file, line);
653 m->bytesize = bytesize;
654 m->precision = precision;
655 m->format = format;
658 #define DECIMAL_FLOAT_MODE(N, Y, F) \
659 FRACTIONAL_DECIMAL_FLOAT_MODE (N, -1U, Y, F)
660 #define FRACTIONAL_DECIMAL_FLOAT_MODE(N, B, Y, F) \
661 make_decimal_float_mode (#N, B, Y, #F, __FILE__, __LINE__)
663 static void
664 make_decimal_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_DECIMAL_FLOAT, name, file, line);
670 m->bytesize = bytesize;
671 m->precision = precision;
672 m->format = format;
675 #define RESET_FLOAT_FORMAT(N, F) \
676 reset_float_format (#N, #F, __FILE__, __LINE__)
677 static void ATTRIBUTE_UNUSED
678 reset_float_format (const char *name, const char *format,
679 const char *file, unsigned int line)
681 struct mode_data *m = find_mode (name);
682 if (!m)
684 error ("%s:%d: no mode \"%s\"", file, line, name);
685 return;
687 if (m->cl != MODE_FLOAT && m->cl != MODE_DECIMAL_FLOAT)
689 error ("%s:%d: mode \"%s\" is not a FLOAT class", file, line, name);
690 return;
692 m->format = format;
695 /* __intN support. */
696 #define INT_N(M,PREC) \
697 make_int_n (#M, PREC, __FILE__, __LINE__)
698 static void ATTRIBUTE_UNUSED
699 make_int_n (const char *m, int bitsize,
700 const char *file, unsigned int line)
702 struct mode_data *component = find_mode (m);
703 if (!component)
705 error ("%s:%d: no mode \"%s\"", file, line, m);
706 return;
708 if (component->cl != MODE_INT
709 && component->cl != MODE_PARTIAL_INT)
711 error ("%s:%d: mode \"%s\" is not class INT or PARTIAL_INT", file, line, m);
712 return;
714 if (component->int_n != 0)
716 error ("%s:%d: mode \"%s\" already has an intN", file, line, m);
717 return;
720 component->int_n = bitsize;
723 /* Partial integer modes are specified by relation to a full integer
724 mode. */
725 #define PARTIAL_INT_MODE(M,PREC,NAME) \
726 make_partial_integer_mode (#M, #NAME, PREC, __FILE__, __LINE__)
727 static void ATTRIBUTE_UNUSED
728 make_partial_integer_mode (const char *base, const char *name,
729 unsigned int precision,
730 const char *file, unsigned int line)
732 struct mode_data *m;
733 struct mode_data *component = find_mode (base);
734 if (!component)
736 error ("%s:%d: no mode \"%s\"", file, line, name);
737 return;
739 if (component->cl != MODE_INT)
741 error ("%s:%d: mode \"%s\" is not class INT", file, line, name);
742 return;
745 m = new_mode (MODE_PARTIAL_INT, name, file, line);
746 m->precision = precision;
747 m->component = component;
750 /* A single vector mode can be specified by naming its component
751 mode and the number of components. */
752 #define VECTOR_MODE_WITH_PREFIX(PREFIX, C, M, N, ORDER) \
753 make_vector_mode (MODE_##C, #PREFIX, #M, N, ORDER, __FILE__, __LINE__);
754 #define VECTOR_MODE(C, M, N) VECTOR_MODE_WITH_PREFIX(V, C, M, N, 0);
755 static void ATTRIBUTE_UNUSED
756 make_vector_mode (enum mode_class bclass,
757 const char *prefix,
758 const char *base,
759 unsigned int ncomponents,
760 unsigned int order,
761 const char *file, unsigned int line)
763 struct mode_data *v;
764 enum mode_class vclass = vector_class (bclass);
765 struct mode_data *component = find_mode (base);
766 char namebuf[16];
768 if (vclass == MODE_RANDOM)
769 return;
770 if (component == 0)
772 error ("%s:%d: no mode \"%s\"", file, line, base);
773 return;
775 if (component->cl != bclass
776 && (component->cl != MODE_PARTIAL_INT
777 || bclass != MODE_INT))
779 error ("%s:%d: mode \"%s\" is not class %s",
780 file, line, base, mode_class_names[bclass] + 5);
781 return;
784 if ((size_t)snprintf (namebuf, sizeof namebuf, "%s%u%s", prefix,
785 ncomponents, base) >= sizeof namebuf)
787 error ("%s:%d: mode name \"%s\" is too long",
788 file, line, base);
789 return;
792 v = new_mode (vclass, xstrdup (namebuf), file, line);
793 v->order = order;
794 v->ncomponents = ncomponents;
795 v->component = component;
798 /* Adjustability. */
799 #define _ADD_ADJUST(A, M, X, C1, C2) \
800 new_adjust (#M, &adj_##A, #A, #X, MODE_##C1, MODE_##C2, __FILE__, __LINE__)
802 #define ADJUST_NUNITS(M, X) _ADD_ADJUST (nunits, M, X, RANDOM, RANDOM)
803 #define ADJUST_BYTESIZE(M, X) _ADD_ADJUST (bytesize, M, X, RANDOM, RANDOM)
804 #define ADJUST_ALIGNMENT(M, X) _ADD_ADJUST (alignment, M, X, RANDOM, RANDOM)
805 #define ADJUST_FLOAT_FORMAT(M, X) _ADD_ADJUST (format, M, X, FLOAT, FLOAT)
806 #define ADJUST_IBIT(M, X) _ADD_ADJUST (ibit, M, X, ACCUM, UACCUM)
807 #define ADJUST_FBIT(M, X) _ADD_ADJUST (fbit, M, X, FRACT, UACCUM)
809 static int bits_per_unit;
810 static int max_bitsize_mode_any_int;
811 static int max_bitsize_mode_any_mode;
813 static void
814 create_modes (void)
816 #include "machmode.def"
818 /* So put the default value unless the target needs a non standard
819 value. */
820 #ifdef BITS_PER_UNIT
821 bits_per_unit = BITS_PER_UNIT;
822 #else
823 bits_per_unit = 8;
824 #endif
826 #ifdef MAX_BITSIZE_MODE_ANY_INT
827 max_bitsize_mode_any_int = MAX_BITSIZE_MODE_ANY_INT;
828 #else
829 max_bitsize_mode_any_int = 0;
830 #endif
832 #ifdef MAX_BITSIZE_MODE_ANY_MODE
833 max_bitsize_mode_any_mode = MAX_BITSIZE_MODE_ANY_MODE;
834 #else
835 max_bitsize_mode_any_mode = 0;
836 #endif
839 #ifndef NUM_POLY_INT_COEFFS
840 #define NUM_POLY_INT_COEFFS 1
841 #endif
843 /* Processing. */
845 /* Sort a list of modes into the order needed for the WIDER field:
846 major sort by precision, minor sort by component precision.
848 For instance:
849 QI < HI < SI < DI < TI
850 V4QI < V2HI < V8QI < V4HI < V2SI.
852 If the precision is not set, sort by the bytesize. A mode with
853 precision set gets sorted before a mode without precision set, if
854 they have the same bytesize; this is the right thing because
855 the precision must always be smaller than the bytesize * BITS_PER_UNIT.
856 We don't have to do anything special to get this done -- an unset
857 precision shows up as (unsigned int)-1, i.e. UINT_MAX. */
858 static int
859 cmp_modes (const void *a, const void *b)
861 const struct mode_data *const m = *(const struct mode_data *const*)a;
862 const struct mode_data *const n = *(const struct mode_data *const*)b;
864 if (m->order > n->order)
865 return 1;
866 else if (m->order < n->order)
867 return -1;
869 if (m->bytesize > n->bytesize)
870 return 1;
871 else if (m->bytesize < n->bytesize)
872 return -1;
874 if (m->precision > n->precision)
875 return 1;
876 else if (m->precision < n->precision)
877 return -1;
879 if (!m->component && !n->component)
881 if (m->counter < n->counter)
882 return -1;
883 else
884 return 1;
887 if (m->component->bytesize > n->component->bytesize)
888 return 1;
889 else if (m->component->bytesize < n->component->bytesize)
890 return -1;
892 if (m->component->precision > n->component->precision)
893 return 1;
894 else if (m->component->precision < n->component->precision)
895 return -1;
897 if (m->counter < n->counter)
898 return -1;
899 else
900 return 1;
903 static void
904 calc_wider_mode (void)
906 int c;
907 struct mode_data *m;
908 struct mode_data **sortbuf;
909 unsigned int max_n_modes = 0;
910 unsigned int i, j;
912 for (c = 0; c < MAX_MODE_CLASS; c++)
913 max_n_modes = MAX (max_n_modes, n_modes[c]);
915 /* Allocate max_n_modes + 1 entries to leave room for the extra null
916 pointer assigned after the qsort call below. */
917 sortbuf = XALLOCAVEC (struct mode_data *, max_n_modes + 1);
919 for (c = 0; c < MAX_MODE_CLASS; c++)
921 /* "wider" is not meaningful for MODE_RANDOM and MODE_CC.
922 However, we want these in textual order, and we have
923 precisely the reverse. */
924 if (c == MODE_RANDOM || c == MODE_CC)
926 struct mode_data *prev, *next;
928 for (prev = 0, m = modes[c]; m; m = next)
930 m->wider = void_mode;
932 /* this is nreverse */
933 next = m->next;
934 m->next = prev;
935 prev = m;
937 modes[c] = prev;
939 else
941 if (!modes[c])
942 continue;
944 for (i = 0, m = modes[c]; m; i++, m = m->next)
945 sortbuf[i] = m;
947 (qsort) (sortbuf, i, sizeof (struct mode_data *), cmp_modes);
949 sortbuf[i] = 0;
950 for (j = 0; j < i; j++)
952 sortbuf[j]->next = sortbuf[j + 1];
953 if (c == MODE_PARTIAL_INT)
954 sortbuf[j]->wider = sortbuf[j]->component;
955 else
956 sortbuf[j]->wider = sortbuf[j]->next;
959 modes[c] = sortbuf[0];
964 /* Text to add to the constant part of a poly_int_pod initializer in
965 order to fill out te whole structure. */
966 #if NUM_POLY_INT_COEFFS == 1
967 #define ZERO_COEFFS ""
968 #elif NUM_POLY_INT_COEFFS == 2
969 #define ZERO_COEFFS ", 0"
970 #else
971 #error "Unknown value of NUM_POLY_INT_COEFFS"
972 #endif
974 /* Output routines. */
976 #define tagged_printf(FMT, ARG, TAG) do { \
977 int count_ = printf (" " FMT ",", ARG); \
978 printf ("%*s/* %s */\n", 27 - count_, "", TAG); \
979 } while (0)
981 #define print_decl(TYPE, NAME, ASIZE) \
982 puts ("\nconst " TYPE " " NAME "[" ASIZE "] =\n{");
984 #define print_maybe_const_decl(TYPE, NAME, ASIZE, NEEDS_ADJ) \
985 printf ("\n" TYPE " " NAME "[" ASIZE "] = \n{\n", \
986 NEEDS_ADJ ? "" : "const ")
988 #define print_closer() puts ("};")
990 /* Compute the max bitsize of some of the classes of integers. It may
991 be that there are needs for the other integer classes, and this
992 code is easy to extend. */
993 static void
994 emit_max_int (void)
996 unsigned int max, mmax;
997 struct mode_data *i;
998 int j;
1000 puts ("");
1002 printf ("#define BITS_PER_UNIT (%d)\n", bits_per_unit);
1004 if (max_bitsize_mode_any_int == 0)
1006 for (max = 1, i = modes[MODE_INT]; i; i = i->next)
1007 if (max < i->bytesize)
1008 max = i->bytesize;
1009 mmax = max;
1010 for (max = 1, i = modes[MODE_PARTIAL_INT]; i; i = i->next)
1011 if (max < i->bytesize)
1012 max = i->bytesize;
1013 if (max > mmax)
1014 mmax = max;
1015 printf ("#define MAX_BITSIZE_MODE_ANY_INT (%d*BITS_PER_UNIT)\n", mmax);
1017 else
1018 printf ("#define MAX_BITSIZE_MODE_ANY_INT %d\n", max_bitsize_mode_any_int);
1020 if (max_bitsize_mode_any_mode == 0)
1022 mmax = 0;
1023 for (j = 0; j < MAX_MODE_CLASS; j++)
1024 for (i = modes[j]; i; i = i->next)
1025 if (mmax < i->bytesize)
1026 mmax = i->bytesize;
1027 printf ("#define MAX_BITSIZE_MODE_ANY_MODE (%d*BITS_PER_UNIT)\n", mmax);
1029 else
1030 printf ("#define MAX_BITSIZE_MODE_ANY_MODE %d\n",
1031 max_bitsize_mode_any_mode);
1034 /* Emit mode_size_inline routine into insn-modes.h header. */
1035 static void
1036 emit_mode_size_inline (void)
1038 int c;
1039 struct mode_adjust *a;
1040 struct mode_data *m;
1042 /* Size adjustments must be propagated to all containing modes. */
1043 for (a = adj_bytesize; a; a = a->next)
1045 a->mode->need_bytesize_adj = true;
1046 for (m = a->mode->contained; m; m = m->next_cont)
1047 m->need_bytesize_adj = true;
1050 /* Changing the number of units by a factor of X also changes the size
1051 by a factor of X. */
1052 for (mode_adjust *a = adj_nunits; a; a = a->next)
1053 a->mode->need_bytesize_adj = true;
1055 printf ("\
1056 #ifdef __cplusplus\n\
1057 inline __attribute__((__always_inline__))\n\
1058 #else\n\
1059 extern __inline__ __attribute__((__always_inline__, __gnu_inline__))\n\
1060 #endif\n\
1061 poly_uint16\n\
1062 mode_size_inline (machine_mode mode)\n\
1063 {\n\
1064 extern %spoly_uint16_pod mode_size[NUM_MACHINE_MODES];\n\
1065 gcc_assert (mode >= 0 && mode < NUM_MACHINE_MODES);\n\
1066 switch (mode)\n\
1067 {\n", adj_nunits || adj_bytesize ? "" : "const ");
1069 for_all_modes (c, m)
1070 if (!m->need_bytesize_adj)
1071 printf (" case E_%smode: return %u;\n", m->name, m->bytesize);
1073 puts ("\
1074 default: return mode_size[mode];\n\
1075 }\n\
1076 }\n");
1079 /* Emit mode_nunits_inline routine into insn-modes.h header. */
1080 static void
1081 emit_mode_nunits_inline (void)
1083 int c;
1084 struct mode_data *m;
1086 for (mode_adjust *a = adj_nunits; a; a = a->next)
1087 a->mode->need_nunits_adj = true;
1089 printf ("\
1090 #ifdef __cplusplus\n\
1091 inline __attribute__((__always_inline__))\n\
1092 #else\n\
1093 extern __inline__ __attribute__((__always_inline__, __gnu_inline__))\n\
1094 #endif\n\
1095 poly_uint16\n\
1096 mode_nunits_inline (machine_mode mode)\n\
1097 {\n\
1098 extern %spoly_uint16_pod mode_nunits[NUM_MACHINE_MODES];\n\
1099 switch (mode)\n\
1100 {\n", adj_nunits ? "" : "const ");
1102 for_all_modes (c, m)
1103 if (!m->need_nunits_adj)
1104 printf (" case E_%smode: return %u;\n", m->name, m->ncomponents);
1106 puts ("\
1107 default: return mode_nunits[mode];\n\
1108 }\n\
1109 }\n");
1112 /* Emit mode_inner_inline routine into insn-modes.h header. */
1113 static void
1114 emit_mode_inner_inline (void)
1116 int c;
1117 struct mode_data *m;
1119 puts ("\
1120 #ifdef __cplusplus\n\
1121 inline __attribute__((__always_inline__))\n\
1122 #else\n\
1123 extern __inline__ __attribute__((__always_inline__, __gnu_inline__))\n\
1124 #endif\n\
1125 unsigned char\n\
1126 mode_inner_inline (machine_mode mode)\n\
1127 {\n\
1128 extern const unsigned char mode_inner[NUM_MACHINE_MODES];\n\
1129 gcc_assert (mode >= 0 && mode < NUM_MACHINE_MODES);\n\
1130 switch (mode)\n\
1131 {");
1133 for_all_modes (c, m)
1134 printf (" case E_%smode: return E_%smode;\n", m->name,
1135 c != MODE_PARTIAL_INT && m->component
1136 ? m->component->name : m->name);
1138 puts ("\
1139 default: return mode_inner[mode];\n\
1140 }\n\
1141 }\n");
1144 /* Emit mode_unit_size_inline routine into insn-modes.h header. */
1145 static void
1146 emit_mode_unit_size_inline (void)
1148 int c;
1149 struct mode_data *m;
1151 puts ("\
1152 #ifdef __cplusplus\n\
1153 inline __attribute__((__always_inline__))\n\
1154 #else\n\
1155 extern __inline__ __attribute__((__always_inline__, __gnu_inline__))\n\
1156 #endif\n\
1157 unsigned char\n\
1158 mode_unit_size_inline (machine_mode mode)\n\
1159 {\n\
1160 extern CONST_MODE_UNIT_SIZE unsigned char mode_unit_size[NUM_MACHINE_MODES];\
1162 gcc_assert (mode >= 0 && mode < NUM_MACHINE_MODES);\n\
1163 switch (mode)\n\
1164 {");
1166 for_all_modes (c, m)
1168 const char *name = m->name;
1169 struct mode_data *m2 = m;
1170 if (c != MODE_PARTIAL_INT && m2->component)
1171 m2 = m2->component;
1172 if (!m2->need_bytesize_adj)
1173 printf (" case E_%smode: return %u;\n", name, m2->bytesize);
1176 puts ("\
1177 default: return mode_unit_size[mode];\n\
1178 }\n\
1179 }\n");
1182 /* Emit mode_unit_precision_inline routine into insn-modes.h header. */
1183 static void
1184 emit_mode_unit_precision_inline (void)
1186 int c;
1187 struct mode_data *m;
1189 puts ("\
1190 #ifdef __cplusplus\n\
1191 inline __attribute__((__always_inline__))\n\
1192 #else\n\
1193 extern __inline__ __attribute__((__always_inline__, __gnu_inline__))\n\
1194 #endif\n\
1195 unsigned short\n\
1196 mode_unit_precision_inline (machine_mode mode)\n\
1197 {\n\
1198 extern const unsigned short mode_unit_precision[NUM_MACHINE_MODES];\n\
1199 gcc_assert (mode >= 0 && mode < NUM_MACHINE_MODES);\n\
1200 switch (mode)\n\
1201 {");
1203 for_all_modes (c, m)
1205 struct mode_data *m2
1206 = (c != MODE_PARTIAL_INT && m->component) ? m->component : m;
1207 if (m2->precision != (unsigned int)-1)
1208 printf (" case E_%smode: return %u;\n", m->name, m2->precision);
1209 else
1210 printf (" case E_%smode: return %u*BITS_PER_UNIT;\n",
1211 m->name, m2->bytesize);
1214 puts ("\
1215 default: return mode_unit_precision[mode];\n\
1216 }\n\
1217 }\n");
1220 /* Return the best machine mode class for MODE, or null if machine_mode
1221 should be used. */
1223 static const char *
1224 get_mode_class (struct mode_data *mode)
1226 switch (mode->cl)
1228 case MODE_INT:
1229 case MODE_PARTIAL_INT:
1230 return "scalar_int_mode";
1232 case MODE_FRACT:
1233 case MODE_UFRACT:
1234 case MODE_ACCUM:
1235 case MODE_UACCUM:
1236 return "scalar_mode";
1238 case MODE_FLOAT:
1239 case MODE_DECIMAL_FLOAT:
1240 return "scalar_float_mode";
1242 case MODE_COMPLEX_INT:
1243 case MODE_COMPLEX_FLOAT:
1244 return "complex_mode";
1246 default:
1247 return NULL;
1251 static void
1252 emit_insn_modes_h (void)
1254 int c;
1255 struct mode_data *m, *first, *last;
1256 int n_int_n_ents = 0;
1258 printf ("/* Generated automatically from machmode.def%s%s\n",
1259 HAVE_EXTRA_MODES ? " and " : "",
1260 EXTRA_MODES_FILE);
1262 puts ("\
1263 by genmodes. */\n\
1265 #ifndef GCC_INSN_MODES_H\n\
1266 #define GCC_INSN_MODES_H\n\
1268 enum machine_mode\n{");
1270 for (c = 0; c < MAX_MODE_CLASS; c++)
1271 for (m = modes[c]; m; m = m->next)
1273 int count_ = printf (" E_%smode,", m->name);
1274 printf ("%*s/* %s:%d */\n", 27 - count_, "",
1275 trim_filename (m->file), m->line);
1276 printf ("#define HAVE_%smode\n", m->name);
1277 printf ("#ifdef USE_ENUM_MODES\n");
1278 printf ("#define %smode E_%smode\n", m->name, m->name);
1279 printf ("#else\n");
1280 if (const char *mode_class = get_mode_class (m))
1281 printf ("#define %smode (%s ((%s::from_int) E_%smode))\n",
1282 m->name, mode_class, mode_class, m->name);
1283 else
1284 printf ("#define %smode ((void) 0, E_%smode)\n",
1285 m->name, m->name);
1286 printf ("#endif\n");
1289 puts (" MAX_MACHINE_MODE,\n");
1291 for (c = 0; c < MAX_MODE_CLASS; c++)
1293 first = modes[c];
1294 last = 0;
1295 for (m = first; m; last = m, m = m->next)
1298 /* Don't use BImode for MIN_MODE_INT, since otherwise the middle
1299 end will try to use it for bitfields in structures and the
1300 like, which we do not want. Only the target md file should
1301 generate BImode widgets. */
1302 if (first && first->precision == 1 && c == MODE_INT)
1303 first = first->next;
1305 if (first && last)
1306 printf (" MIN_%s = E_%smode,\n MAX_%s = E_%smode,\n\n",
1307 mode_class_names[c], first->name,
1308 mode_class_names[c], last->name);
1309 else
1310 printf (" MIN_%s = E_%smode,\n MAX_%s = E_%smode,\n\n",
1311 mode_class_names[c], void_mode->name,
1312 mode_class_names[c], void_mode->name);
1315 puts ("\
1316 NUM_MACHINE_MODES = MAX_MACHINE_MODE\n\
1317 };\n");
1319 /* Define a NUM_* macro for each mode class, giving the number of modes
1320 in the class. */
1321 for (c = 0; c < MAX_MODE_CLASS; c++)
1323 printf ("#define NUM_%s ", mode_class_names[c]);
1324 if (modes[c])
1325 printf ("(MAX_%s - MIN_%s + 1)\n", mode_class_names[c],
1326 mode_class_names[c]);
1327 else
1328 printf ("0\n");
1330 printf ("\n");
1332 /* I can't think of a better idea, can you? */
1333 printf ("#define CONST_MODE_NUNITS%s\n", adj_nunits ? "" : " const");
1334 printf ("#define CONST_MODE_PRECISION%s\n", adj_nunits ? "" : " const");
1335 printf ("#define CONST_MODE_SIZE%s\n",
1336 adj_bytesize || adj_nunits ? "" : " const");
1337 printf ("#define CONST_MODE_UNIT_SIZE%s\n", adj_bytesize ? "" : " const");
1338 printf ("#define CONST_MODE_BASE_ALIGN%s\n", adj_alignment ? "" : " const");
1339 #if 0 /* disabled for backward compatibility, temporary */
1340 printf ("#define CONST_REAL_FORMAT_FOR_MODE%s\n", adj_format ? "" :" const");
1341 #endif
1342 printf ("#define CONST_MODE_IBIT%s\n", adj_ibit ? "" : " const");
1343 printf ("#define CONST_MODE_FBIT%s\n", adj_fbit ? "" : " const");
1344 printf ("#define CONST_MODE_MASK%s\n", adj_nunits ? "" : " const");
1345 emit_max_int ();
1347 for_all_modes (c, m)
1348 if (m->int_n)
1349 n_int_n_ents ++;
1351 printf ("#define NUM_INT_N_ENTS %d\n", n_int_n_ents);
1353 printf ("#define NUM_POLY_INT_COEFFS %d\n", NUM_POLY_INT_COEFFS);
1355 puts ("\
1357 #endif /* insn-modes.h */");
1360 static void
1361 emit_insn_modes_inline_h (void)
1363 printf ("/* Generated automatically from machmode.def%s%s\n",
1364 HAVE_EXTRA_MODES ? " and " : "",
1365 EXTRA_MODES_FILE);
1367 puts ("\
1368 by genmodes. */\n\
1370 #ifndef GCC_INSN_MODES_INLINE_H\n\
1371 #define GCC_INSN_MODES_INLINE_H");
1373 puts ("\n#if !defined (USED_FOR_TARGET) && GCC_VERSION >= 4001\n");
1374 emit_mode_size_inline ();
1375 emit_mode_nunits_inline ();
1376 emit_mode_inner_inline ();
1377 emit_mode_unit_size_inline ();
1378 emit_mode_unit_precision_inline ();
1379 puts ("#endif /* GCC_VERSION >= 4001 */");
1381 puts ("\
1383 #endif /* insn-modes-inline.h */");
1386 static void
1387 emit_insn_modes_c_header (void)
1389 printf ("/* Generated automatically from machmode.def%s%s\n",
1390 HAVE_EXTRA_MODES ? " and " : "",
1391 EXTRA_MODES_FILE);
1393 puts ("\
1394 by genmodes. */\n\
1396 #include \"config.h\"\n\
1397 #include \"system.h\"\n\
1398 #include \"coretypes.h\"\n\
1399 #include \"tm.h\"\n\
1400 #include \"real.h\"");
1403 static void
1404 emit_min_insn_modes_c_header (void)
1406 printf ("/* Generated automatically from machmode.def%s%s\n",
1407 HAVE_EXTRA_MODES ? " and " : "",
1408 EXTRA_MODES_FILE);
1410 puts ("\
1411 by genmodes. */\n\
1413 #include \"bconfig.h\"\n\
1414 #include \"system.h\"\n\
1415 #include \"coretypes.h\"");
1418 static void
1419 emit_mode_name (void)
1421 int c;
1422 struct mode_data *m;
1424 print_decl ("char *const", "mode_name", "NUM_MACHINE_MODES");
1426 for_all_modes (c, m)
1427 printf (" \"%s\",\n", m->name);
1429 print_closer ();
1432 static void
1433 emit_mode_class (void)
1435 int c;
1436 struct mode_data *m;
1438 print_decl ("unsigned char", "mode_class", "NUM_MACHINE_MODES");
1440 for_all_modes (c, m)
1441 tagged_printf ("%s", mode_class_names[m->cl], m->name);
1443 print_closer ();
1446 static void
1447 emit_mode_precision (void)
1449 int c;
1450 struct mode_data *m;
1452 print_maybe_const_decl ("%spoly_uint16_pod", "mode_precision",
1453 "NUM_MACHINE_MODES", adj_nunits);
1455 for_all_modes (c, m)
1456 if (m->precision != (unsigned int)-1)
1457 tagged_printf ("{ %u" ZERO_COEFFS " }", m->precision, m->name);
1458 else
1459 tagged_printf ("{ %u * BITS_PER_UNIT" ZERO_COEFFS " }",
1460 m->bytesize, m->name);
1462 print_closer ();
1465 static void
1466 emit_mode_size (void)
1468 int c;
1469 struct mode_data *m;
1471 print_maybe_const_decl ("%spoly_uint16_pod", "mode_size",
1472 "NUM_MACHINE_MODES", adj_nunits || adj_bytesize);
1474 for_all_modes (c, m)
1475 tagged_printf ("{ %u" ZERO_COEFFS " }", m->bytesize, m->name);
1477 print_closer ();
1480 static void
1481 emit_mode_nunits (void)
1483 int c;
1484 struct mode_data *m;
1486 print_maybe_const_decl ("%spoly_uint16_pod", "mode_nunits",
1487 "NUM_MACHINE_MODES", adj_nunits);
1489 for_all_modes (c, m)
1490 tagged_printf ("{ %u" ZERO_COEFFS " }", m->ncomponents, m->name);
1492 print_closer ();
1495 static void
1496 emit_mode_wider (void)
1498 int c;
1499 struct mode_data *m;
1501 print_decl ("unsigned char", "mode_wider", "NUM_MACHINE_MODES");
1503 for_all_modes (c, m)
1504 tagged_printf ("E_%smode",
1505 m->wider ? m->wider->name : void_mode->name,
1506 m->name);
1508 print_closer ();
1509 print_decl ("unsigned char", "mode_2xwider", "NUM_MACHINE_MODES");
1511 for_all_modes (c, m)
1513 struct mode_data * m2;
1515 for (m2 = m;
1516 m2 && m2 != void_mode;
1517 m2 = m2->wider)
1519 if (m2->bytesize < 2 * m->bytesize)
1520 continue;
1521 if (m->precision != (unsigned int) -1)
1523 if (m2->precision != 2 * m->precision)
1524 continue;
1526 else
1528 if (m2->precision != (unsigned int) -1)
1529 continue;
1532 /* For vectors we want twice the number of components,
1533 with the same element type. */
1534 if (m->cl == MODE_VECTOR_BOOL
1535 || m->cl == MODE_VECTOR_INT
1536 || m->cl == MODE_VECTOR_FLOAT
1537 || m->cl == MODE_VECTOR_FRACT
1538 || m->cl == MODE_VECTOR_UFRACT
1539 || m->cl == MODE_VECTOR_ACCUM
1540 || m->cl == MODE_VECTOR_UACCUM)
1542 if (m2->ncomponents != 2 * m->ncomponents)
1543 continue;
1544 if (m->component != m2->component)
1545 continue;
1548 break;
1550 if (m2 == void_mode)
1551 m2 = 0;
1552 tagged_printf ("E_%smode",
1553 m2 ? m2->name : void_mode->name,
1554 m->name);
1557 print_closer ();
1560 static void
1561 emit_mode_complex (void)
1563 int c;
1564 struct mode_data *m;
1566 print_decl ("unsigned char", "mode_complex", "NUM_MACHINE_MODES");
1568 for_all_modes (c, m)
1569 tagged_printf ("E_%smode",
1570 m->complex ? m->complex->name : void_mode->name,
1571 m->name);
1573 print_closer ();
1576 static void
1577 emit_mode_mask (void)
1579 int c;
1580 struct mode_data *m;
1582 print_maybe_const_decl ("%sunsigned HOST_WIDE_INT", "mode_mask_array",
1583 "NUM_MACHINE_MODES", adj_nunits);
1584 puts ("\
1585 #define MODE_MASK(m) \\\n\
1586 ((m) >= HOST_BITS_PER_WIDE_INT) \\\n\
1587 ? HOST_WIDE_INT_M1U \\\n\
1588 : (HOST_WIDE_INT_1U << (m)) - 1\n");
1590 for_all_modes (c, m)
1591 if (m->precision != (unsigned int)-1)
1592 tagged_printf ("MODE_MASK (%u)", m->precision, m->name);
1593 else
1594 tagged_printf ("MODE_MASK (%u*BITS_PER_UNIT)", m->bytesize, m->name);
1596 puts ("#undef MODE_MASK");
1597 print_closer ();
1600 static void
1601 emit_mode_inner (void)
1603 int c;
1604 struct mode_data *m;
1606 print_decl ("unsigned char", "mode_inner", "NUM_MACHINE_MODES");
1608 for_all_modes (c, m)
1609 tagged_printf ("E_%smode",
1610 c != MODE_PARTIAL_INT && m->component
1611 ? m->component->name : m->name,
1612 m->name);
1614 print_closer ();
1617 /* Emit mode_unit_size array into insn-modes.c file. */
1618 static void
1619 emit_mode_unit_size (void)
1621 int c;
1622 struct mode_data *m;
1624 print_maybe_const_decl ("%sunsigned char", "mode_unit_size",
1625 "NUM_MACHINE_MODES", adj_bytesize);
1627 for_all_modes (c, m)
1628 tagged_printf ("%u",
1629 c != MODE_PARTIAL_INT && m->component
1630 ? m->component->bytesize : m->bytesize, m->name);
1632 print_closer ();
1635 /* Emit mode_unit_precision array into insn-modes.c file. */
1636 static void
1637 emit_mode_unit_precision (void)
1639 int c;
1640 struct mode_data *m;
1642 print_decl ("unsigned short", "mode_unit_precision", "NUM_MACHINE_MODES");
1644 for_all_modes (c, m)
1646 struct mode_data *m2 = (c != MODE_PARTIAL_INT && m->component) ?
1647 m->component : m;
1648 if (m2->precision != (unsigned int)-1)
1649 tagged_printf ("%u", m2->precision, m->name);
1650 else
1651 tagged_printf ("%u*BITS_PER_UNIT", m2->bytesize, m->name);
1654 print_closer ();
1658 static void
1659 emit_mode_base_align (void)
1661 int c;
1662 struct mode_data *m;
1664 print_maybe_const_decl ("%sunsigned short",
1665 "mode_base_align", "NUM_MACHINE_MODES",
1666 adj_alignment);
1668 for_all_modes (c, m)
1669 tagged_printf ("%u", m->alignment, m->name);
1671 print_closer ();
1674 static void
1675 emit_class_narrowest_mode (void)
1677 int c;
1679 print_decl ("unsigned char", "class_narrowest_mode", "MAX_MODE_CLASS");
1681 for (c = 0; c < MAX_MODE_CLASS; c++)
1682 /* Bleah, all this to get the comment right for MIN_MODE_INT. */
1683 tagged_printf ("MIN_%s", mode_class_names[c],
1684 modes[c]
1685 ? ((c != MODE_INT || modes[c]->precision != 1)
1686 ? modes[c]->name
1687 : (modes[c]->next
1688 ? modes[c]->next->name
1689 : void_mode->name))
1690 : void_mode->name);
1692 print_closer ();
1695 static void
1696 emit_real_format_for_mode (void)
1698 struct mode_data *m;
1700 /* The entities pointed to by this table are constant, whether
1701 or not the table itself is constant.
1703 For backward compatibility this table is always writable
1704 (several targets modify it in TARGET_OPTION_OVERRIDE). FIXME:
1705 convert all said targets to use ADJUST_FORMAT instead. */
1706 #if 0
1707 print_maybe_const_decl ("const struct real_format *%s",
1708 "real_format_for_mode",
1709 "MAX_MODE_FLOAT - MIN_MODE_FLOAT + 1",
1710 format);
1711 #else
1712 print_decl ("struct real_format *\n", "real_format_for_mode",
1713 "MAX_MODE_FLOAT - MIN_MODE_FLOAT + 1 "
1714 "+ MAX_MODE_DECIMAL_FLOAT - MIN_MODE_DECIMAL_FLOAT + 1");
1715 #endif
1717 /* The beginning of the table is entries for float modes. */
1718 for (m = modes[MODE_FLOAT]; m; m = m->next)
1719 if (!strcmp (m->format, "0"))
1720 tagged_printf ("%s", m->format, m->name);
1721 else
1722 tagged_printf ("&%s", m->format, m->name);
1724 /* The end of the table is entries for decimal float modes. */
1725 for (m = modes[MODE_DECIMAL_FLOAT]; m; m = m->next)
1726 if (!strcmp (m->format, "0"))
1727 tagged_printf ("%s", m->format, m->name);
1728 else
1729 tagged_printf ("&%s", m->format, m->name);
1731 print_closer ();
1734 static void
1735 emit_mode_adjustments (void)
1737 struct mode_adjust *a;
1738 struct mode_data *m;
1740 if (adj_nunits)
1741 printf ("\n"
1742 "void\n"
1743 "adjust_mode_mask (machine_mode mode)\n"
1744 "{\n"
1745 " unsigned int precision;\n"
1746 " if (GET_MODE_PRECISION (mode).is_constant (&precision)\n"
1747 " && precision < HOST_BITS_PER_WIDE_INT)\n"
1748 " mode_mask_array[mode] = (HOST_WIDE_INT_1U << precision) - 1;"
1749 "\n"
1750 " else\n"
1751 " mode_mask_array[mode] = HOST_WIDE_INT_M1U;\n"
1752 "}\n");
1754 puts ("\
1755 \nvoid\
1756 \ninit_adjust_machine_modes (void)\
1757 \n{\
1758 \n poly_uint16 ps ATTRIBUTE_UNUSED;\n\
1759 size_t s ATTRIBUTE_UNUSED;");
1761 for (a = adj_nunits; a; a = a->next)
1763 m = a->mode;
1764 printf ("\n"
1765 " {\n"
1766 " /* %s:%d */\n ps = %s;\n",
1767 a->file, a->line, a->adjustment);
1768 printf (" int old_factor = vector_element_size"
1769 " (mode_precision[E_%smode], mode_nunits[E_%smode]);\n",
1770 m->name, m->name);
1771 printf (" mode_precision[E_%smode] = ps * old_factor;\n", m->name);
1772 printf (" mode_size[E_%smode] = exact_div (mode_precision[E_%smode],"
1773 " BITS_PER_UNIT);\n", m->name, m->name);
1774 printf (" mode_nunits[E_%smode] = ps;\n", m->name);
1775 printf (" adjust_mode_mask (E_%smode);\n", m->name);
1776 printf (" }\n");
1779 /* Size adjustments must be propagated to all containing modes.
1780 A size adjustment forces us to recalculate the alignment too. */
1781 for (a = adj_bytesize; a; a = a->next)
1783 printf ("\n /* %s:%d */\n", a->file, a->line);
1784 switch (a->mode->cl)
1786 case MODE_VECTOR_BOOL:
1787 case MODE_VECTOR_INT:
1788 case MODE_VECTOR_FLOAT:
1789 case MODE_VECTOR_FRACT:
1790 case MODE_VECTOR_UFRACT:
1791 case MODE_VECTOR_ACCUM:
1792 case MODE_VECTOR_UACCUM:
1793 printf (" ps = %s;\n", a->adjustment);
1794 printf (" s = mode_unit_size[E_%smode];\n", a->mode->name);
1795 break;
1797 default:
1798 printf (" ps = s = %s;\n", a->adjustment);
1799 printf (" mode_unit_size[E_%smode] = s;\n", a->mode->name);
1800 break;
1802 printf (" mode_size[E_%smode] = ps;\n", a->mode->name);
1803 printf (" mode_base_align[E_%smode] = known_alignment (ps);\n",
1804 a->mode->name);
1806 for (m = a->mode->contained; m; m = m->next_cont)
1808 switch (m->cl)
1810 case MODE_COMPLEX_INT:
1811 case MODE_COMPLEX_FLOAT:
1812 printf (" mode_size[E_%smode] = 2*s;\n", m->name);
1813 printf (" mode_unit_size[E_%smode] = s;\n", m->name);
1814 printf (" mode_base_align[E_%smode] = s & (~s + 1);\n",
1815 m->name);
1816 break;
1818 case MODE_VECTOR_BOOL:
1819 /* Changes to BImode should not affect vector booleans. */
1820 break;
1822 case MODE_VECTOR_INT:
1823 case MODE_VECTOR_FLOAT:
1824 case MODE_VECTOR_FRACT:
1825 case MODE_VECTOR_UFRACT:
1826 case MODE_VECTOR_ACCUM:
1827 case MODE_VECTOR_UACCUM:
1828 printf (" mode_size[E_%smode] = %d * ps;\n",
1829 m->name, m->ncomponents);
1830 printf (" mode_unit_size[E_%smode] = s;\n", m->name);
1831 printf (" mode_base_align[E_%smode]"
1832 " = known_alignment (%d * ps);\n",
1833 m->name, m->ncomponents);
1834 break;
1836 default:
1837 internal_error (
1838 "mode %s is neither vector nor complex but contains %s",
1839 m->name, a->mode->name);
1840 /* NOTREACHED */
1845 /* Alignment adjustments propagate too.
1846 ??? This may not be the right thing for vector modes. */
1847 for (a = adj_alignment; a; a = a->next)
1849 printf ("\n /* %s:%d */\n s = %s;\n",
1850 a->file, a->line, a->adjustment);
1851 printf (" mode_base_align[E_%smode] = s;\n", a->mode->name);
1853 for (m = a->mode->contained; m; m = m->next_cont)
1855 switch (m->cl)
1857 case MODE_COMPLEX_INT:
1858 case MODE_COMPLEX_FLOAT:
1859 printf (" mode_base_align[E_%smode] = s;\n", m->name);
1860 break;
1862 case MODE_VECTOR_BOOL:
1863 /* Changes to BImode should not affect vector booleans. */
1864 break;
1866 case MODE_VECTOR_INT:
1867 case MODE_VECTOR_FLOAT:
1868 case MODE_VECTOR_FRACT:
1869 case MODE_VECTOR_UFRACT:
1870 case MODE_VECTOR_ACCUM:
1871 case MODE_VECTOR_UACCUM:
1872 printf (" mode_base_align[E_%smode] = %d*s;\n",
1873 m->name, m->ncomponents);
1874 break;
1876 default:
1877 internal_error (
1878 "mode %s is neither vector nor complex but contains %s",
1879 m->name, a->mode->name);
1880 /* NOTREACHED */
1885 /* Ibit adjustments don't have to propagate. */
1886 for (a = adj_ibit; a; a = a->next)
1888 printf ("\n /* %s:%d */\n s = %s;\n",
1889 a->file, a->line, a->adjustment);
1890 printf (" mode_ibit[E_%smode] = s;\n", a->mode->name);
1893 /* Fbit adjustments don't have to propagate. */
1894 for (a = adj_fbit; a; a = a->next)
1896 printf ("\n /* %s:%d */\n s = %s;\n",
1897 a->file, a->line, a->adjustment);
1898 printf (" mode_fbit[E_%smode] = s;\n", a->mode->name);
1901 /* Real mode formats don't have to propagate anywhere. */
1902 for (a = adj_format; a; a = a->next)
1903 printf ("\n /* %s:%d */\n REAL_MODE_FORMAT (E_%smode) = %s;\n",
1904 a->file, a->line, a->mode->name, a->adjustment);
1906 puts ("}");
1909 /* Emit ibit for all modes. */
1911 static void
1912 emit_mode_ibit (void)
1914 int c;
1915 struct mode_data *m;
1917 print_maybe_const_decl ("%sunsigned char",
1918 "mode_ibit", "NUM_MACHINE_MODES",
1919 adj_ibit);
1921 for_all_modes (c, m)
1922 tagged_printf ("%u", m->ibit, m->name);
1924 print_closer ();
1927 /* Emit fbit for all modes. */
1929 static void
1930 emit_mode_fbit (void)
1932 int c;
1933 struct mode_data *m;
1935 print_maybe_const_decl ("%sunsigned char",
1936 "mode_fbit", "NUM_MACHINE_MODES",
1937 adj_fbit);
1939 for_all_modes (c, m)
1940 tagged_printf ("%u", m->fbit, m->name);
1942 print_closer ();
1945 /* Emit __intN for all modes. */
1947 static void
1948 emit_mode_int_n (void)
1950 int c;
1951 struct mode_data *m;
1952 struct mode_data **mode_sort;
1953 int n_modes = 0;
1954 int i, j;
1956 print_decl ("int_n_data_t", "int_n_data", "");
1958 n_modes = 0;
1959 for_all_modes (c, m)
1960 if (m->int_n)
1961 n_modes ++;
1962 mode_sort = XALLOCAVEC (struct mode_data *, n_modes);
1964 n_modes = 0;
1965 for_all_modes (c, m)
1966 if (m->int_n)
1967 mode_sort[n_modes++] = m;
1969 /* Yes, this is a bubblesort, but there are at most four (and
1970 usually only 1-2) entries to sort. */
1971 for (i = 0; i<n_modes - 1; i++)
1972 for (j = i + 1; j < n_modes; j++)
1973 if (mode_sort[i]->int_n > mode_sort[j]->int_n)
1974 std::swap (mode_sort[i], mode_sort[j]);
1976 for (i = 0; i < n_modes; i ++)
1978 m = mode_sort[i];
1979 printf(" {\n");
1980 tagged_printf ("%u", m->int_n, m->name);
1981 printf ("{ E_%smode },", m->name);
1982 printf(" },\n");
1985 print_closer ();
1989 static void
1990 emit_insn_modes_c (void)
1992 emit_insn_modes_c_header ();
1993 emit_mode_name ();
1994 emit_mode_class ();
1995 emit_mode_precision ();
1996 emit_mode_size ();
1997 emit_mode_nunits ();
1998 emit_mode_wider ();
1999 emit_mode_complex ();
2000 emit_mode_mask ();
2001 emit_mode_inner ();
2002 emit_mode_unit_size ();
2003 emit_mode_unit_precision ();
2004 emit_mode_base_align ();
2005 emit_class_narrowest_mode ();
2006 emit_real_format_for_mode ();
2007 emit_mode_adjustments ();
2008 emit_mode_ibit ();
2009 emit_mode_fbit ();
2010 emit_mode_int_n ();
2013 static void
2014 emit_min_insn_modes_c (void)
2016 emit_min_insn_modes_c_header ();
2017 emit_mode_name ();
2018 emit_mode_class ();
2019 emit_mode_nunits ();
2020 emit_mode_wider ();
2021 emit_mode_inner ();
2022 emit_class_narrowest_mode ();
2025 /* Master control. */
2027 main (int argc, char **argv)
2029 bool gen_header = false, gen_inlines = false, gen_min = false;
2030 progname = argv[0];
2032 if (argc == 1)
2034 else if (argc == 2 && !strcmp (argv[1], "-h"))
2035 gen_header = true;
2036 else if (argc == 2 && !strcmp (argv[1], "-i"))
2037 gen_inlines = true;
2038 else if (argc == 2 && !strcmp (argv[1], "-m"))
2039 gen_min = true;
2040 else
2042 error ("usage: %s [-h|-i|-m] > file", progname);
2043 return FATAL_EXIT_CODE;
2046 modes_by_name = htab_create_alloc (64, hash_mode, eq_mode, 0, xcalloc, free);
2048 create_modes ();
2049 complete_all_modes ();
2051 if (have_error)
2052 return FATAL_EXIT_CODE;
2054 calc_wider_mode ();
2056 if (gen_header)
2057 emit_insn_modes_h ();
2058 else if (gen_inlines)
2059 emit_insn_modes_inline_h ();
2060 else if (gen_min)
2061 emit_min_insn_modes_c ();
2062 else
2063 emit_insn_modes_c ();
2065 if (fflush (stdout) || fclose (stdout))
2066 return FATAL_EXIT_CODE;
2067 return SUCCESS_EXIT_CODE;