]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/brig/brig-lang.c
Remove enum before machine_mode
[thirdparty/gcc.git] / gcc / brig / brig-lang.c
1 /* brig-lang.c -- brig (HSAIL) input gcc interface.
2 Copyright (C) 2016-2017 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 /* This file is based on Go frontent'd go-lang.c and gogo-tree.cc. */
55
56 /* If -v set. */
57
58 int gccbrig_verbose = 0;
59
60 /* Language-dependent contents of a type. */
61
62 struct GTY (()) lang_type
63 {
64 char dummy;
65 };
66
67 /* Language-dependent contents of a decl. */
68
69 struct GTY ((variable_size)) lang_decl
70 {
71 char dummy;
72 };
73
74 /* Language-dependent contents of an identifier. This must include a
75 tree_identifier. */
76
77 struct GTY (()) lang_identifier
78 {
79 struct tree_identifier common;
80 };
81
82 /* The resulting tree type. */
83
84 union GTY ((desc ("TREE_CODE (&%h.generic) == IDENTIFIER_NODE"),
85 chain_next ("CODE_CONTAINS_STRUCT (TREE_CODE (&%h.generic), "
86 "TS_COMMON) ? ((union lang_tree_node *) TREE_CHAIN "
87 "(&%h.generic)) : NULL"))) lang_tree_node
88 {
89 union tree_node GTY ((tag ("0"), desc ("tree_node_structure (&%h)"))) generic;
90 struct lang_identifier GTY ((tag ("1"))) identifier;
91 };
92
93 /* We don't use language_function. */
94
95 struct GTY (()) language_function
96 {
97 int dummy;
98 };
99
100
101 /* The option mask. */
102
103 static unsigned int
104 brig_langhook_option_lang_mask (void)
105 {
106 return CL_BRIG;
107 }
108
109 /* Initialize the options structure. */
110
111 static void
112 brig_langhook_init_options_struct (struct gcc_options *opts)
113 {
114 /* Signed overflow is precisely defined. */
115 opts->x_flag_wrapv = 1;
116
117 /* If we set this to one, the whole program optimizations internalize
118 all global variables, making them invisible to the dyn loader (and
119 thus the HSA runtime implementation). */
120 opts->x_flag_whole_program = 0;
121
122 /* The builtin math functions should not set errno. */
123 opts->x_flag_errno_math = 0;
124 opts->frontend_set_flag_errno_math = false;
125
126 opts->x_flag_exceptions = 0;
127 opts->x_flag_non_call_exceptions = 0;
128
129 opts->x_flag_finite_math_only = 0;
130 opts->x_flag_signed_zeros = 1;
131 }
132
133 /* Handle Brig specific options. Return 0 if we didn't do anything. */
134
135 static bool
136 brig_langhook_handle_option
137 (size_t scode, const char *arg ATTRIBUTE_UNUSED,
138 int value ATTRIBUTE_UNUSED, int kind ATTRIBUTE_UNUSED,
139 location_t loc ATTRIBUTE_UNUSED,
140 const struct cl_option_handlers *handlers ATTRIBUTE_UNUSED)
141 {
142 enum opt_code code = (enum opt_code) scode;
143 switch (code)
144 {
145 case OPT_v:
146 gccbrig_verbose = 1;
147 break;
148 default:
149 break;
150 }
151 return 1;
152 }
153
154 /* Run after parsing options. */
155
156 static bool
157 brig_langhook_post_options (const char **pfilename ATTRIBUTE_UNUSED)
158 {
159 if (flag_excess_precision_cmdline == EXCESS_PRECISION_DEFAULT)
160 flag_excess_precision_cmdline = EXCESS_PRECISION_STANDARD;
161
162 /* gccbrig casts pointers around like crazy, TBAA produces
163 broken code if not force disabling it. */
164 flag_strict_aliasing = 0;
165
166 /* Returning false means that the backend should be used. */
167 return false;
168 }
169
170 static size_t
171 get_file_size (FILE *file)
172 {
173 size_t size;
174 fseek (file, 0, SEEK_END);
175 size = (size_t) ftell (file);
176 fseek (file, 0, SEEK_SET);
177 return size;
178 }
179
180 static void
181 brig_langhook_parse_file (void)
182 {
183 brig_to_generic brig_to_gen;
184
185 for (unsigned int i = 0; i < num_in_fnames; ++i)
186 {
187
188 FILE *f;
189 f = fopen (in_fnames[i], "r");
190 size_t fsize = get_file_size (f);
191 char *brig_blob = new char[fsize];
192 if (fread (brig_blob, 1, fsize, f) != fsize)
193 {
194 error ("could not read the BRIG file");
195 exit (1);
196 }
197 brig_to_gen.parse (brig_blob);
198 fclose (f);
199 }
200
201 brig_to_gen.write_globals ();
202 }
203
204 static tree
205 brig_langhook_type_for_size (unsigned int bits,
206 int unsignedp)
207 {
208 /* Copied from go-lang.c */
209 tree type;
210 if (unsignedp)
211 {
212 if (bits == INT_TYPE_SIZE)
213 type = unsigned_type_node;
214 else if (bits == CHAR_TYPE_SIZE)
215 type = unsigned_char_type_node;
216 else if (bits == SHORT_TYPE_SIZE)
217 type = short_unsigned_type_node;
218 else if (bits == LONG_TYPE_SIZE)
219 type = long_unsigned_type_node;
220 else if (bits == LONG_LONG_TYPE_SIZE)
221 type = long_long_unsigned_type_node;
222 else
223 type = make_unsigned_type(bits);
224 }
225 else
226 {
227 if (bits == INT_TYPE_SIZE)
228 type = integer_type_node;
229 else if (bits == CHAR_TYPE_SIZE)
230 type = signed_char_type_node;
231 else if (bits == SHORT_TYPE_SIZE)
232 type = short_integer_type_node;
233 else if (bits == LONG_TYPE_SIZE)
234 type = long_integer_type_node;
235 else if (bits == LONG_LONG_TYPE_SIZE)
236 type = long_long_integer_type_node;
237 else
238 type = make_signed_type(bits);
239 }
240 return type;
241 }
242
243 static tree
244 brig_langhook_type_for_mode (machine_mode mode, int unsignedp)
245 {
246 if (mode == TYPE_MODE (void_type_node))
247 return void_type_node;
248
249 if (VECTOR_MODE_P (mode))
250 {
251 tree inner;
252
253 inner = brig_langhook_type_for_mode (GET_MODE_INNER (mode), unsignedp);
254 if (inner != NULL_TREE)
255 return build_vector_type_for_mode (inner, mode);
256 gcc_unreachable ();
257 return NULL_TREE;
258 }
259
260 enum mode_class mc = GET_MODE_CLASS (mode);
261 if (mc == MODE_FLOAT)
262 {
263 switch (GET_MODE_BITSIZE (mode))
264 {
265 case 32:
266 return float_type_node;
267 case 64:
268 return double_type_node;
269 default:
270 /* We have to check for long double in order to support
271 i386 excess precision. */
272 if (mode == TYPE_MODE (long_double_type_node))
273 return long_double_type_node;
274
275 gcc_unreachable ();
276 return NULL_TREE;
277 }
278 }
279 else if (mc == MODE_INT)
280 return brig_langhook_type_for_size(GET_MODE_BITSIZE(mode), unsignedp);
281 else
282 {
283 /* E.g., build_common_builtin_nodes () asks for modes/builtins
284 we do not generate or need. Just ignore them silently for now.
285 */
286 return NULL_TREE;
287 }
288 return NULL_TREE;
289 }
290
291 static tree
292 brig_langhook_builtin_function (tree decl)
293 {
294 return decl;
295 }
296
297 static GTY(()) tree registered_builtin_types;
298
299 static void
300 brig_langhook_register_builtin_type (tree type, const char *name)
301 {
302 tree decl;
303
304 if (!TYPE_NAME (type))
305 {
306 decl = build_decl (UNKNOWN_LOCATION, TYPE_DECL,
307 get_identifier (name), type);
308 DECL_ARTIFICIAL (decl) = 1;
309 TYPE_NAME (type) = decl;
310 }
311
312 registered_builtin_types = tree_cons (0, type, registered_builtin_types);
313 }
314
315
316 /* Return true if we are in the global binding level. */
317
318 static bool
319 brig_langhook_global_bindings_p (void)
320 {
321 return current_function_decl == NULL_TREE;
322 }
323
324 /* Push a declaration into the current binding level. From Go: We can't
325 usefully implement this since we don't want to convert from tree
326 back to one of our internal data structures. I think the only way
327 this is used is to record a decl which is to be returned by
328 getdecls, and we could implement it for that purpose if
329 necessary. */
330
331 static tree
332 brig_langhook_pushdecl (tree decl ATTRIBUTE_UNUSED)
333 {
334 gcc_unreachable ();
335 }
336
337 /* This hook is used to get the current list of declarations as trees.
338 From Go: We don't support that; instead we use the write_globals hook.
339 This can't simply crash because it is called by -gstabs. */
340
341 static tree
342 brig_langhook_getdecls (void)
343 {
344 return NULL;
345 }
346
347 static int
348 brig_langhook_gimplify_expr (tree *expr_p, gimple_seq *pre_p ATTRIBUTE_UNUSED,
349 gimple_seq *post_p ATTRIBUTE_UNUSED)
350 {
351
352 /* Strip off the static chain info that appears to function
353 calls for some strange reason even though we don't add
354 nested functions. Maybe something wrong with the function
355 declaration contexts? */
356 if (TREE_CODE (*expr_p) == CALL_EXPR
357 && CALL_EXPR_STATIC_CHAIN (*expr_p) != NULL_TREE)
358 CALL_EXPR_STATIC_CHAIN (*expr_p) = NULL_TREE;
359 return GS_UNHANDLED;
360 }
361
362 static tree
363 brig_langhook_eh_personality (void)
364 {
365 gcc_unreachable ();
366 }
367
368 /* Functions called directly by the generic backend.
369 Adapted from go-lang.c. */
370
371 tree
372 convert (tree type, tree expr)
373 {
374 if (type == error_mark_node || expr == error_mark_node
375 || TREE_TYPE (expr) == error_mark_node)
376 return error_mark_node;
377
378 if (type == TREE_TYPE (expr))
379 return expr;
380
381 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (TREE_TYPE (expr)))
382 return fold_convert (type, expr);
383
384 switch (TREE_CODE (type))
385 {
386 case VOID_TYPE:
387 case BOOLEAN_TYPE:
388 return fold_convert (type, expr);
389 case INTEGER_TYPE:
390 return fold (convert_to_integer (type, expr));
391 case REAL_TYPE:
392 return fold (convert_to_real (type, expr));
393 case VECTOR_TYPE:
394 return fold (convert_to_vector (type, expr));
395 case POINTER_TYPE:
396 return build1 (VIEW_CONVERT_EXPR, type, convert (size_type_node, expr));
397 default:
398 break;
399 }
400
401 gcc_unreachable ();
402 }
403
404 static GTY (()) tree brig_gc_root;
405
406 /* Preserve trees that we create from the garbage collector. */
407
408 void
409 brig_preserve_from_gc (tree t)
410 {
411 brig_gc_root = tree_cons (NULL_TREE, t, brig_gc_root);
412 }
413
414 /* Convert an identifier for use in an error message. */
415
416 const char *
417 brig_localize_identifier (const char *ident)
418 {
419 return identifier_to_locale (ident);
420 }
421
422 /* Built-in initialization code cribbed from lto-lang.c which cribbed it
423 from c-common.c. */
424
425
426 static GTY(()) tree built_in_attributes[(int) ATTR_LAST];
427
428
429 static GTY(()) tree builtin_types[(int) BT_LAST + 1];
430
431 static GTY(()) tree string_type_node;
432 static GTY(()) tree const_string_type_node;
433 static GTY(()) tree wint_type_node;
434 static GTY(()) tree intmax_type_node;
435 static GTY(()) tree uintmax_type_node;
436 static GTY(()) tree signed_size_type_node;
437
438 /* Flags needed to process builtins.def. */
439 int flag_isoc94;
440 int flag_isoc99;
441 int flag_isoc11;
442
443 static void
444 def_fn_type (builtin_type def, builtin_type ret, bool var, int n, ...)
445 {
446 tree t;
447 tree *args = XALLOCAVEC (tree, n);
448 va_list list;
449 int i;
450 bool err = false;
451
452 va_start (list, n);
453 for (i = 0; i < n; ++i)
454 {
455 builtin_type a = (builtin_type) va_arg (list, int);
456 t = builtin_types[a];
457 if (t == error_mark_node)
458 err = true;
459 args[i] = t;
460 }
461 va_end (list);
462
463 t = builtin_types[ret];
464 if (err)
465 t = error_mark_node;
466 if (t == error_mark_node)
467 ;
468 else if (var)
469 t = build_varargs_function_type_array (t, n, args);
470 else
471 t = build_function_type_array (t, n, args);
472
473 builtin_types[def] = t;
474 }
475
476 /* Used to help initialize the builtin-types.def table. When a type of
477 the correct size doesn't exist, use error_mark_node instead of NULL.
478 The later results in segfaults even when a decl using the type doesn't
479 get invoked. */
480
481 static tree
482 builtin_type_for_size (int size, bool unsignedp)
483 {
484 tree type = brig_langhook_type_for_size (size, unsignedp);
485 return type ? type : error_mark_node;
486 }
487
488 /* Support for DEF_BUILTIN. */
489
490 static void
491 def_builtin_1 (enum built_in_function fncode, const char *name,
492 enum built_in_class fnclass, tree fntype, tree libtype,
493 bool both_p, bool fallback_p, bool nonansi_p,
494 tree fnattrs, bool implicit_p)
495 {
496 tree decl;
497 const char *libname;
498
499 if (fntype == error_mark_node)
500 return;
501
502 libname = name + strlen ("__builtin_");
503 decl = add_builtin_function (name, fntype, fncode, fnclass,
504 (fallback_p ? libname : NULL),
505 fnattrs);
506
507 if (both_p
508 && !flag_no_builtin
509 && !(nonansi_p && flag_no_nonansi_builtin))
510 add_builtin_function (libname, libtype, fncode, fnclass,
511 NULL, fnattrs);
512
513 set_builtin_decl (fncode, decl, implicit_p);
514 }
515
516
517 /* Initialize the attribute table for all the supported builtins. */
518
519 static void
520 brig_init_attributes (void)
521 {
522 /* Fill in the built_in_attributes array. */
523 #define DEF_ATTR_NULL_TREE(ENUM) \
524 built_in_attributes[(int) ENUM] = NULL_TREE;
525 #define DEF_ATTR_INT(ENUM, VALUE) \
526 built_in_attributes[(int) ENUM] = build_int_cst (NULL_TREE, VALUE);
527 #define DEF_ATTR_STRING(ENUM, VALUE) \
528 built_in_attributes[(int) ENUM] = build_string (strlen (VALUE), VALUE);
529 #define DEF_ATTR_IDENT(ENUM, STRING) \
530 built_in_attributes[(int) ENUM] = get_identifier (STRING);
531 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) \
532 built_in_attributes[(int) ENUM] \
533 = tree_cons (built_in_attributes[(int) PURPOSE], \
534 built_in_attributes[(int) VALUE], \
535 built_in_attributes[(int) CHAIN]);
536 #include "builtin-attrs.def"
537 #undef DEF_ATTR_NULL_TREE
538 #undef DEF_ATTR_INT
539 #undef DEF_ATTR_STRING
540 #undef DEF_ATTR_IDENT
541 #undef DEF_ATTR_TREE_LIST
542 }
543
544 /* Create builtin types and functions. VA_LIST_REF_TYPE_NODE and
545 VA_LIST_ARG_TYPE_NODE are used in builtin-types.def. */
546
547 static void
548 brig_define_builtins (tree va_list_ref_type_node ATTRIBUTE_UNUSED,
549 tree va_list_arg_type_node ATTRIBUTE_UNUSED)
550 {
551 #define DEF_PRIMITIVE_TYPE(ENUM, VALUE) \
552 builtin_types[ENUM] = VALUE;
553 #define DEF_FUNCTION_TYPE_0(ENUM, RETURN) \
554 def_fn_type (ENUM, RETURN, 0, 0);
555 #define DEF_FUNCTION_TYPE_1(ENUM, RETURN, ARG1) \
556 def_fn_type (ENUM, RETURN, 0, 1, ARG1);
557 #define DEF_FUNCTION_TYPE_2(ENUM, RETURN, ARG1, ARG2) \
558 def_fn_type (ENUM, RETURN, 0, 2, ARG1, ARG2);
559 #define DEF_FUNCTION_TYPE_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
560 def_fn_type (ENUM, RETURN, 0, 3, ARG1, ARG2, ARG3);
561 #define DEF_FUNCTION_TYPE_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
562 def_fn_type (ENUM, RETURN, 0, 4, ARG1, ARG2, ARG3, ARG4);
563 #define DEF_FUNCTION_TYPE_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
564 def_fn_type (ENUM, RETURN, 0, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
565 #define DEF_FUNCTION_TYPE_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
566 ARG6) \
567 def_fn_type (ENUM, RETURN, 0, 6, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6);
568 #define DEF_FUNCTION_TYPE_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
569 ARG6, ARG7) \
570 def_fn_type (ENUM, RETURN, 0, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
571 #define DEF_FUNCTION_TYPE_8(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
572 ARG6, ARG7, ARG8) \
573 def_fn_type (ENUM, RETURN, 0, 8, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
574 ARG7, ARG8);
575 #define DEF_FUNCTION_TYPE_9(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
576 ARG6, ARG7, ARG8, ARG9) \
577 def_fn_type (ENUM, RETURN, 0, 9, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
578 ARG7, ARG8, ARG9);
579 #define DEF_FUNCTION_TYPE_10(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
580 ARG6, ARG7, ARG8, ARG9, ARG10) \
581 def_fn_type (ENUM, RETURN, 0, 10, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
582 ARG7, ARG8, ARG9, ARG10);
583 #define DEF_FUNCTION_TYPE_11(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
584 ARG6, ARG7, ARG8, ARG9, ARG10, ARG11) \
585 def_fn_type (ENUM, RETURN, 0, 11, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
586 ARG7, ARG8, ARG9, ARG10, ARG11);
587 #define DEF_FUNCTION_TYPE_VAR_0(ENUM, RETURN) \
588 def_fn_type (ENUM, RETURN, 1, 0);
589 #define DEF_FUNCTION_TYPE_VAR_1(ENUM, RETURN, ARG1) \
590 def_fn_type (ENUM, RETURN, 1, 1, ARG1);
591 #define DEF_FUNCTION_TYPE_VAR_2(ENUM, RETURN, ARG1, ARG2) \
592 def_fn_type (ENUM, RETURN, 1, 2, ARG1, ARG2);
593 #define DEF_FUNCTION_TYPE_VAR_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
594 def_fn_type (ENUM, RETURN, 1, 3, ARG1, ARG2, ARG3);
595 #define DEF_FUNCTION_TYPE_VAR_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
596 def_fn_type (ENUM, RETURN, 1, 4, ARG1, ARG2, ARG3, ARG4);
597 #define DEF_FUNCTION_TYPE_VAR_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
598 def_fn_type (ENUM, RETURN, 1, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
599 #define DEF_FUNCTION_TYPE_VAR_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
600 ARG6) \
601 def_fn_type (ENUM, RETURN, 1, 6, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6);
602 #define DEF_FUNCTION_TYPE_VAR_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
603 ARG6, ARG7) \
604 def_fn_type (ENUM, RETURN, 1, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
605 #define DEF_POINTER_TYPE(ENUM, TYPE) \
606 builtin_types[(int) ENUM] = build_pointer_type (builtin_types[(int) TYPE]);
607
608 #include "builtin-types.def"
609
610 #undef DEF_PRIMITIVE_TYPE
611 #undef DEF_FUNCTION_TYPE_0
612 #undef DEF_FUNCTION_TYPE_1
613 #undef DEF_FUNCTION_TYPE_2
614 #undef DEF_FUNCTION_TYPE_3
615 #undef DEF_FUNCTION_TYPE_4
616 #undef DEF_FUNCTION_TYPE_5
617 #undef DEF_FUNCTION_TYPE_6
618 #undef DEF_FUNCTION_TYPE_7
619 #undef DEF_FUNCTION_TYPE_8
620 #undef DEF_FUNCTION_TYPE_9
621 #undef DEF_FUNCTION_TYPE_10
622 #undef DEF_FUNCTION_TYPE_11
623 #undef DEF_FUNCTION_TYPE_VAR_0
624 #undef DEF_FUNCTION_TYPE_VAR_1
625 #undef DEF_FUNCTION_TYPE_VAR_2
626 #undef DEF_FUNCTION_TYPE_VAR_3
627 #undef DEF_FUNCTION_TYPE_VAR_4
628 #undef DEF_FUNCTION_TYPE_VAR_5
629 #undef DEF_FUNCTION_TYPE_VAR_6
630 #undef DEF_FUNCTION_TYPE_VAR_7
631 #undef DEF_POINTER_TYPE
632 builtin_types[(int) BT_LAST] = NULL_TREE;
633
634 brig_init_attributes ();
635
636 #define DEF_BUILTIN(ENUM, NAME, CLASS, TYPE, LIBTYPE, BOTH_P, FALLBACK_P,\
637 NONANSI_P, ATTRS, IMPLICIT, COND) \
638 if (NAME && COND) \
639 def_builtin_1 (ENUM, NAME, CLASS, builtin_types[(int) TYPE], \
640 builtin_types[(int) LIBTYPE], BOTH_P, FALLBACK_P, \
641 NONANSI_P, built_in_attributes[(int) ATTRS], IMPLICIT);
642
643 #undef DEF_HSAIL_BUILTIN
644 #define DEF_HSAIL_BUILTIN(ENUM, HSAIL_OPCODE, HSAIL_TYPE, NAME, TYPE, ATTRS) \
645 DEF_BUILTIN (ENUM, "__builtin_" NAME, BUILT_IN_NORMAL, TYPE, TYPE, \
646 false, true, true, ATTRS, false, true)
647
648 /* HSAIL atomic builtins do not have separate identifying opcodes. */
649
650 #undef DEF_HSAIL_ATOMIC_BUILTIN
651 #define DEF_HSAIL_ATOMIC_BUILTIN(ENUM, ATOMIC_OPCODE, HSAIL_TYPE, NAME, \
652 TYPE, ATTRS) \
653 DEF_BUILTIN (ENUM, "__builtin_" NAME, BUILT_IN_NORMAL, TYPE, TYPE, \
654 false, true, true, ATTRS, false, true)
655
656 /* HSAIL saturating arithmetics builtins. */
657
658 #undef DEF_HSAIL_SAT_BUILTIN
659 #define DEF_HSAIL_SAT_BUILTIN(ENUM, BRIG_OPCODE, HSAIL_TYPE, NAME, \
660 TYPE, ATTRS) \
661 DEF_BUILTIN (ENUM, "__builtin_" NAME, BUILT_IN_NORMAL, TYPE, TYPE, \
662 false, true, true, ATTRS, false, true)
663
664 /* HSAIL builtins used internally by the frontend. */
665
666 #undef DEF_HSAIL_INTR_BUILTIN
667 #define DEF_HSAIL_INTR_BUILTIN(ENUM, NAME, TYPE, ATTRS) \
668 DEF_BUILTIN (ENUM, "__builtin_" NAME, BUILT_IN_NORMAL, TYPE, TYPE, \
669 false, true, true, ATTRS, false, true)
670
671 /* HSAIL saturated conversions. */
672
673 #undef DEF_HSAIL_CVT_ZEROI_SAT_BUILTIN
674 #define DEF_HSAIL_CVT_ZEROI_SAT_BUILTIN(ENUM, HSAIL_DEST_TYPE, HSAIL_SRC_TYPE, \
675 NAME, TYPE, ATTRS) \
676 DEF_BUILTIN (ENUM, "__builtin_" NAME, BUILT_IN_NORMAL, TYPE, TYPE, \
677 false, true, true, ATTRS, false, true)
678
679 #include "builtins.def"
680 }
681
682 /* Build nodes that would have be created by the C front-end; necessary
683 for including builtin-types.def and ultimately builtins.def. Borrowed
684 from lto-lang.c. */
685
686 static void
687 brig_build_c_type_nodes (void)
688 {
689 gcc_assert (void_type_node);
690
691 void_list_node = build_tree_list (NULL_TREE, void_type_node);
692 string_type_node = build_pointer_type (char_type_node);
693 const_string_type_node
694 = build_pointer_type (build_qualified_type (char_type_node,
695 TYPE_QUAL_CONST));
696
697 if (strcmp (SIZE_TYPE, "unsigned int") == 0)
698 {
699 intmax_type_node = integer_type_node;
700 uintmax_type_node = unsigned_type_node;
701 signed_size_type_node = integer_type_node;
702 }
703 else if (strcmp (SIZE_TYPE, "long unsigned int") == 0)
704 {
705 intmax_type_node = long_integer_type_node;
706 uintmax_type_node = long_unsigned_type_node;
707 signed_size_type_node = long_integer_type_node;
708 }
709 else if (strcmp (SIZE_TYPE, "long long unsigned int") == 0)
710 {
711 intmax_type_node = long_long_integer_type_node;
712 uintmax_type_node = long_long_unsigned_type_node;
713 signed_size_type_node = long_long_integer_type_node;
714 }
715 else
716 {
717 int i;
718
719 signed_size_type_node = NULL_TREE;
720 for (i = 0; i < NUM_INT_N_ENTS; i++)
721 if (int_n_enabled_p[i])
722 {
723 char name[50];
724 sprintf (name, "__int%d unsigned", int_n_data[i].bitsize);
725
726 if (strcmp (name, SIZE_TYPE) == 0)
727 {
728 intmax_type_node = int_n_trees[i].signed_type;
729 uintmax_type_node = int_n_trees[i].unsigned_type;
730 signed_size_type_node = int_n_trees[i].signed_type;
731 }
732 }
733 if (signed_size_type_node == NULL_TREE)
734 gcc_unreachable ();
735 }
736
737 wint_type_node = unsigned_type_node;
738 pid_type_node = integer_type_node;
739 }
740
741
742 static bool
743 brig_langhook_init (void)
744 {
745 build_common_tree_nodes (false);
746
747 /* Builtin initialization related code borrowed from lto-lang.c. */
748 void_list_node = build_tree_list (NULL_TREE, void_type_node);
749
750 brig_build_c_type_nodes ();
751
752 if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
753 {
754 tree x = build_pointer_type (TREE_TYPE (va_list_type_node));
755 brig_define_builtins (x, x);
756 }
757 else
758 {
759 brig_define_builtins (build_reference_type (va_list_type_node),
760 va_list_type_node);
761 }
762
763 targetm.init_builtins ();
764 build_common_builtin_nodes ();
765
766 return true;
767 }
768
769 #undef LANG_HOOKS_NAME
770 #undef LANG_HOOKS_INIT
771 #undef LANG_HOOKS_OPTION_LANG_MASK
772 #undef LANG_HOOKS_INIT_OPTIONS_STRUCT
773 #undef LANG_HOOKS_HANDLE_OPTION
774 #undef LANG_HOOKS_POST_OPTIONS
775 #undef LANG_HOOKS_PARSE_FILE
776 #undef LANG_HOOKS_TYPE_FOR_MODE
777 #undef LANG_HOOKS_TYPE_FOR_SIZE
778 #undef LANG_HOOKS_REGISTER_BUILTIN_TYPE
779 #undef LANG_HOOKS_BUILTIN_FUNCTION
780 #undef LANG_HOOKS_GLOBAL_BINDINGS_P
781 #undef LANG_HOOKS_PUSHDECL
782 #undef LANG_HOOKS_GETDECLS
783 #undef LANG_HOOKS_WRITE_GLOBALS
784 #undef LANG_HOOKS_GIMPLIFY_EXPR
785 #undef LANG_HOOKS_EH_PERSONALITY
786
787 #define LANG_HOOKS_NAME "GNU Brig"
788 #define LANG_HOOKS_INIT brig_langhook_init
789 #define LANG_HOOKS_OPTION_LANG_MASK brig_langhook_option_lang_mask
790 #define LANG_HOOKS_INIT_OPTIONS_STRUCT brig_langhook_init_options_struct
791 #define LANG_HOOKS_HANDLE_OPTION brig_langhook_handle_option
792 #define LANG_HOOKS_POST_OPTIONS brig_langhook_post_options
793 #define LANG_HOOKS_PARSE_FILE brig_langhook_parse_file
794 #define LANG_HOOKS_TYPE_FOR_MODE brig_langhook_type_for_mode
795 #define LANG_HOOKS_TYPE_FOR_SIZE brig_langhook_type_for_size
796 #define LANG_HOOKS_REGISTER_BUILTIN_TYPE brig_langhook_register_builtin_type
797 #define LANG_HOOKS_BUILTIN_FUNCTION brig_langhook_builtin_function
798 #define LANG_HOOKS_GLOBAL_BINDINGS_P brig_langhook_global_bindings_p
799 #define LANG_HOOKS_PUSHDECL brig_langhook_pushdecl
800 #define LANG_HOOKS_GETDECLS brig_langhook_getdecls
801 #define LANG_HOOKS_GIMPLIFY_EXPR brig_langhook_gimplify_expr
802 #define LANG_HOOKS_EH_PERSONALITY brig_langhook_eh_personality
803
804 struct lang_hooks lang_hooks = LANG_HOOKS_INITIALIZER;
805
806 #include "gt-brig-brig-lang.h"
807 #include "gtype-brig.h"