2013-11-08 Andrew MacLeod <amacleod@redhat.com>
[official-gcc.git] / gcc / genmodes.c
blob12a98f301628bd835cdbcc044c78bd598413d8b6
1 /* Generate the machine mode enumeration and associated tables.
2 Copyright (C) 2003-2013 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"
23 #include "hashtab.h"
25 /* enum mode_class is normally defined by machmode.h but we can't
26 include that header here. */
27 #include "mode-classes.def"
29 #define DEF_MODE_CLASS(M) M
30 enum mode_class { MODE_CLASSES, MAX_MODE_CLASS };
31 #undef DEF_MODE_CLASS
33 /* Text names of mode classes, for output. */
34 #define DEF_MODE_CLASS(M) #M
35 static const char *const mode_class_names[MAX_MODE_CLASS] =
37 MODE_CLASSES
39 #undef DEF_MODE_CLASS
40 #undef MODE_CLASSES
42 #ifdef EXTRA_MODES_FILE
43 # define HAVE_EXTRA_MODES 1
44 #else
45 # define HAVE_EXTRA_MODES 0
46 # define EXTRA_MODES_FILE ""
47 #endif
49 /* Data structure for building up what we know about a mode.
50 They're clustered by mode class. */
51 struct mode_data
53 struct mode_data *next; /* next this class - arbitrary order */
55 const char *name; /* printable mode name -- SI, not SImode */
56 enum mode_class cl; /* this mode class */
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 const char *file; /* file and line of definition, */
71 unsigned int line; /* for error reporting */
72 unsigned int counter; /* Rank ordering of modes */
73 unsigned int ibit; /* the number of integral bits */
74 unsigned int fbit; /* the number of fractional bits */
77 static struct mode_data *modes[MAX_MODE_CLASS];
78 static unsigned int n_modes[MAX_MODE_CLASS];
79 static struct mode_data *void_mode;
81 static const struct mode_data blank_mode = {
82 0, "<unknown>", MAX_MODE_CLASS,
83 -1U, -1U, -1U, -1U,
84 0, 0, 0, 0, 0,
85 "<unknown>", 0, 0, 0, 0
88 static htab_t modes_by_name;
90 /* Data structure for recording target-specified runtime adjustments
91 to a particular mode. We support varying the byte size, the
92 alignment, and the floating point format. */
93 struct mode_adjust
95 struct mode_adjust *next;
96 struct mode_data *mode;
97 const char *adjustment;
99 const char *file;
100 unsigned int line;
103 static struct mode_adjust *adj_bytesize;
104 static struct mode_adjust *adj_alignment;
105 static struct mode_adjust *adj_format;
106 static struct mode_adjust *adj_ibit;
107 static struct mode_adjust *adj_fbit;
109 /* Mode class operations. */
110 static enum mode_class
111 complex_class (enum mode_class c)
113 switch (c)
115 case MODE_INT: return MODE_COMPLEX_INT;
116 case MODE_FLOAT: return MODE_COMPLEX_FLOAT;
117 default:
118 error ("no complex class for class %s", mode_class_names[c]);
119 return MODE_RANDOM;
123 static enum mode_class
124 vector_class (enum mode_class cl)
126 switch (cl)
128 case MODE_INT: return MODE_VECTOR_INT;
129 case MODE_FLOAT: return MODE_VECTOR_FLOAT;
130 case MODE_FRACT: return MODE_VECTOR_FRACT;
131 case MODE_UFRACT: return MODE_VECTOR_UFRACT;
132 case MODE_ACCUM: return MODE_VECTOR_ACCUM;
133 case MODE_UACCUM: return MODE_VECTOR_UACCUM;
134 default:
135 error ("no vector class for class %s", mode_class_names[cl]);
136 return MODE_RANDOM;
140 /* Utility routines. */
141 static inline struct mode_data *
142 find_mode (const char *name)
144 struct mode_data key;
146 key.name = name;
147 return (struct mode_data *) htab_find (modes_by_name, &key);
150 static struct mode_data *
151 new_mode (enum mode_class cl, const char *name,
152 const char *file, unsigned int line)
154 struct mode_data *m;
155 static unsigned int count = 0;
157 m = find_mode (name);
158 if (m)
160 error ("%s:%d: duplicate definition of mode \"%s\"",
161 trim_filename (file), line, name);
162 error ("%s:%d: previous definition here", m->file, m->line);
163 return m;
166 m = XNEW (struct mode_data);
167 memcpy (m, &blank_mode, sizeof (struct mode_data));
168 m->cl = cl;
169 m->name = name;
170 if (file)
171 m->file = trim_filename (file);
172 m->line = line;
173 m->counter = count++;
175 m->next = modes[cl];
176 modes[cl] = m;
177 n_modes[cl]++;
179 *htab_find_slot (modes_by_name, m, INSERT) = m;
181 return m;
184 static hashval_t
185 hash_mode (const void *p)
187 const struct mode_data *m = (const struct mode_data *)p;
188 return htab_hash_string (m->name);
191 static int
192 eq_mode (const void *p, const void *q)
194 const struct mode_data *a = (const struct mode_data *)p;
195 const struct mode_data *b = (const struct mode_data *)q;
197 return !strcmp (a->name, b->name);
200 #define for_all_modes(C, M) \
201 for (C = 0; C < MAX_MODE_CLASS; C++) \
202 for (M = modes[C]; M; M = M->next)
204 static void ATTRIBUTE_UNUSED
205 new_adjust (const char *name,
206 struct mode_adjust **category, const char *catname,
207 const char *adjustment,
208 enum mode_class required_class_from,
209 enum mode_class required_class_to,
210 const char *file, unsigned int line)
212 struct mode_data *mode = find_mode (name);
213 struct mode_adjust *a;
215 file = trim_filename (file);
217 if (!mode)
219 error ("%s:%d: no mode \"%s\"", file, line, name);
220 return;
223 if (required_class_from != MODE_RANDOM
224 && (mode->cl < required_class_from || mode->cl > required_class_to))
226 error ("%s:%d: mode \"%s\" is not among class {%s, %s}",
227 file, line, name, mode_class_names[required_class_from] + 5,
228 mode_class_names[required_class_to] + 5);
229 return;
232 for (a = *category; a; a = a->next)
233 if (a->mode == mode)
235 error ("%s:%d: mode \"%s\" already has a %s adjustment",
236 file, line, name, catname);
237 error ("%s:%d: previous adjustment here", a->file, a->line);
238 return;
241 a = XNEW (struct mode_adjust);
242 a->mode = mode;
243 a->adjustment = adjustment;
244 a->file = file;
245 a->line = line;
247 a->next = *category;
248 *category = a;
251 /* Diagnose failure to meet expectations in a partially filled out
252 mode structure. */
253 enum requirement { SET, UNSET, OPTIONAL };
255 #define validate_field_(mname, fname, req, val, unset, file, line) do { \
256 switch (req) \
258 case SET: \
259 if (val == unset) \
260 error ("%s:%d: (%s) field %s must be set", \
261 file, line, mname, fname); \
262 break; \
263 case UNSET: \
264 if (val != unset) \
265 error ("%s:%d: (%s) field %s must not be set", \
266 file, line, mname, fname); \
267 case OPTIONAL: \
268 break; \
270 } while (0)
272 #define validate_field(M, F) \
273 validate_field_(M->name, #F, r_##F, M->F, blank_mode.F, M->file, M->line)
275 static void
276 validate_mode (struct mode_data *m,
277 enum requirement r_precision,
278 enum requirement r_bytesize,
279 enum requirement r_component,
280 enum requirement r_ncomponents,
281 enum requirement r_format)
283 validate_field (m, precision);
284 validate_field (m, bytesize);
285 validate_field (m, component);
286 validate_field (m, ncomponents);
287 validate_field (m, format);
289 #undef validate_field
290 #undef validate_field_
292 /* Given a partially-filled-out mode structure, figure out what we can
293 and fill the rest of it in; die if it isn't enough. */
294 static void
295 complete_mode (struct mode_data *m)
297 unsigned int alignment;
299 if (!m->name)
301 error ("%s:%d: mode with no name", m->file, m->line);
302 return;
304 if (m->cl == MAX_MODE_CLASS)
306 error ("%s:%d: %smode has no mode class", m->file, m->line, m->name);
307 return;
310 switch (m->cl)
312 case MODE_RANDOM:
313 /* Nothing more need be said. */
314 if (!strcmp (m->name, "VOID"))
315 void_mode = m;
317 validate_mode (m, UNSET, UNSET, UNSET, UNSET, UNSET);
319 m->precision = 0;
320 m->bytesize = 0;
321 m->ncomponents = 0;
322 m->component = 0;
323 break;
325 case MODE_CC:
326 /* Again, nothing more need be said. For historical reasons,
327 the size of a CC mode is four units. */
328 validate_mode (m, UNSET, UNSET, UNSET, UNSET, UNSET);
330 m->bytesize = 4;
331 m->ncomponents = 1;
332 m->component = 0;
333 break;
335 case MODE_INT:
336 case MODE_POINTER_BOUNDS:
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) \
527 make_special_mode (MODE_##C, #N, __FILE__, __LINE__)
528 #define RANDOM_MODE(N) _SPECIAL_MODE (RANDOM, N)
529 #define CC_MODE(N) _SPECIAL_MODE (CC, N)
531 static void
532 make_special_mode (enum mode_class cl, const char *name,
533 const char *file, unsigned int line)
535 new_mode (cl, name, file, line);
538 #define POINTER_BOUNDS_MODE(N, Y) \
539 make_pointer_bounds_mode (#N, Y, __FILE__, __LINE__)
541 static void ATTRIBUTE_UNUSED
542 make_pointer_bounds_mode (const char *name,
543 unsigned int bytesize,
544 const char *file, unsigned int line)
546 struct mode_data *m = new_mode (MODE_POINTER_BOUNDS, name, file, line);
547 m->bytesize = bytesize;
551 #define INT_MODE(N, Y) FRACTIONAL_INT_MODE (N, -1U, Y)
552 #define FRACTIONAL_INT_MODE(N, B, Y) \
553 make_int_mode (#N, B, Y, __FILE__, __LINE__)
555 static void
556 make_int_mode (const char *name,
557 unsigned int precision, unsigned int bytesize,
558 const char *file, unsigned int line)
560 struct mode_data *m = new_mode (MODE_INT, name, file, line);
561 m->bytesize = bytesize;
562 m->precision = precision;
565 #define FRACT_MODE(N, Y, F) \
566 make_fixed_point_mode (MODE_FRACT, #N, Y, 0, F, __FILE__, __LINE__)
568 #define UFRACT_MODE(N, Y, F) \
569 make_fixed_point_mode (MODE_UFRACT, #N, Y, 0, F, __FILE__, __LINE__)
571 #define ACCUM_MODE(N, Y, I, F) \
572 make_fixed_point_mode (MODE_ACCUM, #N, Y, I, F, __FILE__, __LINE__)
574 #define UACCUM_MODE(N, Y, I, F) \
575 make_fixed_point_mode (MODE_UACCUM, #N, Y, I, F, __FILE__, __LINE__)
577 /* Create a fixed-point mode by setting CL, NAME, BYTESIZE, IBIT, FBIT,
578 FILE, and LINE. */
580 static void
581 make_fixed_point_mode (enum mode_class cl,
582 const char *name,
583 unsigned int bytesize,
584 unsigned int ibit,
585 unsigned int fbit,
586 const char *file, unsigned int line)
588 struct mode_data *m = new_mode (cl, name, file, line);
589 m->bytesize = bytesize;
590 m->ibit = ibit;
591 m->fbit = fbit;
594 #define FLOAT_MODE(N, Y, F) FRACTIONAL_FLOAT_MODE (N, -1U, Y, F)
595 #define FRACTIONAL_FLOAT_MODE(N, B, Y, F) \
596 make_float_mode (#N, B, Y, #F, __FILE__, __LINE__)
598 static void
599 make_float_mode (const char *name,
600 unsigned int precision, unsigned int bytesize,
601 const char *format,
602 const char *file, unsigned int line)
604 struct mode_data *m = new_mode (MODE_FLOAT, name, file, line);
605 m->bytesize = bytesize;
606 m->precision = precision;
607 m->format = format;
610 #define DECIMAL_FLOAT_MODE(N, Y, F) \
611 FRACTIONAL_DECIMAL_FLOAT_MODE (N, -1U, Y, F)
612 #define FRACTIONAL_DECIMAL_FLOAT_MODE(N, B, Y, F) \
613 make_decimal_float_mode (#N, B, Y, #F, __FILE__, __LINE__)
615 static void
616 make_decimal_float_mode (const char *name,
617 unsigned int precision, unsigned int bytesize,
618 const char *format,
619 const char *file, unsigned int line)
621 struct mode_data *m = new_mode (MODE_DECIMAL_FLOAT, name, file, line);
622 m->bytesize = bytesize;
623 m->precision = precision;
624 m->format = format;
627 #define RESET_FLOAT_FORMAT(N, F) \
628 reset_float_format (#N, #F, __FILE__, __LINE__)
629 static void ATTRIBUTE_UNUSED
630 reset_float_format (const char *name, const char *format,
631 const char *file, unsigned int line)
633 struct mode_data *m = find_mode (name);
634 if (!m)
636 error ("%s:%d: no mode \"%s\"", file, line, name);
637 return;
639 if (m->cl != MODE_FLOAT && m->cl != MODE_DECIMAL_FLOAT)
641 error ("%s:%d: mode \"%s\" is not a FLOAT class", file, line, name);
642 return;
644 m->format = format;
647 /* Partial integer modes are specified by relation to a full integer
648 mode. */
649 #define PARTIAL_INT_MODE(M,PREC,NAME) \
650 make_partial_integer_mode (#M, #NAME, PREC, __FILE__, __LINE__)
651 static void ATTRIBUTE_UNUSED
652 make_partial_integer_mode (const char *base, const char *name,
653 unsigned int precision,
654 const char *file, unsigned int line)
656 struct mode_data *m;
657 struct mode_data *component = find_mode (base);
658 if (!component)
660 error ("%s:%d: no mode \"%s\"", file, line, name);
661 return;
663 if (component->cl != MODE_INT)
665 error ("%s:%d: mode \"%s\" is not class INT", file, line, name);
666 return;
669 m = new_mode (MODE_PARTIAL_INT, name, file, line);
670 m->precision = precision;
671 m->component = component;
674 /* A single vector mode can be specified by naming its component
675 mode and the number of components. */
676 #define VECTOR_MODE(C, M, N) \
677 make_vector_mode (MODE_##C, #M, N, __FILE__, __LINE__);
678 static void ATTRIBUTE_UNUSED
679 make_vector_mode (enum mode_class bclass,
680 const char *base,
681 unsigned int ncomponents,
682 const char *file, unsigned int line)
684 struct mode_data *v;
685 enum mode_class vclass = vector_class (bclass);
686 struct mode_data *component = find_mode (base);
687 char namebuf[16];
689 if (vclass == MODE_RANDOM)
690 return;
691 if (component == 0)
693 error ("%s:%d: no mode \"%s\"", file, line, base);
694 return;
696 if (component->cl != bclass
697 && (component->cl != MODE_PARTIAL_INT
698 || bclass != MODE_INT))
700 error ("%s:%d: mode \"%s\" is not class %s",
701 file, line, base, mode_class_names[bclass] + 5);
702 return;
705 if ((size_t)snprintf (namebuf, sizeof namebuf, "V%u%s",
706 ncomponents, base) >= sizeof namebuf)
708 error ("%s:%d: mode name \"%s\" is too long",
709 file, line, base);
710 return;
713 v = new_mode (vclass, xstrdup (namebuf), file, line);
714 v->ncomponents = ncomponents;
715 v->component = component;
718 /* Adjustability. */
719 #define _ADD_ADJUST(A, M, X, C1, C2) \
720 new_adjust (#M, &adj_##A, #A, #X, MODE_##C1, MODE_##C2, __FILE__, __LINE__)
722 #define ADJUST_BYTESIZE(M, X) _ADD_ADJUST (bytesize, M, X, RANDOM, RANDOM)
723 #define ADJUST_ALIGNMENT(M, X) _ADD_ADJUST (alignment, M, X, RANDOM, RANDOM)
724 #define ADJUST_FLOAT_FORMAT(M, X) _ADD_ADJUST (format, M, X, FLOAT, FLOAT)
725 #define ADJUST_IBIT(M, X) _ADD_ADJUST (ibit, M, X, ACCUM, UACCUM)
726 #define ADJUST_FBIT(M, X) _ADD_ADJUST (fbit, M, X, FRACT, UACCUM)
728 static void
729 create_modes (void)
731 #include "machmode.def"
734 /* Processing. */
736 /* Sort a list of modes into the order needed for the WIDER field:
737 major sort by precision, minor sort by component precision.
739 For instance:
740 QI < HI < SI < DI < TI
741 V4QI < V2HI < V8QI < V4HI < V2SI.
743 If the precision is not set, sort by the bytesize. A mode with
744 precision set gets sorted before a mode without precision set, if
745 they have the same bytesize; this is the right thing because
746 the precision must always be smaller than the bytesize * BITS_PER_UNIT.
747 We don't have to do anything special to get this done -- an unset
748 precision shows up as (unsigned int)-1, i.e. UINT_MAX. */
749 static int
750 cmp_modes (const void *a, const void *b)
752 const struct mode_data *const m = *(const struct mode_data *const*)a;
753 const struct mode_data *const n = *(const struct mode_data *const*)b;
755 if (m->bytesize > n->bytesize)
756 return 1;
757 else if (m->bytesize < n->bytesize)
758 return -1;
760 if (m->precision > n->precision)
761 return 1;
762 else if (m->precision < n->precision)
763 return -1;
765 if (!m->component && !n->component)
767 if (m->counter < n->counter)
768 return -1;
769 else
770 return 1;
773 if (m->component->bytesize > n->component->bytesize)
774 return 1;
775 else if (m->component->bytesize < n->component->bytesize)
776 return -1;
778 if (m->component->precision > n->component->precision)
779 return 1;
780 else if (m->component->precision < n->component->precision)
781 return -1;
783 if (m->counter < n->counter)
784 return -1;
785 else
786 return 1;
789 static void
790 calc_wider_mode (void)
792 int c;
793 struct mode_data *m;
794 struct mode_data **sortbuf;
795 unsigned int max_n_modes = 0;
796 unsigned int i, j;
798 for (c = 0; c < MAX_MODE_CLASS; c++)
799 max_n_modes = MAX (max_n_modes, n_modes[c]);
801 /* Allocate max_n_modes + 1 entries to leave room for the extra null
802 pointer assigned after the qsort call below. */
803 sortbuf = XALLOCAVEC (struct mode_data *, max_n_modes + 1);
805 for (c = 0; c < MAX_MODE_CLASS; c++)
807 /* "wider" is not meaningful for MODE_RANDOM and MODE_CC.
808 However, we want these in textual order, and we have
809 precisely the reverse. */
810 if (c == MODE_RANDOM || c == MODE_CC)
812 struct mode_data *prev, *next;
814 for (prev = 0, m = modes[c]; m; m = next)
816 m->wider = void_mode;
818 /* this is nreverse */
819 next = m->next;
820 m->next = prev;
821 prev = m;
823 modes[c] = prev;
825 else
827 if (!modes[c])
828 continue;
830 for (i = 0, m = modes[c]; m; i++, m = m->next)
831 sortbuf[i] = m;
833 qsort (sortbuf, i, sizeof (struct mode_data *), cmp_modes);
835 sortbuf[i] = 0;
836 for (j = 0; j < i; j++)
838 sortbuf[j]->next = sortbuf[j + 1];
839 if (c == MODE_PARTIAL_INT)
840 sortbuf[j]->wider = sortbuf[j]->component;
841 else
842 sortbuf[j]->wider = sortbuf[j]->next;
845 modes[c] = sortbuf[0];
850 /* Output routines. */
852 #define tagged_printf(FMT, ARG, TAG) do { \
853 int count_ = printf (" " FMT ",", ARG); \
854 printf ("%*s/* %s */\n", 27 - count_, "", TAG); \
855 } while (0)
857 #define print_decl(TYPE, NAME, ASIZE) \
858 puts ("\nconst " TYPE " " NAME "[" ASIZE "] =\n{");
860 #define print_maybe_const_decl(TYPE, NAME, ASIZE, CATEGORY) \
861 printf ("\n" TYPE " " NAME "[" ASIZE "] = \n{\n", \
862 adj_##CATEGORY ? "" : "const ")
864 #define print_closer() puts ("};")
866 /* Compute the max bitsize of some of the classes of integers. It may
867 be that there are needs for the other integer classes, and this
868 code is easy to extend. */
869 static void
870 emit_max_int (void)
872 unsigned int max, mmax;
873 struct mode_data *i;
874 int j;
876 puts ("");
877 for (max = 1, i = modes[MODE_INT]; i; i = i->next)
878 if (max < i->bytesize)
879 max = i->bytesize;
880 mmax = max;
881 for (max = 1, i = modes[MODE_PARTIAL_INT]; i; i = i->next)
882 if (max < i->bytesize)
883 max = i->bytesize;
884 if (max > mmax)
885 mmax = max;
886 printf ("#define MAX_BITSIZE_MODE_ANY_INT %d*BITS_PER_UNIT\n", mmax);
888 mmax = 0;
889 for (j = 0; j < MAX_MODE_CLASS; j++)
890 for (i = modes[j]; i; i = i->next)
891 if (mmax < i->bytesize)
892 mmax = i->bytesize;
893 printf ("#define MAX_BITSIZE_MODE_ANY_MODE %d*BITS_PER_UNIT\n", mmax);
896 static void
897 emit_insn_modes_h (void)
899 int c;
900 struct mode_data *m, *first, *last;
902 printf ("/* Generated automatically from machmode.def%s%s\n",
903 HAVE_EXTRA_MODES ? " and " : "",
904 EXTRA_MODES_FILE);
906 puts ("\
907 by genmodes. */\n\
909 #ifndef GCC_INSN_MODES_H\n\
910 #define GCC_INSN_MODES_H\n\
912 enum machine_mode\n{");
914 for (c = 0; c < MAX_MODE_CLASS; c++)
915 for (m = modes[c]; m; m = m->next)
917 int count_ = printf (" %smode,", m->name);
918 printf ("%*s/* %s:%d */\n", 27 - count_, "",
919 trim_filename (m->file), m->line);
922 puts (" MAX_MACHINE_MODE,\n");
924 for (c = 0; c < MAX_MODE_CLASS; c++)
926 first = modes[c];
927 last = 0;
928 for (m = first; m; last = m, m = m->next)
931 /* Don't use BImode for MIN_MODE_INT, since otherwise the middle
932 end will try to use it for bitfields in structures and the
933 like, which we do not want. Only the target md file should
934 generate BImode widgets. */
935 if (first && first->precision == 1 && c == MODE_INT)
936 first = first->next;
938 if (first && last)
939 printf (" MIN_%s = %smode,\n MAX_%s = %smode,\n\n",
940 mode_class_names[c], first->name,
941 mode_class_names[c], last->name);
942 else
943 printf (" MIN_%s = %smode,\n MAX_%s = %smode,\n\n",
944 mode_class_names[c], void_mode->name,
945 mode_class_names[c], void_mode->name);
948 puts ("\
949 NUM_MACHINE_MODES = MAX_MACHINE_MODE\n\
950 };\n");
952 /* I can't think of a better idea, can you? */
953 printf ("#define CONST_MODE_SIZE%s\n", adj_bytesize ? "" : " const");
954 printf ("#define CONST_MODE_BASE_ALIGN%s\n", adj_alignment ? "" : " const");
955 #if 0 /* disabled for backward compatibility, temporary */
956 printf ("#define CONST_REAL_FORMAT_FOR_MODE%s\n", adj_format ? "" :" const");
957 #endif
958 printf ("#define CONST_MODE_IBIT%s\n", adj_ibit ? "" : " const");
959 printf ("#define CONST_MODE_FBIT%s\n", adj_fbit ? "" : " const");
960 emit_max_int ();
961 puts ("\
963 #endif /* insn-modes.h */");
966 static void
967 emit_insn_modes_c_header (void)
969 printf ("/* Generated automatically from machmode.def%s%s\n",
970 HAVE_EXTRA_MODES ? " and " : "",
971 EXTRA_MODES_FILE);
973 puts ("\
974 by genmodes. */\n\
976 #include \"config.h\"\n\
977 #include \"system.h\"\n\
978 #include \"coretypes.h\"\n\
979 #include \"tm.h\"\n\
980 #include \"machmode.h\"\n\
981 #include \"real.h\"");
984 static void
985 emit_min_insn_modes_c_header (void)
987 printf ("/* Generated automatically from machmode.def%s%s\n",
988 HAVE_EXTRA_MODES ? " and " : "",
989 EXTRA_MODES_FILE);
991 puts ("\
992 by genmodes. */\n\
994 #include \"bconfig.h\"\n\
995 #include \"system.h\"\n\
996 #include \"machmode.h\"");
999 static void
1000 emit_mode_name (void)
1002 int c;
1003 struct mode_data *m;
1005 print_decl ("char *const", "mode_name", "NUM_MACHINE_MODES");
1007 for_all_modes (c, m)
1008 printf (" \"%s\",\n", m->name);
1010 print_closer ();
1013 static void
1014 emit_mode_class (void)
1016 int c;
1017 struct mode_data *m;
1019 print_decl ("unsigned char", "mode_class", "NUM_MACHINE_MODES");
1021 for_all_modes (c, m)
1022 tagged_printf ("%s", mode_class_names[m->cl], m->name);
1024 print_closer ();
1027 static void
1028 emit_mode_precision (void)
1030 int c;
1031 struct mode_data *m;
1033 print_decl ("unsigned short", "mode_precision", "NUM_MACHINE_MODES");
1035 for_all_modes (c, m)
1036 if (m->precision != (unsigned int)-1)
1037 tagged_printf ("%u", m->precision, m->name);
1038 else
1039 tagged_printf ("%u*BITS_PER_UNIT", m->bytesize, m->name);
1041 print_closer ();
1044 static void
1045 emit_mode_size (void)
1047 int c;
1048 struct mode_data *m;
1050 print_maybe_const_decl ("%sunsigned char", "mode_size",
1051 "NUM_MACHINE_MODES", bytesize);
1053 for_all_modes (c, m)
1054 tagged_printf ("%u", m->bytesize, m->name);
1056 print_closer ();
1059 static void
1060 emit_mode_nunits (void)
1062 int c;
1063 struct mode_data *m;
1065 print_decl ("unsigned char", "mode_nunits", "NUM_MACHINE_MODES");
1067 for_all_modes (c, m)
1068 tagged_printf ("%u", m->ncomponents, m->name);
1070 print_closer ();
1073 static void
1074 emit_mode_wider (void)
1076 int c;
1077 struct mode_data *m;
1079 print_decl ("unsigned char", "mode_wider", "NUM_MACHINE_MODES");
1081 for_all_modes (c, m)
1082 tagged_printf ("%smode",
1083 m->wider ? m->wider->name : void_mode->name,
1084 m->name);
1086 print_closer ();
1087 print_decl ("unsigned char", "mode_2xwider", "NUM_MACHINE_MODES");
1089 for_all_modes (c, m)
1091 struct mode_data * m2;
1093 for (m2 = m;
1094 m2 && m2 != void_mode;
1095 m2 = m2->wider)
1097 if (m2->bytesize < 2 * m->bytesize)
1098 continue;
1099 if (m->precision != (unsigned int) -1)
1101 if (m2->precision != 2 * m->precision)
1102 continue;
1104 else
1106 if (m2->precision != (unsigned int) -1)
1107 continue;
1110 /* For vectors we want twice the number of components,
1111 with the same element type. */
1112 if (m->cl == MODE_VECTOR_INT
1113 || m->cl == MODE_VECTOR_FLOAT
1114 || m->cl == MODE_VECTOR_FRACT
1115 || m->cl == MODE_VECTOR_UFRACT
1116 || m->cl == MODE_VECTOR_ACCUM
1117 || m->cl == MODE_VECTOR_UACCUM)
1119 if (m2->ncomponents != 2 * m->ncomponents)
1120 continue;
1121 if (m->component != m2->component)
1122 continue;
1125 break;
1127 if (m2 == void_mode)
1128 m2 = 0;
1129 tagged_printf ("%smode",
1130 m2 ? m2->name : void_mode->name,
1131 m->name);
1134 print_closer ();
1137 static void
1138 emit_mode_mask (void)
1140 int c;
1141 struct mode_data *m;
1143 print_decl ("unsigned HOST_WIDE_INT", "mode_mask_array",
1144 "NUM_MACHINE_MODES");
1145 puts ("\
1146 #define MODE_MASK(m) \\\n\
1147 ((m) >= HOST_BITS_PER_WIDE_INT) \\\n\
1148 ? ~(unsigned HOST_WIDE_INT) 0 \\\n\
1149 : ((unsigned HOST_WIDE_INT) 1 << (m)) - 1\n");
1151 for_all_modes (c, m)
1152 if (m->precision != (unsigned int)-1)
1153 tagged_printf ("MODE_MASK (%u)", m->precision, m->name);
1154 else
1155 tagged_printf ("MODE_MASK (%u*BITS_PER_UNIT)", m->bytesize, m->name);
1157 puts ("#undef MODE_MASK");
1158 print_closer ();
1161 static void
1162 emit_mode_inner (void)
1164 int c;
1165 struct mode_data *m;
1167 print_decl ("unsigned char", "mode_inner", "NUM_MACHINE_MODES");
1169 for_all_modes (c, m)
1170 tagged_printf ("%smode",
1171 c != MODE_PARTIAL_INT && m->component
1172 ? m->component->name : void_mode->name,
1173 m->name);
1175 print_closer ();
1178 static void
1179 emit_mode_base_align (void)
1181 int c;
1182 struct mode_data *m;
1184 print_maybe_const_decl ("%sunsigned char",
1185 "mode_base_align", "NUM_MACHINE_MODES",
1186 alignment);
1188 for_all_modes (c, m)
1189 tagged_printf ("%u", m->alignment, m->name);
1191 print_closer ();
1194 static void
1195 emit_class_narrowest_mode (void)
1197 int c;
1199 print_decl ("unsigned char", "class_narrowest_mode", "MAX_MODE_CLASS");
1201 for (c = 0; c < MAX_MODE_CLASS; c++)
1202 /* Bleah, all this to get the comment right for MIN_MODE_INT. */
1203 tagged_printf ("MIN_%s", mode_class_names[c],
1204 modes[c]
1205 ? ((c != MODE_INT || modes[c]->precision != 1)
1206 ? modes[c]->name
1207 : (modes[c]->next
1208 ? modes[c]->next->name
1209 : void_mode->name))
1210 : void_mode->name);
1212 print_closer ();
1215 static void
1216 emit_real_format_for_mode (void)
1218 struct mode_data *m;
1220 /* The entities pointed to by this table are constant, whether
1221 or not the table itself is constant.
1223 For backward compatibility this table is always writable
1224 (several targets modify it in TARGET_OPTION_OVERRIDE). FIXME:
1225 convert all said targets to use ADJUST_FORMAT instead. */
1226 #if 0
1227 print_maybe_const_decl ("const struct real_format *%s",
1228 "real_format_for_mode",
1229 "MAX_MODE_FLOAT - MIN_MODE_FLOAT + 1",
1230 format);
1231 #else
1232 print_decl ("struct real_format *\n", "real_format_for_mode",
1233 "MAX_MODE_FLOAT - MIN_MODE_FLOAT + 1 "
1234 "+ MAX_MODE_DECIMAL_FLOAT - MIN_MODE_DECIMAL_FLOAT + 1");
1235 #endif
1237 /* The beginning of the table is entries for float modes. */
1238 for (m = modes[MODE_FLOAT]; m; m = m->next)
1239 if (!strcmp (m->format, "0"))
1240 tagged_printf ("%s", m->format, m->name);
1241 else
1242 tagged_printf ("&%s", m->format, m->name);
1244 /* The end of the table is entries for decimal float modes. */
1245 for (m = modes[MODE_DECIMAL_FLOAT]; m; m = m->next)
1246 if (!strcmp (m->format, "0"))
1247 tagged_printf ("%s", m->format, m->name);
1248 else
1249 tagged_printf ("&%s", m->format, m->name);
1251 print_closer ();
1254 static void
1255 emit_mode_adjustments (void)
1257 struct mode_adjust *a;
1258 struct mode_data *m;
1260 puts ("\
1261 \nvoid\
1262 \ninit_adjust_machine_modes (void)\
1263 \n{\
1264 \n size_t s ATTRIBUTE_UNUSED;");
1266 /* Size adjustments must be propagated to all containing modes.
1267 A size adjustment forces us to recalculate the alignment too. */
1268 for (a = adj_bytesize; a; a = a->next)
1270 printf ("\n /* %s:%d */\n s = %s;\n",
1271 a->file, a->line, a->adjustment);
1272 printf (" mode_size[%smode] = s;\n", a->mode->name);
1273 printf (" mode_base_align[%smode] = s & (~s + 1);\n",
1274 a->mode->name);
1276 for (m = a->mode->contained; m; m = m->next_cont)
1278 switch (m->cl)
1280 case MODE_COMPLEX_INT:
1281 case MODE_COMPLEX_FLOAT:
1282 printf (" mode_size[%smode] = 2*s;\n", m->name);
1283 printf (" mode_base_align[%smode] = s & (~s + 1);\n",
1284 m->name);
1285 break;
1287 case MODE_VECTOR_INT:
1288 case MODE_VECTOR_FLOAT:
1289 case MODE_VECTOR_FRACT:
1290 case MODE_VECTOR_UFRACT:
1291 case MODE_VECTOR_ACCUM:
1292 case MODE_VECTOR_UACCUM:
1293 printf (" mode_size[%smode] = %d*s;\n",
1294 m->name, m->ncomponents);
1295 printf (" mode_base_align[%smode] = (%d*s) & (~(%d*s)+1);\n",
1296 m->name, m->ncomponents, m->ncomponents);
1297 break;
1299 default:
1300 internal_error (
1301 "mode %s is neither vector nor complex but contains %s",
1302 m->name, a->mode->name);
1303 /* NOTREACHED */
1308 /* Alignment adjustments propagate too.
1309 ??? This may not be the right thing for vector modes. */
1310 for (a = adj_alignment; a; a = a->next)
1312 printf ("\n /* %s:%d */\n s = %s;\n",
1313 a->file, a->line, a->adjustment);
1314 printf (" mode_base_align[%smode] = s;\n", a->mode->name);
1316 for (m = a->mode->contained; m; m = m->next_cont)
1318 switch (m->cl)
1320 case MODE_COMPLEX_INT:
1321 case MODE_COMPLEX_FLOAT:
1322 printf (" mode_base_align[%smode] = s;\n", m->name);
1323 break;
1325 case MODE_VECTOR_INT:
1326 case MODE_VECTOR_FLOAT:
1327 case MODE_VECTOR_FRACT:
1328 case MODE_VECTOR_UFRACT:
1329 case MODE_VECTOR_ACCUM:
1330 case MODE_VECTOR_UACCUM:
1331 printf (" mode_base_align[%smode] = %d*s;\n",
1332 m->name, m->ncomponents);
1333 break;
1335 default:
1336 internal_error (
1337 "mode %s is neither vector nor complex but contains %s",
1338 m->name, a->mode->name);
1339 /* NOTREACHED */
1344 /* Ibit adjustments don't have to propagate. */
1345 for (a = adj_ibit; a; a = a->next)
1347 printf ("\n /* %s:%d */\n s = %s;\n",
1348 a->file, a->line, a->adjustment);
1349 printf (" mode_ibit[%smode] = s;\n", a->mode->name);
1352 /* Fbit adjustments don't have to propagate. */
1353 for (a = adj_fbit; a; a = a->next)
1355 printf ("\n /* %s:%d */\n s = %s;\n",
1356 a->file, a->line, a->adjustment);
1357 printf (" mode_fbit[%smode] = s;\n", a->mode->name);
1360 /* Real mode formats don't have to propagate anywhere. */
1361 for (a = adj_format; a; a = a->next)
1362 printf ("\n /* %s:%d */\n REAL_MODE_FORMAT (%smode) = %s;\n",
1363 a->file, a->line, a->mode->name, a->adjustment);
1365 puts ("}");
1368 /* Emit ibit for all modes. */
1370 static void
1371 emit_mode_ibit (void)
1373 int c;
1374 struct mode_data *m;
1376 print_maybe_const_decl ("%sunsigned char",
1377 "mode_ibit", "NUM_MACHINE_MODES",
1378 ibit);
1380 for_all_modes (c, m)
1381 tagged_printf ("%u", m->ibit, m->name);
1383 print_closer ();
1386 /* Emit fbit for all modes. */
1388 static void
1389 emit_mode_fbit (void)
1391 int c;
1392 struct mode_data *m;
1394 print_maybe_const_decl ("%sunsigned char",
1395 "mode_fbit", "NUM_MACHINE_MODES",
1396 fbit);
1398 for_all_modes (c, m)
1399 tagged_printf ("%u", m->fbit, m->name);
1401 print_closer ();
1405 static void
1406 emit_insn_modes_c (void)
1408 emit_insn_modes_c_header ();
1409 emit_mode_name ();
1410 emit_mode_class ();
1411 emit_mode_precision ();
1412 emit_mode_size ();
1413 emit_mode_nunits ();
1414 emit_mode_wider ();
1415 emit_mode_mask ();
1416 emit_mode_inner ();
1417 emit_mode_base_align ();
1418 emit_class_narrowest_mode ();
1419 emit_real_format_for_mode ();
1420 emit_mode_adjustments ();
1421 emit_mode_ibit ();
1422 emit_mode_fbit ();
1425 static void
1426 emit_min_insn_modes_c (void)
1428 emit_min_insn_modes_c_header ();
1429 emit_mode_name ();
1430 emit_mode_class ();
1431 emit_mode_wider ();
1432 emit_class_narrowest_mode ();
1435 /* Master control. */
1437 main (int argc, char **argv)
1439 bool gen_header = false, gen_min = false;
1440 progname = argv[0];
1442 if (argc == 1)
1444 else if (argc == 2 && !strcmp (argv[1], "-h"))
1445 gen_header = true;
1446 else if (argc == 2 && !strcmp (argv[1], "-m"))
1447 gen_min = true;
1448 else
1450 error ("usage: %s [-h|-m] > file", progname);
1451 return FATAL_EXIT_CODE;
1454 modes_by_name = htab_create_alloc (64, hash_mode, eq_mode, 0, xcalloc, free);
1456 create_modes ();
1457 complete_all_modes ();
1459 if (have_error)
1460 return FATAL_EXIT_CODE;
1462 calc_wider_mode ();
1464 if (gen_header)
1465 emit_insn_modes_h ();
1466 else if (gen_min)
1467 emit_min_insn_modes_c ();
1468 else
1469 emit_insn_modes_c ();
1471 if (fflush (stdout) || fclose (stdout))
1472 return FATAL_EXIT_CODE;
1473 return SUCCESS_EXIT_CODE;