Line data Source code
1 : /* Internals of libgccjit: classes for playing back recorded API calls.
2 : Copyright (C) 2013-2026 Free Software Foundation, Inc.
3 : Contributed by David Malcolm <dmalcolm@redhat.com>.
4 :
5 : This file is part of GCC.
6 :
7 : GCC is free software; you can redistribute it and/or modify it
8 : under the terms of the GNU General Public License as published by
9 : the Free Software Foundation; either version 3, or (at your option)
10 : any later version.
11 :
12 : GCC is distributed in the hope that it will be useful, but
13 : WITHOUT ANY WARRANTY; without even the implied warranty of
14 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 : General Public License for more details.
16 :
17 : You should have received a copy of the GNU General Public License
18 : along with GCC; see the file COPYING3. If not see
19 : <http://www.gnu.org/licenses/>. */
20 :
21 : #include "config.h"
22 : #define INCLUDE_MUTEX
23 : #define INCLUDE_DLFCN_H
24 : #include "libgccjit.h"
25 : #include "system.h"
26 : #include "coretypes.h"
27 : #include "target.h"
28 : #include "tree.h"
29 : #include "stringpool.h"
30 : #include "cgraph.h"
31 : #include "dumpfile.h"
32 : #include "toplev.h"
33 : #include "tree-cfg.h"
34 : #include "convert.h"
35 : #include "gimple-expr.h"
36 : #include "stor-layout.h"
37 : #include "print-tree.h"
38 : #include "gimplify.h"
39 : #include "gcc-driver-name.h"
40 : #include "attribs.h"
41 : #include "context.h"
42 : #include "fold-const.h"
43 : #include "opt-suggestions.h"
44 : #include "gcc.h"
45 : #include "diagnostic.h"
46 : #include "stmt.h"
47 : #include "realmpfr.h"
48 :
49 : #include "jit-playback.h"
50 : #include "jit-result.h"
51 : #include "jit-builtins.h"
52 : #include "jit-tempdir.h"
53 : #include "jit-target.h"
54 :
55 : #ifdef _WIN32
56 : #include "jit-w32.h"
57 : #endif
58 :
59 : /* Compare with gcc/c-family/c-common.h: DECL_C_BIT_FIELD,
60 : SET_DECL_C_BIT_FIELD.
61 : These are redefined here to avoid depending from the C frontend. */
62 : #define DECL_JIT_BIT_FIELD(NODE) \
63 : (DECL_LANG_FLAG_4 (FIELD_DECL_CHECK (NODE)) == 1)
64 : #define SET_DECL_JIT_BIT_FIELD(NODE) \
65 : (DECL_LANG_FLAG_4 (FIELD_DECL_CHECK (NODE)) = 1)
66 :
67 : /* gcc::jit::playback::context::build_cast uses the convert.h API,
68 : which in turn requires the frontend to provide a "convert"
69 : function, apparently as a fallback for casts that can be simplified
70 : (truncation, extension). */
71 : extern tree convert (tree type, tree expr);
72 :
73 : tree
74 20 : convert (tree dst_type, tree expr)
75 : {
76 20 : tree t_ret = NULL;
77 20 : t_ret = targetm.convert_to_type (dst_type, expr);
78 20 : if (t_ret)
79 : return t_ret;
80 20 : switch (TREE_CODE (dst_type))
81 : {
82 20 : case INTEGER_TYPE:
83 20 : case ENUMERAL_TYPE:
84 20 : return fold (convert_to_integer (dst_type, expr));
85 :
86 0 : default:
87 0 : gcc_assert (gcc::jit::active_playback_ctxt);
88 0 : gcc::jit::active_playback_ctxt->add_error (NULL, "unhandled conversion");
89 0 : fprintf (stderr, "input expression:\n");
90 0 : debug_tree (expr);
91 0 : fprintf (stderr, "requested type:\n");
92 0 : debug_tree (dst_type);
93 0 : return error_mark_node;
94 : }
95 : }
96 :
97 : namespace gcc {
98 : namespace jit {
99 :
100 : /**********************************************************************
101 : Playback.
102 : **********************************************************************/
103 :
104 : /* Fold a readonly non-volatile variable with an initial constant value,
105 : to that value.
106 :
107 : Otherwise return the argument unchanged.
108 :
109 : This fold is needed for setting a variable's DECL_INITIAL to the value
110 : of a const variable. The c-frontend does this in its own special
111 : fold (), so we lift this part out and do it explicitly where there is a
112 : potential for variables to be used as rvalues. */
113 : static tree
114 15801 : fold_const_var (tree node)
115 : {
116 : /* See c_fully_fold_internal in c-fold.cc and decl_constant_value_1
117 : in c-typeck.cc. */
118 15801 : if (VAR_P (node)
119 5053 : && TREE_READONLY (node)
120 65 : && !TREE_THIS_VOLATILE (node)
121 65 : && DECL_INITIAL (node) != NULL_TREE
122 : /* "This is invalid if initial value is not constant.
123 : If it has either a function call, a memory reference,
124 : or a variable, then re-evaluating it could give different
125 : results." */
126 15861 : && TREE_CONSTANT (DECL_INITIAL (node)))
127 : {
128 60 : tree ret = DECL_INITIAL (node);
129 : /* "Avoid unwanted tree sharing between the initializer and current
130 : function's body where the tree can be modified e.g. by the
131 : gimplifier." */
132 60 : if (TREE_STATIC (node))
133 60 : ret = unshare_expr (ret);
134 :
135 : return ret;
136 : }
137 :
138 : return node;
139 : }
140 :
141 : /* Build a STRING_CST tree for STR, or return NULL if it is NULL.
142 : The TREE_TYPE is not initialized. */
143 :
144 : static tree
145 350 : build_string (const char *str)
146 : {
147 350 : if (str)
148 270 : return ::build_string (strlen (str), str);
149 : else
150 : return NULL_TREE;
151 : }
152 :
153 : /* The constructor for gcc::jit::playback::context. */
154 :
155 1381 : playback::context::context (recording::context *ctxt)
156 : : log_user (ctxt->get_logger ()),
157 1381 : m_recording_ctxt (ctxt),
158 1381 : m_tempdir (NULL),
159 1381 : m_const_char_ptr (NULL)
160 : {
161 1381 : JIT_LOG_SCOPE (get_logger ());
162 1381 : m_functions.create (0);
163 1381 : m_globals.create (0);
164 1381 : m_source_files.create (0);
165 1381 : m_cached_locations.create (0);
166 1381 : }
167 :
168 : /* The destructor for gcc::jit::playback::context. */
169 :
170 1381 : playback::context::~context ()
171 : {
172 1381 : JIT_LOG_SCOPE (get_logger ());
173 :
174 : /* Normally the playback::context is responsible for cleaning up the
175 : tempdir (including "fake.so" within the filesystem).
176 :
177 : In the normal case, clean it up now.
178 :
179 : However m_tempdir can be NULL if the context has handed over
180 : responsibility for the tempdir cleanup to the jit::result object, so
181 : that the cleanup can be delayed (see PR jit/64206). If that's the
182 : case this "delete NULL;" is a no-op. */
183 1381 : delete m_tempdir;
184 :
185 1381 : m_functions.release ();
186 1381 : }
187 :
188 : /* A playback::context can reference GC-managed pointers. Mark them
189 : ("by hand", rather than by gengtype).
190 :
191 : This is called on the active playback context (if any) by the
192 : my_ggc_walker hook in the jit_root_table in dummy-frontend.cc. */
193 :
194 : void
195 671447 : playback::context::
196 : gt_ggc_mx ()
197 : {
198 671447 : int i;
199 671447 : function *func;
200 66816911 : FOR_EACH_VEC_ELT (m_functions, i, func)
201 : {
202 66145464 : if (ggc_test_and_set_mark (func))
203 66145464 : func->gt_ggc_mx ();
204 : }
205 671447 : }
206 :
207 : /* Given an enum gcc_jit_types value, get a "tree" type. */
208 :
209 : tree
210 8759 : playback::context::
211 : get_tree_node_for_type (enum gcc_jit_types type_)
212 : {
213 8759 : switch (type_)
214 : {
215 1341 : case GCC_JIT_TYPE_VOID:
216 1341 : return void_type_node;
217 :
218 85 : case GCC_JIT_TYPE_VOID_PTR:
219 85 : return ptr_type_node;
220 :
221 677 : case GCC_JIT_TYPE_BOOL:
222 677 : return boolean_type_node;
223 :
224 146 : case GCC_JIT_TYPE_CHAR:
225 146 : return char_type_node;
226 35 : case GCC_JIT_TYPE_SIGNED_CHAR:
227 35 : return signed_char_type_node;
228 46 : case GCC_JIT_TYPE_UNSIGNED_CHAR:
229 46 : return unsigned_char_type_node;
230 :
231 45 : case GCC_JIT_TYPE_SHORT:
232 45 : return short_integer_type_node;
233 30 : case GCC_JIT_TYPE_UNSIGNED_SHORT:
234 30 : return short_unsigned_type_node;
235 :
236 122 : case GCC_JIT_TYPE_CONST_CHAR_PTR:
237 122 : return m_const_char_ptr;
238 :
239 1371 : case GCC_JIT_TYPE_INT:
240 1371 : return integer_type_node;
241 1341 : case GCC_JIT_TYPE_UNSIGNED_INT:
242 1341 : return unsigned_type_node;
243 :
244 20 : case GCC_JIT_TYPE_UINT8_T:
245 20 : return unsigned_intQI_type_node;
246 20 : case GCC_JIT_TYPE_UINT16_T:
247 20 : return uint16_type_node;
248 20 : case GCC_JIT_TYPE_UINT32_T:
249 20 : return uint32_type_node;
250 20 : case GCC_JIT_TYPE_UINT64_T:
251 20 : return uint64_type_node;
252 20 : case GCC_JIT_TYPE_UINT128_T:
253 20 : if (targetm.scalar_mode_supported_p (TImode))
254 20 : return uint128_type_node;
255 :
256 0 : add_error (NULL, "gcc_jit_types value unsupported on this target: %i",
257 : type_);
258 0 : return NULL;
259 :
260 20 : case GCC_JIT_TYPE_INT8_T:
261 20 : return intQI_type_node;
262 20 : case GCC_JIT_TYPE_INT16_T:
263 20 : return intHI_type_node;
264 20 : case GCC_JIT_TYPE_INT32_T:
265 20 : return intSI_type_node;
266 20 : case GCC_JIT_TYPE_INT64_T:
267 20 : return intDI_type_node;
268 20 : case GCC_JIT_TYPE_INT128_T:
269 20 : if (targetm.scalar_mode_supported_p (TImode))
270 20 : return intTI_type_node;
271 :
272 0 : add_error (NULL, "gcc_jit_types value unsupported on this target: %i",
273 : type_);
274 0 : return NULL;
275 :
276 83 : case GCC_JIT_TYPE_LONG:
277 83 : return long_integer_type_node;
278 1341 : case GCC_JIT_TYPE_UNSIGNED_LONG:
279 1341 : return long_unsigned_type_node;
280 :
281 33 : case GCC_JIT_TYPE_LONG_LONG:
282 33 : return long_long_integer_type_node;
283 1341 : case GCC_JIT_TYPE_UNSIGNED_LONG_LONG:
284 1341 : return long_long_unsigned_type_node;
285 :
286 130 : case GCC_JIT_TYPE_FLOAT:
287 130 : return float_type_node;
288 5 : case GCC_JIT_TYPE_BFLOAT16:
289 : #ifndef HAVE_BFmode
290 : add_error (NULL, "gcc_jit_types value unsupported on this target: %i",
291 : type_);
292 : #endif
293 5 : return bfloat16_type_node;
294 147 : case GCC_JIT_TYPE_DOUBLE:
295 147 : return double_type_node;
296 20 : case GCC_JIT_TYPE_LONG_DOUBLE:
297 20 : return long_double_type_node;
298 20 : case GCC_JIT_TYPE_FLOAT16:
299 20 : if (float16_type_node == NULL || TYPE_PRECISION (float16_type_node) != 16)
300 : {
301 0 : add_error (NULL, "gcc_jit_types value unsupported on this target: %i",
302 : type_);
303 0 : return NULL;
304 : }
305 : return float16_type_node;
306 30 : case GCC_JIT_TYPE_FLOAT32:
307 30 : if (float32_type_node == NULL || TYPE_PRECISION (float32_type_node) != 32)
308 : {
309 0 : add_error (NULL, "gcc_jit_types value unsupported on this target: %i",
310 : type_);
311 0 : return NULL;
312 : }
313 : return float32_type_node;
314 20 : case GCC_JIT_TYPE_FLOAT64:
315 20 : if (float64_type_node == NULL || TYPE_PRECISION (float64_type_node) != 64)
316 : {
317 0 : add_error (NULL, "gcc_jit_types value unsupported on this target: %i",
318 : type_);
319 0 : return NULL;
320 : }
321 : return float64_type_node;
322 20 : case GCC_JIT_TYPE_FLOAT128:
323 20 : if (float128_type_node == NULL
324 20 : || TYPE_PRECISION (float128_type_node) != 128)
325 : {
326 0 : add_error (NULL, "gcc_jit_types value unsupported on this target: %i",
327 : type_);
328 0 : return NULL;
329 : }
330 : return float128_type_node;
331 :
332 80 : case GCC_JIT_TYPE_SIZE_T:
333 80 : return size_type_node;
334 :
335 20 : case GCC_JIT_TYPE_FILE_PTR:
336 20 : return fileptr_type_node;
337 :
338 0 : case GCC_JIT_TYPE_COMPLEX_FLOAT:
339 0 : return complex_float_type_node;
340 30 : case GCC_JIT_TYPE_COMPLEX_DOUBLE:
341 30 : return complex_double_type_node;
342 0 : case GCC_JIT_TYPE_COMPLEX_LONG_DOUBLE:
343 0 : return complex_long_double_type_node;
344 : }
345 :
346 0 : add_error (NULL, "unrecognized (enum gcc_jit_types) value: %i",
347 : type_);
348 :
349 0 : return NULL;
350 : }
351 :
352 : /* Construct a playback::type instance (wrapping a tree) for the given
353 : enum value. */
354 :
355 : playback::type *
356 8759 : playback::context::
357 : get_type (enum gcc_jit_types type_)
358 : {
359 8759 : tree type_node = get_tree_node_for_type (type_);
360 8759 : if (type_node == NULL)
361 : return NULL;
362 :
363 8759 : return new type (type_node);
364 : }
365 :
366 : void
367 5 : playback::context::
368 : set_output_ident (const char* ident)
369 : {
370 5 : targetm.asm_out.output_ident (ident);
371 5 : }
372 :
373 : /* Construct a playback::type instance (wrapping a tree) for the given
374 : array type. */
375 :
376 : playback::type *
377 445 : playback::context::
378 : new_array_type (playback::location *loc,
379 : playback::type *element_type,
380 : uint64_t num_elements)
381 : {
382 445 : gcc_assert (element_type);
383 :
384 445 : tree t = build_array_type_nelts (element_type->as_tree (),
385 445 : num_elements);
386 445 : layout_type (t);
387 :
388 445 : if (loc)
389 0 : set_tree_location (t, loc);
390 :
391 445 : return new type (t);
392 : }
393 :
394 : /* Construct a playback::field instance (wrapping a tree). */
395 :
396 : playback::field *
397 2248 : playback::context::
398 : new_field (location *loc,
399 : type *type,
400 : const char *name)
401 : {
402 2248 : gcc_assert (type);
403 2248 : gcc_assert (name);
404 :
405 : /* compare with c/c-decl.cc:grokfield and grokdeclarator. */
406 2248 : tree decl = build_decl (UNKNOWN_LOCATION, FIELD_DECL,
407 : get_identifier (name), type->as_tree ());
408 :
409 2248 : if (loc)
410 0 : set_tree_location (decl, loc);
411 :
412 2248 : return new field (decl);
413 : }
414 :
415 : /* Construct a playback::bitfield instance (wrapping a tree). */
416 :
417 : playback::field *
418 120 : playback::context::
419 : new_bitfield (location *loc,
420 : type *type,
421 : int width,
422 : const char *name)
423 : {
424 120 : gcc_assert (type);
425 120 : gcc_assert (name);
426 120 : gcc_assert (width);
427 :
428 : /* compare with c/c-decl.cc:grokfield, grokdeclarator and
429 : check_bitfield_type_and_width. */
430 :
431 120 : tree tree_type = type->as_tree ();
432 120 : gcc_assert (INTEGRAL_TYPE_P (tree_type));
433 120 : tree tree_width = build_int_cst (integer_type_node, width);
434 120 : if (compare_tree_int (tree_width, TYPE_PRECISION (tree_type)) > 0)
435 : {
436 10 : add_error (
437 : loc,
438 : "width of bit-field %s (width: %i) is wider than its type (width: %i)",
439 5 : name, width, TYPE_PRECISION (tree_type));
440 5 : return NULL;
441 : }
442 :
443 115 : tree decl = build_decl (UNKNOWN_LOCATION, FIELD_DECL,
444 : get_identifier (name), type->as_tree ());
445 115 : DECL_NONADDRESSABLE_P (decl) = true;
446 115 : DECL_INITIAL (decl) = tree_width;
447 115 : SET_DECL_JIT_BIT_FIELD (decl);
448 :
449 115 : if (loc)
450 0 : set_tree_location (decl, loc);
451 :
452 115 : return new field (decl);
453 : }
454 :
455 : /* Construct a playback::compound_type instance (wrapping a tree). */
456 :
457 : playback::compound_type *
458 777 : playback::context::
459 : new_compound_type (location *loc,
460 : const char *name,
461 : bool is_struct) /* else is union */
462 : {
463 777 : gcc_assert (name);
464 :
465 : /* Compare with c/c-decl.cc: start_struct. */
466 :
467 877 : tree t = make_node (is_struct ? RECORD_TYPE : UNION_TYPE);
468 777 : TYPE_NAME (t) = get_identifier (name);
469 777 : TYPE_SIZE (t) = 0;
470 :
471 777 : if (loc)
472 0 : set_tree_location (t, loc);
473 :
474 777 : return new compound_type (t);
475 : }
476 :
477 : void
478 737 : playback::compound_type::set_fields (const auto_vec<playback::field *> *fields)
479 : {
480 : /* Compare with c/c-decl.cc: finish_struct. */
481 737 : tree t = as_tree ();
482 :
483 737 : tree fieldlist = NULL;
484 3095 : for (unsigned i = 0; i < fields->length (); i++)
485 : {
486 2358 : field *f = (*fields)[i];
487 2358 : tree x = f->as_tree ();
488 2358 : DECL_CONTEXT (x) = t;
489 2358 : if (DECL_JIT_BIT_FIELD (x))
490 : {
491 110 : unsigned HOST_WIDE_INT width = tree_to_uhwi (DECL_INITIAL (x));
492 110 : DECL_SIZE (x) = bitsize_int (width);
493 110 : DECL_BIT_FIELD (x) = 1;
494 : }
495 2358 : fieldlist = chainon (x, fieldlist);
496 : }
497 737 : fieldlist = nreverse (fieldlist);
498 737 : TYPE_FIELDS (t) = fieldlist;
499 :
500 737 : layout_type (t);
501 737 : }
502 :
503 : /* Construct a playback::type instance (wrapping a tree) for a function
504 : type. */
505 :
506 : playback::type *
507 5674 : playback::context::
508 : new_function_type (type *return_type,
509 : const auto_vec<type *> *param_types,
510 : int is_variadic)
511 : {
512 5674 : int i;
513 5674 : type *param_type;
514 :
515 5674 : tree *arg_types = (tree *)xcalloc(param_types->length (), sizeof(tree*));
516 :
517 16001 : FOR_EACH_VEC_ELT (*param_types, i, param_type)
518 4653 : arg_types[i] = param_type->as_tree ();
519 :
520 5674 : tree fn_type;
521 5674 : if (is_variadic)
522 20 : fn_type =
523 20 : build_varargs_function_type_array (return_type->as_tree (),
524 20 : param_types->length (),
525 : arg_types);
526 : else
527 11308 : fn_type = build_function_type_array (return_type->as_tree (),
528 9967 : param_types->length (),
529 : arg_types);
530 5674 : free (arg_types);
531 :
532 5674 : return new type (fn_type);
533 : }
534 :
535 : /* Construct a playback::param instance (wrapping a tree). */
536 :
537 : playback::param *
538 18208 : playback::context::
539 : new_param (location *loc,
540 : type *type,
541 : const char *name)
542 : {
543 18208 : gcc_assert (type);
544 18208 : gcc_assert (name);
545 18208 : tree inner = build_decl (UNKNOWN_LOCATION, PARM_DECL,
546 : get_identifier (name), type->as_tree ());
547 18208 : if (loc)
548 34 : set_tree_location (inner, loc);
549 :
550 18208 : return new param (this, inner);
551 : }
552 :
553 55 : const char* fn_attribute_to_string (gcc_jit_fn_attribute attr)
554 : {
555 55 : switch (attr)
556 : {
557 : case GCC_JIT_FN_ATTRIBUTE_ALIAS:
558 : return "alias";
559 5 : case GCC_JIT_FN_ATTRIBUTE_ALWAYS_INLINE:
560 5 : return "always_inline";
561 : case GCC_JIT_FN_ATTRIBUTE_INLINE:
562 : return NULL;
563 5 : case GCC_JIT_FN_ATTRIBUTE_NOINLINE:
564 5 : return "noinline";
565 0 : case GCC_JIT_FN_ATTRIBUTE_TARGET:
566 0 : return "target";
567 5 : case GCC_JIT_FN_ATTRIBUTE_USED:
568 5 : return "used";
569 0 : case GCC_JIT_FN_ATTRIBUTE_VISIBILITY:
570 0 : return "visibility";
571 5 : case GCC_JIT_FN_ATTRIBUTE_COLD:
572 5 : return "cold";
573 0 : case GCC_JIT_FN_ATTRIBUTE_RETURNS_TWICE:
574 0 : return "returns_twice";
575 5 : case GCC_JIT_FN_ATTRIBUTE_PURE:
576 5 : return "pure";
577 5 : case GCC_JIT_FN_ATTRIBUTE_CONST:
578 5 : return "const";
579 5 : case GCC_JIT_FN_ATTRIBUTE_WEAK:
580 5 : return "weak";
581 5 : case GCC_JIT_FN_ATTRIBUTE_NONNULL:
582 5 : return "nonnull";
583 0 : case GCC_JIT_FN_ATTRIBUTE_ARM_CMSE_NONSECURE_CALL:
584 0 : return "cmse_nonsecure_call";
585 0 : case GCC_JIT_FN_ATTRIBUTE_ARM_CMSE_NONSECURE_ENTRY:
586 0 : return "cmse_nonsecure_entry";
587 0 : case GCC_JIT_FN_ATTRIBUTE_ARM_PCS:
588 0 : return "pcs";
589 : case GCC_JIT_FN_ATTRIBUTE_AVR_INTERRUPT:
590 : return "interrupt";
591 0 : case GCC_JIT_FN_ATTRIBUTE_AVR_NOBLOCK:
592 0 : return "noblock";
593 0 : case GCC_JIT_FN_ATTRIBUTE_AVR_SIGNAL:
594 0 : return "signal";
595 0 : case GCC_JIT_FN_ATTRIBUTE_GCN_AMDGPU_HSA_KERNEL:
596 0 : return "amdgpu_hsa_kernel";
597 : case GCC_JIT_FN_ATTRIBUTE_MSP430_INTERRUPT:
598 : return "interrupt";
599 0 : case GCC_JIT_FN_ATTRIBUTE_NVPTX_KERNEL:
600 0 : return "kernel";
601 : case GCC_JIT_FN_ATTRIBUTE_RISCV_INTERRUPT:
602 : return "interrupt";
603 0 : case GCC_JIT_FN_ATTRIBUTE_X86_FAST_CALL:
604 0 : return "fastcall";
605 : case GCC_JIT_FN_ATTRIBUTE_X86_INTERRUPT:
606 : return "interrupt";
607 5 : case GCC_JIT_FN_ATTRIBUTE_X86_MS_ABI:
608 5 : return "ms_abi";
609 0 : case GCC_JIT_FN_ATTRIBUTE_X86_STDCALL:
610 0 : return "stdcall";
611 0 : case GCC_JIT_FN_ATTRIBUTE_X86_SYSV_ABI:
612 0 : return "sysv_abi";
613 0 : case GCC_JIT_FN_ATTRIBUTE_X86_THIS_CALL:
614 0 : return "thiscall";
615 : case GCC_JIT_FN_ATTRIBUTE_MAX:
616 : return NULL;
617 : }
618 : return NULL;
619 : }
620 :
621 5 : const char* variable_attribute_to_string (gcc_jit_variable_attribute attr)
622 : {
623 5 : switch (attr)
624 : {
625 : case GCC_JIT_VARIABLE_ATTRIBUTE_VISIBILITY:
626 : return "visibility";
627 : case GCC_JIT_VARIABLE_ATTRIBUTE_MAX:
628 : return NULL;
629 : }
630 : return NULL;
631 : }
632 :
633 : /* Construct a playback::function instance. */
634 :
635 : playback::function *
636 18167 : playback::context::
637 : new_function (location *loc,
638 : enum gcc_jit_function_kind kind,
639 : type *return_type,
640 : const char *name,
641 : const auto_vec<param *> *params,
642 : int is_variadic,
643 : enum built_in_function builtin_id,
644 : const std::vector<gcc_jit_fn_attribute> &attributes,
645 : const std::vector<std::pair<gcc_jit_fn_attribute,
646 : std::string>> &string_attributes,
647 : const std::vector<std::pair<gcc_jit_fn_attribute,
648 : std::vector<int>>>
649 : &int_array_attributes,
650 : bool is_target_builtin)
651 : {
652 18167 : int i;
653 18167 : param *param;
654 :
655 : //can return_type be NULL?
656 18167 : gcc_assert (name);
657 :
658 18167 : tree *arg_types = (tree *)xcalloc(params->length (), sizeof(tree*));
659 54542 : FOR_EACH_VEC_ELT (*params, i, param)
660 18208 : arg_types[i] = TREE_TYPE (param->as_tree ());
661 :
662 18167 : tree fn_type;
663 18167 : if (is_variadic)
664 254 : fn_type = build_varargs_function_type_array (return_type->as_tree (),
665 189 : params->length (), arg_types);
666 : else
667 36080 : fn_type = build_function_type_array (return_type->as_tree (),
668 33598 : params->length (), arg_types);
669 18167 : free (arg_types);
670 :
671 : /* FIXME: this uses input_location: */
672 18167 : tree fndecl = build_fn_decl (name, fn_type);
673 :
674 18167 : if (loc)
675 61 : set_tree_location (fndecl, loc);
676 :
677 18167 : tree resdecl = build_decl (UNKNOWN_LOCATION, RESULT_DECL,
678 : NULL_TREE, return_type->as_tree ());
679 18167 : DECL_ARTIFICIAL (resdecl) = 1;
680 18167 : DECL_IGNORED_P (resdecl) = 1;
681 18167 : DECL_RESULT (fndecl) = resdecl;
682 18167 : DECL_CONTEXT (resdecl) = fndecl;
683 :
684 18167 : tree fn_attributes = NULL_TREE;
685 :
686 18167 : if (is_target_builtin)
687 : {
688 10 : tree *decl = target_builtins.get (name);
689 10 : if (decl != NULL)
690 10 : fndecl = *decl;
691 : else
692 0 : add_error (loc, "cannot find target builtin %s", name);
693 : }
694 :
695 18167 : if (builtin_id)
696 : {
697 13715 : gcc_assert (loc == NULL);
698 13715 : DECL_SOURCE_LOCATION (fndecl) = BUILTINS_LOCATION;
699 :
700 13715 : built_in_class fclass = builtins_manager::get_class (builtin_id);
701 13715 : set_decl_built_in_function (fndecl, fclass, builtin_id);
702 13715 : set_builtin_decl (builtin_id, fndecl,
703 : builtins_manager::implicit_p (builtin_id));
704 :
705 13715 : builtins_manager *bm = get_builtins_manager ();
706 13715 : tree attrs = bm->get_attrs_tree (builtin_id);
707 13715 : if (attrs)
708 13715 : decl_attributes (&fndecl, attrs, ATTR_FLAG_BUILT_IN);
709 : else
710 0 : decl_attributes (&fndecl, NULL_TREE, 0);
711 : }
712 :
713 18167 : if (kind != GCC_JIT_FUNCTION_IMPORTED)
714 : {
715 : tree param_decl_list = NULL;
716 9460 : FOR_EACH_VEC_ELT (*params, i, param)
717 : {
718 5249 : param_decl_list = chainon (param->as_tree (), param_decl_list);
719 : }
720 :
721 : /* The param list was created in reverse order; fix it: */
722 4211 : param_decl_list = nreverse (param_decl_list);
723 :
724 4211 : tree t;
725 13671 : for (t = param_decl_list; t; t = DECL_CHAIN (t))
726 : {
727 5249 : DECL_CONTEXT (t) = fndecl;
728 5249 : DECL_ARG_TYPE (t) = TREE_TYPE (t);
729 : }
730 :
731 : /* Set it up on DECL_ARGUMENTS */
732 4211 : DECL_ARGUMENTS(fndecl) = param_decl_list;
733 : }
734 :
735 4211 : if (kind == GCC_JIT_FUNCTION_ALWAYS_INLINE)
736 : {
737 20 : DECL_DECLARED_INLINE_P (fndecl) = 1;
738 :
739 : /* Add attribute "always_inline": */
740 20 : fn_attributes = tree_cons (get_identifier ("always_inline"),
741 : NULL,
742 : fn_attributes);
743 : }
744 :
745 : /* All attributes need to be declared in `dummy-frontend.cc` and more
746 : specifically in `jit_attribute_table`. */
747 18212 : for (auto attr: attributes)
748 : {
749 45 : if (attr == GCC_JIT_FN_ATTRIBUTE_INLINE)
750 5 : DECL_DECLARED_INLINE_P (fndecl) = 1;
751 :
752 45 : const char* attribute = fn_attribute_to_string (attr);
753 45 : if (attribute)
754 : {
755 40 : tree ident = get_identifier (attribute);
756 40 : fn_attributes = tree_cons (ident, NULL_TREE, fn_attributes);
757 : }
758 : }
759 :
760 18172 : for (auto attr: string_attributes)
761 : {
762 5 : gcc_jit_fn_attribute& name = std::get<0>(attr);
763 5 : std::string& value = std::get<1>(attr);
764 10 : tree attribute_value = build_tree_list (NULL_TREE,
765 5 : ::build_string (value.length () + 1, value.c_str ()));
766 5 : const char* attribute = fn_attribute_to_string (name);
767 5 : tree ident = attribute ? get_identifier (attribute) : NULL;
768 :
769 5 : if (ident)
770 5 : fn_attributes = tree_cons (ident, attribute_value, fn_attributes);
771 5 : }
772 :
773 18172 : for (auto attr: int_array_attributes)
774 : {
775 5 : gcc_jit_fn_attribute& name = std::get<0>(attr);
776 5 : std::vector<int>& values = std::get<1>(attr);
777 :
778 5 : const char* attribute = fn_attribute_to_string (name);
779 5 : tree ident = attribute ? get_identifier (attribute) : NULL;
780 :
781 5 : if (!ident)
782 0 : continue;
783 :
784 5 : tree tree_list = NULL_TREE;
785 5 : tree *p_tree_list = &tree_list;
786 10 : for (auto value : values)
787 : {
788 5 : tree int_value = build_int_cst (integer_type_node, value);
789 5 : *p_tree_list = build_tree_list (NULL, int_value);
790 5 : p_tree_list = &TREE_CHAIN (*p_tree_list);
791 : }
792 5 : fn_attributes = tree_cons (ident, tree_list, fn_attributes);
793 5 : }
794 :
795 18167 : decl_attributes (&fndecl, fn_attributes, 0);
796 18167 : function *func = new function (this, fndecl, kind);
797 18167 : m_functions.safe_push (func);
798 18167 : return func;
799 : }
800 :
801 : /* In use by new_global and new_global_initialized. */
802 :
803 : tree
804 1376 : playback::context::
805 : global_new_decl (location *loc,
806 : enum gcc_jit_global_kind kind,
807 : type *type,
808 : const char *name,
809 : enum global_var_flags flags,
810 : const std::vector<std::pair<gcc_jit_variable_attribute,
811 : std::string>> &attributes,
812 : bool readonly)
813 : {
814 1376 : gcc_assert (type);
815 1376 : gcc_assert (name);
816 :
817 1376 : tree type_tree = type->as_tree ();
818 :
819 1376 : tree inner = build_decl (UNKNOWN_LOCATION, VAR_DECL,
820 : get_identifier (name),
821 : type_tree);
822 :
823 1376 : TREE_PUBLIC (inner) = (kind != GCC_JIT_GLOBAL_INTERNAL);
824 :
825 :
826 1376 : int will_be_init = flags & (GLOBAL_VAR_FLAGS_WILL_BE_RVAL_INIT |
827 : GLOBAL_VAR_FLAGS_WILL_BE_BLOB_INIT);
828 :
829 : /* A VAR_DECL with DECL_INITIAL will not end up in .common section. */
830 1376 : if (!will_be_init)
831 241 : DECL_COMMON (inner) = 1;
832 :
833 1376 : switch (kind)
834 : {
835 0 : default:
836 0 : gcc_unreachable ();
837 :
838 1190 : case GCC_JIT_GLOBAL_EXPORTED:
839 1190 : TREE_STATIC (inner) = 1;
840 1190 : break;
841 :
842 126 : case GCC_JIT_GLOBAL_INTERNAL:
843 126 : TREE_STATIC (inner) = 1;
844 126 : break;
845 :
846 60 : case GCC_JIT_GLOBAL_IMPORTED:
847 60 : DECL_EXTERNAL (inner) = 1;
848 60 : break;
849 : }
850 :
851 1376 : if (TYPE_READONLY (type_tree) || readonly)
852 190 : TREE_READONLY (inner) = 1;
853 :
854 1376 : if (loc)
855 5 : set_tree_location (inner, loc);
856 :
857 1376 : set_variable_string_attribute (attributes, inner);
858 :
859 1376 : return inner;
860 : }
861 :
862 : void
863 3968 : playback::
864 : set_variable_string_attribute (
865 : const std::vector<std::pair<gcc_jit_variable_attribute,
866 : std::string>> &string_attributes,
867 : tree decl)
868 : {
869 3968 : tree var_attributes = NULL_TREE;
870 3973 : for (auto attr: string_attributes)
871 : {
872 5 : gcc_jit_variable_attribute& name = std::get<0>(attr);
873 5 : std::string& value = std::get<1>(attr);
874 10 : tree attribute_value = build_tree_list (NULL_TREE,
875 5 : ::build_string (value.length () + 1, value.c_str ()));
876 5 : tree ident = get_identifier (variable_attribute_to_string (name));
877 5 : if (ident)
878 5 : var_attributes = tree_cons (ident, attribute_value, var_attributes);
879 5 : }
880 3968 : decl_attributes (&decl, var_attributes, 0);
881 3968 : }
882 :
883 : /* In use by new_global and new_global_initialized. */
884 :
885 : playback::lvalue *
886 1376 : playback::context::
887 : global_finalize_lvalue (tree inner)
888 : {
889 1376 : m_globals.safe_push (inner);
890 :
891 1376 : return new lvalue (this, inner);
892 : }
893 :
894 : /* Construct a playback::lvalue instance (wrapping a tree). */
895 :
896 : playback::lvalue *
897 1361 : playback::context::
898 : new_global (location *loc,
899 : enum gcc_jit_global_kind kind,
900 : type *type,
901 : const char *name,
902 : enum global_var_flags flags,
903 : const std::vector<std::pair<gcc_jit_variable_attribute,
904 : std::string>> &attributes,
905 : bool readonly)
906 : {
907 1361 : tree inner =
908 1361 : global_new_decl (loc, kind, type, name, flags, attributes, readonly);
909 :
910 1361 : return global_finalize_lvalue (inner);
911 : }
912 :
913 : void
914 1120 : playback::context::
915 : global_set_init_rvalue (lvalue* variable,
916 : rvalue* init)
917 : {
918 1120 : tree inner = variable->as_tree ();
919 :
920 : /* We need to fold all expressions as much as possible. The code
921 : for a DECL_INITIAL only handles some operations,
922 : etc addition, minus, 'address of'. See output_addressed_constants ()
923 : in varasm.cc. */
924 1120 : tree init_tree = init->as_tree ();
925 1120 : tree folded = fold_const_var (init_tree);
926 :
927 1120 : if (!TREE_CONSTANT (folded))
928 : {
929 15 : tree name = DECL_NAME (inner);
930 :
931 15 : if (name != NULL_TREE)
932 15 : add_error (NULL,
933 : "unable to convert initial value for the global variable %s"
934 : " to a compile-time constant",
935 15 : IDENTIFIER_POINTER (name));
936 : else
937 0 : add_error (NULL,
938 : "unable to convert initial value for global variable"
939 : " to a compile-time constant");
940 : return;
941 : }
942 :
943 1105 : DECL_INITIAL (inner) = folded;
944 : }
945 :
946 : playback::rvalue *
947 900 : playback::context::
948 : new_ctor (location *loc,
949 : type *type,
950 : const auto_vec<field*> *fields,
951 : const auto_vec<rvalue*> *rvalues)
952 : {
953 900 : tree type_tree = type->as_tree ();
954 :
955 : /* Handle empty ctors first. I.e. set everything to 0. */
956 900 : if (rvalues->length () == 0)
957 80 : return new rvalue (this, build_constructor (type_tree, NULL));
958 :
959 : /* Handle arrays (and return). */
960 820 : if (TREE_CODE (type_tree) == ARRAY_TYPE)
961 : {
962 320 : int n = rvalues->length ();
963 : /* The vec for the constructor node. */
964 320 : vec<constructor_elt, va_gc> *v = NULL;
965 320 : vec_alloc (v, n);
966 :
967 1680 : for (int i = 0; i < n; i++)
968 : {
969 1040 : rvalue *rv = (*rvalues)[i];
970 : /* null rvalues indicate that the element should be zeroed. */
971 1040 : if (rv)
972 940 : CONSTRUCTOR_APPEND_ELT (v,
973 : build_int_cst (size_type_node, i),
974 : rv->as_tree ());
975 : else
976 1140 : CONSTRUCTOR_APPEND_ELT (v,
977 : build_int_cst (size_type_node, i),
978 : build_zero_cst (TREE_TYPE (type_tree)));
979 : }
980 :
981 320 : tree ctor = build_constructor (type_tree, v);
982 :
983 320 : if (loc)
984 0 : set_tree_location (ctor, loc);
985 :
986 320 : return new rvalue (this, ctor);
987 : }
988 :
989 : /* Handle structs and unions. */
990 500 : int n = fields->length ();
991 :
992 : /* The vec for the constructor node. */
993 500 : vec<constructor_elt, va_gc> *v = NULL;
994 500 : vec_alloc (v, n);
995 :
996 : /* Iterate over the fields, building initializations. */
997 1820 : for (int i = 0;i < n; i++)
998 : {
999 820 : tree field = (*fields)[i]->as_tree ();
1000 820 : rvalue *rv = (*rvalues)[i];
1001 : /* If the value is NULL, it means we should zero the field. */
1002 820 : if (rv)
1003 700 : CONSTRUCTOR_APPEND_ELT (v, field, rv->as_tree ());
1004 : else
1005 : {
1006 120 : tree zero_cst = build_zero_cst (TREE_TYPE (field));
1007 940 : CONSTRUCTOR_APPEND_ELT (v, field, zero_cst);
1008 : }
1009 : }
1010 :
1011 500 : tree ctor = build_constructor (type_tree, v);
1012 :
1013 500 : if (loc)
1014 0 : set_tree_location (ctor, loc);
1015 :
1016 500 : return new rvalue (this, build_constructor (type_tree, v));
1017 : }
1018 :
1019 : /* Fill 'constructor_elements' with the memory content of
1020 : 'initializer'. Each element of the initializer is of the size of
1021 : type T. In use by new_global_initialized.*/
1022 :
1023 : template<typename T>
1024 : static void
1025 15 : load_blob_in_ctor (vec<constructor_elt, va_gc> *&constructor_elements,
1026 : size_t num_elem,
1027 : const void *initializer)
1028 : {
1029 : /* Loosely based on 'output_init_element' c-typeck.cc:9691. */
1030 15 : const T *p = (const T *)initializer;
1031 15 : tree node = make_unsigned_type (BITS_PER_UNIT * sizeof (T));
1032 20555 : for (size_t i = 0; i < num_elem; i++)
1033 : {
1034 20540 : constructor_elt celt =
1035 20540 : { build_int_cst (long_unsigned_type_node, i),
1036 20540 : build_int_cst (node, p[i]) };
1037 20540 : vec_safe_push (constructor_elements, celt);
1038 : }
1039 15 : }
1040 :
1041 : /* Construct an initialized playback::lvalue instance (wrapping a
1042 : tree). */
1043 :
1044 : playback::lvalue *
1045 15 : playback::context::
1046 : new_global_initialized (location *loc,
1047 : enum gcc_jit_global_kind kind,
1048 : type *type,
1049 : size_t element_size,
1050 : size_t initializer_num_elem,
1051 : const void *initializer,
1052 : const char *name,
1053 : enum global_var_flags flags,
1054 : const std::vector<std::pair<gcc_jit_variable_attribute,
1055 : std::string>> &attributes,
1056 : bool readonly)
1057 : {
1058 15 : tree inner = global_new_decl (loc, kind, type, name, flags, attributes, readonly);
1059 :
1060 15 : vec<constructor_elt, va_gc> *constructor_elements = NULL;
1061 :
1062 15 : switch (element_size)
1063 : {
1064 10 : case 1:
1065 10 : load_blob_in_ctor<uint8_t> (constructor_elements, initializer_num_elem,
1066 : initializer);
1067 10 : break;
1068 0 : case 2:
1069 0 : load_blob_in_ctor<uint16_t> (constructor_elements, initializer_num_elem,
1070 : initializer);
1071 0 : break;
1072 5 : case 4:
1073 5 : load_blob_in_ctor<uint32_t> (constructor_elements, initializer_num_elem,
1074 : initializer);
1075 5 : break;
1076 0 : case 8:
1077 0 : load_blob_in_ctor<uint64_t> (constructor_elements, initializer_num_elem,
1078 : initializer);
1079 0 : break;
1080 0 : default:
1081 : /* This function is serving on sizes returned by 'get_size',
1082 : these are all covered by the previous cases. */
1083 0 : gcc_unreachable ();
1084 : }
1085 : /* Compare with 'pop_init_level' c-typeck.cc:8780. */
1086 15 : tree ctor = build_constructor (type->as_tree (), constructor_elements);
1087 15 : constructor_elements = NULL;
1088 :
1089 : /* Compare with 'store_init_value' c-typeck.cc:7555. */
1090 15 : DECL_INITIAL (inner) = ctor;
1091 :
1092 15 : return global_finalize_lvalue (inner);
1093 : }
1094 :
1095 : /* Implementation of the various
1096 : gcc::jit::playback::context::new_rvalue_from_const <HOST_TYPE>
1097 : methods.
1098 : Each of these constructs a playback::rvalue instance (wrapping a tree).
1099 :
1100 : These specializations are required to be in the same namespace
1101 : as the template, hence we now have to enter the gcc::jit::playback
1102 : namespace. */
1103 :
1104 : namespace playback
1105 : {
1106 :
1107 : /* Specialization of making an rvalue from a const, for host <int>. */
1108 :
1109 : template <>
1110 : rvalue *
1111 7689 : context::
1112 : new_rvalue_from_const <int> (type *type,
1113 : int value)
1114 : {
1115 : // FIXME: type-checking, or coercion?
1116 7689 : tree inner_type = type->as_tree ();
1117 7689 : if (INTEGRAL_TYPE_P (inner_type))
1118 : {
1119 6902 : tree inner = build_int_cst (inner_type, value);
1120 6902 : return new rvalue (this, inner);
1121 : }
1122 : else
1123 : {
1124 787 : REAL_VALUE_TYPE real_value;
1125 787 : real_from_integer (&real_value, VOIDmode, value, SIGNED);
1126 787 : tree inner = build_real (inner_type, real_value);
1127 787 : return new rvalue (this, inner);
1128 : }
1129 : }
1130 :
1131 : /* Specialization of making an rvalue from a const, for host <long>. */
1132 :
1133 : template <>
1134 : rvalue *
1135 100 : context::
1136 : new_rvalue_from_const <long> (type *type,
1137 : long value)
1138 : {
1139 : // FIXME: type-checking, or coercion?
1140 100 : tree inner_type = type->as_tree ();
1141 100 : if (INTEGRAL_TYPE_P (inner_type))
1142 : {
1143 100 : tree inner = build_int_cst (inner_type, value);
1144 100 : return new rvalue (this, inner);
1145 : }
1146 : else
1147 : {
1148 0 : REAL_VALUE_TYPE real_value;
1149 0 : real_from_integer (&real_value, VOIDmode, value, SIGNED);
1150 0 : tree inner = build_real (inner_type, real_value);
1151 0 : return new rvalue (this, inner);
1152 : }
1153 : }
1154 :
1155 : /* Specialization of making an rvalue from a const, for host <double>. */
1156 :
1157 : template <>
1158 : rvalue *
1159 285 : context::
1160 : new_rvalue_from_const <double> (type *type,
1161 : double value)
1162 : {
1163 : // FIXME: type-checking, or coercion?
1164 285 : tree inner_type = type->as_tree ();
1165 :
1166 285 : mpfr_t mpf_value;
1167 :
1168 285 : mpfr_init2 (mpf_value, 64);
1169 285 : mpfr_set_d (mpf_value, value, MPFR_RNDN);
1170 :
1171 : /* We have a "double", we want a REAL_VALUE_TYPE.
1172 :
1173 : realmpfr.cc:real_from_mpfr. */
1174 285 : REAL_VALUE_TYPE real_value;
1175 285 : real_from_mpfr (&real_value, mpf_value, inner_type, MPFR_RNDN);
1176 285 : tree inner = build_real (inner_type, real_value);
1177 285 : return new rvalue (this, inner);
1178 : }
1179 :
1180 : /* Specialization of making an rvalue from a const, for host <void *>. */
1181 :
1182 : template <>
1183 : rvalue *
1184 185 : context::
1185 : new_rvalue_from_const <void *> (type *type,
1186 : void *value)
1187 : {
1188 185 : tree inner_type = type->as_tree ();
1189 : /* FIXME: how to ensure we have a wide enough type? */
1190 185 : tree inner = build_int_cstu (inner_type, (unsigned HOST_WIDE_INT)value);
1191 185 : return new rvalue (this, inner);
1192 : }
1193 :
1194 : /* We're done implementing the specializations of
1195 : gcc::jit::playback::context::new_rvalue_from_const <T>
1196 : so we can exit the gcc::jit::playback namespace. */
1197 :
1198 : } // namespace playback
1199 :
1200 : /* Construct a playback::rvalue instance (wrapping a tree). */
1201 :
1202 : playback::rvalue *
1203 20 : playback::context::
1204 : new_sizeof (type *type)
1205 : {
1206 20 : tree inner = TYPE_SIZE_UNIT (type->as_tree ());
1207 20 : return new rvalue (this, inner);
1208 : }
1209 :
1210 : /* Construct a playback::rvalue instance (wrapping a tree). */
1211 :
1212 : playback::rvalue *
1213 20 : playback::context::
1214 : new_alignof (type *type)
1215 : {
1216 20 : int alignment = TYPE_ALIGN (type->as_tree ()) / BITS_PER_UNIT;
1217 20 : tree inner = build_int_cst (integer_type_node, alignment);
1218 20 : return new rvalue (this, inner);
1219 : }
1220 :
1221 : /* Construct a playback::rvalue instance (wrapping a tree). */
1222 :
1223 : playback::rvalue *
1224 242 : playback::context::
1225 : new_string_literal (const char *value)
1226 : {
1227 : /* Compare with c-family/c-common.cc: fix_string_type. */
1228 242 : size_t len = strlen (value);
1229 242 : tree i_type = build_index_type (size_int (len));
1230 242 : tree a_type = build_array_type (char_type_node, i_type);
1231 : /* build_string len parameter must include NUL terminator when
1232 : building C strings. */
1233 242 : tree t_str = ::build_string (len + 1, value);
1234 242 : TREE_TYPE (t_str) = a_type;
1235 :
1236 : /* Convert to (const char*), loosely based on
1237 : c/c-typeck.cc: array_to_pointer_conversion,
1238 : by taking address of start of string. */
1239 242 : tree t_addr = build1 (ADDR_EXPR, m_const_char_ptr, t_str);
1240 :
1241 242 : return new rvalue (this, t_addr);
1242 : }
1243 :
1244 : /* Construct a playback::rvalue instance (wrapping a tree) for a
1245 : vector. */
1246 :
1247 : playback::rvalue *
1248 160 : playback::context::new_rvalue_from_vector (location *,
1249 : type *type,
1250 : const auto_vec<rvalue *> &elements)
1251 : {
1252 160 : vec<constructor_elt, va_gc> *v;
1253 320 : vec_alloc (v, elements.length ());
1254 960 : for (unsigned i = 0; i < elements.length (); ++i)
1255 640 : CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, elements[i]->as_tree ());
1256 160 : tree t_ctor = build_constructor (type->as_tree (), v);
1257 160 : return new rvalue (this, t_ctor);
1258 : }
1259 :
1260 : /* Construct a playback::rvalue instance (wrapping a tree) for a
1261 : vector perm. */
1262 :
1263 : playback::rvalue *
1264 20 : playback::context::new_rvalue_vector_perm (location *loc,
1265 : rvalue* elements1,
1266 : rvalue* elements2,
1267 : rvalue* mask)
1268 : {
1269 20 : tree t_elements1 = elements1->as_tree ();
1270 20 : tree t_elements2 = elements2->as_tree ();
1271 20 : tree t_mask = mask->as_tree ();
1272 :
1273 20 : tree t_vector_perm = build3 (VEC_PERM_EXPR, TREE_TYPE (t_elements1),
1274 : t_elements1, t_elements2, t_mask);
1275 20 : if (loc)
1276 0 : set_tree_location (t_vector_perm, loc);
1277 20 : return new rvalue (this, t_vector_perm);
1278 : }
1279 :
1280 : /* Coerce a tree expression into a boolean tree expression. */
1281 :
1282 : tree
1283 825 : playback::context::
1284 : as_truth_value (tree expr, location *loc)
1285 : {
1286 : /* Compare to c-typeck.cc:c_objc_common_truthvalue_conversion */
1287 825 : tree typed_zero = fold_build1 (CONVERT_EXPR,
1288 : TREE_TYPE (expr),
1289 : integer_zero_node);
1290 825 : if (loc)
1291 0 : set_tree_location (typed_zero, loc);
1292 :
1293 825 : tree type = TREE_TYPE (expr);
1294 825 : expr = fold_build2_loc (UNKNOWN_LOCATION,
1295 : NE_EXPR, type, expr, typed_zero);
1296 825 : if (loc)
1297 0 : set_tree_location (expr, loc);
1298 :
1299 825 : return expr;
1300 : }
1301 :
1302 : /* Add a "top-level" basic asm statement (i.e. one outside of any functions)
1303 : containing ASM_STMTS.
1304 :
1305 : Compare with c_parser_asm_definition. */
1306 :
1307 : void
1308 10 : playback::context::add_top_level_asm (const char *asm_stmts)
1309 : {
1310 10 : tree asm_str = build_string (asm_stmts);
1311 10 : symtab->finalize_toplevel_asm (asm_str);
1312 10 : }
1313 :
1314 : /* Construct a playback::rvalue instance (wrapping a tree) for a
1315 : unary op. */
1316 :
1317 : playback::rvalue *
1318 163 : playback::context::
1319 : new_unary_op (location *loc,
1320 : enum gcc_jit_unary_op op,
1321 : type *result_type,
1322 : rvalue *a)
1323 : {
1324 : // FIXME: type-checking, or coercion?
1325 163 : enum tree_code inner_op;
1326 :
1327 163 : gcc_assert (result_type);
1328 163 : gcc_assert (a);
1329 :
1330 163 : tree node = a->as_tree ();
1331 163 : node = fold_const_var (node);
1332 :
1333 163 : tree inner_result = NULL;
1334 :
1335 163 : switch (op)
1336 : {
1337 0 : default:
1338 0 : add_error (loc, "unrecognized (enum gcc_jit_unary_op) value: %i", op);
1339 0 : return NULL;
1340 :
1341 : case GCC_JIT_UNARY_OP_MINUS:
1342 : inner_op = NEGATE_EXPR;
1343 : break;
1344 :
1345 40 : case GCC_JIT_UNARY_OP_BITWISE_NEGATE:
1346 40 : inner_op = BIT_NOT_EXPR;
1347 40 : break;
1348 :
1349 25 : case GCC_JIT_UNARY_OP_LOGICAL_NEGATE:
1350 25 : node = as_truth_value (node, loc);
1351 25 : inner_result = invert_truthvalue (node);
1352 25 : if (loc)
1353 0 : set_tree_location (inner_result, loc);
1354 25 : return new rvalue (this, inner_result);
1355 :
1356 20 : case GCC_JIT_UNARY_OP_ABS:
1357 20 : inner_op = ABS_EXPR;
1358 20 : break;
1359 : }
1360 :
1361 138 : inner_result = build1 (inner_op,
1362 : result_type->as_tree (),
1363 : node);
1364 :
1365 : /* Try to fold. */
1366 138 : inner_result = fold (inner_result);
1367 :
1368 138 : if (loc)
1369 0 : set_tree_location (inner_result, loc);
1370 :
1371 138 : return new rvalue (this, inner_result);
1372 : }
1373 :
1374 : /* Construct a playback::rvalue instance (wrapping a tree) for a
1375 : binary op. */
1376 :
1377 : playback::rvalue *
1378 4494 : playback::context::
1379 : new_binary_op (location *loc,
1380 : enum gcc_jit_binary_op op,
1381 : type *result_type,
1382 : rvalue *a, rvalue *b)
1383 : {
1384 : // FIXME: type-checking, or coercion?
1385 4494 : enum tree_code inner_op;
1386 :
1387 4494 : gcc_assert (result_type);
1388 4494 : gcc_assert (a);
1389 4494 : gcc_assert (b);
1390 :
1391 4494 : tree node_a = a->as_tree ();
1392 4494 : node_a = fold_const_var (node_a);
1393 :
1394 4494 : tree node_b = b->as_tree ();
1395 4494 : node_b = fold_const_var (node_b);
1396 :
1397 4494 : switch (op)
1398 : {
1399 0 : default:
1400 0 : add_error (loc, "unrecognized (enum gcc_jit_binary_op) value: %i", op);
1401 0 : return NULL;
1402 :
1403 : case GCC_JIT_BINARY_OP_PLUS:
1404 : inner_op = PLUS_EXPR;
1405 : break;
1406 :
1407 323 : case GCC_JIT_BINARY_OP_MINUS:
1408 323 : inner_op = MINUS_EXPR;
1409 323 : break;
1410 :
1411 1475 : case GCC_JIT_BINARY_OP_MULT:
1412 1475 : inner_op = MULT_EXPR;
1413 1475 : break;
1414 :
1415 184 : case GCC_JIT_BINARY_OP_DIVIDE:
1416 184 : if (FLOAT_TYPE_P (result_type->as_tree ()))
1417 : /* Floating-point division: */
1418 : inner_op = RDIV_EXPR;
1419 : else
1420 : /* Truncating to zero: */
1421 : inner_op = TRUNC_DIV_EXPR;
1422 : break;
1423 :
1424 20 : case GCC_JIT_BINARY_OP_MODULO:
1425 20 : inner_op = TRUNC_MOD_EXPR;
1426 20 : break;
1427 :
1428 285 : case GCC_JIT_BINARY_OP_BITWISE_AND:
1429 285 : inner_op = BIT_AND_EXPR;
1430 285 : break;
1431 :
1432 40 : case GCC_JIT_BINARY_OP_BITWISE_XOR:
1433 40 : inner_op = BIT_XOR_EXPR;
1434 40 : break;
1435 :
1436 40 : case GCC_JIT_BINARY_OP_BITWISE_OR:
1437 40 : inner_op = BIT_IOR_EXPR;
1438 40 : break;
1439 :
1440 320 : case GCC_JIT_BINARY_OP_LOGICAL_AND:
1441 320 : node_a = as_truth_value (node_a, loc);
1442 320 : node_b = as_truth_value (node_b, loc);
1443 320 : inner_op = TRUTH_ANDIF_EXPR;
1444 320 : break;
1445 :
1446 80 : case GCC_JIT_BINARY_OP_LOGICAL_OR:
1447 80 : node_a = as_truth_value (node_a, loc);
1448 80 : node_b = as_truth_value (node_b, loc);
1449 80 : inner_op = TRUTH_ORIF_EXPR;
1450 80 : break;
1451 :
1452 40 : case GCC_JIT_BINARY_OP_LSHIFT:
1453 40 : inner_op = LSHIFT_EXPR;
1454 40 : break;
1455 :
1456 50 : case GCC_JIT_BINARY_OP_RSHIFT:
1457 50 : inner_op = RSHIFT_EXPR;
1458 50 : break;
1459 : }
1460 :
1461 4494 : tree inner_expr = build2 (inner_op,
1462 : result_type->as_tree (),
1463 : node_a,
1464 : node_b);
1465 :
1466 : /* Try to fold the expression. */
1467 4494 : inner_expr = fold (inner_expr);
1468 :
1469 4494 : if (loc)
1470 280 : set_tree_location (inner_expr, loc);
1471 :
1472 4494 : return new rvalue (this, inner_expr);
1473 : }
1474 :
1475 : /* Construct a playback::rvalue instance (wrapping a tree) for a
1476 : comparison. */
1477 :
1478 : playback::rvalue *
1479 1874 : playback::context::
1480 : new_comparison (location *loc,
1481 : enum gcc_jit_comparison op,
1482 : rvalue *a, rvalue *b, type *vec_result_type)
1483 : {
1484 : // FIXME: type-checking, or coercion?
1485 1874 : enum tree_code inner_op;
1486 :
1487 1874 : gcc_assert (a);
1488 1874 : gcc_assert (b);
1489 :
1490 1874 : switch (op)
1491 : {
1492 0 : default:
1493 0 : add_error (loc, "unrecognized (enum gcc_jit_comparison) value: %i", op);
1494 0 : return NULL;
1495 :
1496 : case GCC_JIT_COMPARISON_EQ:
1497 : inner_op = EQ_EXPR;
1498 : break;
1499 : case GCC_JIT_COMPARISON_NE:
1500 : inner_op = NE_EXPR;
1501 : break;
1502 : case GCC_JIT_COMPARISON_LT:
1503 : inner_op = LT_EXPR;
1504 : break;
1505 : case GCC_JIT_COMPARISON_LE:
1506 : inner_op = LE_EXPR;
1507 : break;
1508 : case GCC_JIT_COMPARISON_GT:
1509 : inner_op = GT_EXPR;
1510 : break;
1511 : case GCC_JIT_COMPARISON_GE:
1512 : inner_op = GE_EXPR;
1513 : break;
1514 : }
1515 :
1516 1874 : tree node_a = a->as_tree ();
1517 1874 : node_a = fold_const_var (node_a);
1518 1874 : tree node_b = b->as_tree ();
1519 1874 : node_b = fold_const_var (node_b);
1520 :
1521 1874 : tree inner_expr;
1522 1874 : tree a_type = TREE_TYPE (node_a);
1523 1874 : if (VECTOR_TYPE_P (a_type))
1524 : {
1525 : /* Build a vector comparison. See build_vec_cmp in c-typeck.cc for
1526 : reference. */
1527 180 : tree t_vec_result_type = vec_result_type->as_tree ();
1528 180 : tree zero_vec = build_zero_cst (t_vec_result_type);
1529 180 : tree minus_one_vec = build_minus_one_cst (t_vec_result_type);
1530 180 : tree cmp_type = truth_type_for (a_type);
1531 180 : tree cmp = build2 (inner_op, cmp_type, node_a, node_b);
1532 180 : inner_expr = build3 (VEC_COND_EXPR, t_vec_result_type, cmp, minus_one_vec,
1533 : zero_vec);
1534 : }
1535 : else
1536 : {
1537 1694 : inner_expr = build2 (inner_op,
1538 : boolean_type_node,
1539 : node_a,
1540 : node_b);
1541 : }
1542 :
1543 : /* Try to fold. */
1544 1874 : inner_expr = fold (inner_expr);
1545 :
1546 1874 : if (loc)
1547 26 : set_tree_location (inner_expr, loc);
1548 1874 : return new rvalue (this, inner_expr);
1549 : }
1550 :
1551 : /* Construct a playback::rvalue instance (wrapping a tree) for a
1552 : function call. */
1553 :
1554 : playback::rvalue *
1555 1030 : playback::context::
1556 : build_call (location *loc,
1557 : tree fn_ptr,
1558 : const auto_vec<rvalue *> *args,
1559 : bool require_tail_call)
1560 : {
1561 1030 : vec<tree, va_gc> *tree_args;
1562 1945 : vec_alloc (tree_args, args->length ());
1563 3557 : for (unsigned i = 0; i < args->length (); i++)
1564 1497 : tree_args->quick_push ((*args)[i]->as_tree ());
1565 :
1566 1030 : if (loc)
1567 47 : set_tree_location (fn_ptr, loc);
1568 :
1569 1030 : tree fn = TREE_TYPE (fn_ptr);
1570 1030 : tree fn_type = TREE_TYPE (fn);
1571 1030 : tree return_type = TREE_TYPE (fn_type);
1572 :
1573 1030 : tree call = build_call_vec (return_type,
1574 : fn_ptr, tree_args);
1575 :
1576 1030 : if (require_tail_call)
1577 25 : CALL_EXPR_MUST_TAIL_CALL (call) = 1;
1578 :
1579 1030 : return new rvalue (this, call);
1580 :
1581 : /* see c-typeck.cc: build_function_call
1582 : which calls build_function_call_vec
1583 :
1584 : which does lots of checking, then:
1585 : result = build_call_array_loc (loc, TREE_TYPE (fntype),
1586 : function, nargs, argarray);
1587 : which is in tree.cc
1588 : (see also build_call_vec)
1589 : */
1590 : }
1591 :
1592 : /* Construct a playback::rvalue instance (wrapping a tree) for a
1593 : call to a specific function. */
1594 :
1595 : playback::rvalue *
1596 1010 : playback::context::
1597 : new_call (location *loc,
1598 : function *func,
1599 : const auto_vec<rvalue *> *args,
1600 : bool require_tail_call)
1601 : {
1602 1010 : tree fndecl;
1603 :
1604 1010 : gcc_assert (func);
1605 :
1606 1010 : fndecl = func->as_fndecl ();
1607 :
1608 1010 : tree fntype = TREE_TYPE (fndecl);
1609 :
1610 1010 : tree fn = build1 (ADDR_EXPR, build_pointer_type (fntype), fndecl);
1611 :
1612 1010 : return build_call (loc, fn, args, require_tail_call);
1613 : }
1614 :
1615 : /* Construct a playback::rvalue instance (wrapping a tree) for a
1616 : call through a function pointer. */
1617 :
1618 : playback::rvalue *
1619 20 : playback::context::
1620 : new_call_through_ptr (location *loc,
1621 : rvalue *fn_ptr,
1622 : const auto_vec<rvalue *> *args,
1623 : bool require_tail_call)
1624 : {
1625 20 : gcc_assert (fn_ptr);
1626 20 : tree t_fn_ptr = fn_ptr->as_tree ();
1627 :
1628 20 : return build_call (loc, t_fn_ptr, args, require_tail_call);
1629 : }
1630 :
1631 : /* Construct a tree for a cast. */
1632 :
1633 : tree
1634 514 : playback::context::build_cast (playback::location *loc,
1635 : playback::rvalue *expr,
1636 : playback::type *type_)
1637 : {
1638 : /* For comparison, see:
1639 : - c/c-typeck.cc:build_c_cast
1640 : - c/c-convert.cc: convert
1641 : - convert.h
1642 :
1643 : Only some kinds of cast are currently supported here. */
1644 514 : tree t_expr = expr->as_tree ();
1645 514 : t_expr = fold_const_var (t_expr);
1646 :
1647 514 : tree t_dst_type = type_->as_tree ();
1648 514 : tree t_ret = NULL;
1649 514 : t_ret = targetm.convert_to_type (t_dst_type, t_expr);
1650 514 : if (t_ret)
1651 : return t_ret;
1652 514 : enum tree_code dst_code = TREE_CODE (t_dst_type);
1653 514 : switch (dst_code)
1654 : {
1655 225 : case INTEGER_TYPE:
1656 225 : case ENUMERAL_TYPE:
1657 225 : t_ret = convert_to_integer (t_dst_type, t_expr);
1658 225 : goto maybe_fold;
1659 :
1660 44 : case BOOLEAN_TYPE:
1661 : /* Compare with c_objc_common_truthvalue_conversion and
1662 : c_common_truthvalue_conversion. */
1663 : /* For now, convert to: (t_expr != 0) */
1664 44 : t_ret = build2 (NE_EXPR, t_dst_type,
1665 : t_expr,
1666 44 : build_int_cst (TREE_TYPE (t_expr), 0));
1667 44 : goto maybe_fold;
1668 :
1669 20 : case REAL_TYPE:
1670 20 : t_ret = convert_to_real (t_dst_type, t_expr);
1671 20 : goto maybe_fold;
1672 :
1673 225 : case POINTER_TYPE:
1674 225 : t_ret = build1 (NOP_EXPR, t_dst_type, t_expr);
1675 225 : goto maybe_fold;
1676 :
1677 0 : default:
1678 0 : add_error (loc, "couldn't handle cast during playback");
1679 0 : fprintf (stderr, "input expression:\n");
1680 0 : debug_tree (t_expr);
1681 0 : fprintf (stderr, "requested type:\n");
1682 0 : debug_tree (t_dst_type);
1683 0 : return error_mark_node;
1684 :
1685 514 : maybe_fold:
1686 514 : if (TREE_CODE (t_ret) != C_MAYBE_CONST_EXPR)
1687 514 : t_ret = fold (t_ret);
1688 : return t_ret;
1689 : }
1690 : }
1691 :
1692 : /* Construct a playback::rvalue instance (wrapping a tree) for a
1693 : cast. */
1694 :
1695 : playback::rvalue *
1696 514 : playback::context::
1697 : new_cast (playback::location *loc,
1698 : playback::rvalue *expr,
1699 : playback::type *type_)
1700 : {
1701 :
1702 514 : tree t_cast = build_cast (loc, expr, type_);
1703 514 : if (loc)
1704 9 : set_tree_location (t_cast, loc);
1705 514 : return new rvalue (this, t_cast);
1706 : }
1707 :
1708 : /* Construct a playback::rvalue instance (wrapping a tree) for a
1709 : bitcast. */
1710 :
1711 : playback::rvalue *
1712 30 : playback::context::
1713 : new_bitcast (location *loc,
1714 : rvalue *expr,
1715 : type *type_)
1716 : {
1717 30 : tree expr_size = TYPE_SIZE (expr->get_type ()->as_tree ());
1718 30 : tree type_size = TYPE_SIZE (type_->as_tree ());
1719 30 : tree t_expr = expr->as_tree ();
1720 30 : tree t_dst_type = type_->as_tree ();
1721 30 : if (expr_size != type_size)
1722 : {
1723 10 : active_playback_ctxt->add_error (loc,
1724 : "bitcast with types of different sizes");
1725 10 : fprintf (stderr, "input expression (size: " HOST_WIDE_INT_PRINT_DEC "):\n",
1726 : tree_to_uhwi (expr_size));
1727 10 : debug_tree (t_expr);
1728 10 : fprintf (stderr, "requested type (size: " HOST_WIDE_INT_PRINT_DEC "):\n",
1729 : tree_to_uhwi (type_size));
1730 10 : debug_tree (t_dst_type);
1731 : }
1732 30 : tree t_bitcast = build1 (VIEW_CONVERT_EXPR, t_dst_type, t_expr);
1733 30 : if (loc)
1734 0 : set_tree_location (t_bitcast, loc);
1735 30 : return new rvalue (this, t_bitcast);
1736 : }
1737 :
1738 : /* Construct a playback::lvalue instance (wrapping a tree) for an
1739 : array access. */
1740 :
1741 : playback::lvalue *
1742 634 : playback::context::
1743 : new_array_access (location *loc,
1744 : rvalue *ptr,
1745 : rvalue *index)
1746 : {
1747 634 : gcc_assert (ptr);
1748 634 : gcc_assert (index);
1749 :
1750 : /* For comparison, see:
1751 : c/c-typeck.cc: build_array_ref
1752 : c-family/c-common.cc: pointer_int_sum
1753 : */
1754 634 : tree t_ptr = ptr->as_tree ();
1755 634 : t_ptr = fold_const_var (t_ptr);
1756 634 : tree t_index = index->as_tree ();
1757 634 : t_index = fold_const_var (t_index);
1758 :
1759 634 : tree t_type_ptr = TREE_TYPE (t_ptr);
1760 634 : tree t_type_star_ptr = TREE_TYPE (t_type_ptr);
1761 :
1762 634 : if (TREE_CODE (t_type_ptr) == ARRAY_TYPE)
1763 : {
1764 409 : tree t_result = build4 (ARRAY_REF, t_type_star_ptr, t_ptr, t_index,
1765 : NULL_TREE, NULL_TREE);
1766 409 : t_result = fold (t_result);
1767 409 : if (loc)
1768 209 : set_tree_location (t_result, loc);
1769 409 : return new lvalue (this, t_result);
1770 : }
1771 : else
1772 : {
1773 : /* Convert index to an offset in bytes. */
1774 225 : tree t_sizeof = size_in_bytes (t_type_star_ptr);
1775 225 : t_index = fold_build1 (CONVERT_EXPR, sizetype, t_index);
1776 225 : tree t_offset = fold_build2_loc (UNKNOWN_LOCATION,
1777 : MULT_EXPR, sizetype, t_index, t_sizeof);
1778 :
1779 : /* Locate (ptr + offset). */
1780 225 : tree t_address = fold_build2_loc (UNKNOWN_LOCATION,
1781 : POINTER_PLUS_EXPR, t_type_ptr, t_ptr, t_offset);
1782 :
1783 225 : tree t_indirection = fold_build1 (INDIRECT_REF, t_type_star_ptr, t_address);
1784 225 : if (loc)
1785 : {
1786 0 : set_tree_location (t_sizeof, loc);
1787 0 : set_tree_location (t_offset, loc);
1788 0 : set_tree_location (t_address, loc);
1789 0 : set_tree_location (t_indirection, loc);
1790 : }
1791 :
1792 225 : return new lvalue (this, t_indirection);
1793 : }
1794 : }
1795 :
1796 : /* Construct a playback::rvalue instance (wrapping a tree) for a
1797 : vector conversion. */
1798 :
1799 : playback::rvalue *
1800 20 : playback::context::
1801 : convert_vector (location *loc,
1802 : rvalue *vector,
1803 : type *type)
1804 : {
1805 20 : gcc_assert (vector);
1806 20 : gcc_assert (type);
1807 :
1808 : /* For comparison, see:
1809 : c/c-common.cc: c_build_vec_convert
1810 : */
1811 :
1812 20 : tree t_vector = vector->as_tree ();
1813 :
1814 20 : tree t_result =
1815 20 : build_call_expr_internal_loc (UNKNOWN_LOCATION, IFN_VEC_CONVERT,
1816 : type->as_tree (), 1, t_vector);
1817 :
1818 20 : if (loc)
1819 0 : set_tree_location (t_result, loc);
1820 :
1821 20 : return new rvalue (this, t_result);
1822 : }
1823 :
1824 : /* The following functions come from c-common.h. */
1825 : /* Like c_mark_addressable but don't check register qualifier. */
1826 : void
1827 20 : common_mark_addressable_vec (tree t)
1828 : {
1829 20 : while (handled_component_p (t) || TREE_CODE (t) == C_MAYBE_CONST_EXPR)
1830 : {
1831 0 : t = TREE_OPERAND (t, 0);
1832 : }
1833 20 : if (!VAR_P (t)
1834 20 : && TREE_CODE (t) != PARM_DECL
1835 20 : && TREE_CODE (t) != COMPOUND_LITERAL_EXPR
1836 20 : && TREE_CODE (t) != TARGET_EXPR)
1837 : return;
1838 0 : if (!VAR_P (t) || !DECL_HARD_REGISTER (t))
1839 0 : TREE_ADDRESSABLE (t) = 1;
1840 0 : if (TREE_CODE (t) == COMPOUND_LITERAL_EXPR)
1841 0 : TREE_ADDRESSABLE (COMPOUND_LITERAL_EXPR_DECL (t)) = 1;
1842 0 : else if (TREE_CODE (t) == TARGET_EXPR)
1843 0 : TREE_ADDRESSABLE (TARGET_EXPR_SLOT (t)) = 1;
1844 : }
1845 :
1846 : /* Return true if TYPE is a vector type that should be subject to the GNU
1847 : vector extensions (as opposed to a vector type that is used only for
1848 : the purposes of defining target-specific built-in functions). */
1849 :
1850 : inline bool
1851 20 : gnu_vector_type_p (const_tree type)
1852 : {
1853 20 : return TREE_CODE (type) == VECTOR_TYPE && !TYPE_INDIVISIBLE_P (type);
1854 : }
1855 :
1856 : /* Return nonzero if REF is an lvalue valid for this language.
1857 : Lvalues can be assigned, unless their type has TYPE_READONLY.
1858 : Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
1859 :
1860 : bool
1861 20 : lvalue_p (const_tree ref)
1862 : {
1863 20 : const enum tree_code code = TREE_CODE (ref);
1864 :
1865 20 : switch (code)
1866 : {
1867 0 : case REALPART_EXPR:
1868 0 : case IMAGPART_EXPR:
1869 0 : case COMPONENT_REF:
1870 0 : return lvalue_p (TREE_OPERAND (ref, 0));
1871 :
1872 0 : case C_MAYBE_CONST_EXPR:
1873 0 : return lvalue_p (TREE_OPERAND (ref, 1));
1874 :
1875 : case COMPOUND_LITERAL_EXPR:
1876 : case STRING_CST:
1877 : return true;
1878 :
1879 0 : case MEM_REF:
1880 0 : case TARGET_MEM_REF:
1881 : /* MEM_REFs can appear from -fgimple parsing or folding, so allow them
1882 : here as well. */
1883 0 : case INDIRECT_REF:
1884 0 : case ARRAY_REF:
1885 0 : case VAR_DECL:
1886 0 : case PARM_DECL:
1887 0 : case RESULT_DECL:
1888 0 : case ERROR_MARK:
1889 0 : return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
1890 0 : && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
1891 :
1892 0 : case BIND_EXPR:
1893 0 : return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
1894 :
1895 : default:
1896 : return false;
1897 : }
1898 : }
1899 :
1900 : bool
1901 20 : convert_vector_to_array_for_subscript (tree *vecp)
1902 : {
1903 20 : bool ret = false;
1904 20 : if (gnu_vector_type_p (TREE_TYPE (*vecp)))
1905 : {
1906 20 : tree type = TREE_TYPE (*vecp);
1907 :
1908 20 : ret = !lvalue_p (*vecp);
1909 :
1910 : /* We are building an ARRAY_REF so mark the vector as addressable
1911 : to not run into the gimplifiers premature setting of DECL_GIMPLE_REG_P
1912 : for function parameters. */
1913 : /* NOTE: that was the missing piece for making vector access work with
1914 : optimizations enabled. */
1915 20 : common_mark_addressable_vec (*vecp);
1916 :
1917 40 : *vecp = build1 (VIEW_CONVERT_EXPR,
1918 20 : build_array_type_nelts (TREE_TYPE (type),
1919 : TYPE_VECTOR_SUBPARTS (type)),
1920 : *vecp);
1921 : }
1922 20 : return ret;
1923 : }
1924 :
1925 : /* Construct a playback::lvalue instance (wrapping a tree) for a
1926 : vector access. */
1927 :
1928 : playback::lvalue *
1929 20 : playback::context::
1930 : new_vector_access (location *loc,
1931 : rvalue *vector,
1932 : rvalue *index)
1933 : {
1934 20 : gcc_assert (vector);
1935 20 : gcc_assert (index);
1936 :
1937 : /* For comparison, see:
1938 : c/c-typeck.cc: build_array_ref
1939 : */
1940 :
1941 20 : tree t_vector = vector->as_tree ();
1942 20 : bool non_lvalue = convert_vector_to_array_for_subscript (&t_vector);
1943 20 : tree type = TREE_TYPE (TREE_TYPE (t_vector));
1944 20 : tree t_result = build4 (ARRAY_REF, type, t_vector, index->as_tree (),
1945 : NULL_TREE, NULL_TREE);
1946 20 : if (non_lvalue)
1947 20 : t_result = non_lvalue (t_result);
1948 :
1949 20 : if (loc)
1950 0 : set_tree_location (t_result, loc);
1951 20 : return new lvalue (this, t_result);
1952 : }
1953 :
1954 : /* Construct a tree for a field access. */
1955 :
1956 : tree
1957 2416 : playback::context::
1958 : new_field_access (location *loc,
1959 : tree datum,
1960 : field *field)
1961 : {
1962 2416 : gcc_assert (datum);
1963 2416 : gcc_assert (field);
1964 :
1965 : /* Compare with c/c-typeck.cc:lookup_field, build_indirect_ref, and
1966 : build_component_ref. */
1967 2416 : tree type = TREE_TYPE (datum);
1968 2416 : gcc_assert (type);
1969 2416 : gcc_assert (TREE_CODE (type) != POINTER_TYPE);
1970 :
1971 2416 : tree t_field = field->as_tree ();
1972 2416 : tree ref = build3 (COMPONENT_REF, TREE_TYPE (t_field), datum,
1973 : t_field, NULL_TREE);
1974 2416 : if (loc)
1975 0 : set_tree_location (ref, loc);
1976 2416 : return ref;
1977 : }
1978 :
1979 : /* Construct a tree for a dereference. */
1980 :
1981 : tree
1982 2777 : playback::context::
1983 : new_dereference (tree ptr,
1984 : location *loc)
1985 : {
1986 2777 : gcc_assert (ptr);
1987 :
1988 2777 : tree type = TREE_TYPE (TREE_TYPE(ptr));
1989 2777 : tree datum = fold_build1 (INDIRECT_REF, type, ptr);
1990 2777 : if (loc)
1991 0 : set_tree_location (datum, loc);
1992 2777 : return datum;
1993 : }
1994 :
1995 : /* Construct a playback::type instance (wrapping a tree)
1996 : with the given alignment. */
1997 :
1998 : playback::type *
1999 240 : playback::type::
2000 : get_aligned (size_t alignment_in_bytes) const
2001 : {
2002 240 : tree t_new_type = build_variant_type_copy (m_inner);
2003 :
2004 240 : SET_TYPE_ALIGN (t_new_type, alignment_in_bytes * BITS_PER_UNIT);
2005 240 : TYPE_USER_ALIGN (t_new_type) = 1;
2006 :
2007 240 : return new type (t_new_type);
2008 : }
2009 :
2010 : /* Construct a playback::type instance (wrapping a tree)
2011 : for the given vector type. */
2012 :
2013 : playback::type *
2014 525 : playback::type::
2015 : get_vector (size_t num_units) const
2016 : {
2017 525 : tree t_new_type = build_vector_type (m_inner, num_units);
2018 525 : return new type (t_new_type);
2019 : }
2020 :
2021 : /* Construct a playback::lvalue instance (wrapping a tree) for a
2022 : field access. */
2023 :
2024 : playback::lvalue *
2025 234 : playback::lvalue::
2026 : access_field (location *loc,
2027 : field *field)
2028 : {
2029 234 : tree datum = as_tree ();
2030 234 : tree ref = get_context ()->new_field_access (loc, datum, field);
2031 234 : if (!ref)
2032 : return NULL;
2033 234 : return new lvalue (get_context (), ref);
2034 : }
2035 :
2036 : /* Construct a playback::rvalue instance (wrapping a tree) for a
2037 : field access. */
2038 :
2039 : playback::rvalue *
2040 174 : playback::rvalue::
2041 : access_field (location *loc,
2042 : field *field)
2043 : {
2044 174 : tree datum = as_tree ();
2045 174 : tree ref = get_context ()->new_field_access (loc, datum, field);
2046 174 : if (!ref)
2047 : return NULL;
2048 174 : return new rvalue (get_context (), ref);
2049 : }
2050 :
2051 : /* Construct a playback::lvalue instance (wrapping a tree) for a
2052 : dereferenced field access. */
2053 :
2054 : playback::lvalue *
2055 2008 : playback::rvalue::
2056 : dereference_field (location *loc,
2057 : field *field)
2058 : {
2059 2008 : tree ptr = as_tree ();
2060 2008 : tree datum = get_context ()->new_dereference (ptr, loc);
2061 2008 : if (!datum)
2062 : return NULL;
2063 2008 : tree ref = get_context ()->new_field_access (loc, datum, field);
2064 2008 : if (!ref)
2065 : return NULL;
2066 2008 : return new lvalue (get_context (), ref);
2067 : }
2068 :
2069 : /* Construct a playback::lvalue instance (wrapping a tree) for a
2070 : dereference. */
2071 :
2072 : playback::lvalue *
2073 769 : playback::rvalue::
2074 : dereference (location *loc)
2075 : {
2076 769 : tree ptr = as_tree ();
2077 769 : tree datum = get_context ()->new_dereference (ptr, loc);
2078 769 : return new lvalue (get_context (), datum);
2079 : }
2080 :
2081 : /* Mark the lvalue saying that we need to be able to take the
2082 : address of it; it should not be allocated in a register.
2083 : Compare with e.g. c/c-typeck.cc: c_mark_addressable really_atomic_lvalue.
2084 : Returns false if a failure occurred (an error will already have been
2085 : added to the active context for this case). */
2086 :
2087 : bool
2088 653 : playback::lvalue::
2089 : mark_addressable (location *loc)
2090 : {
2091 653 : tree x = as_tree ();
2092 :
2093 733 : while (1)
2094 693 : switch (TREE_CODE (x))
2095 : {
2096 5 : case COMPONENT_REF:
2097 5 : if (DECL_JIT_BIT_FIELD (TREE_OPERAND (x, 1)))
2098 : {
2099 5 : gcc_assert (gcc::jit::active_playback_ctxt);
2100 5 : gcc::jit::
2101 5 : active_playback_ctxt->add_error (loc,
2102 : "cannot take address of "
2103 : "bit-field");
2104 5 : return false;
2105 : }
2106 : /* fallthrough */
2107 40 : case ADDR_EXPR:
2108 40 : case ARRAY_REF:
2109 40 : case REALPART_EXPR:
2110 40 : case IMAGPART_EXPR:
2111 40 : x = TREE_OPERAND (x, 0);
2112 40 : break;
2113 :
2114 0 : case COMPOUND_LITERAL_EXPR:
2115 0 : case CONSTRUCTOR:
2116 0 : TREE_ADDRESSABLE (x) = 1;
2117 0 : return true;
2118 :
2119 568 : case VAR_DECL:
2120 568 : case CONST_DECL:
2121 568 : case PARM_DECL:
2122 568 : case RESULT_DECL:
2123 : /* (we don't have a concept of a "register" declaration) */
2124 : /* fallthrough */
2125 568 : case FUNCTION_DECL:
2126 568 : TREE_ADDRESSABLE (x) = 1;
2127 : /* fallthrough */
2128 : default:
2129 : return true;
2130 : }
2131 : }
2132 :
2133 : /* Construct a playback::rvalue instance (wrapping a tree) for an
2134 : address-lookup. */
2135 :
2136 : playback::rvalue *
2137 653 : playback::lvalue::
2138 : get_address (location *loc)
2139 : {
2140 653 : tree t_lvalue = as_tree ();
2141 653 : tree t_thistype = TREE_TYPE (t_lvalue);
2142 653 : tree t_ptrtype = build_pointer_type (t_thistype);
2143 653 : tree ptr = fold_build1 (ADDR_EXPR, t_ptrtype, t_lvalue);
2144 653 : if (loc)
2145 0 : get_context ()->set_tree_location (ptr, loc);
2146 653 : if (mark_addressable (loc))
2147 648 : return new rvalue (get_context (), ptr);
2148 : else
2149 : return NULL;
2150 : }
2151 :
2152 : /* The wrapper subclasses are GC-managed, but can own non-GC memory.
2153 : Provide this finalization hook for calling then they are collected,
2154 : which calls the finalizer vfunc. This allows them to call "release"
2155 : on any vec<> within them. */
2156 :
2157 : static void
2158 89557 : wrapper_finalizer (void *ptr)
2159 : {
2160 89557 : playback::wrapper *wrapper = reinterpret_cast <playback::wrapper *> (ptr);
2161 89557 : wrapper->finalizer ();
2162 89557 : }
2163 :
2164 : /* gcc::jit::playback::wrapper subclasses are GC-managed:
2165 : allocate them using ggc_internal_cleared_alloc. */
2166 :
2167 : void *
2168 92909 : playback::wrapper::
2169 : operator new (size_t sz)
2170 : {
2171 92909 : return ggc_internal_cleared_alloc (sz, wrapper_finalizer, 0, 1);
2172 :
2173 : }
2174 :
2175 : /* Constructor for gcc:jit::playback::function. */
2176 :
2177 18167 : playback::function::
2178 : function (context *ctxt,
2179 : tree fndecl,
2180 : enum gcc_jit_function_kind kind)
2181 18167 : : m_ctxt(ctxt),
2182 18167 : m_inner_fndecl (fndecl),
2183 18167 : m_inner_bind_expr (NULL),
2184 18167 : m_kind (kind),
2185 18167 : m_blocks ()
2186 : {
2187 18167 : if (m_kind != GCC_JIT_FUNCTION_IMPORTED)
2188 : {
2189 : /* Create a BIND_EXPR, and within it, a statement list. */
2190 4211 : m_stmt_list = alloc_stmt_list ();
2191 4211 : m_stmt_iter = tsi_start (m_stmt_list);
2192 4211 : m_inner_block = make_node (BLOCK);
2193 4211 : m_inner_bind_expr =
2194 4211 : build3 (BIND_EXPR, void_type_node, NULL, m_stmt_list, m_inner_block);
2195 : }
2196 : else
2197 : {
2198 13956 : m_inner_block = NULL;
2199 13956 : m_stmt_list = NULL;
2200 : }
2201 18167 : }
2202 :
2203 : /* Hand-written GC-marking hook for playback functions. */
2204 :
2205 : void
2206 66145464 : playback::function::
2207 : gt_ggc_mx ()
2208 : {
2209 66145464 : gt_ggc_m_9tree_node (m_inner_fndecl);
2210 66145464 : gt_ggc_m_9tree_node (m_inner_bind_expr);
2211 66145464 : gt_ggc_m_9tree_node (m_stmt_list);
2212 66145464 : gt_ggc_m_9tree_node (m_inner_block);
2213 66145464 : }
2214 :
2215 : /* Don't leak vec's internal buffer (in non-GC heap) when we are
2216 : GC-ed. */
2217 :
2218 : void
2219 16328 : playback::function::finalizer ()
2220 : {
2221 16328 : m_blocks.release ();
2222 16328 : }
2223 :
2224 : /* Get the return type of a playback function, in tree form. */
2225 :
2226 : tree
2227 4717 : playback::function::
2228 : get_return_type_as_tree () const
2229 : {
2230 4717 : return TREE_TYPE (TREE_TYPE(m_inner_fndecl));
2231 : }
2232 :
2233 : /* Construct a new local within this playback::function. */
2234 :
2235 : playback::lvalue *
2236 2592 : playback::function::
2237 : new_local (location *loc,
2238 : type *type,
2239 : const char *name,
2240 : const std::vector<std::pair<gcc_jit_variable_attribute,
2241 : std::string>> &attributes)
2242 : {
2243 2592 : gcc_assert (type);
2244 2592 : tree inner;
2245 2592 : if (name)
2246 2587 : inner = build_decl (UNKNOWN_LOCATION, VAR_DECL,
2247 : get_identifier (name),
2248 : type->as_tree ());
2249 : else
2250 : {
2251 5 : inner = build_decl (UNKNOWN_LOCATION, VAR_DECL,
2252 : create_tmp_var_name ("JITTMP"),
2253 : type->as_tree ());
2254 5 : DECL_ARTIFICIAL (inner) = 1;
2255 5 : DECL_IGNORED_P (inner) = 1;
2256 5 : DECL_NAMELESS (inner) = 1;
2257 : }
2258 2592 : DECL_CONTEXT (inner) = this->m_inner_fndecl;
2259 :
2260 : /* Prepend to BIND_EXPR_VARS: */
2261 2592 : DECL_CHAIN (inner) = BIND_EXPR_VARS (m_inner_bind_expr);
2262 2592 : BIND_EXPR_VARS (m_inner_bind_expr) = inner;
2263 :
2264 2592 : set_variable_string_attribute (attributes, inner);
2265 :
2266 2592 : if (loc)
2267 21 : set_tree_location (inner, loc);
2268 2592 : return new lvalue (m_ctxt, inner);
2269 : }
2270 :
2271 : /* Construct a new block within this playback::function. */
2272 :
2273 : playback::block *
2274 7133 : playback::function::
2275 : new_block (const char *name)
2276 : {
2277 7133 : gcc_assert (m_kind != GCC_JIT_FUNCTION_IMPORTED);
2278 :
2279 7133 : block *result = new playback::block (this, name);
2280 7133 : m_blocks.safe_push (result);
2281 7133 : return result;
2282 : }
2283 :
2284 : /* Construct a playback::rvalue instance wrapping an ADDR_EXPR for
2285 : this playback::function. */
2286 :
2287 : playback::rvalue *
2288 20 : playback::function::get_address (location *loc)
2289 : {
2290 20 : tree t_fndecl = as_fndecl ();
2291 20 : tree t_fntype = TREE_TYPE (t_fndecl);
2292 20 : tree t_fnptr = build1 (ADDR_EXPR, build_pointer_type (t_fntype), t_fndecl);
2293 20 : if (loc)
2294 0 : m_ctxt->set_tree_location (t_fnptr, loc);
2295 20 : return new rvalue (m_ctxt, t_fnptr);
2296 : }
2297 :
2298 : /* Build a statement list for the function as a whole out of the
2299 : lists of statements for the individual blocks, building labels
2300 : for each block. */
2301 :
2302 : void
2303 18152 : playback::function::
2304 : build_stmt_list ()
2305 : {
2306 18152 : int i;
2307 18152 : block *b;
2308 :
2309 18152 : JIT_LOG_SCOPE (m_ctxt->get_logger ());
2310 :
2311 43422 : FOR_EACH_VEC_ELT (m_blocks, i, b)
2312 : {
2313 7118 : int j;
2314 7118 : tree stmt;
2315 :
2316 7118 : b->m_label_expr = build1 (LABEL_EXPR,
2317 : void_type_node,
2318 : b->as_label_decl ());
2319 7118 : tsi_link_after (&m_stmt_iter, b->m_label_expr, TSI_CONTINUE_LINKING);
2320 :
2321 28072 : FOR_EACH_VEC_ELT (b->m_stmts, j, stmt)
2322 13836 : tsi_link_after (&m_stmt_iter, stmt, TSI_CONTINUE_LINKING);
2323 : }
2324 18152 : }
2325 :
2326 : /* Finish compiling the given function, potentially running the
2327 : garbage-collector.
2328 : The function will have a statement list by now.
2329 : Amongst other things, this gimplifies the statement list,
2330 : and calls cgraph_node::finalize_function on the function. */
2331 :
2332 : void
2333 18152 : playback::function::
2334 : postprocess ()
2335 : {
2336 18152 : JIT_LOG_SCOPE (m_ctxt->get_logger ());
2337 :
2338 18152 : if (m_ctxt->get_bool_option (GCC_JIT_BOOL_OPTION_DUMP_INITIAL_TREE))
2339 0 : debug_tree (m_stmt_list);
2340 :
2341 : /* Do we need this to force cgraphunit.cc to output the function? */
2342 18152 : if (m_kind == GCC_JIT_FUNCTION_EXPORTED)
2343 : {
2344 4026 : DECL_EXTERNAL (m_inner_fndecl) = 0;
2345 4026 : DECL_PRESERVE_P (m_inner_fndecl) = 1;
2346 : }
2347 :
2348 18152 : if (m_kind == GCC_JIT_FUNCTION_INTERNAL
2349 18152 : ||m_kind == GCC_JIT_FUNCTION_ALWAYS_INLINE)
2350 : {
2351 170 : DECL_EXTERNAL (m_inner_fndecl) = 0;
2352 170 : TREE_PUBLIC (m_inner_fndecl) = 0;
2353 : }
2354 :
2355 18152 : if (m_kind != GCC_JIT_FUNCTION_IMPORTED)
2356 : {
2357 : /* Seem to need this in gimple-low.cc: */
2358 4196 : gcc_assert (m_inner_block);
2359 4196 : DECL_INITIAL (m_inner_fndecl) = m_inner_block;
2360 :
2361 : /* how to add to function? the following appears to be how to
2362 : set the body of a m_inner_fndecl: */
2363 4196 : DECL_SAVED_TREE(m_inner_fndecl) = m_inner_bind_expr;
2364 :
2365 : /* Ensure that locals appear in the debuginfo. */
2366 4196 : BLOCK_VARS (m_inner_block) = BIND_EXPR_VARS (m_inner_bind_expr);
2367 :
2368 : //debug_tree (m_inner_fndecl);
2369 :
2370 : /* Convert to gimple: */
2371 : //printf("about to gimplify_function_tree\n");
2372 4196 : gimplify_function_tree (m_inner_fndecl);
2373 : //printf("finished gimplify_function_tree\n");
2374 :
2375 4196 : current_function_decl = m_inner_fndecl;
2376 4196 : if (m_ctxt->get_bool_option (GCC_JIT_BOOL_OPTION_DUMP_INITIAL_GIMPLE))
2377 0 : dump_function_to_file (m_inner_fndecl, stderr, TDF_VOPS|TDF_MEMSYMS|TDF_LINENO);
2378 : //debug_tree (m_inner_fndecl);
2379 :
2380 : //printf("about to add to cgraph\n");
2381 : /* Add to cgraph: */
2382 4196 : cgraph_node::finalize_function (m_inner_fndecl, false);
2383 : /* This can trigger a collection, so we need to have all of
2384 : the funcs as roots. */
2385 :
2386 4196 : current_function_decl = NULL;
2387 : }
2388 : else
2389 : /* Add to cgraph to output aliases: */
2390 13956 : rest_of_decl_compilation (m_inner_fndecl, true, 0);
2391 18152 : }
2392 :
2393 : /* Don't leak vec's internal buffer (in non-GC heap) when we are
2394 : GC-ed. */
2395 :
2396 : void
2397 7058 : playback::block::finalizer ()
2398 : {
2399 7058 : m_stmts.release ();
2400 7058 : }
2401 :
2402 : /* Add an eval of the rvalue to the function's statement list. */
2403 :
2404 : void
2405 321 : playback::block::
2406 : add_eval (location *loc,
2407 : rvalue *rvalue)
2408 : {
2409 321 : gcc_assert (rvalue);
2410 :
2411 321 : if (loc)
2412 9 : set_tree_location (rvalue->as_tree (), loc);
2413 :
2414 321 : add_stmt (rvalue->as_tree ());
2415 321 : }
2416 :
2417 : /* Add an assignment to the function's statement list. */
2418 :
2419 : void
2420 5819 : playback::block::
2421 : add_assignment (location *loc,
2422 : lvalue *lvalue,
2423 : rvalue *rvalue)
2424 : {
2425 5819 : gcc_assert (lvalue);
2426 5819 : gcc_assert (rvalue);
2427 :
2428 5819 : tree t_lvalue = lvalue->as_tree ();
2429 5819 : tree t_rvalue = rvalue->as_tree ();
2430 5819 : if (TREE_TYPE (t_rvalue) != TREE_TYPE (t_lvalue))
2431 : {
2432 400 : t_rvalue = build1 (CONVERT_EXPR,
2433 400 : TREE_TYPE (t_lvalue),
2434 : t_rvalue);
2435 400 : if (loc)
2436 0 : set_tree_location (t_rvalue, loc);
2437 : }
2438 :
2439 5819 : tree stmt =
2440 5819 : build2 (MODIFY_EXPR, TREE_TYPE (t_lvalue),
2441 : t_lvalue, t_rvalue);
2442 5819 : if (loc)
2443 390 : set_tree_location (stmt, loc);
2444 5819 : add_stmt (stmt);
2445 5819 : }
2446 :
2447 : /* Add a comment to the function's statement list.
2448 : For now this is done by adding a dummy label. */
2449 :
2450 : void
2451 513 : playback::block::
2452 : add_comment (location *loc,
2453 : const char *text)
2454 : {
2455 : /* Wrap the text in C-style comment delimiters. */
2456 513 : size_t sz =
2457 : (3 /* opening delim */
2458 513 : + strlen (text)
2459 : + 3 /* closing delim */
2460 : + 1 /* terminator */);
2461 513 : char *wrapped = (char *)ggc_internal_alloc (sz);
2462 513 : snprintf (wrapped, sz, "/* %s */", text);
2463 :
2464 : /* For now we simply implement this by adding a dummy label with a name
2465 : containing the given text. */
2466 513 : tree identifier = get_identifier (wrapped);
2467 513 : tree label_decl = build_decl (UNKNOWN_LOCATION, LABEL_DECL,
2468 : identifier, void_type_node);
2469 513 : DECL_CONTEXT (label_decl) = m_func->as_fndecl ();
2470 :
2471 513 : tree label_expr = build1 (LABEL_EXPR, void_type_node, label_decl);
2472 513 : if (loc)
2473 202 : set_tree_location (label_expr, loc);
2474 513 : add_stmt (label_expr);
2475 513 : }
2476 :
2477 : /* Add a conditional jump statement to the function's statement list. */
2478 :
2479 : void
2480 1034 : playback::block::
2481 : add_conditional (location *loc,
2482 : rvalue *boolval,
2483 : block *on_true,
2484 : block *on_false)
2485 : {
2486 1034 : gcc_assert (boolval);
2487 1034 : gcc_assert (on_true);
2488 1034 : gcc_assert (on_false);
2489 :
2490 : /* COND_EXPR wants statement lists for the true/false operands, but we
2491 : want labels.
2492 : Shim it by creating jumps to the labels */
2493 1034 : tree true_jump = build1 (GOTO_EXPR, void_type_node,
2494 : on_true->as_label_decl ());
2495 1034 : if (loc)
2496 42 : set_tree_location (true_jump, loc);
2497 :
2498 1034 : tree false_jump = build1 (GOTO_EXPR, void_type_node,
2499 : on_false->as_label_decl ());
2500 1034 : if (loc)
2501 42 : set_tree_location (false_jump, loc);
2502 :
2503 1034 : tree stmt =
2504 1034 : build3 (COND_EXPR, void_type_node, boolval->as_tree (),
2505 : true_jump, false_jump);
2506 1034 : if (loc)
2507 42 : set_tree_location (stmt, loc);
2508 1034 : add_stmt (stmt);
2509 1034 : }
2510 :
2511 : /* Add an unconditional jump statement to the function's statement list. */
2512 :
2513 : void
2514 1327 : playback::block::
2515 : add_jump (location *loc,
2516 : block *target)
2517 : {
2518 1327 : gcc_assert (target);
2519 :
2520 : // see c_finish_loop
2521 : //tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
2522 : //add_stmt (top);
2523 :
2524 : //tree stmt = build_and_jump (&LABEL_EXPR_LABEL (target->label_));
2525 1327 : TREE_USED (target->as_label_decl ()) = 1;
2526 1327 : tree stmt = build1 (GOTO_EXPR, void_type_node, target->as_label_decl ());
2527 1327 : if (loc)
2528 48 : set_tree_location (stmt, loc);
2529 1327 : add_stmt (stmt);
2530 :
2531 : /*
2532 : from c-typeck.cc:
2533 : tree
2534 : c_finish_goto_label (location_t loc, tree label)
2535 : {
2536 : tree decl = lookup_label_for_goto (loc, label);
2537 : if (!decl)
2538 : return NULL_TREE;
2539 : TREE_USED (decl) = 1;
2540 : {
2541 : tree t = build1 (GOTO_EXPR, void_type_node, decl);
2542 : SET_EXPR_LOCATION (t, loc);
2543 : return add_stmt (t);
2544 : }
2545 : }
2546 : */
2547 :
2548 1327 : }
2549 :
2550 : /* Take the address of a block as a void * rvalue (&&block). */
2551 :
2552 : playback::rvalue *
2553 60 : playback::block::
2554 : get_address (location *loc)
2555 : {
2556 60 : tree label = as_label_decl ();
2557 60 : TREE_USED (label) = 1;
2558 : /* Also set during gimplification by gimplify_addr_expr, but marked
2559 : explicitly here when constructing the ADDR_EXPR. */
2560 60 : FORCED_LABEL (label) = 1;
2561 :
2562 60 : tree addr = build1 (ADDR_EXPR, ptr_type_node, label);
2563 60 : if (loc)
2564 0 : set_tree_location (addr, loc);
2565 :
2566 60 : return new rvalue (m_func->m_ctxt, addr);
2567 : }
2568 :
2569 : /* Add an indirect jump statement (goto *target_address;). */
2570 :
2571 : void
2572 20 : playback::block::add_indirect_jump (location *loc,
2573 : rvalue *target_address,
2574 : auto_vec <block *> *destinations)
2575 : {
2576 20 : unsigned i;
2577 20 : block *dest;
2578 80 : FOR_EACH_VEC_ELT (*destinations, i, dest)
2579 : {
2580 60 : tree label = dest->as_label_decl ();
2581 60 : TREE_USED (label) = 1;
2582 60 : FORCED_LABEL (label) = 1;
2583 : }
2584 :
2585 20 : tree target = target_address->as_tree ();
2586 20 : target = fold_convert (ptr_type_node, target);
2587 20 : tree stmt = build1 (GOTO_EXPR, void_type_node, target);
2588 20 : if (loc)
2589 0 : set_tree_location (stmt, loc);
2590 20 : add_stmt (stmt);
2591 20 : }
2592 :
2593 : /* Add a return statement to the function's statement list. */
2594 :
2595 : void
2596 4717 : playback::block::
2597 : add_return (location *loc,
2598 : rvalue *rvalue)
2599 : {
2600 4717 : tree modify_retval = NULL;
2601 4717 : tree return_type = m_func->get_return_type_as_tree ();
2602 4717 : if (rvalue)
2603 : {
2604 4083 : tree t_lvalue = DECL_RESULT (m_func->as_fndecl ());
2605 4083 : tree t_rvalue = rvalue->as_tree ();
2606 4083 : if (TREE_TYPE (t_rvalue) != TREE_TYPE (t_lvalue))
2607 135 : t_rvalue = build1 (CONVERT_EXPR,
2608 135 : TREE_TYPE (t_lvalue),
2609 : t_rvalue);
2610 4083 : modify_retval = build2 (MODIFY_EXPR, return_type,
2611 : t_lvalue, t_rvalue);
2612 4083 : if (loc)
2613 78 : set_tree_location (modify_retval, loc);
2614 : }
2615 4717 : tree return_stmt = build1 (RETURN_EXPR, return_type,
2616 : modify_retval);
2617 4717 : if (loc)
2618 90 : set_tree_location (return_stmt, loc);
2619 :
2620 4717 : add_stmt (return_stmt);
2621 4717 : }
2622 :
2623 : /* Helper function for playback::block::add_switch.
2624 : Construct a case label for the given range, followed by a goto stmt
2625 : to the given block, appending them to stmt list *ptr_t_switch_body. */
2626 :
2627 : static void
2628 125 : add_case (tree *ptr_t_switch_body,
2629 : tree t_low_value,
2630 : tree t_high_value,
2631 : playback::block *dest_block)
2632 : {
2633 125 : tree t_label = create_artificial_label (UNKNOWN_LOCATION);
2634 125 : DECL_CONTEXT (t_label) = dest_block->get_function ()->as_fndecl ();
2635 :
2636 125 : tree t_case_label =
2637 125 : build_case_label (t_low_value, t_high_value, t_label);
2638 125 : append_to_statement_list (t_case_label, ptr_t_switch_body);
2639 :
2640 125 : tree t_goto_stmt =
2641 125 : build1 (GOTO_EXPR, void_type_node, dest_block->as_label_decl ());
2642 125 : append_to_statement_list (t_goto_stmt, ptr_t_switch_body);
2643 125 : }
2644 :
2645 : /* Add a switch statement to the function's statement list.
2646 :
2647 : We create a switch body, and populate it with case labels, each
2648 : followed by a goto to the desired block. */
2649 :
2650 : void
2651 25 : playback::block::
2652 : add_switch (location *loc,
2653 : rvalue *expr,
2654 : block *default_block,
2655 : const auto_vec <case_> *cases)
2656 : {
2657 : /* Compare with:
2658 : - c/c-typeck.cc: c_start_case
2659 : - c-family/c-common.cc:c_add_case_label
2660 : - java/expr.cc:expand_java_switch and expand_java_add_case
2661 : We've already rejected overlaps and duplicates in
2662 : libgccjit.cc:case_range_validator::validate. */
2663 :
2664 25 : tree t_expr = expr->as_tree ();
2665 25 : tree t_type = TREE_TYPE (t_expr);
2666 :
2667 25 : tree t_switch_body = alloc_stmt_list ();
2668 :
2669 25 : int i;
2670 25 : case_ *c;
2671 125 : FOR_EACH_VEC_ELT (*cases, i, c)
2672 : {
2673 100 : tree t_low_value = c->m_min_value->as_tree ();
2674 100 : tree t_high_value = c->m_max_value->as_tree ();
2675 100 : add_case (&t_switch_body, t_low_value, t_high_value, c->m_dest_block);
2676 : }
2677 : /* Default label. */
2678 25 : add_case (&t_switch_body, NULL_TREE, NULL_TREE, default_block);
2679 :
2680 25 : tree switch_stmt = build2 (SWITCH_EXPR, t_type, t_expr, t_switch_body);
2681 25 : if (loc)
2682 0 : set_tree_location (switch_stmt, loc);
2683 25 : add_stmt (switch_stmt);
2684 25 : }
2685 :
2686 : /* Convert OPERANDS to a tree-based chain suitable for creating an
2687 : extended asm stmt.
2688 : Compare with c_parser_asm_operands. */
2689 :
2690 : static tree
2691 140 : build_operand_chain (const auto_vec <playback::asm_operand> *operands)
2692 : {
2693 140 : tree result = NULL_TREE;
2694 140 : unsigned i;
2695 140 : playback::asm_operand *asm_op;
2696 240 : FOR_EACH_VEC_ELT (*operands, i, asm_op)
2697 : {
2698 100 : tree name = build_string (asm_op->m_asm_symbolic_name);
2699 100 : tree str = build_string (asm_op->m_constraint);
2700 100 : tree value = asm_op->m_expr;
2701 100 : result = chainon (result,
2702 : build_tree_list (build_tree_list (name, str),
2703 : value));
2704 : }
2705 140 : return result;
2706 : }
2707 :
2708 : /* Convert CLOBBERS to a tree-based list suitable for creating an
2709 : extended asm stmt.
2710 : Compare with c_parser_asm_clobbers. */
2711 :
2712 : static tree
2713 70 : build_clobbers (const auto_vec <const char *> *clobbers)
2714 : {
2715 70 : tree list = NULL_TREE;
2716 70 : unsigned i;
2717 70 : const char *clobber;
2718 120 : FOR_EACH_VEC_ELT (*clobbers, i, clobber)
2719 : {
2720 50 : tree str = build_string (clobber);
2721 50 : list = tree_cons (NULL_TREE, str, list);
2722 : }
2723 70 : return list;
2724 : }
2725 :
2726 : /* Convert BLOCKS to a tree-based list suitable for creating an
2727 : extended asm stmt.
2728 : Compare with c_parser_asm_goto_operands. */
2729 :
2730 : static tree
2731 70 : build_goto_operands (const auto_vec <playback::block *> *blocks)
2732 : {
2733 70 : tree list = NULL_TREE;
2734 70 : unsigned i;
2735 70 : playback::block *b;
2736 90 : FOR_EACH_VEC_ELT (*blocks, i, b)
2737 : {
2738 20 : tree label = b->as_label_decl ();
2739 20 : tree name = build_string (IDENTIFIER_POINTER (DECL_NAME (label)));
2740 20 : TREE_USED (label) = 1;
2741 20 : list = tree_cons (name, label, list);
2742 : }
2743 70 : return nreverse (list);
2744 : }
2745 :
2746 : /* Add an extended asm statement to this block.
2747 :
2748 : Compare with c_parser_asm_statement (in c/c-parser.cc)
2749 : and build_asm_expr (in c/c-typeck.cc). */
2750 :
2751 : void
2752 70 : playback::block::add_extended_asm (location *loc,
2753 : const char *asm_template,
2754 : bool is_volatile,
2755 : bool is_inline,
2756 : const auto_vec <asm_operand> *outputs,
2757 : const auto_vec <asm_operand> *inputs,
2758 : const auto_vec <const char *> *clobbers,
2759 : const auto_vec <block *> *goto_blocks)
2760 : {
2761 70 : tree t_string = build_string (asm_template);
2762 70 : tree t_outputs = build_operand_chain (outputs);
2763 70 : tree t_inputs = build_operand_chain (inputs);
2764 70 : tree t_clobbers = build_clobbers (clobbers);
2765 70 : tree t_labels = build_goto_operands (goto_blocks);
2766 70 : t_string
2767 70 : = resolve_asm_operand_names (t_string, t_outputs, t_inputs, t_labels);
2768 70 : tree asm_stmt
2769 70 : = build5 (ASM_EXPR, void_type_node,
2770 : t_string, t_outputs, t_inputs, t_clobbers, t_labels);
2771 :
2772 : /* asm statements without outputs, including simple ones, are treated
2773 : as volatile. */
2774 110 : ASM_VOLATILE_P (asm_stmt) = (outputs->length () == 0);
2775 70 : ASM_BASIC_P (asm_stmt) = 0;
2776 70 : ASM_INLINE_P (asm_stmt) = is_inline;
2777 70 : if (is_volatile)
2778 20 : ASM_VOLATILE_P (asm_stmt) = 1;
2779 70 : if (loc)
2780 0 : set_tree_location (asm_stmt, loc);
2781 70 : add_stmt (asm_stmt);
2782 70 : }
2783 :
2784 : /* Constructor for gcc::jit::playback::block. */
2785 :
2786 7133 : playback::block::
2787 : block (function *func,
2788 : const char *name)
2789 7133 : : m_func (func),
2790 7133 : m_stmts ()
2791 : {
2792 7133 : tree identifier;
2793 :
2794 7133 : gcc_assert (func);
2795 : // name can be NULL
2796 7133 : if (name)
2797 5951 : identifier = get_identifier (name);
2798 : else
2799 : identifier = NULL;
2800 7133 : m_label_decl = build_decl (UNKNOWN_LOCATION, LABEL_DECL,
2801 : identifier, void_type_node);
2802 7133 : DECL_CONTEXT (m_label_decl) = func->as_fndecl ();
2803 7133 : m_label_expr = NULL;
2804 7133 : }
2805 :
2806 : // This is basically std::lock_guard but it can call the private lock/unlock
2807 : // members of playback::context.
2808 : struct playback::context::scoped_lock
2809 : {
2810 1381 : scoped_lock (context &ctx) : m_ctx (&ctx) { m_ctx->lock (); }
2811 2762 : ~scoped_lock () { m_ctx->unlock (); }
2812 :
2813 : context *m_ctx;
2814 :
2815 : // Not movable or copyable.
2816 : scoped_lock (scoped_lock &&) = delete;
2817 : scoped_lock &operator= (scoped_lock &&) = delete;
2818 : };
2819 :
2820 : /* Compile a playback::context:
2821 :
2822 : - Use the context's options to cconstruct command-line options, and
2823 : call into the rest of GCC (toplev::main).
2824 : - Assuming it succeeds, we have a .s file.
2825 : - We then run the "postprocess" vfunc:
2826 :
2827 : (A) In-memory compile ("gcc_jit_context_compile")
2828 :
2829 : For an in-memory compile we have the playback::compile_to_memory
2830 : subclass; "postprocess" will convert the .s file to a .so DSO,
2831 : and load it in memory (via dlopen), wrapping the result up as
2832 : a jit::result and returning it.
2833 :
2834 : (B) Compile to file ("gcc_jit_context_compile_to_file")
2835 :
2836 : When compiling to a file, we have the playback::compile_to_file
2837 : subclass; "postprocess" will either copy the .s file to the
2838 : destination (for GCC_JIT_OUTPUT_KIND_ASSEMBLER), or invoke
2839 : the driver to convert it as necessary, copying the result. */
2840 :
2841 : void
2842 1380 : playback::context::
2843 : compile ()
2844 : {
2845 1380 : JIT_LOG_SCOPE (get_logger ());
2846 :
2847 1380 : const char *ctxt_progname;
2848 :
2849 1380 : int keep_intermediates =
2850 1380 : get_bool_option (GCC_JIT_BOOL_OPTION_KEEP_INTERMEDIATES);
2851 :
2852 1380 : m_tempdir = new tempdir (get_logger (), keep_intermediates);
2853 1381 : if (!m_tempdir->create ())
2854 : return;
2855 :
2856 : /* Call into the rest of gcc.
2857 : For now, we have to assemble command-line options to pass into
2858 : toplev::main, so that they can be parsed. */
2859 :
2860 : /* Pass in user-provided program name as argv0, if any, so that it
2861 : makes it into GCC's "progname" global, used in various diagnostics. */
2862 1381 : ctxt_progname = get_str_option (GCC_JIT_STR_OPTION_PROGNAME);
2863 :
2864 1381 : if (!ctxt_progname)
2865 134 : ctxt_progname = "libgccjit.so";
2866 :
2867 1381 : auto_vec <recording::requested_dump> requested_dumps;
2868 1381 : m_recording_ctxt->get_all_requested_dumps (&requested_dumps);
2869 :
2870 : /* Acquire the JIT mutex and set "this" as the active playback ctxt. */
2871 1381 : scoped_lock lock(*this);
2872 :
2873 1381 : auto_string_vec fake_args;
2874 1381 : make_fake_args (&fake_args, ctxt_progname, &requested_dumps);
2875 1381 : if (errors_occurred ())
2876 : return;
2877 :
2878 : /* This runs the compiler. */
2879 1381 : toplev toplev (get_timer (), /* external_timer */
2880 1381 : false); /* init_signals */
2881 1381 : enter_scope ("toplev::main");
2882 1381 : if (get_logger ())
2883 7339 : for (unsigned i = 0; i < fake_args.length (); i++)
2884 6712 : get_logger ()->log ("argv[%i]: %s", i, fake_args[i]);
2885 :
2886 : /* Add a trailing null to argvec; this is not counted in argc. */
2887 1381 : fake_args.safe_push (nullptr);
2888 2762 : toplev.main (/* The trailing null is not counted in argv. */
2889 1381 : fake_args.length () - 1,
2890 : const_cast <char **> (fake_args.address ()));
2891 1381 : exit_scope ("toplev::main");
2892 :
2893 : /* Extracting dumps makes use of the gcc::dump_manager, hence we
2894 : need to do it between toplev::main (which creates the dump manager)
2895 : and toplev::finalize (which deletes it). */
2896 1381 : extract_any_requested_dumps (&requested_dumps);
2897 :
2898 : /* Clean up the compiler. */
2899 1381 : enter_scope ("toplev::finalize");
2900 1381 : toplev.finalize ();
2901 1381 : exit_scope ("toplev::finalize");
2902 :
2903 : /* Ideally we would release the jit mutex here, but we can't yet since
2904 : followup activities use timevars, which are global state. */
2905 :
2906 1381 : if (errors_occurred ())
2907 55 : return;
2908 :
2909 1326 : if (get_bool_option (GCC_JIT_BOOL_OPTION_DUMP_GENERATED_CODE))
2910 0 : dump_generated_code ();
2911 :
2912 : /* We now have a .s file.
2913 :
2914 : Run any postprocessing steps. This will either convert the .s file to
2915 : a .so DSO, and load it in memory (playback::compile_to_memory), or
2916 : convert the .s file to the requested output format, and copy it to a
2917 : given file (playback::compile_to_file). */
2918 1326 : postprocess (ctxt_progname);
2919 1381 : }
2920 :
2921 : /* Implementation of class gcc::jit::playback::compile_to_memory,
2922 : a subclass of gcc::jit::playback::context. */
2923 :
2924 : /* playback::compile_to_memory's trivial constructor. */
2925 :
2926 1193 : playback::compile_to_memory::compile_to_memory (recording::context *ctxt) :
2927 : playback::context (ctxt),
2928 1193 : m_result (NULL)
2929 : {
2930 1192 : JIT_LOG_SCOPE (get_logger ());
2931 1192 : }
2932 :
2933 : /* Implementation of the playback::context::process vfunc for compiling
2934 : to memory.
2935 :
2936 : Convert the .s file to a .so DSO, and load it in memory (via dlopen),
2937 : wrapping the result up as a jit::result and returning it. */
2938 :
2939 : void
2940 1138 : playback::compile_to_memory::postprocess (const char *ctxt_progname)
2941 : {
2942 1138 : JIT_LOG_SCOPE (get_logger ());
2943 1138 : convert_to_dso (ctxt_progname);
2944 1138 : if (errors_occurred ())
2945 5 : return;
2946 1133 : m_result = dlopen_built_dso ();
2947 1138 : }
2948 :
2949 : /* Implementation of class gcc::jit::playback::compile_to_file,
2950 : a subclass of gcc::jit::playback::context. */
2951 :
2952 : /* playback::compile_to_file's trivial constructor. */
2953 :
2954 126 : playback::compile_to_file::compile_to_file (recording::context *ctxt,
2955 : enum gcc_jit_output_kind output_kind,
2956 : const char *output_path) :
2957 : playback::context (ctxt),
2958 126 : m_output_kind (output_kind),
2959 126 : m_output_path (output_path)
2960 : {
2961 126 : JIT_LOG_SCOPE (get_logger ());
2962 126 : }
2963 :
2964 : /* Implementation of the playback::context::process vfunc for compiling
2965 : to a file.
2966 :
2967 : Either copy the .s file to the given destination (for
2968 : GCC_JIT_OUTPUT_KIND_ASSEMBLER), or invoke the driver to convert it
2969 : as necessary, copying the result. */
2970 :
2971 : void
2972 126 : playback::compile_to_file::postprocess (const char *ctxt_progname)
2973 : {
2974 126 : JIT_LOG_SCOPE (get_logger ());
2975 :
2976 : /* The driver takes different actions based on the filename, so
2977 : we provide a filename with an appropriate suffix for the
2978 : output kind, and then copy it up to the user-provided path,
2979 : rather than directly compiling it to the requested output path. */
2980 :
2981 126 : switch (m_output_kind)
2982 : {
2983 0 : default:
2984 0 : gcc_unreachable ();
2985 :
2986 105 : case GCC_JIT_OUTPUT_KIND_ASSEMBLER:
2987 105 : copy_file (get_tempdir ()->get_path_s_file (),
2988 : m_output_path);
2989 : /* The .s file is automatically unlinked by tempdir::~tempdir. */
2990 105 : break;
2991 :
2992 5 : case GCC_JIT_OUTPUT_KIND_OBJECT_FILE:
2993 5 : {
2994 5 : char *tmp_o_path = ::concat (get_tempdir ()->get_path (),
2995 : "/fake.o",
2996 : NULL);
2997 5 : invoke_driver (ctxt_progname,
2998 : get_tempdir ()->get_path_s_file (),
2999 : tmp_o_path,
3000 : TV_ASSEMBLE,
3001 : false, /* bool shared, */
3002 : false);/* bool run_linker */
3003 5 : if (!errors_occurred ())
3004 : {
3005 5 : copy_file (tmp_o_path,
3006 : m_output_path);
3007 5 : get_tempdir ()->add_temp_file (tmp_o_path);
3008 : }
3009 : else
3010 0 : free (tmp_o_path);
3011 : }
3012 : break;
3013 :
3014 5 : case GCC_JIT_OUTPUT_KIND_DYNAMIC_LIBRARY:
3015 5 : invoke_driver (ctxt_progname,
3016 : get_tempdir ()->get_path_s_file (),
3017 : get_tempdir ()->get_path_so_file (),
3018 : TV_ASSEMBLE,
3019 : true, /* bool shared, */
3020 : true);/* bool run_linker */
3021 5 : if (!errors_occurred ())
3022 5 : copy_file (get_tempdir ()->get_path_so_file (),
3023 : m_output_path);
3024 : /* The .so file is automatically unlinked by tempdir::~tempdir. */
3025 : break;
3026 :
3027 11 : case GCC_JIT_OUTPUT_KIND_EXECUTABLE:
3028 11 : {
3029 11 : char *tmp_exe_path = ::concat (get_tempdir ()->get_path (),
3030 : "/fake.exe",
3031 : NULL);
3032 11 : invoke_driver (ctxt_progname,
3033 : get_tempdir ()->get_path_s_file (),
3034 : tmp_exe_path,
3035 : TV_ASSEMBLE,
3036 : false, /* bool shared, */
3037 : true);/* bool run_linker */
3038 11 : if (!errors_occurred ())
3039 : {
3040 11 : copy_file (tmp_exe_path,
3041 : m_output_path);
3042 11 : get_tempdir ()->add_temp_file (tmp_exe_path);
3043 : }
3044 : else
3045 0 : free (tmp_exe_path);
3046 : }
3047 : break;
3048 :
3049 : }
3050 :
3051 126 : }
3052 :
3053 : /* Copy SRC_PATH to DST_PATH, preserving permission bits (in particular,
3054 : the "executable" bits).
3055 :
3056 : Any errors that occur are reported on the context and hence count as
3057 : a failure of the compile.
3058 :
3059 : We can't in general hardlink or use "rename" from the tempdir since
3060 : it might be on a different filesystem to the destination. For example,
3061 : I get EXDEV: "Invalid cross-device link". */
3062 :
3063 : void
3064 126 : playback::compile_to_file::copy_file (const char *src_path,
3065 : const char *dst_path)
3066 : {
3067 126 : JIT_LOG_SCOPE (get_logger ());
3068 126 : if (get_logger ())
3069 : {
3070 125 : get_logger ()->log ("src_path: %s", src_path);
3071 125 : get_logger ()->log ("dst_path: %s", dst_path);
3072 : }
3073 :
3074 126 : FILE *f_in = NULL;
3075 126 : FILE *f_out = NULL;
3076 126 : size_t total_sz_in = 0;
3077 126 : size_t total_sz_out = 0;
3078 126 : char buf[4096];
3079 126 : size_t sz_in;
3080 126 : struct stat stat_buf;
3081 :
3082 126 : f_in = fopen (src_path, "rb");
3083 126 : if (!f_in)
3084 : {
3085 0 : add_error (NULL,
3086 : "unable to open %s for reading: %s",
3087 : src_path,
3088 0 : xstrerror (errno));
3089 0 : return;
3090 : }
3091 :
3092 : /* Use stat on the filedescriptor to get the mode,
3093 : so that we can copy it over (in particular, the
3094 : "executable" bits). */
3095 126 : if (fstat (fileno (f_in), &stat_buf) == -1)
3096 : {
3097 0 : add_error (NULL,
3098 : "unable to fstat %s: %s",
3099 : src_path,
3100 0 : xstrerror (errno));
3101 0 : fclose (f_in);
3102 0 : return;
3103 : }
3104 :
3105 126 : f_out = fopen (dst_path, "wb");
3106 126 : if (!f_out)
3107 : {
3108 0 : add_error (NULL,
3109 : "unable to open %s for writing: %s",
3110 : dst_path,
3111 0 : xstrerror (errno));
3112 0 : fclose (f_in);
3113 0 : return;
3114 : }
3115 :
3116 332 : while ( (sz_in = fread (buf, 1, sizeof (buf), f_in)) )
3117 : {
3118 206 : total_sz_in += sz_in;
3119 206 : size_t sz_out_remaining = sz_in;
3120 206 : size_t sz_out_so_far = 0;
3121 412 : while (sz_out_remaining)
3122 : {
3123 206 : size_t sz_out = fwrite (buf + sz_out_so_far,
3124 : 1,
3125 : sz_out_remaining,
3126 : f_out);
3127 206 : gcc_assert (sz_out <= sz_out_remaining);
3128 206 : if (!sz_out)
3129 : {
3130 0 : add_error (NULL,
3131 : "error writing to %s: %s",
3132 : dst_path,
3133 0 : xstrerror (errno));
3134 0 : fclose (f_in);
3135 0 : fclose (f_out);
3136 0 : return;
3137 : }
3138 206 : total_sz_out += sz_out;
3139 206 : sz_out_so_far += sz_out;
3140 206 : sz_out_remaining -= sz_out;
3141 : }
3142 206 : gcc_assert (sz_out_so_far == sz_in);
3143 : }
3144 :
3145 126 : if (!feof (f_in))
3146 0 : add_error (NULL,
3147 : "error reading from %s: %s",
3148 : src_path,
3149 0 : xstrerror (errno));
3150 :
3151 126 : fclose (f_in);
3152 :
3153 126 : gcc_assert (total_sz_in == total_sz_out);
3154 126 : if (get_logger ())
3155 125 : get_logger ()->log ("total bytes copied: %zu", total_sz_out);
3156 :
3157 : /* fchmod does not exist in Windows. */
3158 : #ifndef _WIN32
3159 : /* Set the permissions of the copy to those of the original file,
3160 : in particular the "executable" bits. */
3161 126 : if (fchmod (fileno (f_out), stat_buf.st_mode) == -1)
3162 0 : add_error (NULL,
3163 : "error setting mode of %s: %s",
3164 : dst_path,
3165 0 : xstrerror (errno));
3166 : #endif
3167 :
3168 126 : fclose (f_out);
3169 126 : }
3170 :
3171 : /* Helper functions for gcc::jit::playback::context::compile. */
3172 :
3173 : /* This mutex guards gcc::jit::recording::context::compile, so that only
3174 : one thread can be accessing the bulk of GCC's state at once. */
3175 :
3176 : static std::mutex jit_mutex;
3177 :
3178 : /* Acquire jit_mutex and set "this" as the active playback ctxt. */
3179 :
3180 : void
3181 1381 : playback::context::lock ()
3182 : {
3183 1381 : auto_timevar tv (get_timer (), TV_JIT_ACQUIRING_MUTEX);
3184 :
3185 : /* Acquire the big GCC mutex. */
3186 1381 : JIT_LOG_SCOPE (get_logger ());
3187 1381 : jit_mutex.lock ();
3188 1381 : gcc_assert (active_playback_ctxt == NULL);
3189 1381 : active_playback_ctxt = this;
3190 1381 : }
3191 :
3192 : /* Release jit_mutex and clear the active playback ctxt. */
3193 :
3194 : void
3195 1381 : playback::context::unlock ()
3196 : {
3197 : /* Release the big GCC mutex. */
3198 1381 : JIT_LOG_SCOPE (get_logger ());
3199 1381 : gcc_assert (active_playback_ctxt == this);
3200 1381 : active_playback_ctxt = NULL;
3201 1381 : jit_mutex.unlock ();
3202 1381 : }
3203 :
3204 : /* Callback used by gcc::jit::playback::context::make_fake_args when
3205 : invoking driver_get_configure_time_options.
3206 : Populate a vec <char * > with the configure-time options. */
3207 :
3208 : static void
3209 266 : append_arg_from_driver (const char *option, void *user_data)
3210 : {
3211 266 : gcc_assert (option);
3212 266 : gcc_assert (user_data);
3213 266 : vec <char *> *argvec = static_cast <vec <char *> *> (user_data);
3214 266 : argvec->safe_push (concat ("-", option, NULL));
3215 266 : }
3216 :
3217 : /* Build a fake argv for toplev::main from the options set
3218 : by the user on the context . */
3219 :
3220 : void
3221 1381 : playback::context::
3222 : make_fake_args (vec <char *> *argvec,
3223 : const char *ctxt_progname,
3224 : vec <recording::requested_dump> *requested_dumps)
3225 : {
3226 1381 : JIT_LOG_SCOPE (get_logger ());
3227 :
3228 : #define ADD_ARG(arg) argvec->safe_push (xstrdup (arg))
3229 : #define ADD_ARG_TAKE_OWNERSHIP(arg) argvec->safe_push (arg)
3230 :
3231 1381 : ADD_ARG (ctxt_progname);
3232 1381 : ADD_ARG (get_path_c_file ());
3233 1381 : ADD_ARG ("-fPIC");
3234 :
3235 : /* Handle int options: */
3236 1381 : switch (get_int_option (GCC_JIT_INT_OPTION_OPTIMIZATION_LEVEL))
3237 : {
3238 0 : default:
3239 0 : add_error (NULL,
3240 : "unrecognized optimization level: %i",
3241 : get_int_option (GCC_JIT_INT_OPTION_OPTIMIZATION_LEVEL));
3242 0 : return;
3243 :
3244 194 : case 0:
3245 194 : ADD_ARG ("-O0");
3246 194 : break;
3247 :
3248 105 : case 1:
3249 105 : ADD_ARG ("-O1");
3250 105 : break;
3251 :
3252 115 : case 2:
3253 115 : ADD_ARG ("-O2");
3254 115 : break;
3255 :
3256 967 : case 3:
3257 967 : ADD_ARG ("-O3");
3258 967 : break;
3259 : }
3260 : /* What about -Os? */
3261 :
3262 : /* Handle bool options: */
3263 1381 : if (get_bool_option (GCC_JIT_BOOL_OPTION_DEBUGINFO))
3264 862 : ADD_ARG ("-g");
3265 :
3266 : /* Suppress timing (and other) info. */
3267 1381 : if (!get_bool_option (GCC_JIT_BOOL_OPTION_DUMP_SUMMARY))
3268 : {
3269 1381 : ADD_ARG ("-quiet");
3270 1381 : quiet_flag = 1;
3271 : }
3272 :
3273 : /* Aggressively garbage-collect, to shake out bugs: */
3274 1381 : if (get_bool_option (GCC_JIT_BOOL_OPTION_SELFCHECK_GC))
3275 : {
3276 852 : ADD_ARG ("--param=ggc-min-expand=0");
3277 852 : ADD_ARG ("--param=ggc-min-heapsize=0");
3278 : }
3279 :
3280 1381 : if (get_bool_option (GCC_JIT_BOOL_OPTION_DUMP_EVERYTHING))
3281 : {
3282 0 : ADD_ARG ("-fdump-tree-all");
3283 0 : ADD_ARG ("-fdump-rtl-all");
3284 0 : ADD_ARG ("-fdump-ipa-all");
3285 : }
3286 :
3287 : /* Add "-fdump-" options for any calls to
3288 : gcc_jit_context_enable_dump. */
3289 1381 : {
3290 1381 : int i;
3291 1381 : recording::requested_dump *d;
3292 1446 : FOR_EACH_VEC_ELT (*requested_dumps, i, d)
3293 : {
3294 65 : char *arg = concat ("-fdump-", d->m_dumpname, NULL);
3295 65 : ADD_ARG_TAKE_OWNERSHIP (arg);
3296 : }
3297 : }
3298 :
3299 : /* PR jit/64810: Add any target-specific default options
3300 : from OPTION_DEFAULT_SPECS, normally provided by the driver
3301 : in the non-jit case.
3302 :
3303 : The target-specific code can define OPTION_DEFAULT_SPECS:
3304 : default command options in the form of spec macros for the
3305 : driver to expand ().
3306 :
3307 : For cc1 etc, the driver processes OPTION_DEFAULT_SPECS and,
3308 : if not overridden, injects the defaults as extra arguments to
3309 : cc1 etc.
3310 : For the jit case, we need to add these arguments here. The
3311 : input format (using the specs language) means that we have to run
3312 : part of the driver code here (driver_get_configure_time_options).
3313 :
3314 : To avoid running the spec-expansion code every time, we just do
3315 : it the first time (via a function-static flag), saving the result
3316 : into a function-static vec.
3317 : This flag and vec are global state (i.e. per-process).
3318 : They are guarded by the jit mutex. */
3319 1381 : {
3320 1381 : static bool have_configure_time_options = false;
3321 1381 : static vec <char *> configure_time_options;
3322 :
3323 1381 : if (have_configure_time_options)
3324 1248 : log ("reusing cached configure-time options");
3325 : else
3326 : {
3327 133 : have_configure_time_options = true;
3328 133 : log ("getting configure-time options from driver");
3329 133 : driver_get_configure_time_options (append_arg_from_driver,
3330 : &configure_time_options);
3331 : }
3332 :
3333 1381 : int i;
3334 1381 : char *opt;
3335 :
3336 1381 : if (get_logger ())
3337 1881 : FOR_EACH_VEC_ELT (configure_time_options, i, opt)
3338 1254 : log ("configure_time_options[%i]: %s", i, opt);
3339 :
3340 : /* configure_time_options should now contain the expanded options
3341 : from OPTION_DEFAULT_SPECS (if any). */
3342 4143 : FOR_EACH_VEC_ELT (configure_time_options, i, opt)
3343 : {
3344 2762 : gcc_assert (opt);
3345 2762 : gcc_assert (opt[0] == '-');
3346 2762 : ADD_ARG (opt);
3347 : }
3348 : }
3349 :
3350 1381 : if (get_timer ())
3351 400 : ADD_ARG ("-ftime-report");
3352 :
3353 : /* Add any user-provided extra options, starting with any from
3354 : parent contexts. */
3355 1381 : m_recording_ctxt->append_command_line_options (argvec);
3356 :
3357 : #undef ADD_ARG
3358 : #undef ADD_ARG_TAKE_OWNERSHIP
3359 1381 : }
3360 :
3361 : /* The second half of the implementation of gcc_jit_context_enable_dump.
3362 : Iterate through the requested dumps, reading the underlying files
3363 : into heap-allocated buffers, writing pointers to the buffers into
3364 : the char ** pointers provided by client code.
3365 : Client code is responsible for calling free on the results. */
3366 :
3367 : void
3368 1381 : playback::context::
3369 : extract_any_requested_dumps (vec <recording::requested_dump> *requested_dumps)
3370 : {
3371 1381 : JIT_LOG_SCOPE (get_logger ());
3372 :
3373 1381 : int i;
3374 1381 : recording::requested_dump *d;
3375 2827 : FOR_EACH_VEC_ELT (*requested_dumps, i, d)
3376 : {
3377 65 : dump_file_info *dfi;
3378 65 : char *filename;
3379 65 : char *content;
3380 :
3381 65 : dfi = g->get_dumps ()->get_dump_file_info_by_switch (d->m_dumpname);
3382 65 : if (!dfi)
3383 : {
3384 5 : add_error (NULL, "unrecognized dump: %s", d->m_dumpname);
3385 5 : continue;
3386 : }
3387 :
3388 60 : filename = g->get_dumps ()->get_dump_file_name (dfi);
3389 60 : content = read_dump_file (filename);
3390 60 : *(d->m_out_ptr) = content;
3391 60 : m_tempdir->add_temp_file (filename);
3392 : }
3393 1381 : }
3394 :
3395 : /* Helper function for playback::context::extract_any_requested_dumps
3396 : (itself for use in implementation of gcc_jit_context_enable_dump).
3397 :
3398 : Attempt to read the complete file at the given path, returning the
3399 : bytes found there as a buffer.
3400 : The caller is responsible for calling free on the result.
3401 : Errors will be reported on the context, and lead to NULL being
3402 : returned; an out-of-memory error will terminate the process. */
3403 :
3404 : char *
3405 60 : playback::context::read_dump_file (const char *path)
3406 : {
3407 60 : char *result = NULL;
3408 60 : size_t total_sz = 0;
3409 60 : char buf[4096];
3410 60 : size_t sz;
3411 60 : FILE *f_in;
3412 :
3413 60 : f_in = fopen (path, "r");
3414 60 : if (!f_in)
3415 : {
3416 0 : add_error (NULL, "unable to open %s for reading", path);
3417 0 : return NULL;
3418 : }
3419 :
3420 10530 : while ( (sz = fread (buf, 1, sizeof (buf), f_in)) )
3421 : {
3422 10470 : size_t old_total_sz = total_sz;
3423 10470 : total_sz += sz;
3424 10470 : result = reinterpret_cast <char *> (xrealloc (result, total_sz + 1));
3425 10470 : memcpy (result + old_total_sz, buf, sz);
3426 : }
3427 :
3428 60 : if (!feof (f_in))
3429 : {
3430 0 : add_error (NULL, "error reading from %s", path);
3431 0 : free (result);
3432 0 : fclose (f_in);
3433 0 : return NULL;
3434 : }
3435 :
3436 60 : fclose (f_in);
3437 :
3438 60 : if (result)
3439 : {
3440 60 : result[total_sz] = '\0';
3441 60 : return result;
3442 : }
3443 : else
3444 0 : return xstrdup ("");
3445 : }
3446 :
3447 : /* Part of playback::context::compile ().
3448 :
3449 : We have a .s file; we want a .so file.
3450 : We could reuse parts of gcc/gcc.cc to do this.
3451 : For now, just use the driver binary from the install, as
3452 : named in gcc-driver-name.h
3453 : e.g. "x86_64-unknown-linux-gnu-gcc-5.0.0". */
3454 :
3455 : void
3456 1138 : playback::context::
3457 : convert_to_dso (const char *ctxt_progname)
3458 : {
3459 1138 : JIT_LOG_SCOPE (get_logger ());
3460 :
3461 1138 : invoke_driver (ctxt_progname,
3462 1138 : m_tempdir->get_path_s_file (),
3463 1138 : m_tempdir->get_path_so_file (),
3464 : TV_ASSEMBLE,
3465 : true, /* bool shared, */
3466 : true);/* bool run_linker */
3467 1138 : }
3468 :
3469 : static const char * const gcc_driver_name = GCC_DRIVER_NAME;
3470 :
3471 : void
3472 1159 : playback::context::
3473 : invoke_driver (const char *ctxt_progname,
3474 : const char *input_file,
3475 : const char *output_file,
3476 : timevar_id_t tv_id,
3477 : bool shared,
3478 : bool run_linker)
3479 : {
3480 1159 : JIT_LOG_SCOPE (get_logger ());
3481 :
3482 1159 : bool embedded_driver
3483 1159 : = !get_inner_bool_option (INNER_BOOL_OPTION_USE_EXTERNAL_DRIVER);
3484 :
3485 : /* Currently this lumps together both assembling and linking into
3486 : TV_ASSEMBLE. */
3487 1159 : auto_timevar assemble_timevar (get_timer (), tv_id);
3488 1159 : auto_string_vec argvec;
3489 : #define ADD_ARG(arg) argvec.safe_push (xstrdup (arg))
3490 :
3491 1159 : ADD_ARG (gcc_driver_name);
3492 :
3493 1159 : add_multilib_driver_arguments (&argvec);
3494 :
3495 1159 : if (shared)
3496 1143 : ADD_ARG ("-shared");
3497 :
3498 1159 : if (!run_linker)
3499 5 : ADD_ARG ("-c");
3500 :
3501 1159 : ADD_ARG (input_file);
3502 1159 : ADD_ARG ("-o");
3503 1159 : ADD_ARG (output_file);
3504 :
3505 : /* Don't use the linker plugin.
3506 : If running with just a "make" and not a "make install", then we'd
3507 : run into
3508 : "fatal error: -fuse-linker-plugin, but liblto_plugin.so not found"
3509 : libto_plugin is a .la at build time, with it becoming installed with
3510 : ".so" suffix: i.e. it doesn't exist with a .so suffix until install
3511 : time. */
3512 1159 : ADD_ARG ("-fno-use-linker-plugin");
3513 :
3514 : #if defined (DARWIN_X86) || defined (DARWIN_PPC)
3515 : /* macOS's linker defaults to treating undefined symbols as errors.
3516 : If the context has any imported functions or globals they will be
3517 : undefined until the .so is dynamically-linked into the process.
3518 : Ensure that the driver passes in "-undefined dynamic_lookup" to the
3519 : linker. */
3520 : ADD_ARG ("-Wl,-undefined,dynamic_lookup");
3521 : #endif
3522 :
3523 1159 : if (0)
3524 : ADD_ARG ("-v");
3525 :
3526 : /* Add any user-provided driver extra options. */
3527 :
3528 1159 : m_recording_ctxt->append_driver_options (&argvec);
3529 :
3530 : #undef ADD_ARG
3531 :
3532 : /* pex_one's error-handling requires pname to be non-NULL. */
3533 1159 : gcc_assert (ctxt_progname);
3534 :
3535 1159 : if (get_logger ())
3536 3661 : for (unsigned i = 0; i < argvec.length (); i++)
3537 3204 : get_logger ()->log ("argv[%i]: %s", i, argvec[i]);
3538 :
3539 1159 : if (embedded_driver)
3540 1154 : invoke_embedded_driver (&argvec);
3541 : else
3542 5 : invoke_external_driver (ctxt_progname, &argvec);
3543 1159 : }
3544 :
3545 : void
3546 1154 : playback::context::
3547 : invoke_embedded_driver (const vec <char *> *argvec)
3548 : {
3549 1154 : JIT_LOG_SCOPE (get_logger ());
3550 1154 : driver d (true, /* can_finalize */
3551 1154 : false); /* debug */
3552 2308 : int result = d.main (argvec->length (),
3553 1154 : const_cast <char **> (argvec->address ()));
3554 1154 : d.finalize ();
3555 1154 : if (result)
3556 0 : add_error (NULL, "error invoking gcc driver");
3557 1154 : }
3558 :
3559 : void
3560 5 : playback::context::
3561 : invoke_external_driver (const char *ctxt_progname,
3562 : vec <char *> *argvec)
3563 : {
3564 5 : JIT_LOG_SCOPE (get_logger ());
3565 5 : const char *errmsg;
3566 5 : int exit_status = 0;
3567 5 : int err = 0;
3568 :
3569 : /* pex argv arrays are NULL-terminated. */
3570 5 : argvec->safe_push (NULL);
3571 :
3572 5 : errmsg = pex_one (PEX_SEARCH, /* int flags, */
3573 : gcc_driver_name,
3574 5 : const_cast <char *const *> (argvec->address ()),
3575 : ctxt_progname, /* const char *pname */
3576 : NULL, /* const char *outname */
3577 : NULL, /* const char *errname */
3578 : &exit_status, /* int *status */
3579 : &err); /* int *err*/
3580 5 : if (errmsg)
3581 : {
3582 5 : add_error (NULL, "error invoking gcc driver: %s", errmsg);
3583 5 : return;
3584 : }
3585 :
3586 : /* pex_one can return a NULL errmsg when the executable wasn't
3587 : found (or doesn't exist), so trap these cases also. */
3588 0 : if (exit_status || err)
3589 : {
3590 0 : add_error (NULL,
3591 : "error invoking gcc driver: exit_status: %i err: %i",
3592 : exit_status, err);
3593 0 : add_error (NULL,
3594 : "whilst attempting to run a driver named: %s",
3595 : gcc_driver_name);
3596 0 : add_error (NULL,
3597 : "PATH was: %s",
3598 : getenv ("PATH"));
3599 0 : return;
3600 : }
3601 5 : }
3602 :
3603 : /* Extract the target-specific MULTILIB_DEFAULTS to
3604 : multilib_defaults_raw for use by
3605 : playback::context::add_multilib_driver_arguments (). */
3606 :
3607 : #ifndef MULTILIB_DEFAULTS
3608 : #define MULTILIB_DEFAULTS { "" }
3609 : #endif
3610 :
3611 : static const char *const multilib_defaults_raw[] = MULTILIB_DEFAULTS;
3612 :
3613 : /* Helper function for playback::context::invoke_driver ().
3614 :
3615 : 32-bit and 64-bit multilib peer builds of libgccjit.so may share
3616 : a driver binary. We need to pass in options to the shared driver
3617 : to get the appropriate assembler/linker options for this multilib
3618 : peer. */
3619 :
3620 : void
3621 1159 : playback::context::
3622 : add_multilib_driver_arguments (vec <char *> *argvec)
3623 : {
3624 1159 : JIT_LOG_SCOPE (get_logger ());
3625 :
3626 : /* Add copies of the arguments in multilib_defaults_raw to argvec,
3627 : prepending each with a "-". */
3628 3477 : for (size_t i = 0; i < ARRAY_SIZE (multilib_defaults_raw); i++)
3629 1159 : if (multilib_defaults_raw[i][0])
3630 1159 : argvec->safe_push (concat ("-", multilib_defaults_raw[i], NULL));
3631 1159 : }
3632 :
3633 : /* Dynamically-link the built DSO file into this process, using dlopen.
3634 : Wrap it up within a jit::result *, and return that.
3635 : Return NULL if any errors occur, reporting them on this context. */
3636 :
3637 : result *
3638 1133 : playback::context::
3639 : dlopen_built_dso ()
3640 : {
3641 1133 : JIT_LOG_SCOPE (get_logger ());
3642 1133 : auto_timevar load_timevar (get_timer (), TV_LOAD);
3643 1133 : result::handle handle = NULL;
3644 1133 : result *result_obj = NULL;
3645 :
3646 : #ifdef _WIN32
3647 : /* Clear any existing error. */
3648 : SetLastError(0);
3649 :
3650 : handle = LoadLibrary(m_tempdir->get_path_so_file ());
3651 : if (GetLastError() != 0) {
3652 : print_last_error();
3653 : }
3654 : #else
3655 1133 : const char *error = NULL;
3656 : /* Clear any existing error. */
3657 1133 : dlerror ();
3658 :
3659 1133 : handle = dlopen (m_tempdir->get_path_so_file (),
3660 : RTLD_NOW | RTLD_LOCAL);
3661 1133 : if ((error = dlerror()) != NULL) {
3662 0 : add_error (NULL, "%s", error);
3663 : }
3664 : #endif
3665 :
3666 1133 : if (handle)
3667 : {
3668 : /* We've successfully dlopened the result; create a
3669 : jit::result object to wrap it.
3670 :
3671 : We're done with the tempdir for now, but if the user
3672 : has requested debugging, the user's debugger might not
3673 : be capable of dealing with the .so file being unlinked
3674 : immediately, so keep it around until after the result
3675 : is released. We do this by handing over ownership of
3676 : the jit::tempdir to the result. See PR jit/64206. */
3677 1133 : tempdir *handover_tempdir;
3678 1133 : if (get_bool_option (GCC_JIT_BOOL_OPTION_DEBUGINFO))
3679 : {
3680 726 : handover_tempdir = m_tempdir;
3681 726 : m_tempdir = NULL;
3682 : /* The tempdir will eventually be cleaned up in the
3683 : jit::result's dtor. */
3684 726 : log ("GCC_JIT_BOOL_OPTION_DEBUGINFO was set:"
3685 : " handing over tempdir to jit::result");
3686 : }
3687 : else
3688 : {
3689 407 : handover_tempdir = NULL;
3690 : /* ... and retain ownership of m_tempdir so we clean it
3691 : up it the playback::context's dtor. */
3692 407 : log ("GCC_JIT_BOOL_OPTION_DEBUGINFO was not set:"
3693 : " retaining ownership of tempdir");
3694 : }
3695 :
3696 1133 : result_obj = new result (get_logger (), handle, handover_tempdir);
3697 : }
3698 : else
3699 : result_obj = NULL;
3700 :
3701 2266 : return result_obj;
3702 1133 : }
3703 :
3704 : /* Top-level hook for playing back a recording context.
3705 :
3706 : This plays back m_recording_ctxt, and, if no errors
3707 : occurred builds statement lists for and then postprocesses
3708 : every function in the result. */
3709 :
3710 : void
3711 1376 : playback::context::
3712 : replay ()
3713 : {
3714 1376 : JIT_LOG_SCOPE (get_logger ());
3715 :
3716 1376 : init_types ();
3717 1376 : jit_target_init ();
3718 :
3719 : /* Replay the recorded events: */
3720 1376 : timevar_push (TV_JIT_REPLAY);
3721 :
3722 : /* Ensure that builtins that could be needed during optimization
3723 : get created ahead of time. */
3724 1376 : builtins_manager *bm = m_recording_ctxt->get_builtins_manager ();
3725 1376 : bm->ensure_optimization_builtins_exist ();
3726 :
3727 1376 : m_recording_ctxt->replay_into (this);
3728 :
3729 : /* Clean away the temporary references from recording objects
3730 : to playback objects. We have to do this now since the
3731 : latter are GC-allocated, but the former don't mark these
3732 : refs. Hence we must stop using them before the GC can run. */
3733 1376 : m_recording_ctxt->disassociate_from_playback ();
3734 :
3735 : /* The builtins_manager is associated with the recording::context
3736 : and might be reused for future compiles on other playback::contexts,
3737 : but its m_attributes array is not GTY-labeled and hence will become
3738 : nonsense if the GC runs. Purge this state. */
3739 1376 : bm->finish_playback ();
3740 :
3741 1376 : timevar_pop (TV_JIT_REPLAY);
3742 :
3743 1376 : if (!errors_occurred ())
3744 : {
3745 1341 : int i;
3746 1341 : function *func;
3747 1341 : tree global;
3748 : /* No GC can happen yet; process the cached source locations. */
3749 1341 : handle_locations ();
3750 :
3751 : /* Finalize globals. See how FORTRAN 95 does it in gfc_be_parse_file()
3752 : for a simple reference. */
3753 4028 : FOR_EACH_VEC_ELT (m_globals, i, global)
3754 1346 : rest_of_decl_compilation (global, true, true);
3755 :
3756 1482 : wrapup_global_declarations (m_globals.address(), m_globals.length());
3757 :
3758 : /* We've now created tree nodes for the stmts in the various blocks
3759 : in each function, but we haven't built each function's single stmt
3760 : list yet. Do so now. */
3761 20834 : FOR_EACH_VEC_ELT (m_functions, i, func)
3762 18152 : func->build_stmt_list ();
3763 :
3764 : /* No GC can have happened yet. */
3765 :
3766 : /* Postprocess the functions. This could trigger GC. */
3767 19493 : FOR_EACH_VEC_ELT (m_functions, i, func)
3768 : {
3769 18152 : gcc_assert (func);
3770 18152 : func->postprocess ();
3771 : }
3772 : }
3773 1376 : }
3774 :
3775 : /* Dump the generated .s file to stderr. */
3776 :
3777 : void
3778 0 : playback::context::
3779 : dump_generated_code ()
3780 : {
3781 0 : JIT_LOG_SCOPE (get_logger ());
3782 0 : char buf[4096];
3783 0 : size_t sz;
3784 0 : FILE *f_in = fopen (get_path_s_file (), "r");
3785 0 : if (!f_in)
3786 0 : return;
3787 :
3788 0 : while ( (sz = fread (buf, 1, sizeof (buf), f_in)) )
3789 0 : fwrite (buf, 1, sz, stderr);
3790 :
3791 0 : fclose (f_in);
3792 0 : }
3793 :
3794 : /* Get the supposed path of the notional "fake.c" file within the
3795 : tempdir. This file doesn't exist, but the rest of the compiler
3796 : needs a name. */
3797 :
3798 : const char *
3799 1381 : playback::context::
3800 : get_path_c_file () const
3801 : {
3802 1381 : return m_tempdir->get_path_c_file ();
3803 : }
3804 :
3805 : /* Get the path of the assembler output file "fake.s" file within the
3806 : tempdir. */
3807 :
3808 : const char *
3809 0 : playback::context::
3810 : get_path_s_file () const
3811 : {
3812 0 : return m_tempdir->get_path_s_file ();
3813 : }
3814 :
3815 : /* Get the path of the DSO object file "fake.so" file within the
3816 : tempdir. */
3817 :
3818 : const char *
3819 0 : playback::context::
3820 : get_path_so_file () const
3821 : {
3822 0 : return m_tempdir->get_path_so_file ();
3823 : }
3824 :
3825 : /* qsort comparator for comparing pairs of playback::source_line *,
3826 : ordering them by line number. */
3827 :
3828 : static int
3829 5683 : line_comparator (const void *lhs, const void *rhs)
3830 : {
3831 5683 : const playback::source_line *line_lhs = \
3832 : *static_cast<const playback::source_line * const*> (lhs);
3833 5683 : const playback::source_line *line_rhs = \
3834 : *static_cast<const playback::source_line * const*> (rhs);
3835 5683 : return line_lhs->get_line_num () - line_rhs->get_line_num ();
3836 : }
3837 :
3838 : /* qsort comparator for comparing pairs of playback::location *,
3839 : ordering them by column number. */
3840 :
3841 : static int
3842 6963 : location_comparator (const void *lhs, const void *rhs)
3843 : {
3844 6963 : const playback::location *loc_lhs = \
3845 : *static_cast<const playback::location * const *> (lhs);
3846 6963 : const playback::location *loc_rhs = \
3847 : *static_cast<const playback::location * const *> (rhs);
3848 6963 : return loc_lhs->get_column_num () - loc_rhs->get_column_num ();
3849 : }
3850 :
3851 : /* Initialize the NAME_TYPE of the primitive types as well as some
3852 : others. */
3853 : void
3854 1376 : playback::context::
3855 : init_types ()
3856 : {
3857 : /* See lto_init () in lto-lang.cc or void visit (TypeBasic *t) in D's types.cc
3858 : for reference. If TYPE_NAME is not set, debug info will not contain types */
3859 : #define NAME_TYPE(t,n) \
3860 : if (t) \
3861 : TYPE_NAME (t) = build_decl (UNKNOWN_LOCATION, TYPE_DECL, \
3862 : get_identifier (n), t)
3863 :
3864 1376 : NAME_TYPE (integer_type_node, "int");
3865 1376 : NAME_TYPE (char_type_node, "char");
3866 1376 : NAME_TYPE (long_integer_type_node, "long int");
3867 1376 : NAME_TYPE (unsigned_type_node, "unsigned int");
3868 1376 : NAME_TYPE (long_unsigned_type_node, "long unsigned int");
3869 1376 : NAME_TYPE (long_long_integer_type_node, "long long int");
3870 1376 : NAME_TYPE (long_long_unsigned_type_node, "long long unsigned int");
3871 1376 : NAME_TYPE (short_integer_type_node, "short int");
3872 1376 : NAME_TYPE (short_unsigned_type_node, "short unsigned int");
3873 1376 : if (signed_char_type_node != char_type_node)
3874 1376 : NAME_TYPE (signed_char_type_node, "signed char");
3875 1376 : if (unsigned_char_type_node != char_type_node)
3876 1376 : NAME_TYPE (unsigned_char_type_node, "unsigned char");
3877 1376 : NAME_TYPE (float_type_node, "float");
3878 1376 : NAME_TYPE (double_type_node, "double");
3879 1376 : NAME_TYPE (long_double_type_node, "long double");
3880 1376 : NAME_TYPE (void_type_node, "void");
3881 1376 : NAME_TYPE (boolean_type_node, "bool");
3882 1376 : NAME_TYPE (complex_float_type_node, "complex float");
3883 1376 : NAME_TYPE (complex_double_type_node, "complex double");
3884 1376 : NAME_TYPE (complex_long_double_type_node, "complex long double");
3885 :
3886 1376 : m_const_char_ptr = build_pointer_type(
3887 : build_qualified_type (char_type_node, TYPE_QUAL_CONST));
3888 :
3889 1376 : NAME_TYPE (m_const_char_ptr, "char");
3890 1376 : NAME_TYPE (size_type_node, "size_t");
3891 1376 : NAME_TYPE (fileptr_type_node, "FILE");
3892 : #undef NAME_TYPE
3893 1376 : }
3894 :
3895 : /* Our API allows locations to be created in arbitrary orders, but the
3896 : linemap API requires locations to be created in ascending order
3897 : as if we were tokenizing files.
3898 :
3899 : This hook sorts all of the locations that have been created, and
3900 : calls into the linemap API, creating linemap entries in sorted order
3901 : for our locations. */
3902 :
3903 : void
3904 1341 : playback::context::
3905 : handle_locations ()
3906 : {
3907 : /* Create the source code locations, following the ordering rules
3908 : imposed by the linemap API.
3909 :
3910 : line_table is a global. */
3911 1341 : JIT_LOG_SCOPE (get_logger ());
3912 1341 : int i;
3913 1341 : source_file *file;
3914 :
3915 2749 : FOR_EACH_VEC_ELT (m_source_files, i, file)
3916 : {
3917 67 : linemap_add (line_table, LC_ENTER, false, file->get_filename (), 0);
3918 :
3919 : /* Sort lines by ascending line numbers. */
3920 67 : file->m_source_lines.qsort (&line_comparator);
3921 :
3922 67 : int j;
3923 67 : source_line *line;
3924 485 : FOR_EACH_VEC_ELT (file->m_source_lines, j, line)
3925 : {
3926 418 : int k;
3927 418 : location *loc;
3928 :
3929 : /* Sort locations in line by ascending column numbers. */
3930 418 : line->m_locations.qsort (&location_comparator);
3931 :
3932 : /* Determine maximum column within this line. */
3933 418 : gcc_assert (line->m_locations.length () > 0);
3934 418 : location *final_column =
3935 418 : line->m_locations[line->m_locations.length () - 1];
3936 418 : int max_col = final_column->get_column_num ();
3937 :
3938 418 : linemap_line_start (line_table, line->get_line_num (), max_col);
3939 2046 : FOR_EACH_VEC_ELT (line->m_locations, k, loc)
3940 : {
3941 792 : loc->m_srcloc = \
3942 792 : linemap_position_for_column (line_table, loc->get_column_num ());
3943 : }
3944 : }
3945 :
3946 67 : linemap_add (line_table, LC_LEAVE, false, NULL, 0);
3947 : }
3948 :
3949 : /* line_table should now be populated; every playback::location should
3950 : now have an m_srcloc. */
3951 :
3952 : /* Now assign them to tree nodes as appropriate. */
3953 : std::pair<tree, location *> *cached_location;
3954 :
3955 2976 : FOR_EACH_VEC_ELT (m_cached_locations, i, cached_location)
3956 : {
3957 1635 : tree t = cached_location->first;
3958 1635 : location_t srcloc = cached_location->second->m_srcloc;
3959 :
3960 : /* This covers expressions: */
3961 1635 : if (CAN_HAVE_LOCATION_P (t))
3962 1514 : SET_EXPR_LOCATION (t, srcloc);
3963 121 : else if (CODE_CONTAINS_STRUCT(TREE_CODE(t), TS_DECL_MINIMAL))
3964 121 : DECL_SOURCE_LOCATION (t) = srcloc;
3965 : else
3966 : {
3967 : /* Don't know how to set location on this node. */
3968 : }
3969 : }
3970 1341 : }
3971 :
3972 : /* We handle errors on a playback::context by adding them to the
3973 : corresponding recording::context. */
3974 :
3975 : void
3976 45 : playback::context::
3977 : add_error (location *loc, const char *fmt, ...)
3978 : {
3979 45 : va_list ap;
3980 45 : va_start (ap, fmt);
3981 45 : m_recording_ctxt->add_error_va (loc ? loc->get_recording_loc () : NULL,
3982 : diagnostics::kind::error, fmt, ap);
3983 45 : va_end (ap);
3984 45 : }
3985 :
3986 : /* We handle errors on a playback::context by adding them to the
3987 : corresponding recording::context. */
3988 :
3989 : void
3990 0 : playback::context::
3991 : add_error_va (location *loc, const char *fmt, va_list ap)
3992 : {
3993 0 : m_recording_ctxt->add_error_va (loc ? loc->get_recording_loc () : NULL,
3994 : diagnostics::kind::error, fmt, ap);
3995 0 : }
3996 :
3997 : /* Report a diagnostic up to the jit context, so that the
3998 : compilation is treated as a failure if the diagnostic
3999 : is an error. */
4000 :
4001 : void
4002 25 : playback::context::
4003 : add_diagnostic (const char *text,
4004 : const diagnostics::diagnostic_info &diagnostic)
4005 : {
4006 : /* Get location information (if any) from the diagnostic.
4007 : The recording::context::add_error[_va] methods require a
4008 : recording::location. We can't lookup the playback::location
4009 : from the file/line/column since any playback location instances
4010 : may have been garbage-collected away by now, so instead we create
4011 : another recording::location directly. */
4012 25 : location_t gcc_loc = diagnostic_location (&diagnostic);
4013 25 : recording::location *rec_loc = NULL;
4014 25 : if (gcc_loc)
4015 : {
4016 10 : expanded_location exploc = expand_location (gcc_loc);
4017 10 : if (exploc.file)
4018 5 : rec_loc = m_recording_ctxt->new_location (exploc.file,
4019 : exploc.line,
4020 : exploc.column,
4021 : false);
4022 : }
4023 :
4024 25 : m_recording_ctxt->add_diagnostic (rec_loc, diagnostic.m_kind, "%s", text);
4025 25 : }
4026 :
4027 : /* Dealing with the linemap API. */
4028 :
4029 : /* Construct a playback::location for a recording::location, if it
4030 : doesn't exist already. */
4031 :
4032 : playback::location *
4033 1020 : playback::context::
4034 : new_location (recording::location *rloc,
4035 : const char *filename,
4036 : int line,
4037 : int column)
4038 : {
4039 : /* Get the source_file for filename, creating if necessary. */
4040 1020 : source_file *src_file = get_source_file (filename);
4041 : /* Likewise for the line within the file. */
4042 1020 : source_line *src_line = src_file->get_source_line (line);
4043 : /* Likewise for the column within the line. */
4044 1020 : location *loc = src_line->get_location (rloc, column);
4045 1020 : return loc;
4046 : }
4047 :
4048 : /* Deferred setting of the location for a given tree, by adding the
4049 : (tree, playback::location) pair to a list of deferred associations.
4050 : We will actually set the location on the tree later on once
4051 : the location_t for the playback::location exists. */
4052 :
4053 : void
4054 1635 : playback::context::
4055 : set_tree_location (tree t, location *loc)
4056 : {
4057 1635 : gcc_assert (loc);
4058 1635 : m_cached_locations.safe_push (std::make_pair (t, loc));
4059 1635 : }
4060 :
4061 :
4062 : /* Construct a playback::source_file for the given source
4063 : filename, if it doesn't exist already. */
4064 :
4065 : playback::source_file *
4066 1020 : playback::context::
4067 : get_source_file (const char *filename)
4068 : {
4069 : /* Locate the file.
4070 : For simplicity, this is currently a linear search.
4071 : Replace with a hash if this shows up in the profile. */
4072 1020 : int i;
4073 1020 : source_file *file;
4074 1020 : tree ident_filename = get_identifier (filename);
4075 :
4076 2828 : FOR_EACH_VEC_ELT (m_source_files, i, file)
4077 1721 : if (file->filename_as_tree () == ident_filename)
4078 : return file;
4079 :
4080 : /* Not found. */
4081 67 : file = new source_file (ident_filename);
4082 67 : m_source_files.safe_push (file);
4083 67 : return file;
4084 : }
4085 :
4086 : /* Constructor for gcc::jit::playback::source_file. */
4087 :
4088 67 : playback::source_file::source_file (tree filename) :
4089 67 : m_source_lines (),
4090 67 : m_filename (filename)
4091 : {
4092 67 : }
4093 :
4094 : /* Don't leak vec's internal buffer (in non-GC heap) when we are
4095 : GC-ed. */
4096 :
4097 : void
4098 62 : playback::source_file::finalizer ()
4099 : {
4100 62 : m_source_lines.release ();
4101 62 : }
4102 :
4103 : /* Construct a playback::source_line for the given line
4104 : within this source file, if one doesn't exist already. */
4105 :
4106 : playback::source_line *
4107 1020 : playback::source_file::
4108 : get_source_line (int line_num)
4109 : {
4110 : /* Locate the line.
4111 : For simplicity, this is currently a linear search.
4112 : Replace with a hash if this shows up in the profile. */
4113 1020 : int i;
4114 1020 : source_line *line;
4115 :
4116 7970 : FOR_EACH_VEC_ELT (m_source_lines, i, line)
4117 7201 : if (line->get_line_num () == line_num)
4118 : return line;
4119 :
4120 : /* Not found. */
4121 418 : line = new source_line (this, line_num);
4122 418 : m_source_lines.safe_push (line);
4123 418 : return line;
4124 : }
4125 :
4126 : /* Constructor for gcc::jit::playback::source_line. */
4127 :
4128 418 : playback::source_line::source_line (source_file *file, int line_num) :
4129 418 : m_locations (),
4130 418 : m_source_file (file),
4131 418 : m_line_num (line_num)
4132 : {
4133 418 : }
4134 :
4135 : /* Don't leak vec's internal buffer (in non-GC heap) when we are
4136 : GC-ed. */
4137 :
4138 : void
4139 353 : playback::source_line::finalizer ()
4140 : {
4141 353 : m_locations.release ();
4142 353 : }
4143 :
4144 : /* Construct a playback::location for the given column
4145 : within this line of a specific source file, if one doesn't exist
4146 : already. */
4147 :
4148 : playback::location *
4149 1020 : playback::source_line::
4150 : get_location (recording::location *rloc, int column_num)
4151 : {
4152 1020 : int i;
4153 1020 : location *loc;
4154 :
4155 : /* Another linear search that probably should be a hash table. */
4156 5885 : FOR_EACH_VEC_ELT (m_locations, i, loc)
4157 4719 : if (loc->get_column_num () == column_num)
4158 : return loc;
4159 :
4160 : /* Not found. */
4161 792 : loc = new location (rloc, this, column_num);
4162 792 : m_locations.safe_push (loc);
4163 792 : return loc;
4164 : }
4165 :
4166 : /* Constructor for gcc::jit::playback::location. */
4167 :
4168 792 : playback::location::location (recording::location *loc,
4169 : source_line *line,
4170 : int column_num) :
4171 792 : m_srcloc (UNKNOWN_LOCATION),
4172 792 : m_recording_loc (loc),
4173 792 : m_line (line),
4174 792 : m_column_num(column_num)
4175 : {
4176 792 : }
4177 :
4178 : /* The active gcc::jit::playback::context instance. This is a singleton,
4179 : guarded by jit_mutex. */
4180 :
4181 : playback::context *active_playback_ctxt;
4182 :
4183 : } // namespace gcc::jit
4184 :
4185 : } // namespace gcc
|