2004-12-17 Andreas Krebbel <krebbel1@de.ibm.com>
[official-gcc.git] / gcc / genmodes.c
blob6e282f36dd9cf995cb0b146f7371187826ca2b7d
1 /* Generate the machine mode enumeration and associated tables.
2 Copyright (C) 2003, 2004
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 2, 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 COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA. */
22 #include "bconfig.h"
23 #include "system.h"
24 #include "errors.h"
25 #include "hashtab.h"
27 /* enum mode_class is normally defined by machmode.h but we can't
28 include that header here. */
29 #include "mode-classes.def"
31 #define DEF_MODE_CLASS(M) M
32 enum mode_class { MODE_CLASSES, MAX_MODE_CLASS };
33 #undef DEF_MODE_CLASS
35 /* Text names of mode classes, for output. */
36 #define DEF_MODE_CLASS(M) #M
37 static const char *const mode_class_names[MAX_MODE_CLASS] =
39 MODE_CLASSES
41 #undef DEF_MODE_CLASS
42 #undef MODE_CLASSES
44 #ifdef EXTRA_MODES_FILE
45 # define HAVE_EXTRA_MODES 1
46 #else
47 # define HAVE_EXTRA_MODES 0
48 # define EXTRA_MODES_FILE ""
49 #endif
51 /* Data structure for building up what we know about a mode.
52 They're clustered by mode class. */
53 struct mode_data
55 struct mode_data *next; /* next this class - arbitrary order */
57 const char *name; /* printable mode name -- SI, not SImode */
58 enum mode_class cl; /* this mode class */
59 unsigned int precision; /* size in bits, equiv to TYPE_PRECISION */
60 unsigned int bytesize; /* storage size in addressable units */
61 unsigned int ncomponents; /* number of subunits */
62 unsigned int alignment; /* mode alignment */
63 const char *format; /* floating point format - MODE_FLOAT only */
65 struct mode_data *component; /* mode of components */
66 struct mode_data *wider; /* next wider mode */
68 struct mode_data *contained; /* Pointer to list of modes that have
69 this mode as a component. */
70 struct mode_data *next_cont; /* Next mode in that list. */
72 const char *file; /* file and line of definition, */
73 unsigned int line; /* for error reporting */
76 static struct mode_data *modes[MAX_MODE_CLASS];
77 static unsigned int n_modes[MAX_MODE_CLASS];
78 static struct mode_data *void_mode;
80 static const struct mode_data blank_mode = {
81 0, "<unknown>", MAX_MODE_CLASS,
82 -1U, -1U, -1U, -1U,
83 0, 0, 0, 0, 0,
84 "<unknown>", 0
87 static htab_t modes_by_name;
89 /* Data structure for recording target-specified runtime adjustments
90 to a particular mode. We support varying the byte size, the
91 alignment, and the floating point format. */
92 struct mode_adjust
94 struct mode_adjust *next;
95 struct mode_data *mode;
96 const char *adjustment;
98 const char *file;
99 unsigned int line;
102 static struct mode_adjust *adj_bytesize;
103 static struct mode_adjust *adj_alignment;
104 static struct mode_adjust *adj_format;
106 /* Mode class operations. */
107 static enum mode_class
108 complex_class (enum mode_class c)
110 switch (c)
112 case MODE_INT: return MODE_COMPLEX_INT;
113 case MODE_FLOAT: return MODE_COMPLEX_FLOAT;
114 default:
115 error ("no complex class for class %s", mode_class_names[c]);
116 return MODE_RANDOM;
120 static enum mode_class
121 vector_class (enum mode_class cl)
123 switch (cl)
125 case MODE_INT: return MODE_VECTOR_INT;
126 case MODE_FLOAT: return MODE_VECTOR_FLOAT;
127 default:
128 error ("no vector class for class %s", mode_class_names[cl]);
129 return MODE_RANDOM;
133 /* Utility routines. */
134 static inline struct mode_data *
135 find_mode (const char *name)
137 struct mode_data key;
139 key.name = name;
140 return (struct mode_data *) htab_find (modes_by_name, &key);
143 static struct mode_data *
144 new_mode (enum mode_class cl, const char *name,
145 const char *file, unsigned int line)
147 struct mode_data *m;
149 m = find_mode (name);
150 if (m)
152 error ("%s:%d: duplicate definition of mode \"%s\"",
153 trim_filename (file), line, name);
154 error ("%s:%d: previous definition here", m->file, m->line);
155 return m;
158 m = XNEW (struct mode_data);
159 memcpy (m, &blank_mode, sizeof (struct mode_data));
160 m->cl = cl;
161 m->name = name;
162 if (file)
163 m->file = trim_filename (file);
164 m->line = line;
166 m->next = modes[cl];
167 modes[cl] = m;
168 n_modes[cl]++;
170 *htab_find_slot (modes_by_name, m, INSERT) = m;
172 return m;
175 static hashval_t
176 hash_mode (const void *p)
178 const struct mode_data *m = (const struct mode_data *)p;
179 return htab_hash_string (m->name);
182 static int
183 eq_mode (const void *p, const void *q)
185 const struct mode_data *a = (const struct mode_data *)p;
186 const struct mode_data *b = (const struct mode_data *)q;
188 return !strcmp (a->name, b->name);
191 #define for_all_modes(C, M) \
192 for (C = 0; C < MAX_MODE_CLASS; C++) \
193 for (M = modes[C]; M; M = M->next)
195 static void ATTRIBUTE_UNUSED
196 new_adjust (const char *name,
197 struct mode_adjust **category, const char *catname,
198 const char *adjustment,
199 enum mode_class required_class,
200 const char *file, unsigned int line)
202 struct mode_data *mode = find_mode (name);
203 struct mode_adjust *a;
205 file = trim_filename (file);
207 if (!mode)
209 error ("%s:%d: no mode \"%s\"", file, line, name);
210 return;
213 if (required_class != MODE_RANDOM && mode->cl != required_class)
215 error ("%s:%d: mode \"%s\" is not class %s",
216 file, line, name, mode_class_names[required_class] + 5);
217 return;
220 for (a = *category; a; a = a->next)
221 if (a->mode == mode)
223 error ("%s:%d: mode \"%s\" already has a %s adjustment",
224 file, line, name, catname);
225 error ("%s:%d: previous adjustment here", a->file, a->line);
226 return;
229 a = XNEW (struct mode_adjust);
230 a->mode = mode;
231 a->adjustment = adjustment;
232 a->file = file;
233 a->line = line;
235 a->next = *category;
236 *category = a;
239 /* Diagnose failure to meet expectations in a partially filled out
240 mode structure. */
241 enum requirement { SET, UNSET, OPTIONAL };
243 #define validate_field_(mname, fname, req, val, unset, file, line) do { \
244 switch (req) \
246 case SET: \
247 if (val == unset) \
248 error ("%s:%d: (%s) field %s must be set", \
249 file, line, mname, fname); \
250 break; \
251 case UNSET: \
252 if (val != unset) \
253 error ("%s:%d: (%s) field %s must not be set", \
254 file, line, mname, fname); \
255 case OPTIONAL: \
256 break; \
258 } while (0)
260 #define validate_field(M, F) \
261 validate_field_(M->name, #F, r_##F, M->F, blank_mode.F, M->file, M->line)
263 static void
264 validate_mode (struct mode_data *m,
265 enum requirement r_precision,
266 enum requirement r_bytesize,
267 enum requirement r_component,
268 enum requirement r_ncomponents,
269 enum requirement r_format)
271 validate_field (m, precision);
272 validate_field (m, bytesize);
273 validate_field (m, component);
274 validate_field (m, ncomponents);
275 validate_field (m, format);
277 #undef validate_field
278 #undef validate_field_
280 /* Given a partially-filled-out mode structure, figure out what we can
281 and fill the rest of it in; die if it isn't enough. */
282 static void
283 complete_mode (struct mode_data *m)
285 unsigned int alignment;
287 if (!m->name)
289 error ("%s:%d: mode with no name", m->file, m->line);
290 return;
292 if (m->cl == MAX_MODE_CLASS)
294 error ("%s:%d: %smode has no mode class", m->file, m->line, m->name);
295 return;
298 switch (m->cl)
300 case MODE_RANDOM:
301 /* Nothing more need be said. */
302 if (!strcmp (m->name, "VOID"))
303 void_mode = m;
305 validate_mode (m, UNSET, UNSET, UNSET, UNSET, UNSET);
307 m->precision = 0;
308 m->bytesize = 0;
309 m->ncomponents = 0;
310 m->component = 0;
311 break;
313 case MODE_CC:
314 /* Again, nothing more need be said. For historical reasons,
315 the size of a CC mode is four units. */
316 validate_mode (m, UNSET, UNSET, UNSET, UNSET, UNSET);
318 m->bytesize = 4;
319 m->ncomponents = 1;
320 m->component = 0;
321 break;
323 case MODE_INT:
324 case MODE_FLOAT:
325 /* A scalar mode must have a byte size, may have a bit size,
326 and must not have components. A float mode must have a
327 format. */
328 validate_mode (m, OPTIONAL, SET, UNSET, UNSET,
329 m->cl == MODE_FLOAT ? SET : UNSET);
331 m->ncomponents = 1;
332 m->component = 0;
333 break;
335 case MODE_PARTIAL_INT:
336 /* A partial integer mode uses ->component to say what the
337 corresponding full-size integer mode is, and may also
338 specify a bit size. */
339 validate_mode (m, OPTIONAL, UNSET, SET, UNSET, UNSET);
341 m->bytesize = m->component->bytesize;
343 m->ncomponents = 1;
344 m->component = 0; /* ??? preserve this */
345 break;
347 case MODE_COMPLEX_INT:
348 case MODE_COMPLEX_FLOAT:
349 /* Complex modes should have a component indicated, but no more. */
350 validate_mode (m, UNSET, UNSET, SET, UNSET, UNSET);
351 m->ncomponents = 2;
352 if (m->component->precision != (unsigned int)-1)
353 m->precision = 2 * m->component->precision;
354 m->bytesize = 2 * m->component->bytesize;
355 break;
357 case MODE_VECTOR_INT:
358 case MODE_VECTOR_FLOAT:
359 /* Vector modes should have a component and a number of components. */
360 validate_mode (m, UNSET, UNSET, SET, SET, UNSET);
361 if (m->component->precision != (unsigned int)-1)
362 m->precision = m->ncomponents * m->component->precision;
363 m->bytesize = m->ncomponents * m->component->bytesize;
364 break;
366 default:
367 gcc_unreachable ();
370 /* If not already specified, the mode alignment defaults to the largest
371 power of two that divides the size of the object. Complex types are
372 not more aligned than their contents. */
373 if (m->cl == MODE_COMPLEX_INT || m->cl == MODE_COMPLEX_FLOAT)
374 alignment = m->component->bytesize;
375 else
376 alignment = m->bytesize;
378 m->alignment = alignment & (~alignment + 1);
380 /* If this mode has components, make the component mode point back
381 to this mode, for the sake of adjustments. */
382 if (m->component)
384 m->next_cont = m->component->contained;
385 m->component->contained = m;
389 static void
390 complete_all_modes (void)
392 struct mode_data *m;
393 int cl;
395 for_all_modes (cl, m)
396 complete_mode (m);
399 /* For each mode in class CLASS, construct a corresponding complex mode. */
400 #define COMPLEX_MODES(C) make_complex_modes(MODE_##C, __FILE__, __LINE__)
401 static void
402 make_complex_modes (enum mode_class cl,
403 const char *file, unsigned int line)
405 struct mode_data *m;
406 struct mode_data *c;
407 char buf[8];
408 enum mode_class cclass = complex_class (cl);
410 if (cclass == MODE_RANDOM)
411 return;
413 for (m = modes[cl]; m; m = m->next)
415 /* Skip BImode. FIXME: BImode probably shouldn't be MODE_INT. */
416 if (m->precision == 1)
417 continue;
419 if (strlen (m->name) >= sizeof buf)
421 error ("%s:%d:mode name \"%s\" is too long",
422 m->file, m->line, m->name);
423 continue;
426 /* Float complex modes are named SCmode, etc.
427 Int complex modes are named CSImode, etc.
428 This inconsistency should be eliminated. */
429 if (cl == MODE_FLOAT)
431 char *p;
432 strncpy (buf, m->name, sizeof buf);
433 p = strchr (buf, 'F');
434 if (p == 0)
436 error ("%s:%d: float mode \"%s\" has no 'F'",
437 m->file, m->line, m->name);
438 continue;
441 *p = 'C';
443 else
444 snprintf (buf, sizeof buf, "C%s", m->name);
446 c = new_mode (cclass, xstrdup (buf), file, line);
447 c->component = m;
451 /* For all modes in class CL, construct vector modes of width
452 WIDTH, having as many components as necessary. */
453 #define VECTOR_MODES(C, W) make_vector_modes(MODE_##C, W, __FILE__, __LINE__)
454 static void ATTRIBUTE_UNUSED
455 make_vector_modes (enum mode_class cl, unsigned int width,
456 const char *file, unsigned int line)
458 struct mode_data *m;
459 struct mode_data *v;
460 char buf[8];
461 unsigned int ncomponents;
462 enum mode_class vclass = vector_class (cl);
464 if (vclass == MODE_RANDOM)
465 return;
467 for (m = modes[cl]; m; m = m->next)
469 /* Do not construct vector modes with only one element, or
470 vector modes where the element size doesn't divide the full
471 size evenly. */
472 ncomponents = width / m->bytesize;
473 if (ncomponents < 2)
474 continue;
475 if (width % m->bytesize)
476 continue;
478 /* Skip QFmode and BImode. FIXME: this special case should
479 not be necessary. */
480 if (cl == MODE_FLOAT && m->bytesize == 1)
481 continue;
482 if (cl == MODE_INT && m->precision == 1)
483 continue;
485 if ((size_t)snprintf (buf, sizeof buf, "V%u%s", ncomponents, m->name)
486 >= sizeof buf)
488 error ("%s:%d: mode name \"%s\" is too long",
489 m->file, m->line, m->name);
490 continue;
493 v = new_mode (vclass, xstrdup (buf), file, line);
494 v->component = m;
495 v->ncomponents = ncomponents;
499 /* Input. */
501 #define _SPECIAL_MODE(C, N) make_special_mode(MODE_##C, #N, __FILE__, __LINE__)
502 #define RANDOM_MODE(N) _SPECIAL_MODE (RANDOM, N)
503 #define CC_MODE(N) _SPECIAL_MODE (CC, N)
505 static void
506 make_special_mode (enum mode_class cl, const char *name,
507 const char *file, unsigned int line)
509 new_mode (cl, name, file, line);
512 #define INT_MODE(N, Y) FRACTIONAL_INT_MODE (N, -1U, Y)
513 #define FRACTIONAL_INT_MODE(N, B, Y) \
514 make_int_mode (#N, B, Y, __FILE__, __LINE__)
516 static void
517 make_int_mode (const char *name,
518 unsigned int precision, unsigned int bytesize,
519 const char *file, unsigned int line)
521 struct mode_data *m = new_mode (MODE_INT, name, file, line);
522 m->bytesize = bytesize;
523 m->precision = precision;
526 #define FLOAT_MODE(N, Y, F) FRACTIONAL_FLOAT_MODE (N, -1U, Y, F)
527 #define FRACTIONAL_FLOAT_MODE(N, B, Y, F) \
528 make_float_mode (#N, B, Y, #F, __FILE__, __LINE__)
530 static void
531 make_float_mode (const char *name,
532 unsigned int precision, unsigned int bytesize,
533 const char *format,
534 const char *file, unsigned int line)
536 struct mode_data *m = new_mode (MODE_FLOAT, name, file, line);
537 m->bytesize = bytesize;
538 m->precision = precision;
539 m->format = format;
542 #define RESET_FLOAT_FORMAT(N, F) \
543 reset_float_format (#N, #F, __FILE__, __LINE__)
544 static void ATTRIBUTE_UNUSED
545 reset_float_format (const char *name, const char *format,
546 const char *file, unsigned int line)
548 struct mode_data *m = find_mode (name);
549 if (!m)
551 error ("%s:%d: no mode \"%s\"", file, line, name);
552 return;
554 if (m->cl != MODE_FLOAT)
556 error ("%s:%d: mode \"%s\" is not class FLOAT", file, line, name);
557 return;
559 m->format = format;
562 /* Partial integer modes are specified by relation to a full integer mode.
563 For now, we do not attempt to narrow down their bit sizes. */
564 #define PARTIAL_INT_MODE(M) \
565 make_partial_integer_mode (#M, "P" #M, -1U, __FILE__, __LINE__)
566 static void ATTRIBUTE_UNUSED
567 make_partial_integer_mode (const char *base, const char *name,
568 unsigned int precision,
569 const char *file, unsigned int line)
571 struct mode_data *m;
572 struct mode_data *component = find_mode (base);
573 if (!component)
575 error ("%s:%d: no mode \"%s\"", file, line, name);
576 return;
578 if (component->cl != MODE_INT)
580 error ("%s:%d: mode \"%s\" is not class INT", file, line, name);
581 return;
584 m = new_mode (MODE_PARTIAL_INT, name, file, line);
585 m->precision = precision;
586 m->component = component;
589 /* A single vector mode can be specified by naming its component
590 mode and the number of components. */
591 #define VECTOR_MODE(C, M, N) \
592 make_vector_mode (MODE_##C, #M, N, __FILE__, __LINE__);
593 static void ATTRIBUTE_UNUSED
594 make_vector_mode (enum mode_class bclass,
595 const char *base,
596 unsigned int ncomponents,
597 const char *file, unsigned int line)
599 struct mode_data *v;
600 enum mode_class vclass = vector_class (bclass);
601 struct mode_data *component = find_mode (base);
602 char namebuf[8];
604 if (vclass == MODE_RANDOM)
605 return;
606 if (component == 0)
608 error ("%s:%d: no mode \"%s\"", file, line, base);
609 return;
611 if (component->cl != bclass)
613 error ("%s:%d: mode \"%s\" is not class %s",
614 file, line, base, mode_class_names[bclass] + 5);
615 return;
618 if ((size_t)snprintf (namebuf, sizeof namebuf, "V%u%s",
619 ncomponents, base) >= sizeof namebuf)
621 error ("%s:%d: mode name \"%s\" is too long",
622 base, file, line);
623 return;
626 v = new_mode (vclass, xstrdup (namebuf), file, line);
627 v->ncomponents = ncomponents;
628 v->component = component;
631 /* Adjustability. */
632 #define _ADD_ADJUST(A, M, X, C) \
633 new_adjust (#M, &adj_##A, #A, #X, MODE_##C, __FILE__, __LINE__)
635 #define ADJUST_BYTESIZE(M, X) _ADD_ADJUST(bytesize, M, X, RANDOM)
636 #define ADJUST_ALIGNMENT(M, X) _ADD_ADJUST(alignment, M, X, RANDOM)
637 #define ADJUST_FLOAT_FORMAT(M, X) _ADD_ADJUST(format, M, X, FLOAT)
639 static void
640 create_modes (void)
642 #include "machmode.def"
645 /* Processing. */
647 /* Sort a list of modes into the order needed for the WIDER field:
648 major sort by precision, minor sort by component precision.
650 For instance:
651 QI < HI < SI < DI < TI
652 V4QI < V2HI < V8QI < V4HI < V2SI.
654 If the precision is not set, sort by the bytesize. A mode with
655 precision set gets sorted before a mode without precision set, if
656 they have the same bytesize; this is the right thing because
657 the precision must always be smaller than the bytesize * BITS_PER_UNIT.
658 We don't have to do anything special to get this done -- an unset
659 precision shows up as (unsigned int)-1, i.e. UINT_MAX. */
660 static int
661 cmp_modes (const void *a, const void *b)
663 struct mode_data *m = *(struct mode_data **)a;
664 struct mode_data *n = *(struct mode_data **)b;
666 if (m->bytesize > n->bytesize)
667 return 1;
668 else if (m->bytesize < n->bytesize)
669 return -1;
671 if (m->precision > n->precision)
672 return 1;
673 else if (m->precision < n->precision)
674 return -1;
676 if (!m->component && !n->component)
677 return 0;
679 if (m->component->bytesize > n->component->bytesize)
680 return 1;
681 else if (m->component->bytesize < n->component->bytesize)
682 return -1;
684 if (m->component->precision > n->component->precision)
685 return 1;
686 else if (m->component->precision < n->component->precision)
687 return -1;
689 return 0;
692 static void
693 calc_wider_mode (void)
695 int c;
696 struct mode_data *m;
697 struct mode_data **sortbuf;
698 unsigned int max_n_modes = 0;
699 unsigned int i, j;
701 for (c = 0; c < MAX_MODE_CLASS; c++)
702 max_n_modes = MAX (max_n_modes, n_modes[c]);
704 /* Allocate max_n_modes + 1 entries to leave room for the extra null
705 pointer assigned after the qsort call below. */
706 sortbuf = (struct mode_data **) alloca ((max_n_modes + 1) * sizeof (struct mode_data *));
708 for (c = 0; c < MAX_MODE_CLASS; c++)
710 /* "wider" is not meaningful for MODE_RANDOM and MODE_CC.
711 However, we want these in textual order, and we have
712 precisely the reverse. */
713 if (c == MODE_RANDOM || c == MODE_CC)
715 struct mode_data *prev, *next;
717 for (prev = 0, m = modes[c]; m; m = next)
719 m->wider = void_mode;
721 /* this is nreverse */
722 next = m->next;
723 m->next = prev;
724 prev = m;
726 modes[c] = prev;
728 else
730 if (!modes[c])
731 continue;
733 for (i = 0, m = modes[c]; m; i++, m = m->next)
734 sortbuf[i] = m;
736 qsort (sortbuf, i, sizeof (struct mode_data *), cmp_modes);
738 sortbuf[i] = 0;
739 for (j = 0; j < i; j++)
740 sortbuf[j]->next = sortbuf[j]->wider = sortbuf[j + 1];
743 modes[c] = sortbuf[0];
748 /* Output routines. */
750 #define tagged_printf(FMT, ARG, TAG) do { \
751 int count_; \
752 printf (" " FMT ",%n", ARG, &count_); \
753 printf ("%*s/* %s */\n", 27 - count_, "", TAG); \
754 } while (0)
756 #define print_decl(TYPE, NAME, ASIZE) \
757 puts ("\nconst " TYPE " " NAME "[" ASIZE "] =\n{");
759 #define print_maybe_const_decl(TYPE, NAME, ASIZE, CATEGORY) \
760 printf ("\n" TYPE " " NAME "[" ASIZE "] = \n{\n", \
761 adj_##CATEGORY ? "" : "const ")
763 #define print_closer() puts ("};")
765 static void
766 emit_insn_modes_h (void)
768 int c;
769 struct mode_data *m, *first, *last;
771 printf ("/* Generated automatically from machmode.def%s%s\n",
772 HAVE_EXTRA_MODES ? " and " : "",
773 EXTRA_MODES_FILE);
775 puts ("\
776 by genmodes. */\n\
778 #ifndef GCC_INSN_MODES_H\n\
779 #define GCC_INSN_MODES_H\n\
781 enum machine_mode\n{");
783 for (c = 0; c < MAX_MODE_CLASS; c++)
784 for (m = modes[c]; m; m = m->next)
786 int count_;
787 printf (" %smode,%n", m->name, &count_);
788 printf ("%*s/* %s:%d */\n", 27 - count_, "",
789 trim_filename (m->file), m->line);
792 puts (" MAX_MACHINE_MODE,\n");
794 for (c = 0; c < MAX_MODE_CLASS; c++)
796 first = modes[c];
797 last = 0;
798 for (m = first; m; last = m, m = m->next)
801 /* Don't use BImode for MIN_MODE_INT, since otherwise the middle
802 end will try to use it for bitfields in structures and the
803 like, which we do not want. Only the target md file should
804 generate BImode widgets. */
805 if (first && first->precision == 1)
806 first = first->next;
808 if (first && last)
809 printf (" MIN_%s = %smode,\n MAX_%s = %smode,\n\n",
810 mode_class_names[c], first->name,
811 mode_class_names[c], last->name);
812 else
813 printf (" MIN_%s = %smode,\n MAX_%s = %smode,\n\n",
814 mode_class_names[c], void_mode->name,
815 mode_class_names[c], void_mode->name);
818 puts ("\
819 NUM_MACHINE_MODES = MAX_MACHINE_MODE\n\
820 };\n");
822 /* I can't think of a better idea, can you? */
823 printf ("#define CONST_MODE_SIZE%s\n", adj_bytesize ? "" : " const");
824 printf ("#define CONST_MODE_BASE_ALIGN%s\n", adj_alignment ? "" : " const");
825 #if 0 /* disabled for backward compatibility, temporary */
826 printf ("#define CONST_REAL_FORMAT_FOR_MODE%s\n", adj_format ? "" :" const");
827 #endif
828 puts ("\
830 #endif /* insn-modes.h */");
833 static void
834 emit_insn_modes_c_header (void)
836 printf ("/* Generated automatically from machmode.def%s%s\n",
837 HAVE_EXTRA_MODES ? " and " : "",
838 EXTRA_MODES_FILE);
840 puts ("\
841 by genmodes. */\n\
843 #include \"config.h\"\n\
844 #include \"system.h\"\n\
845 #include \"coretypes.h\"\n\
846 #include \"tm.h\"\n\
847 #include \"machmode.h\"\n\
848 #include \"real.h\"");
851 static void
852 emit_min_insn_modes_c_header (void)
854 printf ("/* Generated automatically from machmode.def%s%s\n",
855 HAVE_EXTRA_MODES ? " and " : "",
856 EXTRA_MODES_FILE);
858 puts ("\
859 by genmodes. */\n\
861 #include \"bconfig.h\"\n\
862 #include \"system.h\"\n\
863 #include \"machmode.h\"");
866 static void
867 emit_mode_name (void)
869 int c;
870 struct mode_data *m;
872 print_decl ("char *const", "mode_name", "NUM_MACHINE_MODES");
874 for_all_modes (c, m)
875 printf (" \"%s\",\n", m->name);
877 print_closer ();
880 static void
881 emit_mode_class (void)
883 int c;
884 struct mode_data *m;
886 print_decl ("unsigned char", "mode_class", "NUM_MACHINE_MODES");
888 for_all_modes (c, m)
889 tagged_printf ("%s", mode_class_names[m->cl], m->name);
891 print_closer ();
894 static void
895 emit_mode_precision (void)
897 int c;
898 struct mode_data *m;
900 print_decl ("unsigned short", "mode_precision", "NUM_MACHINE_MODES");
902 for_all_modes (c, m)
903 if (m->precision != (unsigned int)-1)
904 tagged_printf ("%u", m->precision, m->name);
905 else
906 tagged_printf ("%u*BITS_PER_UNIT", m->bytesize, m->name);
908 print_closer ();
911 static void
912 emit_mode_size (void)
914 int c;
915 struct mode_data *m;
917 print_maybe_const_decl ("%sunsigned char", "mode_size",
918 "NUM_MACHINE_MODES", bytesize);
920 for_all_modes (c, m)
921 tagged_printf ("%u", m->bytesize, m->name);
923 print_closer ();
926 static void
927 emit_mode_nunits (void)
929 int c;
930 struct mode_data *m;
932 print_decl ("unsigned char", "mode_nunits", "NUM_MACHINE_MODES");
934 for_all_modes (c, m)
935 tagged_printf ("%u", m->ncomponents, m->name);
937 print_closer ();
940 static void
941 emit_mode_wider (void)
943 int c;
944 struct mode_data *m;
946 print_decl ("unsigned char", "mode_wider", "NUM_MACHINE_MODES");
948 for_all_modes (c, m)
949 tagged_printf ("%smode",
950 m->wider ? m->wider->name : void_mode->name,
951 m->name);
953 print_closer ();
956 static void
957 emit_mode_mask (void)
959 int c;
960 struct mode_data *m;
962 print_decl ("unsigned HOST_WIDE_INT", "mode_mask_array",
963 "NUM_MACHINE_MODES");
964 puts ("\
965 #define MODE_MASK(m) \\\n\
966 ((m) >= HOST_BITS_PER_WIDE_INT) \\\n\
967 ? ~(unsigned HOST_WIDE_INT) 0 \\\n\
968 : ((unsigned HOST_WIDE_INT) 1 << (m)) - 1\n");
970 for_all_modes (c, m)
971 if (m->precision != (unsigned int)-1)
972 tagged_printf ("MODE_MASK (%u)", m->precision, m->name);
973 else
974 tagged_printf ("MODE_MASK (%u*BITS_PER_UNIT)", m->bytesize, m->name);
976 puts ("#undef MODE_MASK");
977 print_closer ();
980 static void
981 emit_mode_inner (void)
983 int c;
984 struct mode_data *m;
986 print_decl ("unsigned char", "mode_inner", "NUM_MACHINE_MODES");
988 for_all_modes (c, m)
989 tagged_printf ("%smode",
990 m->component ? m->component->name : void_mode->name,
991 m->name);
993 print_closer ();
996 static void
997 emit_mode_base_align (void)
999 int c;
1000 struct mode_data *m;
1002 print_maybe_const_decl ("%sunsigned char",
1003 "mode_base_align", "NUM_MACHINE_MODES",
1004 alignment);
1006 for_all_modes (c, m)
1007 tagged_printf ("%u", m->alignment, m->name);
1009 print_closer ();
1012 static void
1013 emit_class_narrowest_mode (void)
1015 int c;
1017 print_decl ("unsigned char", "class_narrowest_mode", "MAX_MODE_CLASS");
1019 for (c = 0; c < MAX_MODE_CLASS; c++)
1020 /* Bleah, all this to get the comment right for MIN_MODE_INT. */
1021 tagged_printf ("MIN_%s", mode_class_names[c],
1022 modes[c]
1023 ? (modes[c]->precision != 1
1024 ? modes[c]->name
1025 : (modes[c]->next
1026 ? modes[c]->next->name
1027 : void_mode->name))
1028 : void_mode->name);
1030 print_closer ();
1033 static void
1034 emit_real_format_for_mode (void)
1036 struct mode_data *m;
1038 /* The entities pointed to by this table are constant, whether
1039 or not the table itself is constant.
1041 For backward compatibility this table is always writable
1042 (several targets modify it in OVERRIDE_OPTIONS). FIXME:
1043 convert all said targets to use ADJUST_FORMAT instead. */
1044 #if 0
1045 print_maybe_const_decl ("const struct real_format *%s",
1046 "real_format_for_mode",
1047 "MAX_MODE_FLOAT - MIN_MODE_FLOAT + 1",
1048 format);
1049 #else
1050 print_decl ("struct real_format *\n", "real_format_for_mode",
1051 "MAX_MODE_FLOAT - MIN_MODE_FLOAT + 1");
1052 #endif
1054 for (m = modes[MODE_FLOAT]; m; m = m->next)
1055 if (!strcmp (m->format, "0"))
1056 tagged_printf ("%s", m->format, m->name);
1057 else
1058 tagged_printf ("&%s", m->format, m->name);
1060 print_closer ();
1063 static void
1064 emit_mode_adjustments (void)
1066 struct mode_adjust *a;
1067 struct mode_data *m;
1069 puts ("\
1070 \nvoid\
1071 \ninit_adjust_machine_modes (void)\
1072 \n{\
1073 \n size_t s ATTRIBUTE_UNUSED;");
1075 /* Size adjustments must be propagated to all containing modes.
1076 A size adjustment forces us to recalculate the alignment too. */
1077 for (a = adj_bytesize; a; a = a->next)
1079 printf ("\n /* %s:%d */\n s = %s;\n",
1080 a->file, a->line, a->adjustment);
1081 printf (" mode_size[%smode] = s;\n", a->mode->name);
1082 printf (" mode_base_align[%smode] = s & (~s + 1);\n",
1083 a->mode->name);
1085 for (m = a->mode->contained; m; m = m->next_cont)
1087 switch (m->cl)
1089 case MODE_COMPLEX_INT:
1090 case MODE_COMPLEX_FLOAT:
1091 printf (" mode_size[%smode] = 2*s;\n", m->name);
1092 printf (" mode_base_align[%smode] = s & (~s + 1);\n",
1093 m->name);
1094 break;
1096 case MODE_VECTOR_INT:
1097 case MODE_VECTOR_FLOAT:
1098 printf (" mode_size[%smode] = %d*s;\n",
1099 m->name, m->ncomponents);
1100 printf (" mode_base_align[%smode] = (%d*s) & (~(%d*s)+1);\n",
1101 m->name, m->ncomponents, m->ncomponents);
1102 break;
1104 default:
1105 internal_error (
1106 "mode %s is neither vector nor complex but contains %s",
1107 m->name, a->mode->name);
1108 /* NOTREACHED */
1113 /* Alignment adjustments propagate too.
1114 ??? This may not be the right thing for vector modes. */
1115 for (a = adj_alignment; a; a = a->next)
1117 printf ("\n /* %s:%d */\n s = %s;\n",
1118 a->file, a->line, a->adjustment);
1119 printf (" mode_base_align[%smode] = s;\n", a->mode->name);
1121 for (m = a->mode->contained; m; m = m->next_cont)
1123 switch (m->cl)
1125 case MODE_COMPLEX_INT:
1126 case MODE_COMPLEX_FLOAT:
1127 printf (" mode_base_align[%smode] = s;\n", m->name);
1128 break;
1130 case MODE_VECTOR_INT:
1131 case MODE_VECTOR_FLOAT:
1132 printf (" mode_base_align[%smode] = %d*s;\n",
1133 m->name, m->ncomponents);
1134 break;
1136 default:
1137 internal_error (
1138 "mode %s is neither vector nor complex but contains %s",
1139 m->name, a->mode->name);
1140 /* NOTREACHED */
1145 /* Real mode formats don't have to propagate anywhere. */
1146 for (a = adj_format; a; a = a->next)
1147 printf ("\n /* %s:%d */\n REAL_MODE_FORMAT (%smode) = %s;\n",
1148 a->file, a->line, a->mode->name, a->adjustment);
1150 puts ("}");
1153 static void
1154 emit_insn_modes_c (void)
1156 emit_insn_modes_c_header ();
1157 emit_mode_name ();
1158 emit_mode_class ();
1159 emit_mode_precision ();
1160 emit_mode_size ();
1161 emit_mode_nunits ();
1162 emit_mode_wider ();
1163 emit_mode_mask ();
1164 emit_mode_inner ();
1165 emit_mode_base_align ();
1166 emit_class_narrowest_mode ();
1167 emit_real_format_for_mode ();
1168 emit_mode_adjustments ();
1171 static void
1172 emit_min_insn_modes_c (void)
1174 emit_min_insn_modes_c_header ();
1175 emit_mode_name ();
1176 emit_mode_class ();
1177 emit_mode_wider ();
1178 emit_class_narrowest_mode ();
1181 /* Master control. */
1183 main(int argc, char **argv)
1185 bool gen_header = false, gen_min = false;
1186 progname = argv[0];
1188 if (argc == 1)
1190 else if (argc == 2 && !strcmp (argv[1], "-h"))
1191 gen_header = true;
1192 else if (argc == 2 && !strcmp (argv[1], "-m"))
1193 gen_min = true;
1194 else
1196 error ("usage: %s [-h|-m] > file", progname);
1197 return FATAL_EXIT_CODE;
1200 modes_by_name = htab_create_alloc (64, hash_mode, eq_mode, 0, xcalloc, free);
1202 create_modes ();
1203 complete_all_modes ();
1205 if (have_error)
1206 return FATAL_EXIT_CODE;
1208 calc_wider_mode ();
1210 if (gen_header)
1211 emit_insn_modes_h ();
1212 else if (gen_min)
1213 emit_min_insn_modes_c ();
1214 else
1215 emit_insn_modes_c ();
1217 if (fflush (stdout) || fclose (stdout))
1218 return FATAL_EXIT_CODE;
1219 return SUCCESS_EXIT_CODE;