Line data Source code
1 : /* jit.c -- Dummy "frontend" for use during JIT-compilation.
2 : Copyright (C) 2013-2026 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 "attr-handlers.h"
34 : #include "cgraph.h"
35 : #include "target.h"
36 : #include "diagnostics/text-sink.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. These are the libgccjit-specific attribute handlers;
45 : the handlers shared with the C family live in gcc/attr-handlers.cc. */
46 :
47 : static tree jit_handle_cold_attribute (tree *, tree, tree, int, bool *);
48 : static tree jit_handle_format_arg_attribute (tree *, tree, tree, int, bool *);
49 : static tree jit_handle_format_attribute (tree *, tree, tree, int, bool *);
50 : static tree jit_handle_nonnull_attribute (tree *, tree, tree, int, bool *);
51 : static tree jit_handle_noreturn_attribute (tree *, tree, tree, int, bool *);
52 : static tree jit_handle_patchable_function_entry_attribute (tree *, tree, tree,
53 : int, bool *);
54 : static tree jit_handle_sentinel_attribute (tree *, tree, tree, int, bool *);
55 : static tree jit_handle_transaction_pure_attribute (tree *, tree, tree, int,
56 : bool *);
57 :
58 : /* Helper to define attribute exclusions. */
59 : #define ATTR_EXCL(name, function, type, variable) \
60 : { name, function, type, variable }
61 :
62 : /* These variables act as a cache for the target builtins. This is needed in
63 : order to be able to type-check the calls since we can only get those types
64 : in the playback phase while we need them in the recording phase. */
65 : hash_map<nofree_string_hash, tree> target_builtins{};
66 : std::unordered_map<std::string, recording::function_type*> target_function_types
67 : {};
68 : recording::context target_builtins_ctxt{NULL};
69 :
70 : /* Table of machine-independent attributes supported in libgccjit. */
71 : static const attribute_spec jit_gnu_attributes[] =
72 : {
73 : /* { name, min_len, max_len, decl_req, type_req, fn_type_req,
74 : affects_type_identity, handler, exclude } */
75 : { "alias", 1, 1, true, false, false, false,
76 : handle_alias_attribute, NULL },
77 : { "always_inline", 0, 0, true, false, false, false,
78 : handle_always_inline_attribute,
79 : attr_always_inline_exclusions },
80 : { "cold", 0, 0, true, false, false, false,
81 : jit_handle_cold_attribute,
82 : attr_cold_hot_exclusions },
83 : /* The same comments as for noreturn attributes apply to const ones. */
84 : { "const", 0, 0, true, false, false, false,
85 : handle_const_attribute,
86 : attr_const_pure_exclusions },
87 : { "fn spec", 1, 1, false, true, true, false,
88 : handle_fnspec_attribute, NULL },
89 :
90 : { "leaf", 0, 0, true, false, false, false,
91 : handle_leaf_attribute, NULL },
92 : { "malloc", 0, 0, true, false, false, false,
93 : handle_malloc_common, attr_alloc_exclusions },
94 : { "noreturn", 0, 0, true, false, false, false,
95 : jit_handle_noreturn_attribute,
96 : attr_noreturn_exclusions },
97 : { "no vops", 0, 0, true, false, false, false,
98 : handle_novops_attribute, NULL },
99 : { "noinline", 0, 0, true, false, false, false,
100 : handle_noinline_attribute,
101 : attr_noinline_exclusions },
102 : { "nonnull", 0, -1, false, true, true, false,
103 : jit_handle_nonnull_attribute, NULL },
104 : { "nothrow", 0, 0, true, false, false, false,
105 : handle_nothrow_attribute, NULL },
106 : { "patchable_function_entry", 1, 2, true, false, false, false,
107 : jit_handle_patchable_function_entry_attribute,
108 : NULL },
109 : { "pure", 0, 0, true, false, false, false,
110 : handle_pure_attribute,
111 : attr_const_pure_exclusions },
112 : { "returns_twice", 0, 0, true, false, false, false,
113 : handle_returns_twice_attribute,
114 : attr_returns_twice_exclusions },
115 : { "sentinel", 0, 1, false, true, true, false,
116 : jit_handle_sentinel_attribute, NULL },
117 : { "target", 1, -1, true, false, false, false,
118 : handle_target_attribute, attr_target_exclusions },
119 : { "type generic", 0, 0, false, true, true, false,
120 : handle_type_generic_attribute, NULL },
121 : { "transaction_pure", 0, 0, false, true, true, false,
122 : jit_handle_transaction_pure_attribute, NULL },
123 : { "used", 0, 0, true, false, false, false,
124 : handle_used_attribute, NULL },
125 : { "visibility", 1, 1, false, false, false, false,
126 : handle_visibility_attribute, NULL },
127 : { "weak", 0, 0, true, false, false, false,
128 : handle_weak_attribute, NULL },
129 : /* For internal use only. The leading '*' both prevents its usage in
130 : source code and signals that it may be overridden by machine tables. */
131 : { "*tm regparm", 0, 0, false, true, true, false,
132 : ignore_attribute, NULL },
133 : };
134 :
135 : static const scoped_attribute_specs jit_gnu_attribute_table =
136 : {
137 : "gnu", { jit_gnu_attributes }
138 : };
139 :
140 : /* Give the specifications for the format attributes, used by C and all
141 : descendants. */
142 :
143 : static const attribute_spec jit_format_attributes[] =
144 : {
145 : /* { name, min_len, max_len, decl_req, type_req, fn_type_req,
146 : affects_type_identity, handler, exclude } */
147 : { "format", 3, 3, false, true, true, false,
148 : jit_handle_format_attribute, NULL },
149 : { "format_arg", 1, 1, false, true, true, false,
150 : jit_handle_format_arg_attribute, NULL }
151 : };
152 :
153 : static const scoped_attribute_specs jit_format_attribute_table =
154 : {
155 : "gnu", { jit_format_attributes }
156 : };
157 :
158 : static const scoped_attribute_specs *const jit_attribute_table[] =
159 : {
160 : &jit_gnu_attribute_table,
161 : &jit_format_attribute_table
162 : };
163 :
164 : /* Attribute handlers. */
165 :
166 : /* Attribute handlers specific to libgccjit. If the attribute handler code
167 : is the same as the one in `c-family/c-attribs.cc`, please move the
168 : handler into `attr-handlers.cc`. */
169 :
170 :
171 : /* Handle a "noreturn" attribute; arguments as in
172 : struct attribute_spec.handler. */
173 :
174 : static tree
175 1336 : jit_handle_noreturn_attribute (tree *node, tree ARG_UNUSED (name),
176 : tree ARG_UNUSED (args), int ARG_UNUSED (flags),
177 : bool * ARG_UNUSED (no_add_attrs))
178 : {
179 1336 : tree type = TREE_TYPE (*node);
180 :
181 1336 : if (TREE_CODE (*node) == FUNCTION_DECL)
182 1336 : TREE_THIS_VOLATILE (*node) = 1;
183 0 : else if (TREE_CODE (type) == POINTER_TYPE
184 0 : && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
185 0 : TREE_TYPE (*node)
186 0 : = build_pointer_type
187 0 : (build_type_variant (TREE_TYPE (type),
188 : TYPE_READONLY (TREE_TYPE (type)), 1));
189 : else
190 0 : gcc_unreachable ();
191 :
192 1336 : return NULL_TREE;
193 : }
194 :
195 : /* Helper for nonnull attribute handling; fetch the operand number
196 : from the attribute argument list. */
197 :
198 : static bool
199 5 : get_nonnull_operand (tree arg_num_expr, unsigned HOST_WIDE_INT *valp)
200 : {
201 : /* Verify the arg number is a constant. */
202 5 : if (!tree_fits_uhwi_p (arg_num_expr))
203 : return false;
204 :
205 5 : *valp = TREE_INT_CST_LOW (arg_num_expr);
206 5 : return true;
207 : }
208 :
209 : /* Handle the "nonnull" attribute. */
210 :
211 : static tree
212 105 : jit_handle_nonnull_attribute (tree *node, tree ARG_UNUSED (name), tree args,
213 : int ARG_UNUSED (flags),
214 : bool * ARG_UNUSED (no_add_attrs))
215 : {
216 105 : tree type = *node;
217 :
218 : /* If no arguments are specified, all pointer arguments should be
219 : non-null. Verify a full prototype is given so that the arguments
220 : will have the correct types when we actually check them later.
221 : Avoid diagnosing type-generic built-ins since those have no
222 : prototype. */
223 105 : if (!args)
224 : {
225 100 : gcc_assert (prototype_p (type)
226 : || !TYPE_ATTRIBUTES (type)
227 : || lookup_attribute ("type generic", TYPE_ATTRIBUTES (type)));
228 :
229 : return NULL_TREE;
230 : }
231 :
232 : /* Argument list specified. Verify that each argument number references
233 : a pointer argument. */
234 10 : for (; args; args = TREE_CHAIN (args))
235 : {
236 5 : tree argument;
237 5 : unsigned HOST_WIDE_INT arg_num = 0, ck_num;
238 :
239 5 : if (!get_nonnull_operand (TREE_VALUE (args), &arg_num))
240 0 : gcc_unreachable ();
241 :
242 5 : argument = TYPE_ARG_TYPES (type);
243 5 : if (argument)
244 : {
245 0 : for (ck_num = 1; ; ck_num++)
246 : {
247 5 : if (!argument || ck_num == arg_num)
248 : break;
249 0 : argument = TREE_CHAIN (argument);
250 : }
251 :
252 5 : gcc_assert (argument
253 : && TREE_CODE (TREE_VALUE (argument)) == POINTER_TYPE);
254 : }
255 : }
256 :
257 : return NULL_TREE;
258 : }
259 :
260 : /* Handle a "sentinel" attribute. */
261 :
262 : static tree
263 0 : jit_handle_sentinel_attribute (tree *node, tree ARG_UNUSED (name), tree args,
264 : int ARG_UNUSED (flags),
265 : bool * ARG_UNUSED (no_add_attrs))
266 : {
267 0 : gcc_assert (stdarg_p (*node));
268 :
269 0 : if (args)
270 : {
271 0 : tree position = TREE_VALUE (args);
272 0 : gcc_assert (TREE_CODE (position) == INTEGER_CST);
273 0 : if (tree_int_cst_lt (position, integer_zero_node))
274 0 : gcc_unreachable ();
275 : }
276 :
277 0 : return NULL_TREE;
278 : }
279 :
280 : /* Handle a "transaction_pure" attribute. */
281 :
282 : static tree
283 0 : jit_handle_transaction_pure_attribute (tree *node, tree ARG_UNUSED (name),
284 : tree ARG_UNUSED (args),
285 : int ARG_UNUSED (flags),
286 : bool * ARG_UNUSED (no_add_attrs))
287 : {
288 : /* Ensure we have a function type. */
289 0 : gcc_assert (TREE_CODE (*node) == FUNCTION_TYPE);
290 :
291 0 : return NULL_TREE;
292 : }
293 :
294 : /* Handle a "cold" and attribute; arguments as in
295 : struct attribute_spec.handler. */
296 :
297 : static tree
298 1341 : jit_handle_cold_attribute (tree *node, tree name, tree ARG_UNUSED (args),
299 : int ARG_UNUSED (flags), bool *no_add_attrs)
300 : {
301 1341 : if (TREE_CODE (*node) == FUNCTION_DECL
302 1341 : || TREE_CODE (*node) == LABEL_DECL)
303 : {
304 : /* Attribute cold processing is done later with lookup_attribute. */
305 : }
306 : else
307 : {
308 0 : warning (OPT_Wattributes, "%qE attribute ignored", name);
309 0 : *no_add_attrs = true;
310 : }
311 :
312 1341 : return NULL_TREE;
313 : }
314 :
315 : static tree
316 0 : jit_handle_patchable_function_entry_attribute (tree *, tree, tree, int, bool *)
317 : {
318 : /* Nothing to be done here. */
319 0 : return NULL_TREE;
320 : }
321 :
322 : /* Handle a "format" attribute; arguments as in
323 : struct attribute_spec.handler. */
324 :
325 : static tree
326 0 : jit_handle_format_attribute (tree * ARG_UNUSED (node), tree ARG_UNUSED (name),
327 : tree ARG_UNUSED (args), int ARG_UNUSED (flags),
328 : bool *no_add_attrs)
329 : {
330 0 : *no_add_attrs = true;
331 0 : return NULL_TREE;
332 : }
333 :
334 :
335 : /* Handle a "format_arg" attribute; arguments as in
336 : struct attribute_spec.handler. */
337 :
338 : tree
339 0 : jit_handle_format_arg_attribute (tree * ARG_UNUSED (node),
340 : tree ARG_UNUSED (name), tree ARG_UNUSED (args),
341 : int ARG_UNUSED (flags), bool *no_add_attrs)
342 : {
343 0 : *no_add_attrs = true;
344 0 : return NULL_TREE;
345 : }
346 :
347 : /* (end of attribute-handling). */
348 :
349 : /* Language-dependent contents of a type. */
350 :
351 : struct GTY(()) lang_type
352 : {
353 : char dummy;
354 : };
355 :
356 : /* Language-dependent contents of a decl. */
357 :
358 : struct GTY((variable_size)) lang_decl
359 : {
360 : char dummy;
361 : };
362 :
363 : /* Language-dependent contents of an identifier. This must include a
364 : tree_identifier. */
365 :
366 : struct GTY(()) lang_identifier
367 : {
368 : struct tree_identifier common;
369 : };
370 :
371 : /* The resulting tree type. */
372 :
373 : /* See lang_tree_node in gcc/c/c-decl.cc. */
374 : union GTY((desc ("TREE_CODE (&%h.generic) == IDENTIFIER_NODE"),
375 : chain_next ("(union lang_tree_node *) jit_tree_chain_next (&%h.generic)")))
376 : lang_tree_node
377 : {
378 : union tree_node GTY ((tag ("0"),
379 : desc ("tree_node_structure (&%h)")))
380 : generic;
381 : struct lang_identifier GTY ((tag ("1"))) identifier;
382 : };
383 :
384 : /* We don't use language_function. */
385 :
386 : struct GTY(()) language_function
387 : {
388 : int dummy;
389 : };
390 :
391 : /* GC-marking callback for use from jit_root_tab.
392 :
393 : If there's an active playback context, call its marking method
394 : so that it can mark any pointers it references. */
395 :
396 667202 : static void my_ggc_walker (void *)
397 : {
398 667202 : if (gcc::jit::active_playback_ctxt)
399 667202 : gcc::jit::active_playback_ctxt->gt_ggc_mx ();
400 667202 : }
401 :
402 : const char *dummy;
403 :
404 : struct ggc_root_tab jit_root_tab[] =
405 : {
406 : {
407 : &dummy, 1, 0, my_ggc_walker, NULL
408 : },
409 : LAST_GGC_ROOT_TAB
410 : };
411 :
412 : /* Subclass of diagnostics::text_sink for libgccjit: like text
413 : output, but capture the message and call add_diagnostic with it
414 : on the active playback context. */
415 :
416 : class jit_diagnostic_listener : public diagnostics::text_sink
417 : {
418 : public:
419 1366 : jit_diagnostic_listener (diagnostics::context &dc,
420 : gcc::jit::playback::context &playback_ctxt)
421 : : diagnostics::text_sink (dc),
422 1366 : m_playback_ctxt (playback_ctxt)
423 : {
424 : }
425 :
426 0 : void dump (FILE *out, int indent) const final override
427 : {
428 0 : fprintf (out, "%*sjit_diagnostic_listener\n", indent, "");
429 0 : fprintf (out, "%*sm_playback_context: %p\n",
430 : indent + 2, "",
431 0 : (void *)&m_playback_ctxt);
432 0 : }
433 :
434 25 : void on_report_diagnostic (const diagnostics::diagnostic_info &info,
435 : enum diagnostics::kind orig_diag_kind) final override
436 : {
437 25 : JIT_LOG_SCOPE (gcc::jit::active_playback_ctxt->get_logger ());
438 :
439 : /* Let the text output format do most of the work. */
440 25 : diagnostics::text_sink::on_report_diagnostic (info, orig_diag_kind);
441 :
442 25 : const char *text = pp_formatted_text (get_printer ());
443 :
444 : /* Delegate to the playback context (and thence to the
445 : recording context). */
446 25 : gcc::jit::active_playback_ctxt->add_diagnostic (text, info);
447 :
448 25 : pp_clear_output_area (get_printer ());
449 25 : }
450 :
451 : private:
452 : gcc::jit::playback::context &m_playback_ctxt;
453 : };
454 :
455 : /* JIT-specific implementation of diagnostic callbacks. */
456 :
457 : /* Implementation of "begin_diagnostic". */
458 :
459 : static void
460 25 : jit_begin_diagnostic (diagnostics::text_sink &,
461 : const diagnostics::diagnostic_info */*diagnostic*/)
462 : {
463 25 : gcc_assert (gcc::jit::active_playback_ctxt);
464 25 : JIT_LOG_SCOPE (gcc::jit::active_playback_ctxt->get_logger ());
465 :
466 : /* No-op (apart from logging); the real error-handling is done by the
467 : jit_diagnostic_listener. */
468 25 : }
469 :
470 : /* Implementation of "end_diagnostic". */
471 :
472 : static void
473 25 : jit_end_diagnostic (diagnostics::text_sink &,
474 : const diagnostics::diagnostic_info *,
475 : enum diagnostics::kind)
476 : {
477 25 : gcc_assert (gcc::jit::active_playback_ctxt);
478 25 : JIT_LOG_SCOPE (gcc::jit::active_playback_ctxt->get_logger ());
479 :
480 : /* No-op (apart from logging); the real error-handling is done by the
481 : jit_diagnostic_listener. */
482 25 : }
483 :
484 : /* Language hooks. */
485 :
486 : static bool
487 1366 : jit_langhook_init (void)
488 : {
489 1366 : gcc_assert (gcc::jit::active_playback_ctxt);
490 1366 : JIT_LOG_SCOPE (gcc::jit::active_playback_ctxt->get_logger ());
491 :
492 1366 : static bool registered_root_tab = false;
493 1366 : if (!registered_root_tab)
494 : {
495 131 : ggc_register_root_tab (jit_root_tab);
496 131 : registered_root_tab = true;
497 : }
498 :
499 1366 : gcc_assert (global_dc);
500 1366 : diagnostics::text_starter (global_dc) = jit_begin_diagnostic;
501 1366 : diagnostics::text_finalizer (global_dc) = jit_end_diagnostic;
502 1366 : auto sink
503 : = std::make_unique<jit_diagnostic_listener>
504 : (*global_dc,
505 1366 : *gcc::jit::active_playback_ctxt);
506 1366 : global_dc->set_sink (std::move (sink));
507 :
508 1366 : build_common_tree_nodes (flag_signed_char);
509 :
510 1366 : target_builtins.empty ();
511 1366 : build_common_builtin_nodes ();
512 :
513 : /* The default precision for floating point numbers. This is used
514 : for floating point constants with abstract type. This may
515 : eventually be controllable by a command line option. */
516 1366 : mpfr_set_default_prec (256);
517 :
518 1366 : targetm.init_builtins ();
519 :
520 2732 : return true;
521 1366 : }
522 :
523 : static void
524 1366 : jit_langhook_parse_file (void)
525 : {
526 : /* Replay the activity by the client, recorded on the context. */
527 1366 : gcc_assert (gcc::jit::active_playback_ctxt);
528 1366 : gcc::jit::active_playback_ctxt->replay ();
529 1366 : }
530 :
531 : static tree
532 13578 : jit_langhook_type_for_mode (machine_mode mode, int unsignedp)
533 : {
534 : /* Build any vector types here (see PR 46805). */
535 13578 : if (VECTOR_MODE_P (mode))
536 : {
537 360 : tree inner;
538 :
539 720 : inner = jit_langhook_type_for_mode (GET_MODE_INNER (mode), unsignedp);
540 360 : if (inner != NULL_TREE)
541 360 : return build_vector_type_for_mode (inner, mode);
542 : return NULL_TREE;
543 : }
544 :
545 13218 : if (mode == TYPE_MODE (float_type_node))
546 170 : return float_type_node;
547 :
548 13048 : if (mode == TYPE_MODE (double_type_node))
549 494 : return double_type_node;
550 :
551 12554 : if (mode == TYPE_MODE (intQI_type_node))
552 1025 : return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
553 11529 : if (mode == TYPE_MODE (intHI_type_node))
554 80 : return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
555 11449 : if (mode == TYPE_MODE (intSI_type_node))
556 1092 : return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
557 10357 : if (mode == TYPE_MODE (intDI_type_node))
558 2161 : return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
559 8196 : if (mode == TYPE_MODE (intTI_type_node))
560 0 : return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
561 :
562 8196 : if (mode == TYPE_MODE (integer_type_node))
563 0 : return unsignedp ? unsigned_type_node : integer_type_node;
564 :
565 8196 : if (mode == TYPE_MODE (long_integer_type_node))
566 0 : return unsignedp ? long_unsigned_type_node : long_integer_type_node;
567 :
568 8196 : if (mode == TYPE_MODE (long_long_integer_type_node))
569 0 : return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
570 :
571 8196 : if (COMPLEX_MODE_P (mode))
572 : {
573 8196 : if (mode == TYPE_MODE (complex_float_type_node))
574 1366 : return complex_float_type_node;
575 6830 : if (mode == TYPE_MODE (complex_double_type_node))
576 1366 : return complex_double_type_node;
577 5464 : if (mode == TYPE_MODE (complex_long_double_type_node))
578 1366 : return complex_long_double_type_node;
579 4098 : if (mode == TYPE_MODE (complex_integer_type_node) && !unsignedp)
580 0 : return complex_integer_type_node;
581 : }
582 :
583 32784 : for (int i = 0; i < NUM_FLOATN_NX_TYPES; i++)
584 28686 : if (FLOATN_NX_TYPE_NODE (i) != NULL_TREE
585 28686 : && mode == TYPE_MODE (FLOATN_NX_TYPE_NODE (i)))
586 0 : return FLOATN_NX_TYPE_NODE (i);
587 : /* gcc_unreachable */
588 : return NULL;
589 : }
590 :
591 3465998 : recording::type* tree_type_to_jit_type (tree type)
592 : {
593 3465998 : gcc_assert (gcc::jit::active_playback_ctxt);
594 3465998 : gcc::jit::playback::context* ctxt = gcc::jit::active_playback_ctxt;
595 :
596 3465998 : if (TREE_CODE (type) == VECTOR_TYPE)
597 : {
598 1414276 : tree inner_type = TREE_TYPE (type);
599 1414276 : recording::type* element_type = tree_type_to_jit_type (inner_type);
600 1414276 : poly_uint64 size = TYPE_VECTOR_SUBPARTS (type);
601 1414276 : long constant_size = size.to_constant ();
602 1414276 : if (element_type != NULL)
603 1414276 : return element_type->get_vector (constant_size);
604 : return NULL;
605 : }
606 2051722 : if (TREE_CODE (type) == REFERENCE_TYPE)
607 : // For __builtin_ms_va_start.
608 : // FIXME: wrong type.
609 393 : return new recording::memento_of_get_type (&target_builtins_ctxt,
610 393 : GCC_JIT_TYPE_VOID);
611 2051329 : if (TREE_CODE (type) == RECORD_TYPE)
612 : // For __builtin_sysv_va_copy.
613 : // FIXME: wrong type.
614 524 : return new recording::memento_of_get_type (&target_builtins_ctxt,
615 524 : GCC_JIT_TYPE_VOID);
616 2050805 : if (type == void_type_node)
617 41003 : return new recording::memento_of_get_type (&target_builtins_ctxt,
618 41003 : GCC_JIT_TYPE_VOID);
619 2009802 : else if (type == ptr_type_node)
620 12969 : return new recording::memento_of_get_type (&target_builtins_ctxt,
621 12969 : GCC_JIT_TYPE_VOID_PTR);
622 1996833 : else if (type == const_ptr_type_node)
623 : {
624 : // Void const ptr.
625 7336 : recording::type* result =
626 : new recording::memento_of_get_type (&target_builtins_ctxt,
627 7336 : GCC_JIT_TYPE_VOID_PTR);
628 7336 : return new recording::memento_of_get_const (result);
629 : }
630 1989497 : else if (type == unsigned_type_node)
631 51221 : return new recording::memento_of_get_type (&target_builtins_ctxt,
632 51221 : GCC_JIT_TYPE_UNSIGNED_INT);
633 1938276 : else if (type == long_unsigned_type_node)
634 1703 : return new recording::memento_of_get_type (&target_builtins_ctxt,
635 1703 : GCC_JIT_TYPE_UNSIGNED_LONG);
636 1936573 : else if (type == integer_type_node)
637 431252 : return new recording::memento_of_get_type (&target_builtins_ctxt,
638 431252 : GCC_JIT_TYPE_INT);
639 1505321 : else if (type == long_integer_type_node)
640 131 : return new recording::memento_of_get_type (&target_builtins_ctxt,
641 131 : GCC_JIT_TYPE_LONG);
642 1505190 : else if (type == long_long_integer_type_node)
643 207635 : return new recording::memento_of_get_type (&target_builtins_ctxt,
644 207635 : GCC_JIT_TYPE_LONG_LONG);
645 1297555 : else if (type == signed_char_type_node)
646 0 : return new recording::memento_of_get_type (&target_builtins_ctxt,
647 0 : GCC_JIT_TYPE_SIGNED_CHAR);
648 1297555 : else if (type == char_type_node)
649 174361 : return new recording::memento_of_get_type (&target_builtins_ctxt,
650 174361 : GCC_JIT_TYPE_CHAR);
651 1123194 : else if (type == unsigned_intQI_type_node)
652 189164 : return new recording::memento_of_get_type (&target_builtins_ctxt,
653 189164 : GCC_JIT_TYPE_UINT8_T);
654 934030 : else if (type == short_integer_type_node)
655 182352 : return new recording::memento_of_get_type (&target_builtins_ctxt,
656 182352 : GCC_JIT_TYPE_SHORT);
657 751678 : else if (type == short_unsigned_type_node)
658 68251 : return new recording::memento_of_get_type (&target_builtins_ctxt,
659 68251 : GCC_JIT_TYPE_UNSIGNED_SHORT);
660 683427 : else if (type == complex_float_type_node)
661 262 : return new recording::memento_of_get_type (&target_builtins_ctxt,
662 262 : GCC_JIT_TYPE_COMPLEX_FLOAT);
663 683165 : else if (type == complex_double_type_node)
664 262 : return new recording::memento_of_get_type (&target_builtins_ctxt,
665 262 : GCC_JIT_TYPE_COMPLEX_DOUBLE);
666 682903 : else if (type == complex_long_double_type_node)
667 262 : return new recording::memento_of_get_type (&target_builtins_ctxt,
668 262 : GCC_JIT_TYPE_COMPLEX_LONG_DOUBLE);
669 682641 : else if (type == float_type_node)
670 225713 : return new recording::memento_of_get_type (&target_builtins_ctxt,
671 225713 : GCC_JIT_TYPE_FLOAT);
672 456928 : else if (type == double_type_node)
673 190998 : return new recording::memento_of_get_type (&target_builtins_ctxt,
674 190998 : GCC_JIT_TYPE_DOUBLE);
675 265930 : else if (type == long_double_type_node)
676 1048 : return new recording::memento_of_get_type (&target_builtins_ctxt,
677 1048 : GCC_JIT_TYPE_LONG_DOUBLE);
678 264882 : else if (type == bfloat16_type_node)
679 58950 : return new recording::memento_of_get_type (&target_builtins_ctxt,
680 58950 : GCC_JIT_TYPE_BFLOAT16);
681 205932 : else if (type == float16_type_node)
682 : {
683 122354 : return new recording::memento_of_get_type (&target_builtins_ctxt,
684 122354 : GCC_JIT_TYPE_FLOAT16);
685 : }
686 83578 : else if (type == float32_type_node)
687 : {
688 0 : return new recording::memento_of_get_type (&target_builtins_ctxt,
689 0 : GCC_JIT_TYPE_FLOAT32);
690 : }
691 83578 : else if (type == float64_type_node)
692 : {
693 0 : return new recording::memento_of_get_type (&target_builtins_ctxt,
694 0 : GCC_JIT_TYPE_FLOAT64);
695 : }
696 83578 : else if (type == float128_type_node)
697 : {
698 1179 : return new recording::memento_of_get_type (&target_builtins_ctxt,
699 1179 : GCC_JIT_TYPE_FLOAT128);
700 : }
701 82399 : else if (type == dfloat128_type_node)
702 : // FIXME: wrong type.
703 0 : return new recording::memento_of_get_type (&target_builtins_ctxt,
704 0 : GCC_JIT_TYPE_VOID);
705 82399 : else if (type == long_long_unsigned_type_node)
706 25152 : return new recording::memento_of_get_type (&target_builtins_ctxt,
707 25152 : GCC_JIT_TYPE_UNSIGNED_LONG_LONG);
708 57247 : else if (type == boolean_type_node)
709 0 : return new recording::memento_of_get_type (&target_builtins_ctxt,
710 0 : GCC_JIT_TYPE_BOOL);
711 57247 : else if (type == size_type_node)
712 0 : return new recording::memento_of_get_type (&target_builtins_ctxt,
713 0 : GCC_JIT_TYPE_SIZE_T);
714 57247 : else if (TREE_CODE (type) == POINTER_TYPE)
715 : {
716 47422 : tree inner_type = TREE_TYPE (type);
717 47422 : recording::type* element_type = tree_type_to_jit_type (inner_type);
718 47422 : if (!element_type)
719 : return nullptr;
720 47422 : return element_type->get_pointer ();
721 : }
722 9825 : else if (type == unsigned_intTI_type_node)
723 0 : return new recording::memento_of_get_type (&target_builtins_ctxt,
724 0 : GCC_JIT_TYPE_UINT128_T);
725 9825 : else if (INTEGRAL_TYPE_P (type))
726 : {
727 4978 : unsigned int size = tree_to_uhwi (TYPE_SIZE_UNIT (type));
728 4978 : return target_builtins_ctxt.get_int_type (size, TYPE_UNSIGNED (type));
729 : }
730 4847 : else if (SCALAR_FLOAT_TYPE_P (type))
731 : {
732 4847 : unsigned int size = tree_to_uhwi (TYPE_SIZE_UNIT (type));
733 4847 : enum gcc_jit_types type;
734 4847 : switch (size)
735 : {
736 : case 2:
737 : type = GCC_JIT_TYPE_BFLOAT16;
738 : break;
739 1965 : case 4:
740 1965 : type = GCC_JIT_TYPE_FLOAT;
741 1965 : break;
742 2096 : case 8:
743 2096 : type = GCC_JIT_TYPE_DOUBLE;
744 2096 : break;
745 0 : default:
746 0 : if (ctxt->get_abort_on_unsupported_target_builtin ())
747 : {
748 0 : fprintf (stderr, "Unexpected float size: %d\n", size);
749 0 : abort ();
750 : }
751 : return NULL;
752 : }
753 4847 : return new recording::memento_of_get_type (&target_builtins_ctxt, type);
754 : }
755 : else
756 : {
757 : // Attempt to find an unqualified type when the current type has qualifiers.
758 0 : tree tp = TYPE_MAIN_VARIANT (type);
759 0 : for ( ; tp != NULL ; tp = TYPE_NEXT_VARIANT (tp))
760 : {
761 0 : if (!TYPE_QUALS (tp) && type != tp)
762 : {
763 0 : recording::type* result = tree_type_to_jit_type (tp);
764 0 : if (result != NULL)
765 : {
766 0 : if (TYPE_READONLY (tp))
767 0 : result = new recording::memento_of_get_const (result);
768 0 : if (TYPE_VOLATILE (tp))
769 0 : result = new recording::memento_of_get_volatile (result);
770 : return result;
771 : }
772 : }
773 : }
774 :
775 0 : if (ctxt->get_abort_on_unsupported_target_builtin ())
776 : {
777 0 : fprintf (stderr, "Unknown type:\n");
778 0 : debug_tree (type);
779 0 : abort ();
780 : }
781 : }
782 :
783 : return NULL;
784 : }
785 :
786 : /* Record a builtin function. We save their types to be able to check types
787 : in recording and for reflection. */
788 :
789 : static tree
790 5186702 : jit_langhook_builtin_function (tree decl)
791 : {
792 5186702 : if (TREE_CODE (decl) == FUNCTION_DECL)
793 : {
794 5186702 : const char* name = IDENTIFIER_POINTER (DECL_NAME (decl));
795 5186702 : target_builtins.put (name, decl);
796 :
797 5186702 : std::string string_name (name);
798 9875997 : if (target_function_types.count (string_name) == 0)
799 : {
800 497407 : tree function_type = TREE_TYPE (decl);
801 497407 : tree arg = TYPE_ARG_TYPES (function_type);
802 497407 : bool is_variadic = false;
803 :
804 497407 : auto_vec <recording::type *> param_types;
805 :
806 2004300 : while (arg != void_list_node)
807 : {
808 1507155 : if (arg == NULL)
809 : {
810 : is_variadic = true;
811 : break;
812 : }
813 1506893 : if (arg != void_list_node)
814 : {
815 1506893 : recording::type* arg_type = tree_type_to_jit_type (TREE_VALUE (arg));
816 1506893 : if (arg_type == NULL)
817 0 : return decl;
818 1506893 : param_types.safe_push (arg_type);
819 : }
820 1506893 : arg = TREE_CHAIN (arg);
821 : }
822 :
823 497407 : tree result_type = TREE_TYPE (function_type);
824 497407 : recording::type* return_type = tree_type_to_jit_type (result_type);
825 :
826 497407 : if (return_type == NULL)
827 : return decl;
828 :
829 497407 : recording::function_type* func_type =
830 : new recording::function_type (&target_builtins_ctxt, return_type,
831 497407 : param_types.length (),
832 : param_types.address (), is_variadic,
833 1479645 : false);
834 :
835 497407 : target_function_types[string_name] = func_type;
836 497407 : }
837 5186702 : }
838 : return decl;
839 : }
840 :
841 : static bool
842 80 : jit_langhook_global_bindings_p (void)
843 : {
844 80 : return true;
845 : }
846 :
847 : static tree
848 0 : jit_langhook_pushdecl (tree decl ATTRIBUTE_UNUSED)
849 : {
850 0 : return NULL_TREE;
851 : }
852 :
853 : static tree
854 0 : jit_langhook_getdecls (void)
855 : {
856 0 : return NULL;
857 : }
858 :
859 : #undef LANG_HOOKS_NAME
860 : #define LANG_HOOKS_NAME "libgccjit"
861 :
862 : #undef LANG_HOOKS_INIT
863 : #define LANG_HOOKS_INIT jit_langhook_init
864 :
865 : #undef LANG_HOOKS_PARSE_FILE
866 : #define LANG_HOOKS_PARSE_FILE jit_langhook_parse_file
867 :
868 : #undef LANG_HOOKS_TYPE_FOR_MODE
869 : #define LANG_HOOKS_TYPE_FOR_MODE jit_langhook_type_for_mode
870 :
871 : #undef LANG_HOOKS_BUILTIN_FUNCTION
872 : #define LANG_HOOKS_BUILTIN_FUNCTION jit_langhook_builtin_function
873 :
874 : #undef LANG_HOOKS_GLOBAL_BINDINGS_P
875 : #define LANG_HOOKS_GLOBAL_BINDINGS_P jit_langhook_global_bindings_p
876 :
877 : #undef LANG_HOOKS_PUSHDECL
878 : #define LANG_HOOKS_PUSHDECL jit_langhook_pushdecl
879 :
880 : #undef LANG_HOOKS_GETDECLS
881 : #define LANG_HOOKS_GETDECLS jit_langhook_getdecls
882 :
883 : /* Attribute hooks. */
884 : #undef LANG_HOOKS_ATTRIBUTE_TABLE
885 : #define LANG_HOOKS_ATTRIBUTE_TABLE jit_attribute_table
886 :
887 : #undef LANG_HOOKS_DEEP_UNSHARING
888 : #define LANG_HOOKS_DEEP_UNSHARING true
889 :
890 : struct lang_hooks lang_hooks = LANG_HOOKS_INITIALIZER;
891 :
892 : #include "gt-jit-dummy-frontend.h"
893 : #include "gtype-jit.h"
|