* configure: Regenerated.
[official-gcc.git] / gcc / genmodes.c
blobd0095c3b0b136a3f165c9ffbd2afcd69896d2a8f
1 /* Generate the machine mode enumeration and associated tables.
2 Copyright (C) 2003, 2004, 2005, 2006, 2007, 2010, 2012
3 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "bconfig.h"
22 #include "system.h"
23 #include "errors.h"
24 #include "hashtab.h"
26 /* enum mode_class is normally defined by machmode.h but we can't
27 include that header here. */
28 #include "mode-classes.def"
30 #define DEF_MODE_CLASS(M) M
31 enum mode_class { MODE_CLASSES, MAX_MODE_CLASS };
32 #undef DEF_MODE_CLASS
34 /* Text names of mode classes, for output. */
35 #define DEF_MODE_CLASS(M) #M
36 static const char *const mode_class_names[MAX_MODE_CLASS] =
38 MODE_CLASSES
40 #undef DEF_MODE_CLASS
41 #undef MODE_CLASSES
43 #ifdef EXTRA_MODES_FILE
44 # define HAVE_EXTRA_MODES 1
45 #else
46 # define HAVE_EXTRA_MODES 0
47 # define EXTRA_MODES_FILE ""
48 #endif
50 /* Data structure for building up what we know about a mode.
51 They're clustered by mode class. */
52 struct mode_data
54 struct mode_data *next; /* next this class - arbitrary order */
56 const char *name; /* printable mode name -- SI, not SImode */
57 enum mode_class cl; /* this mode class */
58 unsigned int precision; /* size in bits, equiv to TYPE_PRECISION */
59 unsigned int bytesize; /* storage size in addressable units */
60 unsigned int ncomponents; /* number of subunits */
61 unsigned int alignment; /* mode alignment */
62 const char *format; /* floating point format - float modes only */
64 struct mode_data *component; /* mode of components */
65 struct mode_data *wider; /* next wider mode */
67 struct mode_data *contained; /* Pointer to list of modes that have
68 this mode as a component. */
69 struct mode_data *next_cont; /* Next mode in that list. */
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 */
78 static struct mode_data *modes[MAX_MODE_CLASS];
79 static unsigned int n_modes[MAX_MODE_CLASS];
80 static struct mode_data *void_mode;
82 static const struct mode_data blank_mode = {
83 0, "<unknown>", MAX_MODE_CLASS,
84 -1U, -1U, -1U, -1U,
85 0, 0, 0, 0, 0,
86 "<unknown>", 0, 0, 0, 0
89 static htab_t modes_by_name;
91 /* Data structure for recording target-specified runtime adjustments
92 to a particular mode. We support varying the byte size, the
93 alignment, and the floating point format. */
94 struct mode_adjust
96 struct mode_adjust *next;
97 struct mode_data *mode;
98 const char *adjustment;
100 const char *file;
101 unsigned int line;
104 static struct mode_adjust *adj_bytesize;
105 static struct mode_adjust *adj_alignment;
106 static struct mode_adjust *adj_format;
107 static struct mode_adjust *adj_ibit;
108 static struct mode_adjust *adj_fbit;
110 /* Mode class operations. */
111 static enum mode_class
112 complex_class (enum mode_class c)
114 switch (c)
116 case MODE_INT: return MODE_COMPLEX_INT;
117 case MODE_FLOAT: return MODE_COMPLEX_FLOAT;
118 default:
119 error ("no complex class for class %s", mode_class_names[c]);
120 return MODE_RANDOM;
124 static enum mode_class
125 vector_class (enum mode_class cl)
127 switch (cl)
129 case MODE_INT: return MODE_VECTOR_INT;
130 case MODE_FLOAT: return MODE_VECTOR_FLOAT;
131 case MODE_FRACT: return MODE_VECTOR_FRACT;
132 case MODE_UFRACT: return MODE_VECTOR_UFRACT;
133 case MODE_ACCUM: return MODE_VECTOR_ACCUM;
134 case MODE_UACCUM: return MODE_VECTOR_UACCUM;
135 default:
136 error ("no vector class for class %s", mode_class_names[cl]);
137 return MODE_RANDOM;
141 /* Utility routines. */
142 static inline struct mode_data *
143 find_mode (const char *name)
145 struct mode_data key;
147 key.name = name;
148 return (struct mode_data *) htab_find (modes_by_name, &key);
151 static struct mode_data *
152 new_mode (enum mode_class cl, const char *name,
153 const char *file, unsigned int line)
155 struct mode_data *m;
156 static unsigned int count = 0;
158 m = find_mode (name);
159 if (m)
161 error ("%s:%d: duplicate definition of mode \"%s\"",
162 trim_filename (file), line, name);
163 error ("%s:%d: previous definition here", m->file, m->line);
164 return m;
167 m = XNEW (struct mode_data);
168 memcpy (m, &blank_mode, sizeof (struct mode_data));
169 m->cl = cl;
170 m->name = name;
171 if (file)
172 m->file = trim_filename (file);
173 m->line = line;
174 m->counter = count++;
176 m->next = modes[cl];
177 modes[cl] = m;
178 n_modes[cl]++;
180 *htab_find_slot (modes_by_name, m, INSERT) = m;
182 return m;
185 static hashval_t
186 hash_mode (const void *p)
188 const struct mode_data *m = (const struct mode_data *)p;
189 return htab_hash_string (m->name);
192 static int
193 eq_mode (const void *p, const void *q)
195 const struct mode_data *a = (const struct mode_data *)p;
196 const struct mode_data *b = (const struct mode_data *)q;
198 return !strcmp (a->name, b->name);
201 #define for_all_modes(C, M) \
202 for (C = 0; C < MAX_MODE_CLASS; C++) \
203 for (M = modes[C]; M; M = M->next)
205 static void ATTRIBUTE_UNUSED
206 new_adjust (const char *name,
207 struct mode_adjust **category, const char *catname,
208 const char *adjustment,
209 enum mode_class required_class_from,
210 enum mode_class required_class_to,
211 const char *file, unsigned int line)
213 struct mode_data *mode = find_mode (name);
214 struct mode_adjust *a;
216 file = trim_filename (file);
218 if (!mode)
220 error ("%s:%d: no mode \"%s\"", file, line, name);
221 return;
224 if (required_class_from != MODE_RANDOM
225 && (mode->cl < required_class_from || mode->cl > required_class_to))
227 error ("%s:%d: mode \"%s\" is not among class {%s, %s}",
228 file, line, name, mode_class_names[required_class_from] + 5,
229 mode_class_names[required_class_to] + 5);
230 return;
233 for (a = *category; a; a = a->next)
234 if (a->mode == mode)
236 error ("%s:%d: mode \"%s\" already has a %s adjustment",
237 file, line, name, catname);
238 error ("%s:%d: previous adjustment here", a->file, a->line);
239 return;
242 a = XNEW (struct mode_adjust);
243 a->mode = mode;
244 a->adjustment = adjustment;
245 a->file = file;
246 a->line = line;
248 a->next = *category;
249 *category = a;
252 /* Diagnose failure to meet expectations in a partially filled out
253 mode structure. */
254 enum requirement { SET, UNSET, OPTIONAL };
256 #define validate_field_(mname, fname, req, val, unset, file, line) do { \
257 switch (req) \
259 case SET: \
260 if (val == unset) \
261 error ("%s:%d: (%s) field %s must be set", \
262 file, line, mname, fname); \
263 break; \
264 case UNSET: \
265 if (val != unset) \
266 error ("%s:%d: (%s) field %s must not be set", \
267 file, line, mname, fname); \
268 case OPTIONAL: \
269 break; \
271 } while (0)
273 #define validate_field(M, F) \
274 validate_field_(M->name, #F, r_##F, M->F, blank_mode.F, M->file, M->line)
276 static void
277 validate_mode (struct mode_data *m,
278 enum requirement r_precision,
279 enum requirement r_bytesize,
280 enum requirement r_component,
281 enum requirement r_ncomponents,
282 enum requirement r_format)
284 validate_field (m, precision);
285 validate_field (m, bytesize);
286 validate_field (m, component);
287 validate_field (m, ncomponents);
288 validate_field (m, format);
290 #undef validate_field
291 #undef validate_field_
293 /* Given a partially-filled-out mode structure, figure out what we can
294 and fill the rest of it in; die if it isn't enough. */
295 static void
296 complete_mode (struct mode_data *m)
298 unsigned int alignment;
300 if (!m->name)
302 error ("%s:%d: mode with no name", m->file, m->line);
303 return;
305 if (m->cl == MAX_MODE_CLASS)
307 error ("%s:%d: %smode has no mode class", m->file, m->line, m->name);
308 return;
311 switch (m->cl)
313 case MODE_RANDOM:
314 /* Nothing more need be said. */
315 if (!strcmp (m->name, "VOID"))
316 void_mode = m;
318 validate_mode (m, UNSET, UNSET, UNSET, UNSET, UNSET);
320 m->precision = 0;
321 m->bytesize = 0;
322 m->ncomponents = 0;
323 m->component = 0;
324 break;
326 case MODE_CC:
327 /* Again, nothing more need be said. For historical reasons,
328 the size of a CC mode is four units. */
329 validate_mode (m, UNSET, UNSET, UNSET, UNSET, UNSET);
331 m->bytesize = 4;
332 m->ncomponents = 1;
333 m->component = 0;
334 break;
336 case MODE_INT:
337 case MODE_FLOAT:
338 case MODE_DECIMAL_FLOAT:
339 case MODE_FRACT:
340 case MODE_UFRACT:
341 case MODE_ACCUM:
342 case MODE_UACCUM:
343 /* A scalar mode must have a byte size, may have a bit size,
344 and must not have components. A float mode must have a
345 format. */
346 validate_mode (m, OPTIONAL, SET, UNSET, UNSET,
347 (m->cl == MODE_FLOAT || m->cl == MODE_DECIMAL_FLOAT)
348 ? SET : UNSET);
350 m->ncomponents = 1;
351 m->component = 0;
352 break;
354 case MODE_PARTIAL_INT:
355 /* A partial integer mode uses ->component to say what the
356 corresponding full-size integer mode is, and may also
357 specify a bit size. */
358 validate_mode (m, OPTIONAL, UNSET, SET, UNSET, UNSET);
360 m->bytesize = m->component->bytesize;
362 m->ncomponents = 1;
363 break;
365 case MODE_COMPLEX_INT:
366 case MODE_COMPLEX_FLOAT:
367 /* Complex modes should have a component indicated, but no more. */
368 validate_mode (m, UNSET, UNSET, SET, UNSET, UNSET);
369 m->ncomponents = 2;
370 if (m->component->precision != (unsigned int)-1)
371 m->precision = 2 * m->component->precision;
372 m->bytesize = 2 * m->component->bytesize;
373 break;
375 case MODE_VECTOR_INT:
376 case MODE_VECTOR_FLOAT:
377 case MODE_VECTOR_FRACT:
378 case MODE_VECTOR_UFRACT:
379 case MODE_VECTOR_ACCUM:
380 case MODE_VECTOR_UACCUM:
381 /* Vector modes should have a component and a number of components. */
382 validate_mode (m, UNSET, UNSET, SET, SET, UNSET);
383 if (m->component->precision != (unsigned int)-1)
384 m->precision = m->ncomponents * m->component->precision;
385 m->bytesize = m->ncomponents * m->component->bytesize;
386 break;
388 default:
389 gcc_unreachable ();
392 /* If not already specified, the mode alignment defaults to the largest
393 power of two that divides the size of the object. Complex types are
394 not more aligned than their contents. */
395 if (m->cl == MODE_COMPLEX_INT || m->cl == MODE_COMPLEX_FLOAT)
396 alignment = m->component->bytesize;
397 else
398 alignment = m->bytesize;
400 m->alignment = alignment & (~alignment + 1);
402 /* If this mode has components, make the component mode point back
403 to this mode, for the sake of adjustments. */
404 if (m->component)
406 m->next_cont = m->component->contained;
407 m->component->contained = m;
411 static void
412 complete_all_modes (void)
414 struct mode_data *m;
415 int cl;
417 for_all_modes (cl, m)
418 complete_mode (m);
421 /* For each mode in class CLASS, construct a corresponding complex mode. */
422 #define COMPLEX_MODES(C) make_complex_modes(MODE_##C, __FILE__, __LINE__)
423 static void
424 make_complex_modes (enum mode_class cl,
425 const char *file, unsigned int line)
427 struct mode_data *m;
428 struct mode_data *c;
429 enum mode_class cclass = complex_class (cl);
431 if (cclass == MODE_RANDOM)
432 return;
434 for (m = modes[cl]; m; m = m->next)
436 char *p, *buf;
437 size_t m_len;
439 /* Skip BImode. FIXME: BImode probably shouldn't be MODE_INT. */
440 if (m->precision == 1)
441 continue;
443 m_len = strlen (m->name);
444 /* The leading "1 +" is in case we prepend a "C" below. */
445 buf = (char *) xmalloc (1 + m_len + 1);
447 /* Float complex modes are named SCmode, etc.
448 Int complex modes are named CSImode, etc.
449 This inconsistency should be eliminated. */
450 p = 0;
451 if (cl == MODE_FLOAT)
453 memcpy (buf, m->name, m_len + 1);
454 p = strchr (buf, 'F');
455 if (p == 0 && strchr (buf, 'D') == 0)
457 error ("%s:%d: float mode \"%s\" has no 'F' or 'D'",
458 m->file, m->line, m->name);
459 free (buf);
460 continue;
463 if (p != 0)
464 *p = 'C';
465 else
467 buf[0] = 'C';
468 memcpy (buf + 1, m->name, m_len + 1);
471 c = new_mode (cclass, buf, file, line);
472 c->component = m;
476 /* For all modes in class CL, construct vector modes of width
477 WIDTH, having as many components as necessary. */
478 #define VECTOR_MODES(C, W) make_vector_modes(MODE_##C, W, __FILE__, __LINE__)
479 static void ATTRIBUTE_UNUSED
480 make_vector_modes (enum mode_class cl, unsigned int width,
481 const char *file, unsigned int line)
483 struct mode_data *m;
484 struct mode_data *v;
485 char buf[8];
486 unsigned int ncomponents;
487 enum mode_class vclass = vector_class (cl);
489 if (vclass == MODE_RANDOM)
490 return;
492 for (m = modes[cl]; m; m = m->next)
494 /* Do not construct vector modes with only one element, or
495 vector modes where the element size doesn't divide the full
496 size evenly. */
497 ncomponents = width / m->bytesize;
498 if (ncomponents < 2)
499 continue;
500 if (width % m->bytesize)
501 continue;
503 /* Skip QFmode and BImode. FIXME: this special case should
504 not be necessary. */
505 if (cl == MODE_FLOAT && m->bytesize == 1)
506 continue;
507 if (cl == MODE_INT && m->precision == 1)
508 continue;
510 if ((size_t)snprintf (buf, sizeof buf, "V%u%s", ncomponents, m->name)
511 >= sizeof buf)
513 error ("%s:%d: mode name \"%s\" is too long",
514 m->file, m->line, m->name);
515 continue;
518 v = new_mode (vclass, xstrdup (buf), file, line);
519 v->component = m;
520 v->ncomponents = ncomponents;
524 /* Input. */
526 #define _SPECIAL_MODE(C, N) make_special_mode(MODE_##C, #N, __FILE__, __LINE__)
527 #define RANDOM_MODE(N) _SPECIAL_MODE (RANDOM, N)
528 #define CC_MODE(N) _SPECIAL_MODE (CC, N)
530 static void
531 make_special_mode (enum mode_class cl, const char *name,
532 const char *file, unsigned int line)
534 new_mode (cl, name, file, line);
537 #define INT_MODE(N, Y) FRACTIONAL_INT_MODE (N, -1U, Y)
538 #define FRACTIONAL_INT_MODE(N, B, Y) \
539 make_int_mode (#N, B, Y, __FILE__, __LINE__)
541 static void
542 make_int_mode (const char *name,
543 unsigned int precision, unsigned int bytesize,
544 const char *file, unsigned int line)
546 struct mode_data *m = new_mode (MODE_INT, name, file, line);
547 m->bytesize = bytesize;
548 m->precision = precision;
551 #define FRACT_MODE(N, Y, F) \
552 make_fixed_point_mode (MODE_FRACT, #N, Y, 0, F, __FILE__, __LINE__)
554 #define UFRACT_MODE(N, Y, F) \
555 make_fixed_point_mode (MODE_UFRACT, #N, Y, 0, F, __FILE__, __LINE__)
557 #define ACCUM_MODE(N, Y, I, F) \
558 make_fixed_point_mode (MODE_ACCUM, #N, Y, I, F, __FILE__, __LINE__)
560 #define UACCUM_MODE(N, Y, I, F) \
561 make_fixed_point_mode (MODE_UACCUM, #N, Y, I, F, __FILE__, __LINE__)
563 /* Create a fixed-point mode by setting CL, NAME, BYTESIZE, IBIT, FBIT,
564 FILE, and LINE. */
566 static void
567 make_fixed_point_mode (enum mode_class cl,
568 const char *name,
569 unsigned int bytesize,
570 unsigned int ibit,
571 unsigned int fbit,
572 const char *file, unsigned int line)
574 struct mode_data *m = new_mode (cl, name, file, line);
575 m->bytesize = bytesize;
576 m->ibit = ibit;
577 m->fbit = fbit;
580 #define FLOAT_MODE(N, Y, F) FRACTIONAL_FLOAT_MODE (N, -1U, Y, F)
581 #define FRACTIONAL_FLOAT_MODE(N, B, Y, F) \
582 make_float_mode (#N, B, Y, #F, __FILE__, __LINE__)
584 static void
585 make_float_mode (const char *name,
586 unsigned int precision, unsigned int bytesize,
587 const char *format,
588 const char *file, unsigned int line)
590 struct mode_data *m = new_mode (MODE_FLOAT, name, file, line);
591 m->bytesize = bytesize;
592 m->precision = precision;
593 m->format = format;
596 #define DECIMAL_FLOAT_MODE(N, Y, F) \
597 FRACTIONAL_DECIMAL_FLOAT_MODE (N, -1U, Y, F)
598 #define FRACTIONAL_DECIMAL_FLOAT_MODE(N, B, Y, F) \
599 make_decimal_float_mode (#N, B, Y, #F, __FILE__, __LINE__)
601 static void
602 make_decimal_float_mode (const char *name,
603 unsigned int precision, unsigned int bytesize,
604 const char *format,
605 const char *file, unsigned int line)
607 struct mode_data *m = new_mode (MODE_DECIMAL_FLOAT, name, file, line);
608 m->bytesize = bytesize;
609 m->precision = precision;
610 m->format = format;
613 #define RESET_FLOAT_FORMAT(N, F) \
614 reset_float_format (#N, #F, __FILE__, __LINE__)
615 static void ATTRIBUTE_UNUSED
616 reset_float_format (const char *name, const char *format,
617 const char *file, unsigned int line)
619 struct mode_data *m = find_mode (name);
620 if (!m)
622 error ("%s:%d: no mode \"%s\"", file, line, name);
623 return;
625 if (m->cl != MODE_FLOAT && m->cl != MODE_DECIMAL_FLOAT)
627 error ("%s:%d: mode \"%s\" is not a FLOAT class", file, line, name);
628 return;
630 m->format = format;
633 /* Partial integer modes are specified by relation to a full integer mode.
634 For now, we do not attempt to narrow down their bit sizes. */
635 #define PARTIAL_INT_MODE(M) \
636 make_partial_integer_mode (#M, "P" #M, -1U, __FILE__, __LINE__)
637 static void ATTRIBUTE_UNUSED
638 make_partial_integer_mode (const char *base, const char *name,
639 unsigned int precision,
640 const char *file, unsigned int line)
642 struct mode_data *m;
643 struct mode_data *component = find_mode (base);
644 if (!component)
646 error ("%s:%d: no mode \"%s\"", file, line, name);
647 return;
649 if (component->cl != MODE_INT)
651 error ("%s:%d: mode \"%s\" is not class INT", file, line, name);
652 return;
655 m = new_mode (MODE_PARTIAL_INT, name, file, line);
656 m->precision = precision;
657 m->component = component;
660 /* A single vector mode can be specified by naming its component
661 mode and the number of components. */
662 #define VECTOR_MODE(C, M, N) \
663 make_vector_mode (MODE_##C, #M, N, __FILE__, __LINE__);
664 static void ATTRIBUTE_UNUSED
665 make_vector_mode (enum mode_class bclass,
666 const char *base,
667 unsigned int ncomponents,
668 const char *file, unsigned int line)
670 struct mode_data *v;
671 enum mode_class vclass = vector_class (bclass);
672 struct mode_data *component = find_mode (base);
673 char namebuf[8];
675 if (vclass == MODE_RANDOM)
676 return;
677 if (component == 0)
679 error ("%s:%d: no mode \"%s\"", file, line, base);
680 return;
682 if (component->cl != bclass
683 && (component->cl != MODE_PARTIAL_INT
684 || bclass != MODE_INT))
686 error ("%s:%d: mode \"%s\" is not class %s",
687 file, line, base, mode_class_names[bclass] + 5);
688 return;
691 if ((size_t)snprintf (namebuf, sizeof namebuf, "V%u%s",
692 ncomponents, base) >= sizeof namebuf)
694 error ("%s:%d: mode name \"%s\" is too long",
695 file, line, base);
696 return;
699 v = new_mode (vclass, xstrdup (namebuf), file, line);
700 v->ncomponents = ncomponents;
701 v->component = component;
704 /* Adjustability. */
705 #define _ADD_ADJUST(A, M, X, C1, C2) \
706 new_adjust (#M, &adj_##A, #A, #X, MODE_##C1, MODE_##C2, __FILE__, __LINE__)
708 #define ADJUST_BYTESIZE(M, X) _ADD_ADJUST(bytesize, M, X, RANDOM, RANDOM)
709 #define ADJUST_ALIGNMENT(M, X) _ADD_ADJUST(alignment, M, X, RANDOM, RANDOM)
710 #define ADJUST_FLOAT_FORMAT(M, X) _ADD_ADJUST(format, M, X, FLOAT, FLOAT)
711 #define ADJUST_IBIT(M, X) _ADD_ADJUST(ibit, M, X, ACCUM, UACCUM)
712 #define ADJUST_FBIT(M, X) _ADD_ADJUST(fbit, M, X, FRACT, UACCUM)
714 static void
715 create_modes (void)
717 #include "machmode.def"
720 /* Processing. */
722 /* Sort a list of modes into the order needed for the WIDER field:
723 major sort by precision, minor sort by component precision.
725 For instance:
726 QI < HI < SI < DI < TI
727 V4QI < V2HI < V8QI < V4HI < V2SI.
729 If the precision is not set, sort by the bytesize. A mode with
730 precision set gets sorted before a mode without precision set, if
731 they have the same bytesize; this is the right thing because
732 the precision must always be smaller than the bytesize * BITS_PER_UNIT.
733 We don't have to do anything special to get this done -- an unset
734 precision shows up as (unsigned int)-1, i.e. UINT_MAX. */
735 static int
736 cmp_modes (const void *a, const void *b)
738 const struct mode_data *const m = *(const struct mode_data *const*)a;
739 const struct mode_data *const n = *(const struct mode_data *const*)b;
741 if (m->bytesize > n->bytesize)
742 return 1;
743 else if (m->bytesize < n->bytesize)
744 return -1;
746 if (m->precision > n->precision)
747 return 1;
748 else if (m->precision < n->precision)
749 return -1;
751 if (!m->component && !n->component)
753 if (m->counter < n->counter)
754 return -1;
755 else
756 return 1;
759 if (m->component->bytesize > n->component->bytesize)
760 return 1;
761 else if (m->component->bytesize < n->component->bytesize)
762 return -1;
764 if (m->component->precision > n->component->precision)
765 return 1;
766 else if (m->component->precision < n->component->precision)
767 return -1;
769 if (m->counter < n->counter)
770 return -1;
771 else
772 return 1;
775 static void
776 calc_wider_mode (void)
778 int c;
779 struct mode_data *m;
780 struct mode_data **sortbuf;
781 unsigned int max_n_modes = 0;
782 unsigned int i, j;
784 for (c = 0; c < MAX_MODE_CLASS; c++)
785 max_n_modes = MAX (max_n_modes, n_modes[c]);
787 /* Allocate max_n_modes + 1 entries to leave room for the extra null
788 pointer assigned after the qsort call below. */
789 sortbuf = XALLOCAVEC (struct mode_data *, max_n_modes + 1);
791 for (c = 0; c < MAX_MODE_CLASS; c++)
793 /* "wider" is not meaningful for MODE_RANDOM and MODE_CC.
794 However, we want these in textual order, and we have
795 precisely the reverse. */
796 if (c == MODE_RANDOM || c == MODE_CC)
798 struct mode_data *prev, *next;
800 for (prev = 0, m = modes[c]; m; m = next)
802 m->wider = void_mode;
804 /* this is nreverse */
805 next = m->next;
806 m->next = prev;
807 prev = m;
809 modes[c] = prev;
811 else
813 if (!modes[c])
814 continue;
816 for (i = 0, m = modes[c]; m; i++, m = m->next)
817 sortbuf[i] = m;
819 qsort (sortbuf, i, sizeof (struct mode_data *), cmp_modes);
821 sortbuf[i] = 0;
822 for (j = 0; j < i; j++)
824 sortbuf[j]->next = sortbuf[j + 1];
825 if (c == MODE_PARTIAL_INT)
826 sortbuf[j]->wider = sortbuf[j]->component;
827 else
828 sortbuf[j]->wider = sortbuf[j]->next;
831 modes[c] = sortbuf[0];
836 /* Output routines. */
838 #define tagged_printf(FMT, ARG, TAG) do { \
839 int count_ = printf (" " FMT ",", ARG); \
840 printf ("%*s/* %s */\n", 27 - count_, "", TAG); \
841 } while (0)
843 #define print_decl(TYPE, NAME, ASIZE) \
844 puts ("\nconst " TYPE " " NAME "[" ASIZE "] =\n{");
846 #define print_maybe_const_decl(TYPE, NAME, ASIZE, CATEGORY) \
847 printf ("\n" TYPE " " NAME "[" ASIZE "] = \n{\n", \
848 adj_##CATEGORY ? "" : "const ")
850 #define print_closer() puts ("};")
852 static void
853 emit_insn_modes_h (void)
855 int c;
856 struct mode_data *m, *first, *last;
858 printf ("/* Generated automatically from machmode.def%s%s\n",
859 HAVE_EXTRA_MODES ? " and " : "",
860 EXTRA_MODES_FILE);
862 puts ("\
863 by genmodes. */\n\
865 #ifndef GCC_INSN_MODES_H\n\
866 #define GCC_INSN_MODES_H\n\
868 enum machine_mode\n{");
870 for (c = 0; c < MAX_MODE_CLASS; c++)
871 for (m = modes[c]; m; m = m->next)
873 int count_ = printf (" %smode,", m->name);
874 printf ("%*s/* %s:%d */\n", 27 - count_, "",
875 trim_filename (m->file), m->line);
878 puts (" MAX_MACHINE_MODE,\n");
880 for (c = 0; c < MAX_MODE_CLASS; c++)
882 first = modes[c];
883 last = 0;
884 for (m = first; m; last = m, m = m->next)
887 /* Don't use BImode for MIN_MODE_INT, since otherwise the middle
888 end will try to use it for bitfields in structures and the
889 like, which we do not want. Only the target md file should
890 generate BImode widgets. */
891 if (first && first->precision == 1)
892 first = first->next;
894 if (first && last)
895 printf (" MIN_%s = %smode,\n MAX_%s = %smode,\n\n",
896 mode_class_names[c], first->name,
897 mode_class_names[c], last->name);
898 else
899 printf (" MIN_%s = %smode,\n MAX_%s = %smode,\n\n",
900 mode_class_names[c], void_mode->name,
901 mode_class_names[c], void_mode->name);
904 puts ("\
905 NUM_MACHINE_MODES = MAX_MACHINE_MODE\n\
906 };\n");
908 /* I can't think of a better idea, can you? */
909 printf ("#define CONST_MODE_SIZE%s\n", adj_bytesize ? "" : " const");
910 printf ("#define CONST_MODE_BASE_ALIGN%s\n", adj_alignment ? "" : " const");
911 #if 0 /* disabled for backward compatibility, temporary */
912 printf ("#define CONST_REAL_FORMAT_FOR_MODE%s\n", adj_format ? "" :" const");
913 #endif
914 printf ("#define CONST_MODE_IBIT%s\n", adj_ibit ? "" : " const");
915 printf ("#define CONST_MODE_FBIT%s\n", adj_fbit ? "" : " const");
916 puts ("\
918 #endif /* insn-modes.h */");
921 static void
922 emit_insn_modes_c_header (void)
924 printf ("/* Generated automatically from machmode.def%s%s\n",
925 HAVE_EXTRA_MODES ? " and " : "",
926 EXTRA_MODES_FILE);
928 puts ("\
929 by genmodes. */\n\
931 #include \"config.h\"\n\
932 #include \"system.h\"\n\
933 #include \"coretypes.h\"\n\
934 #include \"tm.h\"\n\
935 #include \"machmode.h\"\n\
936 #include \"real.h\"");
939 static void
940 emit_min_insn_modes_c_header (void)
942 printf ("/* Generated automatically from machmode.def%s%s\n",
943 HAVE_EXTRA_MODES ? " and " : "",
944 EXTRA_MODES_FILE);
946 puts ("\
947 by genmodes. */\n\
949 #include \"bconfig.h\"\n\
950 #include \"system.h\"\n\
951 #include \"machmode.h\"");
954 static void
955 emit_mode_name (void)
957 int c;
958 struct mode_data *m;
960 print_decl ("char *const", "mode_name", "NUM_MACHINE_MODES");
962 for_all_modes (c, m)
963 printf (" \"%s\",\n", m->name);
965 print_closer ();
968 static void
969 emit_mode_class (void)
971 int c;
972 struct mode_data *m;
974 print_decl ("unsigned char", "mode_class", "NUM_MACHINE_MODES");
976 for_all_modes (c, m)
977 tagged_printf ("%s", mode_class_names[m->cl], m->name);
979 print_closer ();
982 static void
983 emit_mode_precision (void)
985 int c;
986 struct mode_data *m;
988 print_decl ("unsigned short", "mode_precision", "NUM_MACHINE_MODES");
990 for_all_modes (c, m)
991 if (m->precision != (unsigned int)-1)
992 tagged_printf ("%u", m->precision, m->name);
993 else
994 tagged_printf ("%u*BITS_PER_UNIT", m->bytesize, m->name);
996 print_closer ();
999 static void
1000 emit_mode_size (void)
1002 int c;
1003 struct mode_data *m;
1005 print_maybe_const_decl ("%sunsigned char", "mode_size",
1006 "NUM_MACHINE_MODES", bytesize);
1008 for_all_modes (c, m)
1009 tagged_printf ("%u", m->bytesize, m->name);
1011 print_closer ();
1014 static void
1015 emit_mode_nunits (void)
1017 int c;
1018 struct mode_data *m;
1020 print_decl ("unsigned char", "mode_nunits", "NUM_MACHINE_MODES");
1022 for_all_modes (c, m)
1023 tagged_printf ("%u", m->ncomponents, m->name);
1025 print_closer ();
1028 static void
1029 emit_mode_wider (void)
1031 int c;
1032 struct mode_data *m;
1034 print_decl ("unsigned char", "mode_wider", "NUM_MACHINE_MODES");
1036 for_all_modes (c, m)
1037 tagged_printf ("%smode",
1038 m->wider ? m->wider->name : void_mode->name,
1039 m->name);
1041 print_closer ();
1042 print_decl ("unsigned char", "mode_2xwider", "NUM_MACHINE_MODES");
1044 for_all_modes (c, m)
1046 struct mode_data * m2;
1048 for (m2 = m;
1049 m2 && m2 != void_mode;
1050 m2 = m2->wider)
1052 if (m2->bytesize < 2 * m->bytesize)
1053 continue;
1054 if (m->precision != (unsigned int) -1)
1056 if (m2->precision != 2 * m->precision)
1057 continue;
1059 else
1061 if (m2->precision != (unsigned int) -1)
1062 continue;
1065 /* For vectors we want twice the number of components,
1066 with the same element type. */
1067 if (m->cl == MODE_VECTOR_INT
1068 || m->cl == MODE_VECTOR_FLOAT
1069 || m->cl == MODE_VECTOR_FRACT
1070 || m->cl == MODE_VECTOR_UFRACT
1071 || m->cl == MODE_VECTOR_ACCUM
1072 || m->cl == MODE_VECTOR_UACCUM)
1074 if (m2->ncomponents != 2 * m->ncomponents)
1075 continue;
1076 if (m->component != m2->component)
1077 continue;
1080 break;
1082 if (m2 == void_mode)
1083 m2 = 0;
1084 tagged_printf ("%smode",
1085 m2 ? m2->name : void_mode->name,
1086 m->name);
1089 print_closer ();
1092 static void
1093 emit_mode_mask (void)
1095 int c;
1096 struct mode_data *m;
1098 print_decl ("unsigned HOST_WIDE_INT", "mode_mask_array",
1099 "NUM_MACHINE_MODES");
1100 puts ("\
1101 #define MODE_MASK(m) \\\n\
1102 ((m) >= HOST_BITS_PER_WIDE_INT) \\\n\
1103 ? ~(unsigned HOST_WIDE_INT) 0 \\\n\
1104 : ((unsigned HOST_WIDE_INT) 1 << (m)) - 1\n");
1106 for_all_modes (c, m)
1107 if (m->precision != (unsigned int)-1)
1108 tagged_printf ("MODE_MASK (%u)", m->precision, m->name);
1109 else
1110 tagged_printf ("MODE_MASK (%u*BITS_PER_UNIT)", m->bytesize, m->name);
1112 puts ("#undef MODE_MASK");
1113 print_closer ();
1116 static void
1117 emit_mode_inner (void)
1119 int c;
1120 struct mode_data *m;
1122 print_decl ("unsigned char", "mode_inner", "NUM_MACHINE_MODES");
1124 for_all_modes (c, m)
1125 tagged_printf ("%smode",
1126 c != MODE_PARTIAL_INT && m->component
1127 ? m->component->name : void_mode->name,
1128 m->name);
1130 print_closer ();
1133 static void
1134 emit_mode_base_align (void)
1136 int c;
1137 struct mode_data *m;
1139 print_maybe_const_decl ("%sunsigned char",
1140 "mode_base_align", "NUM_MACHINE_MODES",
1141 alignment);
1143 for_all_modes (c, m)
1144 tagged_printf ("%u", m->alignment, m->name);
1146 print_closer ();
1149 static void
1150 emit_class_narrowest_mode (void)
1152 int c;
1154 print_decl ("unsigned char", "class_narrowest_mode", "MAX_MODE_CLASS");
1156 for (c = 0; c < MAX_MODE_CLASS; c++)
1157 /* Bleah, all this to get the comment right for MIN_MODE_INT. */
1158 tagged_printf ("MIN_%s", mode_class_names[c],
1159 modes[c]
1160 ? (modes[c]->precision != 1
1161 ? modes[c]->name
1162 : (modes[c]->next
1163 ? modes[c]->next->name
1164 : void_mode->name))
1165 : void_mode->name);
1167 print_closer ();
1170 static void
1171 emit_real_format_for_mode (void)
1173 struct mode_data *m;
1175 /* The entities pointed to by this table are constant, whether
1176 or not the table itself is constant.
1178 For backward compatibility this table is always writable
1179 (several targets modify it in TARGET_OPTION_OVERRIDE). FIXME:
1180 convert all said targets to use ADJUST_FORMAT instead. */
1181 #if 0
1182 print_maybe_const_decl ("const struct real_format *%s",
1183 "real_format_for_mode",
1184 "MAX_MODE_FLOAT - MIN_MODE_FLOAT + 1",
1185 format);
1186 #else
1187 print_decl ("struct real_format *\n", "real_format_for_mode",
1188 "MAX_MODE_FLOAT - MIN_MODE_FLOAT + 1 "
1189 "+ MAX_MODE_DECIMAL_FLOAT - MIN_MODE_DECIMAL_FLOAT + 1");
1190 #endif
1192 /* The beginning of the table is entries for float modes. */
1193 for (m = modes[MODE_FLOAT]; m; m = m->next)
1194 if (!strcmp (m->format, "0"))
1195 tagged_printf ("%s", m->format, m->name);
1196 else
1197 tagged_printf ("&%s", m->format, m->name);
1199 /* The end of the table is entries for decimal float modes. */
1200 for (m = modes[MODE_DECIMAL_FLOAT]; m; m = m->next)
1201 if (!strcmp (m->format, "0"))
1202 tagged_printf ("%s", m->format, m->name);
1203 else
1204 tagged_printf ("&%s", m->format, m->name);
1206 print_closer ();
1209 static void
1210 emit_mode_adjustments (void)
1212 struct mode_adjust *a;
1213 struct mode_data *m;
1215 puts ("\
1216 \nvoid\
1217 \ninit_adjust_machine_modes (void)\
1218 \n{\
1219 \n size_t s ATTRIBUTE_UNUSED;");
1221 /* Size adjustments must be propagated to all containing modes.
1222 A size adjustment forces us to recalculate the alignment too. */
1223 for (a = adj_bytesize; a; a = a->next)
1225 printf ("\n /* %s:%d */\n s = %s;\n",
1226 a->file, a->line, a->adjustment);
1227 printf (" mode_size[%smode] = s;\n", a->mode->name);
1228 printf (" mode_base_align[%smode] = s & (~s + 1);\n",
1229 a->mode->name);
1231 for (m = a->mode->contained; m; m = m->next_cont)
1233 switch (m->cl)
1235 case MODE_COMPLEX_INT:
1236 case MODE_COMPLEX_FLOAT:
1237 printf (" mode_size[%smode] = 2*s;\n", m->name);
1238 printf (" mode_base_align[%smode] = s & (~s + 1);\n",
1239 m->name);
1240 break;
1242 case MODE_VECTOR_INT:
1243 case MODE_VECTOR_FLOAT:
1244 case MODE_VECTOR_FRACT:
1245 case MODE_VECTOR_UFRACT:
1246 case MODE_VECTOR_ACCUM:
1247 case MODE_VECTOR_UACCUM:
1248 printf (" mode_size[%smode] = %d*s;\n",
1249 m->name, m->ncomponents);
1250 printf (" mode_base_align[%smode] = (%d*s) & (~(%d*s)+1);\n",
1251 m->name, m->ncomponents, m->ncomponents);
1252 break;
1254 default:
1255 internal_error (
1256 "mode %s is neither vector nor complex but contains %s",
1257 m->name, a->mode->name);
1258 /* NOTREACHED */
1263 /* Alignment adjustments propagate too.
1264 ??? This may not be the right thing for vector modes. */
1265 for (a = adj_alignment; a; a = a->next)
1267 printf ("\n /* %s:%d */\n s = %s;\n",
1268 a->file, a->line, a->adjustment);
1269 printf (" mode_base_align[%smode] = s;\n", a->mode->name);
1271 for (m = a->mode->contained; m; m = m->next_cont)
1273 switch (m->cl)
1275 case MODE_COMPLEX_INT:
1276 case MODE_COMPLEX_FLOAT:
1277 printf (" mode_base_align[%smode] = s;\n", m->name);
1278 break;
1280 case MODE_VECTOR_INT:
1281 case MODE_VECTOR_FLOAT:
1282 case MODE_VECTOR_FRACT:
1283 case MODE_VECTOR_UFRACT:
1284 case MODE_VECTOR_ACCUM:
1285 case MODE_VECTOR_UACCUM:
1286 printf (" mode_base_align[%smode] = %d*s;\n",
1287 m->name, m->ncomponents);
1288 break;
1290 default:
1291 internal_error (
1292 "mode %s is neither vector nor complex but contains %s",
1293 m->name, a->mode->name);
1294 /* NOTREACHED */
1299 /* Ibit adjustments don't have to propagate. */
1300 for (a = adj_ibit; a; a = a->next)
1302 printf ("\n /* %s:%d */\n s = %s;\n",
1303 a->file, a->line, a->adjustment);
1304 printf (" mode_ibit[%smode] = s;\n", a->mode->name);
1307 /* Fbit adjustments don't have to propagate. */
1308 for (a = adj_fbit; a; a = a->next)
1310 printf ("\n /* %s:%d */\n s = %s;\n",
1311 a->file, a->line, a->adjustment);
1312 printf (" mode_fbit[%smode] = s;\n", a->mode->name);
1315 /* Real mode formats don't have to propagate anywhere. */
1316 for (a = adj_format; a; a = a->next)
1317 printf ("\n /* %s:%d */\n REAL_MODE_FORMAT (%smode) = %s;\n",
1318 a->file, a->line, a->mode->name, a->adjustment);
1320 puts ("}");
1323 /* Emit ibit for all modes. */
1325 static void
1326 emit_mode_ibit (void)
1328 int c;
1329 struct mode_data *m;
1331 print_maybe_const_decl ("%sunsigned char",
1332 "mode_ibit", "NUM_MACHINE_MODES",
1333 ibit);
1335 for_all_modes (c, m)
1336 tagged_printf ("%u", m->ibit, m->name);
1338 print_closer ();
1341 /* Emit fbit for all modes. */
1343 static void
1344 emit_mode_fbit (void)
1346 int c;
1347 struct mode_data *m;
1349 print_maybe_const_decl ("%sunsigned char",
1350 "mode_fbit", "NUM_MACHINE_MODES",
1351 fbit);
1353 for_all_modes (c, m)
1354 tagged_printf ("%u", m->fbit, m->name);
1356 print_closer ();
1360 static void
1361 emit_insn_modes_c (void)
1363 emit_insn_modes_c_header ();
1364 emit_mode_name ();
1365 emit_mode_class ();
1366 emit_mode_precision ();
1367 emit_mode_size ();
1368 emit_mode_nunits ();
1369 emit_mode_wider ();
1370 emit_mode_mask ();
1371 emit_mode_inner ();
1372 emit_mode_base_align ();
1373 emit_class_narrowest_mode ();
1374 emit_real_format_for_mode ();
1375 emit_mode_adjustments ();
1376 emit_mode_ibit ();
1377 emit_mode_fbit ();
1380 static void
1381 emit_min_insn_modes_c (void)
1383 emit_min_insn_modes_c_header ();
1384 emit_mode_name ();
1385 emit_mode_class ();
1386 emit_mode_wider ();
1387 emit_class_narrowest_mode ();
1390 /* Master control. */
1392 main (int argc, char **argv)
1394 bool gen_header = false, gen_min = false;
1395 progname = argv[0];
1397 if (argc == 1)
1399 else if (argc == 2 && !strcmp (argv[1], "-h"))
1400 gen_header = true;
1401 else if (argc == 2 && !strcmp (argv[1], "-m"))
1402 gen_min = true;
1403 else
1405 error ("usage: %s [-h|-m] > file", progname);
1406 return FATAL_EXIT_CODE;
1409 modes_by_name = htab_create_alloc (64, hash_mode, eq_mode, 0, xcalloc, free);
1411 create_modes ();
1412 complete_all_modes ();
1414 if (have_error)
1415 return FATAL_EXIT_CODE;
1417 calc_wider_mode ();
1419 if (gen_header)
1420 emit_insn_modes_h ();
1421 else if (gen_min)
1422 emit_min_insn_modes_c ();
1423 else
1424 emit_insn_modes_c ();
1426 if (fflush (stdout) || fclose (stdout))
1427 return FATAL_EXIT_CODE;
1428 return SUCCESS_EXIT_CODE;