2015-11-30 Richard Biener <rguenther@suse.de>
[official-gcc.git] / gcc / genmodes.c
blob15d62a0f40532df910344fe95dece098e597a363
1 /* Generate the machine mode enumeration and associated tables.
2 Copyright (C) 2003-2015 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 const char *file; /* file and line of definition, */
70 unsigned int line; /* for error reporting */
71 unsigned int counter; /* Rank ordering of modes */
72 unsigned int ibit; /* the number of integral bits */
73 unsigned int fbit; /* the number of fractional bits */
74 bool need_bytesize_adj; /* true if this mode need dynamic size
75 adjustment */
76 unsigned int int_n; /* If nonzero, then __int<INT_N> will be defined */
79 static struct mode_data *modes[MAX_MODE_CLASS];
80 static unsigned int n_modes[MAX_MODE_CLASS];
81 static struct mode_data *void_mode;
83 static const struct mode_data blank_mode = {
84 0, "<unknown>", MAX_MODE_CLASS,
85 -1U, -1U, -1U, -1U,
86 0, 0, 0, 0, 0,
87 "<unknown>", 0, 0, 0, 0, false, 0
90 static htab_t modes_by_name;
92 /* Data structure for recording target-specified runtime adjustments
93 to a particular mode. We support varying the byte size, the
94 alignment, and the floating point format. */
95 struct mode_adjust
97 struct mode_adjust *next;
98 struct mode_data *mode;
99 const char *adjustment;
101 const char *file;
102 unsigned int line;
105 static struct mode_adjust *adj_bytesize;
106 static struct mode_adjust *adj_alignment;
107 static struct mode_adjust *adj_format;
108 static struct mode_adjust *adj_ibit;
109 static struct mode_adjust *adj_fbit;
111 /* Mode class operations. */
112 static enum mode_class
113 complex_class (enum mode_class c)
115 switch (c)
117 case MODE_INT: return MODE_COMPLEX_INT;
118 case MODE_FLOAT: return MODE_COMPLEX_FLOAT;
119 default:
120 error ("no complex class for class %s", mode_class_names[c]);
121 return MODE_RANDOM;
125 static enum mode_class
126 vector_class (enum mode_class cl)
128 switch (cl)
130 case MODE_INT: return MODE_VECTOR_INT;
131 case MODE_FLOAT: return MODE_VECTOR_FLOAT;
132 case MODE_FRACT: return MODE_VECTOR_FRACT;
133 case MODE_UFRACT: return MODE_VECTOR_UFRACT;
134 case MODE_ACCUM: return MODE_VECTOR_ACCUM;
135 case MODE_UACCUM: return MODE_VECTOR_UACCUM;
136 default:
137 error ("no vector class for class %s", mode_class_names[cl]);
138 return MODE_RANDOM;
142 /* Utility routines. */
143 static inline struct mode_data *
144 find_mode (const char *name)
146 struct mode_data key;
148 key.name = name;
149 return (struct mode_data *) htab_find (modes_by_name, &key);
152 static struct mode_data *
153 new_mode (enum mode_class cl, const char *name,
154 const char *file, unsigned int line)
156 struct mode_data *m;
157 static unsigned int count = 0;
159 m = find_mode (name);
160 if (m)
162 error ("%s:%d: duplicate definition of mode \"%s\"",
163 trim_filename (file), line, name);
164 error ("%s:%d: previous definition here", m->file, m->line);
165 return m;
168 m = XNEW (struct mode_data);
169 memcpy (m, &blank_mode, sizeof (struct mode_data));
170 m->cl = cl;
171 m->name = name;
172 if (file)
173 m->file = trim_filename (file);
174 m->line = line;
175 m->counter = count++;
177 m->next = modes[cl];
178 modes[cl] = m;
179 n_modes[cl]++;
181 *htab_find_slot (modes_by_name, m, INSERT) = m;
183 return m;
186 static hashval_t
187 hash_mode (const void *p)
189 const struct mode_data *m = (const struct mode_data *)p;
190 return htab_hash_string (m->name);
193 static int
194 eq_mode (const void *p, const void *q)
196 const struct mode_data *a = (const struct mode_data *)p;
197 const struct mode_data *b = (const struct mode_data *)q;
199 return !strcmp (a->name, b->name);
202 #define for_all_modes(C, M) \
203 for (C = 0; C < MAX_MODE_CLASS; C++) \
204 for (M = modes[C]; M; M = M->next)
206 static void ATTRIBUTE_UNUSED
207 new_adjust (const char *name,
208 struct mode_adjust **category, const char *catname,
209 const char *adjustment,
210 enum mode_class required_class_from,
211 enum mode_class required_class_to,
212 const char *file, unsigned int line)
214 struct mode_data *mode = find_mode (name);
215 struct mode_adjust *a;
217 file = trim_filename (file);
219 if (!mode)
221 error ("%s:%d: no mode \"%s\"", file, line, name);
222 return;
225 if (required_class_from != MODE_RANDOM
226 && (mode->cl < required_class_from || mode->cl > required_class_to))
228 error ("%s:%d: mode \"%s\" is not among class {%s, %s}",
229 file, line, name, mode_class_names[required_class_from] + 5,
230 mode_class_names[required_class_to] + 5);
231 return;
234 for (a = *category; a; a = a->next)
235 if (a->mode == mode)
237 error ("%s:%d: mode \"%s\" already has a %s adjustment",
238 file, line, name, catname);
239 error ("%s:%d: previous adjustment here", a->file, a->line);
240 return;
243 a = XNEW (struct mode_adjust);
244 a->mode = mode;
245 a->adjustment = adjustment;
246 a->file = file;
247 a->line = line;
249 a->next = *category;
250 *category = a;
253 /* Diagnose failure to meet expectations in a partially filled out
254 mode structure. */
255 enum requirement { SET, UNSET, OPTIONAL };
257 #define validate_field_(mname, fname, req, val, unset, file, line) do { \
258 switch (req) \
260 case SET: \
261 if (val == unset) \
262 error ("%s:%d: (%s) field %s must be set", \
263 file, line, mname, fname); \
264 break; \
265 case UNSET: \
266 if (val != unset) \
267 error ("%s:%d: (%s) field %s must not be set", \
268 file, line, mname, fname); \
269 case OPTIONAL: \
270 break; \
272 } while (0)
274 #define validate_field(M, F) \
275 validate_field_(M->name, #F, r_##F, M->F, blank_mode.F, M->file, M->line)
277 static void
278 validate_mode (struct mode_data *m,
279 enum requirement r_precision,
280 enum requirement r_bytesize,
281 enum requirement r_component,
282 enum requirement r_ncomponents,
283 enum requirement r_format)
285 validate_field (m, precision);
286 validate_field (m, bytesize);
287 validate_field (m, component);
288 validate_field (m, ncomponents);
289 validate_field (m, format);
291 #undef validate_field
292 #undef validate_field_
294 /* Given a partially-filled-out mode structure, figure out what we can
295 and fill the rest of it in; die if it isn't enough. */
296 static void
297 complete_mode (struct mode_data *m)
299 unsigned int alignment;
301 if (!m->name)
303 error ("%s:%d: mode with no name", m->file, m->line);
304 return;
306 if (m->cl == MAX_MODE_CLASS)
308 error ("%s:%d: %smode has no mode class", m->file, m->line, m->name);
309 return;
312 switch (m->cl)
314 case MODE_RANDOM:
315 /* Nothing more need be said. */
316 if (!strcmp (m->name, "VOID"))
317 void_mode = m;
319 validate_mode (m, UNSET, UNSET, UNSET, UNSET, UNSET);
321 m->precision = 0;
322 m->bytesize = 0;
323 m->ncomponents = 0;
324 m->component = 0;
325 break;
327 case MODE_CC:
328 /* Again, nothing more need be said. For historical reasons,
329 the size of a CC mode is four units. */
330 validate_mode (m, UNSET, UNSET, UNSET, UNSET, UNSET);
332 m->bytesize = 4;
333 m->ncomponents = 1;
334 m->component = 0;
335 break;
337 case MODE_INT:
338 case MODE_POINTER_BOUNDS:
339 case MODE_FLOAT:
340 case MODE_DECIMAL_FLOAT:
341 case MODE_FRACT:
342 case MODE_UFRACT:
343 case MODE_ACCUM:
344 case MODE_UACCUM:
345 /* A scalar mode must have a byte size, may have a bit size,
346 and must not have components. A float mode must have a
347 format. */
348 validate_mode (m, OPTIONAL, SET, UNSET, UNSET,
349 (m->cl == MODE_FLOAT || m->cl == MODE_DECIMAL_FLOAT)
350 ? SET : UNSET);
352 m->ncomponents = 1;
353 m->component = 0;
354 break;
356 case MODE_PARTIAL_INT:
357 /* A partial integer mode uses ->component to say what the
358 corresponding full-size integer mode is, and may also
359 specify a bit size. */
360 validate_mode (m, OPTIONAL, UNSET, SET, UNSET, UNSET);
362 m->bytesize = m->component->bytesize;
364 m->ncomponents = 1;
365 break;
367 case MODE_COMPLEX_INT:
368 case MODE_COMPLEX_FLOAT:
369 /* Complex modes should have a component indicated, but no more. */
370 validate_mode (m, UNSET, UNSET, SET, UNSET, UNSET);
371 m->ncomponents = 2;
372 if (m->component->precision != (unsigned int)-1)
373 m->precision = 2 * m->component->precision;
374 m->bytesize = 2 * m->component->bytesize;
375 break;
377 case MODE_VECTOR_INT:
378 case MODE_VECTOR_FLOAT:
379 case MODE_VECTOR_FRACT:
380 case MODE_VECTOR_UFRACT:
381 case MODE_VECTOR_ACCUM:
382 case MODE_VECTOR_UACCUM:
383 /* Vector modes should have a component and a number of components. */
384 validate_mode (m, UNSET, UNSET, SET, SET, UNSET);
385 if (m->component->precision != (unsigned int)-1)
386 m->precision = m->ncomponents * m->component->precision;
387 m->bytesize = m->ncomponents * m->component->bytesize;
388 break;
390 default:
391 gcc_unreachable ();
394 /* If not already specified, the mode alignment defaults to the largest
395 power of two that divides the size of the object. Complex types are
396 not more aligned than their contents. */
397 if (m->cl == MODE_COMPLEX_INT || m->cl == MODE_COMPLEX_FLOAT)
398 alignment = m->component->bytesize;
399 else
400 alignment = m->bytesize;
402 m->alignment = alignment & (~alignment + 1);
404 /* If this mode has components, make the component mode point back
405 to this mode, for the sake of adjustments. */
406 if (m->component)
408 m->next_cont = m->component->contained;
409 m->component->contained = m;
413 static void
414 complete_all_modes (void)
416 struct mode_data *m;
417 int cl;
419 for_all_modes (cl, m)
420 complete_mode (m);
423 /* For each mode in class CLASS, construct a corresponding complex mode. */
424 #define COMPLEX_MODES(C) make_complex_modes (MODE_##C, __FILE__, __LINE__)
425 static void
426 make_complex_modes (enum mode_class cl,
427 const char *file, unsigned int line)
429 struct mode_data *m;
430 struct mode_data *c;
431 enum mode_class cclass = complex_class (cl);
433 if (cclass == MODE_RANDOM)
434 return;
436 for (m = modes[cl]; m; m = m->next)
438 char *p, *buf;
439 size_t m_len;
441 /* Skip BImode. FIXME: BImode probably shouldn't be MODE_INT. */
442 if (m->precision == 1)
443 continue;
445 m_len = strlen (m->name);
446 /* The leading "1 +" is in case we prepend a "C" below. */
447 buf = (char *) xmalloc (1 + m_len + 1);
449 /* Float complex modes are named SCmode, etc.
450 Int complex modes are named CSImode, etc.
451 This inconsistency should be eliminated. */
452 p = 0;
453 if (cl == MODE_FLOAT)
455 memcpy (buf, m->name, m_len + 1);
456 p = strchr (buf, 'F');
457 if (p == 0 && strchr (buf, 'D') == 0)
459 error ("%s:%d: float mode \"%s\" has no 'F' or 'D'",
460 m->file, m->line, m->name);
461 free (buf);
462 continue;
465 if (p != 0)
466 *p = 'C';
467 else
469 buf[0] = 'C';
470 memcpy (buf + 1, m->name, m_len + 1);
473 c = new_mode (cclass, buf, file, line);
474 c->component = m;
478 /* For all modes in class CL, construct vector modes of width
479 WIDTH, having as many components as necessary. */
480 #define VECTOR_MODES(C, W) make_vector_modes (MODE_##C, W, __FILE__, __LINE__)
481 static void ATTRIBUTE_UNUSED
482 make_vector_modes (enum mode_class cl, unsigned int width,
483 const char *file, unsigned int line)
485 struct mode_data *m;
486 struct mode_data *v;
487 char buf[8];
488 unsigned int ncomponents;
489 enum mode_class vclass = vector_class (cl);
491 if (vclass == MODE_RANDOM)
492 return;
494 for (m = modes[cl]; m; m = m->next)
496 /* Do not construct vector modes with only one element, or
497 vector modes where the element size doesn't divide the full
498 size evenly. */
499 ncomponents = width / m->bytesize;
500 if (ncomponents < 2)
501 continue;
502 if (width % m->bytesize)
503 continue;
505 /* Skip QFmode and BImode. FIXME: this special case should
506 not be necessary. */
507 if (cl == MODE_FLOAT && m->bytesize == 1)
508 continue;
509 if (cl == MODE_INT && m->precision == 1)
510 continue;
512 if ((size_t)snprintf (buf, sizeof buf, "V%u%s", ncomponents, m->name)
513 >= sizeof buf)
515 error ("%s:%d: mode name \"%s\" is too long",
516 m->file, m->line, m->name);
517 continue;
520 v = new_mode (vclass, xstrdup (buf), file, line);
521 v->component = m;
522 v->ncomponents = ncomponents;
526 /* Input. */
528 #define _SPECIAL_MODE(C, N) \
529 make_special_mode (MODE_##C, #N, __FILE__, __LINE__)
530 #define RANDOM_MODE(N) _SPECIAL_MODE (RANDOM, N)
531 #define CC_MODE(N) _SPECIAL_MODE (CC, N)
533 static void
534 make_special_mode (enum mode_class cl, const char *name,
535 const char *file, unsigned int line)
537 new_mode (cl, name, file, line);
540 #define POINTER_BOUNDS_MODE(N, Y) \
541 make_pointer_bounds_mode (#N, Y, __FILE__, __LINE__)
543 static void ATTRIBUTE_UNUSED
544 make_pointer_bounds_mode (const char *name,
545 unsigned int bytesize,
546 const char *file, unsigned int line)
548 struct mode_data *m = new_mode (MODE_POINTER_BOUNDS, name, file, line);
549 m->bytesize = bytesize;
553 #define INT_MODE(N, Y) FRACTIONAL_INT_MODE (N, -1U, Y)
554 #define FRACTIONAL_INT_MODE(N, B, Y) \
555 make_int_mode (#N, B, Y, __FILE__, __LINE__)
557 static void
558 make_int_mode (const char *name,
559 unsigned int precision, unsigned int bytesize,
560 const char *file, unsigned int line)
562 struct mode_data *m = new_mode (MODE_INT, name, file, line);
563 m->bytesize = bytesize;
564 m->precision = precision;
567 #define FRACT_MODE(N, Y, F) \
568 make_fixed_point_mode (MODE_FRACT, #N, Y, 0, F, __FILE__, __LINE__)
570 #define UFRACT_MODE(N, Y, F) \
571 make_fixed_point_mode (MODE_UFRACT, #N, Y, 0, F, __FILE__, __LINE__)
573 #define ACCUM_MODE(N, Y, I, F) \
574 make_fixed_point_mode (MODE_ACCUM, #N, Y, I, F, __FILE__, __LINE__)
576 #define UACCUM_MODE(N, Y, I, F) \
577 make_fixed_point_mode (MODE_UACCUM, #N, Y, I, F, __FILE__, __LINE__)
579 /* Create a fixed-point mode by setting CL, NAME, BYTESIZE, IBIT, FBIT,
580 FILE, and LINE. */
582 static void
583 make_fixed_point_mode (enum mode_class cl,
584 const char *name,
585 unsigned int bytesize,
586 unsigned int ibit,
587 unsigned int fbit,
588 const char *file, unsigned int line)
590 struct mode_data *m = new_mode (cl, name, file, line);
591 m->bytesize = bytesize;
592 m->ibit = ibit;
593 m->fbit = fbit;
596 #define FLOAT_MODE(N, Y, F) FRACTIONAL_FLOAT_MODE (N, -1U, Y, F)
597 #define FRACTIONAL_FLOAT_MODE(N, B, Y, F) \
598 make_float_mode (#N, B, Y, #F, __FILE__, __LINE__)
600 static void
601 make_float_mode (const char *name,
602 unsigned int precision, unsigned int bytesize,
603 const char *format,
604 const char *file, unsigned int line)
606 struct mode_data *m = new_mode (MODE_FLOAT, name, file, line);
607 m->bytesize = bytesize;
608 m->precision = precision;
609 m->format = format;
612 #define DECIMAL_FLOAT_MODE(N, Y, F) \
613 FRACTIONAL_DECIMAL_FLOAT_MODE (N, -1U, Y, F)
614 #define FRACTIONAL_DECIMAL_FLOAT_MODE(N, B, Y, F) \
615 make_decimal_float_mode (#N, B, Y, #F, __FILE__, __LINE__)
617 static void
618 make_decimal_float_mode (const char *name,
619 unsigned int precision, unsigned int bytesize,
620 const char *format,
621 const char *file, unsigned int line)
623 struct mode_data *m = new_mode (MODE_DECIMAL_FLOAT, name, file, line);
624 m->bytesize = bytesize;
625 m->precision = precision;
626 m->format = format;
629 #define RESET_FLOAT_FORMAT(N, F) \
630 reset_float_format (#N, #F, __FILE__, __LINE__)
631 static void ATTRIBUTE_UNUSED
632 reset_float_format (const char *name, const char *format,
633 const char *file, unsigned int line)
635 struct mode_data *m = find_mode (name);
636 if (!m)
638 error ("%s:%d: no mode \"%s\"", file, line, name);
639 return;
641 if (m->cl != MODE_FLOAT && m->cl != MODE_DECIMAL_FLOAT)
643 error ("%s:%d: mode \"%s\" is not a FLOAT class", file, line, name);
644 return;
646 m->format = format;
649 /* __intN support. */
650 #define INT_N(M,PREC) \
651 make_int_n (#M, PREC, __FILE__, __LINE__)
652 static void ATTRIBUTE_UNUSED
653 make_int_n (const char *m, int bitsize,
654 const char *file, unsigned int line)
656 struct mode_data *component = find_mode (m);
657 if (!component)
659 error ("%s:%d: no mode \"%s\"", file, line, m);
660 return;
662 if (component->cl != MODE_INT
663 && component->cl != MODE_PARTIAL_INT)
665 error ("%s:%d: mode \"%s\" is not class INT or PARTIAL_INT", file, line, m);
666 return;
668 if (component->int_n != 0)
670 error ("%s:%d: mode \"%s\" already has an intN", file, line, m);
671 return;
674 component->int_n = bitsize;
677 /* Partial integer modes are specified by relation to a full integer
678 mode. */
679 #define PARTIAL_INT_MODE(M,PREC,NAME) \
680 make_partial_integer_mode (#M, #NAME, PREC, __FILE__, __LINE__)
681 static void ATTRIBUTE_UNUSED
682 make_partial_integer_mode (const char *base, const char *name,
683 unsigned int precision,
684 const char *file, unsigned int line)
686 struct mode_data *m;
687 struct mode_data *component = find_mode (base);
688 if (!component)
690 error ("%s:%d: no mode \"%s\"", file, line, name);
691 return;
693 if (component->cl != MODE_INT)
695 error ("%s:%d: mode \"%s\" is not class INT", file, line, name);
696 return;
699 m = new_mode (MODE_PARTIAL_INT, name, file, line);
700 m->precision = precision;
701 m->component = component;
704 /* A single vector mode can be specified by naming its component
705 mode and the number of components. */
706 #define VECTOR_MODE(C, M, N) \
707 make_vector_mode (MODE_##C, #M, N, __FILE__, __LINE__);
708 static void ATTRIBUTE_UNUSED
709 make_vector_mode (enum mode_class bclass,
710 const char *base,
711 unsigned int ncomponents,
712 const char *file, unsigned int line)
714 struct mode_data *v;
715 enum mode_class vclass = vector_class (bclass);
716 struct mode_data *component = find_mode (base);
717 char namebuf[16];
719 if (vclass == MODE_RANDOM)
720 return;
721 if (component == 0)
723 error ("%s:%d: no mode \"%s\"", file, line, base);
724 return;
726 if (component->cl != bclass
727 && (component->cl != MODE_PARTIAL_INT
728 || bclass != MODE_INT))
730 error ("%s:%d: mode \"%s\" is not class %s",
731 file, line, base, mode_class_names[bclass] + 5);
732 return;
735 if ((size_t)snprintf (namebuf, sizeof namebuf, "V%u%s",
736 ncomponents, base) >= sizeof namebuf)
738 error ("%s:%d: mode name \"%s\" is too long",
739 file, line, base);
740 return;
743 v = new_mode (vclass, xstrdup (namebuf), file, line);
744 v->ncomponents = ncomponents;
745 v->component = component;
748 /* Adjustability. */
749 #define _ADD_ADJUST(A, M, X, C1, C2) \
750 new_adjust (#M, &adj_##A, #A, #X, MODE_##C1, MODE_##C2, __FILE__, __LINE__)
752 #define ADJUST_BYTESIZE(M, X) _ADD_ADJUST (bytesize, M, X, RANDOM, RANDOM)
753 #define ADJUST_ALIGNMENT(M, X) _ADD_ADJUST (alignment, M, X, RANDOM, RANDOM)
754 #define ADJUST_FLOAT_FORMAT(M, X) _ADD_ADJUST (format, M, X, FLOAT, FLOAT)
755 #define ADJUST_IBIT(M, X) _ADD_ADJUST (ibit, M, X, ACCUM, UACCUM)
756 #define ADJUST_FBIT(M, X) _ADD_ADJUST (fbit, M, X, FRACT, UACCUM)
758 static int bits_per_unit;
759 static int max_bitsize_mode_any_int;
761 static void
762 create_modes (void)
764 #include "machmode.def"
766 /* So put the default value unless the target needs a non standard
767 value. */
768 #ifdef BITS_PER_UNIT
769 bits_per_unit = BITS_PER_UNIT;
770 #else
771 bits_per_unit = 8;
772 #endif
774 #ifdef MAX_BITSIZE_MODE_ANY_INT
775 max_bitsize_mode_any_int = MAX_BITSIZE_MODE_ANY_INT;
776 #else
777 max_bitsize_mode_any_int = 0;
778 #endif
781 /* Processing. */
783 /* Sort a list of modes into the order needed for the WIDER field:
784 major sort by precision, minor sort by component precision.
786 For instance:
787 QI < HI < SI < DI < TI
788 V4QI < V2HI < V8QI < V4HI < V2SI.
790 If the precision is not set, sort by the bytesize. A mode with
791 precision set gets sorted before a mode without precision set, if
792 they have the same bytesize; this is the right thing because
793 the precision must always be smaller than the bytesize * BITS_PER_UNIT.
794 We don't have to do anything special to get this done -- an unset
795 precision shows up as (unsigned int)-1, i.e. UINT_MAX. */
796 static int
797 cmp_modes (const void *a, const void *b)
799 const struct mode_data *const m = *(const struct mode_data *const*)a;
800 const struct mode_data *const n = *(const struct mode_data *const*)b;
802 if (m->bytesize > n->bytesize)
803 return 1;
804 else if (m->bytesize < n->bytesize)
805 return -1;
807 if (m->precision > n->precision)
808 return 1;
809 else if (m->precision < n->precision)
810 return -1;
812 if (!m->component && !n->component)
814 if (m->counter < n->counter)
815 return -1;
816 else
817 return 1;
820 if (m->component->bytesize > n->component->bytesize)
821 return 1;
822 else if (m->component->bytesize < n->component->bytesize)
823 return -1;
825 if (m->component->precision > n->component->precision)
826 return 1;
827 else if (m->component->precision < n->component->precision)
828 return -1;
830 if (m->counter < n->counter)
831 return -1;
832 else
833 return 1;
836 static void
837 calc_wider_mode (void)
839 int c;
840 struct mode_data *m;
841 struct mode_data **sortbuf;
842 unsigned int max_n_modes = 0;
843 unsigned int i, j;
845 for (c = 0; c < MAX_MODE_CLASS; c++)
846 max_n_modes = MAX (max_n_modes, n_modes[c]);
848 /* Allocate max_n_modes + 1 entries to leave room for the extra null
849 pointer assigned after the qsort call below. */
850 sortbuf = XALLOCAVEC (struct mode_data *, max_n_modes + 1);
852 for (c = 0; c < MAX_MODE_CLASS; c++)
854 /* "wider" is not meaningful for MODE_RANDOM and MODE_CC.
855 However, we want these in textual order, and we have
856 precisely the reverse. */
857 if (c == MODE_RANDOM || c == MODE_CC)
859 struct mode_data *prev, *next;
861 for (prev = 0, m = modes[c]; m; m = next)
863 m->wider = void_mode;
865 /* this is nreverse */
866 next = m->next;
867 m->next = prev;
868 prev = m;
870 modes[c] = prev;
872 else
874 if (!modes[c])
875 continue;
877 for (i = 0, m = modes[c]; m; i++, m = m->next)
878 sortbuf[i] = m;
880 qsort (sortbuf, i, sizeof (struct mode_data *), cmp_modes);
882 sortbuf[i] = 0;
883 for (j = 0; j < i; j++)
885 sortbuf[j]->next = sortbuf[j + 1];
886 if (c == MODE_PARTIAL_INT)
887 sortbuf[j]->wider = sortbuf[j]->component;
888 else
889 sortbuf[j]->wider = sortbuf[j]->next;
892 modes[c] = sortbuf[0];
897 /* Output routines. */
899 #define tagged_printf(FMT, ARG, TAG) do { \
900 int count_ = printf (" " FMT ",", ARG); \
901 printf ("%*s/* %s */\n", 27 - count_, "", TAG); \
902 } while (0)
904 #define print_decl(TYPE, NAME, ASIZE) \
905 puts ("\nconst " TYPE " " NAME "[" ASIZE "] =\n{");
907 #define print_maybe_const_decl(TYPE, NAME, ASIZE, CATEGORY) \
908 printf ("\n" TYPE " " NAME "[" ASIZE "] = \n{\n", \
909 adj_##CATEGORY ? "" : "const ")
911 #define print_closer() puts ("};")
913 /* Compute the max bitsize of some of the classes of integers. It may
914 be that there are needs for the other integer classes, and this
915 code is easy to extend. */
916 static void
917 emit_max_int (void)
919 unsigned int max, mmax;
920 struct mode_data *i;
921 int j;
923 puts ("");
925 printf ("#define BITS_PER_UNIT (%d)\n", bits_per_unit);
927 if (max_bitsize_mode_any_int == 0)
929 for (max = 1, i = modes[MODE_INT]; i; i = i->next)
930 if (max < i->bytesize)
931 max = i->bytesize;
932 mmax = max;
933 for (max = 1, i = modes[MODE_PARTIAL_INT]; i; i = i->next)
934 if (max < i->bytesize)
935 max = i->bytesize;
936 if (max > mmax)
937 mmax = max;
938 printf ("#define MAX_BITSIZE_MODE_ANY_INT (%d*BITS_PER_UNIT)\n", mmax);
940 else
941 printf ("#define MAX_BITSIZE_MODE_ANY_INT %d\n", max_bitsize_mode_any_int);
943 mmax = 0;
944 for (j = 0; j < MAX_MODE_CLASS; j++)
945 for (i = modes[j]; i; i = i->next)
946 if (mmax < i->bytesize)
947 mmax = i->bytesize;
948 printf ("#define MAX_BITSIZE_MODE_ANY_MODE (%d*BITS_PER_UNIT)\n", mmax);
951 /* Emit mode_size_inline routine into insn-modes.h header. */
952 static void
953 emit_mode_size_inline (void)
955 int c;
956 struct mode_adjust *a;
957 struct mode_data *m;
959 /* Size adjustments must be propagated to all containing modes. */
960 for (a = adj_bytesize; a; a = a->next)
962 a->mode->need_bytesize_adj = true;
963 for (m = a->mode->contained; m; m = m->next_cont)
964 m->need_bytesize_adj = true;
967 printf ("\
968 #ifdef __cplusplus\n\
969 inline __attribute__((__always_inline__))\n\
970 #else\n\
971 extern __inline__ __attribute__((__always_inline__, __gnu_inline__))\n\
972 #endif\n\
973 unsigned char\n\
974 mode_size_inline (machine_mode mode)\n\
975 {\n\
976 extern %sunsigned char mode_size[NUM_MACHINE_MODES];\n\
977 switch (mode)\n\
978 {\n", adj_bytesize ? "" : "const ");
980 for_all_modes (c, m)
981 if (!m->need_bytesize_adj)
982 printf (" case %smode: return %u;\n", m->name, m->bytesize);
984 puts ("\
985 default: return mode_size[mode];\n\
986 }\n\
987 }\n");
990 /* Emit mode_nunits_inline routine into insn-modes.h header. */
991 static void
992 emit_mode_nunits_inline (void)
994 int c;
995 struct mode_data *m;
997 puts ("\
998 #ifdef __cplusplus\n\
999 inline __attribute__((__always_inline__))\n\
1000 #else\n\
1001 extern __inline__ __attribute__((__always_inline__, __gnu_inline__))\n\
1002 #endif\n\
1003 unsigned char\n\
1004 mode_nunits_inline (machine_mode mode)\n\
1005 {\n\
1006 extern const unsigned char mode_nunits[NUM_MACHINE_MODES];\n\
1007 switch (mode)\n\
1008 {");
1010 for_all_modes (c, m)
1011 printf (" case %smode: return %u;\n", m->name, m->ncomponents);
1013 puts ("\
1014 default: return mode_nunits[mode];\n\
1015 }\n\
1016 }\n");
1019 /* Emit mode_inner_inline routine into insn-modes.h header. */
1020 static void
1021 emit_mode_inner_inline (void)
1023 int c;
1024 struct mode_data *m;
1026 puts ("\
1027 #ifdef __cplusplus\n\
1028 inline __attribute__((__always_inline__))\n\
1029 #else\n\
1030 extern __inline__ __attribute__((__always_inline__, __gnu_inline__))\n\
1031 #endif\n\
1032 unsigned char\n\
1033 mode_inner_inline (machine_mode mode)\n\
1034 {\n\
1035 extern const unsigned char mode_inner[NUM_MACHINE_MODES];\n\
1036 switch (mode)\n\
1037 {");
1039 for_all_modes (c, m)
1040 printf (" case %smode: return %smode;\n", m->name,
1041 c != MODE_PARTIAL_INT && m->component
1042 ? m->component->name : m->name);
1044 puts ("\
1045 default: return mode_inner[mode];\n\
1046 }\n\
1047 }\n");
1050 /* Emit mode_unit_size_inline routine into insn-modes.h header. */
1051 static void
1052 emit_mode_unit_size_inline (void)
1054 int c;
1055 struct mode_data *m;
1057 puts ("\
1058 #ifdef __cplusplus\n\
1059 inline __attribute__((__always_inline__))\n\
1060 #else\n\
1061 extern __inline__ __attribute__((__always_inline__, __gnu_inline__))\n\
1062 #endif\n\
1063 unsigned char\n\
1064 mode_unit_size_inline (machine_mode mode)\n\
1065 {\n\
1066 extern CONST_MODE_UNIT_SIZE unsigned char mode_unit_size[NUM_MACHINE_MODES];\
1068 switch (mode)\n\
1069 {");
1071 for_all_modes (c, m)
1073 const char *name = m->name;
1074 struct mode_data *m2 = m;
1075 if (c != MODE_PARTIAL_INT && m2->component)
1076 m2 = m2->component;
1077 if (!m2->need_bytesize_adj)
1078 printf (" case %smode: return %u;\n", name, m2->bytesize);
1081 puts ("\
1082 default: return mode_unit_size[mode];\n\
1083 }\n\
1084 }\n");
1087 /* Emit mode_unit_precision_inline routine into insn-modes.h header. */
1088 static void
1089 emit_mode_unit_precision_inline (void)
1091 int c;
1092 struct mode_data *m;
1094 puts ("\
1095 #ifdef __cplusplus\n\
1096 inline __attribute__((__always_inline__))\n\
1097 #else\n\
1098 extern __inline__ __attribute__((__always_inline__, __gnu_inline__))\n\
1099 #endif\n\
1100 unsigned short\n\
1101 mode_unit_precision_inline (machine_mode mode)\n\
1102 {\n\
1103 extern const unsigned short mode_unit_precision[NUM_MACHINE_MODES];\n\
1104 switch (mode)\n\
1105 {");
1107 for_all_modes (c, m)
1109 struct mode_data *m2
1110 = (c != MODE_PARTIAL_INT && m->component) ? m->component : m;
1111 if (m2->precision != (unsigned int)-1)
1112 printf (" case %smode: return %u;\n", m->name, m2->precision);
1113 else
1114 printf (" case %smode: return %u*BITS_PER_UNIT;\n",
1115 m->name, m2->bytesize);
1118 puts ("\
1119 default: return mode_unit_precision[mode];\n\
1120 }\n\
1121 }\n");
1124 static void
1125 emit_insn_modes_h (void)
1127 int c;
1128 struct mode_data *m, *first, *last;
1129 int n_int_n_ents = 0;
1131 printf ("/* Generated automatically from machmode.def%s%s\n",
1132 HAVE_EXTRA_MODES ? " and " : "",
1133 EXTRA_MODES_FILE);
1135 puts ("\
1136 by genmodes. */\n\
1138 #ifndef GCC_INSN_MODES_H\n\
1139 #define GCC_INSN_MODES_H\n\
1141 enum machine_mode\n{");
1143 for (c = 0; c < MAX_MODE_CLASS; c++)
1144 for (m = modes[c]; m; m = m->next)
1146 int count_ = printf (" %smode,", m->name);
1147 printf ("%*s/* %s:%d */\n", 27 - count_, "",
1148 trim_filename (m->file), m->line);
1149 printf ("#define HAVE_%smode\n", m->name);
1152 puts (" MAX_MACHINE_MODE,\n");
1154 for (c = 0; c < MAX_MODE_CLASS; c++)
1156 first = modes[c];
1157 last = 0;
1158 for (m = first; m; last = m, m = m->next)
1161 /* Don't use BImode for MIN_MODE_INT, since otherwise the middle
1162 end will try to use it for bitfields in structures and the
1163 like, which we do not want. Only the target md file should
1164 generate BImode widgets. */
1165 if (first && first->precision == 1 && c == MODE_INT)
1166 first = first->next;
1168 if (first && last)
1169 printf (" MIN_%s = %smode,\n MAX_%s = %smode,\n\n",
1170 mode_class_names[c], first->name,
1171 mode_class_names[c], last->name);
1172 else
1173 printf (" MIN_%s = %smode,\n MAX_%s = %smode,\n\n",
1174 mode_class_names[c], void_mode->name,
1175 mode_class_names[c], void_mode->name);
1178 puts ("\
1179 NUM_MACHINE_MODES = MAX_MACHINE_MODE\n\
1180 };\n");
1182 /* I can't think of a better idea, can you? */
1183 printf ("#define CONST_MODE_SIZE%s\n", adj_bytesize ? "" : " const");
1184 printf ("#define CONST_MODE_UNIT_SIZE%s\n", adj_bytesize ? "" : " const");
1185 printf ("#define CONST_MODE_BASE_ALIGN%s\n", adj_alignment ? "" : " const");
1186 #if 0 /* disabled for backward compatibility, temporary */
1187 printf ("#define CONST_REAL_FORMAT_FOR_MODE%s\n", adj_format ? "" :" const");
1188 #endif
1189 printf ("#define CONST_MODE_IBIT%s\n", adj_ibit ? "" : " const");
1190 printf ("#define CONST_MODE_FBIT%s\n", adj_fbit ? "" : " const");
1191 emit_max_int ();
1193 for_all_modes (c, m)
1194 if (m->int_n)
1195 n_int_n_ents ++;
1197 printf ("#define NUM_INT_N_ENTS %d\n", n_int_n_ents);
1199 puts ("\n#if !defined (USED_FOR_TARGET) && GCC_VERSION >= 4001\n");
1200 emit_mode_size_inline ();
1201 emit_mode_nunits_inline ();
1202 emit_mode_inner_inline ();
1203 emit_mode_unit_size_inline ();
1204 emit_mode_unit_precision_inline ();
1205 puts ("#endif /* GCC_VERSION >= 4001 */");
1207 puts ("\
1209 #endif /* insn-modes.h */");
1212 static void
1213 emit_insn_modes_c_header (void)
1215 printf ("/* Generated automatically from machmode.def%s%s\n",
1216 HAVE_EXTRA_MODES ? " and " : "",
1217 EXTRA_MODES_FILE);
1219 puts ("\
1220 by genmodes. */\n\
1222 #include \"config.h\"\n\
1223 #include \"system.h\"\n\
1224 #include \"coretypes.h\"\n\
1225 #include \"tm.h\"\n\
1226 #include \"machmode.h\"\n\
1227 #include \"real.h\"");
1230 static void
1231 emit_min_insn_modes_c_header (void)
1233 printf ("/* Generated automatically from machmode.def%s%s\n",
1234 HAVE_EXTRA_MODES ? " and " : "",
1235 EXTRA_MODES_FILE);
1237 puts ("\
1238 by genmodes. */\n\
1240 #include \"bconfig.h\"\n\
1241 #include \"system.h\"\n\
1242 #include \"machmode.h\"");
1245 static void
1246 emit_mode_name (void)
1248 int c;
1249 struct mode_data *m;
1251 print_decl ("char *const", "mode_name", "NUM_MACHINE_MODES");
1253 for_all_modes (c, m)
1254 printf (" \"%s\",\n", m->name);
1256 print_closer ();
1259 static void
1260 emit_mode_class (void)
1262 int c;
1263 struct mode_data *m;
1265 print_decl ("unsigned char", "mode_class", "NUM_MACHINE_MODES");
1267 for_all_modes (c, m)
1268 tagged_printf ("%s", mode_class_names[m->cl], m->name);
1270 print_closer ();
1273 static void
1274 emit_mode_precision (void)
1276 int c;
1277 struct mode_data *m;
1279 print_decl ("unsigned short", "mode_precision", "NUM_MACHINE_MODES");
1281 for_all_modes (c, m)
1282 if (m->precision != (unsigned int)-1)
1283 tagged_printf ("%u", m->precision, m->name);
1284 else
1285 tagged_printf ("%u*BITS_PER_UNIT", m->bytesize, m->name);
1287 print_closer ();
1290 static void
1291 emit_mode_size (void)
1293 int c;
1294 struct mode_data *m;
1296 print_maybe_const_decl ("%sunsigned char", "mode_size",
1297 "NUM_MACHINE_MODES", bytesize);
1299 for_all_modes (c, m)
1300 tagged_printf ("%u", m->bytesize, m->name);
1302 print_closer ();
1305 static void
1306 emit_mode_nunits (void)
1308 int c;
1309 struct mode_data *m;
1311 print_decl ("unsigned char", "mode_nunits", "NUM_MACHINE_MODES");
1313 for_all_modes (c, m)
1314 tagged_printf ("%u", m->ncomponents, m->name);
1316 print_closer ();
1319 static void
1320 emit_mode_wider (void)
1322 int c;
1323 struct mode_data *m;
1325 print_decl ("unsigned char", "mode_wider", "NUM_MACHINE_MODES");
1327 for_all_modes (c, m)
1328 tagged_printf ("%smode",
1329 m->wider ? m->wider->name : void_mode->name,
1330 m->name);
1332 print_closer ();
1333 print_decl ("unsigned char", "mode_2xwider", "NUM_MACHINE_MODES");
1335 for_all_modes (c, m)
1337 struct mode_data * m2;
1339 for (m2 = m;
1340 m2 && m2 != void_mode;
1341 m2 = m2->wider)
1343 if (m2->bytesize < 2 * m->bytesize)
1344 continue;
1345 if (m->precision != (unsigned int) -1)
1347 if (m2->precision != 2 * m->precision)
1348 continue;
1350 else
1352 if (m2->precision != (unsigned int) -1)
1353 continue;
1356 /* For vectors we want twice the number of components,
1357 with the same element type. */
1358 if (m->cl == MODE_VECTOR_INT
1359 || m->cl == MODE_VECTOR_FLOAT
1360 || m->cl == MODE_VECTOR_FRACT
1361 || m->cl == MODE_VECTOR_UFRACT
1362 || m->cl == MODE_VECTOR_ACCUM
1363 || m->cl == MODE_VECTOR_UACCUM)
1365 if (m2->ncomponents != 2 * m->ncomponents)
1366 continue;
1367 if (m->component != m2->component)
1368 continue;
1371 break;
1373 if (m2 == void_mode)
1374 m2 = 0;
1375 tagged_printf ("%smode",
1376 m2 ? m2->name : void_mode->name,
1377 m->name);
1380 print_closer ();
1383 static void
1384 emit_mode_mask (void)
1386 int c;
1387 struct mode_data *m;
1389 print_decl ("unsigned HOST_WIDE_INT", "mode_mask_array",
1390 "NUM_MACHINE_MODES");
1391 puts ("\
1392 #define MODE_MASK(m) \\\n\
1393 ((m) >= HOST_BITS_PER_WIDE_INT) \\\n\
1394 ? ~(unsigned HOST_WIDE_INT) 0 \\\n\
1395 : ((unsigned HOST_WIDE_INT) 1 << (m)) - 1\n");
1397 for_all_modes (c, m)
1398 if (m->precision != (unsigned int)-1)
1399 tagged_printf ("MODE_MASK (%u)", m->precision, m->name);
1400 else
1401 tagged_printf ("MODE_MASK (%u*BITS_PER_UNIT)", m->bytesize, m->name);
1403 puts ("#undef MODE_MASK");
1404 print_closer ();
1407 static void
1408 emit_mode_inner (void)
1410 int c;
1411 struct mode_data *m;
1413 print_decl ("unsigned char", "mode_inner", "NUM_MACHINE_MODES");
1415 for_all_modes (c, m)
1416 tagged_printf ("%smode",
1417 c != MODE_PARTIAL_INT && m->component
1418 ? m->component->name : m->name,
1419 m->name);
1421 print_closer ();
1424 /* Emit mode_unit_size array into insn-modes.c file. */
1425 static void
1426 emit_mode_unit_size (void)
1428 int c;
1429 struct mode_data *m;
1431 print_maybe_const_decl ("%sunsigned char", "mode_unit_size",
1432 "NUM_MACHINE_MODES", bytesize);
1434 for_all_modes (c, m)
1435 tagged_printf ("%u",
1436 c != MODE_PARTIAL_INT && m->component
1437 ? m->component->bytesize : m->bytesize, m->name);
1439 print_closer ();
1442 /* Emit mode_unit_precision array into insn-modes.c file. */
1443 static void
1444 emit_mode_unit_precision (void)
1446 int c;
1447 struct mode_data *m;
1449 print_decl ("unsigned short", "mode_unit_precision", "NUM_MACHINE_MODES");
1451 for_all_modes (c, m)
1453 struct mode_data *m2 = (c != MODE_PARTIAL_INT && m->component) ?
1454 m->component : m;
1455 if (m2->precision != (unsigned int)-1)
1456 tagged_printf ("%u", m2->precision, m->name);
1457 else
1458 tagged_printf ("%u*BITS_PER_UNIT", m2->bytesize, m->name);
1461 print_closer ();
1465 static void
1466 emit_mode_base_align (void)
1468 int c;
1469 struct mode_data *m;
1471 print_maybe_const_decl ("%sunsigned char",
1472 "mode_base_align", "NUM_MACHINE_MODES",
1473 alignment);
1475 for_all_modes (c, m)
1476 tagged_printf ("%u", m->alignment, m->name);
1478 print_closer ();
1481 static void
1482 emit_class_narrowest_mode (void)
1484 int c;
1486 print_decl ("unsigned char", "class_narrowest_mode", "MAX_MODE_CLASS");
1488 for (c = 0; c < MAX_MODE_CLASS; c++)
1489 /* Bleah, all this to get the comment right for MIN_MODE_INT. */
1490 tagged_printf ("MIN_%s", mode_class_names[c],
1491 modes[c]
1492 ? ((c != MODE_INT || modes[c]->precision != 1)
1493 ? modes[c]->name
1494 : (modes[c]->next
1495 ? modes[c]->next->name
1496 : void_mode->name))
1497 : void_mode->name);
1499 print_closer ();
1502 static void
1503 emit_real_format_for_mode (void)
1505 struct mode_data *m;
1507 /* The entities pointed to by this table are constant, whether
1508 or not the table itself is constant.
1510 For backward compatibility this table is always writable
1511 (several targets modify it in TARGET_OPTION_OVERRIDE). FIXME:
1512 convert all said targets to use ADJUST_FORMAT instead. */
1513 #if 0
1514 print_maybe_const_decl ("const struct real_format *%s",
1515 "real_format_for_mode",
1516 "MAX_MODE_FLOAT - MIN_MODE_FLOAT + 1",
1517 format);
1518 #else
1519 print_decl ("struct real_format *\n", "real_format_for_mode",
1520 "MAX_MODE_FLOAT - MIN_MODE_FLOAT + 1 "
1521 "+ MAX_MODE_DECIMAL_FLOAT - MIN_MODE_DECIMAL_FLOAT + 1");
1522 #endif
1524 /* The beginning of the table is entries for float modes. */
1525 for (m = modes[MODE_FLOAT]; m; m = m->next)
1526 if (!strcmp (m->format, "0"))
1527 tagged_printf ("%s", m->format, m->name);
1528 else
1529 tagged_printf ("&%s", m->format, m->name);
1531 /* The end of the table is entries for decimal float modes. */
1532 for (m = modes[MODE_DECIMAL_FLOAT]; m; m = m->next)
1533 if (!strcmp (m->format, "0"))
1534 tagged_printf ("%s", m->format, m->name);
1535 else
1536 tagged_printf ("&%s", m->format, m->name);
1538 print_closer ();
1541 static void
1542 emit_mode_adjustments (void)
1544 struct mode_adjust *a;
1545 struct mode_data *m;
1547 puts ("\
1548 \nvoid\
1549 \ninit_adjust_machine_modes (void)\
1550 \n{\
1551 \n size_t s ATTRIBUTE_UNUSED;");
1553 /* Size adjustments must be propagated to all containing modes.
1554 A size adjustment forces us to recalculate the alignment too. */
1555 for (a = adj_bytesize; a; a = a->next)
1557 printf ("\n /* %s:%d */\n s = %s;\n",
1558 a->file, a->line, a->adjustment);
1559 printf (" mode_size[%smode] = s;\n", a->mode->name);
1560 printf (" mode_unit_size[%smode] = s;\n", a->mode->name);
1561 printf (" mode_base_align[%smode] = s & (~s + 1);\n",
1562 a->mode->name);
1564 for (m = a->mode->contained; m; m = m->next_cont)
1566 switch (m->cl)
1568 case MODE_COMPLEX_INT:
1569 case MODE_COMPLEX_FLOAT:
1570 printf (" mode_size[%smode] = 2*s;\n", m->name);
1571 printf (" mode_unit_size[%smode] = s;\n", m->name);
1572 printf (" mode_base_align[%smode] = s & (~s + 1);\n",
1573 m->name);
1574 break;
1576 case MODE_VECTOR_INT:
1577 case MODE_VECTOR_FLOAT:
1578 case MODE_VECTOR_FRACT:
1579 case MODE_VECTOR_UFRACT:
1580 case MODE_VECTOR_ACCUM:
1581 case MODE_VECTOR_UACCUM:
1582 printf (" mode_size[%smode] = %d*s;\n",
1583 m->name, m->ncomponents);
1584 printf (" mode_unit_size[%smode] = s;\n", m->name);
1585 printf (" mode_base_align[%smode] = (%d*s) & (~(%d*s)+1);\n",
1586 m->name, m->ncomponents, m->ncomponents);
1587 break;
1589 default:
1590 internal_error (
1591 "mode %s is neither vector nor complex but contains %s",
1592 m->name, a->mode->name);
1593 /* NOTREACHED */
1598 /* Alignment adjustments propagate too.
1599 ??? This may not be the right thing for vector modes. */
1600 for (a = adj_alignment; a; a = a->next)
1602 printf ("\n /* %s:%d */\n s = %s;\n",
1603 a->file, a->line, a->adjustment);
1604 printf (" mode_base_align[%smode] = s;\n", a->mode->name);
1606 for (m = a->mode->contained; m; m = m->next_cont)
1608 switch (m->cl)
1610 case MODE_COMPLEX_INT:
1611 case MODE_COMPLEX_FLOAT:
1612 printf (" mode_base_align[%smode] = s;\n", m->name);
1613 break;
1615 case MODE_VECTOR_INT:
1616 case MODE_VECTOR_FLOAT:
1617 case MODE_VECTOR_FRACT:
1618 case MODE_VECTOR_UFRACT:
1619 case MODE_VECTOR_ACCUM:
1620 case MODE_VECTOR_UACCUM:
1621 printf (" mode_base_align[%smode] = %d*s;\n",
1622 m->name, m->ncomponents);
1623 break;
1625 default:
1626 internal_error (
1627 "mode %s is neither vector nor complex but contains %s",
1628 m->name, a->mode->name);
1629 /* NOTREACHED */
1634 /* Ibit adjustments don't have to propagate. */
1635 for (a = adj_ibit; a; a = a->next)
1637 printf ("\n /* %s:%d */\n s = %s;\n",
1638 a->file, a->line, a->adjustment);
1639 printf (" mode_ibit[%smode] = s;\n", a->mode->name);
1642 /* Fbit adjustments don't have to propagate. */
1643 for (a = adj_fbit; a; a = a->next)
1645 printf ("\n /* %s:%d */\n s = %s;\n",
1646 a->file, a->line, a->adjustment);
1647 printf (" mode_fbit[%smode] = s;\n", a->mode->name);
1650 /* Real mode formats don't have to propagate anywhere. */
1651 for (a = adj_format; a; a = a->next)
1652 printf ("\n /* %s:%d */\n REAL_MODE_FORMAT (%smode) = %s;\n",
1653 a->file, a->line, a->mode->name, a->adjustment);
1655 puts ("}");
1658 /* Emit ibit for all modes. */
1660 static void
1661 emit_mode_ibit (void)
1663 int c;
1664 struct mode_data *m;
1666 print_maybe_const_decl ("%sunsigned char",
1667 "mode_ibit", "NUM_MACHINE_MODES",
1668 ibit);
1670 for_all_modes (c, m)
1671 tagged_printf ("%u", m->ibit, m->name);
1673 print_closer ();
1676 /* Emit fbit for all modes. */
1678 static void
1679 emit_mode_fbit (void)
1681 int c;
1682 struct mode_data *m;
1684 print_maybe_const_decl ("%sunsigned char",
1685 "mode_fbit", "NUM_MACHINE_MODES",
1686 fbit);
1688 for_all_modes (c, m)
1689 tagged_printf ("%u", m->fbit, m->name);
1691 print_closer ();
1694 /* Emit __intN for all modes. */
1696 static void
1697 emit_mode_int_n (void)
1699 int c;
1700 struct mode_data *m;
1701 struct mode_data **mode_sort;
1702 int n_modes = 0;
1703 int i, j;
1705 print_decl ("int_n_data_t", "int_n_data", "");
1707 n_modes = 0;
1708 for_all_modes (c, m)
1709 if (m->int_n)
1710 n_modes ++;
1711 mode_sort = XALLOCAVEC (struct mode_data *, n_modes);
1713 n_modes = 0;
1714 for_all_modes (c, m)
1715 if (m->int_n)
1716 mode_sort[n_modes++] = m;
1718 /* Yes, this is a bubblesort, but there are at most four (and
1719 usually only 1-2) entries to sort. */
1720 for (i = 0; i<n_modes - 1; i++)
1721 for (j = i + 1; j < n_modes; j++)
1722 if (mode_sort[i]->int_n > mode_sort[j]->int_n)
1723 std::swap (mode_sort[i], mode_sort[j]);
1725 for (i = 0; i < n_modes; i ++)
1727 m = mode_sort[i];
1728 printf(" {\n");
1729 tagged_printf ("%u", m->int_n, m->name);
1730 printf ("%smode,", m->name);
1731 printf(" },\n");
1734 print_closer ();
1738 static void
1739 emit_insn_modes_c (void)
1741 emit_insn_modes_c_header ();
1742 emit_mode_name ();
1743 emit_mode_class ();
1744 emit_mode_precision ();
1745 emit_mode_size ();
1746 emit_mode_nunits ();
1747 emit_mode_wider ();
1748 emit_mode_mask ();
1749 emit_mode_inner ();
1750 emit_mode_unit_size ();
1751 emit_mode_unit_precision ();
1752 emit_mode_base_align ();
1753 emit_class_narrowest_mode ();
1754 emit_real_format_for_mode ();
1755 emit_mode_adjustments ();
1756 emit_mode_ibit ();
1757 emit_mode_fbit ();
1758 emit_mode_int_n ();
1761 static void
1762 emit_min_insn_modes_c (void)
1764 emit_min_insn_modes_c_header ();
1765 emit_mode_name ();
1766 emit_mode_class ();
1767 emit_mode_wider ();
1768 emit_class_narrowest_mode ();
1771 /* Master control. */
1773 main (int argc, char **argv)
1775 bool gen_header = false, gen_min = false;
1776 progname = argv[0];
1778 if (argc == 1)
1780 else if (argc == 2 && !strcmp (argv[1], "-h"))
1781 gen_header = true;
1782 else if (argc == 2 && !strcmp (argv[1], "-m"))
1783 gen_min = true;
1784 else
1786 error ("usage: %s [-h|-m] > file", progname);
1787 return FATAL_EXIT_CODE;
1790 modes_by_name = htab_create_alloc (64, hash_mode, eq_mode, 0, xcalloc, free);
1792 create_modes ();
1793 complete_all_modes ();
1795 if (have_error)
1796 return FATAL_EXIT_CODE;
1798 calc_wider_mode ();
1800 if (gen_header)
1801 emit_insn_modes_h ();
1802 else if (gen_min)
1803 emit_min_insn_modes_c ();
1804 else
1805 emit_insn_modes_c ();
1807 if (fflush (stdout) || fclose (stdout))
1808 return FATAL_EXIT_CODE;
1809 return SUCCESS_EXIT_CODE;