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