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