Daily bump.
[gcc.git] / gcc / genmodes.c
1 /* Generate the machine mode enumeration and associated tables.
2 Copyright (C) 2003-2021 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
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.
10
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.
15
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/>. */
19
20 #include "bconfig.h"
21 #include "system.h"
22 #include "errors.h"
23
24 /* enum mode_class is normally defined by machmode.h but we can't
25 include that header here. */
26 #include "mode-classes.def"
27
28 #define DEF_MODE_CLASS(M) M
29 enum mode_class { MODE_CLASSES, MAX_MODE_CLASS };
30 #undef DEF_MODE_CLASS
31
32 /* Text names of mode classes, for output. */
33 #define DEF_MODE_CLASS(M) #M
34 static const char *const mode_class_names[MAX_MODE_CLASS] =
35 {
36 MODE_CLASSES
37 };
38 #undef DEF_MODE_CLASS
39 #undef MODE_CLASSES
40
41 #ifdef EXTRA_MODES_FILE
42 # define HAVE_EXTRA_MODES 1
43 #else
44 # define HAVE_EXTRA_MODES 0
45 # define EXTRA_MODES_FILE ""
46 #endif
47
48 /* Data structure for building up what we know about a mode.
49 They're clustered by mode class. */
50 struct mode_data
51 {
52 struct mode_data *next; /* next this class - arbitrary order */
53
54 const char *name; /* printable mode name -- SI, not SImode */
55 enum mode_class cl; /* this mode class */
56 unsigned int order; /* top-level sorting order */
57 unsigned int precision; /* size in bits, equiv to TYPE_PRECISION */
58 unsigned int bytesize; /* storage size in addressable units */
59 unsigned int ncomponents; /* number of subunits */
60 unsigned int alignment; /* mode alignment */
61 const char *format; /* floating point format - float modes only */
62
63 struct mode_data *component; /* mode of components */
64 struct mode_data *wider; /* next wider mode */
65
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. */
69
70 struct mode_data *complex; /* complex type with mode as component. */
71 const char *file; /* file and line of definition, */
72 unsigned int line; /* for error reporting */
73 unsigned int counter; /* Rank ordering of modes */
74 unsigned int ibit; /* the number of integral bits */
75 unsigned int fbit; /* the number of fractional bits */
76 bool need_nunits_adj; /* true if this mode needs dynamic nunits
77 adjustment */
78 bool need_bytesize_adj; /* true if this mode needs dynamic size
79 adjustment */
80 unsigned int int_n; /* If nonzero, then __int<INT_N> will be defined */
81 };
82
83 static struct mode_data *modes[MAX_MODE_CLASS];
84 static unsigned int n_modes[MAX_MODE_CLASS];
85 static struct mode_data *void_mode;
86
87 static const struct mode_data blank_mode = {
88 0, "<unknown>", MAX_MODE_CLASS,
89 0, -1U, -1U, -1U, -1U,
90 0, 0, 0, 0, 0, 0,
91 "<unknown>", 0, 0, 0, 0, false, false, 0
92 };
93
94 static htab_t modes_by_name;
95
96 /* Data structure for recording target-specified runtime adjustments
97 to a particular mode. We support varying the byte size, the
98 alignment, and the floating point format. */
99 struct mode_adjust
100 {
101 struct mode_adjust *next;
102 struct mode_data *mode;
103 const char *adjustment;
104
105 const char *file;
106 unsigned int line;
107 };
108
109 static struct mode_adjust *adj_nunits;
110 static struct mode_adjust *adj_bytesize;
111 static struct mode_adjust *adj_alignment;
112 static struct mode_adjust *adj_format;
113 static struct mode_adjust *adj_ibit;
114 static struct mode_adjust *adj_fbit;
115
116 /* Mode class operations. */
117 static enum mode_class
118 complex_class (enum mode_class c)
119 {
120 switch (c)
121 {
122 case MODE_INT: return MODE_COMPLEX_INT;
123 case MODE_PARTIAL_INT: return MODE_COMPLEX_INT;
124 case MODE_FLOAT: return MODE_COMPLEX_FLOAT;
125 default:
126 error ("no complex class for class %s", mode_class_names[c]);
127 return MODE_RANDOM;
128 }
129 }
130
131 static enum mode_class
132 vector_class (enum mode_class cl)
133 {
134 switch (cl)
135 {
136 case MODE_INT: return MODE_VECTOR_INT;
137 case MODE_FLOAT: return MODE_VECTOR_FLOAT;
138 case MODE_FRACT: return MODE_VECTOR_FRACT;
139 case MODE_UFRACT: return MODE_VECTOR_UFRACT;
140 case MODE_ACCUM: return MODE_VECTOR_ACCUM;
141 case MODE_UACCUM: return MODE_VECTOR_UACCUM;
142 default:
143 error ("no vector class for class %s", mode_class_names[cl]);
144 return MODE_RANDOM;
145 }
146 }
147
148 /* Utility routines. */
149 static inline struct mode_data *
150 find_mode (const char *name)
151 {
152 struct mode_data key;
153
154 key.name = name;
155 return (struct mode_data *) htab_find (modes_by_name, &key);
156 }
157
158 static struct mode_data *
159 new_mode (enum mode_class cl, const char *name,
160 const char *file, unsigned int line)
161 {
162 struct mode_data *m;
163 static unsigned int count = 0;
164
165 m = find_mode (name);
166 if (m)
167 {
168 error ("%s:%d: duplicate definition of mode \"%s\"",
169 trim_filename (file), line, name);
170 error ("%s:%d: previous definition here", m->file, m->line);
171 return m;
172 }
173
174 m = XNEW (struct mode_data);
175 memcpy (m, &blank_mode, sizeof (struct mode_data));
176 m->cl = cl;
177 m->name = name;
178 if (file)
179 m->file = trim_filename (file);
180 m->line = line;
181 m->counter = count++;
182
183 m->next = modes[cl];
184 modes[cl] = m;
185 n_modes[cl]++;
186
187 *htab_find_slot (modes_by_name, m, INSERT) = m;
188
189 return m;
190 }
191
192 static hashval_t
193 hash_mode (const void *p)
194 {
195 const struct mode_data *m = (const struct mode_data *)p;
196 return htab_hash_string (m->name);
197 }
198
199 static int
200 eq_mode (const void *p, const void *q)
201 {
202 const struct mode_data *a = (const struct mode_data *)p;
203 const struct mode_data *b = (const struct mode_data *)q;
204
205 return !strcmp (a->name, b->name);
206 }
207
208 #define for_all_modes(C, M) \
209 for (C = 0; C < MAX_MODE_CLASS; C++) \
210 for (M = modes[C]; M; M = M->next)
211
212 static void ATTRIBUTE_UNUSED
213 new_adjust (const char *name,
214 struct mode_adjust **category, const char *catname,
215 const char *adjustment,
216 enum mode_class required_class_from,
217 enum mode_class required_class_to,
218 const char *file, unsigned int line)
219 {
220 struct mode_data *mode = find_mode (name);
221 struct mode_adjust *a;
222
223 file = trim_filename (file);
224
225 if (!mode)
226 {
227 error ("%s:%d: no mode \"%s\"", file, line, name);
228 return;
229 }
230
231 if (required_class_from != MODE_RANDOM
232 && (mode->cl < required_class_from || mode->cl > required_class_to))
233 {
234 error ("%s:%d: mode \"%s\" is not among class {%s, %s}",
235 file, line, name, mode_class_names[required_class_from] + 5,
236 mode_class_names[required_class_to] + 5);
237 return;
238 }
239
240 for (a = *category; a; a = a->next)
241 if (a->mode == mode)
242 {
243 error ("%s:%d: mode \"%s\" already has a %s adjustment",
244 file, line, name, catname);
245 error ("%s:%d: previous adjustment here", a->file, a->line);
246 return;
247 }
248
249 a = XNEW (struct mode_adjust);
250 a->mode = mode;
251 a->adjustment = adjustment;
252 a->file = file;
253 a->line = line;
254
255 a->next = *category;
256 *category = a;
257 }
258
259 /* Diagnose failure to meet expectations in a partially filled out
260 mode structure. */
261 enum requirement { SET, UNSET, OPTIONAL };
262
263 #define validate_field_(mname, fname, req, val, unset, file, line) do { \
264 switch (req) \
265 { \
266 case SET: \
267 if (val == unset) \
268 error ("%s:%d: (%s) field %s must be set", \
269 file, line, mname, fname); \
270 break; \
271 case UNSET: \
272 if (val != unset) \
273 error ("%s:%d: (%s) field %s must not be set", \
274 file, line, mname, fname); \
275 case OPTIONAL: \
276 break; \
277 } \
278 } while (0)
279
280 #define validate_field(M, F) \
281 validate_field_(M->name, #F, r_##F, M->F, blank_mode.F, M->file, M->line)
282
283 static void
284 validate_mode (struct mode_data *m,
285 enum requirement r_precision,
286 enum requirement r_bytesize,
287 enum requirement r_component,
288 enum requirement r_ncomponents,
289 enum requirement r_format)
290 {
291 validate_field (m, precision);
292 validate_field (m, bytesize);
293 validate_field (m, component);
294 validate_field (m, ncomponents);
295 validate_field (m, format);
296 }
297 #undef validate_field
298 #undef validate_field_
299
300 /* Given a partially-filled-out mode structure, figure out what we can
301 and fill the rest of it in; die if it isn't enough. */
302 static void
303 complete_mode (struct mode_data *m)
304 {
305 unsigned int alignment;
306
307 if (!m->name)
308 {
309 error ("%s:%d: mode with no name", m->file, m->line);
310 return;
311 }
312 if (m->cl == MAX_MODE_CLASS)
313 {
314 error ("%s:%d: %smode has no mode class", m->file, m->line, m->name);
315 return;
316 }
317
318 switch (m->cl)
319 {
320 case MODE_RANDOM:
321 /* Nothing more need be said. */
322 if (!strcmp (m->name, "VOID"))
323 void_mode = m;
324
325 validate_mode (m, UNSET, UNSET, UNSET, UNSET, UNSET);
326
327 m->precision = 0;
328 m->bytesize = 0;
329 m->ncomponents = 0;
330 m->component = 0;
331 break;
332
333 case MODE_CC:
334 /* Again, nothing more need be said. For historical reasons,
335 the size of a CC mode is four units. */
336 validate_mode (m, UNSET, UNSET, UNSET, UNSET, UNSET);
337
338 m->bytesize = 4;
339 m->ncomponents = 1;
340 m->component = 0;
341 break;
342
343 case MODE_INT:
344 case MODE_FLOAT:
345 case MODE_DECIMAL_FLOAT:
346 case MODE_FRACT:
347 case MODE_UFRACT:
348 case MODE_ACCUM:
349 case MODE_UACCUM:
350 /* A scalar mode must have a byte size, may have a bit size,
351 and must not have components. A float mode must have a
352 format. */
353 validate_mode (m, OPTIONAL, SET, UNSET, UNSET,
354 (m->cl == MODE_FLOAT || m->cl == MODE_DECIMAL_FLOAT)
355 ? SET : UNSET);
356
357 m->ncomponents = 1;
358 m->component = 0;
359 break;
360
361 case MODE_OPAQUE:
362 /* Opaque modes have size and precision. */
363 validate_mode (m, OPTIONAL, SET, UNSET, UNSET, UNSET);
364
365 m->ncomponents = 1;
366 m->component = 0;
367 break;
368
369 case MODE_PARTIAL_INT:
370 /* A partial integer mode uses ->component to say what the
371 corresponding full-size integer mode is, and may also
372 specify a bit size. */
373 validate_mode (m, OPTIONAL, UNSET, SET, UNSET, UNSET);
374
375 m->bytesize = m->component->bytesize;
376
377 m->ncomponents = 1;
378 break;
379
380 case MODE_COMPLEX_INT:
381 case MODE_COMPLEX_FLOAT:
382 /* Complex modes should have a component indicated, but no more. */
383 validate_mode (m, UNSET, UNSET, SET, UNSET, UNSET);
384 m->ncomponents = 2;
385 if (m->component->precision != (unsigned int)-1)
386 m->precision = 2 * m->component->precision;
387 m->bytesize = 2 * m->component->bytesize;
388 break;
389
390 case MODE_VECTOR_BOOL:
391 validate_mode (m, UNSET, SET, SET, SET, UNSET);
392 break;
393
394 case MODE_VECTOR_INT:
395 case MODE_VECTOR_FLOAT:
396 case MODE_VECTOR_FRACT:
397 case MODE_VECTOR_UFRACT:
398 case MODE_VECTOR_ACCUM:
399 case MODE_VECTOR_UACCUM:
400 /* Vector modes should have a component and a number of components. */
401 validate_mode (m, UNSET, UNSET, SET, SET, UNSET);
402 if (m->component->precision != (unsigned int)-1)
403 m->precision = m->ncomponents * m->component->precision;
404 m->bytesize = m->ncomponents * m->component->bytesize;
405 break;
406
407 default:
408 gcc_unreachable ();
409 }
410
411 /* If not already specified, the mode alignment defaults to the largest
412 power of two that divides the size of the object. Complex types are
413 not more aligned than their contents. */
414 if (m->cl == MODE_COMPLEX_INT || m->cl == MODE_COMPLEX_FLOAT)
415 alignment = m->component->bytesize;
416 else
417 alignment = m->bytesize;
418
419 m->alignment = alignment & (~alignment + 1);
420
421 /* If this mode has components, make the component mode point back
422 to this mode, for the sake of adjustments. */
423 if (m->component)
424 {
425 m->next_cont = m->component->contained;
426 m->component->contained = m;
427 }
428 }
429
430 static void
431 complete_all_modes (void)
432 {
433 struct mode_data *m;
434 int cl;
435
436 for_all_modes (cl, m)
437 complete_mode (m);
438 }
439
440 /* For each mode in class CLASS, construct a corresponding complex mode. */
441 #define COMPLEX_MODES(C) make_complex_modes (MODE_##C, __FILE__, __LINE__)
442 static void
443 make_complex_modes (enum mode_class cl,
444 const char *file, unsigned int line)
445 {
446 struct mode_data *m;
447 struct mode_data *c;
448 enum mode_class cclass = complex_class (cl);
449
450 if (cclass == MODE_RANDOM)
451 return;
452
453 for (m = modes[cl]; m; m = m->next)
454 {
455 char *p, *buf;
456 size_t m_len;
457
458 /* Skip BImode. FIXME: BImode probably shouldn't be MODE_INT. */
459 if (m->precision == 1)
460 continue;
461
462 m_len = strlen (m->name);
463 /* The leading "1 +" is in case we prepend a "C" below. */
464 buf = (char *) xmalloc (1 + m_len + 1);
465
466 /* Float complex modes are named SCmode, etc.
467 Int complex modes are named CSImode, etc.
468 This inconsistency should be eliminated. */
469 p = 0;
470 if (cl == MODE_FLOAT)
471 {
472 memcpy (buf, m->name, m_len + 1);
473 p = strchr (buf, 'F');
474 if (p == 0 && strchr (buf, 'D') == 0)
475 {
476 error ("%s:%d: float mode \"%s\" has no 'F' or 'D'",
477 m->file, m->line, m->name);
478 free (buf);
479 continue;
480 }
481 }
482 if (p != 0)
483 *p = 'C';
484 else
485 {
486 buf[0] = 'C';
487 memcpy (buf + 1, m->name, m_len + 1);
488 }
489
490 c = new_mode (cclass, buf, file, line);
491 c->component = m;
492 m->complex = c;
493 }
494 }
495
496 /* For all modes in class CL, construct vector modes of width WIDTH,
497 having as many components as necessary. ORDER is the sorting order
498 of the mode, with smaller numbers indicating a higher priority. */
499 #define VECTOR_MODES_WITH_PREFIX(PREFIX, C, W, ORDER) \
500 make_vector_modes (MODE_##C, #PREFIX, W, ORDER, __FILE__, __LINE__)
501 #define VECTOR_MODES(C, W) VECTOR_MODES_WITH_PREFIX (V, C, W, 0)
502 static void ATTRIBUTE_UNUSED
503 make_vector_modes (enum mode_class cl, const char *prefix, unsigned int width,
504 unsigned int order, const char *file, unsigned int line)
505 {
506 struct mode_data *m;
507 struct mode_data *v;
508 /* Big enough for a 32-bit UINT_MAX plus the text. */
509 char buf[12];
510 unsigned int ncomponents;
511 enum mode_class vclass = vector_class (cl);
512
513 if (vclass == MODE_RANDOM)
514 return;
515
516 for (m = modes[cl]; m; m = m->next)
517 {
518 /* Do not construct vector modes with only one element, or
519 vector modes where the element size doesn't divide the full
520 size evenly. */
521 ncomponents = width / m->bytesize;
522 if (ncomponents < 2)
523 continue;
524 if (width % m->bytesize)
525 continue;
526
527 /* Skip QFmode and BImode. FIXME: this special case should
528 not be necessary. */
529 if (cl == MODE_FLOAT && m->bytesize == 1)
530 continue;
531 if (cl == MODE_INT && m->precision == 1)
532 continue;
533
534 if ((size_t) snprintf (buf, sizeof buf, "%s%u%s", prefix,
535 ncomponents, m->name) >= sizeof buf)
536 {
537 error ("%s:%d: mode name \"%s\" is too long",
538 m->file, m->line, m->name);
539 continue;
540 }
541
542 v = new_mode (vclass, xstrdup (buf), file, line);
543 v->order = order;
544 v->component = m;
545 v->ncomponents = ncomponents;
546 }
547 }
548
549 /* Create a vector of booleans called NAME with COUNT elements and
550 BYTESIZE bytes in total. */
551 #define VECTOR_BOOL_MODE(NAME, COUNT, BYTESIZE) \
552 make_vector_bool_mode (#NAME, COUNT, BYTESIZE, __FILE__, __LINE__)
553 static void ATTRIBUTE_UNUSED
554 make_vector_bool_mode (const char *name, unsigned int count,
555 unsigned int bytesize, const char *file,
556 unsigned int line)
557 {
558 struct mode_data *m = find_mode ("BI");
559 if (!m)
560 {
561 error ("%s:%d: no mode \"BI\"", file, line);
562 return;
563 }
564
565 struct mode_data *v = new_mode (MODE_VECTOR_BOOL, name, file, line);
566 v->component = m;
567 v->ncomponents = count;
568 v->bytesize = bytesize;
569 }
570
571 /* Input. */
572
573 #define _SPECIAL_MODE(C, N) \
574 make_special_mode (MODE_##C, #N, __FILE__, __LINE__)
575 #define RANDOM_MODE(N) _SPECIAL_MODE (RANDOM, N)
576 #define CC_MODE(N) _SPECIAL_MODE (CC, N)
577
578 static void
579 make_special_mode (enum mode_class cl, const char *name,
580 const char *file, unsigned int line)
581 {
582 new_mode (cl, name, file, line);
583 }
584
585 #define INT_MODE(N, Y) FRACTIONAL_INT_MODE (N, -1U, Y)
586 #define FRACTIONAL_INT_MODE(N, B, Y) \
587 make_int_mode (#N, B, Y, __FILE__, __LINE__)
588
589 static void
590 make_int_mode (const char *name,
591 unsigned int precision, unsigned int bytesize,
592 const char *file, unsigned int line)
593 {
594 struct mode_data *m = new_mode (MODE_INT, name, file, line);
595 m->bytesize = bytesize;
596 m->precision = precision;
597 }
598
599 #define OPAQUE_MODE(N, B) \
600 make_opaque_mode (#N, -1U, B, __FILE__, __LINE__)
601
602 static void ATTRIBUTE_UNUSED
603 make_opaque_mode (const char *name,
604 unsigned int precision,
605 unsigned int bytesize,
606 const char *file, unsigned int line)
607 {
608 struct mode_data *m = new_mode (MODE_OPAQUE, name, file, line);
609 m->bytesize = bytesize;
610 m->precision = precision;
611 }
612
613 #define FRACT_MODE(N, Y, F) \
614 make_fixed_point_mode (MODE_FRACT, #N, Y, 0, F, __FILE__, __LINE__)
615
616 #define UFRACT_MODE(N, Y, F) \
617 make_fixed_point_mode (MODE_UFRACT, #N, Y, 0, F, __FILE__, __LINE__)
618
619 #define ACCUM_MODE(N, Y, I, F) \
620 make_fixed_point_mode (MODE_ACCUM, #N, Y, I, F, __FILE__, __LINE__)
621
622 #define UACCUM_MODE(N, Y, I, F) \
623 make_fixed_point_mode (MODE_UACCUM, #N, Y, I, F, __FILE__, __LINE__)
624
625 /* Create a fixed-point mode by setting CL, NAME, BYTESIZE, IBIT, FBIT,
626 FILE, and LINE. */
627
628 static void
629 make_fixed_point_mode (enum mode_class cl,
630 const char *name,
631 unsigned int bytesize,
632 unsigned int ibit,
633 unsigned int fbit,
634 const char *file, unsigned int line)
635 {
636 struct mode_data *m = new_mode (cl, name, file, line);
637 m->bytesize = bytesize;
638 m->ibit = ibit;
639 m->fbit = fbit;
640 }
641
642 #define FLOAT_MODE(N, Y, F) FRACTIONAL_FLOAT_MODE (N, -1U, Y, F)
643 #define FRACTIONAL_FLOAT_MODE(N, B, Y, F) \
644 make_float_mode (#N, B, Y, #F, __FILE__, __LINE__)
645
646 static void
647 make_float_mode (const char *name,
648 unsigned int precision, unsigned int bytesize,
649 const char *format,
650 const char *file, unsigned int line)
651 {
652 struct mode_data *m = new_mode (MODE_FLOAT, name, file, line);
653 m->bytesize = bytesize;
654 m->precision = precision;
655 m->format = format;
656 }
657
658 #define DECIMAL_FLOAT_MODE(N, Y, F) \
659 FRACTIONAL_DECIMAL_FLOAT_MODE (N, -1U, Y, F)
660 #define FRACTIONAL_DECIMAL_FLOAT_MODE(N, B, Y, F) \
661 make_decimal_float_mode (#N, B, Y, #F, __FILE__, __LINE__)
662
663 static void
664 make_decimal_float_mode (const char *name,
665 unsigned int precision, unsigned int bytesize,
666 const char *format,
667 const char *file, unsigned int line)
668 {
669 struct mode_data *m = new_mode (MODE_DECIMAL_FLOAT, name, file, line);
670 m->bytesize = bytesize;
671 m->precision = precision;
672 m->format = format;
673 }
674
675 #define RESET_FLOAT_FORMAT(N, F) \
676 reset_float_format (#N, #F, __FILE__, __LINE__)
677 static void ATTRIBUTE_UNUSED
678 reset_float_format (const char *name, const char *format,
679 const char *file, unsigned int line)
680 {
681 struct mode_data *m = find_mode (name);
682 if (!m)
683 {
684 error ("%s:%d: no mode \"%s\"", file, line, name);
685 return;
686 }
687 if (m->cl != MODE_FLOAT && m->cl != MODE_DECIMAL_FLOAT)
688 {
689 error ("%s:%d: mode \"%s\" is not a FLOAT class", file, line, name);
690 return;
691 }
692 m->format = format;
693 }
694
695 /* __intN support. */
696 #define INT_N(M,PREC) \
697 make_int_n (#M, PREC, __FILE__, __LINE__)
698 static void ATTRIBUTE_UNUSED
699 make_int_n (const char *m, int bitsize,
700 const char *file, unsigned int line)
701 {
702 struct mode_data *component = find_mode (m);
703 if (!component)
704 {
705 error ("%s:%d: no mode \"%s\"", file, line, m);
706 return;
707 }
708 if (component->cl != MODE_INT
709 && component->cl != MODE_PARTIAL_INT)
710 {
711 error ("%s:%d: mode \"%s\" is not class INT or PARTIAL_INT", file, line, m);
712 return;
713 }
714 if (component->int_n != 0)
715 {
716 error ("%s:%d: mode \"%s\" already has an intN", file, line, m);
717 return;
718 }
719
720 component->int_n = bitsize;
721 }
722
723 /* Partial integer modes are specified by relation to a full integer
724 mode. */
725 #define PARTIAL_INT_MODE(M,PREC,NAME) \
726 make_partial_integer_mode (#M, #NAME, PREC, __FILE__, __LINE__)
727 static void ATTRIBUTE_UNUSED
728 make_partial_integer_mode (const char *base, const char *name,
729 unsigned int precision,
730 const char *file, unsigned int line)
731 {
732 struct mode_data *m;
733 struct mode_data *component = find_mode (base);
734 if (!component)
735 {
736 error ("%s:%d: no mode \"%s\"", file, line, name);
737 return;
738 }
739 if (component->cl != MODE_INT)
740 {
741 error ("%s:%d: mode \"%s\" is not class INT", file, line, name);
742 return;
743 }
744
745 m = new_mode (MODE_PARTIAL_INT, name, file, line);
746 m->precision = precision;
747 m->component = component;
748 }
749
750 /* A single vector mode can be specified by naming its component
751 mode and the number of components. */
752 #define VECTOR_MODE(C, M, N) \
753 make_vector_mode (MODE_##C, #M, N, __FILE__, __LINE__);
754 static void ATTRIBUTE_UNUSED
755 make_vector_mode (enum mode_class bclass,
756 const char *base,
757 unsigned int ncomponents,
758 const char *file, unsigned int line)
759 {
760 struct mode_data *v;
761 enum mode_class vclass = vector_class (bclass);
762 struct mode_data *component = find_mode (base);
763 char namebuf[16];
764
765 if (vclass == MODE_RANDOM)
766 return;
767 if (component == 0)
768 {
769 error ("%s:%d: no mode \"%s\"", file, line, base);
770 return;
771 }
772 if (component->cl != bclass
773 && (component->cl != MODE_PARTIAL_INT
774 || bclass != MODE_INT))
775 {
776 error ("%s:%d: mode \"%s\" is not class %s",
777 file, line, base, mode_class_names[bclass] + 5);
778 return;
779 }
780
781 if ((size_t)snprintf (namebuf, sizeof namebuf, "V%u%s",
782 ncomponents, base) >= sizeof namebuf)
783 {
784 error ("%s:%d: mode name \"%s\" is too long",
785 file, line, base);
786 return;
787 }
788
789 v = new_mode (vclass, xstrdup (namebuf), file, line);
790 v->ncomponents = ncomponents;
791 v->component = component;
792 }
793
794 /* Adjustability. */
795 #define _ADD_ADJUST(A, M, X, C1, C2) \
796 new_adjust (#M, &adj_##A, #A, #X, MODE_##C1, MODE_##C2, __FILE__, __LINE__)
797
798 #define ADJUST_NUNITS(M, X) _ADD_ADJUST (nunits, M, X, RANDOM, RANDOM)
799 #define ADJUST_BYTESIZE(M, X) _ADD_ADJUST (bytesize, M, X, RANDOM, RANDOM)
800 #define ADJUST_ALIGNMENT(M, X) _ADD_ADJUST (alignment, M, X, RANDOM, RANDOM)
801 #define ADJUST_FLOAT_FORMAT(M, X) _ADD_ADJUST (format, M, X, FLOAT, FLOAT)
802 #define ADJUST_IBIT(M, X) _ADD_ADJUST (ibit, M, X, ACCUM, UACCUM)
803 #define ADJUST_FBIT(M, X) _ADD_ADJUST (fbit, M, X, FRACT, UACCUM)
804
805 static int bits_per_unit;
806 static int max_bitsize_mode_any_int;
807 static int max_bitsize_mode_any_mode;
808
809 static void
810 create_modes (void)
811 {
812 #include "machmode.def"
813
814 /* So put the default value unless the target needs a non standard
815 value. */
816 #ifdef BITS_PER_UNIT
817 bits_per_unit = BITS_PER_UNIT;
818 #else
819 bits_per_unit = 8;
820 #endif
821
822 #ifdef MAX_BITSIZE_MODE_ANY_INT
823 max_bitsize_mode_any_int = MAX_BITSIZE_MODE_ANY_INT;
824 #else
825 max_bitsize_mode_any_int = 0;
826 #endif
827
828 #ifdef MAX_BITSIZE_MODE_ANY_MODE
829 max_bitsize_mode_any_mode = MAX_BITSIZE_MODE_ANY_MODE;
830 #else
831 max_bitsize_mode_any_mode = 0;
832 #endif
833 }
834
835 #ifndef NUM_POLY_INT_COEFFS
836 #define NUM_POLY_INT_COEFFS 1
837 #endif
838
839 /* Processing. */
840
841 /* Sort a list of modes into the order needed for the WIDER field:
842 major sort by precision, minor sort by component precision.
843
844 For instance:
845 QI < HI < SI < DI < TI
846 V4QI < V2HI < V8QI < V4HI < V2SI.
847
848 If the precision is not set, sort by the bytesize. A mode with
849 precision set gets sorted before a mode without precision set, if
850 they have the same bytesize; this is the right thing because
851 the precision must always be smaller than the bytesize * BITS_PER_UNIT.
852 We don't have to do anything special to get this done -- an unset
853 precision shows up as (unsigned int)-1, i.e. UINT_MAX. */
854 static int
855 cmp_modes (const void *a, const void *b)
856 {
857 const struct mode_data *const m = *(const struct mode_data *const*)a;
858 const struct mode_data *const n = *(const struct mode_data *const*)b;
859
860 if (m->order > n->order)
861 return 1;
862 else if (m->order < n->order)
863 return -1;
864
865 if (m->bytesize > n->bytesize)
866 return 1;
867 else if (m->bytesize < n->bytesize)
868 return -1;
869
870 if (m->precision > n->precision)
871 return 1;
872 else if (m->precision < n->precision)
873 return -1;
874
875 if (!m->component && !n->component)
876 {
877 if (m->counter < n->counter)
878 return -1;
879 else
880 return 1;
881 }
882
883 if (m->component->bytesize > n->component->bytesize)
884 return 1;
885 else if (m->component->bytesize < n->component->bytesize)
886 return -1;
887
888 if (m->component->precision > n->component->precision)
889 return 1;
890 else if (m->component->precision < n->component->precision)
891 return -1;
892
893 if (m->counter < n->counter)
894 return -1;
895 else
896 return 1;
897 }
898
899 static void
900 calc_wider_mode (void)
901 {
902 int c;
903 struct mode_data *m;
904 struct mode_data **sortbuf;
905 unsigned int max_n_modes = 0;
906 unsigned int i, j;
907
908 for (c = 0; c < MAX_MODE_CLASS; c++)
909 max_n_modes = MAX (max_n_modes, n_modes[c]);
910
911 /* Allocate max_n_modes + 1 entries to leave room for the extra null
912 pointer assigned after the qsort call below. */
913 sortbuf = XALLOCAVEC (struct mode_data *, max_n_modes + 1);
914
915 for (c = 0; c < MAX_MODE_CLASS; c++)
916 {
917 /* "wider" is not meaningful for MODE_RANDOM and MODE_CC.
918 However, we want these in textual order, and we have
919 precisely the reverse. */
920 if (c == MODE_RANDOM || c == MODE_CC)
921 {
922 struct mode_data *prev, *next;
923
924 for (prev = 0, m = modes[c]; m; m = next)
925 {
926 m->wider = void_mode;
927
928 /* this is nreverse */
929 next = m->next;
930 m->next = prev;
931 prev = m;
932 }
933 modes[c] = prev;
934 }
935 else
936 {
937 if (!modes[c])
938 continue;
939
940 for (i = 0, m = modes[c]; m; i++, m = m->next)
941 sortbuf[i] = m;
942
943 (qsort) (sortbuf, i, sizeof (struct mode_data *), cmp_modes);
944
945 sortbuf[i] = 0;
946 for (j = 0; j < i; j++)
947 {
948 sortbuf[j]->next = sortbuf[j + 1];
949 if (c == MODE_PARTIAL_INT)
950 sortbuf[j]->wider = sortbuf[j]->component;
951 else
952 sortbuf[j]->wider = sortbuf[j]->next;
953 }
954
955 modes[c] = sortbuf[0];
956 }
957 }
958 }
959
960 /* Text to add to the constant part of a poly_int_pod initializer in
961 order to fill out te whole structure. */
962 #if NUM_POLY_INT_COEFFS == 1
963 #define ZERO_COEFFS ""
964 #elif NUM_POLY_INT_COEFFS == 2
965 #define ZERO_COEFFS ", 0"
966 #else
967 #error "Unknown value of NUM_POLY_INT_COEFFS"
968 #endif
969
970 /* Output routines. */
971
972 #define tagged_printf(FMT, ARG, TAG) do { \
973 int count_ = printf (" " FMT ",", ARG); \
974 printf ("%*s/* %s */\n", 27 - count_, "", TAG); \
975 } while (0)
976
977 #define print_decl(TYPE, NAME, ASIZE) \
978 puts ("\nconst " TYPE " " NAME "[" ASIZE "] =\n{");
979
980 #define print_maybe_const_decl(TYPE, NAME, ASIZE, NEEDS_ADJ) \
981 printf ("\n" TYPE " " NAME "[" ASIZE "] = \n{\n", \
982 NEEDS_ADJ ? "" : "const ")
983
984 #define print_closer() puts ("};")
985
986 /* Compute the max bitsize of some of the classes of integers. It may
987 be that there are needs for the other integer classes, and this
988 code is easy to extend. */
989 static void
990 emit_max_int (void)
991 {
992 unsigned int max, mmax;
993 struct mode_data *i;
994 int j;
995
996 puts ("");
997
998 printf ("#define BITS_PER_UNIT (%d)\n", bits_per_unit);
999
1000 if (max_bitsize_mode_any_int == 0)
1001 {
1002 for (max = 1, i = modes[MODE_INT]; i; i = i->next)
1003 if (max < i->bytesize)
1004 max = i->bytesize;
1005 mmax = max;
1006 for (max = 1, i = modes[MODE_PARTIAL_INT]; i; i = i->next)
1007 if (max < i->bytesize)
1008 max = i->bytesize;
1009 if (max > mmax)
1010 mmax = max;
1011 printf ("#define MAX_BITSIZE_MODE_ANY_INT (%d*BITS_PER_UNIT)\n", mmax);
1012 }
1013 else
1014 printf ("#define MAX_BITSIZE_MODE_ANY_INT %d\n", max_bitsize_mode_any_int);
1015
1016 if (max_bitsize_mode_any_mode == 0)
1017 {
1018 mmax = 0;
1019 for (j = 0; j < MAX_MODE_CLASS; j++)
1020 for (i = modes[j]; i; i = i->next)
1021 if (mmax < i->bytesize)
1022 mmax = i->bytesize;
1023 printf ("#define MAX_BITSIZE_MODE_ANY_MODE (%d*BITS_PER_UNIT)\n", mmax);
1024 }
1025 else
1026 printf ("#define MAX_BITSIZE_MODE_ANY_MODE %d\n",
1027 max_bitsize_mode_any_mode);
1028 }
1029
1030 /* Emit mode_size_inline routine into insn-modes.h header. */
1031 static void
1032 emit_mode_size_inline (void)
1033 {
1034 int c;
1035 struct mode_adjust *a;
1036 struct mode_data *m;
1037
1038 /* Size adjustments must be propagated to all containing modes. */
1039 for (a = adj_bytesize; a; a = a->next)
1040 {
1041 a->mode->need_bytesize_adj = true;
1042 for (m = a->mode->contained; m; m = m->next_cont)
1043 m->need_bytesize_adj = true;
1044 }
1045
1046 /* Changing the number of units by a factor of X also changes the size
1047 by a factor of X. */
1048 for (mode_adjust *a = adj_nunits; a; a = a->next)
1049 a->mode->need_bytesize_adj = true;
1050
1051 printf ("\
1052 #ifdef __cplusplus\n\
1053 inline __attribute__((__always_inline__))\n\
1054 #else\n\
1055 extern __inline__ __attribute__((__always_inline__, __gnu_inline__))\n\
1056 #endif\n\
1057 poly_uint16\n\
1058 mode_size_inline (machine_mode mode)\n\
1059 {\n\
1060 extern %spoly_uint16_pod mode_size[NUM_MACHINE_MODES];\n\
1061 gcc_assert (mode >= 0 && mode < NUM_MACHINE_MODES);\n\
1062 switch (mode)\n\
1063 {\n", adj_nunits || adj_bytesize ? "" : "const ");
1064
1065 for_all_modes (c, m)
1066 if (!m->need_bytesize_adj)
1067 printf (" case E_%smode: return %u;\n", m->name, m->bytesize);
1068
1069 puts ("\
1070 default: return mode_size[mode];\n\
1071 }\n\
1072 }\n");
1073 }
1074
1075 /* Emit mode_nunits_inline routine into insn-modes.h header. */
1076 static void
1077 emit_mode_nunits_inline (void)
1078 {
1079 int c;
1080 struct mode_data *m;
1081
1082 for (mode_adjust *a = adj_nunits; a; a = a->next)
1083 a->mode->need_nunits_adj = true;
1084
1085 printf ("\
1086 #ifdef __cplusplus\n\
1087 inline __attribute__((__always_inline__))\n\
1088 #else\n\
1089 extern __inline__ __attribute__((__always_inline__, __gnu_inline__))\n\
1090 #endif\n\
1091 poly_uint16\n\
1092 mode_nunits_inline (machine_mode mode)\n\
1093 {\n\
1094 extern %spoly_uint16_pod mode_nunits[NUM_MACHINE_MODES];\n\
1095 switch (mode)\n\
1096 {\n", adj_nunits ? "" : "const ");
1097
1098 for_all_modes (c, m)
1099 if (!m->need_nunits_adj)
1100 printf (" case E_%smode: return %u;\n", m->name, m->ncomponents);
1101
1102 puts ("\
1103 default: return mode_nunits[mode];\n\
1104 }\n\
1105 }\n");
1106 }
1107
1108 /* Emit mode_inner_inline routine into insn-modes.h header. */
1109 static void
1110 emit_mode_inner_inline (void)
1111 {
1112 int c;
1113 struct mode_data *m;
1114
1115 puts ("\
1116 #ifdef __cplusplus\n\
1117 inline __attribute__((__always_inline__))\n\
1118 #else\n\
1119 extern __inline__ __attribute__((__always_inline__, __gnu_inline__))\n\
1120 #endif\n\
1121 unsigned char\n\
1122 mode_inner_inline (machine_mode mode)\n\
1123 {\n\
1124 extern const unsigned char mode_inner[NUM_MACHINE_MODES];\n\
1125 gcc_assert (mode >= 0 && mode < NUM_MACHINE_MODES);\n\
1126 switch (mode)\n\
1127 {");
1128
1129 for_all_modes (c, m)
1130 printf (" case E_%smode: return E_%smode;\n", m->name,
1131 c != MODE_PARTIAL_INT && m->component
1132 ? m->component->name : m->name);
1133
1134 puts ("\
1135 default: return mode_inner[mode];\n\
1136 }\n\
1137 }\n");
1138 }
1139
1140 /* Emit mode_unit_size_inline routine into insn-modes.h header. */
1141 static void
1142 emit_mode_unit_size_inline (void)
1143 {
1144 int c;
1145 struct mode_data *m;
1146
1147 puts ("\
1148 #ifdef __cplusplus\n\
1149 inline __attribute__((__always_inline__))\n\
1150 #else\n\
1151 extern __inline__ __attribute__((__always_inline__, __gnu_inline__))\n\
1152 #endif\n\
1153 unsigned char\n\
1154 mode_unit_size_inline (machine_mode mode)\n\
1155 {\n\
1156 extern CONST_MODE_UNIT_SIZE unsigned char mode_unit_size[NUM_MACHINE_MODES];\
1157 \n\
1158 gcc_assert (mode >= 0 && mode < NUM_MACHINE_MODES);\n\
1159 switch (mode)\n\
1160 {");
1161
1162 for_all_modes (c, m)
1163 {
1164 const char *name = m->name;
1165 struct mode_data *m2 = m;
1166 if (c != MODE_PARTIAL_INT && m2->component)
1167 m2 = m2->component;
1168 if (!m2->need_bytesize_adj)
1169 printf (" case E_%smode: return %u;\n", name, m2->bytesize);
1170 }
1171
1172 puts ("\
1173 default: return mode_unit_size[mode];\n\
1174 }\n\
1175 }\n");
1176 }
1177
1178 /* Emit mode_unit_precision_inline routine into insn-modes.h header. */
1179 static void
1180 emit_mode_unit_precision_inline (void)
1181 {
1182 int c;
1183 struct mode_data *m;
1184
1185 puts ("\
1186 #ifdef __cplusplus\n\
1187 inline __attribute__((__always_inline__))\n\
1188 #else\n\
1189 extern __inline__ __attribute__((__always_inline__, __gnu_inline__))\n\
1190 #endif\n\
1191 unsigned short\n\
1192 mode_unit_precision_inline (machine_mode mode)\n\
1193 {\n\
1194 extern const unsigned short mode_unit_precision[NUM_MACHINE_MODES];\n\
1195 gcc_assert (mode >= 0 && mode < NUM_MACHINE_MODES);\n\
1196 switch (mode)\n\
1197 {");
1198
1199 for_all_modes (c, m)
1200 {
1201 struct mode_data *m2
1202 = (c != MODE_PARTIAL_INT && m->component) ? m->component : m;
1203 if (m2->precision != (unsigned int)-1)
1204 printf (" case E_%smode: return %u;\n", m->name, m2->precision);
1205 else
1206 printf (" case E_%smode: return %u*BITS_PER_UNIT;\n",
1207 m->name, m2->bytesize);
1208 }
1209
1210 puts ("\
1211 default: return mode_unit_precision[mode];\n\
1212 }\n\
1213 }\n");
1214 }
1215
1216 /* Return the best machine mode class for MODE, or null if machine_mode
1217 should be used. */
1218
1219 static const char *
1220 get_mode_class (struct mode_data *mode)
1221 {
1222 switch (mode->cl)
1223 {
1224 case MODE_INT:
1225 case MODE_PARTIAL_INT:
1226 return "scalar_int_mode";
1227
1228 case MODE_FRACT:
1229 case MODE_UFRACT:
1230 case MODE_ACCUM:
1231 case MODE_UACCUM:
1232 return "scalar_mode";
1233
1234 case MODE_FLOAT:
1235 case MODE_DECIMAL_FLOAT:
1236 return "scalar_float_mode";
1237
1238 case MODE_COMPLEX_INT:
1239 case MODE_COMPLEX_FLOAT:
1240 return "complex_mode";
1241
1242 default:
1243 return NULL;
1244 }
1245 }
1246
1247 static void
1248 emit_insn_modes_h (void)
1249 {
1250 int c;
1251 struct mode_data *m, *first, *last;
1252 int n_int_n_ents = 0;
1253
1254 printf ("/* Generated automatically from machmode.def%s%s\n",
1255 HAVE_EXTRA_MODES ? " and " : "",
1256 EXTRA_MODES_FILE);
1257
1258 puts ("\
1259 by genmodes. */\n\
1260 \n\
1261 #ifndef GCC_INSN_MODES_H\n\
1262 #define GCC_INSN_MODES_H\n\
1263 \n\
1264 enum machine_mode\n{");
1265
1266 for (c = 0; c < MAX_MODE_CLASS; c++)
1267 for (m = modes[c]; m; m = m->next)
1268 {
1269 int count_ = printf (" E_%smode,", m->name);
1270 printf ("%*s/* %s:%d */\n", 27 - count_, "",
1271 trim_filename (m->file), m->line);
1272 printf ("#define HAVE_%smode\n", m->name);
1273 printf ("#ifdef USE_ENUM_MODES\n");
1274 printf ("#define %smode E_%smode\n", m->name, m->name);
1275 printf ("#else\n");
1276 if (const char *mode_class = get_mode_class (m))
1277 printf ("#define %smode (%s ((%s::from_int) E_%smode))\n",
1278 m->name, mode_class, mode_class, m->name);
1279 else
1280 printf ("#define %smode ((void) 0, E_%smode)\n",
1281 m->name, m->name);
1282 printf ("#endif\n");
1283 }
1284
1285 puts (" MAX_MACHINE_MODE,\n");
1286
1287 for (c = 0; c < MAX_MODE_CLASS; c++)
1288 {
1289 first = modes[c];
1290 last = 0;
1291 for (m = first; m; last = m, m = m->next)
1292 ;
1293
1294 /* Don't use BImode for MIN_MODE_INT, since otherwise the middle
1295 end will try to use it for bitfields in structures and the
1296 like, which we do not want. Only the target md file should
1297 generate BImode widgets. */
1298 if (first && first->precision == 1 && c == MODE_INT)
1299 first = first->next;
1300
1301 if (first && last)
1302 printf (" MIN_%s = E_%smode,\n MAX_%s = E_%smode,\n\n",
1303 mode_class_names[c], first->name,
1304 mode_class_names[c], last->name);
1305 else
1306 printf (" MIN_%s = E_%smode,\n MAX_%s = E_%smode,\n\n",
1307 mode_class_names[c], void_mode->name,
1308 mode_class_names[c], void_mode->name);
1309 }
1310
1311 puts ("\
1312 NUM_MACHINE_MODES = MAX_MACHINE_MODE\n\
1313 };\n");
1314
1315 /* I can't think of a better idea, can you? */
1316 printf ("#define CONST_MODE_NUNITS%s\n", adj_nunits ? "" : " const");
1317 printf ("#define CONST_MODE_PRECISION%s\n", adj_nunits ? "" : " const");
1318 printf ("#define CONST_MODE_SIZE%s\n",
1319 adj_bytesize || adj_nunits ? "" : " const");
1320 printf ("#define CONST_MODE_UNIT_SIZE%s\n", adj_bytesize ? "" : " const");
1321 printf ("#define CONST_MODE_BASE_ALIGN%s\n", adj_alignment ? "" : " const");
1322 #if 0 /* disabled for backward compatibility, temporary */
1323 printf ("#define CONST_REAL_FORMAT_FOR_MODE%s\n", adj_format ? "" :" const");
1324 #endif
1325 printf ("#define CONST_MODE_IBIT%s\n", adj_ibit ? "" : " const");
1326 printf ("#define CONST_MODE_FBIT%s\n", adj_fbit ? "" : " const");
1327 printf ("#define CONST_MODE_MASK%s\n", adj_nunits ? "" : " const");
1328 emit_max_int ();
1329
1330 for_all_modes (c, m)
1331 if (m->int_n)
1332 n_int_n_ents ++;
1333
1334 printf ("#define NUM_INT_N_ENTS %d\n", n_int_n_ents);
1335
1336 printf ("#define NUM_POLY_INT_COEFFS %d\n", NUM_POLY_INT_COEFFS);
1337
1338 puts ("\
1339 \n\
1340 #endif /* insn-modes.h */");
1341 }
1342
1343 static void
1344 emit_insn_modes_inline_h (void)
1345 {
1346 printf ("/* Generated automatically from machmode.def%s%s\n",
1347 HAVE_EXTRA_MODES ? " and " : "",
1348 EXTRA_MODES_FILE);
1349
1350 puts ("\
1351 by genmodes. */\n\
1352 \n\
1353 #ifndef GCC_INSN_MODES_INLINE_H\n\
1354 #define GCC_INSN_MODES_INLINE_H");
1355
1356 puts ("\n#if !defined (USED_FOR_TARGET) && GCC_VERSION >= 4001\n");
1357 emit_mode_size_inline ();
1358 emit_mode_nunits_inline ();
1359 emit_mode_inner_inline ();
1360 emit_mode_unit_size_inline ();
1361 emit_mode_unit_precision_inline ();
1362 puts ("#endif /* GCC_VERSION >= 4001 */");
1363
1364 puts ("\
1365 \n\
1366 #endif /* insn-modes-inline.h */");
1367 }
1368
1369 static void
1370 emit_insn_modes_c_header (void)
1371 {
1372 printf ("/* Generated automatically from machmode.def%s%s\n",
1373 HAVE_EXTRA_MODES ? " and " : "",
1374 EXTRA_MODES_FILE);
1375
1376 puts ("\
1377 by genmodes. */\n\
1378 \n\
1379 #include \"config.h\"\n\
1380 #include \"system.h\"\n\
1381 #include \"coretypes.h\"\n\
1382 #include \"tm.h\"\n\
1383 #include \"real.h\"");
1384 }
1385
1386 static void
1387 emit_min_insn_modes_c_header (void)
1388 {
1389 printf ("/* Generated automatically from machmode.def%s%s\n",
1390 HAVE_EXTRA_MODES ? " and " : "",
1391 EXTRA_MODES_FILE);
1392
1393 puts ("\
1394 by genmodes. */\n\
1395 \n\
1396 #include \"bconfig.h\"\n\
1397 #include \"system.h\"\n\
1398 #include \"coretypes.h\"");
1399 }
1400
1401 static void
1402 emit_mode_name (void)
1403 {
1404 int c;
1405 struct mode_data *m;
1406
1407 print_decl ("char *const", "mode_name", "NUM_MACHINE_MODES");
1408
1409 for_all_modes (c, m)
1410 printf (" \"%s\",\n", m->name);
1411
1412 print_closer ();
1413 }
1414
1415 static void
1416 emit_mode_class (void)
1417 {
1418 int c;
1419 struct mode_data *m;
1420
1421 print_decl ("unsigned char", "mode_class", "NUM_MACHINE_MODES");
1422
1423 for_all_modes (c, m)
1424 tagged_printf ("%s", mode_class_names[m->cl], m->name);
1425
1426 print_closer ();
1427 }
1428
1429 static void
1430 emit_mode_precision (void)
1431 {
1432 int c;
1433 struct mode_data *m;
1434
1435 print_maybe_const_decl ("%spoly_uint16_pod", "mode_precision",
1436 "NUM_MACHINE_MODES", adj_nunits);
1437
1438 for_all_modes (c, m)
1439 if (m->precision != (unsigned int)-1)
1440 tagged_printf ("{ %u" ZERO_COEFFS " }", m->precision, m->name);
1441 else
1442 tagged_printf ("{ %u * BITS_PER_UNIT" ZERO_COEFFS " }",
1443 m->bytesize, m->name);
1444
1445 print_closer ();
1446 }
1447
1448 static void
1449 emit_mode_size (void)
1450 {
1451 int c;
1452 struct mode_data *m;
1453
1454 print_maybe_const_decl ("%spoly_uint16_pod", "mode_size",
1455 "NUM_MACHINE_MODES", adj_nunits || adj_bytesize);
1456
1457 for_all_modes (c, m)
1458 tagged_printf ("{ %u" ZERO_COEFFS " }", m->bytesize, m->name);
1459
1460 print_closer ();
1461 }
1462
1463 static void
1464 emit_mode_nunits (void)
1465 {
1466 int c;
1467 struct mode_data *m;
1468
1469 print_maybe_const_decl ("%spoly_uint16_pod", "mode_nunits",
1470 "NUM_MACHINE_MODES", adj_nunits);
1471
1472 for_all_modes (c, m)
1473 tagged_printf ("{ %u" ZERO_COEFFS " }", m->ncomponents, m->name);
1474
1475 print_closer ();
1476 }
1477
1478 static void
1479 emit_mode_wider (void)
1480 {
1481 int c;
1482 struct mode_data *m;
1483
1484 print_decl ("unsigned char", "mode_wider", "NUM_MACHINE_MODES");
1485
1486 for_all_modes (c, m)
1487 tagged_printf ("E_%smode",
1488 m->wider ? m->wider->name : void_mode->name,
1489 m->name);
1490
1491 print_closer ();
1492 print_decl ("unsigned char", "mode_2xwider", "NUM_MACHINE_MODES");
1493
1494 for_all_modes (c, m)
1495 {
1496 struct mode_data * m2;
1497
1498 for (m2 = m;
1499 m2 && m2 != void_mode;
1500 m2 = m2->wider)
1501 {
1502 if (m2->bytesize < 2 * m->bytesize)
1503 continue;
1504 if (m->precision != (unsigned int) -1)
1505 {
1506 if (m2->precision != 2 * m->precision)
1507 continue;
1508 }
1509 else
1510 {
1511 if (m2->precision != (unsigned int) -1)
1512 continue;
1513 }
1514
1515 /* For vectors we want twice the number of components,
1516 with the same element type. */
1517 if (m->cl == MODE_VECTOR_BOOL
1518 || m->cl == MODE_VECTOR_INT
1519 || m->cl == MODE_VECTOR_FLOAT
1520 || m->cl == MODE_VECTOR_FRACT
1521 || m->cl == MODE_VECTOR_UFRACT
1522 || m->cl == MODE_VECTOR_ACCUM
1523 || m->cl == MODE_VECTOR_UACCUM)
1524 {
1525 if (m2->ncomponents != 2 * m->ncomponents)
1526 continue;
1527 if (m->component != m2->component)
1528 continue;
1529 }
1530
1531 break;
1532 }
1533 if (m2 == void_mode)
1534 m2 = 0;
1535 tagged_printf ("E_%smode",
1536 m2 ? m2->name : void_mode->name,
1537 m->name);
1538 }
1539
1540 print_closer ();
1541 }
1542
1543 static void
1544 emit_mode_complex (void)
1545 {
1546 int c;
1547 struct mode_data *m;
1548
1549 print_decl ("unsigned char", "mode_complex", "NUM_MACHINE_MODES");
1550
1551 for_all_modes (c, m)
1552 tagged_printf ("E_%smode",
1553 m->complex ? m->complex->name : void_mode->name,
1554 m->name);
1555
1556 print_closer ();
1557 }
1558
1559 static void
1560 emit_mode_mask (void)
1561 {
1562 int c;
1563 struct mode_data *m;
1564
1565 print_maybe_const_decl ("%sunsigned HOST_WIDE_INT", "mode_mask_array",
1566 "NUM_MACHINE_MODES", adj_nunits);
1567 puts ("\
1568 #define MODE_MASK(m) \\\n\
1569 ((m) >= HOST_BITS_PER_WIDE_INT) \\\n\
1570 ? HOST_WIDE_INT_M1U \\\n\
1571 : (HOST_WIDE_INT_1U << (m)) - 1\n");
1572
1573 for_all_modes (c, m)
1574 if (m->precision != (unsigned int)-1)
1575 tagged_printf ("MODE_MASK (%u)", m->precision, m->name);
1576 else
1577 tagged_printf ("MODE_MASK (%u*BITS_PER_UNIT)", m->bytesize, m->name);
1578
1579 puts ("#undef MODE_MASK");
1580 print_closer ();
1581 }
1582
1583 static void
1584 emit_mode_inner (void)
1585 {
1586 int c;
1587 struct mode_data *m;
1588
1589 print_decl ("unsigned char", "mode_inner", "NUM_MACHINE_MODES");
1590
1591 for_all_modes (c, m)
1592 tagged_printf ("E_%smode",
1593 c != MODE_PARTIAL_INT && m->component
1594 ? m->component->name : m->name,
1595 m->name);
1596
1597 print_closer ();
1598 }
1599
1600 /* Emit mode_unit_size array into insn-modes.c file. */
1601 static void
1602 emit_mode_unit_size (void)
1603 {
1604 int c;
1605 struct mode_data *m;
1606
1607 print_maybe_const_decl ("%sunsigned char", "mode_unit_size",
1608 "NUM_MACHINE_MODES", adj_bytesize);
1609
1610 for_all_modes (c, m)
1611 tagged_printf ("%u",
1612 c != MODE_PARTIAL_INT && m->component
1613 ? m->component->bytesize : m->bytesize, m->name);
1614
1615 print_closer ();
1616 }
1617
1618 /* Emit mode_unit_precision array into insn-modes.c file. */
1619 static void
1620 emit_mode_unit_precision (void)
1621 {
1622 int c;
1623 struct mode_data *m;
1624
1625 print_decl ("unsigned short", "mode_unit_precision", "NUM_MACHINE_MODES");
1626
1627 for_all_modes (c, m)
1628 {
1629 struct mode_data *m2 = (c != MODE_PARTIAL_INT && m->component) ?
1630 m->component : m;
1631 if (m2->precision != (unsigned int)-1)
1632 tagged_printf ("%u", m2->precision, m->name);
1633 else
1634 tagged_printf ("%u*BITS_PER_UNIT", m2->bytesize, m->name);
1635 }
1636
1637 print_closer ();
1638 }
1639
1640
1641 static void
1642 emit_mode_base_align (void)
1643 {
1644 int c;
1645 struct mode_data *m;
1646
1647 print_maybe_const_decl ("%sunsigned short",
1648 "mode_base_align", "NUM_MACHINE_MODES",
1649 adj_alignment);
1650
1651 for_all_modes (c, m)
1652 tagged_printf ("%u", m->alignment, m->name);
1653
1654 print_closer ();
1655 }
1656
1657 static void
1658 emit_class_narrowest_mode (void)
1659 {
1660 int c;
1661
1662 print_decl ("unsigned char", "class_narrowest_mode", "MAX_MODE_CLASS");
1663
1664 for (c = 0; c < MAX_MODE_CLASS; c++)
1665 /* Bleah, all this to get the comment right for MIN_MODE_INT. */
1666 tagged_printf ("MIN_%s", mode_class_names[c],
1667 modes[c]
1668 ? ((c != MODE_INT || modes[c]->precision != 1)
1669 ? modes[c]->name
1670 : (modes[c]->next
1671 ? modes[c]->next->name
1672 : void_mode->name))
1673 : void_mode->name);
1674
1675 print_closer ();
1676 }
1677
1678 static void
1679 emit_real_format_for_mode (void)
1680 {
1681 struct mode_data *m;
1682
1683 /* The entities pointed to by this table are constant, whether
1684 or not the table itself is constant.
1685
1686 For backward compatibility this table is always writable
1687 (several targets modify it in TARGET_OPTION_OVERRIDE). FIXME:
1688 convert all said targets to use ADJUST_FORMAT instead. */
1689 #if 0
1690 print_maybe_const_decl ("const struct real_format *%s",
1691 "real_format_for_mode",
1692 "MAX_MODE_FLOAT - MIN_MODE_FLOAT + 1",
1693 format);
1694 #else
1695 print_decl ("struct real_format *\n", "real_format_for_mode",
1696 "MAX_MODE_FLOAT - MIN_MODE_FLOAT + 1 "
1697 "+ MAX_MODE_DECIMAL_FLOAT - MIN_MODE_DECIMAL_FLOAT + 1");
1698 #endif
1699
1700 /* The beginning of the table is entries for float modes. */
1701 for (m = modes[MODE_FLOAT]; m; m = m->next)
1702 if (!strcmp (m->format, "0"))
1703 tagged_printf ("%s", m->format, m->name);
1704 else
1705 tagged_printf ("&%s", m->format, m->name);
1706
1707 /* The end of the table is entries for decimal float modes. */
1708 for (m = modes[MODE_DECIMAL_FLOAT]; m; m = m->next)
1709 if (!strcmp (m->format, "0"))
1710 tagged_printf ("%s", m->format, m->name);
1711 else
1712 tagged_printf ("&%s", m->format, m->name);
1713
1714 print_closer ();
1715 }
1716
1717 static void
1718 emit_mode_adjustments (void)
1719 {
1720 struct mode_adjust *a;
1721 struct mode_data *m;
1722
1723 if (adj_nunits)
1724 printf ("\n"
1725 "void\n"
1726 "adjust_mode_mask (machine_mode mode)\n"
1727 "{\n"
1728 " unsigned int precision;\n"
1729 " if (GET_MODE_PRECISION (mode).is_constant (&precision)\n"
1730 " && precision < HOST_BITS_PER_WIDE_INT)\n"
1731 " mode_mask_array[mode] = (HOST_WIDE_INT_1U << precision) - 1;"
1732 "\n"
1733 " else\n"
1734 " mode_mask_array[mode] = HOST_WIDE_INT_M1U;\n"
1735 "}\n");
1736
1737 puts ("\
1738 \nvoid\
1739 \ninit_adjust_machine_modes (void)\
1740 \n{\
1741 \n poly_uint16 ps ATTRIBUTE_UNUSED;\n\
1742 size_t s ATTRIBUTE_UNUSED;");
1743
1744 for (a = adj_nunits; a; a = a->next)
1745 {
1746 m = a->mode;
1747 printf ("\n"
1748 " {\n"
1749 " /* %s:%d */\n ps = %s;\n",
1750 a->file, a->line, a->adjustment);
1751 printf (" int old_factor = vector_element_size"
1752 " (mode_precision[E_%smode], mode_nunits[E_%smode]);\n",
1753 m->name, m->name);
1754 printf (" mode_precision[E_%smode] = ps * old_factor;\n", m->name);
1755 printf (" mode_size[E_%smode] = exact_div (mode_precision[E_%smode],"
1756 " BITS_PER_UNIT);\n", m->name, m->name);
1757 printf (" mode_nunits[E_%smode] = ps;\n", m->name);
1758 printf (" adjust_mode_mask (E_%smode);\n", m->name);
1759 printf (" }\n");
1760 }
1761
1762 /* Size adjustments must be propagated to all containing modes.
1763 A size adjustment forces us to recalculate the alignment too. */
1764 for (a = adj_bytesize; a; a = a->next)
1765 {
1766 printf ("\n /* %s:%d */\n", a->file, a->line);
1767 switch (a->mode->cl)
1768 {
1769 case MODE_VECTOR_BOOL:
1770 case MODE_VECTOR_INT:
1771 case MODE_VECTOR_FLOAT:
1772 case MODE_VECTOR_FRACT:
1773 case MODE_VECTOR_UFRACT:
1774 case MODE_VECTOR_ACCUM:
1775 case MODE_VECTOR_UACCUM:
1776 printf (" ps = %s;\n", a->adjustment);
1777 printf (" s = mode_unit_size[E_%smode];\n", a->mode->name);
1778 break;
1779
1780 default:
1781 printf (" ps = s = %s;\n", a->adjustment);
1782 printf (" mode_unit_size[E_%smode] = s;\n", a->mode->name);
1783 break;
1784 }
1785 printf (" mode_size[E_%smode] = ps;\n", a->mode->name);
1786 printf (" mode_base_align[E_%smode] = known_alignment (ps);\n",
1787 a->mode->name);
1788
1789 for (m = a->mode->contained; m; m = m->next_cont)
1790 {
1791 switch (m->cl)
1792 {
1793 case MODE_COMPLEX_INT:
1794 case MODE_COMPLEX_FLOAT:
1795 printf (" mode_size[E_%smode] = 2*s;\n", m->name);
1796 printf (" mode_unit_size[E_%smode] = s;\n", m->name);
1797 printf (" mode_base_align[E_%smode] = s & (~s + 1);\n",
1798 m->name);
1799 break;
1800
1801 case MODE_VECTOR_BOOL:
1802 /* Changes to BImode should not affect vector booleans. */
1803 break;
1804
1805 case MODE_VECTOR_INT:
1806 case MODE_VECTOR_FLOAT:
1807 case MODE_VECTOR_FRACT:
1808 case MODE_VECTOR_UFRACT:
1809 case MODE_VECTOR_ACCUM:
1810 case MODE_VECTOR_UACCUM:
1811 printf (" mode_size[E_%smode] = %d * ps;\n",
1812 m->name, m->ncomponents);
1813 printf (" mode_unit_size[E_%smode] = s;\n", m->name);
1814 printf (" mode_base_align[E_%smode]"
1815 " = known_alignment (%d * ps);\n",
1816 m->name, m->ncomponents);
1817 break;
1818
1819 default:
1820 internal_error (
1821 "mode %s is neither vector nor complex but contains %s",
1822 m->name, a->mode->name);
1823 /* NOTREACHED */
1824 }
1825 }
1826 }
1827
1828 /* Alignment adjustments propagate too.
1829 ??? This may not be the right thing for vector modes. */
1830 for (a = adj_alignment; a; a = a->next)
1831 {
1832 printf ("\n /* %s:%d */\n s = %s;\n",
1833 a->file, a->line, a->adjustment);
1834 printf (" mode_base_align[E_%smode] = s;\n", a->mode->name);
1835
1836 for (m = a->mode->contained; m; m = m->next_cont)
1837 {
1838 switch (m->cl)
1839 {
1840 case MODE_COMPLEX_INT:
1841 case MODE_COMPLEX_FLOAT:
1842 printf (" mode_base_align[E_%smode] = s;\n", m->name);
1843 break;
1844
1845 case MODE_VECTOR_BOOL:
1846 /* Changes to BImode should not affect vector booleans. */
1847 break;
1848
1849 case MODE_VECTOR_INT:
1850 case MODE_VECTOR_FLOAT:
1851 case MODE_VECTOR_FRACT:
1852 case MODE_VECTOR_UFRACT:
1853 case MODE_VECTOR_ACCUM:
1854 case MODE_VECTOR_UACCUM:
1855 printf (" mode_base_align[E_%smode] = %d*s;\n",
1856 m->name, m->ncomponents);
1857 break;
1858
1859 default:
1860 internal_error (
1861 "mode %s is neither vector nor complex but contains %s",
1862 m->name, a->mode->name);
1863 /* NOTREACHED */
1864 }
1865 }
1866 }
1867
1868 /* Ibit adjustments don't have to propagate. */
1869 for (a = adj_ibit; a; a = a->next)
1870 {
1871 printf ("\n /* %s:%d */\n s = %s;\n",
1872 a->file, a->line, a->adjustment);
1873 printf (" mode_ibit[E_%smode] = s;\n", a->mode->name);
1874 }
1875
1876 /* Fbit adjustments don't have to propagate. */
1877 for (a = adj_fbit; a; a = a->next)
1878 {
1879 printf ("\n /* %s:%d */\n s = %s;\n",
1880 a->file, a->line, a->adjustment);
1881 printf (" mode_fbit[E_%smode] = s;\n", a->mode->name);
1882 }
1883
1884 /* Real mode formats don't have to propagate anywhere. */
1885 for (a = adj_format; a; a = a->next)
1886 printf ("\n /* %s:%d */\n REAL_MODE_FORMAT (E_%smode) = %s;\n",
1887 a->file, a->line, a->mode->name, a->adjustment);
1888
1889 puts ("}");
1890 }
1891
1892 /* Emit ibit for all modes. */
1893
1894 static void
1895 emit_mode_ibit (void)
1896 {
1897 int c;
1898 struct mode_data *m;
1899
1900 print_maybe_const_decl ("%sunsigned char",
1901 "mode_ibit", "NUM_MACHINE_MODES",
1902 adj_ibit);
1903
1904 for_all_modes (c, m)
1905 tagged_printf ("%u", m->ibit, m->name);
1906
1907 print_closer ();
1908 }
1909
1910 /* Emit fbit for all modes. */
1911
1912 static void
1913 emit_mode_fbit (void)
1914 {
1915 int c;
1916 struct mode_data *m;
1917
1918 print_maybe_const_decl ("%sunsigned char",
1919 "mode_fbit", "NUM_MACHINE_MODES",
1920 adj_fbit);
1921
1922 for_all_modes (c, m)
1923 tagged_printf ("%u", m->fbit, m->name);
1924
1925 print_closer ();
1926 }
1927
1928 /* Emit __intN for all modes. */
1929
1930 static void
1931 emit_mode_int_n (void)
1932 {
1933 int c;
1934 struct mode_data *m;
1935 struct mode_data **mode_sort;
1936 int n_modes = 0;
1937 int i, j;
1938
1939 print_decl ("int_n_data_t", "int_n_data", "");
1940
1941 n_modes = 0;
1942 for_all_modes (c, m)
1943 if (m->int_n)
1944 n_modes ++;
1945 mode_sort = XALLOCAVEC (struct mode_data *, n_modes);
1946
1947 n_modes = 0;
1948 for_all_modes (c, m)
1949 if (m->int_n)
1950 mode_sort[n_modes++] = m;
1951
1952 /* Yes, this is a bubblesort, but there are at most four (and
1953 usually only 1-2) entries to sort. */
1954 for (i = 0; i<n_modes - 1; i++)
1955 for (j = i + 1; j < n_modes; j++)
1956 if (mode_sort[i]->int_n > mode_sort[j]->int_n)
1957 std::swap (mode_sort[i], mode_sort[j]);
1958
1959 for (i = 0; i < n_modes; i ++)
1960 {
1961 m = mode_sort[i];
1962 printf(" {\n");
1963 tagged_printf ("%u", m->int_n, m->name);
1964 printf ("{ E_%smode },", m->name);
1965 printf(" },\n");
1966 }
1967
1968 print_closer ();
1969 }
1970
1971
1972 static void
1973 emit_insn_modes_c (void)
1974 {
1975 emit_insn_modes_c_header ();
1976 emit_mode_name ();
1977 emit_mode_class ();
1978 emit_mode_precision ();
1979 emit_mode_size ();
1980 emit_mode_nunits ();
1981 emit_mode_wider ();
1982 emit_mode_complex ();
1983 emit_mode_mask ();
1984 emit_mode_inner ();
1985 emit_mode_unit_size ();
1986 emit_mode_unit_precision ();
1987 emit_mode_base_align ();
1988 emit_class_narrowest_mode ();
1989 emit_real_format_for_mode ();
1990 emit_mode_adjustments ();
1991 emit_mode_ibit ();
1992 emit_mode_fbit ();
1993 emit_mode_int_n ();
1994 }
1995
1996 static void
1997 emit_min_insn_modes_c (void)
1998 {
1999 emit_min_insn_modes_c_header ();
2000 emit_mode_name ();
2001 emit_mode_class ();
2002 emit_mode_nunits ();
2003 emit_mode_wider ();
2004 emit_mode_inner ();
2005 emit_class_narrowest_mode ();
2006 }
2007
2008 /* Master control. */
2009 int
2010 main (int argc, char **argv)
2011 {
2012 bool gen_header = false, gen_inlines = false, gen_min = false;
2013 progname = argv[0];
2014
2015 if (argc == 1)
2016 ;
2017 else if (argc == 2 && !strcmp (argv[1], "-h"))
2018 gen_header = true;
2019 else if (argc == 2 && !strcmp (argv[1], "-i"))
2020 gen_inlines = true;
2021 else if (argc == 2 && !strcmp (argv[1], "-m"))
2022 gen_min = true;
2023 else
2024 {
2025 error ("usage: %s [-h|-i|-m] > file", progname);
2026 return FATAL_EXIT_CODE;
2027 }
2028
2029 modes_by_name = htab_create_alloc (64, hash_mode, eq_mode, 0, xcalloc, free);
2030
2031 create_modes ();
2032 complete_all_modes ();
2033
2034 if (have_error)
2035 return FATAL_EXIT_CODE;
2036
2037 calc_wider_mode ();
2038
2039 if (gen_header)
2040 emit_insn_modes_h ();
2041 else if (gen_inlines)
2042 emit_insn_modes_inline_h ();
2043 else if (gen_min)
2044 emit_min_insn_modes_c ();
2045 else
2046 emit_insn_modes_c ();
2047
2048 if (fflush (stdout) || fclose (stdout))
2049 return FATAL_EXIT_CODE;
2050 return SUCCESS_EXIT_CODE;
2051 }