* c-ada-spec.c (dump_number): Add FLOAT_P parameter.
[official-gcc.git] / gcc / genmodes.c
bloba70f0967859b13a349c83086cde4527d2a3648e9
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_nunits_adj; /* true if this mode needs dynamic nunits
76 adjustment */
77 bool need_bytesize_adj; /* true if this mode needs dynamic size
78 adjustment */
79 unsigned int int_n; /* If nonzero, then __int<INT_N> will be defined */
82 static struct mode_data *modes[MAX_MODE_CLASS];
83 static unsigned int n_modes[MAX_MODE_CLASS];
84 static struct mode_data *void_mode;
86 static const struct mode_data blank_mode = {
87 0, "<unknown>", MAX_MODE_CLASS,
88 -1U, -1U, -1U, -1U,
89 0, 0, 0, 0, 0, 0,
90 "<unknown>", 0, 0, 0, 0, false, false, 0
93 static htab_t modes_by_name;
95 /* Data structure for recording target-specified runtime adjustments
96 to a particular mode. We support varying the byte size, the
97 alignment, and the floating point format. */
98 struct mode_adjust
100 struct mode_adjust *next;
101 struct mode_data *mode;
102 const char *adjustment;
104 const char *file;
105 unsigned int line;
108 static struct mode_adjust *adj_nunits;
109 static struct mode_adjust *adj_bytesize;
110 static struct mode_adjust *adj_alignment;
111 static struct mode_adjust *adj_format;
112 static struct mode_adjust *adj_ibit;
113 static struct mode_adjust *adj_fbit;
115 /* Mode class operations. */
116 static enum mode_class
117 complex_class (enum mode_class c)
119 switch (c)
121 case MODE_INT: return MODE_COMPLEX_INT;
122 case MODE_FLOAT: return MODE_COMPLEX_FLOAT;
123 default:
124 error ("no complex class for class %s", mode_class_names[c]);
125 return MODE_RANDOM;
129 static enum mode_class
130 vector_class (enum mode_class cl)
132 switch (cl)
134 case MODE_INT: return MODE_VECTOR_INT;
135 case MODE_FLOAT: return MODE_VECTOR_FLOAT;
136 case MODE_FRACT: return MODE_VECTOR_FRACT;
137 case MODE_UFRACT: return MODE_VECTOR_UFRACT;
138 case MODE_ACCUM: return MODE_VECTOR_ACCUM;
139 case MODE_UACCUM: return MODE_VECTOR_UACCUM;
140 default:
141 error ("no vector class for class %s", mode_class_names[cl]);
142 return MODE_RANDOM;
146 /* Utility routines. */
147 static inline struct mode_data *
148 find_mode (const char *name)
150 struct mode_data key;
152 key.name = name;
153 return (struct mode_data *) htab_find (modes_by_name, &key);
156 static struct mode_data *
157 new_mode (enum mode_class cl, const char *name,
158 const char *file, unsigned int line)
160 struct mode_data *m;
161 static unsigned int count = 0;
163 m = find_mode (name);
164 if (m)
166 error ("%s:%d: duplicate definition of mode \"%s\"",
167 trim_filename (file), line, name);
168 error ("%s:%d: previous definition here", m->file, m->line);
169 return m;
172 m = XNEW (struct mode_data);
173 memcpy (m, &blank_mode, sizeof (struct mode_data));
174 m->cl = cl;
175 m->name = name;
176 if (file)
177 m->file = trim_filename (file);
178 m->line = line;
179 m->counter = count++;
181 m->next = modes[cl];
182 modes[cl] = m;
183 n_modes[cl]++;
185 *htab_find_slot (modes_by_name, m, INSERT) = m;
187 return m;
190 static hashval_t
191 hash_mode (const void *p)
193 const struct mode_data *m = (const struct mode_data *)p;
194 return htab_hash_string (m->name);
197 static int
198 eq_mode (const void *p, const void *q)
200 const struct mode_data *a = (const struct mode_data *)p;
201 const struct mode_data *b = (const struct mode_data *)q;
203 return !strcmp (a->name, b->name);
206 #define for_all_modes(C, M) \
207 for (C = 0; C < MAX_MODE_CLASS; C++) \
208 for (M = modes[C]; M; M = M->next)
210 static void ATTRIBUTE_UNUSED
211 new_adjust (const char *name,
212 struct mode_adjust **category, const char *catname,
213 const char *adjustment,
214 enum mode_class required_class_from,
215 enum mode_class required_class_to,
216 const char *file, unsigned int line)
218 struct mode_data *mode = find_mode (name);
219 struct mode_adjust *a;
221 file = trim_filename (file);
223 if (!mode)
225 error ("%s:%d: no mode \"%s\"", file, line, name);
226 return;
229 if (required_class_from != MODE_RANDOM
230 && (mode->cl < required_class_from || mode->cl > required_class_to))
232 error ("%s:%d: mode \"%s\" is not among class {%s, %s}",
233 file, line, name, mode_class_names[required_class_from] + 5,
234 mode_class_names[required_class_to] + 5);
235 return;
238 for (a = *category; a; a = a->next)
239 if (a->mode == mode)
241 error ("%s:%d: mode \"%s\" already has a %s adjustment",
242 file, line, name, catname);
243 error ("%s:%d: previous adjustment here", a->file, a->line);
244 return;
247 a = XNEW (struct mode_adjust);
248 a->mode = mode;
249 a->adjustment = adjustment;
250 a->file = file;
251 a->line = line;
253 a->next = *category;
254 *category = a;
257 /* Diagnose failure to meet expectations in a partially filled out
258 mode structure. */
259 enum requirement { SET, UNSET, OPTIONAL };
261 #define validate_field_(mname, fname, req, val, unset, file, line) do { \
262 switch (req) \
264 case SET: \
265 if (val == unset) \
266 error ("%s:%d: (%s) field %s must be set", \
267 file, line, mname, fname); \
268 break; \
269 case UNSET: \
270 if (val != unset) \
271 error ("%s:%d: (%s) field %s must not be set", \
272 file, line, mname, fname); \
273 case OPTIONAL: \
274 break; \
276 } while (0)
278 #define validate_field(M, F) \
279 validate_field_(M->name, #F, r_##F, M->F, blank_mode.F, M->file, M->line)
281 static void
282 validate_mode (struct mode_data *m,
283 enum requirement r_precision,
284 enum requirement r_bytesize,
285 enum requirement r_component,
286 enum requirement r_ncomponents,
287 enum requirement r_format)
289 validate_field (m, precision);
290 validate_field (m, bytesize);
291 validate_field (m, component);
292 validate_field (m, ncomponents);
293 validate_field (m, format);
295 #undef validate_field
296 #undef validate_field_
298 /* Given a partially-filled-out mode structure, figure out what we can
299 and fill the rest of it in; die if it isn't enough. */
300 static void
301 complete_mode (struct mode_data *m)
303 unsigned int alignment;
305 if (!m->name)
307 error ("%s:%d: mode with no name", m->file, m->line);
308 return;
310 if (m->cl == MAX_MODE_CLASS)
312 error ("%s:%d: %smode has no mode class", m->file, m->line, m->name);
313 return;
316 switch (m->cl)
318 case MODE_RANDOM:
319 /* Nothing more need be said. */
320 if (!strcmp (m->name, "VOID"))
321 void_mode = m;
323 validate_mode (m, UNSET, UNSET, UNSET, UNSET, UNSET);
325 m->precision = 0;
326 m->bytesize = 0;
327 m->ncomponents = 0;
328 m->component = 0;
329 break;
331 case MODE_CC:
332 /* Again, nothing more need be said. For historical reasons,
333 the size of a CC mode is four units. */
334 validate_mode (m, UNSET, UNSET, UNSET, UNSET, UNSET);
336 m->bytesize = 4;
337 m->ncomponents = 1;
338 m->component = 0;
339 break;
341 case MODE_INT:
342 case MODE_POINTER_BOUNDS:
343 case MODE_FLOAT:
344 case MODE_DECIMAL_FLOAT:
345 case MODE_FRACT:
346 case MODE_UFRACT:
347 case MODE_ACCUM:
348 case MODE_UACCUM:
349 /* A scalar mode must have a byte size, may have a bit size,
350 and must not have components. A float mode must have a
351 format. */
352 validate_mode (m, OPTIONAL, SET, UNSET, UNSET,
353 (m->cl == MODE_FLOAT || m->cl == MODE_DECIMAL_FLOAT)
354 ? SET : UNSET);
356 m->ncomponents = 1;
357 m->component = 0;
358 break;
360 case MODE_PARTIAL_INT:
361 /* A partial integer mode uses ->component to say what the
362 corresponding full-size integer mode is, and may also
363 specify a bit size. */
364 validate_mode (m, OPTIONAL, UNSET, SET, UNSET, UNSET);
366 m->bytesize = m->component->bytesize;
368 m->ncomponents = 1;
369 break;
371 case MODE_COMPLEX_INT:
372 case MODE_COMPLEX_FLOAT:
373 /* Complex modes should have a component indicated, but no more. */
374 validate_mode (m, UNSET, UNSET, SET, UNSET, UNSET);
375 m->ncomponents = 2;
376 if (m->component->precision != (unsigned int)-1)
377 m->precision = 2 * m->component->precision;
378 m->bytesize = 2 * m->component->bytesize;
379 break;
381 case MODE_VECTOR_BOOL:
382 validate_mode (m, UNSET, SET, SET, SET, UNSET);
383 break;
385 case MODE_VECTOR_INT:
386 case MODE_VECTOR_FLOAT:
387 case MODE_VECTOR_FRACT:
388 case MODE_VECTOR_UFRACT:
389 case MODE_VECTOR_ACCUM:
390 case MODE_VECTOR_UACCUM:
391 /* Vector modes should have a component and a number of components. */
392 validate_mode (m, UNSET, UNSET, SET, SET, UNSET);
393 if (m->component->precision != (unsigned int)-1)
394 m->precision = m->ncomponents * m->component->precision;
395 m->bytesize = m->ncomponents * m->component->bytesize;
396 break;
398 default:
399 gcc_unreachable ();
402 /* If not already specified, the mode alignment defaults to the largest
403 power of two that divides the size of the object. Complex types are
404 not more aligned than their contents. */
405 if (m->cl == MODE_COMPLEX_INT || m->cl == MODE_COMPLEX_FLOAT)
406 alignment = m->component->bytesize;
407 else
408 alignment = m->bytesize;
410 m->alignment = alignment & (~alignment + 1);
412 /* If this mode has components, make the component mode point back
413 to this mode, for the sake of adjustments. */
414 if (m->component)
416 m->next_cont = m->component->contained;
417 m->component->contained = m;
421 static void
422 complete_all_modes (void)
424 struct mode_data *m;
425 int cl;
427 for_all_modes (cl, m)
428 complete_mode (m);
431 /* For each mode in class CLASS, construct a corresponding complex mode. */
432 #define COMPLEX_MODES(C) make_complex_modes (MODE_##C, __FILE__, __LINE__)
433 static void
434 make_complex_modes (enum mode_class cl,
435 const char *file, unsigned int line)
437 struct mode_data *m;
438 struct mode_data *c;
439 enum mode_class cclass = complex_class (cl);
441 if (cclass == MODE_RANDOM)
442 return;
444 for (m = modes[cl]; m; m = m->next)
446 char *p, *buf;
447 size_t m_len;
449 /* Skip BImode. FIXME: BImode probably shouldn't be MODE_INT. */
450 if (m->precision == 1)
451 continue;
453 m_len = strlen (m->name);
454 /* The leading "1 +" is in case we prepend a "C" below. */
455 buf = (char *) xmalloc (1 + m_len + 1);
457 /* Float complex modes are named SCmode, etc.
458 Int complex modes are named CSImode, etc.
459 This inconsistency should be eliminated. */
460 p = 0;
461 if (cl == MODE_FLOAT)
463 memcpy (buf, m->name, m_len + 1);
464 p = strchr (buf, 'F');
465 if (p == 0 && strchr (buf, 'D') == 0)
467 error ("%s:%d: float mode \"%s\" has no 'F' or 'D'",
468 m->file, m->line, m->name);
469 free (buf);
470 continue;
473 if (p != 0)
474 *p = 'C';
475 else
477 buf[0] = 'C';
478 memcpy (buf + 1, m->name, m_len + 1);
481 c = new_mode (cclass, buf, file, line);
482 c->component = m;
483 m->complex = c;
487 /* For all modes in class CL, construct vector modes of width
488 WIDTH, having as many components as necessary. */
489 #define VECTOR_MODES_WITH_PREFIX(PREFIX, C, W) \
490 make_vector_modes (MODE_##C, #PREFIX, W, __FILE__, __LINE__)
491 #define VECTOR_MODES(C, W) VECTOR_MODES_WITH_PREFIX (V, C, W)
492 static void ATTRIBUTE_UNUSED
493 make_vector_modes (enum mode_class cl, const char *prefix, unsigned int width,
494 const char *file, unsigned int line)
496 struct mode_data *m;
497 struct mode_data *v;
498 /* Big enough for a 32-bit UINT_MAX plus the text. */
499 char buf[12];
500 unsigned int ncomponents;
501 enum mode_class vclass = vector_class (cl);
503 if (vclass == MODE_RANDOM)
504 return;
506 for (m = modes[cl]; m; m = m->next)
508 /* Do not construct vector modes with only one element, or
509 vector modes where the element size doesn't divide the full
510 size evenly. */
511 ncomponents = width / m->bytesize;
512 if (ncomponents < 2)
513 continue;
514 if (width % m->bytesize)
515 continue;
517 /* Skip QFmode and BImode. FIXME: this special case should
518 not be necessary. */
519 if (cl == MODE_FLOAT && m->bytesize == 1)
520 continue;
521 if (cl == MODE_INT && m->precision == 1)
522 continue;
524 if ((size_t) snprintf (buf, sizeof buf, "%s%u%s", prefix,
525 ncomponents, m->name) >= sizeof buf)
527 error ("%s:%d: mode name \"%s\" is too long",
528 m->file, m->line, m->name);
529 continue;
532 v = new_mode (vclass, xstrdup (buf), file, line);
533 v->component = m;
534 v->ncomponents = ncomponents;
538 /* Create a vector of booleans called NAME with COUNT elements and
539 BYTESIZE bytes in total. */
540 #define VECTOR_BOOL_MODE(NAME, COUNT, BYTESIZE) \
541 make_vector_bool_mode (#NAME, COUNT, BYTESIZE, __FILE__, __LINE__)
542 static void ATTRIBUTE_UNUSED
543 make_vector_bool_mode (const char *name, unsigned int count,
544 unsigned int bytesize, const char *file,
545 unsigned int line)
547 struct mode_data *m = find_mode ("BI");
548 if (!m)
550 error ("%s:%d: no mode \"BI\"", file, line);
551 return;
554 struct mode_data *v = new_mode (MODE_VECTOR_BOOL, name, file, line);
555 v->component = m;
556 v->ncomponents = count;
557 v->bytesize = bytesize;
560 /* Input. */
562 #define _SPECIAL_MODE(C, N) \
563 make_special_mode (MODE_##C, #N, __FILE__, __LINE__)
564 #define RANDOM_MODE(N) _SPECIAL_MODE (RANDOM, N)
565 #define CC_MODE(N) _SPECIAL_MODE (CC, N)
567 static void
568 make_special_mode (enum mode_class cl, const char *name,
569 const char *file, unsigned int line)
571 new_mode (cl, name, file, line);
574 #define POINTER_BOUNDS_MODE(N, Y) \
575 make_pointer_bounds_mode (#N, Y, __FILE__, __LINE__)
577 static void ATTRIBUTE_UNUSED
578 make_pointer_bounds_mode (const char *name,
579 unsigned int bytesize,
580 const char *file, unsigned int line)
582 struct mode_data *m = new_mode (MODE_POINTER_BOUNDS, name, file, line);
583 m->bytesize = bytesize;
587 #define INT_MODE(N, Y) FRACTIONAL_INT_MODE (N, -1U, Y)
588 #define FRACTIONAL_INT_MODE(N, B, Y) \
589 make_int_mode (#N, B, Y, __FILE__, __LINE__)
591 static void
592 make_int_mode (const char *name,
593 unsigned int precision, unsigned int bytesize,
594 const char *file, unsigned int line)
596 struct mode_data *m = new_mode (MODE_INT, name, file, line);
597 m->bytesize = bytesize;
598 m->precision = precision;
601 #define FRACT_MODE(N, Y, F) \
602 make_fixed_point_mode (MODE_FRACT, #N, Y, 0, F, __FILE__, __LINE__)
604 #define UFRACT_MODE(N, Y, F) \
605 make_fixed_point_mode (MODE_UFRACT, #N, Y, 0, F, __FILE__, __LINE__)
607 #define ACCUM_MODE(N, Y, I, F) \
608 make_fixed_point_mode (MODE_ACCUM, #N, Y, I, F, __FILE__, __LINE__)
610 #define UACCUM_MODE(N, Y, I, F) \
611 make_fixed_point_mode (MODE_UACCUM, #N, Y, I, F, __FILE__, __LINE__)
613 /* Create a fixed-point mode by setting CL, NAME, BYTESIZE, IBIT, FBIT,
614 FILE, and LINE. */
616 static void
617 make_fixed_point_mode (enum mode_class cl,
618 const char *name,
619 unsigned int bytesize,
620 unsigned int ibit,
621 unsigned int fbit,
622 const char *file, unsigned int line)
624 struct mode_data *m = new_mode (cl, name, file, line);
625 m->bytesize = bytesize;
626 m->ibit = ibit;
627 m->fbit = fbit;
630 #define FLOAT_MODE(N, Y, F) FRACTIONAL_FLOAT_MODE (N, -1U, Y, F)
631 #define FRACTIONAL_FLOAT_MODE(N, B, Y, F) \
632 make_float_mode (#N, B, Y, #F, __FILE__, __LINE__)
634 static void
635 make_float_mode (const char *name,
636 unsigned int precision, unsigned int bytesize,
637 const char *format,
638 const char *file, unsigned int line)
640 struct mode_data *m = new_mode (MODE_FLOAT, name, file, line);
641 m->bytesize = bytesize;
642 m->precision = precision;
643 m->format = format;
646 #define DECIMAL_FLOAT_MODE(N, Y, F) \
647 FRACTIONAL_DECIMAL_FLOAT_MODE (N, -1U, Y, F)
648 #define FRACTIONAL_DECIMAL_FLOAT_MODE(N, B, Y, F) \
649 make_decimal_float_mode (#N, B, Y, #F, __FILE__, __LINE__)
651 static void
652 make_decimal_float_mode (const char *name,
653 unsigned int precision, unsigned int bytesize,
654 const char *format,
655 const char *file, unsigned int line)
657 struct mode_data *m = new_mode (MODE_DECIMAL_FLOAT, name, file, line);
658 m->bytesize = bytesize;
659 m->precision = precision;
660 m->format = format;
663 #define RESET_FLOAT_FORMAT(N, F) \
664 reset_float_format (#N, #F, __FILE__, __LINE__)
665 static void ATTRIBUTE_UNUSED
666 reset_float_format (const char *name, const char *format,
667 const char *file, unsigned int line)
669 struct mode_data *m = find_mode (name);
670 if (!m)
672 error ("%s:%d: no mode \"%s\"", file, line, name);
673 return;
675 if (m->cl != MODE_FLOAT && m->cl != MODE_DECIMAL_FLOAT)
677 error ("%s:%d: mode \"%s\" is not a FLOAT class", file, line, name);
678 return;
680 m->format = format;
683 /* __intN support. */
684 #define INT_N(M,PREC) \
685 make_int_n (#M, PREC, __FILE__, __LINE__)
686 static void ATTRIBUTE_UNUSED
687 make_int_n (const char *m, int bitsize,
688 const char *file, unsigned int line)
690 struct mode_data *component = find_mode (m);
691 if (!component)
693 error ("%s:%d: no mode \"%s\"", file, line, m);
694 return;
696 if (component->cl != MODE_INT
697 && component->cl != MODE_PARTIAL_INT)
699 error ("%s:%d: mode \"%s\" is not class INT or PARTIAL_INT", file, line, m);
700 return;
702 if (component->int_n != 0)
704 error ("%s:%d: mode \"%s\" already has an intN", file, line, m);
705 return;
708 component->int_n = bitsize;
711 /* Partial integer modes are specified by relation to a full integer
712 mode. */
713 #define PARTIAL_INT_MODE(M,PREC,NAME) \
714 make_partial_integer_mode (#M, #NAME, PREC, __FILE__, __LINE__)
715 static void ATTRIBUTE_UNUSED
716 make_partial_integer_mode (const char *base, const char *name,
717 unsigned int precision,
718 const char *file, unsigned int line)
720 struct mode_data *m;
721 struct mode_data *component = find_mode (base);
722 if (!component)
724 error ("%s:%d: no mode \"%s\"", file, line, name);
725 return;
727 if (component->cl != MODE_INT)
729 error ("%s:%d: mode \"%s\" is not class INT", file, line, name);
730 return;
733 m = new_mode (MODE_PARTIAL_INT, name, file, line);
734 m->precision = precision;
735 m->component = component;
738 /* A single vector mode can be specified by naming its component
739 mode and the number of components. */
740 #define VECTOR_MODE(C, M, N) \
741 make_vector_mode (MODE_##C, #M, N, __FILE__, __LINE__);
742 static void ATTRIBUTE_UNUSED
743 make_vector_mode (enum mode_class bclass,
744 const char *base,
745 unsigned int ncomponents,
746 const char *file, unsigned int line)
748 struct mode_data *v;
749 enum mode_class vclass = vector_class (bclass);
750 struct mode_data *component = find_mode (base);
751 char namebuf[16];
753 if (vclass == MODE_RANDOM)
754 return;
755 if (component == 0)
757 error ("%s:%d: no mode \"%s\"", file, line, base);
758 return;
760 if (component->cl != bclass
761 && (component->cl != MODE_PARTIAL_INT
762 || bclass != MODE_INT))
764 error ("%s:%d: mode \"%s\" is not class %s",
765 file, line, base, mode_class_names[bclass] + 5);
766 return;
769 if ((size_t)snprintf (namebuf, sizeof namebuf, "V%u%s",
770 ncomponents, base) >= sizeof namebuf)
772 error ("%s:%d: mode name \"%s\" is too long",
773 file, line, base);
774 return;
777 v = new_mode (vclass, xstrdup (namebuf), file, line);
778 v->ncomponents = ncomponents;
779 v->component = component;
782 /* Adjustability. */
783 #define _ADD_ADJUST(A, M, X, C1, C2) \
784 new_adjust (#M, &adj_##A, #A, #X, MODE_##C1, MODE_##C2, __FILE__, __LINE__)
786 #define ADJUST_NUNITS(M, X) _ADD_ADJUST (nunits, M, X, RANDOM, RANDOM)
787 #define ADJUST_BYTESIZE(M, X) _ADD_ADJUST (bytesize, M, X, RANDOM, RANDOM)
788 #define ADJUST_ALIGNMENT(M, X) _ADD_ADJUST (alignment, M, X, RANDOM, RANDOM)
789 #define ADJUST_FLOAT_FORMAT(M, X) _ADD_ADJUST (format, M, X, FLOAT, FLOAT)
790 #define ADJUST_IBIT(M, X) _ADD_ADJUST (ibit, M, X, ACCUM, UACCUM)
791 #define ADJUST_FBIT(M, X) _ADD_ADJUST (fbit, M, X, FRACT, UACCUM)
793 static int bits_per_unit;
794 static int max_bitsize_mode_any_int;
795 static int max_bitsize_mode_any_mode;
797 static void
798 create_modes (void)
800 #include "machmode.def"
802 /* So put the default value unless the target needs a non standard
803 value. */
804 #ifdef BITS_PER_UNIT
805 bits_per_unit = BITS_PER_UNIT;
806 #else
807 bits_per_unit = 8;
808 #endif
810 #ifdef MAX_BITSIZE_MODE_ANY_INT
811 max_bitsize_mode_any_int = MAX_BITSIZE_MODE_ANY_INT;
812 #else
813 max_bitsize_mode_any_int = 0;
814 #endif
816 #ifdef MAX_BITSIZE_MODE_ANY_MODE
817 max_bitsize_mode_any_mode = MAX_BITSIZE_MODE_ANY_MODE;
818 #else
819 max_bitsize_mode_any_mode = 0;
820 #endif
823 #ifndef NUM_POLY_INT_COEFFS
824 #define NUM_POLY_INT_COEFFS 1
825 #endif
827 /* Processing. */
829 /* Sort a list of modes into the order needed for the WIDER field:
830 major sort by precision, minor sort by component precision.
832 For instance:
833 QI < HI < SI < DI < TI
834 V4QI < V2HI < V8QI < V4HI < V2SI.
836 If the precision is not set, sort by the bytesize. A mode with
837 precision set gets sorted before a mode without precision set, if
838 they have the same bytesize; this is the right thing because
839 the precision must always be smaller than the bytesize * BITS_PER_UNIT.
840 We don't have to do anything special to get this done -- an unset
841 precision shows up as (unsigned int)-1, i.e. UINT_MAX. */
842 static int
843 cmp_modes (const void *a, const void *b)
845 const struct mode_data *const m = *(const struct mode_data *const*)a;
846 const struct mode_data *const n = *(const struct mode_data *const*)b;
848 if (m->bytesize > n->bytesize)
849 return 1;
850 else if (m->bytesize < n->bytesize)
851 return -1;
853 if (m->precision > n->precision)
854 return 1;
855 else if (m->precision < n->precision)
856 return -1;
858 if (!m->component && !n->component)
860 if (m->counter < n->counter)
861 return -1;
862 else
863 return 1;
866 if (m->component->bytesize > n->component->bytesize)
867 return 1;
868 else if (m->component->bytesize < n->component->bytesize)
869 return -1;
871 if (m->component->precision > n->component->precision)
872 return 1;
873 else if (m->component->precision < n->component->precision)
874 return -1;
876 if (m->counter < n->counter)
877 return -1;
878 else
879 return 1;
882 static void
883 calc_wider_mode (void)
885 int c;
886 struct mode_data *m;
887 struct mode_data **sortbuf;
888 unsigned int max_n_modes = 0;
889 unsigned int i, j;
891 for (c = 0; c < MAX_MODE_CLASS; c++)
892 max_n_modes = MAX (max_n_modes, n_modes[c]);
894 /* Allocate max_n_modes + 1 entries to leave room for the extra null
895 pointer assigned after the qsort call below. */
896 sortbuf = XALLOCAVEC (struct mode_data *, max_n_modes + 1);
898 for (c = 0; c < MAX_MODE_CLASS; c++)
900 /* "wider" is not meaningful for MODE_RANDOM and MODE_CC.
901 However, we want these in textual order, and we have
902 precisely the reverse. */
903 if (c == MODE_RANDOM || c == MODE_CC)
905 struct mode_data *prev, *next;
907 for (prev = 0, m = modes[c]; m; m = next)
909 m->wider = void_mode;
911 /* this is nreverse */
912 next = m->next;
913 m->next = prev;
914 prev = m;
916 modes[c] = prev;
918 else
920 if (!modes[c])
921 continue;
923 for (i = 0, m = modes[c]; m; i++, m = m->next)
924 sortbuf[i] = m;
926 (qsort) (sortbuf, i, sizeof (struct mode_data *), cmp_modes);
928 sortbuf[i] = 0;
929 for (j = 0; j < i; j++)
931 sortbuf[j]->next = sortbuf[j + 1];
932 if (c == MODE_PARTIAL_INT)
933 sortbuf[j]->wider = sortbuf[j]->component;
934 else
935 sortbuf[j]->wider = sortbuf[j]->next;
938 modes[c] = sortbuf[0];
943 /* Text to add to the constant part of a poly_int_pod initializer in
944 order to fill out te whole structure. */
945 #if NUM_POLY_INT_COEFFS == 1
946 #define ZERO_COEFFS ""
947 #elif NUM_POLY_INT_COEFFS == 2
948 #define ZERO_COEFFS ", 0"
949 #else
950 #error "Unknown value of NUM_POLY_INT_COEFFS"
951 #endif
953 /* Output routines. */
955 #define tagged_printf(FMT, ARG, TAG) do { \
956 int count_ = printf (" " FMT ",", ARG); \
957 printf ("%*s/* %s */\n", 27 - count_, "", TAG); \
958 } while (0)
960 #define print_decl(TYPE, NAME, ASIZE) \
961 puts ("\nconst " TYPE " " NAME "[" ASIZE "] =\n{");
963 #define print_maybe_const_decl(TYPE, NAME, ASIZE, NEEDS_ADJ) \
964 printf ("\n" TYPE " " NAME "[" ASIZE "] = \n{\n", \
965 NEEDS_ADJ ? "" : "const ")
967 #define print_closer() puts ("};")
969 /* Compute the max bitsize of some of the classes of integers. It may
970 be that there are needs for the other integer classes, and this
971 code is easy to extend. */
972 static void
973 emit_max_int (void)
975 unsigned int max, mmax;
976 struct mode_data *i;
977 int j;
979 puts ("");
981 printf ("#define BITS_PER_UNIT (%d)\n", bits_per_unit);
983 if (max_bitsize_mode_any_int == 0)
985 for (max = 1, i = modes[MODE_INT]; i; i = i->next)
986 if (max < i->bytesize)
987 max = i->bytesize;
988 mmax = max;
989 for (max = 1, i = modes[MODE_PARTIAL_INT]; i; i = i->next)
990 if (max < i->bytesize)
991 max = i->bytesize;
992 if (max > mmax)
993 mmax = max;
994 printf ("#define MAX_BITSIZE_MODE_ANY_INT (%d*BITS_PER_UNIT)\n", mmax);
996 else
997 printf ("#define MAX_BITSIZE_MODE_ANY_INT %d\n", max_bitsize_mode_any_int);
999 if (max_bitsize_mode_any_mode == 0)
1001 mmax = 0;
1002 for (j = 0; j < MAX_MODE_CLASS; j++)
1003 for (i = modes[j]; i; i = i->next)
1004 if (mmax < i->bytesize)
1005 mmax = i->bytesize;
1006 printf ("#define MAX_BITSIZE_MODE_ANY_MODE (%d*BITS_PER_UNIT)\n", mmax);
1008 else
1009 printf ("#define MAX_BITSIZE_MODE_ANY_MODE %d\n",
1010 max_bitsize_mode_any_mode);
1013 /* Emit mode_size_inline routine into insn-modes.h header. */
1014 static void
1015 emit_mode_size_inline (void)
1017 int c;
1018 struct mode_adjust *a;
1019 struct mode_data *m;
1021 /* Size adjustments must be propagated to all containing modes. */
1022 for (a = adj_bytesize; a; a = a->next)
1024 a->mode->need_bytesize_adj = true;
1025 for (m = a->mode->contained; m; m = m->next_cont)
1026 m->need_bytesize_adj = true;
1029 /* Changing the number of units by a factor of X also changes the size
1030 by a factor of X. */
1031 for (mode_adjust *a = adj_nunits; a; a = a->next)
1032 a->mode->need_bytesize_adj = true;
1034 printf ("\
1035 #ifdef __cplusplus\n\
1036 inline __attribute__((__always_inline__))\n\
1037 #else\n\
1038 extern __inline__ __attribute__((__always_inline__, __gnu_inline__))\n\
1039 #endif\n\
1040 poly_uint16\n\
1041 mode_size_inline (machine_mode mode)\n\
1042 {\n\
1043 extern %spoly_uint16_pod mode_size[NUM_MACHINE_MODES];\n\
1044 gcc_assert (mode >= 0 && mode < NUM_MACHINE_MODES);\n\
1045 switch (mode)\n\
1046 {\n", adj_nunits || adj_bytesize ? "" : "const ");
1048 for_all_modes (c, m)
1049 if (!m->need_bytesize_adj)
1050 printf (" case E_%smode: return %u;\n", m->name, m->bytesize);
1052 puts ("\
1053 default: return mode_size[mode];\n\
1054 }\n\
1055 }\n");
1058 /* Emit mode_nunits_inline routine into insn-modes.h header. */
1059 static void
1060 emit_mode_nunits_inline (void)
1062 int c;
1063 struct mode_data *m;
1065 for (mode_adjust *a = adj_nunits; a; a = a->next)
1066 a->mode->need_nunits_adj = true;
1068 printf ("\
1069 #ifdef __cplusplus\n\
1070 inline __attribute__((__always_inline__))\n\
1071 #else\n\
1072 extern __inline__ __attribute__((__always_inline__, __gnu_inline__))\n\
1073 #endif\n\
1074 poly_uint16\n\
1075 mode_nunits_inline (machine_mode mode)\n\
1076 {\n\
1077 extern %spoly_uint16_pod mode_nunits[NUM_MACHINE_MODES];\n\
1078 switch (mode)\n\
1079 {\n", adj_nunits ? "" : "const ");
1081 for_all_modes (c, m)
1082 if (!m->need_nunits_adj)
1083 printf (" case E_%smode: return %u;\n", m->name, m->ncomponents);
1085 puts ("\
1086 default: return mode_nunits[mode];\n\
1087 }\n\
1088 }\n");
1091 /* Emit mode_inner_inline routine into insn-modes.h header. */
1092 static void
1093 emit_mode_inner_inline (void)
1095 int c;
1096 struct mode_data *m;
1098 puts ("\
1099 #ifdef __cplusplus\n\
1100 inline __attribute__((__always_inline__))\n\
1101 #else\n\
1102 extern __inline__ __attribute__((__always_inline__, __gnu_inline__))\n\
1103 #endif\n\
1104 unsigned char\n\
1105 mode_inner_inline (machine_mode mode)\n\
1106 {\n\
1107 extern const unsigned char mode_inner[NUM_MACHINE_MODES];\n\
1108 gcc_assert (mode >= 0 && mode < NUM_MACHINE_MODES);\n\
1109 switch (mode)\n\
1110 {");
1112 for_all_modes (c, m)
1113 printf (" case E_%smode: return E_%smode;\n", m->name,
1114 c != MODE_PARTIAL_INT && m->component
1115 ? m->component->name : m->name);
1117 puts ("\
1118 default: return mode_inner[mode];\n\
1119 }\n\
1120 }\n");
1123 /* Emit mode_unit_size_inline routine into insn-modes.h header. */
1124 static void
1125 emit_mode_unit_size_inline (void)
1127 int c;
1128 struct mode_data *m;
1130 puts ("\
1131 #ifdef __cplusplus\n\
1132 inline __attribute__((__always_inline__))\n\
1133 #else\n\
1134 extern __inline__ __attribute__((__always_inline__, __gnu_inline__))\n\
1135 #endif\n\
1136 unsigned char\n\
1137 mode_unit_size_inline (machine_mode mode)\n\
1138 {\n\
1139 extern CONST_MODE_UNIT_SIZE unsigned char mode_unit_size[NUM_MACHINE_MODES];\
1141 gcc_assert (mode >= 0 && mode < NUM_MACHINE_MODES);\n\
1142 switch (mode)\n\
1143 {");
1145 for_all_modes (c, m)
1147 const char *name = m->name;
1148 struct mode_data *m2 = m;
1149 if (c != MODE_PARTIAL_INT && m2->component)
1150 m2 = m2->component;
1151 if (!m2->need_bytesize_adj)
1152 printf (" case E_%smode: return %u;\n", name, m2->bytesize);
1155 puts ("\
1156 default: return mode_unit_size[mode];\n\
1157 }\n\
1158 }\n");
1161 /* Emit mode_unit_precision_inline routine into insn-modes.h header. */
1162 static void
1163 emit_mode_unit_precision_inline (void)
1165 int c;
1166 struct mode_data *m;
1168 puts ("\
1169 #ifdef __cplusplus\n\
1170 inline __attribute__((__always_inline__))\n\
1171 #else\n\
1172 extern __inline__ __attribute__((__always_inline__, __gnu_inline__))\n\
1173 #endif\n\
1174 unsigned short\n\
1175 mode_unit_precision_inline (machine_mode mode)\n\
1176 {\n\
1177 extern const unsigned short mode_unit_precision[NUM_MACHINE_MODES];\n\
1178 gcc_assert (mode >= 0 && mode < NUM_MACHINE_MODES);\n\
1179 switch (mode)\n\
1180 {");
1182 for_all_modes (c, m)
1184 struct mode_data *m2
1185 = (c != MODE_PARTIAL_INT && m->component) ? m->component : m;
1186 if (m2->precision != (unsigned int)-1)
1187 printf (" case E_%smode: return %u;\n", m->name, m2->precision);
1188 else
1189 printf (" case E_%smode: return %u*BITS_PER_UNIT;\n",
1190 m->name, m2->bytesize);
1193 puts ("\
1194 default: return mode_unit_precision[mode];\n\
1195 }\n\
1196 }\n");
1199 /* Return the best machine mode class for MODE, or null if machine_mode
1200 should be used. */
1202 static const char *
1203 get_mode_class (struct mode_data *mode)
1205 switch (mode->cl)
1207 case MODE_INT:
1208 case MODE_PARTIAL_INT:
1209 return "scalar_int_mode";
1211 case MODE_FRACT:
1212 case MODE_UFRACT:
1213 case MODE_ACCUM:
1214 case MODE_UACCUM:
1215 case MODE_POINTER_BOUNDS:
1216 return "scalar_mode";
1218 case MODE_FLOAT:
1219 case MODE_DECIMAL_FLOAT:
1220 return "scalar_float_mode";
1222 case MODE_COMPLEX_INT:
1223 case MODE_COMPLEX_FLOAT:
1224 return "complex_mode";
1226 default:
1227 return NULL;
1231 static void
1232 emit_insn_modes_h (void)
1234 int c;
1235 struct mode_data *m, *first, *last;
1236 int n_int_n_ents = 0;
1238 printf ("/* Generated automatically from machmode.def%s%s\n",
1239 HAVE_EXTRA_MODES ? " and " : "",
1240 EXTRA_MODES_FILE);
1242 puts ("\
1243 by genmodes. */\n\
1245 #ifndef GCC_INSN_MODES_H\n\
1246 #define GCC_INSN_MODES_H\n\
1248 enum machine_mode\n{");
1250 for (c = 0; c < MAX_MODE_CLASS; c++)
1251 for (m = modes[c]; m; m = m->next)
1253 int count_ = printf (" E_%smode,", m->name);
1254 printf ("%*s/* %s:%d */\n", 27 - count_, "",
1255 trim_filename (m->file), m->line);
1256 printf ("#define HAVE_%smode\n", m->name);
1257 printf ("#ifdef USE_ENUM_MODES\n");
1258 printf ("#define %smode E_%smode\n", m->name, m->name);
1259 printf ("#else\n");
1260 if (const char *mode_class = get_mode_class (m))
1261 printf ("#define %smode (%s ((%s::from_int) E_%smode))\n",
1262 m->name, mode_class, mode_class, m->name);
1263 else
1264 printf ("#define %smode ((void) 0, E_%smode)\n",
1265 m->name, m->name);
1266 printf ("#endif\n");
1269 puts (" MAX_MACHINE_MODE,\n");
1271 for (c = 0; c < MAX_MODE_CLASS; c++)
1273 first = modes[c];
1274 last = 0;
1275 for (m = first; m; last = m, m = m->next)
1278 /* Don't use BImode for MIN_MODE_INT, since otherwise the middle
1279 end will try to use it for bitfields in structures and the
1280 like, which we do not want. Only the target md file should
1281 generate BImode widgets. */
1282 if (first && first->precision == 1 && c == MODE_INT)
1283 first = first->next;
1285 if (first && last)
1286 printf (" MIN_%s = E_%smode,\n MAX_%s = E_%smode,\n\n",
1287 mode_class_names[c], first->name,
1288 mode_class_names[c], last->name);
1289 else
1290 printf (" MIN_%s = E_%smode,\n MAX_%s = E_%smode,\n\n",
1291 mode_class_names[c], void_mode->name,
1292 mode_class_names[c], void_mode->name);
1295 puts ("\
1296 NUM_MACHINE_MODES = MAX_MACHINE_MODE\n\
1297 };\n");
1299 /* I can't think of a better idea, can you? */
1300 printf ("#define CONST_MODE_NUNITS%s\n", adj_nunits ? "" : " const");
1301 printf ("#define CONST_MODE_PRECISION%s\n", adj_nunits ? "" : " const");
1302 printf ("#define CONST_MODE_SIZE%s\n",
1303 adj_bytesize || adj_nunits ? "" : " const");
1304 printf ("#define CONST_MODE_UNIT_SIZE%s\n", adj_bytesize ? "" : " const");
1305 printf ("#define CONST_MODE_BASE_ALIGN%s\n", adj_alignment ? "" : " const");
1306 #if 0 /* disabled for backward compatibility, temporary */
1307 printf ("#define CONST_REAL_FORMAT_FOR_MODE%s\n", adj_format ? "" :" const");
1308 #endif
1309 printf ("#define CONST_MODE_IBIT%s\n", adj_ibit ? "" : " const");
1310 printf ("#define CONST_MODE_FBIT%s\n", adj_fbit ? "" : " const");
1311 emit_max_int ();
1313 for_all_modes (c, m)
1314 if (m->int_n)
1315 n_int_n_ents ++;
1317 printf ("#define NUM_INT_N_ENTS %d\n", n_int_n_ents);
1319 printf ("#define NUM_POLY_INT_COEFFS %d\n", NUM_POLY_INT_COEFFS);
1321 puts ("\
1323 #endif /* insn-modes.h */");
1326 static void
1327 emit_insn_modes_inline_h (void)
1329 printf ("/* Generated automatically from machmode.def%s%s\n",
1330 HAVE_EXTRA_MODES ? " and " : "",
1331 EXTRA_MODES_FILE);
1333 puts ("\
1334 by genmodes. */\n\
1336 #ifndef GCC_INSN_MODES_INLINE_H\n\
1337 #define GCC_INSN_MODES_INLINE_H");
1339 puts ("\n#if !defined (USED_FOR_TARGET) && GCC_VERSION >= 4001\n");
1340 emit_mode_size_inline ();
1341 emit_mode_nunits_inline ();
1342 emit_mode_inner_inline ();
1343 emit_mode_unit_size_inline ();
1344 emit_mode_unit_precision_inline ();
1345 puts ("#endif /* GCC_VERSION >= 4001 */");
1347 puts ("\
1349 #endif /* insn-modes-inline.h */");
1352 static void
1353 emit_insn_modes_c_header (void)
1355 printf ("/* Generated automatically from machmode.def%s%s\n",
1356 HAVE_EXTRA_MODES ? " and " : "",
1357 EXTRA_MODES_FILE);
1359 puts ("\
1360 by genmodes. */\n\
1362 #include \"config.h\"\n\
1363 #include \"system.h\"\n\
1364 #include \"coretypes.h\"\n\
1365 #include \"tm.h\"\n\
1366 #include \"real.h\"");
1369 static void
1370 emit_min_insn_modes_c_header (void)
1372 printf ("/* Generated automatically from machmode.def%s%s\n",
1373 HAVE_EXTRA_MODES ? " and " : "",
1374 EXTRA_MODES_FILE);
1376 puts ("\
1377 by genmodes. */\n\
1379 #include \"bconfig.h\"\n\
1380 #include \"system.h\"\n\
1381 #include \"coretypes.h\"");
1384 static void
1385 emit_mode_name (void)
1387 int c;
1388 struct mode_data *m;
1390 print_decl ("char *const", "mode_name", "NUM_MACHINE_MODES");
1392 for_all_modes (c, m)
1393 printf (" \"%s\",\n", m->name);
1395 print_closer ();
1398 static void
1399 emit_mode_class (void)
1401 int c;
1402 struct mode_data *m;
1404 print_decl ("unsigned char", "mode_class", "NUM_MACHINE_MODES");
1406 for_all_modes (c, m)
1407 tagged_printf ("%s", mode_class_names[m->cl], m->name);
1409 print_closer ();
1412 static void
1413 emit_mode_precision (void)
1415 int c;
1416 struct mode_data *m;
1418 print_maybe_const_decl ("%spoly_uint16_pod", "mode_precision",
1419 "NUM_MACHINE_MODES", adj_nunits);
1421 for_all_modes (c, m)
1422 if (m->precision != (unsigned int)-1)
1423 tagged_printf ("{ %u" ZERO_COEFFS " }", m->precision, m->name);
1424 else
1425 tagged_printf ("{ %u * BITS_PER_UNIT" ZERO_COEFFS " }",
1426 m->bytesize, m->name);
1428 print_closer ();
1431 static void
1432 emit_mode_size (void)
1434 int c;
1435 struct mode_data *m;
1437 print_maybe_const_decl ("%spoly_uint16_pod", "mode_size",
1438 "NUM_MACHINE_MODES", adj_nunits || adj_bytesize);
1440 for_all_modes (c, m)
1441 tagged_printf ("{ %u" ZERO_COEFFS " }", m->bytesize, m->name);
1443 print_closer ();
1446 static void
1447 emit_mode_nunits (void)
1449 int c;
1450 struct mode_data *m;
1452 print_maybe_const_decl ("%spoly_uint16_pod", "mode_nunits",
1453 "NUM_MACHINE_MODES", adj_nunits);
1455 for_all_modes (c, m)
1456 tagged_printf ("{ %u" ZERO_COEFFS " }", m->ncomponents, m->name);
1458 print_closer ();
1461 static void
1462 emit_mode_wider (void)
1464 int c;
1465 struct mode_data *m;
1467 print_decl ("unsigned char", "mode_wider", "NUM_MACHINE_MODES");
1469 for_all_modes (c, m)
1470 tagged_printf ("E_%smode",
1471 m->wider ? m->wider->name : void_mode->name,
1472 m->name);
1474 print_closer ();
1475 print_decl ("unsigned char", "mode_2xwider", "NUM_MACHINE_MODES");
1477 for_all_modes (c, m)
1479 struct mode_data * m2;
1481 for (m2 = m;
1482 m2 && m2 != void_mode;
1483 m2 = m2->wider)
1485 if (m2->bytesize < 2 * m->bytesize)
1486 continue;
1487 if (m->precision != (unsigned int) -1)
1489 if (m2->precision != 2 * m->precision)
1490 continue;
1492 else
1494 if (m2->precision != (unsigned int) -1)
1495 continue;
1498 /* For vectors we want twice the number of components,
1499 with the same element type. */
1500 if (m->cl == MODE_VECTOR_BOOL
1501 || m->cl == MODE_VECTOR_INT
1502 || m->cl == MODE_VECTOR_FLOAT
1503 || m->cl == MODE_VECTOR_FRACT
1504 || m->cl == MODE_VECTOR_UFRACT
1505 || m->cl == MODE_VECTOR_ACCUM
1506 || m->cl == MODE_VECTOR_UACCUM)
1508 if (m2->ncomponents != 2 * m->ncomponents)
1509 continue;
1510 if (m->component != m2->component)
1511 continue;
1514 break;
1516 if (m2 == void_mode)
1517 m2 = 0;
1518 tagged_printf ("E_%smode",
1519 m2 ? m2->name : void_mode->name,
1520 m->name);
1523 print_closer ();
1526 static void
1527 emit_mode_complex (void)
1529 int c;
1530 struct mode_data *m;
1532 print_decl ("unsigned char", "mode_complex", "NUM_MACHINE_MODES");
1534 for_all_modes (c, m)
1535 tagged_printf ("E_%smode",
1536 m->complex ? m->complex->name : void_mode->name,
1537 m->name);
1539 print_closer ();
1542 static void
1543 emit_mode_mask (void)
1545 int c;
1546 struct mode_data *m;
1548 print_decl ("unsigned HOST_WIDE_INT", "mode_mask_array",
1549 "NUM_MACHINE_MODES");
1550 puts ("\
1551 #define MODE_MASK(m) \\\n\
1552 ((m) >= HOST_BITS_PER_WIDE_INT) \\\n\
1553 ? HOST_WIDE_INT_M1U \\\n\
1554 : (HOST_WIDE_INT_1U << (m)) - 1\n");
1556 for_all_modes (c, m)
1557 if (m->precision != (unsigned int)-1)
1558 tagged_printf ("MODE_MASK (%u)", m->precision, m->name);
1559 else
1560 tagged_printf ("MODE_MASK (%u*BITS_PER_UNIT)", m->bytesize, m->name);
1562 puts ("#undef MODE_MASK");
1563 print_closer ();
1566 static void
1567 emit_mode_inner (void)
1569 int c;
1570 struct mode_data *m;
1572 print_decl ("unsigned char", "mode_inner", "NUM_MACHINE_MODES");
1574 for_all_modes (c, m)
1575 tagged_printf ("E_%smode",
1576 c != MODE_PARTIAL_INT && m->component
1577 ? m->component->name : m->name,
1578 m->name);
1580 print_closer ();
1583 /* Emit mode_unit_size array into insn-modes.c file. */
1584 static void
1585 emit_mode_unit_size (void)
1587 int c;
1588 struct mode_data *m;
1590 print_maybe_const_decl ("%sunsigned char", "mode_unit_size",
1591 "NUM_MACHINE_MODES", adj_bytesize);
1593 for_all_modes (c, m)
1594 tagged_printf ("%u",
1595 c != MODE_PARTIAL_INT && m->component
1596 ? m->component->bytesize : m->bytesize, m->name);
1598 print_closer ();
1601 /* Emit mode_unit_precision array into insn-modes.c file. */
1602 static void
1603 emit_mode_unit_precision (void)
1605 int c;
1606 struct mode_data *m;
1608 print_decl ("unsigned short", "mode_unit_precision", "NUM_MACHINE_MODES");
1610 for_all_modes (c, m)
1612 struct mode_data *m2 = (c != MODE_PARTIAL_INT && m->component) ?
1613 m->component : m;
1614 if (m2->precision != (unsigned int)-1)
1615 tagged_printf ("%u", m2->precision, m->name);
1616 else
1617 tagged_printf ("%u*BITS_PER_UNIT", m2->bytesize, m->name);
1620 print_closer ();
1624 static void
1625 emit_mode_base_align (void)
1627 int c;
1628 struct mode_data *m;
1630 print_maybe_const_decl ("%sunsigned short",
1631 "mode_base_align", "NUM_MACHINE_MODES",
1632 adj_alignment);
1634 for_all_modes (c, m)
1635 tagged_printf ("%u", m->alignment, m->name);
1637 print_closer ();
1640 static void
1641 emit_class_narrowest_mode (void)
1643 int c;
1645 print_decl ("unsigned char", "class_narrowest_mode", "MAX_MODE_CLASS");
1647 for (c = 0; c < MAX_MODE_CLASS; c++)
1648 /* Bleah, all this to get the comment right for MIN_MODE_INT. */
1649 tagged_printf ("MIN_%s", mode_class_names[c],
1650 modes[c]
1651 ? ((c != MODE_INT || modes[c]->precision != 1)
1652 ? modes[c]->name
1653 : (modes[c]->next
1654 ? modes[c]->next->name
1655 : void_mode->name))
1656 : void_mode->name);
1658 print_closer ();
1661 static void
1662 emit_real_format_for_mode (void)
1664 struct mode_data *m;
1666 /* The entities pointed to by this table are constant, whether
1667 or not the table itself is constant.
1669 For backward compatibility this table is always writable
1670 (several targets modify it in TARGET_OPTION_OVERRIDE). FIXME:
1671 convert all said targets to use ADJUST_FORMAT instead. */
1672 #if 0
1673 print_maybe_const_decl ("const struct real_format *%s",
1674 "real_format_for_mode",
1675 "MAX_MODE_FLOAT - MIN_MODE_FLOAT + 1",
1676 format);
1677 #else
1678 print_decl ("struct real_format *\n", "real_format_for_mode",
1679 "MAX_MODE_FLOAT - MIN_MODE_FLOAT + 1 "
1680 "+ MAX_MODE_DECIMAL_FLOAT - MIN_MODE_DECIMAL_FLOAT + 1");
1681 #endif
1683 /* The beginning of the table is entries for float modes. */
1684 for (m = modes[MODE_FLOAT]; m; m = m->next)
1685 if (!strcmp (m->format, "0"))
1686 tagged_printf ("%s", m->format, m->name);
1687 else
1688 tagged_printf ("&%s", m->format, m->name);
1690 /* The end of the table is entries for decimal float modes. */
1691 for (m = modes[MODE_DECIMAL_FLOAT]; m; m = m->next)
1692 if (!strcmp (m->format, "0"))
1693 tagged_printf ("%s", m->format, m->name);
1694 else
1695 tagged_printf ("&%s", m->format, m->name);
1697 print_closer ();
1700 static void
1701 emit_mode_adjustments (void)
1703 struct mode_adjust *a;
1704 struct mode_data *m;
1706 puts ("\
1707 \nvoid\
1708 \ninit_adjust_machine_modes (void)\
1709 \n{\
1710 \n poly_uint16 ps ATTRIBUTE_UNUSED;\n\
1711 size_t s ATTRIBUTE_UNUSED;");
1713 for (a = adj_nunits; a; a = a->next)
1715 m = a->mode;
1716 printf ("\n"
1717 " {\n"
1718 " /* %s:%d */\n ps = %s;\n",
1719 a->file, a->line, a->adjustment);
1720 printf (" int old_factor = vector_element_size"
1721 " (mode_precision[E_%smode], mode_nunits[E_%smode]);\n",
1722 m->name, m->name);
1723 printf (" mode_precision[E_%smode] = ps * old_factor;\n", m->name);
1724 printf (" mode_size[E_%smode] = exact_div (mode_precision[E_%smode],"
1725 " BITS_PER_UNIT);\n", m->name, m->name);
1726 printf (" mode_nunits[E_%smode] = ps;\n", m->name);
1727 printf (" }\n");
1730 /* Size adjustments must be propagated to all containing modes.
1731 A size adjustment forces us to recalculate the alignment too. */
1732 for (a = adj_bytesize; a; a = a->next)
1734 printf ("\n /* %s:%d */\n", a->file, a->line);
1735 switch (a->mode->cl)
1737 case MODE_VECTOR_BOOL:
1738 case MODE_VECTOR_INT:
1739 case MODE_VECTOR_FLOAT:
1740 case MODE_VECTOR_FRACT:
1741 case MODE_VECTOR_UFRACT:
1742 case MODE_VECTOR_ACCUM:
1743 case MODE_VECTOR_UACCUM:
1744 printf (" ps = %s;\n", a->adjustment);
1745 printf (" s = mode_unit_size[E_%smode];\n", a->mode->name);
1746 break;
1748 default:
1749 printf (" ps = s = %s;\n", a->adjustment);
1750 printf (" mode_unit_size[E_%smode] = s;\n", a->mode->name);
1751 break;
1753 printf (" mode_size[E_%smode] = ps;\n", a->mode->name);
1754 printf (" mode_base_align[E_%smode] = known_alignment (ps);\n",
1755 a->mode->name);
1757 for (m = a->mode->contained; m; m = m->next_cont)
1759 switch (m->cl)
1761 case MODE_COMPLEX_INT:
1762 case MODE_COMPLEX_FLOAT:
1763 printf (" mode_size[E_%smode] = 2*s;\n", m->name);
1764 printf (" mode_unit_size[E_%smode] = s;\n", m->name);
1765 printf (" mode_base_align[E_%smode] = s & (~s + 1);\n",
1766 m->name);
1767 break;
1769 case MODE_VECTOR_BOOL:
1770 /* Changes to BImode should not affect vector booleans. */
1771 break;
1773 case MODE_VECTOR_INT:
1774 case MODE_VECTOR_FLOAT:
1775 case MODE_VECTOR_FRACT:
1776 case MODE_VECTOR_UFRACT:
1777 case MODE_VECTOR_ACCUM:
1778 case MODE_VECTOR_UACCUM:
1779 printf (" mode_size[E_%smode] = %d * ps;\n",
1780 m->name, m->ncomponents);
1781 printf (" mode_unit_size[E_%smode] = s;\n", m->name);
1782 printf (" mode_base_align[E_%smode]"
1783 " = known_alignment (%d * ps);\n",
1784 m->name, m->ncomponents);
1785 break;
1787 default:
1788 internal_error (
1789 "mode %s is neither vector nor complex but contains %s",
1790 m->name, a->mode->name);
1791 /* NOTREACHED */
1796 /* Alignment adjustments propagate too.
1797 ??? This may not be the right thing for vector modes. */
1798 for (a = adj_alignment; a; a = a->next)
1800 printf ("\n /* %s:%d */\n s = %s;\n",
1801 a->file, a->line, a->adjustment);
1802 printf (" mode_base_align[E_%smode] = s;\n", a->mode->name);
1804 for (m = a->mode->contained; m; m = m->next_cont)
1806 switch (m->cl)
1808 case MODE_COMPLEX_INT:
1809 case MODE_COMPLEX_FLOAT:
1810 printf (" mode_base_align[E_%smode] = s;\n", m->name);
1811 break;
1813 case MODE_VECTOR_BOOL:
1814 /* Changes to BImode should not affect vector booleans. */
1815 break;
1817 case MODE_VECTOR_INT:
1818 case MODE_VECTOR_FLOAT:
1819 case MODE_VECTOR_FRACT:
1820 case MODE_VECTOR_UFRACT:
1821 case MODE_VECTOR_ACCUM:
1822 case MODE_VECTOR_UACCUM:
1823 printf (" mode_base_align[E_%smode] = %d*s;\n",
1824 m->name, m->ncomponents);
1825 break;
1827 default:
1828 internal_error (
1829 "mode %s is neither vector nor complex but contains %s",
1830 m->name, a->mode->name);
1831 /* NOTREACHED */
1836 /* Ibit adjustments don't have to propagate. */
1837 for (a = adj_ibit; a; a = a->next)
1839 printf ("\n /* %s:%d */\n s = %s;\n",
1840 a->file, a->line, a->adjustment);
1841 printf (" mode_ibit[E_%smode] = s;\n", a->mode->name);
1844 /* Fbit adjustments don't have to propagate. */
1845 for (a = adj_fbit; a; a = a->next)
1847 printf ("\n /* %s:%d */\n s = %s;\n",
1848 a->file, a->line, a->adjustment);
1849 printf (" mode_fbit[E_%smode] = s;\n", a->mode->name);
1852 /* Real mode formats don't have to propagate anywhere. */
1853 for (a = adj_format; a; a = a->next)
1854 printf ("\n /* %s:%d */\n REAL_MODE_FORMAT (E_%smode) = %s;\n",
1855 a->file, a->line, a->mode->name, a->adjustment);
1857 puts ("}");
1860 /* Emit ibit for all modes. */
1862 static void
1863 emit_mode_ibit (void)
1865 int c;
1866 struct mode_data *m;
1868 print_maybe_const_decl ("%sunsigned char",
1869 "mode_ibit", "NUM_MACHINE_MODES",
1870 adj_ibit);
1872 for_all_modes (c, m)
1873 tagged_printf ("%u", m->ibit, m->name);
1875 print_closer ();
1878 /* Emit fbit for all modes. */
1880 static void
1881 emit_mode_fbit (void)
1883 int c;
1884 struct mode_data *m;
1886 print_maybe_const_decl ("%sunsigned char",
1887 "mode_fbit", "NUM_MACHINE_MODES",
1888 adj_fbit);
1890 for_all_modes (c, m)
1891 tagged_printf ("%u", m->fbit, m->name);
1893 print_closer ();
1896 /* Emit __intN for all modes. */
1898 static void
1899 emit_mode_int_n (void)
1901 int c;
1902 struct mode_data *m;
1903 struct mode_data **mode_sort;
1904 int n_modes = 0;
1905 int i, j;
1907 print_decl ("int_n_data_t", "int_n_data", "");
1909 n_modes = 0;
1910 for_all_modes (c, m)
1911 if (m->int_n)
1912 n_modes ++;
1913 mode_sort = XALLOCAVEC (struct mode_data *, n_modes);
1915 n_modes = 0;
1916 for_all_modes (c, m)
1917 if (m->int_n)
1918 mode_sort[n_modes++] = m;
1920 /* Yes, this is a bubblesort, but there are at most four (and
1921 usually only 1-2) entries to sort. */
1922 for (i = 0; i<n_modes - 1; i++)
1923 for (j = i + 1; j < n_modes; j++)
1924 if (mode_sort[i]->int_n > mode_sort[j]->int_n)
1925 std::swap (mode_sort[i], mode_sort[j]);
1927 for (i = 0; i < n_modes; i ++)
1929 m = mode_sort[i];
1930 printf(" {\n");
1931 tagged_printf ("%u", m->int_n, m->name);
1932 printf ("{ E_%smode },", m->name);
1933 printf(" },\n");
1936 print_closer ();
1940 static void
1941 emit_insn_modes_c (void)
1943 emit_insn_modes_c_header ();
1944 emit_mode_name ();
1945 emit_mode_class ();
1946 emit_mode_precision ();
1947 emit_mode_size ();
1948 emit_mode_nunits ();
1949 emit_mode_wider ();
1950 emit_mode_complex ();
1951 emit_mode_mask ();
1952 emit_mode_inner ();
1953 emit_mode_unit_size ();
1954 emit_mode_unit_precision ();
1955 emit_mode_base_align ();
1956 emit_class_narrowest_mode ();
1957 emit_real_format_for_mode ();
1958 emit_mode_adjustments ();
1959 emit_mode_ibit ();
1960 emit_mode_fbit ();
1961 emit_mode_int_n ();
1964 static void
1965 emit_min_insn_modes_c (void)
1967 emit_min_insn_modes_c_header ();
1968 emit_mode_name ();
1969 emit_mode_class ();
1970 emit_mode_nunits ();
1971 emit_mode_wider ();
1972 emit_mode_inner ();
1973 emit_class_narrowest_mode ();
1976 /* Master control. */
1978 main (int argc, char **argv)
1980 bool gen_header = false, gen_inlines = false, gen_min = false;
1981 progname = argv[0];
1983 if (argc == 1)
1985 else if (argc == 2 && !strcmp (argv[1], "-h"))
1986 gen_header = true;
1987 else if (argc == 2 && !strcmp (argv[1], "-i"))
1988 gen_inlines = true;
1989 else if (argc == 2 && !strcmp (argv[1], "-m"))
1990 gen_min = true;
1991 else
1993 error ("usage: %s [-h|-i|-m] > file", progname);
1994 return FATAL_EXIT_CODE;
1997 modes_by_name = htab_create_alloc (64, hash_mode, eq_mode, 0, xcalloc, free);
1999 create_modes ();
2000 complete_all_modes ();
2002 if (have_error)
2003 return FATAL_EXIT_CODE;
2005 calc_wider_mode ();
2007 if (gen_header)
2008 emit_insn_modes_h ();
2009 else if (gen_inlines)
2010 emit_insn_modes_inline_h ();
2011 else if (gen_min)
2012 emit_min_insn_modes_c ();
2013 else
2014 emit_insn_modes_c ();
2016 if (fflush (stdout) || fclose (stdout))
2017 return FATAL_EXIT_CODE;
2018 return SUCCESS_EXIT_CODE;