1 /* brig-lang.c -- brig (HSAIL) input gcc interface.
2    Copyright (C) 2016-2021 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 == EXCESS_PRECISION_DEFAULT)
170     flag_excess_precision = 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 int flag_isoc2x;
591 
592 static void
def_fn_type(builtin_type def,builtin_type ret,bool var,int n,...)593 def_fn_type (builtin_type def, builtin_type ret, bool var, int n, ...)
594 {
595   tree t;
596   tree *args = XALLOCAVEC (tree, n);
597   va_list list;
598   int i;
599   bool err = false;
600 
601   va_start (list, n);
602   for (i = 0; i < n; ++i)
603     {
604       builtin_type a = (builtin_type) va_arg (list, int);
605       t = builtin_types[a];
606       if (t == error_mark_node)
607 	err = true;
608       args[i] = t;
609     }
610   va_end (list);
611 
612   t = builtin_types[ret];
613   if (err)
614     t = error_mark_node;
615   if (t == error_mark_node)
616     ;
617   else if (var)
618     t = build_varargs_function_type_array (t, n, args);
619   else
620     t = build_function_type_array (t, n, args);
621 
622   builtin_types[def] = t;
623 }
624 
625 /* Used to help initialize the builtin-types.def table.  When a type of
626    the correct size doesn't exist, use error_mark_node instead of NULL.
627    The later results in segfaults even when a decl using the type doesn't
628    get invoked.  */
629 
630 static tree
builtin_type_for_size(int size,bool unsignedp)631 builtin_type_for_size (int size, bool unsignedp)
632 {
633   tree type = brig_langhook_type_for_size (size, unsignedp);
634   return type ? type : error_mark_node;
635 }
636 
637 /* Support for DEF_BUILTIN.  */
638 
639 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)640 def_builtin_1 (enum built_in_function fncode, const char *name,
641 	       enum built_in_class fnclass ATTRIBUTE_UNUSED,
642 	       tree fntype, tree libtype ATTRIBUTE_UNUSED,
643 	       bool both_p ATTRIBUTE_UNUSED, bool fallback_p,
644 	       bool nonansi_p ATTRIBUTE_UNUSED, tree fnattrs,
645 	       bool implicit_p)
646 {
647   tree decl;
648   const char *libname;
649 
650   if (fntype == error_mark_node)
651     return;
652 
653   libname = name + strlen ("__builtin_");
654   decl = add_builtin_function (name, fntype, fncode, fnclass,
655 			       (fallback_p ? libname : NULL),
656 			       fnattrs);
657 
658   set_builtin_decl (fncode, decl, implicit_p);
659 }
660 
661 
662 /* Initialize the attribute table for all the supported builtins.  */
663 
664 static void
brig_init_attributes(void)665 brig_init_attributes (void)
666 {
667   /* Fill in the built_in_attributes array.  */
668 #define DEF_ATTR_NULL_TREE(ENUM)				\
669   built_in_attributes[(int) ENUM] = NULL_TREE;
670 #define DEF_ATTR_INT(ENUM, VALUE)				\
671   built_in_attributes[(int) ENUM] = build_int_cst (NULL_TREE, VALUE);
672 #define DEF_ATTR_STRING(ENUM, VALUE)				\
673   built_in_attributes[(int) ENUM] = build_string (strlen (VALUE), VALUE);
674 #define DEF_ATTR_IDENT(ENUM, STRING)				\
675   built_in_attributes[(int) ENUM] = get_identifier (STRING);
676 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN)	\
677   built_in_attributes[(int) ENUM]			\
678     = tree_cons (built_in_attributes[(int) PURPOSE],	\
679 		 built_in_attributes[(int) VALUE],	\
680 		 built_in_attributes[(int) CHAIN]);
681 #include "builtin-attrs.def"
682 #undef DEF_ATTR_NULL_TREE
683 #undef DEF_ATTR_INT
684 #undef DEF_ATTR_STRING
685 #undef DEF_ATTR_IDENT
686 #undef DEF_ATTR_TREE_LIST
687 }
688 
689 /* Create builtin types and functions.  VA_LIST_REF_TYPE_NODE and
690    VA_LIST_ARG_TYPE_NODE are used in builtin-types.def.  */
691 
692 static void
brig_define_builtins(tree va_list_ref_type_node ATTRIBUTE_UNUSED,tree va_list_arg_type_node ATTRIBUTE_UNUSED)693 brig_define_builtins (tree va_list_ref_type_node ATTRIBUTE_UNUSED,
694 		     tree va_list_arg_type_node ATTRIBUTE_UNUSED)
695 {
696 #define DEF_PRIMITIVE_TYPE(ENUM, VALUE) \
697   builtin_types[ENUM] = VALUE;
698 #define DEF_FUNCTION_TYPE_0(ENUM, RETURN) \
699   def_fn_type (ENUM, RETURN, 0, 0);
700 #define DEF_FUNCTION_TYPE_1(ENUM, RETURN, ARG1) \
701   def_fn_type (ENUM, RETURN, 0, 1, ARG1);
702 #define DEF_FUNCTION_TYPE_2(ENUM, RETURN, ARG1, ARG2) \
703   def_fn_type (ENUM, RETURN, 0, 2, ARG1, ARG2);
704 #define DEF_FUNCTION_TYPE_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
705   def_fn_type (ENUM, RETURN, 0, 3, ARG1, ARG2, ARG3);
706 #define DEF_FUNCTION_TYPE_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
707   def_fn_type (ENUM, RETURN, 0, 4, ARG1, ARG2, ARG3, ARG4);
708 #define DEF_FUNCTION_TYPE_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5)	\
709   def_fn_type (ENUM, RETURN, 0, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
710 #define DEF_FUNCTION_TYPE_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
711 			    ARG6)					\
712   def_fn_type (ENUM, RETURN, 0, 6, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6);
713 #define DEF_FUNCTION_TYPE_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
714 			    ARG6, ARG7)					\
715   def_fn_type (ENUM, RETURN, 0, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
716 #define DEF_FUNCTION_TYPE_8(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
717 			    ARG6, ARG7, ARG8)				\
718   def_fn_type (ENUM, RETURN, 0, 8, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6,	\
719 	       ARG7, ARG8);
720 #define DEF_FUNCTION_TYPE_9(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
721 			    ARG6, ARG7, ARG8, ARG9)			\
722   def_fn_type (ENUM, RETURN, 0, 9, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6,	\
723 	       ARG7, ARG8, ARG9);
724 #define DEF_FUNCTION_TYPE_10(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
725 			     ARG6, ARG7, ARG8, ARG9, ARG10)		 \
726   def_fn_type (ENUM, RETURN, 0, 10, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6,	 \
727 	       ARG7, ARG8, ARG9, ARG10);
728 #define DEF_FUNCTION_TYPE_11(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
729 			     ARG6, ARG7, ARG8, ARG9, ARG10, ARG11)	 \
730   def_fn_type (ENUM, RETURN, 0, 11, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6,	 \
731 	       ARG7, ARG8, ARG9, ARG10, ARG11);
732 #define DEF_FUNCTION_TYPE_VAR_0(ENUM, RETURN) \
733   def_fn_type (ENUM, RETURN, 1, 0);
734 #define DEF_FUNCTION_TYPE_VAR_1(ENUM, RETURN, ARG1) \
735   def_fn_type (ENUM, RETURN, 1, 1, ARG1);
736 #define DEF_FUNCTION_TYPE_VAR_2(ENUM, RETURN, ARG1, ARG2) \
737   def_fn_type (ENUM, RETURN, 1, 2, ARG1, ARG2);
738 #define DEF_FUNCTION_TYPE_VAR_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
739   def_fn_type (ENUM, RETURN, 1, 3, ARG1, ARG2, ARG3);
740 #define DEF_FUNCTION_TYPE_VAR_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
741   def_fn_type (ENUM, RETURN, 1, 4, ARG1, ARG2, ARG3, ARG4);
742 #define DEF_FUNCTION_TYPE_VAR_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
743   def_fn_type (ENUM, RETURN, 1, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
744 #define DEF_FUNCTION_TYPE_VAR_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
745 				 ARG6)	\
746   def_fn_type (ENUM, RETURN, 1, 6, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6);
747 #define DEF_FUNCTION_TYPE_VAR_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
748 				ARG6, ARG7)				\
749   def_fn_type (ENUM, RETURN, 1, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
750 #define DEF_POINTER_TYPE(ENUM, TYPE) \
751   builtin_types[(int) ENUM] = build_pointer_type (builtin_types[(int) TYPE]);
752 
753 #include "builtin-types.def"
754 
755 #undef DEF_PRIMITIVE_TYPE
756 #undef DEF_FUNCTION_TYPE_0
757 #undef DEF_FUNCTION_TYPE_1
758 #undef DEF_FUNCTION_TYPE_2
759 #undef DEF_FUNCTION_TYPE_3
760 #undef DEF_FUNCTION_TYPE_4
761 #undef DEF_FUNCTION_TYPE_5
762 #undef DEF_FUNCTION_TYPE_6
763 #undef DEF_FUNCTION_TYPE_7
764 #undef DEF_FUNCTION_TYPE_8
765 #undef DEF_FUNCTION_TYPE_9
766 #undef DEF_FUNCTION_TYPE_10
767 #undef DEF_FUNCTION_TYPE_11
768 #undef DEF_FUNCTION_TYPE_VAR_0
769 #undef DEF_FUNCTION_TYPE_VAR_1
770 #undef DEF_FUNCTION_TYPE_VAR_2
771 #undef DEF_FUNCTION_TYPE_VAR_3
772 #undef DEF_FUNCTION_TYPE_VAR_4
773 #undef DEF_FUNCTION_TYPE_VAR_5
774 #undef DEF_FUNCTION_TYPE_VAR_6
775 #undef DEF_FUNCTION_TYPE_VAR_7
776 #undef DEF_POINTER_TYPE
777   builtin_types[(int) BT_LAST] = NULL_TREE;
778 
779   brig_init_attributes ();
780 
781 #define DEF_BUILTIN(ENUM, NAME, CLASS, TYPE, LIBTYPE, BOTH_P, FALLBACK_P,\
782 		    NONANSI_P, ATTRS, IMPLICIT, COND)			\
783     if (NAME && COND)							\
784       def_builtin_1 (ENUM, NAME, CLASS, builtin_types[(int) TYPE],	\
785 		     builtin_types[(int) LIBTYPE], BOTH_P, FALLBACK_P,	\
786 		     NONANSI_P, built_in_attributes[(int) ATTRS], IMPLICIT);
787 
788 #undef DEF_HSAIL_BUILTIN
789 #define DEF_HSAIL_BUILTIN(ENUM, HSAIL_OPCODE, HSAIL_TYPE, NAME, TYPE, ATTRS) \
790   DEF_BUILTIN (ENUM, "__builtin_" NAME, BUILT_IN_NORMAL, TYPE, TYPE,    \
791               false, true, true, ATTRS, false, true)
792 
793 /* HSAIL atomic builtins do not have separate identifying opcodes.  */
794 
795 #undef DEF_HSAIL_ATOMIC_BUILTIN
796 #define DEF_HSAIL_ATOMIC_BUILTIN(ENUM, ATOMIC_OPCODE, HSAIL_TYPE, NAME, \
797        TYPE, ATTRS) \
798   DEF_BUILTIN (ENUM, "__builtin_" NAME, BUILT_IN_NORMAL, TYPE, TYPE,    \
799               false, true, true, ATTRS, false, true)
800 
801 /* HSAIL saturating arithmetics builtins.  */
802 
803 #undef DEF_HSAIL_SAT_BUILTIN
804 #define DEF_HSAIL_SAT_BUILTIN(ENUM, BRIG_OPCODE, HSAIL_TYPE, NAME, \
805        TYPE, ATTRS) \
806   DEF_BUILTIN (ENUM, "__builtin_" NAME, BUILT_IN_NORMAL, TYPE, TYPE,    \
807               false, true, true, ATTRS, false, true)
808 
809 /* HSAIL builtins used internally by the frontend.  */
810 
811 #undef DEF_HSAIL_INTR_BUILTIN
812 #define DEF_HSAIL_INTR_BUILTIN(ENUM, NAME, TYPE, ATTRS) \
813   DEF_BUILTIN (ENUM, "__builtin_" NAME, BUILT_IN_NORMAL, TYPE, TYPE,    \
814               false, true, true, ATTRS, false, true)
815 
816 /* HSAIL saturated conversions.  */
817 
818 #undef DEF_HSAIL_CVT_ZEROI_SAT_BUILTIN
819 #define DEF_HSAIL_CVT_ZEROI_SAT_BUILTIN(ENUM, HSAIL_DEST_TYPE, HSAIL_SRC_TYPE, \
820   NAME, TYPE, ATTRS) \
821   DEF_BUILTIN (ENUM, "__builtin_" NAME, BUILT_IN_NORMAL, TYPE, TYPE,    \
822               false, true, true, ATTRS, false, true)
823 
824 #include "builtins.def"
825 }
826 
827 /* Build nodes that would have be created by the C front-end; necessary
828    for including builtin-types.def and ultimately builtins.def.  Borrowed
829    from lto-lang.c.  */
830 
831 static void
brig_build_c_type_nodes(void)832 brig_build_c_type_nodes (void)
833 {
834   gcc_assert (void_type_node);
835 
836   void_list_node = build_tree_list (NULL_TREE, void_type_node);
837   string_type_node = build_pointer_type (char_type_node);
838   const_string_type_node
839     = build_pointer_type (build_qualified_type (char_type_node,
840 						TYPE_QUAL_CONST));
841 
842   if (strcmp (SIZE_TYPE, "unsigned int") == 0)
843     {
844       intmax_type_node = integer_type_node;
845       uintmax_type_node = unsigned_type_node;
846       signed_size_type_node = integer_type_node;
847     }
848   else if (strcmp (SIZE_TYPE, "long unsigned int") == 0)
849     {
850       intmax_type_node = long_integer_type_node;
851       uintmax_type_node = long_unsigned_type_node;
852       signed_size_type_node = long_integer_type_node;
853     }
854   else if (strcmp (SIZE_TYPE, "long long unsigned int") == 0)
855     {
856       intmax_type_node = long_long_integer_type_node;
857       uintmax_type_node = long_long_unsigned_type_node;
858       signed_size_type_node = long_long_integer_type_node;
859     }
860   else
861     {
862       int i;
863 
864       signed_size_type_node = NULL_TREE;
865       for (i = 0; i < NUM_INT_N_ENTS; i++)
866 	if (int_n_enabled_p[i])
867 	  {
868 	    char name[50], altname[50];
869 	    sprintf (name, "__int%d unsigned", int_n_data[i].bitsize);
870 	    sprintf (altname, "__int%d__ unsigned", int_n_data[i].bitsize);
871 
872 	    if (strcmp (name, SIZE_TYPE) == 0
873 		|| strcmp (altname, SIZE_TYPE) == 0)
874 	      {
875 		intmax_type_node = int_n_trees[i].signed_type;
876 		uintmax_type_node = int_n_trees[i].unsigned_type;
877 		signed_size_type_node = int_n_trees[i].signed_type;
878 	      }
879 	  }
880       if (signed_size_type_node == NULL_TREE)
881 	gcc_unreachable ();
882     }
883 
884   wint_type_node = unsigned_type_node;
885   pid_type_node = integer_type_node;
886 }
887 
888 
889 static bool
brig_langhook_init(void)890 brig_langhook_init (void)
891 {
892   build_common_tree_nodes (false);
893 
894   /* Builtin initialization related code borrowed from lto-lang.c.  */
895   void_list_node = build_tree_list (NULL_TREE, void_type_node);
896 
897   brig_build_c_type_nodes ();
898 
899   if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
900     {
901       tree x = build_pointer_type (TREE_TYPE (va_list_type_node));
902       brig_define_builtins (x, x);
903     }
904   else
905     {
906       brig_define_builtins (build_reference_type (va_list_type_node),
907 			    va_list_type_node);
908     }
909 
910   targetm.init_builtins ();
911   build_common_builtin_nodes ();
912 
913   return true;
914 }
915 
916 #undef LANG_HOOKS_NAME
917 #undef LANG_HOOKS_INIT
918 #undef LANG_HOOKS_OPTION_LANG_MASK
919 #undef LANG_HOOKS_INIT_OPTIONS_STRUCT
920 #undef LANG_HOOKS_HANDLE_OPTION
921 #undef LANG_HOOKS_POST_OPTIONS
922 #undef LANG_HOOKS_PARSE_FILE
923 #undef LANG_HOOKS_TYPE_FOR_MODE
924 #undef LANG_HOOKS_TYPE_FOR_SIZE
925 #undef LANG_HOOKS_REGISTER_BUILTIN_TYPE
926 #undef LANG_HOOKS_BUILTIN_FUNCTION
927 #undef LANG_HOOKS_GLOBAL_BINDINGS_P
928 #undef LANG_HOOKS_PUSHDECL
929 #undef LANG_HOOKS_GETDECLS
930 #undef LANG_HOOKS_WRITE_GLOBALS
931 #undef LANG_HOOKS_GIMPLIFY_EXPR
932 #undef LANG_HOOKS_EH_PERSONALITY
933 
934 #define LANG_HOOKS_NAME "GNU Brig"
935 #define LANG_HOOKS_INIT brig_langhook_init
936 #define LANG_HOOKS_OPTION_LANG_MASK brig_langhook_option_lang_mask
937 #define LANG_HOOKS_INIT_OPTIONS_STRUCT brig_langhook_init_options_struct
938 #define LANG_HOOKS_HANDLE_OPTION brig_langhook_handle_option
939 #define LANG_HOOKS_POST_OPTIONS brig_langhook_post_options
940 #define LANG_HOOKS_PARSE_FILE brig_langhook_parse_file
941 #define LANG_HOOKS_TYPE_FOR_MODE brig_langhook_type_for_mode
942 #define LANG_HOOKS_TYPE_FOR_SIZE brig_langhook_type_for_size
943 #define LANG_HOOKS_REGISTER_BUILTIN_TYPE brig_langhook_register_builtin_type
944 #define LANG_HOOKS_BUILTIN_FUNCTION brig_langhook_builtin_function
945 #define LANG_HOOKS_GLOBAL_BINDINGS_P brig_langhook_global_bindings_p
946 #define LANG_HOOKS_PUSHDECL brig_langhook_pushdecl
947 #define LANG_HOOKS_GETDECLS brig_langhook_getdecls
948 #define LANG_HOOKS_GIMPLIFY_EXPR brig_langhook_gimplify_expr
949 #define LANG_HOOKS_EH_PERSONALITY brig_langhook_eh_personality
950 
951 /* Attribute hooks.  */
952 #undef LANG_HOOKS_COMMON_ATTRIBUTE_TABLE
953 #define LANG_HOOKS_COMMON_ATTRIBUTE_TABLE brig_attribute_table
954 
955 struct lang_hooks lang_hooks = LANG_HOOKS_INITIALIZER;
956 
957 #include "gt-brig-brig-lang.h"
958 #include "gtype-brig.h"
959