Debian Codegraph @

gcc/config/rs6000/rs6000-gen-builtins.cc

raw
1 /* Generate built-in function initialization and recognition for Power.
2 Copyright (C) 2020-2025 Free Software Foundation, Inc.
3 Contributed by Bill Schmidt, IBM <[email protected]>
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
11
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
20
21 /* This program generates built-in function initialization and
22 recognition code for Power targets, based on text files that
23 describe the built-in functions and vector overloads:
24
25 rs6000-builtins.def Table of built-in functions
26 rs6000-overload.def Table of overload functions
27
28 Both files group similar functions together in "stanzas," as
29 described below.
30
31 Each stanza in the built-in function file starts with a line
32 identifying the circumstances in which the group of functions is
33 permitted, with the gating predicate in square brackets. For
34 example, this could be
35
36 [altivec]
37
38 or it could be
39
40 [power9]
41
42 The bracketed gating predicate is the only information allowed on
43 the stanza header line, other than whitespace.
44
45 Following the stanza header are two lines for each function: the
46 prototype line and the attributes line. The prototype line has
47 this format, where the square brackets indicate optional
48 information and angle brackets indicate required information:
49
50 [kind] <return-type> <bif-name> (<argument-list>);
51
52 Here [kind] can be one of "const", "pure", or "fpmath";
53 <return-type> is a legal type for a built-in function result;
54 <bif-name> is the name by which the function can be called;
55 and <argument-list> is a comma-separated list of legal types
56 for built-in function arguments. The argument list may be
57 empty, but the parentheses and semicolon are required.
58
59 The attributes line looks like this:
60
61 <bif-id> <bif-pattern> {<attribute-list>}
62
63 Here <bif-id> is a unique internal identifier for the built-in
64 function that will be used as part of an enumeration of all
65 built-in functions; <bif-pattern> is the define_expand or
66 define_insn that will be invoked when the call is expanded;
67 and <attribute-list> is a comma-separated list of special
68 conditions that apply to the built-in function. The attribute
69 list may be empty, but the braces are required.
70
71 Attributes are strings, such as these:
72
73 set Process as a vec_set function
74 extract Process as a vec_extract function
75 nosoft Not valid with -msoft-float
76 ldvec Needs special handling for vec_ld semantics
77 stvec Needs special handling for vec_st semantics
78 reve Needs special handling for element reversal
79 pred Needs special handling for comparison predicates
80 htm Needs special handling for transactional memory
81 htmspr HTM function using an SPR
82 htmcr HTM function using a CR
83 mma Needs special handling for MMA instructions
84 quad MMA instruction using a register quad as an input operand
85 pair MMA instruction using a register pair as an input operand
86 mmaint MMA instruction expanding to internal call at GIMPLE time
87 no32bit Not valid for TARGET_32BIT
88 32bit Requires different handling for TARGET_32BIT
89 cpu This is a "cpu_is" or "cpu_supports" builtin
90 ldstmask Altivec mask for load or store
91 lxvrse Needs special handling for load-rightmost, sign-extended
92 lxvrze Needs special handling for load-rightmost, zero-extended
93 endian Needs special handling for endianness
94 ibmld Restrict usage to the case when TFmode is IBM-128
95 ibm128 Restrict usage to the case where __ibm128 is supported or
96 if ibmld
97
98 An example stanza might look like this:
99
100 [altivec]
101 const vsc __builtin_altivec_abs_v16qi (vsc);
102 ABS_V16QI absv16qi2 {}
103 const vss __builtin_altivec_abs_v8hi (vss);
104 ABS_V8HI absv8hi2 {}
105
106 Here "vsc" and "vss" are shorthand for "vector signed char" and
107 "vector signed short" to shorten line lengths and improve readability.
108 Note the use of indentation, which is recommended but not required.
109
110 The overload file has more complex stanza headers. Here the stanza
111 represents all functions with the same overloaded function name:
112
113 [<overload-id>, <abi-name>, <builtin-name>[[, <ifdef>]] ]
114
115 Here the single square brackets are part of the syntax, <overload-id>
116 is a unique internal identifier for the overload that will be used as
117 part of an enumeration of all overloaded functions; <abi-name> is the
118 name that will appear as a #define in rs6000-vecdefines.h;
119 <builtin-name> is the name that is overloaded in the back end; and
120 <ifdef> is an optional token used to guard the #define with an #ifdef
121 in rs6000-vecdefines.h.
122
123 Each function entry again has two lines. The first line is again a
124 prototype line (this time without [kind]):
125
126 <return-type> <internal-name> (<argument-list>);
127
128 The second line contains the <bif-id> that this particular instance of
129 the overloaded function maps to. It must match a token that appears in
130 rs6000-builtins.def. Optionally, a second token may appear. If only
131 one token is on the line, it is also used to build the unique identifier
132 for the overloaded function. If a second token is present, the second
133 token is used instead for this purpose. This is necessary in cases
134 where a built-in function accepts more than one type signature. It is
135 common to have a built-in function that, for example, specifies a
136 "vector signed char" argument, but accepts "vector unsigned char" and
137 "vector bool char" as well because only the mode matters. Note that
138 the overload resolution mechanism has always handled these cases by
139 performing fold_convert on vector arguments to hide type mismatches,
140 and it will continue to do so.
141
142 As a concrete example, __builtin_altivec_mtvscr uses an opaque argument
143 type for the source operand. Its built-in function id is MTVSCR. The
144 overloaded function __builtin_vec_mtvscr takes a variety of specific
145 types, but not all vector types. Each of these maps to the same
146 __builtin_altivec_mtvscr built-in function, but the overload ID must
147 be unique, so we must specify the second token as shown here.
148
149 [VEC_MTVSCR, vec_mtvscr, __builtin_vec_mtvscr]
150 void __builtin_vec_mtvscr (vbc);
151 MTVSCR MTVSCR_VBC
152 void __builtin_vec_mtvscr (vsc);
153 MTVSCR MTVSCR_VSC
154 ...
155
156 Blank lines may be used as desired in these files between the lines as
157 defined above; that is, you can introduce as many extra newlines as you
158 like after a required newline, but nowhere else. Lines beginning with
159 a semicolon are also treated as blank lines. */
160
161 #include <stdio.h>
162 #include <stdlib.h>
163 #include <stdarg.h>
164 #include <stdint.h>
165 #include <ctype.h>
166 #include <string.h>
167 #include <assert.h>
168 #include <unistd.h>
169 #include "rbtree.h"
170
171 /* Input and output file descriptors and pathnames. */
172 static FILE *bif_file;
173 static FILE *ovld_file;
174 static FILE *header_file;
175 static FILE *init_file;
176 static FILE *defines_file;
177
178 static const char *pgm_path;
179 static const char *bif_path;
180 static const char *ovld_path;
181 static const char *header_path;
182 static const char *init_path;
183 static const char *defines_path;
184
185 /* Position information. Note that "pos" is zero-indexed, but users
186 expect one-indexed column information, so representations of "pos"
187 as columns in diagnostic messages must be adjusted. */
188 #define MAXLINES 4
189 #define LINELEN 1024
190 static char linebuf[LINELEN * MAXLINES];
191 static int line;
192 static int pos;
193
194 /* Escape-newline support. For readability, we prefer to allow developers
195 to use escape-newline to continue long lines to the next one. We
196 maintain a buffer of "original" lines here, which are concatenated into
197 linebuf, above, and which can be used to convert the virtual line
198 position "line / pos" into actual line and position information. */
199 static char *lines[MAXLINES];
200 static int lastline;
201
202 /* Used to determine whether a type can be void (only return types). */
203 enum void_status
204 {
205 VOID_NOTOK,
206 VOID_OK
207 };
208
209 /* Stanzas are groupings of built-in functions and overloads by some
210 common feature/attribute. These definitions are for built-in function
211 stanzas. */
212 enum bif_stanza
213 {
214 BSTZ_ALWAYS,
215 BSTZ_P5,
216 BSTZ_P6,
217 BSTZ_P6_64,
218 BSTZ_ALTIVEC,
219 BSTZ_CELL,
220 BSTZ_VSX,
221 BSTZ_P7,
222 BSTZ_P7_64,
223 BSTZ_P8,
224 BSTZ_P8V,
225 BSTZ_P9,
226 BSTZ_P9_64,
227 BSTZ_P9V,
228 BSTZ_IEEE128_HW,
229 BSTZ_DFP,
230 BSTZ_CRYPTO,
231 BSTZ_HTM,
232 BSTZ_P10,
233 BSTZ_P10_64,
234 BSTZ_MMA,
235 NUMBIFSTANZAS
236 };
237
238 static bif_stanza curr_bif_stanza;
239
240 struct stanza_entry
241 {
242 const char *stanza_name;
243 bif_stanza stanza;
244 };
245
246 static stanza_entry stanza_map[NUMBIFSTANZAS] =
247 {
248 { "always", BSTZ_ALWAYS },
249 { "power5", BSTZ_P5 },
250 { "power6", BSTZ_P6 },
251 { "power6-64", BSTZ_P6_64 },
252 { "altivec", BSTZ_ALTIVEC },
253 { "cell", BSTZ_CELL },
254 { "vsx", BSTZ_VSX },
255 { "power7", BSTZ_P7 },
256 { "power7-64", BSTZ_P7_64 },
257 { "power8", BSTZ_P8 },
258 { "power8-vector", BSTZ_P8V },
259 { "power9", BSTZ_P9 },
260 { "power9-64", BSTZ_P9_64 },
261 { "power9-vector", BSTZ_P9V },
262 { "ieee128-hw", BSTZ_IEEE128_HW },
263 { "dfp", BSTZ_DFP },
264 { "crypto", BSTZ_CRYPTO },
265 { "htm", BSTZ_HTM },
266 { "power10", BSTZ_P10 },
267 { "power10-64", BSTZ_P10_64 },
268 { "mma", BSTZ_MMA }
269 };
270
271 static const char *enable_string[NUMBIFSTANZAS] =
272 {
273 "ENB_ALWAYS",
274 "ENB_P5",
275 "ENB_P6",
276 "ENB_P6_64",
277 "ENB_ALTIVEC",
278 "ENB_CELL",
279 "ENB_VSX",
280 "ENB_P7",
281 "ENB_P7_64",
282 "ENB_P8",
283 "ENB_P8V",
284 "ENB_P9",
285 "ENB_P9_64",
286 "ENB_P9V",
287 "ENB_IEEE128_HW",
288 "ENB_DFP",
289 "ENB_CRYPTO",
290 "ENB_HTM",
291 "ENB_P10",
292 "ENB_P10_64",
293 "ENB_MMA"
294 };
295
296 /* Function modifiers provide special handling for const, pure, and fpmath
297 functions. These are mutually exclusive, and therefore kept separate
298 from other bif attributes. */
299 enum fnkinds
300 {
301 FNK_NONE,
302 FNK_CONST,
303 FNK_PURE,
304 FNK_FPMATH
305 };
306
307 /* Legal base types for an argument or return type. */
308 enum basetype
309 {
310 BT_CHAR,
311 BT_SHORT,
312 BT_INT,
313 BT_LONG,
314 BT_LONGLONG,
315 BT_FLOAT,
316 BT_DOUBLE,
317 BT_LONGDOUBLE,
318 BT_INT128,
319 BT_FLOAT128,
320 BT_BOOL,
321 BT_STRING,
322 BT_DECIMAL32,
323 BT_DECIMAL64,
324 BT_DECIMAL128,
325 BT_IBM128,
326 BT_VPAIR,
327 BT_VQUAD
328 };
329
330 /* Ways in which a const int value can be restricted. RES_BITS indicates
331 that the integer is restricted to val1 bits, interpreted as an unsigned
332 number. RES_RANGE indicates that the integer is restricted to values
333 between val1 and val2, inclusive. RES_VAR_RANGE is like RES_RANGE, but
334 the argument may be variable, so it can only be checked if it is constant.
335 RES_VALUES indicates that the integer must have one of the values val1
336 or val2. */
337 enum restriction
338 {
339 RES_NONE,
340 RES_BITS,
341 RES_RANGE,
342 RES_VAR_RANGE,
343 RES_VALUES
344 };
345
346 /* Type modifiers for an argument or return type. */
347 struct typeinfo
348 {
349 char isvoid;
350 char isconst;
351 char isvector;
352 char issigned;
353 char isunsigned;
354 char isbool;
355 char ispixel;
356 char ispointer;
357 basetype base;
358 restriction restr;
359 char *val1;
360 char *val2;
361 };
362
363 /* A list of argument types. */
364 struct typelist
365 {
366 typeinfo info;
367 typelist *next;
368 };
369
370 /* Attributes of a builtin function. */
371 struct attrinfo
372 {
373 bool isextract;
374 bool isnosoft;
375 bool isldvec;
376 bool isstvec;
377 bool isreve;
378 bool ispred;
379 bool ishtm;
380 bool ishtmspr;
381 bool ishtmcr;
382 bool ismma;
383 bool isquad;
384 bool ispair;
385 bool ismmaint;
386 bool isno32bit;
387 bool is32bit;
388 bool iscpu;
389 bool isldstmask;
390 bool islxvrse;
391 bool islxvrze;
392 bool isendian;
393 bool isibmld;
394 bool isibm128;
395 };
396
397 /* Fields associated with a function prototype (bif or overload). */
398 #define MAXRESTROPNDS 3
399 struct prototype
400 {
401 typeinfo rettype;
402 char *bifname;
403 int nargs;
404 typelist *args;
405 int restr_opnd[MAXRESTROPNDS];
406 restriction restr[MAXRESTROPNDS];
407 char *restr_val1[MAXRESTROPNDS];
408 char *restr_val2[MAXRESTROPNDS];
409 };
410
411 /* Data associated with a builtin function, and a table of such data. */
412 #define MAXBIFS 16384
413 struct bifdata
414 {
415 int stanza;
416 fnkinds kind;
417 prototype proto;
418 char *idname;
419 char *patname;
420 attrinfo attrs;
421 char *fndecl;
422 };
423
424 static bifdata bifs[MAXBIFS];
425 static int num_bifs;
426 static int curr_bif;
427
428 /* Array used to track the order in which built-ins appeared in the
429 built-in file. We reorder them alphabetically but sometimes need
430 this information. */
431 static int *bif_order;
432 static int bif_index = 0;
433
434 /* Stanzas are groupings of built-in functions and overloads by some
435 common feature/attribute. These definitions are for overload stanzas. */
436 struct ovld_stanza
437 {
438 char *stanza_id;
439 char *extern_name;
440 char *intern_name;
441 char *ifdef;
442 };
443
444 #define MAXOVLDSTANZAS 512
445 static ovld_stanza ovld_stanzas[MAXOVLDSTANZAS];
446 static int num_ovld_stanzas;
447 static int curr_ovld_stanza;
448
449 #define MAXOVLDS 16384
450 struct ovlddata
451 {
452 int stanza;
453 prototype proto;
454 char *bif_id_name;
455 char *ovld_id_name;
456 char *fndecl;
457 };
458
459 static ovlddata ovlds[MAXOVLDS];
460 static int num_ovlds;
461 static int curr_ovld;
462 static int max_ovld_args = 0;
463
464 /* Return codes for parsing routines. */
465 enum parse_codes
466 {
467 PC_OK,
468 PC_EOFILE,
469 PC_EOSTANZA,
470 PC_PARSEFAIL
471 };
472
473 /* The red-black trees for built-in function identifiers, built-in
474 overload identifiers, and function type descriptors. */
475 static rbt_strings bif_rbt;
476 static rbt_strings ovld_rbt;
477 static rbt_strings fntype_rbt;
478
479 /* Another red-black tree containing a mapping from built-in function
480 identifiers to the order in which they were encountered. */
481 static rbt_strings bifo_rbt;
482
483 /* Mapping from type tokens to type node names. */
484 struct typemap
485 {
486 const char *key;
487 const char *value;
488 };
489
490 /* This table must be kept in alphabetical order, as we use binary
491 search for table lookups in map_token_to_type_node. The table
492 maps tokens from a fntype string to a tree type. For example,
493 in "si_ftype_hi" we would map "si" to "intSI_type_node" and
494 map "hi" to "intHI_type_node". */
495 static typemap type_map[] =
496 {
497 { "bi", "bool_int" },
498 { "bv16qi", "bool_V16QI" },
499 { "bv1ti", "bool_V1TI" },
500 { "bv2di", "bool_V2DI" },
501 { "bv4si", "bool_V4SI" },
502 { "bv8hi", "bool_V8HI" },
503 { "ci", "integer" },
504 { "dd", "dfloat64" },
505 { "df", "double" },
506 { "di", "long_long_integer" },
507 { "hi", "intHI" },
508 { "if", "ibm128_float_type_node "
509 "? ibm128_float_type_node "
510 ": long_double" },
511 { "ld", "long_double" },
512 { "lg", "long_integer" },
513 { "pbv16qi", "ptr_bool_V16QI" },
514 { "pbv1ti", "ptr_bool_V1TI" },
515 { "pbv2di", "ptr_bool_V2DI" },
516 { "pbv4si", "ptr_bool_V4SI" },
517 { "pbv8hi", "ptr_bool_V8HI" },
518 { "pcvoid", "pcvoid" },
519 { "pdd", "ptr_dfloat64" },
520 { "pdf", "ptr_double" },
521 { "pdi", "ptr_long_long_integer" },
522 { "phi", "ptr_intHI" },
523 { "pld", "ptr_long_double" },
524 { "plg", "ptr_long_integer" },
525 { "pqi", "ptr_intQI" },
526 { "psf", "ptr_float" },
527 { "psi", "ptr_intSI" },
528 { "ptd", "ptr_dfloat128" },
529 { "ptf", "ptr_float128" },
530 { "pti", "ptr_intTI" },
531 { "pudi", "ptr_long_long_unsigned" },
532 { "puhi", "ptr_uintHI" },
533 { "pulg", "ptr_long_unsigned" },
534 { "puqi", "ptr_uintQI" },
535 { "pusi", "ptr_uintSI" },
536 { "puti", "ptr_uintTI" },
537 { "puv16qi", "ptr_unsigned_V16QI" },
538 { "puv1ti", "ptr_unsigned_V1TI" },
539 { "puv2di", "ptr_unsigned_V2DI" },
540 { "puv4si", "ptr_unsigned_V4SI" },
541 { "puv8hi", "ptr_unsigned_V8HI" },
542 { "pv", "ptr" },
543 { "pv16qi", "ptr_V16QI" },
544 { "pv1poi", "ptr_vector_pair" },
545 { "pv1pxi", "ptr_vector_quad" },
546 { "pv1ti", "ptr_V1TI" },
547 { "pv2df", "ptr_V2DF" },
548 { "pv2di", "ptr_V2DI" },
549 { "pv4sf", "ptr_V4SF" },
550 { "pv4si", "ptr_V4SI" },
551 { "pv8hi", "ptr_V8HI" },
552 { "pvp8hi", "ptr_pixel_V8HI" },
553 { "qi", "intQI" },
554 { "sd", "dfloat32" },
555 { "sf", "float" },
556 { "si", "intSI" },
557 { "st", "const_str" },
558 { "td", "dfloat128" },
559 { "tf", "float128" },
560 { "ti", "intTI" },
561 { "udi", "long_long_unsigned" },
562 { "uhi", "unsigned_intHI" },
563 { "ulg", "long_unsigned" },
564 { "uqi", "unsigned_intQI" },
565 { "usi", "unsigned_intSI" },
566 { "uti", "unsigned_intTI" },
567 { "uv16qi", "unsigned_V16QI" },
568 { "uv1ti", "unsigned_V1TI" },
569 { "uv2di", "unsigned_V2DI" },
570 { "uv4si", "unsigned_V4SI" },
571 { "uv8hi", "unsigned_V8HI" },
572 { "v", "void" },
573 { "v16qi", "V16QI" },
574 { "v1poi", "vector_pair" },
575 { "v1pxi", "vector_quad" },
576 { "v1ti", "V1TI" },
577 { "v2df", "V2DF" },
578 { "v2di", "V2DI" },
579 { "v4sf", "V4SF" },
580 { "v4si", "V4SI" },
581 { "v8hi", "V8HI" },
582 { "vp8hi", "pixel_V8HI" },
583 };
584
585 /* From a possibly extended line with a virtual position, calculate
586 the current line and character position. */
587 static void
588 real_line_pos (int diagpos, int *real_line, int *real_pos)
589 {
590 *real_line = line - lastline;
591 *real_pos = diagpos;
592
593 for (int i = 0; i < MAXLINES; i++)
594 {
595 int len = strlen(lines[i]);
596 if (*real_pos <= len)
597 break;
598
599 (*real_line)++;
600 *real_pos -= len - 2;
601 }
602
603 /* Convert from zero-base to one-base for printing. */
604 (*real_pos)++;
605 }
606
607 /* Pointer to a diagnostic function. */
608 static void (*diag) (int, const char *, ...)
609 __attribute__ ((format (printf, 2, 3)));
610
611 /* Custom diagnostics. */
612 static void __attribute__ ((format (printf, 2, 3)))
613 bif_diag (int diagpos, const char * fmt, ...)
614 {
615 va_list args;
616 int real_line, real_pos;
617 real_line_pos (diagpos, &real_line, &real_pos);
618 fprintf (stderr, "%s:%d:%d: ", bif_path, real_line, real_pos);
619 va_start (args, fmt);
620 vfprintf (stderr, fmt, args);
621 va_end (args);
622 }
623
624 static void __attribute__ ((format (printf, 2, 3)))
625 ovld_diag (int diagpos, const char * fmt, ...)
626 {
627 va_list args;
628 int real_line, real_pos;
629 real_line_pos (diagpos, &real_line, &real_pos);
630 fprintf (stderr, "%s:%d:%d: ", ovld_path, real_line, real_pos);
631 va_start (args, fmt);
632 vfprintf (stderr, fmt, args);
633 va_end (args);
634 }
635
636 /* Produce a fatal error message. */
637 static void
638 fatal (const char *msg)
639 {
640 fprintf (stderr, "FATAL: %s\n", msg);
641 abort ();
642 }
643
644 /* Pass over whitespace (other than a newline, which terminates the scan). */
645 static void
646 consume_whitespace (void)
647 {
648 while (pos < LINELEN && isspace(linebuf[pos]) && linebuf[pos] != '\n')
649 pos++;
650
651 if (pos >= LINELEN)
652 {
653 diag (pos, "line length overrun.\n");
654 exit (1);
655 }
656
657 return;
658 }
659
660 /* Get the next nonblank, noncomment line, returning 0 on EOF, 1 otherwise. */
661 static int
662 advance_line (FILE *file)
663 {
664 while (1)
665 {
666 /* Read ahead one line and check for EOF. */
667 if (!fgets (linebuf, sizeof linebuf, file))
668 return 0;
669 line++;
670 size_t len = strlen (linebuf);
671
672 /* Escape-newline processing. */
673 lastline = 0;
674 if (len > 1)
675 {
676 strcpy (lines[0], linebuf);
677 while (linebuf[len - 2] == '\\'
678 && linebuf[len - 1] == '\n')
679 {
680 lastline++;
681 if (lastline == MAXLINES)
682 fatal ("number of supported overflow lines exceeded");
683 line++;
684 if (!fgets (lines[lastline], LINELEN, file))
685 fatal ("unexpected end of file");
686 strcpy (&linebuf[len - 2], lines[lastline]);
687 len += strlen (lines[lastline]) - 2;
688 }
689 }
690
691 if (linebuf[len - 1] != '\n')
692 fatal ("line doesn't terminate with newline");
693 pos = 0;
694 consume_whitespace ();
695 if (linebuf[pos] != '\n' && linebuf[pos] != ';')
696 return 1;
697 }
698 }
699
700 static inline void
701 safe_inc_pos (void)
702 {
703 if (++pos >= LINELEN)
704 {
705 diag (pos, "line length overrun.\n");
706 exit (1);
707 }
708 }
709
710 /* Match an identifier, returning NULL on failure, else a pointer to a
711 buffer containing the identifier. */
712 static char *
713 match_identifier (void)
714 {
715 int lastpos = pos - 1;
716 while (lastpos < LINELEN - 1
717 && (isalnum (linebuf[lastpos + 1]) || linebuf[lastpos + 1] == '_'))
718 ++lastpos;
719
720 if (lastpos >= LINELEN - 1)
721 {
722 diag (lastpos, "line length overrun.\n");
723 exit (1);
724 }
725
726 if (lastpos < pos)
727 return 0;
728
729 char *buf = (char *) malloc (lastpos - pos + 2);
730 memcpy (buf, &linebuf[pos], lastpos - pos + 1);
731 buf[lastpos - pos + 1] = '\0';
732
733 pos = lastpos + 1;
734 return buf;
735 }
736
737 /* Match an integer and return the string representing its value,
738 or a null string on failure. */
739 static char *
740 match_integer (void)
741 {
742 int startpos = pos;
743 if (linebuf[pos] == '-')
744 safe_inc_pos ();
745
746 int lastpos = pos - 1;
747 while (lastpos < LINELEN - 1 && isdigit (linebuf[lastpos + 1]))
748 ++lastpos;
749
750 if (lastpos >= LINELEN - 1)
751 {
752 diag (lastpos, "line length overrun.\n");
753 exit (1);
754 }
755
756 if (lastpos < pos)
757 return NULL;
758
759 pos = lastpos + 1;
760 char *buf = (char *) malloc (lastpos - startpos + 2);
761 memcpy (buf, &linebuf[startpos], lastpos - startpos + 1);
762 buf[lastpos - startpos + 1] = '\0';
763 return buf;
764 }
765
766 /* Match a string up to but not including a ']', and return its value,
767 or zero if there is nothing before the ']'. Error if we don't find
768 such a character. */
769 static const char *
770 match_to_right_bracket (void)
771 {
772 int lastpos = pos - 1;
773 while (lastpos < LINELEN - 1 && linebuf[lastpos + 1] != ']')
774 {
775 if (linebuf[lastpos + 1] == '\n')
776 fatal ("no ']' found before end of line.\n");
777 ++lastpos;
778 }
779
780 if (lastpos >= LINELEN - 1)
781 {
782 diag (lastpos, "line length overrun.\n");
783 exit (1);
784 }
785
786 if (lastpos < pos)
787 return 0;
788
789 char *buf = (char *) malloc (lastpos - pos + 2);
790 memcpy (buf, &linebuf[pos], lastpos - pos + 1);
791 buf[lastpos - pos + 1] = '\0';
792
793 pos = lastpos + 1;
794 return buf;
795 }
796
797 static inline void
798 handle_pointer (typeinfo *typedata)
799 {
800 consume_whitespace ();
801 if (linebuf[pos] == '*')
802 {
803 typedata->ispointer = 1;
804 safe_inc_pos ();
805 }
806 }
807
808 static bif_stanza
809 stanza_name_to_stanza (const char *stanza_name)
810 {
811 for (int i = 0; i < NUMBIFSTANZAS; i++)
812 if (!strcmp (stanza_name, stanza_map[i].stanza_name))
813 return stanza_map[i].stanza;
814 fatal ("Stanza mapping is inconsistent.");
815 /* Unreachable. */
816 return BSTZ_ALWAYS;
817 }
818
819 /* Match one of the allowable base types. Consumes one token unless the
820 token is "long", which must be paired with a second "long". Optionally
821 consumes a following '*' token for pointers. Return 1 for success,
822 0 for failure. */
823 static int
824 match_basetype (typeinfo *typedata)
825 {
826 consume_whitespace ();
827 int oldpos = pos;
828 char *token = match_identifier ();
829 if (!token)
830 {
831 diag (pos, "missing base type in return type\n");
832 return 0;
833 }
834
835 if (!strcmp (token, "char"))
836 typedata->base = BT_CHAR;
837 else if (!strcmp (token, "short"))
838 typedata->base = BT_SHORT;
839 else if (!strcmp (token, "int"))
840 typedata->base = BT_INT;
841 else if (!strcmp (token, "long"))
842 {
843 consume_whitespace ();
844 oldpos = pos;
845 char *mustbelongordbl = match_identifier ();
846 if (!mustbelongordbl)
847 typedata->base = BT_LONG;
848 else if (!strcmp (mustbelongordbl, "long"))
849 typedata->base = BT_LONGLONG;
850 else if (!strcmp (mustbelongordbl, "double"))
851 typedata->base = BT_LONGDOUBLE;
852 else
853 /* Speculatively accept "long" here and push back the token.
854 This occurs when "long" is a return type and the next token
855 is the function name. */
856 {
857 typedata->base = BT_LONG;
858 pos = oldpos;
859 }
860 }
861 else if (!strcmp (token, "float"))
862 typedata->base = BT_FLOAT;
863 else if (!strcmp (token, "double"))
864 typedata->base = BT_DOUBLE;
865 else if (!strcmp (token, "__int128"))
866 typedata->base = BT_INT128;
867 else if (!strcmp (token, "_Float128"))
868 typedata->base = BT_FLOAT128;
869 else if (!strcmp (token, "bool"))
870 typedata->base = BT_BOOL;
871 /* A "string" is a special "const char *" -- we need it because it
872 cannot match either signed or unsigned char *. */
873 else if (!strcmp (token, "string"))
874 typedata->base = BT_STRING;
875 else if (!strcmp (token, "_Decimal32"))
876 typedata->base = BT_DECIMAL32;
877 else if (!strcmp (token, "_Decimal64"))
878 typedata->base = BT_DECIMAL64;
879 else if (!strcmp (token, "_Decimal128"))
880 typedata->base = BT_DECIMAL128;
881 else if (!strcmp (token, "__ibm128"))
882 typedata->base = BT_IBM128;
883 else
884 {
885 diag (oldpos, "unrecognized base type\n");
886 return 0;
887 }
888
889 handle_pointer (typedata);
890 return 1;
891 }
892
893 /* Helper routine for match_const_restriction. */
894 static int
895 match_bracketed_pair (typeinfo *typedata, char open, char close,
896 restriction restr)
897 {
898 if (linebuf[pos] == open)
899 {
900 safe_inc_pos ();
901 int oldpos = pos;
902 char *x = match_integer ();
903 if (x == NULL)
904 {
905 diag (oldpos, "malformed integer.\n");
906 return 0;
907 }
908 consume_whitespace ();
909 if (linebuf[pos] != ',')
910 {
911 diag (pos, "missing comma.\n");
912 return 0;
913 }
914 safe_inc_pos ();
915 consume_whitespace ();
916 oldpos = pos;
917 char *y = match_integer ();
918 if (y == NULL)
919 {
920 diag (oldpos, "malformed integer.\n");
921 return 0;
922 }
923 typedata->restr = restr;
924 typedata->val1 = x;
925 typedata->val2 = y;
926
927 consume_whitespace ();
928 if (linebuf[pos] != close)
929 {
930 diag (pos, "malformed restriction.\n");
931 return 0;
932 }
933 safe_inc_pos ();
934 return 1;
935 }
936
937 return 0;
938 }
939
940 /* A const int argument may be restricted to certain values. This is
941 indicated by one of the following occurring after the "int' token:
942
943 <x> restricts the constant to x bits, interpreted as unsigned
944 <x,y> restricts the constant to the inclusive range [x,y]
945 [x,y] restricts the constant to the inclusive range [x,y],
946 but only applies if the argument is constant.
947 {x,y} restricts the constant to one of two values, x or y.
948
949 Here x and y are integer tokens. Note that the "const" token is a
950 lie when the restriction is [x,y], but this simplifies the parsing
951 significantly and is hopefully forgivable.
952
953 Return 1 for success, else 0. */
954 static int
955 match_const_restriction (typeinfo *typedata)
956 {
957 int oldpos = pos;
958 if (linebuf[pos] == '<')
959 {
960 safe_inc_pos ();
961 oldpos = pos;
962 char *x = match_integer ();
963 if (x == NULL)
964 {
965 diag (oldpos, "malformed integer.\n");
966 return 0;
967 }
968 consume_whitespace ();
969 if (linebuf[pos] == '>')
970 {
971 typedata->restr = RES_BITS;
972 typedata->val1 = x;
973 safe_inc_pos ();
974 return 1;
975 }
976 else if (linebuf[pos] != ',')
977 {
978 diag (pos, "malformed restriction.\n");
979 return 0;
980 }
981 safe_inc_pos ();
982 oldpos = pos;
983 char *y = match_integer ();
984 if (y == NULL)
985 {
986 diag (oldpos, "malformed integer.\n");
987 return 0;
988 }
989 typedata->restr = RES_RANGE;
990 typedata->val1 = x;
991 typedata->val2 = y;
992
993 consume_whitespace ();
994 if (linebuf[pos] != '>')
995 {
996 diag (pos, "malformed restriction.\n");
997 return 0;
998 }
999 safe_inc_pos ();
1000 return 1;
1001 }
1002 else if (match_bracketed_pair (typedata, '{', '}', RES_VALUES)
1003 || match_bracketed_pair (typedata, '[', ']', RES_VAR_RANGE))
1004 return 1;
1005
1006 return 0;
1007 }
1008
1009 /* Look for a type, which can be terminated by a token that is not part of
1010 a type, a comma, or a closing parenthesis. Place information about the
1011 type in TYPEDATA. Return 1 for success, 0 for failure. */
1012 static int
1013 match_type (typeinfo *typedata, int voidok)
1014 {
1015 /* A legal type is of the form:
1016
1017 [const] [[signed|unsigned] <basetype> | <vectype>] [*]
1018
1019 Legal values of <basetype> are (for now):
1020
1021 char
1022 short
1023 int
1024 long
1025 long double
1026 long long
1027 float
1028 double
1029 __int128
1030 _Float128
1031 bool
1032 string
1033 _Decimal32
1034 _Decimal64
1035 _Decimal128
1036 __ibm128
1037
1038 Legal values of <vectype> are as follows, and are shorthand for
1039 the associated meaning:
1040
1041 vsc vector signed char
1042 vuc vector unsigned char
1043 vbc vector bool char
1044 vss vector signed short
1045 vus vector unsigned short
1046 vbs vector bool short
1047 vsi vector signed int
1048 vui vector unsigned int
1049 vbi vector bool int
1050 vsll vector signed long long
1051 vull vector unsigned long long
1052 vbll vector bool long long
1053 vsq vector signed __int128
1054 vuq vector unsigned __int128
1055 vbq vector bool __int128
1056 vp vector pixel
1057 vf vector float
1058 vd vector double
1059 v256 __vector_pair
1060 v512 __vector_quad
1061
1062 For simplicity, We don't support "short int" and "long long int".
1063 We don't currently support a <basetype> of "_Float16". "signed"
1064 and "unsigned" only apply to integral base types. The optional *
1065 indicates a pointer type. */
1066
1067 consume_whitespace ();
1068 memset (typedata, 0, sizeof *typedata);
1069 int oldpos = pos;
1070
1071 char *token = match_identifier ();
1072 if (!token)
1073 return 0;
1074
1075 if (!strcmp (token, "const"))
1076 {
1077 typedata->isconst = 1;
1078 consume_whitespace ();
1079 oldpos = pos;
1080 token = match_identifier ();
1081 }
1082
1083 if (!strcmp (token, "void"))
1084 typedata->isvoid = 1;
1085
1086 if (!strcmp (token, "vsc"))
1087 {
1088 typedata->isvector = 1;
1089 typedata->issigned = 1;
1090 typedata->base = BT_CHAR;
1091 handle_pointer (typedata);
1092 return 1;
1093 }
1094 else if (!strcmp (token, "vuc"))
1095 {
1096 typedata->isvector = 1;
1097 typedata->isunsigned = 1;
1098 typedata->base = BT_CHAR;
1099 handle_pointer (typedata);
1100 return 1;
1101 }
1102 else if (!strcmp (token, "vbc"))
1103 {
1104 typedata->isvector = 1;
1105 typedata->isbool = 1;
1106 typedata->base = BT_CHAR;
1107 handle_pointer (typedata);
1108 return 1;
1109 }
1110 else if (!strcmp (token, "vss"))
1111 {
1112 typedata->isvector = 1;
1113 typedata->issigned = 1;
1114 typedata->base = BT_SHORT;
1115 handle_pointer (typedata);
1116 return 1;
1117 }
1118 else if (!strcmp (token, "vus"))
1119 {
1120 typedata->isvector = 1;
1121 typedata->isunsigned = 1;
1122 typedata->base = BT_SHORT;
1123 handle_pointer (typedata);
1124 return 1;
1125 }
1126 else if (!strcmp (token, "vbs"))
1127 {
1128 typedata->isvector = 1;
1129 typedata->isbool = 1;
1130 typedata->base = BT_SHORT;
1131 handle_pointer (typedata);
1132 return 1;
1133 }
1134 else if (!strcmp (token, "vsi"))
1135 {
1136 typedata->isvector = 1;
1137 typedata->issigned = 1;
1138 typedata->base = BT_INT;
1139 handle_pointer (typedata);
1140 return 1;
1141 }
1142 else if (!strcmp (token, "vui"))
1143 {
1144 typedata->isvector = 1;
1145 typedata->isunsigned = 1;
1146 typedata->base = BT_INT;
1147 handle_pointer (typedata);
1148 return 1;
1149 }
1150 else if (!strcmp (token, "vbi"))
1151 {
1152 typedata->isvector = 1;
1153 typedata->isbool = 1;
1154 typedata->base = BT_INT;
1155 handle_pointer (typedata);
1156 return 1;
1157 }
1158 else if (!strcmp (token, "vsll"))
1159 {
1160 typedata->isvector = 1;
1161 typedata->issigned = 1;
1162 typedata->base = BT_LONGLONG;
1163 handle_pointer (typedata);
1164 return 1;
1165 }
1166 else if (!strcmp (token, "vull"))
1167 {
1168 typedata->isvector = 1;
1169 typedata->isunsigned = 1;
1170 typedata->base = BT_LONGLONG;
1171 handle_pointer (typedata);
1172 return 1;
1173 }
1174 else if (!strcmp (token, "vbll"))
1175 {
1176 typedata->isvector = 1;
1177 typedata->isbool = 1;
1178 typedata->base = BT_LONGLONG;
1179 handle_pointer (typedata);
1180 return 1;
1181 }
1182 else if (!strcmp (token, "vsq"))
1183 {
1184 typedata->isvector = 1;
1185 typedata->issigned = 1;
1186 typedata->base = BT_INT128;
1187 handle_pointer (typedata);
1188 return 1;
1189 }
1190 else if (!strcmp (token, "vuq"))
1191 {
1192 typedata->isvector = 1;
1193 typedata->isunsigned = 1;
1194 typedata->base = BT_INT128;
1195 handle_pointer (typedata);
1196 return 1;
1197 }
1198 else if (!strcmp (token, "vbq"))
1199 {
1200 typedata->isvector = 1;
1201 typedata->isbool = 1;
1202 typedata->base = BT_INT128;
1203 handle_pointer (typedata);
1204 return 1;
1205 }
1206 else if (!strcmp (token, "vp"))
1207 {
1208 typedata->isvector = 1;
1209 typedata->ispixel = 1;
1210 typedata->base = BT_SHORT;
1211 handle_pointer (typedata);
1212 return 1;
1213 }
1214 else if (!strcmp (token, "vf"))
1215 {
1216 typedata->isvector = 1;
1217 typedata->base = BT_FLOAT;
1218 handle_pointer (typedata);
1219 return 1;
1220 }
1221 else if (!strcmp (token, "vd"))
1222 {
1223 typedata->isvector = 1;
1224 typedata->base = BT_DOUBLE;
1225 handle_pointer (typedata);
1226 return 1;
1227 }
1228 else if (!strcmp (token, "v256"))
1229 {
1230 typedata->isvector = 1;
1231 typedata->base = BT_VPAIR;
1232 handle_pointer (typedata);
1233 return 1;
1234 }
1235 else if (!strcmp (token, "v512"))
1236 {
1237 typedata->isvector = 1;
1238 typedata->base = BT_VQUAD;
1239 handle_pointer (typedata);
1240 return 1;
1241 }
1242 else if (!strcmp (token, "signed"))
1243 typedata->issigned = 1;
1244 else if (!strcmp (token, "unsigned"))
1245 typedata->isunsigned = 1;
1246 else if (!typedata->isvoid && !typedata->isconst)
1247 {
1248 /* Push back token. */
1249 pos = oldpos;
1250 return match_basetype (typedata);
1251 }
1252
1253 if (typedata->isvoid)
1254 {
1255 consume_whitespace ();
1256 if (linebuf[pos] == '*')
1257 {
1258 typedata->ispointer = 1;
1259 safe_inc_pos ();
1260 }
1261 else if (!voidok)
1262 return 0;
1263 return 1;
1264 }
1265
1266 if (!typedata->issigned && !typedata->isunsigned)
1267 pos = oldpos;
1268 if (!match_basetype (typedata))
1269 return 0;
1270
1271 if (typedata->isconst)
1272 {
1273 if (typedata->ispointer)
1274 return 1;
1275 if (typedata->base != BT_INT)
1276 {
1277 diag (oldpos, "'const' requires pointer or integer type\n");
1278 return 0;
1279 }
1280 consume_whitespace ();
1281 if (linebuf[pos] == '<' || linebuf[pos] == '{' || linebuf[pos] == '[')
1282 return match_const_restriction (typedata);
1283 }
1284
1285 return 1;
1286 }
1287
1288 /* Parse the argument list. */
1289 static parse_codes
1290 parse_args (prototype *protoptr)
1291 {
1292 typelist **argptr = &protoptr->args;
1293 int *nargs = &protoptr->nargs;
1294 int *restr_opnd = protoptr->restr_opnd;
1295 restriction *restr = protoptr->restr;
1296 char **val1 = protoptr->restr_val1;
1297 char **val2 = protoptr->restr_val2;
1298 int restr_cnt = 0;
1299
1300 int success;
1301 *nargs = 0;
1302
1303 /* Start the argument list. */
1304 consume_whitespace ();
1305 if (linebuf[pos] != '(')
1306 {
1307 diag (pos, "missing '('.\n");
1308 return PC_PARSEFAIL;
1309 }
1310 safe_inc_pos ();
1311
1312 do {
1313 consume_whitespace ();
1314 int oldpos = pos;
1315 typelist *argentry = (typelist *) malloc (sizeof (typelist));
1316 memset (argentry, 0, sizeof *argentry);
1317 typeinfo *argtype = &argentry->info;
1318 success = match_type (argtype, VOID_NOTOK);
1319 if (success)
1320 {
1321 if (argtype->restr)
1322 {
1323 if (restr_cnt >= MAXRESTROPNDS)
1324 {
1325 diag (pos, "More than two %d operands\n", MAXRESTROPNDS);
1326 return PC_PARSEFAIL;
1327 }
1328 restr_opnd[restr_cnt] = *nargs + 1;
1329 restr[restr_cnt] = argtype->restr;
1330 val1[restr_cnt] = argtype->val1;
1331 val2[restr_cnt] = argtype->val2;
1332 restr_cnt++;
1333 }
1334 (*nargs)++;
1335 *argptr = argentry;
1336 argptr = &argentry->next;
1337 consume_whitespace ();
1338 if (linebuf[pos] == ',')
1339 safe_inc_pos ();
1340 else if (linebuf[pos] != ')')
1341 {
1342 diag (pos, "arg not followed by ',' or ')'.\n");
1343 return PC_PARSEFAIL;
1344 }
1345
1346 #ifdef DEBUG
1347 diag (0,
1348 "argument type: isvoid = %d, isconst = %d, isvector = %d, "
1349 "issigned = %d, isunsigned = %d, isbool = %d, ispixel = %d, "
1350 "ispointer = %d, base = %d, restr = %d, val1 = \"%s\", "
1351 "val2 = \"%s\", pos = %d.\n",
1352 argtype->isvoid, argtype->isconst, argtype->isvector,
1353 argtype->issigned, argtype->isunsigned, argtype->isbool,
1354 argtype->ispixel, argtype->ispointer, argtype->base,
1355 argtype->restr, argtype->val1, argtype->val2, pos + 1);
1356 #endif
1357 }
1358 else
1359 {
1360 free (argentry);
1361 *argptr = NULL;
1362 pos = oldpos;
1363 if (linebuf[pos] != ')')
1364 {
1365 diag (pos, "badly terminated arg list.\n");
1366 return PC_PARSEFAIL;
1367 }
1368 safe_inc_pos ();
1369 }
1370 } while (success);
1371
1372 return PC_OK;
1373 }
1374
1375 /* Parse the attribute list. */
1376 static parse_codes
1377 parse_bif_attrs (attrinfo *attrptr)
1378 {
1379 consume_whitespace ();
1380 if (linebuf[pos] != '{')
1381 {
1382 diag (pos, "missing attribute set.\n");
1383 return PC_PARSEFAIL;
1384 }
1385 safe_inc_pos ();
1386
1387 memset (attrptr, 0, sizeof *attrptr);
1388 char *attrname = NULL;
1389
1390 do {
1391 consume_whitespace ();
1392 int oldpos = pos;
1393 attrname = match_identifier ();
1394 if (attrname)
1395 {
1396 if (!strcmp (attrname, "extract"))
1397 attrptr->isextract = 1;
1398 else if (!strcmp (attrname, "nosoft"))
1399 attrptr->isnosoft = 1;
1400 else if (!strcmp (attrname, "ldvec"))
1401 attrptr->isldvec = 1;
1402 else if (!strcmp (attrname, "stvec"))
1403 attrptr->isstvec = 1;
1404 else if (!strcmp (attrname, "reve"))
1405 attrptr->isreve = 1;
1406 else if (!strcmp (attrname, "pred"))
1407 attrptr->ispred = 1;
1408 else if (!strcmp (attrname, "htm"))
1409 attrptr->ishtm = 1;
1410 else if (!strcmp (attrname, "htmspr"))
1411 attrptr->ishtmspr = 1;
1412 else if (!strcmp (attrname, "htmcr"))
1413 attrptr->ishtmcr = 1;
1414 else if (!strcmp (attrname, "mma"))
1415 attrptr->ismma = 1;
1416 else if (!strcmp (attrname, "quad"))
1417 attrptr->isquad = 1;
1418 else if (!strcmp (attrname, "pair"))
1419 attrptr->ispair = 1;
1420 else if (!strcmp (attrname, "mmaint"))
1421 attrptr->ismmaint = 1;
1422 else if (!strcmp (attrname, "no32bit"))
1423 attrptr->isno32bit = 1;
1424 else if (!strcmp (attrname, "32bit"))
1425 attrptr->is32bit = 1;
1426 else if (!strcmp (attrname, "cpu"))
1427 attrptr->iscpu = 1;
1428 else if (!strcmp (attrname, "ldstmask"))
1429 attrptr->isldstmask = 1;
1430 else if (!strcmp (attrname, "lxvrse"))
1431 attrptr->islxvrse = 1;
1432 else if (!strcmp (attrname, "lxvrze"))
1433 attrptr->islxvrze = 1;
1434 else if (!strcmp (attrname, "endian"))
1435 attrptr->isendian = 1;
1436 else if (!strcmp (attrname, "ibmld"))
1437 attrptr->isibmld = 1;
1438 else if (!strcmp (attrname, "ibm128"))
1439 attrptr->isibm128 = 1;
1440 else
1441 {
1442 diag (oldpos, "unknown attribute.\n");
1443 return PC_PARSEFAIL;
1444 }
1445
1446 consume_whitespace ();
1447 if (linebuf[pos] == ',')
1448 safe_inc_pos ();
1449 else if (linebuf[pos] != '}')
1450 {
1451 diag (pos, "arg not followed by ',' or '}'.\n");
1452 return PC_PARSEFAIL;
1453 }
1454 }
1455 else
1456 {
1457 pos = oldpos;
1458 if (linebuf[pos] != '}')
1459 {
1460 diag (pos, "badly terminated attr set.\n");
1461 return PC_PARSEFAIL;
1462 }
1463 safe_inc_pos ();
1464 }
1465 } while (attrname);
1466
1467 #ifdef DEBUG
1468 diag (0,
1469 "extract = %d, nosoft = %d, ldvec = %d, stvec = %d, reve = %d, "
1470 "pred = %d, htm = %d, htmspr = %d, htmcr = %d, mma = %d, "
1471 "quad = %d, pair = %d, mmaint = %d, no32bit = %d, 32bit = %d, "
1472 "cpu = %d, ldstmask = %d, lxvrse = %d, lxvrze = %d, endian = %d, "
1473 "ibmdld = %d, ibm128 = %d.\n",
1474 attrptr->isextract, attrptr->isnosoft,attrptr->isldvec,
1475 attrptr->isstvec, attrptr->isreve, attrptr->ispred, attrptr->ishtm,
1476 attrptr->ishtmspr, attrptr->ishtmcr, attrptr->ismma,
1477 attrptr->isquad, attrptr->ispair, attrptr->ismmaint,
1478 attrptr->isno32bit, attrptr->is32bit, attrptr->iscpu,
1479 attrptr->isldstmask, attrptr->islxvrse, attrptr->islxvrze,
1480 attrptr->isendian, attrptr->isibmld, attrptr->isibm128);
1481 #endif
1482
1483 return PC_OK;
1484 }
1485
1486 /* Convert a vector type into a mode string. */
1487 static void
1488 complete_vector_type (typeinfo *typeptr, char *buf, int *bufi)
1489 {
1490 if (typeptr->isbool)
1491 buf[(*bufi)++] = 'b';
1492 buf[(*bufi)++] = 'v';
1493 if (typeptr->ispixel)
1494 {
1495 memcpy (&buf[*bufi], "p8hi", 4);
1496 *bufi += 4;
1497 return;
1498 }
1499 switch (typeptr->base)
1500 {
1501 case BT_CHAR:
1502 memcpy (&buf[*bufi], "16qi", 4);
1503 *bufi += 4;
1504 break;
1505 case BT_SHORT:
1506 memcpy (&buf[*bufi], "8hi", 3);
1507 *bufi += 3;
1508 break;
1509 case BT_INT:
1510 memcpy (&buf[*bufi], "4si", 3);
1511 *bufi += 3;
1512 break;
1513 case BT_LONGLONG:
1514 memcpy (&buf[*bufi], "2di", 3);
1515 *bufi += 3;
1516 break;
1517 case BT_FLOAT:
1518 memcpy (&buf[*bufi], "4sf", 3);
1519 *bufi += 3;
1520 break;
1521 case BT_DOUBLE:
1522 memcpy (&buf[*bufi], "2df", 3);
1523 *bufi += 3;
1524 break;
1525 case BT_INT128:
1526 memcpy (&buf[*bufi], "1ti", 3);
1527 *bufi += 3;
1528 break;
1529 case BT_FLOAT128:
1530 memcpy (&buf[*bufi], "1tf", 3);
1531 *bufi += 3;
1532 break;
1533 case BT_VPAIR:
1534 memcpy (&buf[*bufi], "1poi", 4);
1535 *bufi += 4;
1536 break;
1537 case BT_VQUAD:
1538 memcpy (&buf[*bufi], "1pxi", 4);
1539 *bufi += 4;
1540 break;
1541 default:
1542 diag (pos, "unhandled basetype %d.\n", typeptr->base);
1543 exit (1);
1544 }
1545 }
1546
1547 /* Convert a base type into a mode string. */
1548 static void
1549 complete_base_type (typeinfo *typeptr, char *buf, int *bufi)
1550 {
1551 switch (typeptr->base)
1552 {
1553 case BT_CHAR:
1554 memcpy (&buf[*bufi], "qi", 2);
1555 break;
1556 case BT_SHORT:
1557 memcpy (&buf[*bufi], "hi", 2);
1558 break;
1559 case BT_INT:
1560 memcpy (&buf[*bufi], "si", 2);
1561 break;
1562 case BT_LONG:
1563 memcpy (&buf[*bufi], "lg", 2);
1564 break;
1565 case BT_LONGLONG:
1566 memcpy (&buf[*bufi], "di", 2);
1567 break;
1568 case BT_FLOAT:
1569 memcpy (&buf[*bufi], "sf", 2);
1570 break;
1571 case BT_DOUBLE:
1572 memcpy (&buf[*bufi], "df", 2);
1573 break;
1574 case BT_LONGDOUBLE:
1575 memcpy (&buf[*bufi], "ld", 2);
1576 break;
1577 case BT_INT128:
1578 memcpy (&buf[*bufi], "ti", 2);
1579 break;
1580 case BT_FLOAT128:
1581 memcpy (&buf[*bufi], "tf", 2);
1582 break;
1583 case BT_BOOL:
1584 memcpy (&buf[*bufi], "bi", 2);
1585 break;
1586 case BT_STRING:
1587 memcpy (&buf[*bufi], "st", 2);
1588 break;
1589 case BT_DECIMAL32:
1590 memcpy (&buf[*bufi], "sd", 2);
1591 break;
1592 case BT_DECIMAL64:
1593 memcpy (&buf[*bufi], "dd", 2);
1594 break;
1595 case BT_DECIMAL128:
1596 memcpy (&buf[*bufi], "td", 2);
1597 break;
1598 case BT_IBM128:
1599 memcpy (&buf[*bufi], "if", 2);
1600 break;
1601 default:
1602 diag (pos, "unhandled basetype %d.\n", typeptr->base);
1603 exit (1);
1604 }
1605
1606 *bufi += 2;
1607 }
1608
1609 /* Build a function type descriptor identifier from the return type
1610 and argument types described by PROTOPTR, and store it if it does
1611 not already exist. Return the identifier. */
1612 static char *
1613 construct_fntype_id (prototype *protoptr)
1614 {
1615 /* Determine the maximum space for a function type descriptor id.
1616 Each type requires at most 9 characters (6 for the mode*, 1 for
1617 the optional 'u' preceding the mode, 1 for the optional 'p'
1618 preceding the mode, and 1 for an underscore following the mode).
1619 We also need 5 characters for the string "ftype" that separates
1620 the return mode from the argument modes. The last argument doesn't
1621 need a trailing underscore, but we count that as the one trailing
1622 "ftype" instead. For the special case of zero arguments, we need 9
1623 for the return type and 7 for "ftype_v". Finally, we need one
1624 character for the terminating null. Thus for a function with N
1625 arguments, we need at most 9N+15 characters for N>0, otherwise 17.
1626 ----
1627 *Worst case is bv16qi for "vector bool char". */
1628 int len = protoptr->nargs ? (protoptr->nargs + 1) * 9 + 6 : 17;
1629 char *buf = (char *) malloc (len);
1630 int bufi = 0;
1631
1632 if (protoptr->rettype.ispointer)
1633 buf[bufi++] = 'p';
1634
1635 if (protoptr->rettype.isvoid)
1636 buf[bufi++] = 'v';
1637 else
1638 {
1639 if (protoptr->rettype.isunsigned)
1640 buf[bufi++] = 'u';
1641 if (protoptr->rettype.isvector)
1642 complete_vector_type (&protoptr->rettype, buf, &bufi);
1643 else
1644 complete_base_type (&protoptr->rettype, buf, &bufi);
1645 }
1646
1647 memcpy (&buf[bufi], "_ftype", 6);
1648 bufi += 6;
1649
1650 if (!protoptr->nargs)
1651 {
1652 memcpy (&buf[bufi], "_v", 2);
1653 bufi += 2;
1654 }
1655 else
1656 {
1657 typelist *argptr = protoptr->args;
1658 for (int i = 0; i < protoptr->nargs; i++, argptr = argptr->next)
1659 {
1660 assert (argptr);
1661 buf[bufi++] = '_';
1662 if (argptr->info.isconst
1663 && argptr->info.base == BT_INT
1664 && !argptr->info.ispointer)
1665 {
1666 buf[bufi++] = 'c';
1667 buf[bufi++] = 'i';
1668 continue;
1669 }
1670 if (argptr->info.ispointer)
1671 {
1672 if (argptr->info.isvoid)
1673 {
1674 if (argptr->info.isconst)
1675 {
1676 memcpy (&buf[bufi], "pcvoid", 6);
1677 bufi += 6;
1678 continue;
1679 }
1680 else
1681 {
1682 buf[bufi++] = 'p';
1683 buf[bufi++] = 'v';
1684 continue;
1685 }
1686 }
1687 else
1688 buf[bufi++] = 'p';
1689 }
1690
1691 if (argptr->info.isunsigned)
1692 buf[bufi++] = 'u';
1693 if (argptr->info.isvector)
1694 complete_vector_type (&argptr->info, buf, &bufi);
1695 else
1696 complete_base_type (&argptr->info, buf, &bufi);
1697 }
1698 assert (!argptr);
1699 }
1700
1701 buf[bufi] = '\0';
1702
1703 /* Ignore return value, as duplicates are fine and expected here. */
1704 rbt_insert (&fntype_rbt, buf);
1705
1706 return buf;
1707 }
1708
1709 /* Parse a function prototype. This code is shared by the bif and overload
1710 file processing. */
1711 static parse_codes
1712 parse_prototype (prototype *protoptr)
1713 {
1714 typeinfo *ret_type = &protoptr->rettype;
1715 char **bifname = &protoptr->bifname;
1716
1717 /* Get the return type. */
1718 consume_whitespace ();
1719 int oldpos = pos;
1720 int success = match_type (ret_type, VOID_OK);
1721 if (!success)
1722 {
1723 diag (oldpos, "missing or badly formed return type.\n");
1724 return PC_PARSEFAIL;
1725 }
1726
1727 #ifdef DEBUG
1728 diag (0,
1729 "return type: isvoid = %d, isconst = %d, isvector = %d, "
1730 "issigned = %d, isunsigned = %d, isbool = %d, ispixel = %d, "
1731 "ispointer = %d, base = %d, restr = %d, val1 = \"%s\", "
1732 "val2 = \"%s\", pos = %d.\n",
1733 ret_type->isvoid, ret_type->isconst, ret_type->isvector,
1734 ret_type->issigned, ret_type->isunsigned, ret_type->isbool,
1735 ret_type->ispixel, ret_type->ispointer, ret_type->base,
1736 ret_type->restr, ret_type->val1, ret_type->val2, pos + 1);
1737 #endif
1738
1739 /* Get the bif name. */
1740 consume_whitespace ();
1741 oldpos = pos;
1742 *bifname = match_identifier ();
1743 if (!*bifname)
1744 {
1745 diag (oldpos, "missing function name.\n");
1746 return PC_PARSEFAIL;
1747 }
1748
1749 #ifdef DEBUG
1750 diag (0, "function name is '%s'.\n", *bifname);
1751 #endif
1752
1753 /* Process arguments. */
1754 if (parse_args (protoptr) == PC_PARSEFAIL)
1755 return PC_PARSEFAIL;
1756
1757 /* Process terminating semicolon. */
1758 consume_whitespace ();
1759 if (linebuf[pos] != ';')
1760 {
1761 diag (pos, "missing semicolon.\n");
1762 return PC_PARSEFAIL;
1763 }
1764 safe_inc_pos ();
1765 consume_whitespace ();
1766 if (linebuf[pos] != '\n')
1767 {
1768 diag (pos, "garbage at end of line.\n");
1769 return PC_PARSEFAIL;
1770 }
1771
1772 return PC_OK;
1773 }
1774
1775 /* Parse a two-line entry for a built-in function. */
1776 static parse_codes
1777 parse_bif_entry (void)
1778 {
1779 /* Check for end of stanza. */
1780 pos = 0;
1781 consume_whitespace ();
1782 if (linebuf[pos] == '[')
1783 return PC_EOSTANZA;
1784
1785 /* Allocate an entry in the bif table. */
1786 if (num_bifs >= MAXBIFS - 1)
1787 {
1788 diag (pos, "too many built-in functions.\n");
1789 return PC_PARSEFAIL;
1790 }
1791
1792 curr_bif = num_bifs++;
1793 bifs[curr_bif].stanza = curr_bif_stanza;
1794
1795 /* Read the first token and see if it is a function modifier. */
1796 consume_whitespace ();
1797 int oldpos = pos;
1798 char *token = match_identifier ();
1799 if (!token)
1800 {
1801 diag (oldpos, "malformed entry.\n");
1802 return PC_PARSEFAIL;
1803 }
1804
1805 if (!strcmp (token, "const"))
1806 bifs[curr_bif].kind = FNK_CONST;
1807 else if (!strcmp (token, "pure"))
1808 bifs[curr_bif].kind = FNK_PURE;
1809 else if (!strcmp (token, "fpmath"))
1810 bifs[curr_bif].kind = FNK_FPMATH;
1811 else
1812 {
1813 /* No function modifier, so push the token back. */
1814 pos = oldpos;
1815 bifs[curr_bif].kind = FNK_NONE;
1816 }
1817
1818 if (parse_prototype (&bifs[curr_bif].proto) == PC_PARSEFAIL)
1819 return PC_PARSEFAIL;
1820
1821 /* Build a function type descriptor identifier from the return type
1822 and argument types, and store it if it does not already exist. */
1823 bifs[curr_bif].fndecl = construct_fntype_id (&bifs[curr_bif].proto);
1824
1825 /* Now process line 2. First up is the builtin id. */
1826 if (!advance_line (bif_file))
1827 {
1828 diag (pos, "unexpected EOF.\n");
1829 return PC_PARSEFAIL;
1830 }
1831
1832 pos = 0;
1833 consume_whitespace ();
1834 oldpos = pos;
1835 bifs[curr_bif].idname = match_identifier ();
1836 if (!bifs[curr_bif].idname)
1837 {
1838 diag (pos, "missing builtin id.\n");
1839 return PC_PARSEFAIL;
1840 }
1841
1842 #ifdef DEBUG
1843 diag (0, "ID name is '%s'.\n", bifs[curr_bif].idname);
1844 #endif
1845
1846 /* Save the ID in a lookup structure. */
1847 if (!rbt_insert (&bif_rbt, bifs[curr_bif].idname))
1848 {
1849 diag (oldpos, "duplicate function ID '%s'.\n", bifs[curr_bif].idname);
1850 return PC_PARSEFAIL;
1851 }
1852
1853 /* Append a number representing the order in which this function
1854 was encountered to its name, and save in another lookup
1855 structure. */
1856 int orig_len = strlen (bifs[curr_bif].idname);
1857 char *buf = (char *) malloc (orig_len + 7);
1858 sprintf (buf, "%s:%05d", bifs[curr_bif].idname, curr_bif);
1859
1860 if (!rbt_insert (&bifo_rbt, buf))
1861 {
1862 diag (pos, "internal error inserting '%s' in bifo_rbt\n", buf);
1863 return PC_PARSEFAIL;
1864 }
1865
1866 /* Now the pattern name. */
1867 consume_whitespace ();
1868 bifs[curr_bif].patname = match_identifier ();
1869 if (!bifs[curr_bif].patname)
1870 {
1871 diag (pos, "missing pattern name.\n");
1872 return PC_PARSEFAIL;
1873 }
1874
1875 #ifdef DEBUG
1876 diag (0, "pattern name is '%s'.\n", bifs[curr_bif].patname);
1877 #endif
1878
1879 /* Process attributes. */
1880 return parse_bif_attrs (&bifs[curr_bif].attrs);
1881 }
1882
1883 /* Parse one stanza of the input BIF file. linebuf already contains the
1884 first line to parse. */
1885 static parse_codes
1886 parse_bif_stanza (void)
1887 {
1888 /* Parse the stanza header. */
1889 pos = 0;
1890 consume_whitespace ();
1891
1892 if (linebuf[pos] != '[')
1893 {
1894 diag (pos, "ill-formed stanza header.\n");
1895 return PC_PARSEFAIL;
1896 }
1897 safe_inc_pos ();
1898
1899 const char *stanza_name = match_to_right_bracket ();
1900 if (!stanza_name)
1901 {
1902 diag (pos, "no expression found in stanza header.\n");
1903 return PC_PARSEFAIL;
1904 }
1905
1906 curr_bif_stanza = stanza_name_to_stanza (stanza_name);
1907
1908 if (linebuf[pos] != ']')
1909 {
1910 diag (pos, "ill-formed stanza header.\n");
1911 return PC_PARSEFAIL;
1912 }
1913 safe_inc_pos ();
1914
1915 consume_whitespace ();
1916 if (linebuf[pos] != '\n' && pos != LINELEN - 1)
1917 {
1918 diag (pos, "garbage after stanza header.\n");
1919 return PC_PARSEFAIL;
1920 }
1921
1922 parse_codes result = PC_OK;
1923
1924 while (result != PC_EOSTANZA)
1925 {
1926 if (!advance_line (bif_file))
1927 return PC_EOFILE;
1928 result = parse_bif_entry ();
1929 if (result == PC_PARSEFAIL)
1930 return PC_PARSEFAIL;
1931 }
1932
1933 return PC_OK;
1934 }
1935
1936 /* Parse the built-in file. */
1937 static parse_codes
1938 parse_bif (void)
1939 {
1940 parse_codes result;
1941 diag = &bif_diag;
1942 if (!advance_line (bif_file))
1943 return PC_OK;
1944
1945 do
1946 result = parse_bif_stanza ();
1947 while (result == PC_OK);
1948
1949 if (result == PC_EOFILE)
1950 return PC_OK;
1951 return result;
1952 }
1953
1954 /* Callback function for create_bif_order. */
1955 void set_bif_order (char *str)
1956 {
1957 int num = 0;
1958 char *colon = strchr (str, ':');
1959 sscanf (++colon, "%d", &num);
1960 bif_order[bif_index++] = num;
1961 }
1962
1963 /* Create a mapping from function IDs in their final order to the order
1964 they appear in the built-in function file. */
1965 static void
1966 create_bif_order (void)
1967 {
1968 bif_order = (int *) malloc ((curr_bif + 1) * sizeof (int));
1969 rbt_inorder_callback (&bifo_rbt, bifo_rbt.rbt_root, set_bif_order);
1970 }
1971
1972 /* Parse one two-line entry in the overload file. */
1973 static parse_codes
1974 parse_ovld_entry (void)
1975 {
1976 /* Check for end of stanza. */
1977 pos = 0;
1978 consume_whitespace ();
1979 if (linebuf[pos] == '[')
1980 return PC_EOSTANZA;
1981
1982 /* Allocate an entry in the overload table. */
1983 if (num_ovlds >= MAXOVLDS - 1)
1984 {
1985 diag (pos, "too many overloads.\n");
1986 return PC_PARSEFAIL;
1987 }
1988
1989 curr_ovld = num_ovlds++;
1990 ovlds[curr_ovld].stanza = curr_ovld_stanza;
1991
1992 if (parse_prototype (&ovlds[curr_ovld].proto) == PC_PARSEFAIL)
1993 return PC_PARSEFAIL;
1994
1995 if (ovlds[curr_ovld].proto.nargs > max_ovld_args)
1996 max_ovld_args = ovlds[curr_ovld].proto.nargs;
1997
1998 /* Build a function type descriptor identifier from the return type
1999 and argument types, and store it if it does not already exist. */
2000 ovlds[curr_ovld].fndecl = construct_fntype_id (&ovlds[curr_ovld].proto);
2001
2002 /* Now process line 2, which just contains the builtin id and an
2003 optional overload id. */
2004 if (!advance_line (ovld_file))
2005 {
2006 diag (0, "unexpected EOF.\n");
2007 return PC_EOFILE;
2008 }
2009
2010 pos = 0;
2011 consume_whitespace ();
2012 int oldpos = pos;
2013 char *id = match_identifier ();
2014 ovlds[curr_ovld].bif_id_name = id;
2015 ovlds[curr_ovld].ovld_id_name = id;
2016 if (!id)
2017 {
2018 diag (pos, "missing overload id.\n");
2019 return PC_PARSEFAIL;
2020 }
2021
2022 #ifdef DEBUG
2023 diag (pos, "ID name is '%s'.\n", id);
2024 #endif
2025
2026 /* The builtin id has to match one from the bif file. */
2027 if (!rbt_find (&bif_rbt, id))
2028 {
2029 diag (pos, "builtin ID '%s' not found in bif file.\n", id);
2030 return PC_PARSEFAIL;
2031 }
2032
2033 /* Check for an optional overload id. Usually we use the builtin
2034 function id for that purpose, but sometimes we need multiple
2035 overload entries for the same builtin id, and it needs to be unique. */
2036 consume_whitespace ();
2037 if (linebuf[pos] != '\n')
2038 {
2039 id = match_identifier ();
2040 ovlds[curr_ovld].ovld_id_name = id;
2041 consume_whitespace ();
2042 }
2043
2044 /* Save the overload ID in a lookup structure. */
2045 if (!rbt_insert (&ovld_rbt, id))
2046 {
2047 diag (oldpos, "duplicate overload ID '%s'.\n", id);
2048 return PC_PARSEFAIL;
2049 }
2050
2051 if (linebuf[pos] != '\n')
2052 {
2053 diag (pos, "garbage at end of line.\n");
2054 return PC_PARSEFAIL;
2055 }
2056 return PC_OK;
2057 }
2058
2059 /* Parse one stanza of the input overload file. linebuf already contains the
2060 first line to parse. */
2061 static parse_codes
2062 parse_ovld_stanza (void)
2063 {
2064 /* Parse the stanza header. */
2065 pos = 0;
2066 consume_whitespace ();
2067
2068 if (linebuf[pos] != '[')
2069 {
2070 diag (pos, "ill-formed stanza header.\n");
2071 return PC_PARSEFAIL;
2072 }
2073 safe_inc_pos ();
2074
2075 char *stanza_name = match_identifier ();
2076 if (!stanza_name)
2077 {
2078 diag (pos, "no identifier found in stanza header.\n");
2079 return PC_PARSEFAIL;
2080 }
2081
2082 /* Add the identifier to a table and set the number to be recorded
2083 with subsequent overload entries. */
2084 if (num_ovld_stanzas >= MAXOVLDSTANZAS)
2085 {
2086 diag (pos, "too many stanza headers.\n");
2087 return PC_PARSEFAIL;
2088 }
2089
2090 curr_ovld_stanza = num_ovld_stanzas++;
2091 ovld_stanza *stanza = &ovld_stanzas[curr_ovld_stanza];
2092 stanza->stanza_id = stanza_name;
2093
2094 consume_whitespace ();
2095 if (linebuf[pos] != ',')
2096 {
2097 diag (pos, "missing comma.\n");
2098 return PC_PARSEFAIL;
2099 }
2100 safe_inc_pos ();
2101
2102 consume_whitespace ();
2103 stanza->extern_name = match_identifier ();
2104 if (!stanza->extern_name)
2105 {
2106 diag (pos, "missing external name.\n");
2107 return PC_PARSEFAIL;
2108 }
2109
2110 consume_whitespace ();
2111 if (linebuf[pos] != ',')
2112 {
2113 diag (pos, "missing comma.\n");
2114 return PC_PARSEFAIL;
2115 }
2116 safe_inc_pos ();
2117
2118 consume_whitespace ();
2119 stanza->intern_name = match_identifier ();
2120 if (!stanza->intern_name)
2121 {
2122 diag (pos, "missing internal name.\n");
2123 return PC_PARSEFAIL;
2124 }
2125
2126 consume_whitespace ();
2127 if (linebuf[pos] == ',')
2128 {
2129 safe_inc_pos ();
2130 consume_whitespace ();
2131 stanza->ifdef = match_identifier ();
2132 if (!stanza->ifdef)
2133 {
2134 diag (pos, "missing ifdef token.\n");
2135 return PC_PARSEFAIL;
2136 }
2137 consume_whitespace ();
2138 }
2139 else
2140 stanza->ifdef = 0;
2141
2142 if (linebuf[pos] != ']')
2143 {
2144 diag (pos, "ill-formed stanza header.\n");
2145 return PC_PARSEFAIL;
2146 }
2147 safe_inc_pos ();
2148
2149 consume_whitespace ();
2150 if (linebuf[pos] != '\n' && pos != LINELEN - 1)
2151 {
2152 diag (pos, "garbage after stanza header.\n");
2153 return PC_PARSEFAIL;
2154 }
2155
2156 parse_codes result = PC_OK;
2157
2158 while (result != PC_EOSTANZA)
2159 {
2160 if (!advance_line (ovld_file))
2161 return PC_EOFILE;
2162
2163 result = parse_ovld_entry ();
2164 if (result == PC_EOFILE || result == PC_PARSEFAIL)
2165 return result;
2166 }
2167
2168 return PC_OK;
2169 }
2170
2171 /* Parse the overload file. */
2172 static parse_codes
2173 parse_ovld (void)
2174 {
2175 parse_codes result = PC_OK;
2176 diag = &ovld_diag;
2177
2178 if (!advance_line (ovld_file))
2179 return PC_OK;
2180
2181 while (result == PC_OK)
2182 result = parse_ovld_stanza ();
2183
2184 if (result == PC_EOFILE)
2185 return PC_OK;
2186 return result;
2187 }
2188
2189 /* Write a comment at the top of FILE about how the code was generated. */
2190 static void
2191 write_autogenerated_header (FILE *file)
2192 {
2193 fprintf (file, "/* Automatically generated by the program '%s'\n",
2194 pgm_path);
2195 fprintf (file, " from the files '%s' and '%s'. */\n\n",
2196 bif_path, ovld_path);
2197 }
2198
2199 /* Write declarations into the header file. */
2200 static void
2201 write_decls (void)
2202 {
2203 fprintf (header_file, "enum rs6000_gen_builtins\n{\n RS6000_BIF_NONE,\n");
2204 for (int i = 0; i <= curr_bif; i++)
2205 fprintf (header_file, " RS6000_BIF_%s,\n", bifs[bif_order[i]].idname);
2206 fprintf (header_file, " RS6000_BIF_MAX,\n");
2207 fprintf (header_file, " RS6000_OVLD_NONE,\n");
2208 for (int i = 0; i < num_ovld_stanzas; i++)
2209 fprintf (header_file, " RS6000_OVLD_%s,\n", ovld_stanzas[i].stanza_id);
2210 fprintf (header_file, " RS6000_OVLD_MAX\n};\n\n");
2211
2212 fprintf (header_file,
2213 "extern GTY(()) tree rs6000_builtin_decls[RS6000_OVLD_MAX];\n\n");
2214
2215 fprintf (header_file,
2216 "enum rs6000_ovld_instances\n{\n RS6000_INST_NONE,\n");
2217 for (int i = 0; i <= curr_ovld; i++)
2218 fprintf (header_file, " RS6000_INST_%s,\n", ovlds[i].ovld_id_name);
2219 fprintf (header_file, " RS6000_INST_MAX\n};\n\n");
2220
2221 fprintf (header_file, "#define MAX_OVLD_ARGS %d\n", max_ovld_args);
2222
2223 fprintf (header_file, "enum restriction {\n");
2224 fprintf (header_file, " RES_NONE,\n");
2225 fprintf (header_file, " RES_BITS,\n");
2226 fprintf (header_file, " RES_RANGE,\n");
2227 fprintf (header_file, " RES_VAR_RANGE,\n");
2228 fprintf (header_file, " RES_VALUES\n");
2229 fprintf (header_file, "};\n\n");
2230
2231 fprintf (header_file, "enum bif_enable {\n");
2232 fprintf (header_file, " ENB_ALWAYS,\n");
2233 fprintf (header_file, " ENB_P5,\n");
2234 fprintf (header_file, " ENB_P6,\n");
2235 fprintf (header_file, " ENB_P6_64,\n");
2236 fprintf (header_file, " ENB_ALTIVEC,\n");
2237 fprintf (header_file, " ENB_CELL,\n");
2238 fprintf (header_file, " ENB_VSX,\n");
2239 fprintf (header_file, " ENB_P7,\n");
2240 fprintf (header_file, " ENB_P7_64,\n");
2241 fprintf (header_file, " ENB_P8,\n");
2242 fprintf (header_file, " ENB_P8V,\n");
2243 fprintf (header_file, " ENB_P9,\n");
2244 fprintf (header_file, " ENB_P9_64,\n");
2245 fprintf (header_file, " ENB_P9V,\n");
2246 fprintf (header_file, " ENB_IEEE128_HW,\n");
2247 fprintf (header_file, " ENB_DFP,\n");
2248 fprintf (header_file, " ENB_CRYPTO,\n");
2249 fprintf (header_file, " ENB_HTM,\n");
2250 fprintf (header_file, " ENB_P10,\n");
2251 fprintf (header_file, " ENB_P10_64,\n");
2252 fprintf (header_file, " ENB_MMA\n");
2253 fprintf (header_file, "};\n\n");
2254
2255 fprintf (header_file, "#define PPC_MAXRESTROPNDS 3\n");
2256 fprintf (header_file, "struct bifdata\n");
2257 fprintf (header_file, "{\n");
2258 fprintf (header_file, " const char *bifname;\n");
2259 fprintf (header_file, " bif_enable enable;\n");
2260 fprintf (header_file, " insn_code icode;\n");
2261 fprintf (header_file, " int nargs;\n");
2262 fprintf (header_file, " int bifattrs;\n");
2263 fprintf (header_file, " int restr_opnd[PPC_MAXRESTROPNDS];\n");
2264 fprintf (header_file, " restriction restr[PPC_MAXRESTROPNDS];\n");
2265 fprintf (header_file, " int restr_val1[PPC_MAXRESTROPNDS];\n");
2266 fprintf (header_file, " int restr_val2[PPC_MAXRESTROPNDS];\n");
2267 fprintf (header_file, " const char *attr_string;\n");
2268 fprintf (header_file, " rs6000_gen_builtins assoc_bif;\n");
2269 fprintf (header_file, "};\n\n");
2270
2271 /* Bit patterns 0x00000001 and 0x00000002 are available. */
2272 fprintf (header_file, "#define bif_extract_bit\t\t(0x00000004)\n");
2273 fprintf (header_file, "#define bif_nosoft_bit\t\t(0x00000008)\n");
2274 fprintf (header_file, "#define bif_ldvec_bit\t\t(0x00000010)\n");
2275 fprintf (header_file, "#define bif_stvec_bit\t\t(0x00000020)\n");
2276 fprintf (header_file, "#define bif_reve_bit\t\t(0x00000040)\n");
2277 fprintf (header_file, "#define bif_pred_bit\t\t(0x00000080)\n");
2278 fprintf (header_file, "#define bif_htm_bit\t\t(0x00000100)\n");
2279 fprintf (header_file, "#define bif_htmspr_bit\t\t(0x00000200)\n");
2280 fprintf (header_file, "#define bif_htmcr_bit\t\t(0x00000400)\n");
2281 fprintf (header_file, "#define bif_mma_bit\t\t(0x00000800)\n");
2282 fprintf (header_file, "#define bif_quad_bit\t\t(0x00001000)\n");
2283 fprintf (header_file, "#define bif_pair_bit\t\t(0x00002000)\n");
2284 fprintf (header_file, "#define bif_mmaint_bit\t\t(0x00004000)\n");
2285 fprintf (header_file, "#define bif_no32bit_bit\t\t(0x00008000)\n");
2286 fprintf (header_file, "#define bif_32bit_bit\t\t(0x00010000)\n");
2287 fprintf (header_file, "#define bif_cpu_bit\t\t(0x00020000)\n");
2288 fprintf (header_file, "#define bif_ldstmask_bit\t(0x00040000)\n");
2289 fprintf (header_file, "#define bif_lxvrse_bit\t\t(0x00080000)\n");
2290 fprintf (header_file, "#define bif_lxvrze_bit\t\t(0x00100000)\n");
2291 fprintf (header_file, "#define bif_endian_bit\t\t(0x00200000)\n");
2292 fprintf (header_file, "#define bif_ibmld_bit\t\t(0x00400000)\n");
2293 fprintf (header_file, "#define bif_ibm128_bit\t\t(0x00800000)\n");
2294 fprintf (header_file, "\n");
2295 fprintf (header_file,
2296 "#define bif_is_extract(x)\t((x).bifattrs & bif_extract_bit)\n");
2297 fprintf (header_file,
2298 "#define bif_is_nosoft(x)\t((x).bifattrs & bif_nosoft_bit)\n");
2299 fprintf (header_file,
2300 "#define bif_is_ldvec(x)\t\t((x).bifattrs & bif_ldvec_bit)\n");
2301 fprintf (header_file,
2302 "#define bif_is_stvec(x)\t\t((x).bifattrs & bif_stvec_bit)\n");
2303 fprintf (header_file,
2304 "#define bif_is_reve(x)\t\t((x).bifattrs & bif_reve_bit)\n");
2305 fprintf (header_file,
2306 "#define bif_is_predicate(x)\t((x).bifattrs & bif_pred_bit)\n");
2307 fprintf (header_file,
2308 "#define bif_is_htm(x)\t\t((x).bifattrs & bif_htm_bit)\n");
2309 fprintf (header_file,
2310 "#define bif_is_htmspr(x)\t((x).bifattrs & bif_htmspr_bit)\n");
2311 fprintf (header_file,
2312 "#define bif_is_htmcr(x)\t\t((x).bifattrs & bif_htmcr_bit)\n");
2313 fprintf (header_file,
2314 "#define bif_is_mma(x)\t\t((x).bifattrs & bif_mma_bit)\n");
2315 fprintf (header_file,
2316 "#define bif_is_quad(x)\t\t((x).bifattrs & bif_quad_bit)\n");
2317 fprintf (header_file,
2318 "#define bif_is_pair(x)\t\t((x).bifattrs & bif_pair_bit)\n");
2319 fprintf (header_file,
2320 "#define bif_is_mmaint(x)\t\t((x).bifattrs & bif_mmaint_bit)\n");
2321 fprintf (header_file,
2322 "#define bif_is_no32bit(x)\t((x).bifattrs & bif_no32bit_bit)\n");
2323 fprintf (header_file,
2324 "#define bif_is_32bit(x)\t((x).bifattrs & bif_32bit_bit)\n");
2325 fprintf (header_file,
2326 "#define bif_is_cpu(x)\t\t((x).bifattrs & bif_cpu_bit)\n");
2327 fprintf (header_file,
2328 "#define bif_is_ldstmask(x)\t((x).bifattrs & bif_ldstmask_bit)\n");
2329 fprintf (header_file,
2330 "#define bif_is_lxvrse(x)\t((x).bifattrs & bif_lxvrse_bit)\n");
2331 fprintf (header_file,
2332 "#define bif_is_lxvrze(x)\t((x).bifattrs & bif_lxvrze_bit)\n");
2333 fprintf (header_file,
2334 "#define bif_is_endian(x)\t((x).bifattrs & bif_endian_bit)\n");
2335 fprintf (header_file,
2336 "#define bif_is_ibmld(x)\t((x).bifattrs & bif_ibmld_bit)\n");
2337 fprintf (header_file,
2338 "#define bif_is_ibm128(x)\t((x).bifattrs & bif_ibm128_bit)\n");
2339 fprintf (header_file, "\n");
2340
2341 fprintf (header_file,
2342 "extern bifdata rs6000_builtin_info[RS6000_BIF_MAX];\n\n");
2343
2344 fprintf (header_file,
2345 "extern GTY(()) tree rs6000_builtin_info_fntype[RS6000_BIF_MAX];\n\n");
2346
2347 fprintf (header_file, "struct ovlddata\n");
2348 fprintf (header_file, "{\n");
2349 fprintf (header_file, " const char *bifname;\n");
2350 fprintf (header_file, " rs6000_gen_builtins bifid;\n");
2351 fprintf (header_file, " int next;\n");
2352 fprintf (header_file, "};\n\n");
2353
2354 fprintf (header_file, "struct ovldrecord\n");
2355 fprintf (header_file, "{\n");
2356 fprintf (header_file, " const char *ovld_name;\n");
2357 fprintf (header_file, " int first_instance;\n");
2358 fprintf (header_file, "};\n\n");
2359
2360 fprintf (header_file,
2361 "extern ovlddata rs6000_instance_info[RS6000_INST_MAX];\n");
2362 fprintf (header_file, "extern GTY(()) tree "
2363 "rs6000_instance_info_fntype[RS6000_INST_MAX];\n");
2364 fprintf (header_file, "extern ovldrecord rs6000_overload_info[];\n\n");
2365
2366 fprintf (header_file,
2367 "extern void rs6000_init_generated_builtins (tree *, tree *,\n");
2368 fprintf (header_file,
2369 "\t\t\t\t\t ovldrecord *, tree *);\n\n");
2370 fprintf (header_file,
2371 "extern bool rs6000_builtin_is_supported (rs6000_gen_builtins);\n");
2372 fprintf (header_file,
2373 "extern tree rs6000_builtin_decl (unsigned, "
2374 "bool ATTRIBUTE_UNUSED);\n\n");
2375 }
2376
2377 /* Comparator for bsearch on the type map. */
2378 int
2379 typemap_cmp (const void *key, const void *entry)
2380 {
2381 return strcmp ((const char *)key, ((const typemap *)entry)->key);
2382 }
2383
2384 /* Write the type node corresponding to TOK. */
2385 static void
2386 write_type_node (char *tok, bool indent)
2387 {
2388 if (indent)
2389 fprintf (init_file, " ");
2390 typemap *entry
2391 = (typemap *) bsearch (tok, type_map,
2392 sizeof type_map / sizeof type_map[0],
2393 sizeof (typemap), typemap_cmp);
2394 if (!entry)
2395 fatal ("Type map is inconsistent.");
2396 fprintf (init_file, "%s_type_node", entry->value);
2397 }
2398
2399 /* Write an initializer for a function type identified by STR. */
2400 void
2401 write_fntype_init (char *str)
2402 {
2403 char *tok;
2404
2405 /* Check whether we have a "tf" token in this string, representing
2406 a float128_type_node. It's possible that float128_type_node is
2407 undefined (occurs for -maltivec -mno-vsx, for example), so we
2408 must guard against that. */
2409 int tf_found = strstr (str, "tf") != NULL;
2410
2411 /* Similarly, look for decimal float tokens. */
2412 int dfp_found = (strstr (str, "dd") != NULL
2413 || strstr (str, "td") != NULL
2414 || strstr (str, "sd") != NULL);
2415
2416 /* Avoid side effects of strtok on the original string by using a copy. */
2417 char *buf = strdup (str);
2418
2419 if (tf_found || dfp_found)
2420 fprintf (init_file, " tree %s = NULL_TREE;\n", buf);
2421 else
2422 fprintf (init_file, " tree ");
2423
2424 if (tf_found)
2425 fprintf (init_file, " if (float128_type_node)\n ");
2426 else if (dfp_found)
2427 fprintf (init_file, " if (dfloat64_type_node)\n ");
2428
2429 fprintf (init_file, "%s\n = build_function_type_list (", buf);
2430 tok = strtok (buf, "_");
2431 write_type_node (tok, tf_found || dfp_found);
2432 tok = strtok (0, "_");
2433 assert (tok);
2434 assert (!strcmp (tok, "ftype"));
2435
2436 tok = strtok (0, "_");
2437 if (tok)
2438 fprintf (init_file, ",\n\t\t\t\t");
2439
2440 /* Note: A function with no arguments ends with '_ftype_v'. */
2441 while (tok && strcmp (tok, "v"))
2442 {
2443 write_type_node (tok, tf_found || dfp_found);
2444 tok = strtok (0, "_");
2445 fprintf (init_file, ",\n\t\t\t\t");
2446 }
2447 fprintf (init_file, "NULL_TREE);\n");
2448 free (buf);
2449 }
2450
2451 /* Write everything to the header file (rs6000-builtins.h). Return
2452 1 if successful, 0 otherwise. */
2453 static int
2454 write_header_file (void)
2455 {
2456 write_autogenerated_header (header_file);
2457
2458 fprintf (header_file, "#ifndef _RS6000_BUILTINS_H\n");
2459 fprintf (header_file, "#define _RS6000_BUILTINS_H 1\n\n");
2460
2461 write_decls ();
2462
2463 fprintf (header_file, "\n");
2464 fprintf (header_file, "\n#endif\n");
2465
2466 return 1;
2467 }
2468
2469 /* Write the decl and initializer for rs6000_builtin_info[]. */
2470 static void
2471 write_bif_static_init (void)
2472 {
2473 const char *res[3];
2474 fprintf (init_file, "bifdata rs6000_builtin_info[RS6000_BIF_MAX] =\n");
2475 fprintf (init_file, " {\n");
2476 fprintf (init_file, " { /* RS6000_BIF_NONE: */\n");
2477 fprintf (init_file, " \"\", ENB_ALWAYS, CODE_FOR_nothing, 0,\n");
2478 fprintf (init_file, " 0, {0, 0, 0}, {RES_NONE, RES_NONE, RES_NONE},\n");
2479 fprintf (init_file, " {0, 0, 0}, {0, 0, 0}, \"\", RS6000_BIF_NONE\n");
2480 fprintf (init_file, " },\n");
2481 for (int i = 0; i <= curr_bif; i++)
2482 {
2483 bifdata *bifp = &bifs[bif_order[i]];
2484 fprintf (init_file, " { /* RS6000_BIF_%s: */\n", bifp->idname);
2485 fprintf (init_file, " /* bifname */\t\"%s\",\n",
2486 bifp->proto.bifname);
2487 fprintf (init_file, " /* enable*/\t%s,\n",
2488 enable_string[bifp->stanza]);
2489 fprintf (init_file, " /* icode */\tCODE_FOR_%s,\n",
2490 bifp->patname);
2491 fprintf (init_file, " /* nargs */\t%d,\n",
2492 bifp->proto.nargs);
2493 fprintf (init_file, " /* bifattrs */\t0");
2494 if (bifp->attrs.isextract)
2495 fprintf (init_file, " | bif_extract_bit");
2496 if (bifp->attrs.isnosoft)
2497 fprintf (init_file, " | bif_nosoft_bit");
2498 if (bifp->attrs.isldvec)
2499 fprintf (init_file, " | bif_ldvec_bit");
2500 if (bifp->attrs.isstvec)
2501 fprintf (init_file, " | bif_stvec_bit");
2502 if (bifp->attrs.isreve)
2503 fprintf (init_file, " | bif_reve_bit");
2504 if (bifp->attrs.ispred)
2505 fprintf (init_file, " | bif_pred_bit");
2506 if (bifp->attrs.ishtm)
2507 fprintf (init_file, " | bif_htm_bit");
2508 if (bifp->attrs.ishtmspr)
2509 fprintf (init_file, " | bif_htmspr_bit");
2510 if (bifp->attrs.ishtmcr)
2511 fprintf (init_file, " | bif_htmcr_bit");
2512 if (bifp->attrs.ismma)
2513 fprintf (init_file, " | bif_mma_bit");
2514 if (bifp->attrs.isquad)
2515 fprintf (init_file, " | bif_quad_bit");
2516 if (bifp->attrs.ispair)
2517 fprintf (init_file, " | bif_pair_bit");
2518 if (bifp->attrs.ismmaint)
2519 fprintf (init_file, " | bif_mmaint_bit");
2520 if (bifp->attrs.isno32bit)
2521 fprintf (init_file, " | bif_no32bit_bit");
2522 if (bifp->attrs.is32bit)
2523 fprintf (init_file, " | bif_32bit_bit");
2524 if (bifp->attrs.iscpu)
2525 fprintf (init_file, " | bif_cpu_bit");
2526 if (bifp->attrs.isldstmask)
2527 fprintf (init_file, " | bif_ldstmask_bit");
2528 if (bifp->attrs.islxvrse)
2529 fprintf (init_file, " | bif_lxvrse_bit");
2530 if (bifp->attrs.islxvrze)
2531 fprintf (init_file, " | bif_lxvrze_bit");
2532 if (bifp->attrs.isendian)
2533 fprintf (init_file, " | bif_endian_bit");
2534 if (bifp->attrs.isibmld)
2535 fprintf (init_file, " | bif_ibmld_bit");
2536 if (bifp->attrs.isibm128)
2537 fprintf (init_file, " | bif_ibm128_bit");
2538 fprintf (init_file, ",\n");
2539 fprintf (init_file, " /* restr_opnd */\t{%d, %d, %d},\n",
2540 bifp->proto.restr_opnd[0], bifp->proto.restr_opnd[1],
2541 bifp->proto.restr_opnd[2]);
2542 for (int j = 0; j < 3; j++)
2543 if (!bifp->proto.restr_opnd[j])
2544 res[j] = "RES_NONE";
2545 else if (bifp->proto.restr[j] == RES_BITS)
2546 res[j] = "RES_BITS";
2547 else if (bifp->proto.restr[j] == RES_RANGE)
2548 res[j] = "RES_RANGE";
2549 else if (bifp->proto.restr[j] == RES_VALUES)
2550 res[j] = "RES_VALUES";
2551 else if (bifp->proto.restr[j] == RES_VAR_RANGE)
2552 res[j] = "RES_VAR_RANGE";
2553 else
2554 res[j] = "ERROR";
2555 fprintf (init_file, " /* restr */\t{%s, %s, %s},\n",
2556 res[0], res[1], res[2]);
2557 fprintf (init_file, " /* restr_val1 */\t{%s, %s, %s},\n",
2558 bifp->proto.restr_val1[0] ? bifp->proto.restr_val1[0] : "0",
2559 bifp->proto.restr_val1[1] ? bifp->proto.restr_val1[1] : "0",
2560 bifp->proto.restr_val1[2] ? bifp->proto.restr_val1[2] : "0");
2561 fprintf (init_file, " /* restr_val2 */\t{%s, %s, %s},\n",
2562 bifp->proto.restr_val2[0] ? bifp->proto.restr_val2[0] : "0",
2563 bifp->proto.restr_val2[1] ? bifp->proto.restr_val2[1] : "0",
2564 bifp->proto.restr_val2[2] ? bifp->proto.restr_val2[2] : "0");
2565 fprintf (init_file, " /* attr_string */\t\"%s\",\n",
2566 (bifp->kind == FNK_CONST ? "= const"
2567 : (bifp->kind == FNK_PURE ? "= pure"
2568 : (bifp->kind == FNK_FPMATH ? "= fp, const"
2569 : ""))));
2570 fprintf (init_file, " /* assoc_bif */\tRS6000_BIF_%s%s\n",
2571 bifp->attrs.ismmaint ? bifp->idname : "NONE",
2572 bifp->attrs.ismmaint ? "_INTERNAL" : "");
2573 fprintf (init_file, " },\n");
2574 }
2575 fprintf (init_file, " };\n\n");
2576
2577 fprintf (init_file, "tree rs6000_builtin_info_fntype[RS6000_BIF_MAX];\n\n");
2578 }
2579
2580 /* Write the decls and initializers for rs6000_overload_info[] and
2581 rs6000_instance_info[]. */
2582 static void
2583 write_ovld_static_init (void)
2584 {
2585 fprintf (init_file,
2586 "ovldrecord rs6000_overload_info[RS6000_OVLD_MAX "
2587 "- RS6000_OVLD_NONE] =\n");
2588 fprintf (init_file, " {\n");
2589 fprintf (init_file, " { /* RS6000_OVLD_NONE: */\n");
2590 fprintf (init_file, " \"\", -1\n");
2591 fprintf (init_file, " },\n");
2592 for (int i = 0; i <= curr_ovld_stanza; i++)
2593 {
2594 fprintf (init_file, " { /* RS6000_OVLD_%s: */\n",
2595 ovld_stanzas[i].stanza_id);
2596 fprintf (init_file, " /* ovld_name */\t\"%s\",\n",
2597 ovld_stanzas[i].intern_name);
2598 /* First-instance must currently be instantiated at run time. */
2599 fprintf (init_file, " /* first_instance */\t-1\n");
2600 fprintf (init_file, " },\n");
2601 }
2602 fprintf (init_file, " };\n\n");
2603
2604 fprintf (init_file, "ovlddata rs6000_instance_info[RS6000_INST_MAX] =\n");
2605 fprintf (init_file, " {\n");
2606 fprintf (init_file, " { /* RS6000_INST_NONE: */\n");
2607 fprintf (init_file, " \"\", RS6000_BIF_NONE, -1\n");
2608 fprintf (init_file, " },\n");
2609 for (int i = 0; i <= curr_ovld; i++)
2610 {
2611 fprintf (init_file, " { /* RS6000_INST_%s: */\n",
2612 ovlds[i].ovld_id_name);
2613 fprintf (init_file, " /* bifname */\t\"%s\",\n",
2614 ovlds[i].proto.bifname);
2615 fprintf (init_file, " /* bifid */\tRS6000_BIF_%s,\n",
2616 ovlds[i].bif_id_name);
2617 fprintf (init_file, " /* next */\t");
2618 if (i < curr_ovld
2619 && !strcmp (ovlds[i+1].proto.bifname, ovlds[i].proto.bifname))
2620 fprintf (init_file,
2621 "RS6000_INST_%s\n",
2622 ovlds[i+1].ovld_id_name);
2623 else
2624 fprintf (init_file, "-1\n");
2625 fprintf (init_file, " },\n");
2626 }
2627 fprintf (init_file, " };\n\n");
2628
2629 fprintf (init_file,
2630 "tree rs6000_instance_info_fntype[RS6000_INST_MAX];\n\n");
2631 }
2632
2633 /* Write code to initialize the built-in function table. */
2634 static void
2635 write_init_bif_table (void)
2636 {
2637 for (int i = 0; i <= curr_bif; i++)
2638 {
2639 fprintf (init_file,
2640 " builtin_info_fntype[RS6000_BIF_%s]"
2641 "\n = %s;\n",
2642 bifs[i].idname, bifs[i].fndecl);
2643
2644 /* Check whether we have a "tf" token in this string, representing
2645 a float128_type_node. It's possible that float128_type_node is
2646 undefined (occurs for -maltivec -mno-vsx, for example), so we
2647 must guard against that. */
2648 int tf_found = strstr (bifs[i].fndecl, "tf") != NULL;
2649
2650 /* Similarly, look for decimal float tokens. */
2651 int dfp_found = (strstr (bifs[i].fndecl, "sd") != NULL
2652 || strstr (bifs[i].fndecl, "dd") != NULL
2653 || strstr (bifs[i].fndecl, "td") != NULL);
2654
2655 if (tf_found)
2656 {
2657 fprintf (init_file, " if (float128_type_node)\n");
2658 fprintf (init_file, " {\n");
2659 }
2660 else if (dfp_found)
2661 {
2662 fprintf (init_file, " if (dfloat64_type_node)\n");
2663 fprintf (init_file, " {\n");
2664 }
2665
2666 fprintf (init_file,
2667 " builtin_decls[(int)RS6000_BIF_%s] = t\n",
2668 bifs[i].idname);
2669 fprintf (init_file,
2670 " = add_builtin_function (\"%s\",\n",
2671 bifs[i].proto.bifname);
2672 fprintf (init_file,
2673 " %s,\n",
2674 bifs[i].fndecl);
2675 fprintf (init_file,
2676 " (int)RS6000_BIF_%s,"
2677 " BUILT_IN_MD,\n",
2678 bifs[i].idname);
2679 fprintf (init_file,
2680 " NULL, NULL_TREE);\n");
2681 if (bifs[i].kind == FNK_CONST)
2682 {
2683 fprintf (init_file, " TREE_READONLY (t) = 1;\n");
2684 fprintf (init_file, " TREE_NOTHROW (t) = 1;\n");
2685 }
2686 else if (bifs[i].kind == FNK_PURE)
2687 {
2688 fprintf (init_file, " DECL_PURE_P (t) = 1;\n");
2689 fprintf (init_file, " TREE_NOTHROW (t) = 1;\n");
2690 }
2691 else if (bifs[i].kind == FNK_FPMATH)
2692 {
2693 fprintf (init_file, " TREE_NOTHROW (t) = 1;\n");
2694 fprintf (init_file, " if (flag_rounding_math)\n");
2695 fprintf (init_file, " {\n");
2696 fprintf (init_file, " DECL_PURE_P (t) = 1;\n");
2697 fprintf (init_file, " DECL_IS_NOVOPS (t) = 1;\n");
2698 fprintf (init_file, " }\n");
2699 fprintf (init_file, " else\n");
2700 fprintf (init_file, " TREE_READONLY (t) = 1;\n");
2701 }
2702
2703 if (tf_found || dfp_found)
2704 {
2705 fprintf (init_file, " }\n");
2706 fprintf (init_file, " else\n");
2707 fprintf (init_file, " {\n");
2708 fprintf (init_file, " builtin_decls"
2709 "[(int)RS6000_BIF_%s] = NULL_TREE;\n", bifs[i].idname);
2710 fprintf (init_file, " }\n");
2711 }
2712 fprintf (init_file, "\n");
2713 }
2714 }
2715
2716 /* Write code to initialize the overload table. */
2717 static void
2718 write_init_ovld_table (void)
2719 {
2720 fprintf (init_file, " int base = RS6000_OVLD_NONE;\n\n");
2721 fprintf (init_file,
2722 " /* The fndecl for an overload is arbitrarily the first one\n"
2723 " for the overload. We sort out the real types when\n"
2724 " processing the overload in the gcc front end. */\n");
2725
2726 for (int i = 0; i <= curr_ovld; i++)
2727 {
2728 fprintf (init_file,
2729 " instance_info_fntype[RS6000_INST_%s]"
2730 "\n = %s;\n",
2731 ovlds[i].ovld_id_name, ovlds[i].fndecl);
2732
2733 if (i == 0 || ovlds[i].stanza != ovlds[i-1].stanza)
2734 {
2735 ovld_stanza *stanza = &ovld_stanzas[ovlds[i].stanza];
2736 fprintf (init_file, "\n");
2737
2738 /* Check whether we have a "tf" token in this string, representing
2739 a float128_type_node. It's possible that float128_type_node is
2740 undefined (occurs for -maltivec -mno-vsx, for example), so we
2741 must guard against that. */
2742 int tf_found = strstr (ovlds[i].fndecl, "tf") != NULL;
2743
2744 /* Similarly, look for decimal float tokens. */
2745 int dfp_found = (strstr (ovlds[i].fndecl, "sd") != NULL
2746 || strstr (ovlds[i].fndecl, "dd") != NULL
2747 || strstr (ovlds[i].fndecl, "td") != NULL);
2748
2749 if (tf_found)
2750 {
2751 fprintf (init_file, " if (float128_type_node)\n");
2752 fprintf (init_file, " {\n");
2753 }
2754 else if (dfp_found)
2755 {
2756 fprintf (init_file, " if (dfloat64_type_node)\n");
2757 fprintf (init_file, " {\n");
2758 }
2759
2760 fprintf (init_file,
2761 " builtin_decls[(int)RS6000_OVLD_%s] = t\n",
2762 stanza->stanza_id);
2763 fprintf (init_file,
2764 " = add_builtin_function (\"%s\",\n",
2765 stanza->intern_name);
2766 fprintf (init_file,
2767 " %s,\n",
2768 ovlds[i].fndecl);
2769 fprintf (init_file,
2770 " (int)RS6000_OVLD_%s,"
2771 " BUILT_IN_MD,\n",
2772 stanza->stanza_id);
2773 fprintf (init_file,
2774 " NULL, NULL_TREE);\n");
2775
2776 if (tf_found || dfp_found)
2777 fprintf (init_file, " }\n");
2778
2779 fprintf (init_file, "\n");
2780
2781 fprintf (init_file,
2782 " overload_info[RS6000_OVLD_%s - base]"
2783 ".first_instance\n",
2784 stanza->stanza_id);
2785 fprintf (init_file,
2786 " = RS6000_INST_%s;\n\n",
2787 ovlds[i].ovld_id_name);
2788 }
2789 }
2790 }
2791
2792 /* Write everything to the initialization file (rs6000-builtins.cc).
2793 Return 1 if successful, 0 otherwise. */
2794 static int
2795 write_init_file (void)
2796 {
2797 write_autogenerated_header (init_file);
2798
2799 fprintf (init_file, "#include \"config.h\"\n");
2800 fprintf (init_file, "#include \"system.h\"\n");
2801 fprintf (init_file, "#include \"coretypes.h\"\n");
2802 fprintf (init_file, "#include \"backend.h\"\n");
2803 fprintf (init_file, "#include \"rtl.h\"\n");
2804 fprintf (init_file, "#include \"tree.h\"\n");
2805 fprintf (init_file, "#include \"langhooks.h\"\n");
2806 fprintf (init_file, "#include \"insn-codes.h\"\n");
2807 fprintf (init_file, "#include \"rs6000-builtins.h\"\n");
2808 fprintf (init_file, "\n");
2809
2810 fprintf (init_file, "tree rs6000_builtin_decls[RS6000_OVLD_MAX];\n\n");
2811
2812 write_bif_static_init ();
2813 write_ovld_static_init ();
2814
2815 /* The reason to pass pointers to the function instead of accessing
2816 the rs6000_{{builtin,instance}_info_fntype,overload_info,builtin_decls}
2817 arrays directly is to decrease size of the already large function and
2818 noipa prevents the compiler with LTO to undo that optimization. */
2819 fprintf (init_file, "#if GCC_VERSION >= 8001\n");
2820 fprintf (init_file, "__attribute__((__noipa__))\n");
2821 fprintf (init_file, "#endif\n");
2822 fprintf (init_file, "void\n");
2823 fprintf (init_file,
2824 "rs6000_init_generated_builtins (tree *builtin_info_fntype,\n");
2825 fprintf (init_file, "\t\t\t\ttree *instance_info_fntype,\n");
2826 fprintf (init_file, "\t\t\t\tovldrecord *overload_info,\n");
2827 fprintf (init_file, "\t\t\t\ttree *builtin_decls)\n");
2828 fprintf (init_file, "{\n");
2829 fprintf (init_file, " tree t;\n");
2830 rbt_inorder_callback (&fntype_rbt, fntype_rbt.rbt_root, write_fntype_init);
2831 fprintf (init_file, "\n");
2832
2833 fprintf (init_file,
2834 " builtin_decls[RS6000_BIF_NONE] = NULL_TREE;\n");
2835 fprintf (init_file,
2836 " builtin_decls[RS6000_BIF_MAX] = NULL_TREE;\n");
2837 fprintf (init_file,
2838 " builtin_decls[RS6000_OVLD_NONE] = NULL_TREE;\n\n");
2839
2840 write_init_bif_table ();
2841 write_init_ovld_table ();
2842
2843 fprintf (init_file, "}\n\n");
2844
2845 return 1;
2846 }
2847
2848 /* Write everything to the include file (rs6000-vecdefines.h).
2849 Return 1 if successful, 0 otherwise. */
2850 static int
2851 write_defines_file (void)
2852 {
2853 fprintf (defines_file, "#ifndef _RS6000_VECDEFINES_H\n");
2854 fprintf (defines_file, "#define _RS6000_VECDEFINES_H 1\n\n");
2855 fprintf (defines_file, "#if defined(_ARCH_PPC64) && defined (_ARCH_PWR9)\n");
2856 fprintf (defines_file, " #define _ARCH_PPC64_PWR9 1\n");
2857 fprintf (defines_file, "#endif\n\n");
2858 for (int i = 0; i < num_ovld_stanzas; i++)
2859 if (strcmp (ovld_stanzas[i].extern_name, "SKIP"))
2860 {
2861 if (ovld_stanzas[i].ifdef)
2862 fprintf (defines_file, "#ifdef %s\n", ovld_stanzas[i].ifdef);
2863 fprintf (defines_file, "#define %s %s\n",
2864 ovld_stanzas[i].extern_name,
2865 ovld_stanzas[i].intern_name);
2866 if (ovld_stanzas[i].ifdef)
2867 fprintf (defines_file, "#endif\n");
2868 }
2869 fprintf (defines_file, "\n#endif\n");
2870 return 1;
2871 }
2872
2873 /* Close and delete output files after any failure, so that subsequent
2874 build dependencies will fail. */
2875 static void
2876 delete_output_files (void)
2877 {
2878 /* Depending on whence we're called, some of these may already be
2879 closed. Don't check for errors. */
2880 fclose (header_file);
2881 fclose (init_file);
2882 fclose (defines_file);
2883
2884 remove (header_path);
2885 remove (init_path);
2886 remove (defines_path);
2887 }
2888
2889 /* Main program to convert flat files into built-in initialization code. */
2890 int
2891 main (int argc, const char **argv)
2892 {
2893 if (argc != 6)
2894 {
2895 fprintf (stderr,
2896 "Five arguments required: two input files and three output "
2897 "files.\n");
2898 exit (1);
2899 }
2900
2901 pgm_path = argv[0];
2902 bif_path = argv[1];
2903 ovld_path = argv[2];
2904 header_path = argv[3];
2905 init_path = argv[4];
2906 defines_path = argv[5];
2907
2908 bif_file = fopen (bif_path, "r");
2909 if (!bif_file)
2910 {
2911 fprintf (stderr, "Cannot open input built-in file '%s'.\n", bif_path);
2912 exit (1);
2913 }
2914 ovld_file = fopen (ovld_path, "r");
2915 if (!ovld_file)
2916 {
2917 fprintf (stderr, "Cannot open input overload file '%s'.\n", ovld_path);
2918 exit (1);
2919 }
2920 header_file = fopen (header_path, "w");
2921 if (!header_file)
2922 {
2923 fprintf (stderr, "Cannot open header file '%s' for output.\n",
2924 header_path);
2925 exit (1);
2926 }
2927 init_file = fopen (init_path, "w");
2928 if (!init_file)
2929 {
2930 fprintf (stderr, "Cannot open init file '%s' for output.\n", init_path);
2931 exit (1);
2932 }
2933 defines_file = fopen (defines_path, "w");
2934 if (!defines_file)
2935 {
2936 fprintf (stderr, "Cannot open defines file '%s' for output.\n",
2937 defines_path);
2938 exit (1);
2939 }
2940
2941 /* Allocate some buffers. */
2942 for (int i = 0; i < MAXLINES; i++)
2943 lines[i] = (char *) malloc (LINELEN);
2944
2945 /* Initialize the balanced trees containing built-in function ids,
2946 overload function ids, and function type declaration ids. */
2947 rbt_new (&bif_rbt);
2948 rbt_new (&ovld_rbt);
2949 rbt_new (&fntype_rbt);
2950
2951 /* Initialize another balanced tree that contains a map from built-in
2952 function ids to the order in which they were encountered. */
2953 rbt_new (&bifo_rbt);
2954
2955 /* Parse the built-in function file. */
2956 num_bifs = 0;
2957 line = 0;
2958 if (parse_bif () == PC_PARSEFAIL)
2959 {
2960 fprintf (stderr, "Parsing of '%s' failed, aborting.\n", bif_path);
2961 delete_output_files ();
2962 exit (1);
2963 }
2964 fclose (bif_file);
2965
2966 /* Create a mapping from function IDs in their final order to
2967 the order they appear in the built-in function file. */
2968 create_bif_order ();
2969
2970 #ifdef DEBUG
2971 fprintf (stderr, "\nFunction ID list:\n");
2972 rbt_dump (&bif_rbt, bif_rbt.rbt_root);
2973 fprintf (stderr, "\n");
2974 #endif
2975
2976 /* Parse the overload file. */
2977 num_ovld_stanzas = 0;
2978 num_ovlds = 0;
2979 line = 0;
2980 if (parse_ovld () == PC_PARSEFAIL)
2981 {
2982 fprintf (stderr, "Parsing of '%s' failed, aborting.\n", ovld_path);
2983 delete_output_files ();
2984 exit (1);
2985 }
2986 fclose (ovld_file);
2987
2988 #ifdef DEBUG
2989 fprintf (stderr, "\nFunction type decl list:\n");
2990 rbt_dump (&fntype_rbt, fntype_rbt.rbt_root);
2991 fprintf (stderr, "\n");
2992 #endif
2993
2994 /* Write the header file and the file containing initialization code. */
2995 if (!write_header_file ())
2996 {
2997 fprintf (stderr, "Output to '%s' failed, aborting.\n", header_path);
2998 delete_output_files ();
2999 exit (1);
3000 }
3001 if (!write_init_file ())
3002 {
3003 fprintf (stderr, "Output to '%s' failed, aborting.\n", init_path);
3004 delete_output_files ();
3005 exit (1);
3006 }
3007
3008 /* Write the defines file to be included into altivec.h. */
3009 if (!write_defines_file ())
3010 {
3011 fprintf (stderr, "Output to '%s' failed, aborting.\n", defines_path);
3012 delete_output_files ();
3013 exit (1);
3014 }
3015
3016 /* Always close init_file last. This avoids race conditions in the
3017 build machinery. See comments in t-rs6000. */
3018 fclose (header_file);
3019 fclose (defines_file);
3020 fclose (init_file);
3021
3022 return 0;
3023 }