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