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