Allow targets to pick a vector prefix other than "V"
[official-gcc.git] / gcc / genmodes.c
blob7d58865ca96e5a5b7ec392e9cdae97c4d252c734
1 /* Generate the machine mode enumeration and associated tables.
2 Copyright (C) 2003-2018 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 precision; /* size in bits, equiv to TYPE_PRECISION */
57 unsigned int bytesize; /* storage size in addressable units */
58 unsigned int ncomponents; /* number of subunits */
59 unsigned int alignment; /* mode alignment */
60 const char *format; /* floating point format - float modes only */
62 struct mode_data *component; /* mode of components */
63 struct mode_data *wider; /* next wider mode */
65 struct mode_data *contained; /* Pointer to list of modes that have
66 this mode as a component. */
67 struct mode_data *next_cont; /* Next mode in that list. */
69 struct mode_data *complex; /* complex type with mode as component. */
70 const char *file; /* file and line of definition, */
71 unsigned int line; /* for error reporting */
72 unsigned int counter; /* Rank ordering of modes */
73 unsigned int ibit; /* the number of integral bits */
74 unsigned int fbit; /* the number of fractional bits */
75 bool need_bytesize_adj; /* true if this mode need dynamic size
76 adjustment */
77 unsigned int int_n; /* If nonzero, then __int<INT_N> will be defined */
80 static struct mode_data *modes[MAX_MODE_CLASS];
81 static unsigned int n_modes[MAX_MODE_CLASS];
82 static struct mode_data *void_mode;
84 static const struct mode_data blank_mode = {
85 0, "<unknown>", MAX_MODE_CLASS,
86 -1U, -1U, -1U, -1U,
87 0, 0, 0, 0, 0, 0,
88 "<unknown>", 0, 0, 0, 0, false, 0
91 static htab_t modes_by_name;
93 /* Data structure for recording target-specified runtime adjustments
94 to a particular mode. We support varying the byte size, the
95 alignment, and the floating point format. */
96 struct mode_adjust
98 struct mode_adjust *next;
99 struct mode_data *mode;
100 const char *adjustment;
102 const char *file;
103 unsigned int line;
106 static struct mode_adjust *adj_bytesize;
107 static struct mode_adjust *adj_alignment;
108 static struct mode_adjust *adj_format;
109 static struct mode_adjust *adj_ibit;
110 static struct mode_adjust *adj_fbit;
112 /* Mode class operations. */
113 static enum mode_class
114 complex_class (enum mode_class c)
116 switch (c)
118 case MODE_INT: return MODE_COMPLEX_INT;
119 case MODE_FLOAT: return MODE_COMPLEX_FLOAT;
120 default:
121 error ("no complex class for class %s", mode_class_names[c]);
122 return MODE_RANDOM;
126 static enum mode_class
127 vector_class (enum mode_class cl)
129 switch (cl)
131 case MODE_INT: return MODE_VECTOR_INT;
132 case MODE_FLOAT: return MODE_VECTOR_FLOAT;
133 case MODE_FRACT: return MODE_VECTOR_FRACT;
134 case MODE_UFRACT: return MODE_VECTOR_UFRACT;
135 case MODE_ACCUM: return MODE_VECTOR_ACCUM;
136 case MODE_UACCUM: return MODE_VECTOR_UACCUM;
137 default:
138 error ("no vector class for class %s", mode_class_names[cl]);
139 return MODE_RANDOM;
143 /* Utility routines. */
144 static inline struct mode_data *
145 find_mode (const char *name)
147 struct mode_data key;
149 key.name = name;
150 return (struct mode_data *) htab_find (modes_by_name, &key);
153 static struct mode_data *
154 new_mode (enum mode_class cl, const char *name,
155 const char *file, unsigned int line)
157 struct mode_data *m;
158 static unsigned int count = 0;
160 m = find_mode (name);
161 if (m)
163 error ("%s:%d: duplicate definition of mode \"%s\"",
164 trim_filename (file), line, name);
165 error ("%s:%d: previous definition here", m->file, m->line);
166 return m;
169 m = XNEW (struct mode_data);
170 memcpy (m, &blank_mode, sizeof (struct mode_data));
171 m->cl = cl;
172 m->name = name;
173 if (file)
174 m->file = trim_filename (file);
175 m->line = line;
176 m->counter = count++;
178 m->next = modes[cl];
179 modes[cl] = m;
180 n_modes[cl]++;
182 *htab_find_slot (modes_by_name, m, INSERT) = m;
184 return m;
187 static hashval_t
188 hash_mode (const void *p)
190 const struct mode_data *m = (const struct mode_data *)p;
191 return htab_hash_string (m->name);
194 static int
195 eq_mode (const void *p, const void *q)
197 const struct mode_data *a = (const struct mode_data *)p;
198 const struct mode_data *b = (const struct mode_data *)q;
200 return !strcmp (a->name, b->name);
203 #define for_all_modes(C, M) \
204 for (C = 0; C < MAX_MODE_CLASS; C++) \
205 for (M = modes[C]; M; M = M->next)
207 static void ATTRIBUTE_UNUSED
208 new_adjust (const char *name,
209 struct mode_adjust **category, const char *catname,
210 const char *adjustment,
211 enum mode_class required_class_from,
212 enum mode_class required_class_to,
213 const char *file, unsigned int line)
215 struct mode_data *mode = find_mode (name);
216 struct mode_adjust *a;
218 file = trim_filename (file);
220 if (!mode)
222 error ("%s:%d: no mode \"%s\"", file, line, name);
223 return;
226 if (required_class_from != MODE_RANDOM
227 && (mode->cl < required_class_from || mode->cl > required_class_to))
229 error ("%s:%d: mode \"%s\" is not among class {%s, %s}",
230 file, line, name, mode_class_names[required_class_from] + 5,
231 mode_class_names[required_class_to] + 5);
232 return;
235 for (a = *category; a; a = a->next)
236 if (a->mode == mode)
238 error ("%s:%d: mode \"%s\" already has a %s adjustment",
239 file, line, name, catname);
240 error ("%s:%d: previous adjustment here", a->file, a->line);
241 return;
244 a = XNEW (struct mode_adjust);
245 a->mode = mode;
246 a->adjustment = adjustment;
247 a->file = file;
248 a->line = line;
250 a->next = *category;
251 *category = a;
254 /* Diagnose failure to meet expectations in a partially filled out
255 mode structure. */
256 enum requirement { SET, UNSET, OPTIONAL };
258 #define validate_field_(mname, fname, req, val, unset, file, line) do { \
259 switch (req) \
261 case SET: \
262 if (val == unset) \
263 error ("%s:%d: (%s) field %s must be set", \
264 file, line, mname, fname); \
265 break; \
266 case UNSET: \
267 if (val != unset) \
268 error ("%s:%d: (%s) field %s must not be set", \
269 file, line, mname, fname); \
270 case OPTIONAL: \
271 break; \
273 } while (0)
275 #define validate_field(M, F) \
276 validate_field_(M->name, #F, r_##F, M->F, blank_mode.F, M->file, M->line)
278 static void
279 validate_mode (struct mode_data *m,
280 enum requirement r_precision,
281 enum requirement r_bytesize,
282 enum requirement r_component,
283 enum requirement r_ncomponents,
284 enum requirement r_format)
286 validate_field (m, precision);
287 validate_field (m, bytesize);
288 validate_field (m, component);
289 validate_field (m, ncomponents);
290 validate_field (m, format);
292 #undef validate_field
293 #undef validate_field_
295 /* Given a partially-filled-out mode structure, figure out what we can
296 and fill the rest of it in; die if it isn't enough. */
297 static void
298 complete_mode (struct mode_data *m)
300 unsigned int alignment;
302 if (!m->name)
304 error ("%s:%d: mode with no name", m->file, m->line);
305 return;
307 if (m->cl == MAX_MODE_CLASS)
309 error ("%s:%d: %smode has no mode class", m->file, m->line, m->name);
310 return;
313 switch (m->cl)
315 case MODE_RANDOM:
316 /* Nothing more need be said. */
317 if (!strcmp (m->name, "VOID"))
318 void_mode = m;
320 validate_mode (m, UNSET, UNSET, UNSET, UNSET, UNSET);
322 m->precision = 0;
323 m->bytesize = 0;
324 m->ncomponents = 0;
325 m->component = 0;
326 break;
328 case MODE_CC:
329 /* Again, nothing more need be said. For historical reasons,
330 the size of a CC mode is four units. */
331 validate_mode (m, UNSET, UNSET, UNSET, UNSET, UNSET);
333 m->bytesize = 4;
334 m->ncomponents = 1;
335 m->component = 0;
336 break;
338 case MODE_INT:
339 case MODE_POINTER_BOUNDS:
340 case MODE_FLOAT:
341 case MODE_DECIMAL_FLOAT:
342 case MODE_FRACT:
343 case MODE_UFRACT:
344 case MODE_ACCUM:
345 case MODE_UACCUM:
346 /* A scalar mode must have a byte size, may have a bit size,
347 and must not have components. A float mode must have a
348 format. */
349 validate_mode (m, OPTIONAL, SET, UNSET, UNSET,
350 (m->cl == MODE_FLOAT || m->cl == MODE_DECIMAL_FLOAT)
351 ? SET : UNSET);
353 m->ncomponents = 1;
354 m->component = 0;
355 break;
357 case MODE_PARTIAL_INT:
358 /* A partial integer mode uses ->component to say what the
359 corresponding full-size integer mode is, and may also
360 specify a bit size. */
361 validate_mode (m, OPTIONAL, UNSET, SET, UNSET, UNSET);
363 m->bytesize = m->component->bytesize;
365 m->ncomponents = 1;
366 break;
368 case MODE_COMPLEX_INT:
369 case MODE_COMPLEX_FLOAT:
370 /* Complex modes should have a component indicated, but no more. */
371 validate_mode (m, UNSET, UNSET, SET, UNSET, UNSET);
372 m->ncomponents = 2;
373 if (m->component->precision != (unsigned int)-1)
374 m->precision = 2 * m->component->precision;
375 m->bytesize = 2 * m->component->bytesize;
376 break;
378 case MODE_VECTOR_BOOL:
379 validate_mode (m, UNSET, SET, SET, SET, UNSET);
380 break;
382 case MODE_VECTOR_INT:
383 case MODE_VECTOR_FLOAT:
384 case MODE_VECTOR_FRACT:
385 case MODE_VECTOR_UFRACT:
386 case MODE_VECTOR_ACCUM:
387 case MODE_VECTOR_UACCUM:
388 /* Vector modes should have a component and a number of components. */
389 validate_mode (m, UNSET, UNSET, SET, SET, UNSET);
390 if (m->component->precision != (unsigned int)-1)
391 m->precision = m->ncomponents * m->component->precision;
392 m->bytesize = m->ncomponents * m->component->bytesize;
393 break;
395 default:
396 gcc_unreachable ();
399 /* If not already specified, the mode alignment defaults to the largest
400 power of two that divides the size of the object. Complex types are
401 not more aligned than their contents. */
402 if (m->cl == MODE_COMPLEX_INT || m->cl == MODE_COMPLEX_FLOAT)
403 alignment = m->component->bytesize;
404 else
405 alignment = m->bytesize;
407 m->alignment = alignment & (~alignment + 1);
409 /* If this mode has components, make the component mode point back
410 to this mode, for the sake of adjustments. */
411 if (m->component)
413 m->next_cont = m->component->contained;
414 m->component->contained = m;
418 static void
419 complete_all_modes (void)
421 struct mode_data *m;
422 int cl;
424 for_all_modes (cl, m)
425 complete_mode (m);
428 /* For each mode in class CLASS, construct a corresponding complex mode. */
429 #define COMPLEX_MODES(C) make_complex_modes (MODE_##C, __FILE__, __LINE__)
430 static void
431 make_complex_modes (enum mode_class cl,
432 const char *file, unsigned int line)
434 struct mode_data *m;
435 struct mode_data *c;
436 enum mode_class cclass = complex_class (cl);
438 if (cclass == MODE_RANDOM)
439 return;
441 for (m = modes[cl]; m; m = m->next)
443 char *p, *buf;
444 size_t m_len;
446 /* Skip BImode. FIXME: BImode probably shouldn't be MODE_INT. */
447 if (m->precision == 1)
448 continue;
450 m_len = strlen (m->name);
451 /* The leading "1 +" is in case we prepend a "C" below. */
452 buf = (char *) xmalloc (1 + m_len + 1);
454 /* Float complex modes are named SCmode, etc.
455 Int complex modes are named CSImode, etc.
456 This inconsistency should be eliminated. */
457 p = 0;
458 if (cl == MODE_FLOAT)
460 memcpy (buf, m->name, m_len + 1);
461 p = strchr (buf, 'F');
462 if (p == 0 && strchr (buf, 'D') == 0)
464 error ("%s:%d: float mode \"%s\" has no 'F' or 'D'",
465 m->file, m->line, m->name);
466 free (buf);
467 continue;
470 if (p != 0)
471 *p = 'C';
472 else
474 buf[0] = 'C';
475 memcpy (buf + 1, m->name, m_len + 1);
478 c = new_mode (cclass, buf, file, line);
479 c->component = m;
480 m->complex = c;
484 /* For all modes in class CL, construct vector modes of width
485 WIDTH, having as many components as necessary. */
486 #define VECTOR_MODES_WITH_PREFIX(PREFIX, C, W) \
487 make_vector_modes (MODE_##C, #PREFIX, W, __FILE__, __LINE__)
488 #define VECTOR_MODES(C, W) VECTOR_MODES_WITH_PREFIX (V, C, W)
489 static void ATTRIBUTE_UNUSED
490 make_vector_modes (enum mode_class cl, const char *prefix, unsigned int width,
491 const char *file, unsigned int line)
493 struct mode_data *m;
494 struct mode_data *v;
495 /* Big enough for a 32-bit UINT_MAX plus the text. */
496 char buf[12];
497 unsigned int ncomponents;
498 enum mode_class vclass = vector_class (cl);
500 if (vclass == MODE_RANDOM)
501 return;
503 for (m = modes[cl]; m; m = m->next)
505 /* Do not construct vector modes with only one element, or
506 vector modes where the element size doesn't divide the full
507 size evenly. */
508 ncomponents = width / m->bytesize;
509 if (ncomponents < 2)
510 continue;
511 if (width % m->bytesize)
512 continue;
514 /* Skip QFmode and BImode. FIXME: this special case should
515 not be necessary. */
516 if (cl == MODE_FLOAT && m->bytesize == 1)
517 continue;
518 if (cl == MODE_INT && m->precision == 1)
519 continue;
521 if ((size_t) snprintf (buf, sizeof buf, "%s%u%s", prefix,
522 ncomponents, m->name) >= sizeof buf)
524 error ("%s:%d: mode name \"%s\" is too long",
525 m->file, m->line, m->name);
526 continue;
529 v = new_mode (vclass, xstrdup (buf), file, line);
530 v->component = m;
531 v->ncomponents = ncomponents;
535 /* Create a vector of booleans called NAME with COUNT elements and
536 BYTESIZE bytes in total. */
537 #define VECTOR_BOOL_MODE(NAME, COUNT, BYTESIZE) \
538 make_vector_bool_mode (#NAME, COUNT, BYTESIZE, __FILE__, __LINE__)
539 static void ATTRIBUTE_UNUSED
540 make_vector_bool_mode (const char *name, unsigned int count,
541 unsigned int bytesize, const char *file,
542 unsigned int line)
544 struct mode_data *m = find_mode ("BI");
545 if (!m)
547 error ("%s:%d: no mode \"BI\"", file, line);
548 return;
551 struct mode_data *v = new_mode (MODE_VECTOR_BOOL, name, file, line);
552 v->component = m;
553 v->ncomponents = count;
554 v->bytesize = bytesize;
557 /* Input. */
559 #define _SPECIAL_MODE(C, N) \
560 make_special_mode (MODE_##C, #N, __FILE__, __LINE__)
561 #define RANDOM_MODE(N) _SPECIAL_MODE (RANDOM, N)
562 #define CC_MODE(N) _SPECIAL_MODE (CC, N)
564 static void
565 make_special_mode (enum mode_class cl, const char *name,
566 const char *file, unsigned int line)
568 new_mode (cl, name, file, line);
571 #define POINTER_BOUNDS_MODE(N, Y) \
572 make_pointer_bounds_mode (#N, Y, __FILE__, __LINE__)
574 static void ATTRIBUTE_UNUSED
575 make_pointer_bounds_mode (const char *name,
576 unsigned int bytesize,
577 const char *file, unsigned int line)
579 struct mode_data *m = new_mode (MODE_POINTER_BOUNDS, name, file, line);
580 m->bytesize = bytesize;
584 #define INT_MODE(N, Y) FRACTIONAL_INT_MODE (N, -1U, Y)
585 #define FRACTIONAL_INT_MODE(N, B, Y) \
586 make_int_mode (#N, B, Y, __FILE__, __LINE__)
588 static void
589 make_int_mode (const char *name,
590 unsigned int precision, unsigned int bytesize,
591 const char *file, unsigned int line)
593 struct mode_data *m = new_mode (MODE_INT, name, file, line);
594 m->bytesize = bytesize;
595 m->precision = precision;
598 #define FRACT_MODE(N, Y, F) \
599 make_fixed_point_mode (MODE_FRACT, #N, Y, 0, F, __FILE__, __LINE__)
601 #define UFRACT_MODE(N, Y, F) \
602 make_fixed_point_mode (MODE_UFRACT, #N, Y, 0, F, __FILE__, __LINE__)
604 #define ACCUM_MODE(N, Y, I, F) \
605 make_fixed_point_mode (MODE_ACCUM, #N, Y, I, F, __FILE__, __LINE__)
607 #define UACCUM_MODE(N, Y, I, F) \
608 make_fixed_point_mode (MODE_UACCUM, #N, Y, I, F, __FILE__, __LINE__)
610 /* Create a fixed-point mode by setting CL, NAME, BYTESIZE, IBIT, FBIT,
611 FILE, and LINE. */
613 static void
614 make_fixed_point_mode (enum mode_class cl,
615 const char *name,
616 unsigned int bytesize,
617 unsigned int ibit,
618 unsigned int fbit,
619 const char *file, unsigned int line)
621 struct mode_data *m = new_mode (cl, name, file, line);
622 m->bytesize = bytesize;
623 m->ibit = ibit;
624 m->fbit = fbit;
627 #define FLOAT_MODE(N, Y, F) FRACTIONAL_FLOAT_MODE (N, -1U, Y, F)
628 #define FRACTIONAL_FLOAT_MODE(N, B, Y, F) \
629 make_float_mode (#N, B, Y, #F, __FILE__, __LINE__)
631 static void
632 make_float_mode (const char *name,
633 unsigned int precision, unsigned int bytesize,
634 const char *format,
635 const char *file, unsigned int line)
637 struct mode_data *m = new_mode (MODE_FLOAT, name, file, line);
638 m->bytesize = bytesize;
639 m->precision = precision;
640 m->format = format;
643 #define DECIMAL_FLOAT_MODE(N, Y, F) \
644 FRACTIONAL_DECIMAL_FLOAT_MODE (N, -1U, Y, F)
645 #define FRACTIONAL_DECIMAL_FLOAT_MODE(N, B, Y, F) \
646 make_decimal_float_mode (#N, B, Y, #F, __FILE__, __LINE__)
648 static void
649 make_decimal_float_mode (const char *name,
650 unsigned int precision, unsigned int bytesize,
651 const char *format,
652 const char *file, unsigned int line)
654 struct mode_data *m = new_mode (MODE_DECIMAL_FLOAT, name, file, line);
655 m->bytesize = bytesize;
656 m->precision = precision;
657 m->format = format;
660 #define RESET_FLOAT_FORMAT(N, F) \
661 reset_float_format (#N, #F, __FILE__, __LINE__)
662 static void ATTRIBUTE_UNUSED
663 reset_float_format (const char *name, const char *format,
664 const char *file, unsigned int line)
666 struct mode_data *m = find_mode (name);
667 if (!m)
669 error ("%s:%d: no mode \"%s\"", file, line, name);
670 return;
672 if (m->cl != MODE_FLOAT && m->cl != MODE_DECIMAL_FLOAT)
674 error ("%s:%d: mode \"%s\" is not a FLOAT class", file, line, name);
675 return;
677 m->format = format;
680 /* __intN support. */
681 #define INT_N(M,PREC) \
682 make_int_n (#M, PREC, __FILE__, __LINE__)
683 static void ATTRIBUTE_UNUSED
684 make_int_n (const char *m, int bitsize,
685 const char *file, unsigned int line)
687 struct mode_data *component = find_mode (m);
688 if (!component)
690 error ("%s:%d: no mode \"%s\"", file, line, m);
691 return;
693 if (component->cl != MODE_INT
694 && component->cl != MODE_PARTIAL_INT)
696 error ("%s:%d: mode \"%s\" is not class INT or PARTIAL_INT", file, line, m);
697 return;
699 if (component->int_n != 0)
701 error ("%s:%d: mode \"%s\" already has an intN", file, line, m);
702 return;
705 component->int_n = bitsize;
708 /* Partial integer modes are specified by relation to a full integer
709 mode. */
710 #define PARTIAL_INT_MODE(M,PREC,NAME) \
711 make_partial_integer_mode (#M, #NAME, PREC, __FILE__, __LINE__)
712 static void ATTRIBUTE_UNUSED
713 make_partial_integer_mode (const char *base, const char *name,
714 unsigned int precision,
715 const char *file, unsigned int line)
717 struct mode_data *m;
718 struct mode_data *component = find_mode (base);
719 if (!component)
721 error ("%s:%d: no mode \"%s\"", file, line, name);
722 return;
724 if (component->cl != MODE_INT)
726 error ("%s:%d: mode \"%s\" is not class INT", file, line, name);
727 return;
730 m = new_mode (MODE_PARTIAL_INT, name, file, line);
731 m->precision = precision;
732 m->component = component;
735 /* A single vector mode can be specified by naming its component
736 mode and the number of components. */
737 #define VECTOR_MODE(C, M, N) \
738 make_vector_mode (MODE_##C, #M, N, __FILE__, __LINE__);
739 static void ATTRIBUTE_UNUSED
740 make_vector_mode (enum mode_class bclass,
741 const char *base,
742 unsigned int ncomponents,
743 const char *file, unsigned int line)
745 struct mode_data *v;
746 enum mode_class vclass = vector_class (bclass);
747 struct mode_data *component = find_mode (base);
748 char namebuf[16];
750 if (vclass == MODE_RANDOM)
751 return;
752 if (component == 0)
754 error ("%s:%d: no mode \"%s\"", file, line, base);
755 return;
757 if (component->cl != bclass
758 && (component->cl != MODE_PARTIAL_INT
759 || bclass != MODE_INT))
761 error ("%s:%d: mode \"%s\" is not class %s",
762 file, line, base, mode_class_names[bclass] + 5);
763 return;
766 if ((size_t)snprintf (namebuf, sizeof namebuf, "V%u%s",
767 ncomponents, base) >= sizeof namebuf)
769 error ("%s:%d: mode name \"%s\" is too long",
770 file, line, base);
771 return;
774 v = new_mode (vclass, xstrdup (namebuf), file, line);
775 v->ncomponents = ncomponents;
776 v->component = component;
779 /* Adjustability. */
780 #define _ADD_ADJUST(A, M, X, C1, C2) \
781 new_adjust (#M, &adj_##A, #A, #X, MODE_##C1, MODE_##C2, __FILE__, __LINE__)
783 #define ADJUST_BYTESIZE(M, X) _ADD_ADJUST (bytesize, M, X, RANDOM, RANDOM)
784 #define ADJUST_ALIGNMENT(M, X) _ADD_ADJUST (alignment, M, X, RANDOM, RANDOM)
785 #define ADJUST_FLOAT_FORMAT(M, X) _ADD_ADJUST (format, M, X, FLOAT, FLOAT)
786 #define ADJUST_IBIT(M, X) _ADD_ADJUST (ibit, M, X, ACCUM, UACCUM)
787 #define ADJUST_FBIT(M, X) _ADD_ADJUST (fbit, M, X, FRACT, UACCUM)
789 static int bits_per_unit;
790 static int max_bitsize_mode_any_int;
792 static void
793 create_modes (void)
795 #include "machmode.def"
797 /* So put the default value unless the target needs a non standard
798 value. */
799 #ifdef BITS_PER_UNIT
800 bits_per_unit = BITS_PER_UNIT;
801 #else
802 bits_per_unit = 8;
803 #endif
805 #ifdef MAX_BITSIZE_MODE_ANY_INT
806 max_bitsize_mode_any_int = MAX_BITSIZE_MODE_ANY_INT;
807 #else
808 max_bitsize_mode_any_int = 0;
809 #endif
812 #ifndef NUM_POLY_INT_COEFFS
813 #define NUM_POLY_INT_COEFFS 1
814 #endif
816 /* Processing. */
818 /* Sort a list of modes into the order needed for the WIDER field:
819 major sort by precision, minor sort by component precision.
821 For instance:
822 QI < HI < SI < DI < TI
823 V4QI < V2HI < V8QI < V4HI < V2SI.
825 If the precision is not set, sort by the bytesize. A mode with
826 precision set gets sorted before a mode without precision set, if
827 they have the same bytesize; this is the right thing because
828 the precision must always be smaller than the bytesize * BITS_PER_UNIT.
829 We don't have to do anything special to get this done -- an unset
830 precision shows up as (unsigned int)-1, i.e. UINT_MAX. */
831 static int
832 cmp_modes (const void *a, const void *b)
834 const struct mode_data *const m = *(const struct mode_data *const*)a;
835 const struct mode_data *const n = *(const struct mode_data *const*)b;
837 if (m->bytesize > n->bytesize)
838 return 1;
839 else if (m->bytesize < n->bytesize)
840 return -1;
842 if (m->precision > n->precision)
843 return 1;
844 else if (m->precision < n->precision)
845 return -1;
847 if (!m->component && !n->component)
849 if (m->counter < n->counter)
850 return -1;
851 else
852 return 1;
855 if (m->component->bytesize > n->component->bytesize)
856 return 1;
857 else if (m->component->bytesize < n->component->bytesize)
858 return -1;
860 if (m->component->precision > n->component->precision)
861 return 1;
862 else if (m->component->precision < n->component->precision)
863 return -1;
865 if (m->counter < n->counter)
866 return -1;
867 else
868 return 1;
871 static void
872 calc_wider_mode (void)
874 int c;
875 struct mode_data *m;
876 struct mode_data **sortbuf;
877 unsigned int max_n_modes = 0;
878 unsigned int i, j;
880 for (c = 0; c < MAX_MODE_CLASS; c++)
881 max_n_modes = MAX (max_n_modes, n_modes[c]);
883 /* Allocate max_n_modes + 1 entries to leave room for the extra null
884 pointer assigned after the qsort call below. */
885 sortbuf = XALLOCAVEC (struct mode_data *, max_n_modes + 1);
887 for (c = 0; c < MAX_MODE_CLASS; c++)
889 /* "wider" is not meaningful for MODE_RANDOM and MODE_CC.
890 However, we want these in textual order, and we have
891 precisely the reverse. */
892 if (c == MODE_RANDOM || c == MODE_CC)
894 struct mode_data *prev, *next;
896 for (prev = 0, m = modes[c]; m; m = next)
898 m->wider = void_mode;
900 /* this is nreverse */
901 next = m->next;
902 m->next = prev;
903 prev = m;
905 modes[c] = prev;
907 else
909 if (!modes[c])
910 continue;
912 for (i = 0, m = modes[c]; m; i++, m = m->next)
913 sortbuf[i] = m;
915 (qsort) (sortbuf, i, sizeof (struct mode_data *), cmp_modes);
917 sortbuf[i] = 0;
918 for (j = 0; j < i; j++)
920 sortbuf[j]->next = sortbuf[j + 1];
921 if (c == MODE_PARTIAL_INT)
922 sortbuf[j]->wider = sortbuf[j]->component;
923 else
924 sortbuf[j]->wider = sortbuf[j]->next;
927 modes[c] = sortbuf[0];
932 /* Text to add to the constant part of a poly_int_pod initializer in
933 order to fill out te whole structure. */
934 #if NUM_POLY_INT_COEFFS == 1
935 #define ZERO_COEFFS ""
936 #elif NUM_POLY_INT_COEFFS == 2
937 #define ZERO_COEFFS ", 0"
938 #else
939 #error "Unknown value of NUM_POLY_INT_COEFFS"
940 #endif
942 /* Output routines. */
944 #define tagged_printf(FMT, ARG, TAG) do { \
945 int count_ = printf (" " FMT ",", ARG); \
946 printf ("%*s/* %s */\n", 27 - count_, "", TAG); \
947 } while (0)
949 #define print_decl(TYPE, NAME, ASIZE) \
950 puts ("\nconst " TYPE " " NAME "[" ASIZE "] =\n{");
952 #define print_maybe_const_decl(TYPE, NAME, ASIZE, CATEGORY) \
953 printf ("\n" TYPE " " NAME "[" ASIZE "] = \n{\n", \
954 adj_##CATEGORY ? "" : "const ")
956 #define print_closer() puts ("};")
958 /* Compute the max bitsize of some of the classes of integers. It may
959 be that there are needs for the other integer classes, and this
960 code is easy to extend. */
961 static void
962 emit_max_int (void)
964 unsigned int max, mmax;
965 struct mode_data *i;
966 int j;
968 puts ("");
970 printf ("#define BITS_PER_UNIT (%d)\n", bits_per_unit);
972 if (max_bitsize_mode_any_int == 0)
974 for (max = 1, i = modes[MODE_INT]; i; i = i->next)
975 if (max < i->bytesize)
976 max = i->bytesize;
977 mmax = max;
978 for (max = 1, i = modes[MODE_PARTIAL_INT]; i; i = i->next)
979 if (max < i->bytesize)
980 max = i->bytesize;
981 if (max > mmax)
982 mmax = max;
983 printf ("#define MAX_BITSIZE_MODE_ANY_INT (%d*BITS_PER_UNIT)\n", mmax);
985 else
986 printf ("#define MAX_BITSIZE_MODE_ANY_INT %d\n", max_bitsize_mode_any_int);
988 mmax = 0;
989 for (j = 0; j < MAX_MODE_CLASS; j++)
990 for (i = modes[j]; i; i = i->next)
991 if (mmax < i->bytesize)
992 mmax = i->bytesize;
993 printf ("#define MAX_BITSIZE_MODE_ANY_MODE (%d*BITS_PER_UNIT)\n", mmax);
996 /* Emit mode_size_inline routine into insn-modes.h header. */
997 static void
998 emit_mode_size_inline (void)
1000 int c;
1001 struct mode_adjust *a;
1002 struct mode_data *m;
1004 /* Size adjustments must be propagated to all containing modes. */
1005 for (a = adj_bytesize; a; a = a->next)
1007 a->mode->need_bytesize_adj = true;
1008 for (m = a->mode->contained; m; m = m->next_cont)
1009 m->need_bytesize_adj = true;
1012 printf ("\
1013 #ifdef __cplusplus\n\
1014 inline __attribute__((__always_inline__))\n\
1015 #else\n\
1016 extern __inline__ __attribute__((__always_inline__, __gnu_inline__))\n\
1017 #endif\n\
1018 poly_uint16\n\
1019 mode_size_inline (machine_mode mode)\n\
1020 {\n\
1021 extern %spoly_uint16_pod mode_size[NUM_MACHINE_MODES];\n\
1022 gcc_assert (mode >= 0 && mode < NUM_MACHINE_MODES);\n\
1023 switch (mode)\n\
1024 {\n", adj_bytesize ? "" : "const ");
1026 for_all_modes (c, m)
1027 if (!m->need_bytesize_adj)
1028 printf (" case E_%smode: return %u;\n", m->name, m->bytesize);
1030 puts ("\
1031 default: return mode_size[mode];\n\
1032 }\n\
1033 }\n");
1036 /* Emit mode_nunits_inline routine into insn-modes.h header. */
1037 static void
1038 emit_mode_nunits_inline (void)
1040 int c;
1041 struct mode_data *m;
1043 puts ("\
1044 #ifdef __cplusplus\n\
1045 inline __attribute__((__always_inline__))\n\
1046 #else\n\
1047 extern __inline__ __attribute__((__always_inline__, __gnu_inline__))\n\
1048 #endif\n\
1049 poly_uint16\n\
1050 mode_nunits_inline (machine_mode mode)\n\
1051 {\n\
1052 extern poly_uint16_pod mode_nunits[NUM_MACHINE_MODES];\n\
1053 switch (mode)\n\
1054 {");
1056 for_all_modes (c, m)
1057 printf (" case E_%smode: return %u;\n", m->name, m->ncomponents);
1059 puts ("\
1060 default: return mode_nunits[mode];\n\
1061 }\n\
1062 }\n");
1065 /* Emit mode_inner_inline routine into insn-modes.h header. */
1066 static void
1067 emit_mode_inner_inline (void)
1069 int c;
1070 struct mode_data *m;
1072 puts ("\
1073 #ifdef __cplusplus\n\
1074 inline __attribute__((__always_inline__))\n\
1075 #else\n\
1076 extern __inline__ __attribute__((__always_inline__, __gnu_inline__))\n\
1077 #endif\n\
1078 unsigned char\n\
1079 mode_inner_inline (machine_mode mode)\n\
1080 {\n\
1081 extern const unsigned char mode_inner[NUM_MACHINE_MODES];\n\
1082 gcc_assert (mode >= 0 && mode < NUM_MACHINE_MODES);\n\
1083 switch (mode)\n\
1084 {");
1086 for_all_modes (c, m)
1087 printf (" case E_%smode: return E_%smode;\n", m->name,
1088 c != MODE_PARTIAL_INT && m->component
1089 ? m->component->name : m->name);
1091 puts ("\
1092 default: return mode_inner[mode];\n\
1093 }\n\
1094 }\n");
1097 /* Emit mode_unit_size_inline routine into insn-modes.h header. */
1098 static void
1099 emit_mode_unit_size_inline (void)
1101 int c;
1102 struct mode_data *m;
1104 puts ("\
1105 #ifdef __cplusplus\n\
1106 inline __attribute__((__always_inline__))\n\
1107 #else\n\
1108 extern __inline__ __attribute__((__always_inline__, __gnu_inline__))\n\
1109 #endif\n\
1110 unsigned char\n\
1111 mode_unit_size_inline (machine_mode mode)\n\
1112 {\n\
1113 extern CONST_MODE_UNIT_SIZE unsigned char mode_unit_size[NUM_MACHINE_MODES];\
1115 gcc_assert (mode >= 0 && mode < NUM_MACHINE_MODES);\n\
1116 switch (mode)\n\
1117 {");
1119 for_all_modes (c, m)
1121 const char *name = m->name;
1122 struct mode_data *m2 = m;
1123 if (c != MODE_PARTIAL_INT && m2->component)
1124 m2 = m2->component;
1125 if (!m2->need_bytesize_adj)
1126 printf (" case E_%smode: return %u;\n", name, m2->bytesize);
1129 puts ("\
1130 default: return mode_unit_size[mode];\n\
1131 }\n\
1132 }\n");
1135 /* Emit mode_unit_precision_inline routine into insn-modes.h header. */
1136 static void
1137 emit_mode_unit_precision_inline (void)
1139 int c;
1140 struct mode_data *m;
1142 puts ("\
1143 #ifdef __cplusplus\n\
1144 inline __attribute__((__always_inline__))\n\
1145 #else\n\
1146 extern __inline__ __attribute__((__always_inline__, __gnu_inline__))\n\
1147 #endif\n\
1148 unsigned short\n\
1149 mode_unit_precision_inline (machine_mode mode)\n\
1150 {\n\
1151 extern const unsigned short mode_unit_precision[NUM_MACHINE_MODES];\n\
1152 gcc_assert (mode >= 0 && mode < NUM_MACHINE_MODES);\n\
1153 switch (mode)\n\
1154 {");
1156 for_all_modes (c, m)
1158 struct mode_data *m2
1159 = (c != MODE_PARTIAL_INT && m->component) ? m->component : m;
1160 if (m2->precision != (unsigned int)-1)
1161 printf (" case E_%smode: return %u;\n", m->name, m2->precision);
1162 else
1163 printf (" case E_%smode: return %u*BITS_PER_UNIT;\n",
1164 m->name, m2->bytesize);
1167 puts ("\
1168 default: return mode_unit_precision[mode];\n\
1169 }\n\
1170 }\n");
1173 /* Return the best machine mode class for MODE, or null if machine_mode
1174 should be used. */
1176 static const char *
1177 get_mode_class (struct mode_data *mode)
1179 switch (mode->cl)
1181 case MODE_INT:
1182 case MODE_PARTIAL_INT:
1183 return "scalar_int_mode";
1185 case MODE_FRACT:
1186 case MODE_UFRACT:
1187 case MODE_ACCUM:
1188 case MODE_UACCUM:
1189 case MODE_POINTER_BOUNDS:
1190 return "scalar_mode";
1192 case MODE_FLOAT:
1193 case MODE_DECIMAL_FLOAT:
1194 return "scalar_float_mode";
1196 case MODE_COMPLEX_INT:
1197 case MODE_COMPLEX_FLOAT:
1198 return "complex_mode";
1200 default:
1201 return NULL;
1205 static void
1206 emit_insn_modes_h (void)
1208 int c;
1209 struct mode_data *m, *first, *last;
1210 int n_int_n_ents = 0;
1212 printf ("/* Generated automatically from machmode.def%s%s\n",
1213 HAVE_EXTRA_MODES ? " and " : "",
1214 EXTRA_MODES_FILE);
1216 puts ("\
1217 by genmodes. */\n\
1219 #ifndef GCC_INSN_MODES_H\n\
1220 #define GCC_INSN_MODES_H\n\
1222 enum machine_mode\n{");
1224 for (c = 0; c < MAX_MODE_CLASS; c++)
1225 for (m = modes[c]; m; m = m->next)
1227 int count_ = printf (" E_%smode,", m->name);
1228 printf ("%*s/* %s:%d */\n", 27 - count_, "",
1229 trim_filename (m->file), m->line);
1230 printf ("#define HAVE_%smode\n", m->name);
1231 printf ("#ifdef USE_ENUM_MODES\n");
1232 printf ("#define %smode E_%smode\n", m->name, m->name);
1233 printf ("#else\n");
1234 if (const char *mode_class = get_mode_class (m))
1235 printf ("#define %smode (%s ((%s::from_int) E_%smode))\n",
1236 m->name, mode_class, mode_class, m->name);
1237 else
1238 printf ("#define %smode ((void) 0, E_%smode)\n",
1239 m->name, m->name);
1240 printf ("#endif\n");
1243 puts (" MAX_MACHINE_MODE,\n");
1245 for (c = 0; c < MAX_MODE_CLASS; c++)
1247 first = modes[c];
1248 last = 0;
1249 for (m = first; m; last = m, m = m->next)
1252 /* Don't use BImode for MIN_MODE_INT, since otherwise the middle
1253 end will try to use it for bitfields in structures and the
1254 like, which we do not want. Only the target md file should
1255 generate BImode widgets. */
1256 if (first && first->precision == 1 && c == MODE_INT)
1257 first = first->next;
1259 if (first && last)
1260 printf (" MIN_%s = E_%smode,\n MAX_%s = E_%smode,\n\n",
1261 mode_class_names[c], first->name,
1262 mode_class_names[c], last->name);
1263 else
1264 printf (" MIN_%s = E_%smode,\n MAX_%s = E_%smode,\n\n",
1265 mode_class_names[c], void_mode->name,
1266 mode_class_names[c], void_mode->name);
1269 puts ("\
1270 NUM_MACHINE_MODES = MAX_MACHINE_MODE\n\
1271 };\n");
1273 /* I can't think of a better idea, can you? */
1274 printf ("#define CONST_MODE_SIZE%s\n", adj_bytesize ? "" : " const");
1275 printf ("#define CONST_MODE_UNIT_SIZE%s\n", adj_bytesize ? "" : " const");
1276 printf ("#define CONST_MODE_BASE_ALIGN%s\n", adj_alignment ? "" : " const");
1277 #if 0 /* disabled for backward compatibility, temporary */
1278 printf ("#define CONST_REAL_FORMAT_FOR_MODE%s\n", adj_format ? "" :" const");
1279 #endif
1280 printf ("#define CONST_MODE_IBIT%s\n", adj_ibit ? "" : " const");
1281 printf ("#define CONST_MODE_FBIT%s\n", adj_fbit ? "" : " const");
1282 emit_max_int ();
1284 for_all_modes (c, m)
1285 if (m->int_n)
1286 n_int_n_ents ++;
1288 printf ("#define NUM_INT_N_ENTS %d\n", n_int_n_ents);
1290 printf ("#define NUM_POLY_INT_COEFFS %d\n", NUM_POLY_INT_COEFFS);
1292 puts ("\
1294 #endif /* insn-modes.h */");
1297 static void
1298 emit_insn_modes_inline_h (void)
1300 printf ("/* Generated automatically from machmode.def%s%s\n",
1301 HAVE_EXTRA_MODES ? " and " : "",
1302 EXTRA_MODES_FILE);
1304 puts ("\
1305 by genmodes. */\n\
1307 #ifndef GCC_INSN_MODES_INLINE_H\n\
1308 #define GCC_INSN_MODES_INLINE_H");
1310 puts ("\n#if !defined (USED_FOR_TARGET) && GCC_VERSION >= 4001\n");
1311 emit_mode_size_inline ();
1312 emit_mode_nunits_inline ();
1313 emit_mode_inner_inline ();
1314 emit_mode_unit_size_inline ();
1315 emit_mode_unit_precision_inline ();
1316 puts ("#endif /* GCC_VERSION >= 4001 */");
1318 puts ("\
1320 #endif /* insn-modes-inline.h */");
1323 static void
1324 emit_insn_modes_c_header (void)
1326 printf ("/* Generated automatically from machmode.def%s%s\n",
1327 HAVE_EXTRA_MODES ? " and " : "",
1328 EXTRA_MODES_FILE);
1330 puts ("\
1331 by genmodes. */\n\
1333 #include \"config.h\"\n\
1334 #include \"system.h\"\n\
1335 #include \"coretypes.h\"\n\
1336 #include \"tm.h\"\n\
1337 #include \"real.h\"");
1340 static void
1341 emit_min_insn_modes_c_header (void)
1343 printf ("/* Generated automatically from machmode.def%s%s\n",
1344 HAVE_EXTRA_MODES ? " and " : "",
1345 EXTRA_MODES_FILE);
1347 puts ("\
1348 by genmodes. */\n\
1350 #include \"bconfig.h\"\n\
1351 #include \"system.h\"\n\
1352 #include \"coretypes.h\"");
1355 static void
1356 emit_mode_name (void)
1358 int c;
1359 struct mode_data *m;
1361 print_decl ("char *const", "mode_name", "NUM_MACHINE_MODES");
1363 for_all_modes (c, m)
1364 printf (" \"%s\",\n", m->name);
1366 print_closer ();
1369 static void
1370 emit_mode_class (void)
1372 int c;
1373 struct mode_data *m;
1375 print_decl ("unsigned char", "mode_class", "NUM_MACHINE_MODES");
1377 for_all_modes (c, m)
1378 tagged_printf ("%s", mode_class_names[m->cl], m->name);
1380 print_closer ();
1383 static void
1384 emit_mode_precision (void)
1386 int c;
1387 struct mode_data *m;
1389 print_decl ("poly_uint16_pod", "mode_precision", "NUM_MACHINE_MODES");
1391 for_all_modes (c, m)
1392 if (m->precision != (unsigned int)-1)
1393 tagged_printf ("{ %u" ZERO_COEFFS " }", m->precision, m->name);
1394 else
1395 tagged_printf ("{ %u * BITS_PER_UNIT" ZERO_COEFFS " }",
1396 m->bytesize, m->name);
1398 print_closer ();
1401 static void
1402 emit_mode_size (void)
1404 int c;
1405 struct mode_data *m;
1407 print_maybe_const_decl ("%spoly_uint16_pod", "mode_size",
1408 "NUM_MACHINE_MODES", bytesize);
1410 for_all_modes (c, m)
1411 tagged_printf ("{ %u" ZERO_COEFFS " }", m->bytesize, m->name);
1413 print_closer ();
1416 static void
1417 emit_mode_nunits (void)
1419 int c;
1420 struct mode_data *m;
1422 print_decl ("poly_uint16_pod", "mode_nunits", "NUM_MACHINE_MODES");
1424 for_all_modes (c, m)
1425 tagged_printf ("{ %u" ZERO_COEFFS " }", m->ncomponents, m->name);
1427 print_closer ();
1430 static void
1431 emit_mode_wider (void)
1433 int c;
1434 struct mode_data *m;
1436 print_decl ("unsigned char", "mode_wider", "NUM_MACHINE_MODES");
1438 for_all_modes (c, m)
1439 tagged_printf ("E_%smode",
1440 m->wider ? m->wider->name : void_mode->name,
1441 m->name);
1443 print_closer ();
1444 print_decl ("unsigned char", "mode_2xwider", "NUM_MACHINE_MODES");
1446 for_all_modes (c, m)
1448 struct mode_data * m2;
1450 for (m2 = m;
1451 m2 && m2 != void_mode;
1452 m2 = m2->wider)
1454 if (m2->bytesize < 2 * m->bytesize)
1455 continue;
1456 if (m->precision != (unsigned int) -1)
1458 if (m2->precision != 2 * m->precision)
1459 continue;
1461 else
1463 if (m2->precision != (unsigned int) -1)
1464 continue;
1467 /* For vectors we want twice the number of components,
1468 with the same element type. */
1469 if (m->cl == MODE_VECTOR_BOOL
1470 || m->cl == MODE_VECTOR_INT
1471 || m->cl == MODE_VECTOR_FLOAT
1472 || m->cl == MODE_VECTOR_FRACT
1473 || m->cl == MODE_VECTOR_UFRACT
1474 || m->cl == MODE_VECTOR_ACCUM
1475 || m->cl == MODE_VECTOR_UACCUM)
1477 if (m2->ncomponents != 2 * m->ncomponents)
1478 continue;
1479 if (m->component != m2->component)
1480 continue;
1483 break;
1485 if (m2 == void_mode)
1486 m2 = 0;
1487 tagged_printf ("E_%smode",
1488 m2 ? m2->name : void_mode->name,
1489 m->name);
1492 print_closer ();
1495 static void
1496 emit_mode_complex (void)
1498 int c;
1499 struct mode_data *m;
1501 print_decl ("unsigned char", "mode_complex", "NUM_MACHINE_MODES");
1503 for_all_modes (c, m)
1504 tagged_printf ("E_%smode",
1505 m->complex ? m->complex->name : void_mode->name,
1506 m->name);
1508 print_closer ();
1511 static void
1512 emit_mode_mask (void)
1514 int c;
1515 struct mode_data *m;
1517 print_decl ("unsigned HOST_WIDE_INT", "mode_mask_array",
1518 "NUM_MACHINE_MODES");
1519 puts ("\
1520 #define MODE_MASK(m) \\\n\
1521 ((m) >= HOST_BITS_PER_WIDE_INT) \\\n\
1522 ? HOST_WIDE_INT_M1U \\\n\
1523 : (HOST_WIDE_INT_1U << (m)) - 1\n");
1525 for_all_modes (c, m)
1526 if (m->precision != (unsigned int)-1)
1527 tagged_printf ("MODE_MASK (%u)", m->precision, m->name);
1528 else
1529 tagged_printf ("MODE_MASK (%u*BITS_PER_UNIT)", m->bytesize, m->name);
1531 puts ("#undef MODE_MASK");
1532 print_closer ();
1535 static void
1536 emit_mode_inner (void)
1538 int c;
1539 struct mode_data *m;
1541 print_decl ("unsigned char", "mode_inner", "NUM_MACHINE_MODES");
1543 for_all_modes (c, m)
1544 tagged_printf ("E_%smode",
1545 c != MODE_PARTIAL_INT && m->component
1546 ? m->component->name : m->name,
1547 m->name);
1549 print_closer ();
1552 /* Emit mode_unit_size array into insn-modes.c file. */
1553 static void
1554 emit_mode_unit_size (void)
1556 int c;
1557 struct mode_data *m;
1559 print_maybe_const_decl ("%sunsigned char", "mode_unit_size",
1560 "NUM_MACHINE_MODES", bytesize);
1562 for_all_modes (c, m)
1563 tagged_printf ("%u",
1564 c != MODE_PARTIAL_INT && m->component
1565 ? m->component->bytesize : m->bytesize, m->name);
1567 print_closer ();
1570 /* Emit mode_unit_precision array into insn-modes.c file. */
1571 static void
1572 emit_mode_unit_precision (void)
1574 int c;
1575 struct mode_data *m;
1577 print_decl ("unsigned short", "mode_unit_precision", "NUM_MACHINE_MODES");
1579 for_all_modes (c, m)
1581 struct mode_data *m2 = (c != MODE_PARTIAL_INT && m->component) ?
1582 m->component : m;
1583 if (m2->precision != (unsigned int)-1)
1584 tagged_printf ("%u", m2->precision, m->name);
1585 else
1586 tagged_printf ("%u*BITS_PER_UNIT", m2->bytesize, m->name);
1589 print_closer ();
1593 static void
1594 emit_mode_base_align (void)
1596 int c;
1597 struct mode_data *m;
1599 print_maybe_const_decl ("%sunsigned short",
1600 "mode_base_align", "NUM_MACHINE_MODES",
1601 alignment);
1603 for_all_modes (c, m)
1604 tagged_printf ("%u", m->alignment, m->name);
1606 print_closer ();
1609 static void
1610 emit_class_narrowest_mode (void)
1612 int c;
1614 print_decl ("unsigned char", "class_narrowest_mode", "MAX_MODE_CLASS");
1616 for (c = 0; c < MAX_MODE_CLASS; c++)
1617 /* Bleah, all this to get the comment right for MIN_MODE_INT. */
1618 tagged_printf ("MIN_%s", mode_class_names[c],
1619 modes[c]
1620 ? ((c != MODE_INT || modes[c]->precision != 1)
1621 ? modes[c]->name
1622 : (modes[c]->next
1623 ? modes[c]->next->name
1624 : void_mode->name))
1625 : void_mode->name);
1627 print_closer ();
1630 static void
1631 emit_real_format_for_mode (void)
1633 struct mode_data *m;
1635 /* The entities pointed to by this table are constant, whether
1636 or not the table itself is constant.
1638 For backward compatibility this table is always writable
1639 (several targets modify it in TARGET_OPTION_OVERRIDE). FIXME:
1640 convert all said targets to use ADJUST_FORMAT instead. */
1641 #if 0
1642 print_maybe_const_decl ("const struct real_format *%s",
1643 "real_format_for_mode",
1644 "MAX_MODE_FLOAT - MIN_MODE_FLOAT + 1",
1645 format);
1646 #else
1647 print_decl ("struct real_format *\n", "real_format_for_mode",
1648 "MAX_MODE_FLOAT - MIN_MODE_FLOAT + 1 "
1649 "+ MAX_MODE_DECIMAL_FLOAT - MIN_MODE_DECIMAL_FLOAT + 1");
1650 #endif
1652 /* The beginning of the table is entries for float modes. */
1653 for (m = modes[MODE_FLOAT]; m; m = m->next)
1654 if (!strcmp (m->format, "0"))
1655 tagged_printf ("%s", m->format, m->name);
1656 else
1657 tagged_printf ("&%s", m->format, m->name);
1659 /* The end of the table is entries for decimal float modes. */
1660 for (m = modes[MODE_DECIMAL_FLOAT]; m; m = m->next)
1661 if (!strcmp (m->format, "0"))
1662 tagged_printf ("%s", m->format, m->name);
1663 else
1664 tagged_printf ("&%s", m->format, m->name);
1666 print_closer ();
1669 static void
1670 emit_mode_adjustments (void)
1672 struct mode_adjust *a;
1673 struct mode_data *m;
1675 puts ("\
1676 \nvoid\
1677 \ninit_adjust_machine_modes (void)\
1678 \n{\
1679 \n poly_uint16 ps ATTRIBUTE_UNUSED;\n\
1680 size_t s ATTRIBUTE_UNUSED;");
1682 /* Size adjustments must be propagated to all containing modes.
1683 A size adjustment forces us to recalculate the alignment too. */
1684 for (a = adj_bytesize; a; a = a->next)
1686 printf ("\n /* %s:%d */\n", a->file, a->line);
1687 switch (a->mode->cl)
1689 case MODE_VECTOR_BOOL:
1690 case MODE_VECTOR_INT:
1691 case MODE_VECTOR_FLOAT:
1692 case MODE_VECTOR_FRACT:
1693 case MODE_VECTOR_UFRACT:
1694 case MODE_VECTOR_ACCUM:
1695 case MODE_VECTOR_UACCUM:
1696 printf (" ps = %s;\n", a->adjustment);
1697 printf (" s = mode_unit_size[E_%smode];\n", a->mode->name);
1698 break;
1700 default:
1701 printf (" ps = s = %s;\n", a->adjustment);
1702 printf (" mode_unit_size[E_%smode] = s;\n", a->mode->name);
1703 break;
1705 printf (" mode_size[E_%smode] = ps;\n", a->mode->name);
1706 printf (" mode_base_align[E_%smode] = known_alignment (ps);\n",
1707 a->mode->name);
1709 for (m = a->mode->contained; m; m = m->next_cont)
1711 switch (m->cl)
1713 case MODE_COMPLEX_INT:
1714 case MODE_COMPLEX_FLOAT:
1715 printf (" mode_size[E_%smode] = 2*s;\n", m->name);
1716 printf (" mode_unit_size[E_%smode] = s;\n", m->name);
1717 printf (" mode_base_align[E_%smode] = s & (~s + 1);\n",
1718 m->name);
1719 break;
1721 case MODE_VECTOR_BOOL:
1722 /* Changes to BImode should not affect vector booleans. */
1723 break;
1725 case MODE_VECTOR_INT:
1726 case MODE_VECTOR_FLOAT:
1727 case MODE_VECTOR_FRACT:
1728 case MODE_VECTOR_UFRACT:
1729 case MODE_VECTOR_ACCUM:
1730 case MODE_VECTOR_UACCUM:
1731 printf (" mode_size[E_%smode] = %d * ps;\n",
1732 m->name, m->ncomponents);
1733 printf (" mode_unit_size[E_%smode] = s;\n", m->name);
1734 printf (" mode_base_align[E_%smode]"
1735 " = known_alignment (%d * ps);\n",
1736 m->name, m->ncomponents);
1737 break;
1739 default:
1740 internal_error (
1741 "mode %s is neither vector nor complex but contains %s",
1742 m->name, a->mode->name);
1743 /* NOTREACHED */
1748 /* Alignment adjustments propagate too.
1749 ??? This may not be the right thing for vector modes. */
1750 for (a = adj_alignment; a; a = a->next)
1752 printf ("\n /* %s:%d */\n s = %s;\n",
1753 a->file, a->line, a->adjustment);
1754 printf (" mode_base_align[E_%smode] = s;\n", a->mode->name);
1756 for (m = a->mode->contained; m; m = m->next_cont)
1758 switch (m->cl)
1760 case MODE_COMPLEX_INT:
1761 case MODE_COMPLEX_FLOAT:
1762 printf (" mode_base_align[E_%smode] = s;\n", m->name);
1763 break;
1765 case MODE_VECTOR_BOOL:
1766 /* Changes to BImode should not affect vector booleans. */
1767 break;
1769 case MODE_VECTOR_INT:
1770 case MODE_VECTOR_FLOAT:
1771 case MODE_VECTOR_FRACT:
1772 case MODE_VECTOR_UFRACT:
1773 case MODE_VECTOR_ACCUM:
1774 case MODE_VECTOR_UACCUM:
1775 printf (" mode_base_align[E_%smode] = %d*s;\n",
1776 m->name, m->ncomponents);
1777 break;
1779 default:
1780 internal_error (
1781 "mode %s is neither vector nor complex but contains %s",
1782 m->name, a->mode->name);
1783 /* NOTREACHED */
1788 /* Ibit adjustments don't have to propagate. */
1789 for (a = adj_ibit; a; a = a->next)
1791 printf ("\n /* %s:%d */\n s = %s;\n",
1792 a->file, a->line, a->adjustment);
1793 printf (" mode_ibit[E_%smode] = s;\n", a->mode->name);
1796 /* Fbit adjustments don't have to propagate. */
1797 for (a = adj_fbit; a; a = a->next)
1799 printf ("\n /* %s:%d */\n s = %s;\n",
1800 a->file, a->line, a->adjustment);
1801 printf (" mode_fbit[E_%smode] = s;\n", a->mode->name);
1804 /* Real mode formats don't have to propagate anywhere. */
1805 for (a = adj_format; a; a = a->next)
1806 printf ("\n /* %s:%d */\n REAL_MODE_FORMAT (E_%smode) = %s;\n",
1807 a->file, a->line, a->mode->name, a->adjustment);
1809 puts ("}");
1812 /* Emit ibit for all modes. */
1814 static void
1815 emit_mode_ibit (void)
1817 int c;
1818 struct mode_data *m;
1820 print_maybe_const_decl ("%sunsigned char",
1821 "mode_ibit", "NUM_MACHINE_MODES",
1822 ibit);
1824 for_all_modes (c, m)
1825 tagged_printf ("%u", m->ibit, m->name);
1827 print_closer ();
1830 /* Emit fbit for all modes. */
1832 static void
1833 emit_mode_fbit (void)
1835 int c;
1836 struct mode_data *m;
1838 print_maybe_const_decl ("%sunsigned char",
1839 "mode_fbit", "NUM_MACHINE_MODES",
1840 fbit);
1842 for_all_modes (c, m)
1843 tagged_printf ("%u", m->fbit, m->name);
1845 print_closer ();
1848 /* Emit __intN for all modes. */
1850 static void
1851 emit_mode_int_n (void)
1853 int c;
1854 struct mode_data *m;
1855 struct mode_data **mode_sort;
1856 int n_modes = 0;
1857 int i, j;
1859 print_decl ("int_n_data_t", "int_n_data", "");
1861 n_modes = 0;
1862 for_all_modes (c, m)
1863 if (m->int_n)
1864 n_modes ++;
1865 mode_sort = XALLOCAVEC (struct mode_data *, n_modes);
1867 n_modes = 0;
1868 for_all_modes (c, m)
1869 if (m->int_n)
1870 mode_sort[n_modes++] = m;
1872 /* Yes, this is a bubblesort, but there are at most four (and
1873 usually only 1-2) entries to sort. */
1874 for (i = 0; i<n_modes - 1; i++)
1875 for (j = i + 1; j < n_modes; j++)
1876 if (mode_sort[i]->int_n > mode_sort[j]->int_n)
1877 std::swap (mode_sort[i], mode_sort[j]);
1879 for (i = 0; i < n_modes; i ++)
1881 m = mode_sort[i];
1882 printf(" {\n");
1883 tagged_printf ("%u", m->int_n, m->name);
1884 printf ("{ E_%smode },", m->name);
1885 printf(" },\n");
1888 print_closer ();
1892 static void
1893 emit_insn_modes_c (void)
1895 emit_insn_modes_c_header ();
1896 emit_mode_name ();
1897 emit_mode_class ();
1898 emit_mode_precision ();
1899 emit_mode_size ();
1900 emit_mode_nunits ();
1901 emit_mode_wider ();
1902 emit_mode_complex ();
1903 emit_mode_mask ();
1904 emit_mode_inner ();
1905 emit_mode_unit_size ();
1906 emit_mode_unit_precision ();
1907 emit_mode_base_align ();
1908 emit_class_narrowest_mode ();
1909 emit_real_format_for_mode ();
1910 emit_mode_adjustments ();
1911 emit_mode_ibit ();
1912 emit_mode_fbit ();
1913 emit_mode_int_n ();
1916 static void
1917 emit_min_insn_modes_c (void)
1919 emit_min_insn_modes_c_header ();
1920 emit_mode_name ();
1921 emit_mode_class ();
1922 emit_mode_nunits ();
1923 emit_mode_wider ();
1924 emit_mode_inner ();
1925 emit_class_narrowest_mode ();
1928 /* Master control. */
1930 main (int argc, char **argv)
1932 bool gen_header = false, gen_inlines = false, gen_min = false;
1933 progname = argv[0];
1935 if (argc == 1)
1937 else if (argc == 2 && !strcmp (argv[1], "-h"))
1938 gen_header = true;
1939 else if (argc == 2 && !strcmp (argv[1], "-i"))
1940 gen_inlines = true;
1941 else if (argc == 2 && !strcmp (argv[1], "-m"))
1942 gen_min = true;
1943 else
1945 error ("usage: %s [-h|-i|-m] > file", progname);
1946 return FATAL_EXIT_CODE;
1949 modes_by_name = htab_create_alloc (64, hash_mode, eq_mode, 0, xcalloc, free);
1951 create_modes ();
1952 complete_all_modes ();
1954 if (have_error)
1955 return FATAL_EXIT_CODE;
1957 calc_wider_mode ();
1959 if (gen_header)
1960 emit_insn_modes_h ();
1961 else if (gen_inlines)
1962 emit_insn_modes_inline_h ();
1963 else if (gen_min)
1964 emit_min_insn_modes_c ();
1965 else
1966 emit_insn_modes_c ();
1968 if (fflush (stdout) || fclose (stdout))
1969 return FATAL_EXIT_CODE;
1970 return SUCCESS_EXIT_CODE;