1 /* brig-lang.c -- brig (HSAIL) input gcc interface.
2 Copyright (C) 2016-2019 Free Software Foundation, Inc.
3 Contributed by Pekka Jaaskelainen <pekka.jaaskelainen@parmance.com>
4 for General Processor Tech.
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
12
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
21
22 #include "config.h"
23 #include "system.h"
24 #include "ansidecl.h"
25 #include "coretypes.h"
26 #include "opts.h"
27 #include "tree.h"
28 #include "tree-iterator.h"
29 #include "print-tree.h"
30 #include "stringpool.h"
31 #include "basic-block.h"
32 #include "gimple-expr.h"
33 #include "gimplify.h"
34 #include "dumpfile.h"
35 #include "stor-layout.h"
36 #include "toplev.h"
37 #include "debug.h"
38 #include "options.h"
39 #include "flags.h"
40 #include "convert.h"
41 #include "diagnostic.h"
42 #include "langhooks.h"
43 #include "langhooks-def.h"
44 #include "target.h"
45 #include "vec.h"
46 #include "brigfrontend/brig-to-generic.h"
47 #include "machmode.h"
48 #include "fold-const.h"
49 #include "common/common-target.h"
50 #include <mpfr.h>
51 #include "brig-c.h"
52 #include "brig-builtins.h"
53
54 static tree handle_leaf_attribute (tree *, tree, tree, int, bool *);
55 static tree handle_const_attribute (tree *, tree, tree, int, bool *);
56 static tree handle_pure_attribute (tree *, tree, tree, int, bool *);
57 static tree handle_nothrow_attribute (tree *, tree, tree, int, bool *);
58 static tree handle_returns_twice_attribute (tree *, tree, tree, int, bool *);
59
60 /* This file is based on Go frontend's go-lang.c and gogo-tree.cc. */
61
62 /* If -v set. */
63
64 int gccbrig_verbose = 0;
65
66 /* Language-dependent contents of a type. */
67
68 struct GTY (()) lang_type
69 {
70 char dummy;
71 };
72
73 /* Language-dependent contents of a decl. */
74
75 struct GTY ((variable_size)) lang_decl
76 {
77 char dummy;
78 };
79
80 /* Language-dependent contents of an identifier. This must include a
81 tree_identifier. */
82
83 struct GTY (()) lang_identifier
84 {
85 struct tree_identifier common;
86 };
87
88 /* The resulting tree type. */
89
90 union GTY ((desc ("TREE_CODE (&%h.generic) == IDENTIFIER_NODE"),
91 chain_next ("CODE_CONTAINS_STRUCT (TREE_CODE (&%h.generic), "
92 "TS_COMMON) ? ((union lang_tree_node *) TREE_CHAIN "
93 "(&%h.generic)) : NULL"))) lang_tree_node
94 {
95 union tree_node GTY ((tag ("0"), desc ("tree_node_structure (&%h)"))) generic;
96 struct lang_identifier GTY ((tag ("1"))) identifier;
97 };
98
99 /* We don't use language_function. */
100
101 struct GTY (()) language_function
102 {
103 int dummy;
104 };
105
106
107 /* The option mask. */
108
109 static unsigned int
brig_langhook_option_lang_mask(void)110 brig_langhook_option_lang_mask (void)
111 {
112 return CL_BRIG;
113 }
114
115 /* Initialize the options structure. */
116
117 static void
brig_langhook_init_options_struct(struct gcc_options * opts)118 brig_langhook_init_options_struct (struct gcc_options *opts)
119 {
120 /* Signed overflow is precisely defined. */
121 opts->x_flag_wrapv = 1;
122
123 /* If we set this to one, the whole program optimizations internalize
124 all global variables, making them invisible to the dyn loader (and
125 thus the HSA runtime implementation). */
126 opts->x_flag_whole_program = 1;
127
128 /* The builtin math functions should not set errno. */
129 opts->x_flag_errno_math = 0;
130 opts->frontend_set_flag_errno_math = false;
131
132 opts->x_flag_exceptions = 0;
133 opts->x_flag_non_call_exceptions = 0;
134
135 opts->x_flag_finite_math_only = 0;
136 opts->x_flag_signed_zeros = 1;
137
138 opts->x_optimize = 3;
139
140 flag_no_builtin = 1;
141 }
142
143 /* Handle Brig specific options. Return 0 if we didn't do anything. */
144
145 static bool
brig_langhook_handle_option(size_t scode,const char * arg ATTRIBUTE_UNUSED,HOST_WIDE_INT value ATTRIBUTE_UNUSED,int kind ATTRIBUTE_UNUSED,location_t loc ATTRIBUTE_UNUSED,const struct cl_option_handlers * handlers ATTRIBUTE_UNUSED)146 brig_langhook_handle_option
147 (size_t scode, const char *arg ATTRIBUTE_UNUSED,
148 HOST_WIDE_INT value ATTRIBUTE_UNUSED, int kind ATTRIBUTE_UNUSED,
149 location_t loc ATTRIBUTE_UNUSED,
150 const struct cl_option_handlers *handlers ATTRIBUTE_UNUSED)
151 {
152 enum opt_code code = (enum opt_code) scode;
153 switch (code)
154 {
155 case OPT_v:
156 gccbrig_verbose = 1;
157 break;
158 default:
159 break;
160 }
161 return 1;
162 }
163
164 /* Run after parsing options. */
165
166 static bool
brig_langhook_post_options(const char ** pfilename ATTRIBUTE_UNUSED)167 brig_langhook_post_options (const char **pfilename ATTRIBUTE_UNUSED)
168 {
169 if (flag_excess_precision_cmdline == EXCESS_PRECISION_DEFAULT)
170 flag_excess_precision_cmdline = EXCESS_PRECISION_STANDARD;
171
172 /* gccbrig casts pointers around like crazy, TBAA might produce broken
173 code if not disabling it by default. Some PRM conformance tests such
174 as prm/core/memory/ordinary/ld/ld_u16 fail currently with strict
175 aliasing (to fix). It can be enabled from the command line for cases
176 that are known not to break the C style aliasing requirements. */
177 if (!global_options_set.x_flag_strict_aliasing)
178 flag_strict_aliasing = 0;
179 else
180 flag_strict_aliasing = global_options.x_flag_strict_aliasing;
181
182 /* Returning false means that the backend should be used. */
183 return false;
184 }
185
186 static size_t
get_file_size(FILE * file)187 get_file_size (FILE *file)
188 {
189 size_t size;
190 fseek (file, 0, SEEK_END);
191 size = (size_t) ftell (file);
192 fseek (file, 0, SEEK_SET);
193 return size;
194 }
195
196 static void
brig_langhook_parse_file(void)197 brig_langhook_parse_file (void)
198 {
199 brig_to_generic brig_to_gen;
200
201 std::vector <char*> brig_blobs;
202
203 for (unsigned int i = 0; i < num_in_fnames; ++i)
204 {
205
206 FILE *f;
207 f = fopen (in_fnames[i], "r");
208 size_t fsize = get_file_size (f);
209 char *brig_blob = new char[fsize];
210 if (fread (brig_blob, 1, fsize, f) != fsize)
211 {
212 error ("could not read the BRIG file");
213 exit (1);
214 }
215 fclose (f);
216
217 brig_to_gen.analyze (brig_blob);
218 brig_blobs.push_back (brig_blob);
219 }
220
221 for (size_t i = 0; i < brig_blobs.size(); ++i)
222 {
223 char *brig_blob = brig_blobs.at(i);
224 brig_to_gen.parse (brig_blob);
225 }
226
227 brig_to_gen.write_globals ();
228
229 for (size_t i = 0; i < brig_blobs.size (); ++i)
230 delete brig_blobs[i];
231 }
232
233 static tree
brig_langhook_type_for_size(unsigned int bits,int unsignedp)234 brig_langhook_type_for_size (unsigned int bits,
235 int unsignedp)
236 {
237 /* Copied from go-lang.c */
238 tree type;
239 if (unsignedp)
240 {
241 if (bits == INT_TYPE_SIZE)
242 type = unsigned_type_node;
243 else if (bits == CHAR_TYPE_SIZE)
244 type = unsigned_char_type_node;
245 else if (bits == SHORT_TYPE_SIZE)
246 type = short_unsigned_type_node;
247 else if (bits == LONG_TYPE_SIZE)
248 type = long_unsigned_type_node;
249 else if (bits == LONG_LONG_TYPE_SIZE)
250 type = long_long_unsigned_type_node;
251 else
252 type = make_unsigned_type(bits);
253 }
254 else
255 {
256 if (bits == INT_TYPE_SIZE)
257 type = integer_type_node;
258 else if (bits == CHAR_TYPE_SIZE)
259 type = signed_char_type_node;
260 else if (bits == SHORT_TYPE_SIZE)
261 type = short_integer_type_node;
262 else if (bits == LONG_TYPE_SIZE)
263 type = long_integer_type_node;
264 else if (bits == LONG_LONG_TYPE_SIZE)
265 type = long_long_integer_type_node;
266 else
267 type = make_signed_type(bits);
268 }
269 return type;
270 }
271
272 static tree
brig_langhook_type_for_mode(machine_mode mode,int unsignedp)273 brig_langhook_type_for_mode (machine_mode mode, int unsignedp)
274 {
275 if (mode == TYPE_MODE (void_type_node))
276 return void_type_node;
277
278 if (VECTOR_MODE_P (mode))
279 {
280 tree inner;
281
282 inner = brig_langhook_type_for_mode (GET_MODE_INNER (mode), unsignedp);
283 if (inner != NULL_TREE)
284 return build_vector_type_for_mode (inner, mode);
285 gcc_unreachable ();
286 return NULL_TREE;
287 }
288
289 scalar_int_mode imode;
290 scalar_float_mode fmode;
291 if (is_float_mode (mode, &fmode))
292 {
293 switch (GET_MODE_BITSIZE (fmode))
294 {
295 case 32:
296 return float_type_node;
297 case 64:
298 return double_type_node;
299 default:
300 /* We have to check for long double in order to support
301 i386 excess precision. */
302 if (fmode == TYPE_MODE (long_double_type_node))
303 return long_double_type_node;
304
305 gcc_unreachable ();
306 return NULL_TREE;
307 }
308 }
309 else if (is_int_mode (mode, &imode))
310 return brig_langhook_type_for_size (GET_MODE_BITSIZE (imode), unsignedp);
311 else
312 {
313 /* E.g., build_common_builtin_nodes () asks for modes/builtins
314 we do not generate or need. Just ignore them silently for now.
315 */
316 return NULL_TREE;
317 }
318 return NULL_TREE;
319 }
320
321 static tree
brig_langhook_builtin_function(tree decl)322 brig_langhook_builtin_function (tree decl)
323 {
324 return decl;
325 }
326
327 static GTY(()) tree registered_builtin_types;
328
329 static void
brig_langhook_register_builtin_type(tree type,const char * name)330 brig_langhook_register_builtin_type (tree type, const char *name)
331 {
332 tree decl;
333
334 if (!TYPE_NAME (type))
335 {
336 decl = build_decl (UNKNOWN_LOCATION, TYPE_DECL,
337 get_identifier (name), type);
338 DECL_ARTIFICIAL (decl) = 1;
339 TYPE_NAME (type) = decl;
340 }
341
342 registered_builtin_types = tree_cons (0, type, registered_builtin_types);
343 }
344
345
346 /* Return true if we are in the global binding level. */
347
348 static bool
brig_langhook_global_bindings_p(void)349 brig_langhook_global_bindings_p (void)
350 {
351 return current_function_decl == NULL_TREE;
352 }
353
354 /* Push a declaration into the current binding level. From Go: We can't
355 usefully implement this since we don't want to convert from tree
356 back to one of our internal data structures. I think the only way
357 this is used is to record a decl which is to be returned by
358 getdecls, and we could implement it for that purpose if
359 necessary. */
360
361 static tree
brig_langhook_pushdecl(tree decl ATTRIBUTE_UNUSED)362 brig_langhook_pushdecl (tree decl ATTRIBUTE_UNUSED)
363 {
364 gcc_unreachable ();
365 }
366
367 /* This hook is used to get the current list of declarations as trees.
368 From Go: We don't support that; instead we use the write_globals hook.
369 This can't simply crash because it is called by -gstabs. */
370
371 static tree
brig_langhook_getdecls(void)372 brig_langhook_getdecls (void)
373 {
374 return NULL;
375 }
376
377 static int
brig_langhook_gimplify_expr(tree * expr_p,gimple_seq * pre_p ATTRIBUTE_UNUSED,gimple_seq * post_p ATTRIBUTE_UNUSED)378 brig_langhook_gimplify_expr (tree *expr_p, gimple_seq *pre_p ATTRIBUTE_UNUSED,
379 gimple_seq *post_p ATTRIBUTE_UNUSED)
380 {
381
382 /* Strip off the static chain info that appears to function
383 calls for some strange reason even though we don't add
384 nested functions. Maybe something wrong with the function
385 declaration contexts? */
386 if (TREE_CODE (*expr_p) == CALL_EXPR
387 && CALL_EXPR_STATIC_CHAIN (*expr_p) != NULL_TREE)
388 CALL_EXPR_STATIC_CHAIN (*expr_p) = NULL_TREE;
389 return GS_UNHANDLED;
390 }
391
392 static tree
brig_langhook_eh_personality(void)393 brig_langhook_eh_personality (void)
394 {
395 gcc_unreachable ();
396 }
397
398 /* Functions called directly by the generic backend.
399 Adapted from go-lang.c. */
400
401 tree
convert(tree type,tree expr)402 convert (tree type, tree expr)
403 {
404 if (type == error_mark_node || expr == error_mark_node
405 || TREE_TYPE (expr) == error_mark_node)
406 return error_mark_node;
407
408 if (type == TREE_TYPE (expr))
409 return expr;
410
411 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (TREE_TYPE (expr)))
412 return fold_convert (type, expr);
413
414 switch (TREE_CODE (type))
415 {
416 case VOID_TYPE:
417 case BOOLEAN_TYPE:
418 return fold_convert (type, expr);
419 case INTEGER_TYPE:
420 return fold (convert_to_integer (type, expr));
421 case REAL_TYPE:
422 return fold (convert_to_real (type, expr));
423 case VECTOR_TYPE:
424 return fold (convert_to_vector (type, expr));
425 case POINTER_TYPE:
426 return build1 (VIEW_CONVERT_EXPR, type, convert (size_type_node, expr));
427 default:
428 break;
429 }
430
431 gcc_unreachable ();
432 }
433
434 static GTY (()) tree brig_gc_root;
435
436 /* Preserve trees that we create from the garbage collector. */
437
438 void
brig_preserve_from_gc(tree t)439 brig_preserve_from_gc (tree t)
440 {
441 brig_gc_root = tree_cons (NULL_TREE, t, brig_gc_root);
442 }
443
444 /* Convert an identifier for use in an error message. */
445
446 const char *
brig_localize_identifier(const char * ident)447 brig_localize_identifier (const char *ident)
448 {
449 return identifier_to_locale (ident);
450 }
451
452 /* Define supported attributes and their handlers. Code copied from
453 lto-lang.c */
454
455 /* Table of machine-independent attributes supported in GIMPLE. */
456 const struct attribute_spec brig_attribute_table[] =
457 {
458 /* { name, min_len, max_len, decl_req, type_req, fn_type_req,
459 affects_type_identity, handler, exclude } */
460 { "leaf", 0, 0, true, false, false, false,
461 handle_leaf_attribute, NULL },
462 { "const", 0, 0, true, false, false, false,
463 handle_const_attribute, NULL },
464 { "pure", 0, 0, true, false, false, false,
465 handle_pure_attribute, NULL },
466 { "nothrow", 0, 0, true, false, false, false,
467 handle_nothrow_attribute, NULL },
468 { "returns_twice", 0, 0, true, false, false, false,
469 handle_returns_twice_attribute, NULL },
470 { NULL, 0, 0, false, false, false, false, NULL, NULL }
471 };
472
473 /* Attribute handlers. */
474 /* Handle a "leaf" attribute; arguments as in
475 struct attribute_spec.handler. */
476
477 static tree
handle_leaf_attribute(tree * node,tree name,tree ARG_UNUSED (args),int ARG_UNUSED (flags),bool * no_add_attrs)478 handle_leaf_attribute (tree *node, tree name,
479 tree ARG_UNUSED (args),
480 int ARG_UNUSED (flags), bool *no_add_attrs)
481 {
482 if (TREE_CODE (*node) != FUNCTION_DECL)
483 {
484 warning (OPT_Wattributes, "%qE attribute ignored", name);
485 *no_add_attrs = true;
486 }
487 if (!TREE_PUBLIC (*node))
488 {
489 warning (OPT_Wattributes,
490 "%qE attribute has no effect on unit local functions", name);
491 *no_add_attrs = true;
492 }
493
494 return NULL_TREE;
495 }
496
497 /* Handle a "const" attribute; arguments as in
498 struct attribute_spec.handler. */
499
500 static tree
handle_const_attribute(tree * node,tree ARG_UNUSED (name),tree ARG_UNUSED (args),int ARG_UNUSED (flags),bool * ARG_UNUSED (no_add_attrs))501 handle_const_attribute (tree *node, tree ARG_UNUSED (name),
502 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
503 bool * ARG_UNUSED (no_add_attrs))
504 {
505 tree type = TREE_TYPE (*node);
506
507 /* See FIXME comment on noreturn in c_common_attribute_table. */
508 if (TREE_CODE (*node) == FUNCTION_DECL)
509 TREE_READONLY (*node) = 1;
510 else if (TREE_CODE (type) == POINTER_TYPE
511 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
512 TREE_TYPE (*node)
513 = build_pointer_type
514 (build_type_variant (TREE_TYPE (type), 1,
515 TREE_THIS_VOLATILE (TREE_TYPE (type))));
516 else
517 gcc_unreachable ();
518
519 return NULL_TREE;
520 }
521
522 /* Handle a "pure" attribute; arguments as in
523 struct attribute_spec.handler. */
524
525 static tree
handle_pure_attribute(tree * node,tree ARG_UNUSED (name),tree ARG_UNUSED (args),int ARG_UNUSED (flags),bool * ARG_UNUSED (no_add_attrs))526 handle_pure_attribute (tree *node, tree ARG_UNUSED (name),
527 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
528 bool * ARG_UNUSED (no_add_attrs))
529 {
530 if (TREE_CODE (*node) == FUNCTION_DECL)
531 DECL_PURE_P (*node) = 1;
532 else
533 gcc_unreachable ();
534
535 return NULL_TREE;
536 }
537
538 /* Handle a "nothrow" attribute; arguments as in
539 struct attribute_spec.handler. */
540
541 static tree
handle_nothrow_attribute(tree * node,tree ARG_UNUSED (name),tree ARG_UNUSED (args),int ARG_UNUSED (flags),bool * ARG_UNUSED (no_add_attrs))542 handle_nothrow_attribute (tree *node, tree ARG_UNUSED (name),
543 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
544 bool * ARG_UNUSED (no_add_attrs))
545 {
546 if (TREE_CODE (*node) == FUNCTION_DECL)
547 TREE_NOTHROW (*node) = 1;
548 else
549 gcc_unreachable ();
550
551 return NULL_TREE;
552 }
553
554 /* Handle a "returns_twice" attribute. */
555
556 static tree
handle_returns_twice_attribute(tree * node,tree ARG_UNUSED (name),tree ARG_UNUSED (args),int ARG_UNUSED (flags),bool * ARG_UNUSED (no_add_attrs))557 handle_returns_twice_attribute (tree *node, tree ARG_UNUSED (name),
558 tree ARG_UNUSED (args),
559 int ARG_UNUSED (flags),
560 bool * ARG_UNUSED (no_add_attrs))
561 {
562 gcc_assert (TREE_CODE (*node) == FUNCTION_DECL);
563
564 DECL_IS_RETURNS_TWICE (*node) = 1;
565
566 return NULL_TREE;
567 }
568
569
570 /* Built-in initialization code cribbed from lto-lang.c which cribbed it
571 from c-common.c. */
572
573
574 static GTY(()) tree built_in_attributes[(int) ATTR_LAST];
575
576
577 static GTY(()) tree builtin_types[(int) BT_LAST + 1];
578
579 static GTY(()) tree string_type_node;
580 static GTY(()) tree const_string_type_node;
581 static GTY(()) tree wint_type_node;
582 static GTY(()) tree intmax_type_node;
583 static GTY(()) tree uintmax_type_node;
584 static GTY(()) tree signed_size_type_node;
585
586 /* Flags needed to process builtins.def. */
587 int flag_isoc94;
588 int flag_isoc99;
589 int flag_isoc11;
590
591 static void
def_fn_type(builtin_type def,builtin_type ret,bool var,int n,...)592 def_fn_type (builtin_type def, builtin_type ret, bool var, int n, ...)
593 {
594 tree t;
595 tree *args = XALLOCAVEC (tree, n);
596 va_list list;
597 int i;
598 bool err = false;
599
600 va_start (list, n);
601 for (i = 0; i < n; ++i)
602 {
603 builtin_type a = (builtin_type) va_arg (list, int);
604 t = builtin_types[a];
605 if (t == error_mark_node)
606 err = true;
607 args[i] = t;
608 }
609 va_end (list);
610
611 t = builtin_types[ret];
612 if (err)
613 t = error_mark_node;
614 if (t == error_mark_node)
615 ;
616 else if (var)
617 t = build_varargs_function_type_array (t, n, args);
618 else
619 t = build_function_type_array (t, n, args);
620
621 builtin_types[def] = t;
622 }
623
624 /* Used to help initialize the builtin-types.def table. When a type of
625 the correct size doesn't exist, use error_mark_node instead of NULL.
626 The later results in segfaults even when a decl using the type doesn't
627 get invoked. */
628
629 static tree
builtin_type_for_size(int size,bool unsignedp)630 builtin_type_for_size (int size, bool unsignedp)
631 {
632 tree type = brig_langhook_type_for_size (size, unsignedp);
633 return type ? type : error_mark_node;
634 }
635
636 /* Support for DEF_BUILTIN. */
637
638 static void
def_builtin_1(enum built_in_function fncode,const char * name,enum built_in_class fnclass ATTRIBUTE_UNUSED,tree fntype,tree libtype ATTRIBUTE_UNUSED,bool both_p ATTRIBUTE_UNUSED,bool fallback_p,bool nonansi_p ATTRIBUTE_UNUSED,tree fnattrs,bool implicit_p)639 def_builtin_1 (enum built_in_function fncode, const char *name,
640 enum built_in_class fnclass ATTRIBUTE_UNUSED,
641 tree fntype, tree libtype ATTRIBUTE_UNUSED,
642 bool both_p ATTRIBUTE_UNUSED, bool fallback_p,
643 bool nonansi_p ATTRIBUTE_UNUSED, tree fnattrs,
644 bool implicit_p)
645 {
646 tree decl;
647 const char *libname;
648
649 if (fntype == error_mark_node)
650 return;
651
652 libname = name + strlen ("__builtin_");
653 decl = add_builtin_function (name, fntype, fncode, fnclass,
654 (fallback_p ? libname : NULL),
655 fnattrs);
656
657 set_builtin_decl (fncode, decl, implicit_p);
658 }
659
660
661 /* Initialize the attribute table for all the supported builtins. */
662
663 static void
brig_init_attributes(void)664 brig_init_attributes (void)
665 {
666 /* Fill in the built_in_attributes array. */
667 #define DEF_ATTR_NULL_TREE(ENUM) \
668 built_in_attributes[(int) ENUM] = NULL_TREE;
669 #define DEF_ATTR_INT(ENUM, VALUE) \
670 built_in_attributes[(int) ENUM] = build_int_cst (NULL_TREE, VALUE);
671 #define DEF_ATTR_STRING(ENUM, VALUE) \
672 built_in_attributes[(int) ENUM] = build_string (strlen (VALUE), VALUE);
673 #define DEF_ATTR_IDENT(ENUM, STRING) \
674 built_in_attributes[(int) ENUM] = get_identifier (STRING);
675 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) \
676 built_in_attributes[(int) ENUM] \
677 = tree_cons (built_in_attributes[(int) PURPOSE], \
678 built_in_attributes[(int) VALUE], \
679 built_in_attributes[(int) CHAIN]);
680 #include "builtin-attrs.def"
681 #undef DEF_ATTR_NULL_TREE
682 #undef DEF_ATTR_INT
683 #undef DEF_ATTR_STRING
684 #undef DEF_ATTR_IDENT
685 #undef DEF_ATTR_TREE_LIST
686 }
687
688 /* Create builtin types and functions. VA_LIST_REF_TYPE_NODE and
689 VA_LIST_ARG_TYPE_NODE are used in builtin-types.def. */
690
691 static void
brig_define_builtins(tree va_list_ref_type_node ATTRIBUTE_UNUSED,tree va_list_arg_type_node ATTRIBUTE_UNUSED)692 brig_define_builtins (tree va_list_ref_type_node ATTRIBUTE_UNUSED,
693 tree va_list_arg_type_node ATTRIBUTE_UNUSED)
694 {
695 #define DEF_PRIMITIVE_TYPE(ENUM, VALUE) \
696 builtin_types[ENUM] = VALUE;
697 #define DEF_FUNCTION_TYPE_0(ENUM, RETURN) \
698 def_fn_type (ENUM, RETURN, 0, 0);
699 #define DEF_FUNCTION_TYPE_1(ENUM, RETURN, ARG1) \
700 def_fn_type (ENUM, RETURN, 0, 1, ARG1);
701 #define DEF_FUNCTION_TYPE_2(ENUM, RETURN, ARG1, ARG2) \
702 def_fn_type (ENUM, RETURN, 0, 2, ARG1, ARG2);
703 #define DEF_FUNCTION_TYPE_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
704 def_fn_type (ENUM, RETURN, 0, 3, ARG1, ARG2, ARG3);
705 #define DEF_FUNCTION_TYPE_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
706 def_fn_type (ENUM, RETURN, 0, 4, ARG1, ARG2, ARG3, ARG4);
707 #define DEF_FUNCTION_TYPE_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
708 def_fn_type (ENUM, RETURN, 0, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
709 #define DEF_FUNCTION_TYPE_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
710 ARG6) \
711 def_fn_type (ENUM, RETURN, 0, 6, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6);
712 #define DEF_FUNCTION_TYPE_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
713 ARG6, ARG7) \
714 def_fn_type (ENUM, RETURN, 0, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
715 #define DEF_FUNCTION_TYPE_8(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
716 ARG6, ARG7, ARG8) \
717 def_fn_type (ENUM, RETURN, 0, 8, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
718 ARG7, ARG8);
719 #define DEF_FUNCTION_TYPE_9(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
720 ARG6, ARG7, ARG8, ARG9) \
721 def_fn_type (ENUM, RETURN, 0, 9, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
722 ARG7, ARG8, ARG9);
723 #define DEF_FUNCTION_TYPE_10(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
724 ARG6, ARG7, ARG8, ARG9, ARG10) \
725 def_fn_type (ENUM, RETURN, 0, 10, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
726 ARG7, ARG8, ARG9, ARG10);
727 #define DEF_FUNCTION_TYPE_11(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
728 ARG6, ARG7, ARG8, ARG9, ARG10, ARG11) \
729 def_fn_type (ENUM, RETURN, 0, 11, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
730 ARG7, ARG8, ARG9, ARG10, ARG11);
731 #define DEF_FUNCTION_TYPE_VAR_0(ENUM, RETURN) \
732 def_fn_type (ENUM, RETURN, 1, 0);
733 #define DEF_FUNCTION_TYPE_VAR_1(ENUM, RETURN, ARG1) \
734 def_fn_type (ENUM, RETURN, 1, 1, ARG1);
735 #define DEF_FUNCTION_TYPE_VAR_2(ENUM, RETURN, ARG1, ARG2) \
736 def_fn_type (ENUM, RETURN, 1, 2, ARG1, ARG2);
737 #define DEF_FUNCTION_TYPE_VAR_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
738 def_fn_type (ENUM, RETURN, 1, 3, ARG1, ARG2, ARG3);
739 #define DEF_FUNCTION_TYPE_VAR_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
740 def_fn_type (ENUM, RETURN, 1, 4, ARG1, ARG2, ARG3, ARG4);
741 #define DEF_FUNCTION_TYPE_VAR_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
742 def_fn_type (ENUM, RETURN, 1, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
743 #define DEF_FUNCTION_TYPE_VAR_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
744 ARG6) \
745 def_fn_type (ENUM, RETURN, 1, 6, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6);
746 #define DEF_FUNCTION_TYPE_VAR_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
747 ARG6, ARG7) \
748 def_fn_type (ENUM, RETURN, 1, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
749 #define DEF_POINTER_TYPE(ENUM, TYPE) \
750 builtin_types[(int) ENUM] = build_pointer_type (builtin_types[(int) TYPE]);
751
752 #include "builtin-types.def"
753
754 #undef DEF_PRIMITIVE_TYPE
755 #undef DEF_FUNCTION_TYPE_0
756 #undef DEF_FUNCTION_TYPE_1
757 #undef DEF_FUNCTION_TYPE_2
758 #undef DEF_FUNCTION_TYPE_3
759 #undef DEF_FUNCTION_TYPE_4
760 #undef DEF_FUNCTION_TYPE_5
761 #undef DEF_FUNCTION_TYPE_6
762 #undef DEF_FUNCTION_TYPE_7
763 #undef DEF_FUNCTION_TYPE_8
764 #undef DEF_FUNCTION_TYPE_9
765 #undef DEF_FUNCTION_TYPE_10
766 #undef DEF_FUNCTION_TYPE_11
767 #undef DEF_FUNCTION_TYPE_VAR_0
768 #undef DEF_FUNCTION_TYPE_VAR_1
769 #undef DEF_FUNCTION_TYPE_VAR_2
770 #undef DEF_FUNCTION_TYPE_VAR_3
771 #undef DEF_FUNCTION_TYPE_VAR_4
772 #undef DEF_FUNCTION_TYPE_VAR_5
773 #undef DEF_FUNCTION_TYPE_VAR_6
774 #undef DEF_FUNCTION_TYPE_VAR_7
775 #undef DEF_POINTER_TYPE
776 builtin_types[(int) BT_LAST] = NULL_TREE;
777
778 brig_init_attributes ();
779
780 #define DEF_BUILTIN(ENUM, NAME, CLASS, TYPE, LIBTYPE, BOTH_P, FALLBACK_P,\
781 NONANSI_P, ATTRS, IMPLICIT, COND) \
782 if (NAME && COND) \
783 def_builtin_1 (ENUM, NAME, CLASS, builtin_types[(int) TYPE], \
784 builtin_types[(int) LIBTYPE], BOTH_P, FALLBACK_P, \
785 NONANSI_P, built_in_attributes[(int) ATTRS], IMPLICIT);
786
787 #undef DEF_HSAIL_BUILTIN
788 #define DEF_HSAIL_BUILTIN(ENUM, HSAIL_OPCODE, HSAIL_TYPE, NAME, TYPE, ATTRS) \
789 DEF_BUILTIN (ENUM, "__builtin_" NAME, BUILT_IN_NORMAL, TYPE, TYPE, \
790 false, true, true, ATTRS, false, true)
791
792 /* HSAIL atomic builtins do not have separate identifying opcodes. */
793
794 #undef DEF_HSAIL_ATOMIC_BUILTIN
795 #define DEF_HSAIL_ATOMIC_BUILTIN(ENUM, ATOMIC_OPCODE, HSAIL_TYPE, NAME, \
796 TYPE, ATTRS) \
797 DEF_BUILTIN (ENUM, "__builtin_" NAME, BUILT_IN_NORMAL, TYPE, TYPE, \
798 false, true, true, ATTRS, false, true)
799
800 /* HSAIL saturating arithmetics builtins. */
801
802 #undef DEF_HSAIL_SAT_BUILTIN
803 #define DEF_HSAIL_SAT_BUILTIN(ENUM, BRIG_OPCODE, HSAIL_TYPE, NAME, \
804 TYPE, ATTRS) \
805 DEF_BUILTIN (ENUM, "__builtin_" NAME, BUILT_IN_NORMAL, TYPE, TYPE, \
806 false, true, true, ATTRS, false, true)
807
808 /* HSAIL builtins used internally by the frontend. */
809
810 #undef DEF_HSAIL_INTR_BUILTIN
811 #define DEF_HSAIL_INTR_BUILTIN(ENUM, NAME, TYPE, ATTRS) \
812 DEF_BUILTIN (ENUM, "__builtin_" NAME, BUILT_IN_NORMAL, TYPE, TYPE, \
813 false, true, true, ATTRS, false, true)
814
815 /* HSAIL saturated conversions. */
816
817 #undef DEF_HSAIL_CVT_ZEROI_SAT_BUILTIN
818 #define DEF_HSAIL_CVT_ZEROI_SAT_BUILTIN(ENUM, HSAIL_DEST_TYPE, HSAIL_SRC_TYPE, \
819 NAME, TYPE, ATTRS) \
820 DEF_BUILTIN (ENUM, "__builtin_" NAME, BUILT_IN_NORMAL, TYPE, TYPE, \
821 false, true, true, ATTRS, false, true)
822
823 #include "builtins.def"
824 }
825
826 /* Build nodes that would have be created by the C front-end; necessary
827 for including builtin-types.def and ultimately builtins.def. Borrowed
828 from lto-lang.c. */
829
830 static void
brig_build_c_type_nodes(void)831 brig_build_c_type_nodes (void)
832 {
833 gcc_assert (void_type_node);
834
835 void_list_node = build_tree_list (NULL_TREE, void_type_node);
836 string_type_node = build_pointer_type (char_type_node);
837 const_string_type_node
838 = build_pointer_type (build_qualified_type (char_type_node,
839 TYPE_QUAL_CONST));
840
841 if (strcmp (SIZE_TYPE, "unsigned int") == 0)
842 {
843 intmax_type_node = integer_type_node;
844 uintmax_type_node = unsigned_type_node;
845 signed_size_type_node = integer_type_node;
846 }
847 else if (strcmp (SIZE_TYPE, "long unsigned int") == 0)
848 {
849 intmax_type_node = long_integer_type_node;
850 uintmax_type_node = long_unsigned_type_node;
851 signed_size_type_node = long_integer_type_node;
852 }
853 else if (strcmp (SIZE_TYPE, "long long unsigned int") == 0)
854 {
855 intmax_type_node = long_long_integer_type_node;
856 uintmax_type_node = long_long_unsigned_type_node;
857 signed_size_type_node = long_long_integer_type_node;
858 }
859 else
860 {
861 int i;
862
863 signed_size_type_node = NULL_TREE;
864 for (i = 0; i < NUM_INT_N_ENTS; i++)
865 if (int_n_enabled_p[i])
866 {
867 char name[50];
868 sprintf (name, "__int%d unsigned", int_n_data[i].bitsize);
869
870 if (strcmp (name, SIZE_TYPE) == 0)
871 {
872 intmax_type_node = int_n_trees[i].signed_type;
873 uintmax_type_node = int_n_trees[i].unsigned_type;
874 signed_size_type_node = int_n_trees[i].signed_type;
875 }
876 }
877 if (signed_size_type_node == NULL_TREE)
878 gcc_unreachable ();
879 }
880
881 wint_type_node = unsigned_type_node;
882 pid_type_node = integer_type_node;
883 }
884
885
886 static bool
brig_langhook_init(void)887 brig_langhook_init (void)
888 {
889 build_common_tree_nodes (false);
890
891 /* Builtin initialization related code borrowed from lto-lang.c. */
892 void_list_node = build_tree_list (NULL_TREE, void_type_node);
893
894 brig_build_c_type_nodes ();
895
896 if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
897 {
898 tree x = build_pointer_type (TREE_TYPE (va_list_type_node));
899 brig_define_builtins (x, x);
900 }
901 else
902 {
903 brig_define_builtins (build_reference_type (va_list_type_node),
904 va_list_type_node);
905 }
906
907 targetm.init_builtins ();
908 build_common_builtin_nodes ();
909
910 return true;
911 }
912
913 #undef LANG_HOOKS_NAME
914 #undef LANG_HOOKS_INIT
915 #undef LANG_HOOKS_OPTION_LANG_MASK
916 #undef LANG_HOOKS_INIT_OPTIONS_STRUCT
917 #undef LANG_HOOKS_HANDLE_OPTION
918 #undef LANG_HOOKS_POST_OPTIONS
919 #undef LANG_HOOKS_PARSE_FILE
920 #undef LANG_HOOKS_TYPE_FOR_MODE
921 #undef LANG_HOOKS_TYPE_FOR_SIZE
922 #undef LANG_HOOKS_REGISTER_BUILTIN_TYPE
923 #undef LANG_HOOKS_BUILTIN_FUNCTION
924 #undef LANG_HOOKS_GLOBAL_BINDINGS_P
925 #undef LANG_HOOKS_PUSHDECL
926 #undef LANG_HOOKS_GETDECLS
927 #undef LANG_HOOKS_WRITE_GLOBALS
928 #undef LANG_HOOKS_GIMPLIFY_EXPR
929 #undef LANG_HOOKS_EH_PERSONALITY
930
931 #define LANG_HOOKS_NAME "GNU Brig"
932 #define LANG_HOOKS_INIT brig_langhook_init
933 #define LANG_HOOKS_OPTION_LANG_MASK brig_langhook_option_lang_mask
934 #define LANG_HOOKS_INIT_OPTIONS_STRUCT brig_langhook_init_options_struct
935 #define LANG_HOOKS_HANDLE_OPTION brig_langhook_handle_option
936 #define LANG_HOOKS_POST_OPTIONS brig_langhook_post_options
937 #define LANG_HOOKS_PARSE_FILE brig_langhook_parse_file
938 #define LANG_HOOKS_TYPE_FOR_MODE brig_langhook_type_for_mode
939 #define LANG_HOOKS_TYPE_FOR_SIZE brig_langhook_type_for_size
940 #define LANG_HOOKS_REGISTER_BUILTIN_TYPE brig_langhook_register_builtin_type
941 #define LANG_HOOKS_BUILTIN_FUNCTION brig_langhook_builtin_function
942 #define LANG_HOOKS_GLOBAL_BINDINGS_P brig_langhook_global_bindings_p
943 #define LANG_HOOKS_PUSHDECL brig_langhook_pushdecl
944 #define LANG_HOOKS_GETDECLS brig_langhook_getdecls
945 #define LANG_HOOKS_GIMPLIFY_EXPR brig_langhook_gimplify_expr
946 #define LANG_HOOKS_EH_PERSONALITY brig_langhook_eh_personality
947
948 /* Attribute hooks. */
949 #undef LANG_HOOKS_COMMON_ATTRIBUTE_TABLE
950 #define LANG_HOOKS_COMMON_ATTRIBUTE_TABLE brig_attribute_table
951
952 struct lang_hooks lang_hooks = LANG_HOOKS_INITIALIZER;
953
954 #include "gt-brig-brig-lang.h"
955 #include "gtype-brig.h"
956