testsuite: Update scanning symbol sections to support AIX.
[official-gcc.git] / gcc / genmodes.c
blobbd78310ea24ed814508c6e40c411d93b65e08fdc
1 /* Generate the machine mode enumeration and associated tables.
2 Copyright (C) 2003-2020 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 #include "bconfig.h"
21 #include "system.h"
22 #include "errors.h"
24 /* enum mode_class is normally defined by machmode.h but we can't
25 include that header here. */
26 #include "mode-classes.def"
28 #define DEF_MODE_CLASS(M) M
29 enum mode_class { MODE_CLASSES, MAX_MODE_CLASS };
30 #undef DEF_MODE_CLASS
32 /* Text names of mode classes, for output. */
33 #define DEF_MODE_CLASS(M) #M
34 static const char *const mode_class_names[MAX_MODE_CLASS] =
36 MODE_CLASSES
38 #undef DEF_MODE_CLASS
39 #undef MODE_CLASSES
41 #ifdef EXTRA_MODES_FILE
42 # define HAVE_EXTRA_MODES 1
43 #else
44 # define HAVE_EXTRA_MODES 0
45 # define EXTRA_MODES_FILE ""
46 #endif
48 /* Data structure for building up what we know about a mode.
49 They're clustered by mode class. */
50 struct mode_data
52 struct mode_data *next; /* next this class - arbitrary order */
54 const char *name; /* printable mode name -- SI, not SImode */
55 enum mode_class cl; /* this mode class */
56 unsigned int order; /* top-level sorting order */
57 unsigned int precision; /* size in bits, equiv to TYPE_PRECISION */
58 unsigned int bytesize; /* storage size in addressable units */
59 unsigned int ncomponents; /* number of subunits */
60 unsigned int alignment; /* mode alignment */
61 const char *format; /* floating point format - float modes only */
63 struct mode_data *component; /* mode of components */
64 struct mode_data *wider; /* next wider mode */
66 struct mode_data *contained; /* Pointer to list of modes that have
67 this mode as a component. */
68 struct mode_data *next_cont; /* Next mode in that list. */
70 struct mode_data *complex; /* complex type with mode as component. */
71 const char *file; /* file and line of definition, */
72 unsigned int line; /* for error reporting */
73 unsigned int counter; /* Rank ordering of modes */
74 unsigned int ibit; /* the number of integral bits */
75 unsigned int fbit; /* the number of fractional bits */
76 bool need_nunits_adj; /* true if this mode needs dynamic nunits
77 adjustment */
78 bool need_bytesize_adj; /* true if this mode needs dynamic size
79 adjustment */
80 unsigned int int_n; /* If nonzero, then __int<INT_N> will be defined */
83 static struct mode_data *modes[MAX_MODE_CLASS];
84 static unsigned int n_modes[MAX_MODE_CLASS];
85 static struct mode_data *void_mode;
87 static const struct mode_data blank_mode = {
88 0, "<unknown>", MAX_MODE_CLASS,
89 0, -1U, -1U, -1U, -1U,
90 0, 0, 0, 0, 0, 0,
91 "<unknown>", 0, 0, 0, 0, false, false, 0
94 static htab_t modes_by_name;
96 /* Data structure for recording target-specified runtime adjustments
97 to a particular mode. We support varying the byte size, the
98 alignment, and the floating point format. */
99 struct mode_adjust
101 struct mode_adjust *next;
102 struct mode_data *mode;
103 const char *adjustment;
105 const char *file;
106 unsigned int line;
109 static struct mode_adjust *adj_nunits;
110 static struct mode_adjust *adj_bytesize;
111 static struct mode_adjust *adj_alignment;
112 static struct mode_adjust *adj_format;
113 static struct mode_adjust *adj_ibit;
114 static struct mode_adjust *adj_fbit;
116 /* Mode class operations. */
117 static enum mode_class
118 complex_class (enum mode_class c)
120 switch (c)
122 case MODE_INT: return MODE_COMPLEX_INT;
123 case MODE_PARTIAL_INT: return MODE_COMPLEX_INT;
124 case MODE_FLOAT: return MODE_COMPLEX_FLOAT;
125 default:
126 error ("no complex class for class %s", mode_class_names[c]);
127 return MODE_RANDOM;
131 static enum mode_class
132 vector_class (enum mode_class cl)
134 switch (cl)
136 case MODE_INT: return MODE_VECTOR_INT;
137 case MODE_FLOAT: return MODE_VECTOR_FLOAT;
138 case MODE_FRACT: return MODE_VECTOR_FRACT;
139 case MODE_UFRACT: return MODE_VECTOR_UFRACT;
140 case MODE_ACCUM: return MODE_VECTOR_ACCUM;
141 case MODE_UACCUM: return MODE_VECTOR_UACCUM;
142 default:
143 error ("no vector class for class %s", mode_class_names[cl]);
144 return MODE_RANDOM;
148 /* Utility routines. */
149 static inline struct mode_data *
150 find_mode (const char *name)
152 struct mode_data key;
154 key.name = name;
155 return (struct mode_data *) htab_find (modes_by_name, &key);
158 static struct mode_data *
159 new_mode (enum mode_class cl, const char *name,
160 const char *file, unsigned int line)
162 struct mode_data *m;
163 static unsigned int count = 0;
165 m = find_mode (name);
166 if (m)
168 error ("%s:%d: duplicate definition of mode \"%s\"",
169 trim_filename (file), line, name);
170 error ("%s:%d: previous definition here", m->file, m->line);
171 return m;
174 m = XNEW (struct mode_data);
175 memcpy (m, &blank_mode, sizeof (struct mode_data));
176 m->cl = cl;
177 m->name = name;
178 if (file)
179 m->file = trim_filename (file);
180 m->line = line;
181 m->counter = count++;
183 m->next = modes[cl];
184 modes[cl] = m;
185 n_modes[cl]++;
187 *htab_find_slot (modes_by_name, m, INSERT) = m;
189 return m;
192 static hashval_t
193 hash_mode (const void *p)
195 const struct mode_data *m = (const struct mode_data *)p;
196 return htab_hash_string (m->name);
199 static int
200 eq_mode (const void *p, const void *q)
202 const struct mode_data *a = (const struct mode_data *)p;
203 const struct mode_data *b = (const struct mode_data *)q;
205 return !strcmp (a->name, b->name);
208 #define for_all_modes(C, M) \
209 for (C = 0; C < MAX_MODE_CLASS; C++) \
210 for (M = modes[C]; M; M = M->next)
212 static void ATTRIBUTE_UNUSED
213 new_adjust (const char *name,
214 struct mode_adjust **category, const char *catname,
215 const char *adjustment,
216 enum mode_class required_class_from,
217 enum mode_class required_class_to,
218 const char *file, unsigned int line)
220 struct mode_data *mode = find_mode (name);
221 struct mode_adjust *a;
223 file = trim_filename (file);
225 if (!mode)
227 error ("%s:%d: no mode \"%s\"", file, line, name);
228 return;
231 if (required_class_from != MODE_RANDOM
232 && (mode->cl < required_class_from || mode->cl > required_class_to))
234 error ("%s:%d: mode \"%s\" is not among class {%s, %s}",
235 file, line, name, mode_class_names[required_class_from] + 5,
236 mode_class_names[required_class_to] + 5);
237 return;
240 for (a = *category; a; a = a->next)
241 if (a->mode == mode)
243 error ("%s:%d: mode \"%s\" already has a %s adjustment",
244 file, line, name, catname);
245 error ("%s:%d: previous adjustment here", a->file, a->line);
246 return;
249 a = XNEW (struct mode_adjust);
250 a->mode = mode;
251 a->adjustment = adjustment;
252 a->file = file;
253 a->line = line;
255 a->next = *category;
256 *category = a;
259 /* Diagnose failure to meet expectations in a partially filled out
260 mode structure. */
261 enum requirement { SET, UNSET, OPTIONAL };
263 #define validate_field_(mname, fname, req, val, unset, file, line) do { \
264 switch (req) \
266 case SET: \
267 if (val == unset) \
268 error ("%s:%d: (%s) field %s must be set", \
269 file, line, mname, fname); \
270 break; \
271 case UNSET: \
272 if (val != unset) \
273 error ("%s:%d: (%s) field %s must not be set", \
274 file, line, mname, fname); \
275 case OPTIONAL: \
276 break; \
278 } while (0)
280 #define validate_field(M, F) \
281 validate_field_(M->name, #F, r_##F, M->F, blank_mode.F, M->file, M->line)
283 static void
284 validate_mode (struct mode_data *m,
285 enum requirement r_precision,
286 enum requirement r_bytesize,
287 enum requirement r_component,
288 enum requirement r_ncomponents,
289 enum requirement r_format)
291 validate_field (m, precision);
292 validate_field (m, bytesize);
293 validate_field (m, component);
294 validate_field (m, ncomponents);
295 validate_field (m, format);
297 #undef validate_field
298 #undef validate_field_
300 /* Given a partially-filled-out mode structure, figure out what we can
301 and fill the rest of it in; die if it isn't enough. */
302 static void
303 complete_mode (struct mode_data *m)
305 unsigned int alignment;
307 if (!m->name)
309 error ("%s:%d: mode with no name", m->file, m->line);
310 return;
312 if (m->cl == MAX_MODE_CLASS)
314 error ("%s:%d: %smode has no mode class", m->file, m->line, m->name);
315 return;
318 switch (m->cl)
320 case MODE_RANDOM:
321 /* Nothing more need be said. */
322 if (!strcmp (m->name, "VOID"))
323 void_mode = m;
325 validate_mode (m, UNSET, UNSET, UNSET, UNSET, UNSET);
327 m->precision = 0;
328 m->bytesize = 0;
329 m->ncomponents = 0;
330 m->component = 0;
331 break;
333 case MODE_CC:
334 /* Again, nothing more need be said. For historical reasons,
335 the size of a CC mode is four units. */
336 validate_mode (m, UNSET, UNSET, UNSET, UNSET, UNSET);
338 m->bytesize = 4;
339 m->ncomponents = 1;
340 m->component = 0;
341 break;
343 case MODE_INT:
344 case MODE_FLOAT:
345 case MODE_DECIMAL_FLOAT:
346 case MODE_FRACT:
347 case MODE_UFRACT:
348 case MODE_ACCUM:
349 case MODE_UACCUM:
350 /* A scalar mode must have a byte size, may have a bit size,
351 and must not have components. A float mode must have a
352 format. */
353 validate_mode (m, OPTIONAL, SET, UNSET, UNSET,
354 (m->cl == MODE_FLOAT || m->cl == MODE_DECIMAL_FLOAT)
355 ? SET : UNSET);
357 m->ncomponents = 1;
358 m->component = 0;
359 break;
361 case MODE_PARTIAL_INT:
362 /* A partial integer mode uses ->component to say what the
363 corresponding full-size integer mode is, and may also
364 specify a bit size. */
365 validate_mode (m, OPTIONAL, UNSET, SET, UNSET, UNSET);
367 m->bytesize = m->component->bytesize;
369 m->ncomponents = 1;
370 break;
372 case MODE_COMPLEX_INT:
373 case MODE_COMPLEX_FLOAT:
374 /* Complex modes should have a component indicated, but no more. */
375 validate_mode (m, UNSET, UNSET, SET, UNSET, UNSET);
376 m->ncomponents = 2;
377 if (m->component->precision != (unsigned int)-1)
378 m->precision = 2 * m->component->precision;
379 m->bytesize = 2 * m->component->bytesize;
380 break;
382 case MODE_VECTOR_BOOL:
383 validate_mode (m, UNSET, SET, SET, SET, UNSET);
384 break;
386 case MODE_VECTOR_INT:
387 case MODE_VECTOR_FLOAT:
388 case MODE_VECTOR_FRACT:
389 case MODE_VECTOR_UFRACT:
390 case MODE_VECTOR_ACCUM:
391 case MODE_VECTOR_UACCUM:
392 /* Vector modes should have a component and a number of components. */
393 validate_mode (m, UNSET, UNSET, SET, SET, UNSET);
394 if (m->component->precision != (unsigned int)-1)
395 m->precision = m->ncomponents * m->component->precision;
396 m->bytesize = m->ncomponents * m->component->bytesize;
397 break;
399 default:
400 gcc_unreachable ();
403 /* If not already specified, the mode alignment defaults to the largest
404 power of two that divides the size of the object. Complex types are
405 not more aligned than their contents. */
406 if (m->cl == MODE_COMPLEX_INT || m->cl == MODE_COMPLEX_FLOAT)
407 alignment = m->component->bytesize;
408 else
409 alignment = m->bytesize;
411 m->alignment = alignment & (~alignment + 1);
413 /* If this mode has components, make the component mode point back
414 to this mode, for the sake of adjustments. */
415 if (m->component)
417 m->next_cont = m->component->contained;
418 m->component->contained = m;
422 static void
423 complete_all_modes (void)
425 struct mode_data *m;
426 int cl;
428 for_all_modes (cl, m)
429 complete_mode (m);
432 /* For each mode in class CLASS, construct a corresponding complex mode. */
433 #define COMPLEX_MODES(C) make_complex_modes (MODE_##C, __FILE__, __LINE__)
434 static void
435 make_complex_modes (enum mode_class cl,
436 const char *file, unsigned int line)
438 struct mode_data *m;
439 struct mode_data *c;
440 enum mode_class cclass = complex_class (cl);
442 if (cclass == MODE_RANDOM)
443 return;
445 for (m = modes[cl]; m; m = m->next)
447 char *p, *buf;
448 size_t m_len;
450 /* Skip BImode. FIXME: BImode probably shouldn't be MODE_INT. */
451 if (m->precision == 1)
452 continue;
454 m_len = strlen (m->name);
455 /* The leading "1 +" is in case we prepend a "C" below. */
456 buf = (char *) xmalloc (1 + m_len + 1);
458 /* Float complex modes are named SCmode, etc.
459 Int complex modes are named CSImode, etc.
460 This inconsistency should be eliminated. */
461 p = 0;
462 if (cl == MODE_FLOAT)
464 memcpy (buf, m->name, m_len + 1);
465 p = strchr (buf, 'F');
466 if (p == 0 && strchr (buf, 'D') == 0)
468 error ("%s:%d: float mode \"%s\" has no 'F' or 'D'",
469 m->file, m->line, m->name);
470 free (buf);
471 continue;
474 if (p != 0)
475 *p = 'C';
476 else
478 buf[0] = 'C';
479 memcpy (buf + 1, m->name, m_len + 1);
482 c = new_mode (cclass, buf, file, line);
483 c->component = m;
484 m->complex = c;
488 /* For all modes in class CL, construct vector modes of width WIDTH,
489 having as many components as necessary. ORDER is the sorting order
490 of the mode, with smaller numbers indicating a higher priority. */
491 #define VECTOR_MODES_WITH_PREFIX(PREFIX, C, W, ORDER) \
492 make_vector_modes (MODE_##C, #PREFIX, W, ORDER, __FILE__, __LINE__)
493 #define VECTOR_MODES(C, W) VECTOR_MODES_WITH_PREFIX (V, C, W, 0)
494 static void ATTRIBUTE_UNUSED
495 make_vector_modes (enum mode_class cl, const char *prefix, unsigned int width,
496 unsigned int order, const char *file, unsigned int line)
498 struct mode_data *m;
499 struct mode_data *v;
500 /* Big enough for a 32-bit UINT_MAX plus the text. */
501 char buf[12];
502 unsigned int ncomponents;
503 enum mode_class vclass = vector_class (cl);
505 if (vclass == MODE_RANDOM)
506 return;
508 for (m = modes[cl]; m; m = m->next)
510 /* Do not construct vector modes with only one element, or
511 vector modes where the element size doesn't divide the full
512 size evenly. */
513 ncomponents = width / m->bytesize;
514 if (ncomponents < 2)
515 continue;
516 if (width % m->bytesize)
517 continue;
519 /* Skip QFmode and BImode. FIXME: this special case should
520 not be necessary. */
521 if (cl == MODE_FLOAT && m->bytesize == 1)
522 continue;
523 if (cl == MODE_INT && m->precision == 1)
524 continue;
526 if ((size_t) snprintf (buf, sizeof buf, "%s%u%s", prefix,
527 ncomponents, m->name) >= sizeof buf)
529 error ("%s:%d: mode name \"%s\" is too long",
530 m->file, m->line, m->name);
531 continue;
534 v = new_mode (vclass, xstrdup (buf), file, line);
535 v->order = order;
536 v->component = m;
537 v->ncomponents = ncomponents;
541 /* Create a vector of booleans called NAME with COUNT elements and
542 BYTESIZE bytes in total. */
543 #define VECTOR_BOOL_MODE(NAME, COUNT, BYTESIZE) \
544 make_vector_bool_mode (#NAME, COUNT, BYTESIZE, __FILE__, __LINE__)
545 static void ATTRIBUTE_UNUSED
546 make_vector_bool_mode (const char *name, unsigned int count,
547 unsigned int bytesize, const char *file,
548 unsigned int line)
550 struct mode_data *m = find_mode ("BI");
551 if (!m)
553 error ("%s:%d: no mode \"BI\"", file, line);
554 return;
557 struct mode_data *v = new_mode (MODE_VECTOR_BOOL, name, file, line);
558 v->component = m;
559 v->ncomponents = count;
560 v->bytesize = bytesize;
563 /* Input. */
565 #define _SPECIAL_MODE(C, N) \
566 make_special_mode (MODE_##C, #N, __FILE__, __LINE__)
567 #define RANDOM_MODE(N) _SPECIAL_MODE (RANDOM, N)
568 #define CC_MODE(N) _SPECIAL_MODE (CC, N)
570 static void
571 make_special_mode (enum mode_class cl, const char *name,
572 const char *file, unsigned int line)
574 new_mode (cl, name, file, line);
577 #define INT_MODE(N, Y) FRACTIONAL_INT_MODE (N, -1U, Y)
578 #define FRACTIONAL_INT_MODE(N, B, Y) \
579 make_int_mode (#N, B, Y, __FILE__, __LINE__)
581 static void
582 make_int_mode (const char *name,
583 unsigned int precision, unsigned int bytesize,
584 const char *file, unsigned int line)
586 struct mode_data *m = new_mode (MODE_INT, name, file, line);
587 m->bytesize = bytesize;
588 m->precision = precision;
591 #define FRACT_MODE(N, Y, F) \
592 make_fixed_point_mode (MODE_FRACT, #N, Y, 0, F, __FILE__, __LINE__)
594 #define UFRACT_MODE(N, Y, F) \
595 make_fixed_point_mode (MODE_UFRACT, #N, Y, 0, F, __FILE__, __LINE__)
597 #define ACCUM_MODE(N, Y, I, F) \
598 make_fixed_point_mode (MODE_ACCUM, #N, Y, I, F, __FILE__, __LINE__)
600 #define UACCUM_MODE(N, Y, I, F) \
601 make_fixed_point_mode (MODE_UACCUM, #N, Y, I, F, __FILE__, __LINE__)
603 /* Create a fixed-point mode by setting CL, NAME, BYTESIZE, IBIT, FBIT,
604 FILE, and LINE. */
606 static void
607 make_fixed_point_mode (enum mode_class cl,
608 const char *name,
609 unsigned int bytesize,
610 unsigned int ibit,
611 unsigned int fbit,
612 const char *file, unsigned int line)
614 struct mode_data *m = new_mode (cl, name, file, line);
615 m->bytesize = bytesize;
616 m->ibit = ibit;
617 m->fbit = fbit;
620 #define FLOAT_MODE(N, Y, F) FRACTIONAL_FLOAT_MODE (N, -1U, Y, F)
621 #define FRACTIONAL_FLOAT_MODE(N, B, Y, F) \
622 make_float_mode (#N, B, Y, #F, __FILE__, __LINE__)
624 static void
625 make_float_mode (const char *name,
626 unsigned int precision, unsigned int bytesize,
627 const char *format,
628 const char *file, unsigned int line)
630 struct mode_data *m = new_mode (MODE_FLOAT, name, file, line);
631 m->bytesize = bytesize;
632 m->precision = precision;
633 m->format = format;
636 #define DECIMAL_FLOAT_MODE(N, Y, F) \
637 FRACTIONAL_DECIMAL_FLOAT_MODE (N, -1U, Y, F)
638 #define FRACTIONAL_DECIMAL_FLOAT_MODE(N, B, Y, F) \
639 make_decimal_float_mode (#N, B, Y, #F, __FILE__, __LINE__)
641 static void
642 make_decimal_float_mode (const char *name,
643 unsigned int precision, unsigned int bytesize,
644 const char *format,
645 const char *file, unsigned int line)
647 struct mode_data *m = new_mode (MODE_DECIMAL_FLOAT, name, file, line);
648 m->bytesize = bytesize;
649 m->precision = precision;
650 m->format = format;
653 #define RESET_FLOAT_FORMAT(N, F) \
654 reset_float_format (#N, #F, __FILE__, __LINE__)
655 static void ATTRIBUTE_UNUSED
656 reset_float_format (const char *name, const char *format,
657 const char *file, unsigned int line)
659 struct mode_data *m = find_mode (name);
660 if (!m)
662 error ("%s:%d: no mode \"%s\"", file, line, name);
663 return;
665 if (m->cl != MODE_FLOAT && m->cl != MODE_DECIMAL_FLOAT)
667 error ("%s:%d: mode \"%s\" is not a FLOAT class", file, line, name);
668 return;
670 m->format = format;
673 /* __intN support. */
674 #define INT_N(M,PREC) \
675 make_int_n (#M, PREC, __FILE__, __LINE__)
676 static void ATTRIBUTE_UNUSED
677 make_int_n (const char *m, int bitsize,
678 const char *file, unsigned int line)
680 struct mode_data *component = find_mode (m);
681 if (!component)
683 error ("%s:%d: no mode \"%s\"", file, line, m);
684 return;
686 if (component->cl != MODE_INT
687 && component->cl != MODE_PARTIAL_INT)
689 error ("%s:%d: mode \"%s\" is not class INT or PARTIAL_INT", file, line, m);
690 return;
692 if (component->int_n != 0)
694 error ("%s:%d: mode \"%s\" already has an intN", file, line, m);
695 return;
698 component->int_n = bitsize;
701 /* Partial integer modes are specified by relation to a full integer
702 mode. */
703 #define PARTIAL_INT_MODE(M,PREC,NAME) \
704 make_partial_integer_mode (#M, #NAME, PREC, __FILE__, __LINE__)
705 static void ATTRIBUTE_UNUSED
706 make_partial_integer_mode (const char *base, const char *name,
707 unsigned int precision,
708 const char *file, unsigned int line)
710 struct mode_data *m;
711 struct mode_data *component = find_mode (base);
712 if (!component)
714 error ("%s:%d: no mode \"%s\"", file, line, name);
715 return;
717 if (component->cl != MODE_INT)
719 error ("%s:%d: mode \"%s\" is not class INT", file, line, name);
720 return;
723 m = new_mode (MODE_PARTIAL_INT, name, file, line);
724 m->precision = precision;
725 m->component = component;
728 /* A single vector mode can be specified by naming its component
729 mode and the number of components. */
730 #define VECTOR_MODE(C, M, N) \
731 make_vector_mode (MODE_##C, #M, N, __FILE__, __LINE__);
732 static void ATTRIBUTE_UNUSED
733 make_vector_mode (enum mode_class bclass,
734 const char *base,
735 unsigned int ncomponents,
736 const char *file, unsigned int line)
738 struct mode_data *v;
739 enum mode_class vclass = vector_class (bclass);
740 struct mode_data *component = find_mode (base);
741 char namebuf[16];
743 if (vclass == MODE_RANDOM)
744 return;
745 if (component == 0)
747 error ("%s:%d: no mode \"%s\"", file, line, base);
748 return;
750 if (component->cl != bclass
751 && (component->cl != MODE_PARTIAL_INT
752 || bclass != MODE_INT))
754 error ("%s:%d: mode \"%s\" is not class %s",
755 file, line, base, mode_class_names[bclass] + 5);
756 return;
759 if ((size_t)snprintf (namebuf, sizeof namebuf, "V%u%s",
760 ncomponents, base) >= sizeof namebuf)
762 error ("%s:%d: mode name \"%s\" is too long",
763 file, line, base);
764 return;
767 v = new_mode (vclass, xstrdup (namebuf), file, line);
768 v->ncomponents = ncomponents;
769 v->component = component;
772 /* Adjustability. */
773 #define _ADD_ADJUST(A, M, X, C1, C2) \
774 new_adjust (#M, &adj_##A, #A, #X, MODE_##C1, MODE_##C2, __FILE__, __LINE__)
776 #define ADJUST_NUNITS(M, X) _ADD_ADJUST (nunits, M, X, RANDOM, RANDOM)
777 #define ADJUST_BYTESIZE(M, X) _ADD_ADJUST (bytesize, M, X, RANDOM, RANDOM)
778 #define ADJUST_ALIGNMENT(M, X) _ADD_ADJUST (alignment, M, X, RANDOM, RANDOM)
779 #define ADJUST_FLOAT_FORMAT(M, X) _ADD_ADJUST (format, M, X, FLOAT, FLOAT)
780 #define ADJUST_IBIT(M, X) _ADD_ADJUST (ibit, M, X, ACCUM, UACCUM)
781 #define ADJUST_FBIT(M, X) _ADD_ADJUST (fbit, M, X, FRACT, UACCUM)
783 static int bits_per_unit;
784 static int max_bitsize_mode_any_int;
785 static int max_bitsize_mode_any_mode;
787 static void
788 create_modes (void)
790 #include "machmode.def"
792 /* So put the default value unless the target needs a non standard
793 value. */
794 #ifdef BITS_PER_UNIT
795 bits_per_unit = BITS_PER_UNIT;
796 #else
797 bits_per_unit = 8;
798 #endif
800 #ifdef MAX_BITSIZE_MODE_ANY_INT
801 max_bitsize_mode_any_int = MAX_BITSIZE_MODE_ANY_INT;
802 #else
803 max_bitsize_mode_any_int = 0;
804 #endif
806 #ifdef MAX_BITSIZE_MODE_ANY_MODE
807 max_bitsize_mode_any_mode = MAX_BITSIZE_MODE_ANY_MODE;
808 #else
809 max_bitsize_mode_any_mode = 0;
810 #endif
813 #ifndef NUM_POLY_INT_COEFFS
814 #define NUM_POLY_INT_COEFFS 1
815 #endif
817 /* Processing. */
819 /* Sort a list of modes into the order needed for the WIDER field:
820 major sort by precision, minor sort by component precision.
822 For instance:
823 QI < HI < SI < DI < TI
824 V4QI < V2HI < V8QI < V4HI < V2SI.
826 If the precision is not set, sort by the bytesize. A mode with
827 precision set gets sorted before a mode without precision set, if
828 they have the same bytesize; this is the right thing because
829 the precision must always be smaller than the bytesize * BITS_PER_UNIT.
830 We don't have to do anything special to get this done -- an unset
831 precision shows up as (unsigned int)-1, i.e. UINT_MAX. */
832 static int
833 cmp_modes (const void *a, const void *b)
835 const struct mode_data *const m = *(const struct mode_data *const*)a;
836 const struct mode_data *const n = *(const struct mode_data *const*)b;
838 if (m->order > n->order)
839 return 1;
840 else if (m->order < n->order)
841 return -1;
843 if (m->bytesize > n->bytesize)
844 return 1;
845 else if (m->bytesize < n->bytesize)
846 return -1;
848 if (m->precision > n->precision)
849 return 1;
850 else if (m->precision < n->precision)
851 return -1;
853 if (!m->component && !n->component)
855 if (m->counter < n->counter)
856 return -1;
857 else
858 return 1;
861 if (m->component->bytesize > n->component->bytesize)
862 return 1;
863 else if (m->component->bytesize < n->component->bytesize)
864 return -1;
866 if (m->component->precision > n->component->precision)
867 return 1;
868 else if (m->component->precision < n->component->precision)
869 return -1;
871 if (m->counter < n->counter)
872 return -1;
873 else
874 return 1;
877 static void
878 calc_wider_mode (void)
880 int c;
881 struct mode_data *m;
882 struct mode_data **sortbuf;
883 unsigned int max_n_modes = 0;
884 unsigned int i, j;
886 for (c = 0; c < MAX_MODE_CLASS; c++)
887 max_n_modes = MAX (max_n_modes, n_modes[c]);
889 /* Allocate max_n_modes + 1 entries to leave room for the extra null
890 pointer assigned after the qsort call below. */
891 sortbuf = XALLOCAVEC (struct mode_data *, max_n_modes + 1);
893 for (c = 0; c < MAX_MODE_CLASS; c++)
895 /* "wider" is not meaningful for MODE_RANDOM and MODE_CC.
896 However, we want these in textual order, and we have
897 precisely the reverse. */
898 if (c == MODE_RANDOM || c == MODE_CC)
900 struct mode_data *prev, *next;
902 for (prev = 0, m = modes[c]; m; m = next)
904 m->wider = void_mode;
906 /* this is nreverse */
907 next = m->next;
908 m->next = prev;
909 prev = m;
911 modes[c] = prev;
913 else
915 if (!modes[c])
916 continue;
918 for (i = 0, m = modes[c]; m; i++, m = m->next)
919 sortbuf[i] = m;
921 (qsort) (sortbuf, i, sizeof (struct mode_data *), cmp_modes);
923 sortbuf[i] = 0;
924 for (j = 0; j < i; j++)
926 sortbuf[j]->next = sortbuf[j + 1];
927 if (c == MODE_PARTIAL_INT)
928 sortbuf[j]->wider = sortbuf[j]->component;
929 else
930 sortbuf[j]->wider = sortbuf[j]->next;
933 modes[c] = sortbuf[0];
938 /* Text to add to the constant part of a poly_int_pod initializer in
939 order to fill out te whole structure. */
940 #if NUM_POLY_INT_COEFFS == 1
941 #define ZERO_COEFFS ""
942 #elif NUM_POLY_INT_COEFFS == 2
943 #define ZERO_COEFFS ", 0"
944 #else
945 #error "Unknown value of NUM_POLY_INT_COEFFS"
946 #endif
948 /* Output routines. */
950 #define tagged_printf(FMT, ARG, TAG) do { \
951 int count_ = printf (" " FMT ",", ARG); \
952 printf ("%*s/* %s */\n", 27 - count_, "", TAG); \
953 } while (0)
955 #define print_decl(TYPE, NAME, ASIZE) \
956 puts ("\nconst " TYPE " " NAME "[" ASIZE "] =\n{");
958 #define print_maybe_const_decl(TYPE, NAME, ASIZE, NEEDS_ADJ) \
959 printf ("\n" TYPE " " NAME "[" ASIZE "] = \n{\n", \
960 NEEDS_ADJ ? "" : "const ")
962 #define print_closer() puts ("};")
964 /* Compute the max bitsize of some of the classes of integers. It may
965 be that there are needs for the other integer classes, and this
966 code is easy to extend. */
967 static void
968 emit_max_int (void)
970 unsigned int max, mmax;
971 struct mode_data *i;
972 int j;
974 puts ("");
976 printf ("#define BITS_PER_UNIT (%d)\n", bits_per_unit);
978 if (max_bitsize_mode_any_int == 0)
980 for (max = 1, i = modes[MODE_INT]; i; i = i->next)
981 if (max < i->bytesize)
982 max = i->bytesize;
983 mmax = max;
984 for (max = 1, i = modes[MODE_PARTIAL_INT]; i; i = i->next)
985 if (max < i->bytesize)
986 max = i->bytesize;
987 if (max > mmax)
988 mmax = max;
989 printf ("#define MAX_BITSIZE_MODE_ANY_INT (%d*BITS_PER_UNIT)\n", mmax);
991 else
992 printf ("#define MAX_BITSIZE_MODE_ANY_INT %d\n", max_bitsize_mode_any_int);
994 if (max_bitsize_mode_any_mode == 0)
996 mmax = 0;
997 for (j = 0; j < MAX_MODE_CLASS; j++)
998 for (i = modes[j]; i; i = i->next)
999 if (mmax < i->bytesize)
1000 mmax = i->bytesize;
1001 printf ("#define MAX_BITSIZE_MODE_ANY_MODE (%d*BITS_PER_UNIT)\n", mmax);
1003 else
1004 printf ("#define MAX_BITSIZE_MODE_ANY_MODE %d\n",
1005 max_bitsize_mode_any_mode);
1008 /* Emit mode_size_inline routine into insn-modes.h header. */
1009 static void
1010 emit_mode_size_inline (void)
1012 int c;
1013 struct mode_adjust *a;
1014 struct mode_data *m;
1016 /* Size adjustments must be propagated to all containing modes. */
1017 for (a = adj_bytesize; a; a = a->next)
1019 a->mode->need_bytesize_adj = true;
1020 for (m = a->mode->contained; m; m = m->next_cont)
1021 m->need_bytesize_adj = true;
1024 /* Changing the number of units by a factor of X also changes the size
1025 by a factor of X. */
1026 for (mode_adjust *a = adj_nunits; a; a = a->next)
1027 a->mode->need_bytesize_adj = true;
1029 printf ("\
1030 #ifdef __cplusplus\n\
1031 inline __attribute__((__always_inline__))\n\
1032 #else\n\
1033 extern __inline__ __attribute__((__always_inline__, __gnu_inline__))\n\
1034 #endif\n\
1035 poly_uint16\n\
1036 mode_size_inline (machine_mode mode)\n\
1037 {\n\
1038 extern %spoly_uint16_pod mode_size[NUM_MACHINE_MODES];\n\
1039 gcc_assert (mode >= 0 && mode < NUM_MACHINE_MODES);\n\
1040 switch (mode)\n\
1041 {\n", adj_nunits || adj_bytesize ? "" : "const ");
1043 for_all_modes (c, m)
1044 if (!m->need_bytesize_adj)
1045 printf (" case E_%smode: return %u;\n", m->name, m->bytesize);
1047 puts ("\
1048 default: return mode_size[mode];\n\
1049 }\n\
1050 }\n");
1053 /* Emit mode_nunits_inline routine into insn-modes.h header. */
1054 static void
1055 emit_mode_nunits_inline (void)
1057 int c;
1058 struct mode_data *m;
1060 for (mode_adjust *a = adj_nunits; a; a = a->next)
1061 a->mode->need_nunits_adj = true;
1063 printf ("\
1064 #ifdef __cplusplus\n\
1065 inline __attribute__((__always_inline__))\n\
1066 #else\n\
1067 extern __inline__ __attribute__((__always_inline__, __gnu_inline__))\n\
1068 #endif\n\
1069 poly_uint16\n\
1070 mode_nunits_inline (machine_mode mode)\n\
1071 {\n\
1072 extern %spoly_uint16_pod mode_nunits[NUM_MACHINE_MODES];\n\
1073 switch (mode)\n\
1074 {\n", adj_nunits ? "" : "const ");
1076 for_all_modes (c, m)
1077 if (!m->need_nunits_adj)
1078 printf (" case E_%smode: return %u;\n", m->name, m->ncomponents);
1080 puts ("\
1081 default: return mode_nunits[mode];\n\
1082 }\n\
1083 }\n");
1086 /* Emit mode_inner_inline routine into insn-modes.h header. */
1087 static void
1088 emit_mode_inner_inline (void)
1090 int c;
1091 struct mode_data *m;
1093 puts ("\
1094 #ifdef __cplusplus\n\
1095 inline __attribute__((__always_inline__))\n\
1096 #else\n\
1097 extern __inline__ __attribute__((__always_inline__, __gnu_inline__))\n\
1098 #endif\n\
1099 unsigned char\n\
1100 mode_inner_inline (machine_mode mode)\n\
1101 {\n\
1102 extern const unsigned char mode_inner[NUM_MACHINE_MODES];\n\
1103 gcc_assert (mode >= 0 && mode < NUM_MACHINE_MODES);\n\
1104 switch (mode)\n\
1105 {");
1107 for_all_modes (c, m)
1108 printf (" case E_%smode: return E_%smode;\n", m->name,
1109 c != MODE_PARTIAL_INT && m->component
1110 ? m->component->name : m->name);
1112 puts ("\
1113 default: return mode_inner[mode];\n\
1114 }\n\
1115 }\n");
1118 /* Emit mode_unit_size_inline routine into insn-modes.h header. */
1119 static void
1120 emit_mode_unit_size_inline (void)
1122 int c;
1123 struct mode_data *m;
1125 puts ("\
1126 #ifdef __cplusplus\n\
1127 inline __attribute__((__always_inline__))\n\
1128 #else\n\
1129 extern __inline__ __attribute__((__always_inline__, __gnu_inline__))\n\
1130 #endif\n\
1131 unsigned char\n\
1132 mode_unit_size_inline (machine_mode mode)\n\
1133 {\n\
1134 extern CONST_MODE_UNIT_SIZE unsigned char mode_unit_size[NUM_MACHINE_MODES];\
1136 gcc_assert (mode >= 0 && mode < NUM_MACHINE_MODES);\n\
1137 switch (mode)\n\
1138 {");
1140 for_all_modes (c, m)
1142 const char *name = m->name;
1143 struct mode_data *m2 = m;
1144 if (c != MODE_PARTIAL_INT && m2->component)
1145 m2 = m2->component;
1146 if (!m2->need_bytesize_adj)
1147 printf (" case E_%smode: return %u;\n", name, m2->bytesize);
1150 puts ("\
1151 default: return mode_unit_size[mode];\n\
1152 }\n\
1153 }\n");
1156 /* Emit mode_unit_precision_inline routine into insn-modes.h header. */
1157 static void
1158 emit_mode_unit_precision_inline (void)
1160 int c;
1161 struct mode_data *m;
1163 puts ("\
1164 #ifdef __cplusplus\n\
1165 inline __attribute__((__always_inline__))\n\
1166 #else\n\
1167 extern __inline__ __attribute__((__always_inline__, __gnu_inline__))\n\
1168 #endif\n\
1169 unsigned short\n\
1170 mode_unit_precision_inline (machine_mode mode)\n\
1171 {\n\
1172 extern const unsigned short mode_unit_precision[NUM_MACHINE_MODES];\n\
1173 gcc_assert (mode >= 0 && mode < NUM_MACHINE_MODES);\n\
1174 switch (mode)\n\
1175 {");
1177 for_all_modes (c, m)
1179 struct mode_data *m2
1180 = (c != MODE_PARTIAL_INT && m->component) ? m->component : m;
1181 if (m2->precision != (unsigned int)-1)
1182 printf (" case E_%smode: return %u;\n", m->name, m2->precision);
1183 else
1184 printf (" case E_%smode: return %u*BITS_PER_UNIT;\n",
1185 m->name, m2->bytesize);
1188 puts ("\
1189 default: return mode_unit_precision[mode];\n\
1190 }\n\
1191 }\n");
1194 /* Return the best machine mode class for MODE, or null if machine_mode
1195 should be used. */
1197 static const char *
1198 get_mode_class (struct mode_data *mode)
1200 switch (mode->cl)
1202 case MODE_INT:
1203 case MODE_PARTIAL_INT:
1204 return "scalar_int_mode";
1206 case MODE_FRACT:
1207 case MODE_UFRACT:
1208 case MODE_ACCUM:
1209 case MODE_UACCUM:
1210 return "scalar_mode";
1212 case MODE_FLOAT:
1213 case MODE_DECIMAL_FLOAT:
1214 return "scalar_float_mode";
1216 case MODE_COMPLEX_INT:
1217 case MODE_COMPLEX_FLOAT:
1218 return "complex_mode";
1220 default:
1221 return NULL;
1225 static void
1226 emit_insn_modes_h (void)
1228 int c;
1229 struct mode_data *m, *first, *last;
1230 int n_int_n_ents = 0;
1232 printf ("/* Generated automatically from machmode.def%s%s\n",
1233 HAVE_EXTRA_MODES ? " and " : "",
1234 EXTRA_MODES_FILE);
1236 puts ("\
1237 by genmodes. */\n\
1239 #ifndef GCC_INSN_MODES_H\n\
1240 #define GCC_INSN_MODES_H\n\
1242 enum machine_mode\n{");
1244 for (c = 0; c < MAX_MODE_CLASS; c++)
1245 for (m = modes[c]; m; m = m->next)
1247 int count_ = printf (" E_%smode,", m->name);
1248 printf ("%*s/* %s:%d */\n", 27 - count_, "",
1249 trim_filename (m->file), m->line);
1250 printf ("#define HAVE_%smode\n", m->name);
1251 printf ("#ifdef USE_ENUM_MODES\n");
1252 printf ("#define %smode E_%smode\n", m->name, m->name);
1253 printf ("#else\n");
1254 if (const char *mode_class = get_mode_class (m))
1255 printf ("#define %smode (%s ((%s::from_int) E_%smode))\n",
1256 m->name, mode_class, mode_class, m->name);
1257 else
1258 printf ("#define %smode ((void) 0, E_%smode)\n",
1259 m->name, m->name);
1260 printf ("#endif\n");
1263 puts (" MAX_MACHINE_MODE,\n");
1265 for (c = 0; c < MAX_MODE_CLASS; c++)
1267 first = modes[c];
1268 last = 0;
1269 for (m = first; m; last = m, m = m->next)
1272 /* Don't use BImode for MIN_MODE_INT, since otherwise the middle
1273 end will try to use it for bitfields in structures and the
1274 like, which we do not want. Only the target md file should
1275 generate BImode widgets. */
1276 if (first && first->precision == 1 && c == MODE_INT)
1277 first = first->next;
1279 if (first && last)
1280 printf (" MIN_%s = E_%smode,\n MAX_%s = E_%smode,\n\n",
1281 mode_class_names[c], first->name,
1282 mode_class_names[c], last->name);
1283 else
1284 printf (" MIN_%s = E_%smode,\n MAX_%s = E_%smode,\n\n",
1285 mode_class_names[c], void_mode->name,
1286 mode_class_names[c], void_mode->name);
1289 puts ("\
1290 NUM_MACHINE_MODES = MAX_MACHINE_MODE\n\
1291 };\n");
1293 /* I can't think of a better idea, can you? */
1294 printf ("#define CONST_MODE_NUNITS%s\n", adj_nunits ? "" : " const");
1295 printf ("#define CONST_MODE_PRECISION%s\n", adj_nunits ? "" : " const");
1296 printf ("#define CONST_MODE_SIZE%s\n",
1297 adj_bytesize || adj_nunits ? "" : " const");
1298 printf ("#define CONST_MODE_UNIT_SIZE%s\n", adj_bytesize ? "" : " const");
1299 printf ("#define CONST_MODE_BASE_ALIGN%s\n", adj_alignment ? "" : " const");
1300 #if 0 /* disabled for backward compatibility, temporary */
1301 printf ("#define CONST_REAL_FORMAT_FOR_MODE%s\n", adj_format ? "" :" const");
1302 #endif
1303 printf ("#define CONST_MODE_IBIT%s\n", adj_ibit ? "" : " const");
1304 printf ("#define CONST_MODE_FBIT%s\n", adj_fbit ? "" : " const");
1305 emit_max_int ();
1307 for_all_modes (c, m)
1308 if (m->int_n)
1309 n_int_n_ents ++;
1311 printf ("#define NUM_INT_N_ENTS %d\n", n_int_n_ents);
1313 printf ("#define NUM_POLY_INT_COEFFS %d\n", NUM_POLY_INT_COEFFS);
1315 puts ("\
1317 #endif /* insn-modes.h */");
1320 static void
1321 emit_insn_modes_inline_h (void)
1323 printf ("/* Generated automatically from machmode.def%s%s\n",
1324 HAVE_EXTRA_MODES ? " and " : "",
1325 EXTRA_MODES_FILE);
1327 puts ("\
1328 by genmodes. */\n\
1330 #ifndef GCC_INSN_MODES_INLINE_H\n\
1331 #define GCC_INSN_MODES_INLINE_H");
1333 puts ("\n#if !defined (USED_FOR_TARGET) && GCC_VERSION >= 4001\n");
1334 emit_mode_size_inline ();
1335 emit_mode_nunits_inline ();
1336 emit_mode_inner_inline ();
1337 emit_mode_unit_size_inline ();
1338 emit_mode_unit_precision_inline ();
1339 puts ("#endif /* GCC_VERSION >= 4001 */");
1341 puts ("\
1343 #endif /* insn-modes-inline.h */");
1346 static void
1347 emit_insn_modes_c_header (void)
1349 printf ("/* Generated automatically from machmode.def%s%s\n",
1350 HAVE_EXTRA_MODES ? " and " : "",
1351 EXTRA_MODES_FILE);
1353 puts ("\
1354 by genmodes. */\n\
1356 #include \"config.h\"\n\
1357 #include \"system.h\"\n\
1358 #include \"coretypes.h\"\n\
1359 #include \"tm.h\"\n\
1360 #include \"real.h\"");
1363 static void
1364 emit_min_insn_modes_c_header (void)
1366 printf ("/* Generated automatically from machmode.def%s%s\n",
1367 HAVE_EXTRA_MODES ? " and " : "",
1368 EXTRA_MODES_FILE);
1370 puts ("\
1371 by genmodes. */\n\
1373 #include \"bconfig.h\"\n\
1374 #include \"system.h\"\n\
1375 #include \"coretypes.h\"");
1378 static void
1379 emit_mode_name (void)
1381 int c;
1382 struct mode_data *m;
1384 print_decl ("char *const", "mode_name", "NUM_MACHINE_MODES");
1386 for_all_modes (c, m)
1387 printf (" \"%s\",\n", m->name);
1389 print_closer ();
1392 static void
1393 emit_mode_class (void)
1395 int c;
1396 struct mode_data *m;
1398 print_decl ("unsigned char", "mode_class", "NUM_MACHINE_MODES");
1400 for_all_modes (c, m)
1401 tagged_printf ("%s", mode_class_names[m->cl], m->name);
1403 print_closer ();
1406 static void
1407 emit_mode_precision (void)
1409 int c;
1410 struct mode_data *m;
1412 print_maybe_const_decl ("%spoly_uint16_pod", "mode_precision",
1413 "NUM_MACHINE_MODES", adj_nunits);
1415 for_all_modes (c, m)
1416 if (m->precision != (unsigned int)-1)
1417 tagged_printf ("{ %u" ZERO_COEFFS " }", m->precision, m->name);
1418 else
1419 tagged_printf ("{ %u * BITS_PER_UNIT" ZERO_COEFFS " }",
1420 m->bytesize, m->name);
1422 print_closer ();
1425 static void
1426 emit_mode_size (void)
1428 int c;
1429 struct mode_data *m;
1431 print_maybe_const_decl ("%spoly_uint16_pod", "mode_size",
1432 "NUM_MACHINE_MODES", adj_nunits || adj_bytesize);
1434 for_all_modes (c, m)
1435 tagged_printf ("{ %u" ZERO_COEFFS " }", m->bytesize, m->name);
1437 print_closer ();
1440 static void
1441 emit_mode_nunits (void)
1443 int c;
1444 struct mode_data *m;
1446 print_maybe_const_decl ("%spoly_uint16_pod", "mode_nunits",
1447 "NUM_MACHINE_MODES", adj_nunits);
1449 for_all_modes (c, m)
1450 tagged_printf ("{ %u" ZERO_COEFFS " }", m->ncomponents, m->name);
1452 print_closer ();
1455 static void
1456 emit_mode_wider (void)
1458 int c;
1459 struct mode_data *m;
1461 print_decl ("unsigned char", "mode_wider", "NUM_MACHINE_MODES");
1463 for_all_modes (c, m)
1464 tagged_printf ("E_%smode",
1465 m->wider ? m->wider->name : void_mode->name,
1466 m->name);
1468 print_closer ();
1469 print_decl ("unsigned char", "mode_2xwider", "NUM_MACHINE_MODES");
1471 for_all_modes (c, m)
1473 struct mode_data * m2;
1475 for (m2 = m;
1476 m2 && m2 != void_mode;
1477 m2 = m2->wider)
1479 if (m2->bytesize < 2 * m->bytesize)
1480 continue;
1481 if (m->precision != (unsigned int) -1)
1483 if (m2->precision != 2 * m->precision)
1484 continue;
1486 else
1488 if (m2->precision != (unsigned int) -1)
1489 continue;
1492 /* For vectors we want twice the number of components,
1493 with the same element type. */
1494 if (m->cl == MODE_VECTOR_BOOL
1495 || m->cl == MODE_VECTOR_INT
1496 || m->cl == MODE_VECTOR_FLOAT
1497 || m->cl == MODE_VECTOR_FRACT
1498 || m->cl == MODE_VECTOR_UFRACT
1499 || m->cl == MODE_VECTOR_ACCUM
1500 || m->cl == MODE_VECTOR_UACCUM)
1502 if (m2->ncomponents != 2 * m->ncomponents)
1503 continue;
1504 if (m->component != m2->component)
1505 continue;
1508 break;
1510 if (m2 == void_mode)
1511 m2 = 0;
1512 tagged_printf ("E_%smode",
1513 m2 ? m2->name : void_mode->name,
1514 m->name);
1517 print_closer ();
1520 static void
1521 emit_mode_complex (void)
1523 int c;
1524 struct mode_data *m;
1526 print_decl ("unsigned char", "mode_complex", "NUM_MACHINE_MODES");
1528 for_all_modes (c, m)
1529 tagged_printf ("E_%smode",
1530 m->complex ? m->complex->name : void_mode->name,
1531 m->name);
1533 print_closer ();
1536 static void
1537 emit_mode_mask (void)
1539 int c;
1540 struct mode_data *m;
1542 print_decl ("unsigned HOST_WIDE_INT", "mode_mask_array",
1543 "NUM_MACHINE_MODES");
1544 puts ("\
1545 #define MODE_MASK(m) \\\n\
1546 ((m) >= HOST_BITS_PER_WIDE_INT) \\\n\
1547 ? HOST_WIDE_INT_M1U \\\n\
1548 : (HOST_WIDE_INT_1U << (m)) - 1\n");
1550 for_all_modes (c, m)
1551 if (m->precision != (unsigned int)-1)
1552 tagged_printf ("MODE_MASK (%u)", m->precision, m->name);
1553 else
1554 tagged_printf ("MODE_MASK (%u*BITS_PER_UNIT)", m->bytesize, m->name);
1556 puts ("#undef MODE_MASK");
1557 print_closer ();
1560 static void
1561 emit_mode_inner (void)
1563 int c;
1564 struct mode_data *m;
1566 print_decl ("unsigned char", "mode_inner", "NUM_MACHINE_MODES");
1568 for_all_modes (c, m)
1569 tagged_printf ("E_%smode",
1570 c != MODE_PARTIAL_INT && m->component
1571 ? m->component->name : m->name,
1572 m->name);
1574 print_closer ();
1577 /* Emit mode_unit_size array into insn-modes.c file. */
1578 static void
1579 emit_mode_unit_size (void)
1581 int c;
1582 struct mode_data *m;
1584 print_maybe_const_decl ("%sunsigned char", "mode_unit_size",
1585 "NUM_MACHINE_MODES", adj_bytesize);
1587 for_all_modes (c, m)
1588 tagged_printf ("%u",
1589 c != MODE_PARTIAL_INT && m->component
1590 ? m->component->bytesize : m->bytesize, m->name);
1592 print_closer ();
1595 /* Emit mode_unit_precision array into insn-modes.c file. */
1596 static void
1597 emit_mode_unit_precision (void)
1599 int c;
1600 struct mode_data *m;
1602 print_decl ("unsigned short", "mode_unit_precision", "NUM_MACHINE_MODES");
1604 for_all_modes (c, m)
1606 struct mode_data *m2 = (c != MODE_PARTIAL_INT && m->component) ?
1607 m->component : m;
1608 if (m2->precision != (unsigned int)-1)
1609 tagged_printf ("%u", m2->precision, m->name);
1610 else
1611 tagged_printf ("%u*BITS_PER_UNIT", m2->bytesize, m->name);
1614 print_closer ();
1618 static void
1619 emit_mode_base_align (void)
1621 int c;
1622 struct mode_data *m;
1624 print_maybe_const_decl ("%sunsigned short",
1625 "mode_base_align", "NUM_MACHINE_MODES",
1626 adj_alignment);
1628 for_all_modes (c, m)
1629 tagged_printf ("%u", m->alignment, m->name);
1631 print_closer ();
1634 static void
1635 emit_class_narrowest_mode (void)
1637 int c;
1639 print_decl ("unsigned char", "class_narrowest_mode", "MAX_MODE_CLASS");
1641 for (c = 0; c < MAX_MODE_CLASS; c++)
1642 /* Bleah, all this to get the comment right for MIN_MODE_INT. */
1643 tagged_printf ("MIN_%s", mode_class_names[c],
1644 modes[c]
1645 ? ((c != MODE_INT || modes[c]->precision != 1)
1646 ? modes[c]->name
1647 : (modes[c]->next
1648 ? modes[c]->next->name
1649 : void_mode->name))
1650 : void_mode->name);
1652 print_closer ();
1655 static void
1656 emit_real_format_for_mode (void)
1658 struct mode_data *m;
1660 /* The entities pointed to by this table are constant, whether
1661 or not the table itself is constant.
1663 For backward compatibility this table is always writable
1664 (several targets modify it in TARGET_OPTION_OVERRIDE). FIXME:
1665 convert all said targets to use ADJUST_FORMAT instead. */
1666 #if 0
1667 print_maybe_const_decl ("const struct real_format *%s",
1668 "real_format_for_mode",
1669 "MAX_MODE_FLOAT - MIN_MODE_FLOAT + 1",
1670 format);
1671 #else
1672 print_decl ("struct real_format *\n", "real_format_for_mode",
1673 "MAX_MODE_FLOAT - MIN_MODE_FLOAT + 1 "
1674 "+ MAX_MODE_DECIMAL_FLOAT - MIN_MODE_DECIMAL_FLOAT + 1");
1675 #endif
1677 /* The beginning of the table is entries for float modes. */
1678 for (m = modes[MODE_FLOAT]; m; m = m->next)
1679 if (!strcmp (m->format, "0"))
1680 tagged_printf ("%s", m->format, m->name);
1681 else
1682 tagged_printf ("&%s", m->format, m->name);
1684 /* The end of the table is entries for decimal float modes. */
1685 for (m = modes[MODE_DECIMAL_FLOAT]; m; m = m->next)
1686 if (!strcmp (m->format, "0"))
1687 tagged_printf ("%s", m->format, m->name);
1688 else
1689 tagged_printf ("&%s", m->format, m->name);
1691 print_closer ();
1694 static void
1695 emit_mode_adjustments (void)
1697 struct mode_adjust *a;
1698 struct mode_data *m;
1700 puts ("\
1701 \nvoid\
1702 \ninit_adjust_machine_modes (void)\
1703 \n{\
1704 \n poly_uint16 ps ATTRIBUTE_UNUSED;\n\
1705 size_t s ATTRIBUTE_UNUSED;");
1707 for (a = adj_nunits; a; a = a->next)
1709 m = a->mode;
1710 printf ("\n"
1711 " {\n"
1712 " /* %s:%d */\n ps = %s;\n",
1713 a->file, a->line, a->adjustment);
1714 printf (" int old_factor = vector_element_size"
1715 " (mode_precision[E_%smode], mode_nunits[E_%smode]);\n",
1716 m->name, m->name);
1717 printf (" mode_precision[E_%smode] = ps * old_factor;\n", m->name);
1718 printf (" mode_size[E_%smode] = exact_div (mode_precision[E_%smode],"
1719 " BITS_PER_UNIT);\n", m->name, m->name);
1720 printf (" mode_nunits[E_%smode] = ps;\n", m->name);
1721 printf (" }\n");
1724 /* Size adjustments must be propagated to all containing modes.
1725 A size adjustment forces us to recalculate the alignment too. */
1726 for (a = adj_bytesize; a; a = a->next)
1728 printf ("\n /* %s:%d */\n", a->file, a->line);
1729 switch (a->mode->cl)
1731 case MODE_VECTOR_BOOL:
1732 case MODE_VECTOR_INT:
1733 case MODE_VECTOR_FLOAT:
1734 case MODE_VECTOR_FRACT:
1735 case MODE_VECTOR_UFRACT:
1736 case MODE_VECTOR_ACCUM:
1737 case MODE_VECTOR_UACCUM:
1738 printf (" ps = %s;\n", a->adjustment);
1739 printf (" s = mode_unit_size[E_%smode];\n", a->mode->name);
1740 break;
1742 default:
1743 printf (" ps = s = %s;\n", a->adjustment);
1744 printf (" mode_unit_size[E_%smode] = s;\n", a->mode->name);
1745 break;
1747 printf (" mode_size[E_%smode] = ps;\n", a->mode->name);
1748 printf (" mode_base_align[E_%smode] = known_alignment (ps);\n",
1749 a->mode->name);
1751 for (m = a->mode->contained; m; m = m->next_cont)
1753 switch (m->cl)
1755 case MODE_COMPLEX_INT:
1756 case MODE_COMPLEX_FLOAT:
1757 printf (" mode_size[E_%smode] = 2*s;\n", m->name);
1758 printf (" mode_unit_size[E_%smode] = s;\n", m->name);
1759 printf (" mode_base_align[E_%smode] = s & (~s + 1);\n",
1760 m->name);
1761 break;
1763 case MODE_VECTOR_BOOL:
1764 /* Changes to BImode should not affect vector booleans. */
1765 break;
1767 case MODE_VECTOR_INT:
1768 case MODE_VECTOR_FLOAT:
1769 case MODE_VECTOR_FRACT:
1770 case MODE_VECTOR_UFRACT:
1771 case MODE_VECTOR_ACCUM:
1772 case MODE_VECTOR_UACCUM:
1773 printf (" mode_size[E_%smode] = %d * ps;\n",
1774 m->name, m->ncomponents);
1775 printf (" mode_unit_size[E_%smode] = s;\n", m->name);
1776 printf (" mode_base_align[E_%smode]"
1777 " = known_alignment (%d * ps);\n",
1778 m->name, m->ncomponents);
1779 break;
1781 default:
1782 internal_error (
1783 "mode %s is neither vector nor complex but contains %s",
1784 m->name, a->mode->name);
1785 /* NOTREACHED */
1790 /* Alignment adjustments propagate too.
1791 ??? This may not be the right thing for vector modes. */
1792 for (a = adj_alignment; a; a = a->next)
1794 printf ("\n /* %s:%d */\n s = %s;\n",
1795 a->file, a->line, a->adjustment);
1796 printf (" mode_base_align[E_%smode] = s;\n", a->mode->name);
1798 for (m = a->mode->contained; m; m = m->next_cont)
1800 switch (m->cl)
1802 case MODE_COMPLEX_INT:
1803 case MODE_COMPLEX_FLOAT:
1804 printf (" mode_base_align[E_%smode] = s;\n", m->name);
1805 break;
1807 case MODE_VECTOR_BOOL:
1808 /* Changes to BImode should not affect vector booleans. */
1809 break;
1811 case MODE_VECTOR_INT:
1812 case MODE_VECTOR_FLOAT:
1813 case MODE_VECTOR_FRACT:
1814 case MODE_VECTOR_UFRACT:
1815 case MODE_VECTOR_ACCUM:
1816 case MODE_VECTOR_UACCUM:
1817 printf (" mode_base_align[E_%smode] = %d*s;\n",
1818 m->name, m->ncomponents);
1819 break;
1821 default:
1822 internal_error (
1823 "mode %s is neither vector nor complex but contains %s",
1824 m->name, a->mode->name);
1825 /* NOTREACHED */
1830 /* Ibit adjustments don't have to propagate. */
1831 for (a = adj_ibit; a; a = a->next)
1833 printf ("\n /* %s:%d */\n s = %s;\n",
1834 a->file, a->line, a->adjustment);
1835 printf (" mode_ibit[E_%smode] = s;\n", a->mode->name);
1838 /* Fbit adjustments don't have to propagate. */
1839 for (a = adj_fbit; a; a = a->next)
1841 printf ("\n /* %s:%d */\n s = %s;\n",
1842 a->file, a->line, a->adjustment);
1843 printf (" mode_fbit[E_%smode] = s;\n", a->mode->name);
1846 /* Real mode formats don't have to propagate anywhere. */
1847 for (a = adj_format; a; a = a->next)
1848 printf ("\n /* %s:%d */\n REAL_MODE_FORMAT (E_%smode) = %s;\n",
1849 a->file, a->line, a->mode->name, a->adjustment);
1851 puts ("}");
1854 /* Emit ibit for all modes. */
1856 static void
1857 emit_mode_ibit (void)
1859 int c;
1860 struct mode_data *m;
1862 print_maybe_const_decl ("%sunsigned char",
1863 "mode_ibit", "NUM_MACHINE_MODES",
1864 adj_ibit);
1866 for_all_modes (c, m)
1867 tagged_printf ("%u", m->ibit, m->name);
1869 print_closer ();
1872 /* Emit fbit for all modes. */
1874 static void
1875 emit_mode_fbit (void)
1877 int c;
1878 struct mode_data *m;
1880 print_maybe_const_decl ("%sunsigned char",
1881 "mode_fbit", "NUM_MACHINE_MODES",
1882 adj_fbit);
1884 for_all_modes (c, m)
1885 tagged_printf ("%u", m->fbit, m->name);
1887 print_closer ();
1890 /* Emit __intN for all modes. */
1892 static void
1893 emit_mode_int_n (void)
1895 int c;
1896 struct mode_data *m;
1897 struct mode_data **mode_sort;
1898 int n_modes = 0;
1899 int i, j;
1901 print_decl ("int_n_data_t", "int_n_data", "");
1903 n_modes = 0;
1904 for_all_modes (c, m)
1905 if (m->int_n)
1906 n_modes ++;
1907 mode_sort = XALLOCAVEC (struct mode_data *, n_modes);
1909 n_modes = 0;
1910 for_all_modes (c, m)
1911 if (m->int_n)
1912 mode_sort[n_modes++] = m;
1914 /* Yes, this is a bubblesort, but there are at most four (and
1915 usually only 1-2) entries to sort. */
1916 for (i = 0; i<n_modes - 1; i++)
1917 for (j = i + 1; j < n_modes; j++)
1918 if (mode_sort[i]->int_n > mode_sort[j]->int_n)
1919 std::swap (mode_sort[i], mode_sort[j]);
1921 for (i = 0; i < n_modes; i ++)
1923 m = mode_sort[i];
1924 printf(" {\n");
1925 tagged_printf ("%u", m->int_n, m->name);
1926 printf ("{ E_%smode },", m->name);
1927 printf(" },\n");
1930 print_closer ();
1934 static void
1935 emit_insn_modes_c (void)
1937 emit_insn_modes_c_header ();
1938 emit_mode_name ();
1939 emit_mode_class ();
1940 emit_mode_precision ();
1941 emit_mode_size ();
1942 emit_mode_nunits ();
1943 emit_mode_wider ();
1944 emit_mode_complex ();
1945 emit_mode_mask ();
1946 emit_mode_inner ();
1947 emit_mode_unit_size ();
1948 emit_mode_unit_precision ();
1949 emit_mode_base_align ();
1950 emit_class_narrowest_mode ();
1951 emit_real_format_for_mode ();
1952 emit_mode_adjustments ();
1953 emit_mode_ibit ();
1954 emit_mode_fbit ();
1955 emit_mode_int_n ();
1958 static void
1959 emit_min_insn_modes_c (void)
1961 emit_min_insn_modes_c_header ();
1962 emit_mode_name ();
1963 emit_mode_class ();
1964 emit_mode_nunits ();
1965 emit_mode_wider ();
1966 emit_mode_inner ();
1967 emit_class_narrowest_mode ();
1970 /* Master control. */
1972 main (int argc, char **argv)
1974 bool gen_header = false, gen_inlines = false, gen_min = false;
1975 progname = argv[0];
1977 if (argc == 1)
1979 else if (argc == 2 && !strcmp (argv[1], "-h"))
1980 gen_header = true;
1981 else if (argc == 2 && !strcmp (argv[1], "-i"))
1982 gen_inlines = true;
1983 else if (argc == 2 && !strcmp (argv[1], "-m"))
1984 gen_min = true;
1985 else
1987 error ("usage: %s [-h|-i|-m] > file", progname);
1988 return FATAL_EXIT_CODE;
1991 modes_by_name = htab_create_alloc (64, hash_mode, eq_mode, 0, xcalloc, free);
1993 create_modes ();
1994 complete_all_modes ();
1996 if (have_error)
1997 return FATAL_EXIT_CODE;
1999 calc_wider_mode ();
2001 if (gen_header)
2002 emit_insn_modes_h ();
2003 else if (gen_inlines)
2004 emit_insn_modes_inline_h ();
2005 else if (gen_min)
2006 emit_min_insn_modes_c ();
2007 else
2008 emit_insn_modes_c ();
2010 if (fflush (stdout) || fclose (stdout))
2011 return FATAL_EXIT_CODE;
2012 return SUCCESS_EXIT_CODE;