Debian Codegraph @

gcc/jit/dummy-frontend.cc

raw
1 /* jit.c -- Dummy "frontend" for use during JIT-compilation.
2 Copyright (C) 2013-2025 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
10
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
19
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "target.h"
24 #include "jit-playback.h"
25 #include "stor-layout.h"
26 #include "debug.h"
27 #include "langhooks.h"
28 #include "langhooks-def.h"
29 #include "diagnostic.h"
30 #include "options.h"
31 #include "stringpool.h"
32 #include "attribs.h"
33 #include "cgraph.h"
34 #include "target.h"
35 #include "diagnostic-format-text.h"
36 #include "make-unique.h"
37 #include "print-tree.h"
38
39 #include <mpfr.h>
40 #include <unordered_map>
41
42 using namespace gcc::jit;
43
44 /* Attribute handling. */
45
46 static tree handle_alias_attribute (tree *, tree, tree, int, bool *);
47 static tree handle_always_inline_attribute (tree *, tree, tree, int,
48 bool *);
49 static tree handle_cold_attribute (tree *, tree, tree, int, bool *);
50 static tree handle_const_attribute (tree *, tree, tree, int, bool *);
51 static tree handle_fnspec_attribute (tree *, tree, tree, int, bool *);
52 static tree handle_format_arg_attribute (tree *, tree, tree, int, bool *);
53 static tree handle_format_attribute (tree *, tree, tree, int, bool *);
54 static tree handle_leaf_attribute (tree *, tree, tree, int, bool *);
55 static tree handle_malloc_attribute (tree *, tree, tree, int, bool *);
56 static tree handle_noinline_attribute (tree *, tree, tree, int, bool *);
57 static tree handle_nonnull_attribute (tree *, tree, tree, int, bool *);
58 static tree handle_noreturn_attribute (tree *, tree, tree, int, bool *);
59 static tree handle_nothrow_attribute (tree *, tree, tree, int, bool *);
60 static tree handle_novops_attribute (tree *, tree, tree, int, bool *);
61 static tree handle_patchable_function_entry_attribute (tree *, tree, tree,
62 int, bool *);
63 static tree handle_pure_attribute (tree *, tree, tree, int, bool *);
64 static tree handle_returns_twice_attribute (tree *, tree, tree, int, bool *);
65 static tree handle_sentinel_attribute (tree *, tree, tree, int, bool *);
66 static tree handle_target_attribute (tree *, tree, tree, int, bool *);
67 static tree handle_transaction_pure_attribute (tree *, tree, tree, int, bool *);
68 static tree handle_type_generic_attribute (tree *, tree, tree, int, bool *);
69 static tree handle_used_attribute (tree *, tree, tree, int, bool *);
70 static tree handle_visibility_attribute (tree *, tree, tree, int,
71 bool *);
72 static tree handle_weak_attribute (tree *, tree, tree, int, bool *) ;
73
74 static tree ignore_attribute (tree *, tree, tree, int, bool *);
75
76 /* Helper to define attribute exclusions. */
77 #define ATTR_EXCL(name, function, type, variable) \
78 { name, function, type, variable }
79
80 /* Define attributes that are mutually exclusive with one another. */
81 static const struct attribute_spec::exclusions attr_noreturn_exclusions[] =
82 {
83 ATTR_EXCL ("alloc_align", true, true, true),
84 ATTR_EXCL ("alloc_size", true, true, true),
85 ATTR_EXCL ("const", true, true, true),
86 ATTR_EXCL ("malloc", true, true, true),
87 ATTR_EXCL ("pure", true, true, true),
88 ATTR_EXCL ("returns_twice", true, true, true),
89 ATTR_EXCL ("warn_unused_result", true, true, true),
90 ATTR_EXCL (NULL, false, false, false),
91 };
92
93 static const struct attribute_spec::exclusions attr_returns_twice_exclusions[] =
94 {
95 ATTR_EXCL ("noreturn", true, true, true),
96 ATTR_EXCL (NULL, false, false, false),
97 };
98
99 /* Exclusions that apply to attribute alloc_align, alloc_size, and malloc. */
100 static const struct attribute_spec::exclusions attr_alloc_exclusions[] =
101 {
102 ATTR_EXCL ("const", true, true, true),
103 ATTR_EXCL ("noreturn", true, true, true),
104 ATTR_EXCL ("pure", true, true, true),
105 ATTR_EXCL (NULL, false, false, false),
106 };
107
108 static const struct attribute_spec::exclusions attr_const_pure_exclusions[] =
109 {
110 ATTR_EXCL ("const", true, true, true),
111 ATTR_EXCL ("alloc_align", true, true, true),
112 ATTR_EXCL ("alloc_size", true, true, true),
113 ATTR_EXCL ("malloc", true, true, true),
114 ATTR_EXCL ("noreturn", true, true, true),
115 ATTR_EXCL ("pure", true, true, true),
116 ATTR_EXCL (NULL, false, false, false)
117 };
118
119 static const struct attribute_spec::exclusions attr_always_inline_exclusions[] =
120 {
121 ATTR_EXCL ("noinline", true, true, true),
122 ATTR_EXCL ("target_clones", true, true, true),
123 ATTR_EXCL (NULL, false, false, false),
124 };
125
126 extern const struct attribute_spec::exclusions attr_cold_hot_exclusions[] =
127 {
128 ATTR_EXCL ("cold", true, true, true),
129 ATTR_EXCL ("hot", true, true, true),
130 ATTR_EXCL (NULL, false, false, false)
131 };
132
133 static const struct attribute_spec::exclusions attr_noinline_exclusions[] =
134 {
135 ATTR_EXCL ("always_inline", true, true, true),
136 ATTR_EXCL ("gnu_inline", true, true, true),
137 ATTR_EXCL (NULL, false, false, false),
138 };
139
140 static const struct attribute_spec::exclusions attr_target_exclusions[] =
141 {
142 ATTR_EXCL ("target_clones", TARGET_HAS_FMV_TARGET_ATTRIBUTE,
143 TARGET_HAS_FMV_TARGET_ATTRIBUTE, TARGET_HAS_FMV_TARGET_ATTRIBUTE),
144 ATTR_EXCL (NULL, false, false, false),
145 };
146
147 /* These variables act as a cache for the target builtins. This is needed in
148 order to be able to type-check the calls since we can only get those types
149 in the playback phase while we need them in the recording phase. */
150 hash_map<nofree_string_hash, tree> target_builtins{};
151 std::unordered_map<std::string, recording::function_type*> target_function_types
152 {};
153 recording::context target_builtins_ctxt{NULL};
154
155 /* Table of machine-independent attributes supported in libgccjit. */
156 static const attribute_spec jit_gnu_attributes[] =
157 {
158 /* { name, min_len, max_len, decl_req, type_req, fn_type_req,
159 affects_type_identity, handler, exclude } */
160 { "alias", 1, 1, true, false, false, false,
161 handle_alias_attribute, NULL },
162 { "always_inline", 0, 0, true, false, false, false,
163 handle_always_inline_attribute,
164 attr_always_inline_exclusions },
165 { "cold", 0, 0, true, false, false, false,
166 handle_cold_attribute,
167 attr_cold_hot_exclusions },
168 /* The same comments as for noreturn attributes apply to const ones. */
169 { "const", 0, 0, true, false, false, false,
170 handle_const_attribute,
171 attr_const_pure_exclusions },
172 { "fn spec", 1, 1, false, true, true, false,
173 handle_fnspec_attribute, NULL },
174
175 { "leaf", 0, 0, true, false, false, false,
176 handle_leaf_attribute, NULL },
177 { "malloc", 0, 0, true, false, false, false,
178 handle_malloc_attribute, attr_alloc_exclusions },
179 { "noreturn", 0, 0, true, false, false, false,
180 handle_noreturn_attribute,
181 attr_noreturn_exclusions },
182 { "no vops", 0, 0, true, false, false, false,
183 handle_novops_attribute, NULL },
184 { "noinline", 0, 0, true, false, false, false,
185 handle_noinline_attribute,
186 attr_noinline_exclusions },
187 { "nonnull", 0, -1, false, true, true, false,
188 handle_nonnull_attribute, NULL },
189 { "nothrow", 0, 0, true, false, false, false,
190 handle_nothrow_attribute, NULL },
191 { "patchable_function_entry", 1, 2, true, false, false, false,
192 handle_patchable_function_entry_attribute,
193 NULL },
194 { "pure", 0, 0, true, false, false, false,
195 handle_pure_attribute,
196 attr_const_pure_exclusions },
197 { "returns_twice", 0, 0, true, false, false, false,
198 handle_returns_twice_attribute,
199 attr_returns_twice_exclusions },
200 { "sentinel", 0, 1, false, true, true, false,
201 handle_sentinel_attribute, NULL },
202 { "target", 1, -1, true, false, false, false,
203 handle_target_attribute, attr_target_exclusions },
204 { "type generic", 0, 0, false, true, true, false,
205 handle_type_generic_attribute, NULL },
206 { "transaction_pure", 0, 0, false, true, true, false,
207 handle_transaction_pure_attribute, NULL },
208 { "used", 0, 0, true, false, false, false,
209 handle_used_attribute, NULL },
210 { "visibility", 1, 1, false, false, false, false,
211 handle_visibility_attribute, NULL },
212 { "weak", 0, 0, true, false, false, false,
213 handle_weak_attribute, NULL },
214 /* For internal use only. The leading '*' both prevents its usage in
215 source code and signals that it may be overridden by machine tables. */
216 { "*tm regparm", 0, 0, false, true, true, false,
217 ignore_attribute, NULL },
218 };
219
220 static const scoped_attribute_specs jit_gnu_attribute_table =
221 {
222 "gnu", { jit_gnu_attributes }
223 };
224
225 /* Give the specifications for the format attributes, used by C and all
226 descendants. */
227
228 static const attribute_spec jit_format_attributes[] =
229 {
230 /* { name, min_len, max_len, decl_req, type_req, fn_type_req,
231 affects_type_identity, handler, exclude } */
232 { "format", 3, 3, false, true, true, false,
233 handle_format_attribute, NULL },
234 { "format_arg", 1, 1, false, true, true, false,
235 handle_format_arg_attribute, NULL }
236 };
237
238 static const scoped_attribute_specs jit_format_attribute_table =
239 {
240 "gnu", { jit_format_attributes }
241 };
242
243 static const scoped_attribute_specs *const jit_attribute_table[] =
244 {
245 &jit_gnu_attribute_table,
246 &jit_format_attribute_table
247 };
248
249 /* Attribute handlers. */
250
251 /* Handle a "noreturn" attribute; arguments as in
252 struct attribute_spec.handler. */
253
254 static tree
255 handle_noreturn_attribute (tree *node, tree ARG_UNUSED (name),
256 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
257 bool * ARG_UNUSED (no_add_attrs))
258 {
259 tree type = TREE_TYPE (*node);
260
261 if (TREE_CODE (*node) == FUNCTION_DECL)
262 TREE_THIS_VOLATILE (*node) = 1;
263 else if (TREE_CODE (type) == POINTER_TYPE
264 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
265 TREE_TYPE (*node)
266 = build_pointer_type
267 (build_type_variant (TREE_TYPE (type),
268 TYPE_READONLY (TREE_TYPE (type)), 1));
269 else
270 gcc_unreachable ();
271
272 return NULL_TREE;
273 }
274
275 /* Handle a "leaf" attribute; arguments as in
276 struct attribute_spec.handler. */
277
278 static tree
279 handle_leaf_attribute (tree *node, tree name,
280 tree ARG_UNUSED (args),
281 int ARG_UNUSED (flags), bool *no_add_attrs)
282 {
283 if (TREE_CODE (*node) != FUNCTION_DECL)
284 {
285 warning (OPT_Wattributes, "%qE attribute ignored", name);
286 *no_add_attrs = true;
287 }
288 if (!TREE_PUBLIC (*node))
289 {
290 warning (OPT_Wattributes, "%qE attribute has no effect on unit local functions", name);
291 *no_add_attrs = true;
292 }
293
294 return NULL_TREE;
295 }
296
297 /* Handle a "const" attribute; arguments as in
298 struct attribute_spec.handler. */
299
300 static tree
301 handle_const_attribute (tree *node, tree name, tree ARG_UNUSED (args),
302 int ARG_UNUSED (flags), bool *no_add_attrs)
303 {
304 tree type = TREE_TYPE (*node);
305
306 /* See FIXME comment on noreturn in c_common_attribute_table. */
307 if (TREE_CODE (*node) == FUNCTION_DECL)
308 TREE_READONLY (*node) = 1;
309 else if (TREE_CODE (type) == POINTER_TYPE
310 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
311 TREE_TYPE (*node)
312 = (build_qualified_type
313 (build_pointer_type
314 (build_type_variant (TREE_TYPE (type), 1,
315 TREE_THIS_VOLATILE (TREE_TYPE (type)))),
316 TYPE_QUALS (type)));
317 else
318 {
319 warning (OPT_Wattributes, "%qE attribute ignored", name);
320 *no_add_attrs = true;
321 }
322
323 return NULL_TREE;
324 }
325
326
327 /* Handle a "malloc" attribute; arguments as in
328 struct attribute_spec.handler. */
329
330 static tree
331 handle_malloc_attribute (tree *node, tree ARG_UNUSED (name),
332 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
333 bool * ARG_UNUSED (no_add_attrs))
334 {
335 if (TREE_CODE (*node) == FUNCTION_DECL
336 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (*node))))
337 DECL_IS_MALLOC (*node) = 1;
338 else
339 gcc_unreachable ();
340
341 return NULL_TREE;
342 }
343
344
345 /* Handle a "pure" attribute; arguments as in
346 struct attribute_spec.handler. */
347
348 static tree
349 handle_pure_attribute (tree *node, tree ARG_UNUSED (name),
350 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
351 bool * ARG_UNUSED (no_add_attrs))
352 {
353 if (TREE_CODE (*node) == FUNCTION_DECL)
354 DECL_PURE_P (*node) = 1;
355 else
356 gcc_unreachable ();
357
358 return NULL_TREE;
359 }
360
361
362 /* Handle a "no vops" attribute; arguments as in
363 struct attribute_spec.handler. */
364
365 static tree
366 handle_novops_attribute (tree *node, tree ARG_UNUSED (name),
367 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
368 bool *ARG_UNUSED (no_add_attrs))
369 {
370 gcc_assert (TREE_CODE (*node) == FUNCTION_DECL);
371 DECL_IS_NOVOPS (*node) = 1;
372 return NULL_TREE;
373 }
374
375
376 /* Helper for nonnull attribute handling; fetch the operand number
377 from the attribute argument list. */
378
379 static bool
380 get_nonnull_operand (tree arg_num_expr, unsigned HOST_WIDE_INT *valp)
381 {
382 /* Verify the arg number is a constant. */
383 if (!tree_fits_uhwi_p (arg_num_expr))
384 return false;
385
386 *valp = TREE_INT_CST_LOW (arg_num_expr);
387 return true;
388 }
389
390 /* Handle the "nonnull" attribute. */
391
392 static tree
393 handle_nonnull_attribute (tree *node, tree ARG_UNUSED (name),
394 tree args, int ARG_UNUSED (flags),
395 bool * ARG_UNUSED (no_add_attrs))
396 {
397 tree type = *node;
398
399 /* If no arguments are specified, all pointer arguments should be
400 non-null. Verify a full prototype is given so that the arguments
401 will have the correct types when we actually check them later.
402 Avoid diagnosing type-generic built-ins since those have no
403 prototype. */
404 if (!args)
405 {
406 gcc_assert (prototype_p (type)
407 || !TYPE_ATTRIBUTES (type)
408 || lookup_attribute ("type generic", TYPE_ATTRIBUTES (type)));
409
410 return NULL_TREE;
411 }
412
413 /* Argument list specified. Verify that each argument number references
414 a pointer argument. */
415 for (; args; args = TREE_CHAIN (args))
416 {
417 tree argument;
418 unsigned HOST_WIDE_INT arg_num = 0, ck_num;
419
420 if (!get_nonnull_operand (TREE_VALUE (args), &arg_num))
421 gcc_unreachable ();
422
423 argument = TYPE_ARG_TYPES (type);
424 if (argument)
425 {
426 for (ck_num = 1; ; ck_num++)
427 {
428 if (!argument || ck_num == arg_num)
429 break;
430 argument = TREE_CHAIN (argument);
431 }
432
433 gcc_assert (argument
434 && TREE_CODE (TREE_VALUE (argument)) == POINTER_TYPE);
435 }
436 }
437
438 return NULL_TREE;
439 }
440
441
442 /* Handle a "nothrow" attribute; arguments as in
443 struct attribute_spec.handler. */
444
445 static tree
446 handle_nothrow_attribute (tree *node, tree ARG_UNUSED (name),
447 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
448 bool * ARG_UNUSED (no_add_attrs))
449 {
450 if (TREE_CODE (*node) == FUNCTION_DECL)
451 TREE_NOTHROW (*node) = 1;
452 else
453 gcc_unreachable ();
454
455 return NULL_TREE;
456 }
457
458
459 /* Handle a "sentinel" attribute. */
460
461 static tree
462 handle_sentinel_attribute (tree *node, tree ARG_UNUSED (name), tree args,
463 int ARG_UNUSED (flags),
464 bool * ARG_UNUSED (no_add_attrs))
465 {
466 gcc_assert (stdarg_p (*node));
467
468 if (args)
469 {
470 tree position = TREE_VALUE (args);
471 gcc_assert (TREE_CODE (position) == INTEGER_CST);
472 if (tree_int_cst_lt (position, integer_zero_node))
473 gcc_unreachable ();
474 }
475
476 return NULL_TREE;
477 }
478
479 /* Handle a "type_generic" attribute. */
480
481 static tree
482 handle_type_generic_attribute (tree *node, tree ARG_UNUSED (name),
483 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
484 bool * ARG_UNUSED (no_add_attrs))
485 {
486 /* Ensure we have a function type. */
487 gcc_assert (TREE_CODE (*node) == FUNCTION_TYPE);
488
489 /* Ensure we have a variadic function. */
490 gcc_assert (!prototype_p (*node) || stdarg_p (*node));
491
492 return NULL_TREE;
493 }
494
495 /* Handle a "transaction_pure" attribute. */
496
497 static tree
498 handle_transaction_pure_attribute (tree *node, tree ARG_UNUSED (name),
499 tree ARG_UNUSED (args),
500 int ARG_UNUSED (flags),
501 bool * ARG_UNUSED (no_add_attrs))
502 {
503 /* Ensure we have a function type. */
504 gcc_assert (TREE_CODE (*node) == FUNCTION_TYPE);
505
506 return NULL_TREE;
507 }
508
509 /* Handle a "returns_twice" attribute. */
510
511 static tree
512 handle_returns_twice_attribute (tree *node, tree ARG_UNUSED (name),
513 tree ARG_UNUSED (args),
514 int ARG_UNUSED (flags),
515 bool * ARG_UNUSED (no_add_attrs))
516 {
517 gcc_assert (TREE_CODE (*node) == FUNCTION_DECL);
518
519 DECL_IS_RETURNS_TWICE (*node) = 1;
520
521 return NULL_TREE;
522 }
523
524 static tree
525 handle_patchable_function_entry_attribute (tree *, tree, tree, int, bool *)
526 {
527 /* Nothing to be done here. */
528 return NULL_TREE;
529 }
530
531 /* Ignore the given attribute. Used when this attribute may be usefully
532 overridden by the target, but is not used generically. */
533
534 static tree
535 ignore_attribute (tree * ARG_UNUSED (node), tree ARG_UNUSED (name),
536 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
537 bool *no_add_attrs)
538 {
539 *no_add_attrs = true;
540 return NULL_TREE;
541 }
542
543 /* Handle a "format" attribute; arguments as in
544 struct attribute_spec.handler. */
545
546 static tree
547 handle_format_attribute (tree * ARG_UNUSED (node), tree ARG_UNUSED (name),
548 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
549 bool *no_add_attrs)
550 {
551 *no_add_attrs = true;
552 return NULL_TREE;
553 }
554
555
556 /* Handle a "format_arg" attribute; arguments as in
557 struct attribute_spec.handler. */
558
559 tree
560 handle_format_arg_attribute (tree * ARG_UNUSED (node), tree ARG_UNUSED (name),
561 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
562 bool *no_add_attrs)
563 {
564 *no_add_attrs = true;
565 return NULL_TREE;
566 }
567
568
569 /* Handle a "fn spec" attribute; arguments as in
570 struct attribute_spec.handler. */
571
572 static tree
573 handle_fnspec_attribute (tree *node ATTRIBUTE_UNUSED, tree ARG_UNUSED (name),
574 tree args, int ARG_UNUSED (flags),
575 bool *no_add_attrs ATTRIBUTE_UNUSED)
576 {
577 gcc_assert (args
578 && TREE_CODE (TREE_VALUE (args)) == STRING_CST
579 && !TREE_CHAIN (args));
580 return NULL_TREE;
581 }
582
583 /* Handle an "visibility" attribute; arguments as in
584 struct attribute_spec.handler. */
585
586 static tree
587 handle_visibility_attribute (tree *node, tree name, tree args,
588 int ARG_UNUSED (flags),
589 bool *ARG_UNUSED (no_add_attrs))
590 {
591 tree decl = *node;
592 tree id = TREE_VALUE (args);
593 enum symbol_visibility vis;
594
595 if (TYPE_P (*node))
596 {
597 if (TREE_CODE (*node) == ENUMERAL_TYPE)
598 /* OK. */;
599 else if (!RECORD_OR_UNION_TYPE_P (*node))
600 {
601 warning (OPT_Wattributes, "%qE attribute ignored on non-class types",
602 name);
603 return NULL_TREE;
604 }
605 else if (TYPE_FIELDS (*node))
606 {
607 error ("%qE attribute ignored because %qT is already defined",
608 name, *node);
609 return NULL_TREE;
610 }
611 }
612 else if (decl_function_context (decl) != 0 || !TREE_PUBLIC (decl))
613 {
614 warning (OPT_Wattributes, "%qE attribute ignored", name);
615 return NULL_TREE;
616 }
617
618 if (TREE_CODE (id) != STRING_CST)
619 {
620 error ("visibility argument not a string");
621 return NULL_TREE;
622 }
623
624 /* If this is a type, set the visibility on the type decl. */
625 if (TYPE_P (decl))
626 {
627 decl = TYPE_NAME (decl);
628 if (!decl)
629 return NULL_TREE;
630 if (TREE_CODE (decl) == IDENTIFIER_NODE)
631 {
632 warning (OPT_Wattributes, "%qE attribute ignored on types",
633 name);
634 return NULL_TREE;
635 }
636 }
637
638 if (strcmp (TREE_STRING_POINTER (id), "default") == 0)
639 vis = VISIBILITY_DEFAULT;
640 else if (strcmp (TREE_STRING_POINTER (id), "internal") == 0)
641 vis = VISIBILITY_INTERNAL;
642 else if (strcmp (TREE_STRING_POINTER (id), "hidden") == 0)
643 vis = VISIBILITY_HIDDEN;
644 else if (strcmp (TREE_STRING_POINTER (id), "protected") == 0)
645 vis = VISIBILITY_PROTECTED;
646 else
647 {
648 error ("attribute %qE argument must be one of %qs, %qs, %qs, or %qs",
649 name, "default", "hidden", "protected", "internal");
650 vis = VISIBILITY_DEFAULT;
651 }
652
653 if (DECL_VISIBILITY_SPECIFIED (decl)
654 && vis != DECL_VISIBILITY (decl))
655 {
656 tree attributes = (TYPE_P (*node)
657 ? TYPE_ATTRIBUTES (*node)
658 : DECL_ATTRIBUTES (decl));
659 if (lookup_attribute ("visibility", attributes))
660 error ("%qD redeclared with different visibility", decl);
661 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
662 && lookup_attribute ("dllimport", attributes))
663 error ("%qD was declared %qs which implies default visibility",
664 decl, "dllimport");
665 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
666 && lookup_attribute ("dllexport", attributes))
667 error ("%qD was declared %qs which implies default visibility",
668 decl, "dllexport");
669 }
670
671 DECL_VISIBILITY (decl) = vis;
672 DECL_VISIBILITY_SPECIFIED (decl) = 1;
673
674 /* Go ahead and attach the attribute to the node as well. This is needed
675 so we can determine whether we have VISIBILITY_DEFAULT because the
676 visibility was not specified, or because it was explicitly overridden
677 from the containing scope. */
678
679 return NULL_TREE;
680 }
681
682 /* Handle a "always_inline" attribute; arguments as in
683 struct attribute_spec.handler. */
684
685 static tree
686 handle_always_inline_attribute (tree *node, tree name,
687 tree ARG_UNUSED (args),
688 int ARG_UNUSED (flags),
689 bool *no_add_attrs)
690 {
691 if (TREE_CODE (*node) == FUNCTION_DECL)
692 {
693 /* Set the attribute and mark it for disregarding inline
694 limits. */
695 DECL_DISREGARD_INLINE_LIMITS (*node) = 1;
696 }
697 else
698 {
699 warning (OPT_Wattributes, "%qE attribute ignored", name);
700 *no_add_attrs = true;
701 }
702
703 return NULL_TREE;
704 }
705
706 /* Handle a "cold" and attribute; arguments as in
707 struct attribute_spec.handler. */
708
709 static tree
710 handle_cold_attribute (tree *node, tree name, tree ARG_UNUSED (args),
711 int ARG_UNUSED (flags), bool *no_add_attrs)
712 {
713 if (TREE_CODE (*node) == FUNCTION_DECL
714 || TREE_CODE (*node) == LABEL_DECL)
715 {
716 /* Attribute cold processing is done later with lookup_attribute. */
717 }
718 else
719 {
720 warning (OPT_Wattributes, "%qE attribute ignored", name);
721 *no_add_attrs = true;
722 }
723
724 return NULL_TREE;
725 }
726
727 /* Handle a "noinline" attribute; arguments as in
728 struct attribute_spec.handler. */
729
730 static tree
731 handle_noinline_attribute (tree *node, tree name,
732 tree ARG_UNUSED (args),
733 int ARG_UNUSED (flags), bool *no_add_attrs)
734 {
735 if (TREE_CODE (*node) == FUNCTION_DECL)
736 DECL_UNINLINABLE (*node) = 1;
737 else
738 {
739 warning (OPT_Wattributes, "%qE attribute ignored", name);
740 *no_add_attrs = true;
741 }
742
743 return NULL_TREE;
744 }
745
746 /* Handle a "weak" attribute; arguments as in
747 struct attribute_spec.handler. */
748
749 static tree
750 handle_weak_attribute (tree *node, tree name,
751 tree ARG_UNUSED (args),
752 int ARG_UNUSED (flags),
753 bool * ARG_UNUSED (no_add_attrs))
754 {
755 if (TREE_CODE (*node) == FUNCTION_DECL
756 && DECL_DECLARED_INLINE_P (*node))
757 {
758 warning (OPT_Wattributes, "inline function %q+D declared weak", *node);
759 *no_add_attrs = true;
760 }
761 else if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
762 {
763 error ("indirect function %q+D cannot be declared weak", *node);
764 *no_add_attrs = true;
765 return NULL_TREE;
766 }
767 else if (VAR_OR_FUNCTION_DECL_P (*node))
768 declare_weak (*node);
769 else
770 warning (OPT_Wattributes, "%qE attribute ignored", name);
771
772 return NULL_TREE;
773 }
774
775 /* Handle a "target" attribute. */
776
777 static tree
778 handle_target_attribute (tree *node, tree name, tree args, int flags,
779 bool *no_add_attrs)
780 {
781 /* Ensure we have a function declaration. */
782 if (TREE_CODE (*node) != FUNCTION_DECL)
783 {
784 warning (OPT_Wattributes, "%qE attribute ignored", name);
785 *no_add_attrs = true;
786 }
787 else if (! targetm.target_option.valid_attribute_p (*node, name, args,
788 flags))
789 *no_add_attrs = true;
790
791 /* Check that there's no empty string in values of the attribute. */
792 for (tree t = args; t != NULL_TREE; t = TREE_CHAIN (t))
793 {
794 tree value = TREE_VALUE (t);
795 if (TREE_CODE (value) == STRING_CST
796 && TREE_STRING_LENGTH (value) == 1
797 && TREE_STRING_POINTER (value)[0] == '\0')
798 {
799 warning (OPT_Wattributes, "empty string in attribute %<target%>");
800 *no_add_attrs = true;
801 }
802 }
803
804 return NULL_TREE;
805 }
806
807 /* Handle a "used" attribute; arguments as in
808 struct attribute_spec.handler. */
809
810 static tree
811 handle_used_attribute (tree *pnode, tree name, tree ARG_UNUSED (args),
812 int ARG_UNUSED (flags), bool *no_add_attrs)
813 {
814 tree node = *pnode;
815
816 if (TREE_CODE (node) == FUNCTION_DECL
817 || (VAR_P (node) && TREE_STATIC (node))
818 || (TREE_CODE (node) == TYPE_DECL))
819 {
820 TREE_USED (node) = 1;
821 DECL_PRESERVE_P (node) = 1;
822 if (VAR_P (node))
823 DECL_READ_P (node) = 1;
824 }
825 else
826 {
827 warning (OPT_Wattributes, "%qE attribute ignored", name);
828 *no_add_attrs = true;
829 }
830
831 return NULL_TREE;
832 }
833
834 /* Handle an "alias" or "ifunc" attribute; arguments as in
835 struct attribute_spec.handler, except that IS_ALIAS tells us
836 whether this is an alias as opposed to ifunc attribute. */
837
838 static tree
839 handle_alias_ifunc_attribute (bool is_alias, tree *node, tree name, tree args,
840 bool *no_add_attrs)
841 {
842 tree decl = *node;
843
844 if (TREE_CODE (decl) != FUNCTION_DECL
845 && (!is_alias || !VAR_P (decl)))
846 {
847 warning (OPT_Wattributes, "%qE attribute ignored", name);
848 *no_add_attrs = true;
849 }
850 else if ((TREE_CODE (decl) == FUNCTION_DECL && DECL_INITIAL (decl))
851 || (TREE_CODE (decl) != FUNCTION_DECL
852 && TREE_PUBLIC (decl) && !DECL_EXTERNAL (decl))
853 /* A static variable declaration is always a tentative definition,
854 but the alias is a non-tentative definition which overrides. */
855 || (TREE_CODE (decl) != FUNCTION_DECL
856 && ! TREE_PUBLIC (decl) && DECL_INITIAL (decl)))
857 {
858 error ("%q+D defined both normally and as %qE attribute", decl, name);
859 *no_add_attrs = true;
860 return NULL_TREE;
861 }
862 else if (!is_alias
863 && (lookup_attribute ("weak", DECL_ATTRIBUTES (decl))
864 || lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))))
865 {
866 error ("weak %q+D cannot be defined %qE", decl, name);
867 *no_add_attrs = true;
868 return NULL_TREE;
869 }
870
871 /* Note that the very first time we process a nested declaration,
872 decl_function_context will not be set. Indeed, *would* never
873 be set except for the DECL_INITIAL/DECL_EXTERNAL frobbery that
874 we do below. After such frobbery, pushdecl would set the context.
875 In any case, this is never what we want. */
876 else if (decl_function_context (decl) == 0 && current_function_decl == NULL)
877 {
878 tree id;
879
880 id = TREE_VALUE (args);
881 if (TREE_CODE (id) != STRING_CST)
882 {
883 error ("attribute %qE argument not a string", name);
884 *no_add_attrs = true;
885 return NULL_TREE;
886 }
887 id = get_identifier (TREE_STRING_POINTER (id));
888 /* This counts as a use of the object pointed to. */
889 TREE_USED (id) = 1;
890
891 if (TREE_CODE (decl) == FUNCTION_DECL)
892 DECL_INITIAL (decl) = error_mark_node;
893 else
894 TREE_STATIC (decl) = 1;
895
896 if (!is_alias)
897 {
898 /* ifuncs are also aliases, so set that attribute too. */
899 DECL_ATTRIBUTES (decl)
900 = tree_cons (get_identifier ("alias"), args,
901 DECL_ATTRIBUTES (decl));
902 DECL_ATTRIBUTES (decl) = tree_cons (get_identifier ("ifunc"),
903 NULL, DECL_ATTRIBUTES (decl));
904 }
905 }
906 else
907 {
908 warning (OPT_Wattributes, "%qE attribute ignored", name);
909 *no_add_attrs = true;
910 }
911
912 if (decl_in_symtab_p (*node))
913 {
914 struct symtab_node *n = symtab_node::get (decl);
915 if (n && n->refuse_visibility_changes)
916 error ("%+qD declared %qs after being used",
917 decl, is_alias ? "alias" : "ifunc");
918 }
919
920
921 return NULL_TREE;
922 }
923
924 /* Handle an "alias" or "ifunc" attribute; arguments as in
925 struct attribute_spec.handler. */
926
927 static tree
928 handle_alias_attribute (tree *node, tree name, tree args,
929 int ARG_UNUSED (flags), bool *no_add_attrs)
930 {
931 return handle_alias_ifunc_attribute (true, node, name, args, no_add_attrs);
932 }
933
934 /* (end of attribute-handling). */
935
936 /* Language-dependent contents of a type. */
937
938 struct GTY(()) lang_type
939 {
940 char dummy;
941 };
942
943 /* Language-dependent contents of a decl. */
944
945 struct GTY((variable_size)) lang_decl
946 {
947 char dummy;
948 };
949
950 /* Language-dependent contents of an identifier. This must include a
951 tree_identifier. */
952
953 struct GTY(()) lang_identifier
954 {
955 struct tree_identifier common;
956 };
957
958 /* The resulting tree type. */
959
960 union GTY((desc ("TREE_CODE (&%h.generic) == IDENTIFIER_NODE"),
961 chain_next ("CODE_CONTAINS_STRUCT (TREE_CODE (&%h.generic), TS_COMMON) ? ((union lang_tree_node *) TREE_CHAIN (&%h.generic)) : NULL")))
962 lang_tree_node
963 {
964 union tree_node GTY((tag ("0"),
965 desc ("tree_node_structure (&%h)"))) generic;
966 struct lang_identifier GTY((tag ("1"))) identifier;
967 };
968
969 /* We don't use language_function. */
970
971 struct GTY(()) language_function
972 {
973 int dummy;
974 };
975
976 /* GC-marking callback for use from jit_root_tab.
977
978 If there's an active playback context, call its marking method
979 so that it can mark any pointers it references. */
980
981 static void my_ggc_walker (void *)
982 {
983 if (gcc::jit::active_playback_ctxt)
984 gcc::jit::active_playback_ctxt->gt_ggc_mx ();
985 }
986
987 const char *dummy;
988
989 struct ggc_root_tab jit_root_tab[] =
990 {
991 {
992 &dummy, 1, 0, my_ggc_walker, NULL
993 },
994 LAST_GGC_ROOT_TAB
995 };
996
997 /* Subclass of diagnostic_output_format for libgccjit: like text
998 output, but capture the message and call add_diagnostic with it
999 on the active playback context. */
1000
1001 class jit_diagnostic_listener : public diagnostic_text_output_format
1002 {
1003 public:
1004 jit_diagnostic_listener (diagnostic_context &dc,
1005 gcc::jit::playback::context &playback_ctxt)
1006 : diagnostic_text_output_format (dc),
1007 m_playback_ctxt (playback_ctxt)
1008 {
1009 }
1010
1011 void dump (FILE *out, int indent) const final override
1012 {
1013 fprintf (out, "%*sjit_diagnostic_listener\n", indent, "");
1014 fprintf (out, "%*sm_playback_context: %p\n",
1015 indent + 2, "",
1016 (void *)&m_playback_ctxt);
1017 }
1018
1019 void on_report_diagnostic (const diagnostic_info &info,
1020 diagnostic_t orig_diag_kind) final override
1021 {
1022 JIT_LOG_SCOPE (gcc::jit::active_playback_ctxt->get_logger ());
1023
1024 /* Let the text output format do most of the work. */
1025 diagnostic_text_output_format::on_report_diagnostic (info, orig_diag_kind);
1026
1027 const char *text = pp_formatted_text (get_printer ());
1028
1029 /* Delegate to the playback context (and thence to the
1030 recording context). */
1031 gcc::jit::active_playback_ctxt->add_diagnostic (text, info);
1032
1033 pp_clear_output_area (get_printer ());
1034 }
1035
1036 private:
1037 gcc::jit::playback::context &m_playback_ctxt;
1038 };
1039
1040 /* JIT-specific implementation of diagnostic callbacks. */
1041
1042 /* Implementation of "begin_diagnostic". */
1043
1044 static void
1045 jit_begin_diagnostic (diagnostic_text_output_format &,
1046 const diagnostic_info */*diagnostic*/)
1047 {
1048 gcc_assert (gcc::jit::active_playback_ctxt);
1049 JIT_LOG_SCOPE (gcc::jit::active_playback_ctxt->get_logger ());
1050
1051 /* No-op (apart from logging); the real error-handling is done by the
1052 jit_diagnostic_listener. */
1053 }
1054
1055 /* Implementation of "end_diagnostic". */
1056
1057 static void
1058 jit_end_diagnostic (diagnostic_text_output_format &,
1059 const diagnostic_info *,
1060 diagnostic_t)
1061 {
1062 gcc_assert (gcc::jit::active_playback_ctxt);
1063 JIT_LOG_SCOPE (gcc::jit::active_playback_ctxt->get_logger ());
1064
1065 /* No-op (apart from logging); the real error-handling is done by the
1066 jit_diagnostic_listener. */
1067 }
1068
1069 /* Language hooks. */
1070
1071 static bool
1072 jit_langhook_init (void)
1073 {
1074 gcc_assert (gcc::jit::active_playback_ctxt);
1075 JIT_LOG_SCOPE (gcc::jit::active_playback_ctxt->get_logger ());
1076
1077 static bool registered_root_tab = false;
1078 if (!registered_root_tab)
1079 {
1080 ggc_register_root_tab (jit_root_tab);
1081 registered_root_tab = true;
1082 }
1083
1084 gcc_assert (global_dc);
1085 diagnostic_text_starter (global_dc) = jit_begin_diagnostic;
1086 diagnostic_text_finalizer (global_dc) = jit_end_diagnostic;
1087 auto sink
1088 = ::make_unique<jit_diagnostic_listener> (*global_dc,
1089 *gcc::jit::active_playback_ctxt);
1090 global_dc->set_output_format (std::move (sink));
1091
1092 build_common_tree_nodes (flag_signed_char);
1093
1094 target_builtins.empty ();
1095 build_common_builtin_nodes ();
1096
1097 /* The default precision for floating point numbers. This is used
1098 for floating point constants with abstract type. This may
1099 eventually be controllable by a command line option. */
1100 mpfr_set_default_prec (256);
1101
1102 targetm.init_builtins ();
1103
1104 return true;
1105 }
1106
1107 static void
1108 jit_langhook_parse_file (void)
1109 {
1110 /* Replay the activity by the client, recorded on the context. */
1111 gcc_assert (gcc::jit::active_playback_ctxt);
1112 gcc::jit::active_playback_ctxt->replay ();
1113 }
1114
1115 static tree
1116 jit_langhook_type_for_mode (machine_mode mode, int unsignedp)
1117 {
1118 /* Build any vector types here (see PR 46805). */
1119 if (VECTOR_MODE_P (mode))
1120 {
1121 tree inner;
1122
1123 inner = jit_langhook_type_for_mode (GET_MODE_INNER (mode), unsignedp);
1124 if (inner != NULL_TREE)
1125 return build_vector_type_for_mode (inner, mode);
1126 return NULL_TREE;
1127 }
1128
1129 if (mode == TYPE_MODE (float_type_node))
1130 return float_type_node;
1131
1132 if (mode == TYPE_MODE (double_type_node))
1133 return double_type_node;
1134
1135 if (mode == TYPE_MODE (intQI_type_node))
1136 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
1137 if (mode == TYPE_MODE (intHI_type_node))
1138 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
1139 if (mode == TYPE_MODE (intSI_type_node))
1140 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
1141 if (mode == TYPE_MODE (intDI_type_node))
1142 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
1143 if (mode == TYPE_MODE (intTI_type_node))
1144 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
1145
1146 if (mode == TYPE_MODE (integer_type_node))
1147 return unsignedp ? unsigned_type_node : integer_type_node;
1148
1149 if (mode == TYPE_MODE (long_integer_type_node))
1150 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
1151
1152 if (mode == TYPE_MODE (long_long_integer_type_node))
1153 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
1154
1155 if (COMPLEX_MODE_P (mode))
1156 {
1157 if (mode == TYPE_MODE (complex_float_type_node))
1158 return complex_float_type_node;
1159 if (mode == TYPE_MODE (complex_double_type_node))
1160 return complex_double_type_node;
1161 if (mode == TYPE_MODE (complex_long_double_type_node))
1162 return complex_long_double_type_node;
1163 if (mode == TYPE_MODE (complex_integer_type_node) && !unsignedp)
1164 return complex_integer_type_node;
1165 }
1166
1167 /* gcc_unreachable */
1168 return NULL;
1169 }
1170
1171 recording::type* tree_type_to_jit_type (tree type)
1172 {
1173 if (TREE_CODE (type) == VECTOR_TYPE)
1174 {
1175 tree inner_type = TREE_TYPE (type);
1176 recording::type* element_type = tree_type_to_jit_type (inner_type);
1177 poly_uint64 size = TYPE_VECTOR_SUBPARTS (type);
1178 long constant_size = size.to_constant ();
1179 if (element_type != NULL)
1180 return element_type->get_vector (constant_size);
1181 return NULL;
1182 }
1183 if (TREE_CODE (type) == REFERENCE_TYPE)
1184 // For __builtin_ms_va_start.
1185 // FIXME: wrong type.
1186 return new recording::memento_of_get_type (&target_builtins_ctxt,
1187 GCC_JIT_TYPE_VOID);
1188 if (TREE_CODE (type) == RECORD_TYPE)
1189 // For __builtin_sysv_va_copy.
1190 // FIXME: wrong type.
1191 return new recording::memento_of_get_type (&target_builtins_ctxt,
1192 GCC_JIT_TYPE_VOID);
1193 /* TODO: Remove when we add support for sized floating-point types. */
1194 for (int i = 0; i < NUM_FLOATN_NX_TYPES; i++)
1195 if (type == FLOATN_NX_TYPE_NODE (i))
1196 // FIXME: wrong type.
1197 return new recording::memento_of_get_type (&target_builtins_ctxt,
1198 GCC_JIT_TYPE_VOID);
1199 if (type == void_type_node)
1200 return new recording::memento_of_get_type (&target_builtins_ctxt,
1201 GCC_JIT_TYPE_VOID);
1202 else if (type == ptr_type_node)
1203 return new recording::memento_of_get_type (&target_builtins_ctxt,
1204 GCC_JIT_TYPE_VOID_PTR);
1205 else if (type == const_ptr_type_node)
1206 {
1207 // Void const ptr.
1208 recording::type* result =
1209 new recording::memento_of_get_type (&target_builtins_ctxt,
1210 GCC_JIT_TYPE_VOID_PTR);
1211 return new recording::memento_of_get_const (result);
1212 }
1213 else if (type == unsigned_type_node)
1214 return new recording::memento_of_get_type (&target_builtins_ctxt,
1215 GCC_JIT_TYPE_UNSIGNED_INT);
1216 else if (type == long_unsigned_type_node)
1217 return new recording::memento_of_get_type (&target_builtins_ctxt,
1218 GCC_JIT_TYPE_UNSIGNED_LONG);
1219 else if (type == integer_type_node)
1220 return new recording::memento_of_get_type (&target_builtins_ctxt,
1221 GCC_JIT_TYPE_INT);
1222 else if (type == long_integer_type_node)
1223 return new recording::memento_of_get_type (&target_builtins_ctxt,
1224 GCC_JIT_TYPE_LONG);
1225 else if (type == long_long_integer_type_node)
1226 return new recording::memento_of_get_type (&target_builtins_ctxt,
1227 GCC_JIT_TYPE_LONG_LONG);
1228 else if (type == signed_char_type_node)
1229 return new recording::memento_of_get_type (&target_builtins_ctxt,
1230 GCC_JIT_TYPE_SIGNED_CHAR);
1231 else if (type == char_type_node)
1232 return new recording::memento_of_get_type (&target_builtins_ctxt,
1233 GCC_JIT_TYPE_CHAR);
1234 else if (type == unsigned_intQI_type_node)
1235 return new recording::memento_of_get_type (&target_builtins_ctxt,
1236 GCC_JIT_TYPE_UINT8_T);
1237 else if (type == short_integer_type_node)
1238 return new recording::memento_of_get_type (&target_builtins_ctxt,
1239 GCC_JIT_TYPE_SHORT);
1240 else if (type == short_unsigned_type_node)
1241 return new recording::memento_of_get_type (&target_builtins_ctxt,
1242 GCC_JIT_TYPE_UNSIGNED_SHORT);
1243 else if (type == complex_float_type_node)
1244 return new recording::memento_of_get_type (&target_builtins_ctxt,
1245 GCC_JIT_TYPE_COMPLEX_FLOAT);
1246 else if (type == complex_double_type_node)
1247 return new recording::memento_of_get_type (&target_builtins_ctxt,
1248 GCC_JIT_TYPE_COMPLEX_DOUBLE);
1249 else if (type == complex_long_double_type_node)
1250 return new recording::memento_of_get_type (&target_builtins_ctxt,
1251 GCC_JIT_TYPE_COMPLEX_LONG_DOUBLE);
1252 else if (type == float_type_node)
1253 return new recording::memento_of_get_type (&target_builtins_ctxt,
1254 GCC_JIT_TYPE_FLOAT);
1255 else if (type == double_type_node)
1256 return new recording::memento_of_get_type (&target_builtins_ctxt,
1257 GCC_JIT_TYPE_DOUBLE);
1258 else if (type == long_double_type_node)
1259 return new recording::memento_of_get_type (&target_builtins_ctxt,
1260 GCC_JIT_TYPE_LONG_DOUBLE);
1261 else if (type == bfloat16_type_node)
1262 return new recording::memento_of_get_type (&target_builtins_ctxt,
1263 GCC_JIT_TYPE_BFLOAT16);
1264 else if (type == dfloat128_type_node)
1265 // FIXME: wrong type.
1266 return new recording::memento_of_get_type (&target_builtins_ctxt,
1267 GCC_JIT_TYPE_VOID);
1268 else if (type == long_long_unsigned_type_node)
1269 return new recording::memento_of_get_type (&target_builtins_ctxt,
1270 GCC_JIT_TYPE_UNSIGNED_LONG_LONG);
1271 else if (type == boolean_type_node)
1272 return new recording::memento_of_get_type (&target_builtins_ctxt,
1273 GCC_JIT_TYPE_BOOL);
1274 else if (type == size_type_node)
1275 return new recording::memento_of_get_type (&target_builtins_ctxt,
1276 GCC_JIT_TYPE_SIZE_T);
1277 else if (TREE_CODE (type) == POINTER_TYPE)
1278 {
1279 tree inner_type = TREE_TYPE (type);
1280 recording::type* element_type = tree_type_to_jit_type (inner_type);
1281 if (!element_type)
1282 return nullptr;
1283 return element_type->get_pointer ();
1284 }
1285 else
1286 {
1287 // Attempt to find an unqualified type when the current type has qualifiers.
1288 tree tp = TYPE_MAIN_VARIANT (type);
1289 for ( ; tp != NULL ; tp = TYPE_NEXT_VARIANT (tp))
1290 {
1291 if (TYPE_QUALS (tp) == 0 && type != tp)
1292 {
1293 recording::type* result = tree_type_to_jit_type (tp);
1294 if (result != NULL)
1295 {
1296 if (TYPE_READONLY (tp))
1297 result = new recording::memento_of_get_const (result);
1298 if (TYPE_VOLATILE (tp))
1299 result = new recording::memento_of_get_volatile (result);
1300 return result;
1301 }
1302 }
1303 }
1304 }
1305
1306 return NULL;
1307 }
1308
1309 /* Record a builtin function. We save their types to be able to check types
1310 in recording and for reflection. */
1311
1312 static tree
1313 jit_langhook_builtin_function (tree decl)
1314 {
1315 if (TREE_CODE (decl) == FUNCTION_DECL)
1316 {
1317 const char* name = IDENTIFIER_POINTER (DECL_NAME (decl));
1318 target_builtins.put (name, decl);
1319
1320 std::string string_name (name);
1321 if (target_function_types.count (string_name) == 0)
1322 {
1323 tree function_type = TREE_TYPE (decl);
1324 tree arg = TYPE_ARG_TYPES (function_type);
1325 bool is_variadic = false;
1326
1327 auto_vec <recording::type *> param_types;
1328
1329 while (arg != void_list_node)
1330 {
1331 if (arg == NULL)
1332 {
1333 is_variadic = true;
1334 break;
1335 }
1336 if (arg != void_list_node)
1337 {
1338 recording::type* arg_type = tree_type_to_jit_type (TREE_VALUE (arg));
1339 if (arg_type == NULL)
1340 return decl;
1341 param_types.safe_push (arg_type);
1342 }
1343 arg = TREE_CHAIN (arg);
1344 }
1345
1346 tree result_type = TREE_TYPE (function_type);
1347 recording::type* return_type = tree_type_to_jit_type (result_type);
1348
1349 if (return_type == NULL)
1350 return decl;
1351
1352 recording::function_type* func_type =
1353 new recording::function_type (&target_builtins_ctxt, return_type,
1354 param_types.length (),
1355 param_types.address (), is_variadic,
1356 false);
1357
1358 target_function_types[string_name] = func_type;
1359 }
1360 }
1361 return decl;
1362 }
1363
1364 static bool
1365 jit_langhook_global_bindings_p (void)
1366 {
1367 return true;
1368 }
1369
1370 static tree
1371 jit_langhook_pushdecl (tree decl ATTRIBUTE_UNUSED)
1372 {
1373 return NULL_TREE;
1374 }
1375
1376 static tree
1377 jit_langhook_getdecls (void)
1378 {
1379 return NULL;
1380 }
1381
1382 #undef LANG_HOOKS_NAME
1383 #define LANG_HOOKS_NAME "libgccjit"
1384
1385 #undef LANG_HOOKS_INIT
1386 #define LANG_HOOKS_INIT jit_langhook_init
1387
1388 #undef LANG_HOOKS_PARSE_FILE
1389 #define LANG_HOOKS_PARSE_FILE jit_langhook_parse_file
1390
1391 #undef LANG_HOOKS_TYPE_FOR_MODE
1392 #define LANG_HOOKS_TYPE_FOR_MODE jit_langhook_type_for_mode
1393
1394 #undef LANG_HOOKS_BUILTIN_FUNCTION
1395 #define LANG_HOOKS_BUILTIN_FUNCTION jit_langhook_builtin_function
1396
1397 #undef LANG_HOOKS_GLOBAL_BINDINGS_P
1398 #define LANG_HOOKS_GLOBAL_BINDINGS_P jit_langhook_global_bindings_p
1399
1400 #undef LANG_HOOKS_PUSHDECL
1401 #define LANG_HOOKS_PUSHDECL jit_langhook_pushdecl
1402
1403 #undef LANG_HOOKS_GETDECLS
1404 #define LANG_HOOKS_GETDECLS jit_langhook_getdecls
1405
1406 /* Attribute hooks. */
1407 #undef LANG_HOOKS_ATTRIBUTE_TABLE
1408 #define LANG_HOOKS_ATTRIBUTE_TABLE jit_attribute_table
1409
1410 #undef LANG_HOOKS_DEEP_UNSHARING
1411 #define LANG_HOOKS_DEEP_UNSHARING true
1412
1413 struct lang_hooks lang_hooks = LANG_HOOKS_INITIALIZER;
1414
1415 #include "gt-jit-dummy-frontend.h"
1416 #include "gtype-jit.h"