Line data Source code
1 : /* Internals of libgccjit: classes for recording calls made to the JIT API.
2 : Copyright (C) 2013-2026 Free Software Foundation, Inc.
3 : Contributed by David Malcolm <dmalcolm@redhat.com>.
4 :
5 : This file is part of GCC.
6 :
7 : GCC is free software; you can redistribute it and/or modify it
8 : under the terms of the GNU General Public License as published by
9 : the Free Software Foundation; either version 3, or (at your option)
10 : any later version.
11 :
12 : GCC is distributed in the hope that it will be useful, but
13 : WITHOUT ANY WARRANTY; without even the implied warranty of
14 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 : General Public License for more details.
16 :
17 : You should have received a copy of the GNU General Public License
18 : along with GCC; see the file COPYING3. If not see
19 : <http://www.gnu.org/licenses/>. */
20 :
21 : #include "config.h"
22 : #define INCLUDE_SSTREAM
23 : #include "system.h"
24 : #include "coretypes.h"
25 : #include "target.h"
26 : #include "pretty-print.h"
27 : #include "toplev.h"
28 :
29 :
30 : #include "jit-builtins.h"
31 : #include "jit-recording.h"
32 : #include "jit-playback.h"
33 :
34 : namespace gcc {
35 : namespace jit {
36 :
37 : // class dump
38 :
39 1177 : dump::dump (recording::context &ctxt,
40 : const char *filename,
41 : bool update_locations)
42 1177 : : m_ctxt (ctxt),
43 1177 : m_filename (filename),
44 1177 : m_update_locations (update_locations),
45 1177 : m_line (0),
46 1177 : m_column (0)
47 : {
48 1177 : m_file = fopen (filename, "w");
49 1177 : if (!m_file)
50 0 : ctxt.add_error (NULL,
51 : "error opening dump file %s for writing: %s",
52 : filename,
53 0 : xstrerror (errno));
54 1177 : }
55 :
56 1177 : dump::~dump ()
57 : {
58 1177 : if (m_file)
59 : {
60 1177 : int err = fclose (m_file);
61 1177 : if (err)
62 0 : m_ctxt.add_error (NULL,
63 : "error closing dump file %s: %s",
64 : m_filename,
65 0 : xstrerror (errno));
66 : }
67 1177 : }
68 :
69 : /* Write the given message to the dump, using printf-formatting
70 : conventions, updating the line/column within the dump.
71 :
72 : Emit an error on the context if a failure occurs. */
73 :
74 : void
75 176312 : dump::write (const char *fmt, ...)
76 : {
77 176312 : int len;
78 176312 : va_list ap;
79 176312 : char *buf;
80 :
81 : /* If there was an error opening the file, we've already reported it.
82 : Don't attempt further work. */
83 176312 : if (!m_file)
84 0 : return;
85 :
86 176312 : va_start (ap, fmt);
87 176312 : len = vasprintf (&buf, fmt, ap);
88 176312 : va_end (ap);
89 :
90 176312 : if (buf == NULL || len < 0)
91 : {
92 0 : m_ctxt.add_error (NULL, "malloc failure writing to dumpfile %s",
93 : m_filename);
94 0 : return;
95 : }
96 :
97 176312 : if (fwrite (buf, strlen (buf), 1, m_file) != 1)
98 0 : m_ctxt.add_error (NULL, "error writing to dump file %s",
99 : m_filename);
100 :
101 : /* Flush after each line, to ease debugging crashes. */
102 176312 : fflush (m_file);
103 :
104 : /* Update line/column: */
105 25187447 : for (const char *ptr = buf; *ptr; ptr++)
106 : {
107 25011135 : if ('\n' == *ptr)
108 : {
109 397287 : m_line++;
110 397287 : m_column = 0;
111 : }
112 : else
113 24613848 : m_column++;
114 : }
115 :
116 176312 : free (buf);
117 : }
118 :
119 : /* Construct a gcc::jit::recording::location instance for the current
120 : location within the dump. */
121 :
122 : recording::location *
123 186 : dump::make_location () const
124 : {
125 186 : return m_ctxt.new_location (m_filename, m_line, m_column,
126 : /* We need to flag such locations as *not*
127 : created by the user, so that
128 : reproducer::get_identifier can cope with
129 : them appearing *after* the memento that
130 : refers to them. */
131 186 : false);
132 : }
133 :
134 : /* A collection of allocations, all of which can be released together, to
135 : avoid needing to track and release them individually. */
136 :
137 1163 : class allocator
138 : {
139 : public:
140 : ~allocator ();
141 :
142 : char *
143 : xstrdup_printf (const char *, ...)
144 : ATTRIBUTE_RETURNS_NONNULL
145 : GNU_PRINTF(2, 3);
146 :
147 : char *
148 : xstrdup_printf_va (const char *, va_list ap)
149 : ATTRIBUTE_RETURNS_NONNULL
150 : GNU_PRINTF(2, 0);
151 :
152 : private:
153 : auto_vec <void *> m_buffers;
154 : };
155 :
156 : /* allocator's destructor. Call "free" on all of the allocations. */
157 :
158 1163 : allocator::~allocator ()
159 : {
160 1163 : unsigned i;
161 1163 : void *buffer;
162 163924 : FOR_EACH_VEC_ELT (m_buffers, i, buffer)
163 162761 : free (buffer);
164 1163 : }
165 :
166 : /* Formatted printing, allocating to a buffer (or exiting the process if
167 : the allocation fails).
168 :
169 : The buffer exists until the allocator is cleaned up, and is freed at
170 : that point, so the caller doesn't need to track the result. */
171 :
172 : char *
173 153211 : allocator::xstrdup_printf (const char *fmt, ...)
174 : {
175 153211 : char *result;
176 153211 : va_list ap;
177 153211 : va_start (ap, fmt);
178 153211 : result = xstrdup_printf_va (fmt, ap);
179 153211 : va_end (ap);
180 153211 : return result;
181 : }
182 :
183 : /* Formatted printing, allocating to a buffer (or exiting the process if
184 : the allocation fails).
185 :
186 : The buffer exists until the allocator is cleaned up, and is freed at
187 : that point, so the caller doesn't need to track the result. */
188 :
189 : char *
190 162761 : allocator::xstrdup_printf_va (const char *fmt, va_list ap)
191 : {
192 162761 : char *result = xvasprintf (fmt, ap);
193 162761 : m_buffers.safe_push (result);
194 162761 : return result;
195 : }
196 :
197 : /* gcc::jit::reproducer is a subclass of gcc::jit::dump, used for
198 : implementing gcc_jit_context_dump_reproducer_to_file. */
199 :
200 : class reproducer : public dump
201 : {
202 : public:
203 : reproducer (recording::context &ctxt,
204 : const char *filename);
205 :
206 : void
207 : write_params (const vec <recording::context *> &contexts);
208 :
209 : void
210 : write_args (const vec <recording::context *> &contexts);
211 :
212 : const char *
213 : make_identifier (recording::memento *m, const char *prefix);
214 :
215 : const char *
216 : make_tmp_identifier (const char *prefix, recording::memento *m);
217 :
218 : const char *
219 : get_identifier (recording::context *ctxt);
220 :
221 : const char *
222 : get_identifier (recording::memento *m);
223 :
224 : const char *
225 : get_identifier_as_rvalue (recording::rvalue *m);
226 :
227 : const char *
228 : get_identifier_as_lvalue (recording::lvalue *m);
229 :
230 : const char *
231 : get_identifier_as_type (recording::type *m);
232 :
233 : char *
234 : xstrdup_printf (const char *, ...)
235 : ATTRIBUTE_RETURNS_NONNULL
236 : GNU_PRINTF(2, 3);
237 :
238 : private:
239 : const char * ensure_identifier_is_unique (const char *candidate, void *ptr);
240 :
241 : private:
242 : hash_map<recording::memento *, const char *> m_map_memento_to_identifier;
243 :
244 : struct hash_traits : public string_hash
245 : {
246 : static void remove (const char *) {}
247 : };
248 : hash_set<const char *, false, hash_traits> m_set_identifiers;
249 : allocator m_allocator;
250 : };
251 :
252 : /* gcc::jit::reproducer's constructor. */
253 :
254 1163 : reproducer::reproducer (recording::context &ctxt,
255 : const char *filename) :
256 : dump (ctxt, filename, 0),
257 1163 : m_map_memento_to_identifier (),
258 1163 : m_set_identifiers (),
259 1163 : m_allocator ()
260 : {
261 1163 : }
262 :
263 : /* Write out a list of contexts as a set of parameters within a
264 : C function declaration. */
265 :
266 : void
267 4652 : reproducer::write_params (const vec <recording::context *> &contexts)
268 : {
269 4652 : unsigned i;
270 4652 : recording::context *ctxt;
271 9368 : FOR_EACH_VEC_ELT (contexts, i, ctxt)
272 : {
273 4716 : write ("gcc_jit_context *%s",
274 : get_identifier (ctxt));
275 9432 : if (i < contexts.length () - 1)
276 64 : write (",\n"
277 : " ");
278 : }
279 4652 : }
280 :
281 : /* Write out a list of contexts as a set of arguments within a call
282 : to a C function. */
283 :
284 : void
285 2326 : reproducer::write_args (const vec <recording::context *> &contexts)
286 : {
287 2326 : unsigned i;
288 2326 : recording::context *ctxt;
289 4684 : FOR_EACH_VEC_ELT (contexts, i, ctxt)
290 : {
291 2358 : write ("%s",
292 : get_identifier (ctxt));
293 4716 : if (i < contexts.length () - 1)
294 32 : write (",\n"
295 : " ");
296 : }
297 2326 : }
298 :
299 : /* Ensure that STR is a valid C identifier by overwriting
300 : any invalid chars in-place with underscores.
301 :
302 : This doesn't special-case the first character. */
303 :
304 : static void
305 57385 : convert_to_identifier (char *str)
306 : {
307 1031653 : for (char *p = str; *p; p++)
308 974268 : if (!ISALNUM (*p))
309 196737 : *p = '_';
310 57385 : }
311 :
312 : /* Given CANDIDATE, a possible C identifier for use in a reproducer,
313 : ensure that it is unique within the generated source file by
314 : appending PTR to it if necessary. Return the resulting string.
315 :
316 : The reproducer will eventually clean up the buffer in its dtor. */
317 :
318 : const char *
319 57524 : reproducer::ensure_identifier_is_unique (const char *candidate, void *ptr)
320 : {
321 57524 : if (m_set_identifiers.contains (candidate))
322 14956 : candidate = m_allocator.xstrdup_printf ("%s_%p", candidate, ptr);
323 57524 : gcc_assert (!m_set_identifiers.contains (candidate));
324 57524 : m_set_identifiers.add (candidate);
325 57524 : return candidate;
326 : }
327 :
328 : /* Generate a C identifier for the given memento, associating the generated
329 : buffer with the memento (for future calls to get_identifier et al).
330 :
331 : The reproducer will eventually clean up the buffer in its dtor. */
332 : const char *
333 57524 : reproducer::make_identifier (recording::memento *m, const char *prefix)
334 : {
335 57524 : const char *result;
336 57524 : if (strlen (m->get_debug_string ()) < 100)
337 : {
338 57385 : char *buf = m_allocator.xstrdup_printf ("%s_%s",
339 : prefix,
340 : m->get_debug_string ());
341 57385 : convert_to_identifier (buf);
342 57385 : result = buf;
343 : }
344 : else
345 139 : result = m_allocator.xstrdup_printf ("%s_%p",
346 : prefix, (void *) m);
347 57524 : result = ensure_identifier_is_unique (result, m);
348 57524 : m_map_memento_to_identifier.put (m, result);
349 57524 : return result;
350 : }
351 :
352 : /* Generate a C identifier for a temporary variable.
353 : The reproducer will eventually clean up the buffer in its dtor. */
354 :
355 : const char *
356 4081 : reproducer::make_tmp_identifier (const char *prefix, recording::memento *m)
357 : {
358 4081 : return m_allocator.xstrdup_printf ("%s_%s",
359 4081 : prefix, get_identifier (m));
360 : }
361 :
362 : /* Generate a C identifier for the given context.
363 : The reproducer will eventually clean up the buffer in its dtor. */
364 :
365 : const char *
366 76650 : reproducer::get_identifier (recording::context *ctxt)
367 : {
368 76650 : return m_allocator.xstrdup_printf ("ctxt_%p",
369 76650 : (void *)ctxt);
370 : }
371 :
372 : /* Locate the C identifier for the given memento, which is assumed to
373 : have already been created via make_identifier. */
374 :
375 : const char *
376 119070 : reproducer::get_identifier (recording::memento *m)
377 : {
378 119070 : if (!m)
379 : return "NULL";
380 :
381 : /* gcc_jit_context_dump_to_file (, , 1) generates and writes locations,
382 : and hence these locations appear in the context's memento list
383 : out-of-order: they appear in the context's memento list *after*
384 : the memento that refers to them. For this case, it's simplest to
385 : pretend that they're NULL when writing out the code to recreate the
386 : memento that uses them. */
387 89578 : if (recording::location *loc = m->dyn_cast_location ())
388 6494 : if (!loc->created_by_user ())
389 : return "NULL";
390 :
391 89370 : const char **slot = m_map_memento_to_identifier.get (m);
392 89370 : if (!slot)
393 : {
394 0 : get_context ().add_error (NULL,
395 : "unable to find identifier for %p: %s",
396 : (void *)m,
397 : m->get_debug_string ());
398 0 : gcc_unreachable ();
399 : }
400 89370 : return *slot;
401 : }
402 :
403 : /* Locate the C identifier for the given rvalue, wrapping it within
404 : a gcc_*_as_rvalue upcast if necessary. */
405 :
406 : const char *
407 17266 : reproducer::get_identifier_as_rvalue (recording::rvalue *m)
408 : {
409 17266 : if (!m)
410 : return "NULL";
411 17156 : return m->access_as_rvalue (*this);
412 : }
413 :
414 : /* Locate the C identifier for the given lvalue, wrapping it within
415 : a gcc_*_as_lvalue upcast if necessary. */
416 :
417 : const char *
418 2972 : reproducer::get_identifier_as_lvalue (recording::lvalue *m)
419 : {
420 2972 : if (!m)
421 : return "NULL";
422 2972 : return m->access_as_lvalue (*this);
423 : }
424 :
425 : /* Locate the C identifier for the given type, wrapping it within
426 : a gcc_*_as_type upcast if necessary. */
427 :
428 : const char *
429 30203 : reproducer::get_identifier_as_type (recording::type *m)
430 : {
431 30203 : if (!m)
432 : return "NULL";
433 30203 : return m->access_as_type (*this);
434 : }
435 :
436 : /* Formatted printing, allocating to a buffer (or exiting the process if
437 : the allocation fails).
438 :
439 : The buffer exists until the allocator is cleaned up, and is freed at
440 : that point, so the caller doesn't need to track the result.
441 :
442 : Note that we can't use ggc_printf since we're not within the compiler
443 : proper (when within gcc_jit_context_dump_reproducer_to_file). */
444 :
445 : char *
446 9550 : reproducer::xstrdup_printf (const char *fmt, ...)
447 : {
448 9550 : char *result;
449 9550 : va_list ap;
450 9550 : va_start (ap, fmt);
451 9550 : result = m_allocator.xstrdup_printf_va (fmt, ap);
452 9550 : va_end (ap);
453 9550 : return result;
454 : }
455 :
456 : /* A helper class for implementing make_debug_string, for building
457 : a temporary string from a vec of rvalues. */
458 :
459 : class comma_separated_string
460 : {
461 : public:
462 : comma_separated_string (const auto_vec<recording::rvalue *> &rvalues,
463 : enum recording::precedence prec);
464 : ~comma_separated_string ();
465 :
466 721 : const char *as_char_ptr () const { return m_buf; }
467 :
468 : private:
469 : char *m_buf;
470 : };
471 :
472 : /* comma_separated_string's ctor
473 : Build m_buf. */
474 :
475 721 : comma_separated_string::comma_separated_string
476 : (const auto_vec<recording::rvalue *> &rvalues,
477 : enum recording::precedence prec)
478 721 : : m_buf (NULL)
479 : {
480 : /* Calculate length of said buffer. */
481 721 : size_t sz = 1; /* nil terminator */
482 1997 : for (unsigned i = 0; i< rvalues.length (); i++)
483 : {
484 1276 : sz += strlen (rvalues[i]->get_debug_string_parens (prec));
485 1276 : sz += 2; /* ", " separator */
486 : }
487 :
488 : /* Now allocate and populate the buffer. */
489 721 : m_buf = new char[sz];
490 721 : size_t len = 0;
491 :
492 1997 : for (unsigned i = 0; i< rvalues.length (); i++)
493 : {
494 1276 : strcpy (m_buf + len, rvalues[i]->get_debug_string_parens (prec));
495 1276 : len += strlen (rvalues[i]->get_debug_string_parens (prec));
496 1906 : if (i + 1 < rvalues.length ())
497 : {
498 630 : strcpy (m_buf + len, ", ");
499 630 : len += 2;
500 : }
501 : }
502 721 : m_buf[len] = '\0';
503 721 : }
504 :
505 : /* comma_separated_string's dtor. */
506 :
507 721 : comma_separated_string::~comma_separated_string ()
508 : {
509 721 : delete[] m_buf;
510 721 : }
511 :
512 : /**********************************************************************
513 : Recording.
514 : **********************************************************************/
515 :
516 : /* Get the playback::location for the given recording::location,
517 : handling a NULL input with a NULL output. */
518 :
519 : playback::location *
520 71556 : recording::playback_location (replayer *r, recording::location *loc)
521 : {
522 71556 : if (loc)
523 1473 : return loc->playback_location (r);
524 : else
525 : return NULL;
526 : }
527 :
528 : /* Get a const char * for the given recording::string
529 : handling a NULL input with a NULL output. */
530 :
531 : const char *
532 13469 : recording::playback_string (recording::string *str)
533 : {
534 13469 : if (str)
535 12282 : return str->c_str ();
536 : else
537 : return NULL;
538 : }
539 :
540 : /* Get the playback::block for the given recording::block,
541 : handling a NULL input with a NULL output. */
542 :
543 : playback::block *
544 15914 : recording::playback_block (recording::block *b)
545 : {
546 15914 : if (b)
547 15914 : return b->playback_block ();
548 : else
549 : return NULL;
550 : }
551 :
552 : /* Methods of cc::jit::recording::context. */
553 :
554 : /* The constructor for gcc::jit::recording::context, used by
555 : gcc_jit_context_acquire and gcc_jit_context_new_child_context. */
556 :
557 2122 : recording::context::context (context *parent_ctxt)
558 : : log_user (NULL),
559 2122 : m_parent_ctxt (parent_ctxt),
560 2122 : m_toplevel_ctxt (m_parent_ctxt ? m_parent_ctxt->m_toplevel_ctxt : this),
561 2122 : m_timer (NULL),
562 2122 : m_error_count (0),
563 2122 : m_first_error_str (NULL),
564 2122 : m_owns_first_error_str (false),
565 2122 : m_last_error_str (NULL),
566 2122 : m_owns_last_error_str (false),
567 2122 : m_mementos (),
568 2122 : m_compound_types (),
569 2122 : m_globals (),
570 2122 : m_functions (),
571 2122 : m_FILE_type (NULL),
572 4244 : m_builtins_manager(NULL)
573 : {
574 2122 : if (parent_ctxt)
575 : {
576 : /* Inherit options from parent. */
577 36 : for (unsigned i = 0; i < ARRAY_SIZE (m_str_options); i++)
578 : {
579 24 : const char *parent_opt = parent_ctxt->m_str_options[i];
580 24 : m_str_options[i] = parent_opt ? xstrdup (parent_opt) : NULL;
581 : }
582 12 : memcpy (m_int_options,
583 : parent_ctxt->m_int_options,
584 : sizeof (m_int_options));
585 12 : memcpy (m_bool_options,
586 : parent_ctxt->m_bool_options,
587 : sizeof (m_bool_options));
588 12 : memcpy (m_inner_bool_options,
589 12 : parent_ctxt->m_inner_bool_options,
590 : sizeof (m_inner_bool_options));
591 12 : set_logger (parent_ctxt->get_logger ());
592 : }
593 : else
594 : {
595 2110 : memset (m_str_options, 0, sizeof (m_str_options));
596 2110 : memset (m_int_options, 0, sizeof (m_int_options));
597 2110 : memset (m_bool_options, 0, sizeof (m_bool_options));
598 2110 : memset (m_inner_bool_options, 0, sizeof (m_inner_bool_options));
599 2110 : m_inner_bool_options[INNER_BOOL_OPTION_PRINT_ERRORS_TO_STDERR] = true;
600 : }
601 :
602 2122 : memset (m_basic_types, 0, sizeof (m_basic_types));
603 2122 : }
604 :
605 : /* The destructor for gcc::jit::recording::context, implicitly used by
606 : gcc_jit_context_release. */
607 :
608 2075 : recording::context::~context ()
609 : {
610 2075 : JIT_LOG_SCOPE (get_logger ());
611 2075 : int i;
612 2075 : memento *m;
613 1738490 : FOR_EACH_VEC_ELT (m_mementos, i, m)
614 : {
615 1734340 : delete m;
616 : }
617 :
618 6225 : for (i = 0; i < GCC_JIT_NUM_STR_OPTIONS; ++i)
619 4150 : free (m_str_options[i]);
620 :
621 : char *optname;
622 3492 : FOR_EACH_VEC_ELT (m_command_line_options, i, optname)
623 1417 : free (optname);
624 2107 : FOR_EACH_VEC_ELT (m_driver_options, i, optname)
625 32 : free (optname);
626 :
627 2075 : if (m_builtins_manager)
628 1314 : delete m_builtins_manager;
629 :
630 2075 : if (m_owns_first_error_str)
631 616 : free (m_first_error_str);
632 :
633 2075 : if (m_owns_last_error_str)
634 616 : if (m_last_error_str != m_first_error_str)
635 340 : free (m_last_error_str);
636 2075 : }
637 :
638 : /* Add the given mememto to the list of those tracked by this
639 : gcc::jit::recording::context, so that e.g. it can be deleted
640 : when this context is released. */
641 :
642 : void
643 1736926 : recording::context::record (memento *m)
644 : {
645 1736926 : gcc_assert (m);
646 :
647 1736926 : m_mementos.safe_push (m);
648 1736914 : }
649 :
650 : /* Replay this context (and any parents) into the given replayer. */
651 :
652 : void
653 1396 : recording::context::replay_into (replayer *r)
654 : {
655 1396 : JIT_LOG_SCOPE (get_logger ());
656 1396 : int i;
657 1396 : memento *m;
658 :
659 : /* If we have a parent context, we must replay it. This will
660 : recursively walk backwards up the historical tree, then replay things
661 : forwards "in historical order", starting with the ultimate parent
662 : context, until we reach the "this" context.
663 :
664 : Note that we fully replay the parent, then fully replay the child,
665 : which means that inter-context references can only exist from child
666 : to parent, not the other way around.
667 :
668 : All of this replaying is suboptimal - it would be better to do the
669 : work for the parent context *once*, rather than replaying the parent
670 : every time we replay each child. However, fixing this requires deep
671 : surgery to lifetime-management: we'd need every context family tree
672 : to have its own GC heap, and to initialize the GCC code to use that
673 : heap (with a mutex on such a heap). */
674 1396 : if (m_parent_ctxt)
675 20 : m_parent_ctxt->replay_into (r);
676 :
677 1396 : if (r->errors_occurred ())
678 : return;
679 :
680 : /* Replay this context's saved operations into r. */
681 200882 : FOR_EACH_VEC_ELT (m_mementos, i, m)
682 : {
683 : /* Disabled low-level debugging, here if we need it: print what
684 : we're replaying.
685 : Note that the calls to get_debug_string might lead to more
686 : mementos being created for the strings.
687 : This can also be used to exercise the debug_string
688 : machinery. */
689 199521 : if (0)
690 : printf ("context %p replaying (%p): %s\n",
691 : (void *)this, (void *)m, m->get_debug_string ());
692 :
693 199521 : m->replay_into (r);
694 :
695 199521 : if (r->errors_occurred ())
696 : return;
697 : }
698 1396 : }
699 :
700 : /* During a playback, we associate objects from the recording with
701 : their counterparts during this playback.
702 :
703 : For simplicity, we store this within the recording objects.
704 :
705 : The following method cleans away these associations, to ensure that
706 : we never have out-of-date associations lingering on subsequent
707 : playbacks (the objects pointed to are GC-managed, but the
708 : recording objects don't own refs to them). */
709 :
710 : void
711 1396 : recording::context::disassociate_from_playback ()
712 : {
713 1396 : JIT_LOG_SCOPE (get_logger ());
714 1396 : int i;
715 1396 : memento *m;
716 :
717 1396 : if (m_parent_ctxt)
718 20 : m_parent_ctxt->disassociate_from_playback ();
719 :
720 202802 : FOR_EACH_VEC_ELT (m_mementos, i, m)
721 : {
722 201406 : m->set_playback_obj (NULL);
723 : }
724 1396 : }
725 :
726 : /* Create a recording::string instance and add it to this context's list
727 : of mementos.
728 :
729 : This creates a fresh copy of the given 0-terminated buffer. */
730 :
731 : recording::string *
732 142271 : recording::context::new_string (const char *text, bool escaped)
733 : {
734 142271 : if (!text)
735 : return NULL;
736 :
737 140743 : recording::string *result = new string (this, text, escaped);
738 140743 : record (result);
739 140743 : return result;
740 : }
741 :
742 : /* Create a recording::location instance and add it to this context's
743 : list of mementos.
744 :
745 : Implements the post-error-checking part of
746 : gcc_jit_context_new_location. */
747 :
748 : recording::location *
749 9908 : recording::context::new_location (const char *filename,
750 : int line,
751 : int column,
752 : bool created_by_user)
753 : {
754 9908 : recording::location *result =
755 : new recording::location (this,
756 : new_string (filename),
757 : line, column,
758 9908 : created_by_user);
759 9908 : record (result);
760 9908 : return result;
761 : }
762 :
763 : /* If we haven't seen this enum value yet, create a recording::type
764 : instance and add it to this context's list of mementos.
765 :
766 : If we have seen it before, reuse our cached value, so that repeated
767 : calls on the context give the same object.
768 :
769 : If we have a parent context, the cache is within the ultimate
770 : ancestor context.
771 :
772 : Implements the post-error-checking part of
773 : gcc_jit_context_get_type. */
774 :
775 : recording::type *
776 38318 : recording::context::get_type (enum gcc_jit_types kind)
777 : {
778 38318 : if (!m_basic_types[kind])
779 : {
780 13472 : if (m_parent_ctxt)
781 24 : m_basic_types[kind] = m_parent_ctxt->get_type (kind);
782 : else
783 : {
784 13448 : recording::type *result = new memento_of_get_type (this, kind);
785 13448 : record (result);
786 13448 : m_basic_types[kind] = result;
787 : }
788 : }
789 :
790 38318 : return m_basic_types[kind];
791 : }
792 :
793 : /* Get a recording::type instance for the given size and signedness.
794 : This is implemented in terms of recording::context::get_type
795 : above.
796 :
797 : Implements the post-error-checking part of
798 : gcc_jit_context_get_int_type. */
799 :
800 : recording::type *
801 5257 : recording::context::get_int_type (int num_bytes, int is_signed)
802 : {
803 : /* We can't use a switch here since some of the values are macros affected
804 : by options; e.g. i386.h has
805 : #define LONG_TYPE_SIZE (TARGET_X32 ? 32 : BITS_PER_WORD)
806 : Compare with tree.cc's make_or_reuse_type. Note that the _SIZE macros
807 : are in bits, rather than bytes.
808 : */
809 5257 : const int num_bits = num_bytes * 8;
810 5257 : if (num_bits == INT_TYPE_SIZE)
811 3100 : return get_type (is_signed
812 : ? GCC_JIT_TYPE_INT
813 1593 : : GCC_JIT_TYPE_UNSIGNED_INT);
814 3664 : if (num_bits == CHAR_TYPE_SIZE)
815 3213 : return get_type (is_signed
816 : ? GCC_JIT_TYPE_SIGNED_CHAR
817 1614 : : GCC_JIT_TYPE_UNSIGNED_CHAR);
818 2050 : if (num_bits == SHORT_TYPE_SIZE)
819 1056 : return get_type (is_signed
820 : ? GCC_JIT_TYPE_SHORT
821 528 : : GCC_JIT_TYPE_UNSIGNED_SHORT);
822 1528 : if (num_bits == LONG_TYPE_SIZE)
823 2991 : return get_type (is_signed
824 : ? GCC_JIT_TYPE_LONG
825 1516 : : GCC_JIT_TYPE_UNSIGNED_LONG);
826 6 : if (num_bits == LONG_LONG_TYPE_SIZE)
827 8 : return get_type (is_signed
828 : ? GCC_JIT_TYPE_LONG_LONG
829 6 : : GCC_JIT_TYPE_UNSIGNED_LONG_LONG);
830 0 : if (num_bits == 128)
831 0 : return get_type (is_signed
832 : ? GCC_JIT_TYPE_INT128_T
833 0 : : GCC_JIT_TYPE_UINT128_T);
834 :
835 : /* Some other size, not corresponding to the C int types. */
836 : /* To be written: support arbitrary other sizes, sharing by
837 : memoizing at the recording::context level? */
838 0 : gcc_unreachable ();
839 : }
840 :
841 : /* Create a recording::type instance and add it to this context's list
842 : of mementos.
843 :
844 : Implements the post-error-checking part of
845 : gcc_jit_context_new_array_type. */
846 :
847 : recording::type *
848 410 : recording::context::new_array_type (recording::location *loc,
849 : recording::type *element_type,
850 : uint64_t num_elements)
851 : {
852 410 : if (struct_ *s = element_type->dyn_cast_struct ())
853 0 : if (!s->get_fields ())
854 : {
855 0 : add_error (NULL,
856 : "cannot create an array of type %s"
857 : " until the fields have been set",
858 : s->get_name ()->c_str ());
859 0 : return NULL;
860 : }
861 410 : recording::type *result =
862 410 : new recording::array_type (this, loc, element_type, num_elements);
863 410 : record (result);
864 410 : return result;
865 : }
866 :
867 : /* Create a recording::field instance and add it to this context's list
868 : of mementos.
869 :
870 : Implements the post-error-checking part of
871 : gcc_jit_context_new_field. */
872 :
873 : recording::field *
874 4520 : recording::context::new_field (recording::location *loc,
875 : recording::type *type,
876 : const char *name)
877 : {
878 4520 : recording::field *result =
879 4520 : new recording::field (this, loc, type, new_string (name));
880 4520 : record (result);
881 4520 : return result;
882 : }
883 :
884 : /* Create a recording::bitfield instance and add it to this context's list
885 : of mementos.
886 :
887 : Implements the post-error-checking part of
888 : gcc_jit_context_new_bitfield. */
889 :
890 : recording::field *
891 110 : recording::context::new_bitfield (recording::location *loc,
892 : recording::type *type,
893 : int width,
894 : const char *name)
895 : {
896 110 : recording::field *result =
897 110 : new recording::bitfield (this, loc, type, width, new_string (name));
898 110 : record (result);
899 110 : return result;
900 : }
901 :
902 : /* Create a recording::struct_ instance and add it to this context's
903 : list of mementos and list of compound types.
904 :
905 : Implements the post-error-checking part of
906 : gcc_jit_context_new_struct_type. */
907 :
908 : recording::struct_ *
909 1001 : recording::context::new_struct_type (recording::location *loc,
910 : const char *name)
911 : {
912 1001 : recording::struct_ *result = new struct_ (this, loc, new_string (name));
913 1001 : record (result);
914 1001 : m_compound_types.safe_push (result);
915 1001 : return result;
916 : }
917 :
918 : /* Create a recording::union_ instance and add it to this context's
919 : list of mementos and list of compound types.
920 :
921 : Implements the first post-error-checking part of
922 : gcc_jit_context_new_union_type. */
923 :
924 : recording::union_ *
925 85 : recording::context::new_union_type (recording::location *loc,
926 : const char *name)
927 : {
928 85 : recording::union_ *result = new union_ (this, loc, new_string (name));
929 85 : record (result);
930 85 : m_compound_types.safe_push (result);
931 85 : return result;
932 : }
933 :
934 : /* Create a recording::function_type instance and add it to this context's
935 : list of mementos.
936 :
937 : Used by new_function_ptr_type and by builtins_manager::make_fn_type. */
938 :
939 : recording::function_type *
940 5679 : recording::context::new_function_type (recording::type *return_type,
941 : int num_params,
942 : recording::type **param_types,
943 : int is_variadic,
944 : bool is_target_builtin)
945 : {
946 5679 : recording::function_type *fn_type
947 : = new function_type (this,
948 : return_type,
949 : num_params,
950 : param_types,
951 : is_variadic,
952 5679 : is_target_builtin);
953 5679 : record (fn_type);
954 5679 : return fn_type;
955 : }
956 :
957 : /* Create a recording::type instance and add it to this context's list
958 : of mementos.
959 :
960 : Implements the post-error-checking part of
961 : gcc_jit_context_new_function_ptr_type. */
962 :
963 : recording::type *
964 70 : recording::context::new_function_ptr_type (recording::location *, /* unused loc */
965 : recording::type *return_type,
966 : int num_params,
967 : recording::type **param_types,
968 : int is_variadic)
969 : {
970 70 : recording::function_type *fn_type
971 70 : = new_function_type (return_type,
972 : num_params,
973 : param_types,
974 : is_variadic,
975 : false);
976 :
977 : /* Return a pointer-type to the function type. */
978 70 : return fn_type->get_pointer ();
979 : }
980 :
981 : /* Create a recording::param instance and add it to this context's list
982 : of mementos.
983 :
984 : Implements the post-error-checking part of
985 : gcc_jit_context_new_param. */
986 :
987 : recording::param *
988 20959 : recording::context::new_param (recording::location *loc,
989 : recording::type *type,
990 : const char *name)
991 : {
992 20959 : recording::param *result = new recording::param (this, loc, type, new_string (name));
993 20959 : record (result);
994 20959 : return result;
995 : }
996 :
997 : /* Create a recording::function instance and add it to this context's list
998 : of mementos and list of functions.
999 :
1000 : Implements the post-error-checking part of
1001 : gcc_jit_context_new_function. */
1002 :
1003 : recording::function *
1004 4445 : recording::context::new_function (recording::location *loc,
1005 : enum gcc_jit_function_kind kind,
1006 : recording::type *return_type,
1007 : const char *name,
1008 : int num_params,
1009 : recording::param **params,
1010 : int is_variadic,
1011 : enum built_in_function builtin_id)
1012 : {
1013 4445 : recording::function *result =
1014 : new recording::function (this,
1015 : loc, kind, return_type,
1016 : new_string (name),
1017 : num_params, params, is_variadic,
1018 4445 : builtin_id, false);
1019 4445 : record (result);
1020 4445 : m_functions.safe_push (result);
1021 :
1022 4445 : return result;
1023 : }
1024 :
1025 : /* Locate the builtins_manager (if any) for this family of contexts,
1026 : creating it if it doesn't exist already.
1027 :
1028 : All of the recording contexts in a family share one builtins_manager:
1029 : if we have a child context, follow the parent links to get the
1030 : ultimate ancestor context, and look for it/store it there. */
1031 :
1032 : builtins_manager *
1033 15346 : recording::context::get_builtins_manager ()
1034 : {
1035 15566 : if (m_parent_ctxt)
1036 : return m_parent_ctxt->get_builtins_manager ();
1037 :
1038 15346 : if (!m_builtins_manager)
1039 1361 : m_builtins_manager = new builtins_manager (this);
1040 :
1041 15346 : return m_builtins_manager;
1042 : }
1043 :
1044 : /* Get a recording::function instance, which is lazily-created and added
1045 : to the context's lists of mementos.
1046 :
1047 : Implements the post-error-checking part of
1048 : gcc_jit_context_get_builtin_function. */
1049 :
1050 : recording::function *
1051 255 : recording::context::get_builtin_function (const char *name)
1052 : {
1053 255 : builtins_manager *bm = get_builtins_manager ();
1054 255 : return bm->get_builtin_function (name);
1055 : }
1056 :
1057 : /* Create a recording::function instance for a target-specific builtin.
1058 :
1059 : Implements the post-error-checking part of
1060 : gcc_jit_context_get_target_builtin_function. */
1061 :
1062 : recording::function *
1063 10 : recording::context::get_target_builtin_function (const char *name)
1064 : {
1065 10 : const char *asm_name = name;
1066 20 : if (target_function_types.count (name) == 0)
1067 : {
1068 0 : fprintf (stderr, "Cannot find target builtin %s\n", name);
1069 0 : return NULL;
1070 : }
1071 :
1072 10 : recording::function_type* func_type = target_function_types[name]
1073 10 : ->copy (this)->dyn_cast_function_type ();
1074 10 : const vec<type *>& param_types = func_type->get_param_types ();
1075 10 : recording::param **params = new recording::param *[param_types.length ()];
1076 :
1077 10 : int i;
1078 10 : recording::type *param_type;
1079 30 : FOR_EACH_VEC_ELT (param_types, i, param_type)
1080 : {
1081 10 : char buf[16];
1082 10 : snprintf (buf, 16, "arg%d", i);
1083 10 : params[i] = new_param (NULL,
1084 : param_type,
1085 : buf);
1086 : }
1087 :
1088 10 : recording::function *result =
1089 : new recording::function (this,
1090 : NULL,
1091 : GCC_JIT_FUNCTION_IMPORTED,
1092 : func_type->get_return_type (),
1093 : new_string (asm_name),
1094 10 : param_types.length (),
1095 : params,
1096 : func_type->is_variadic (),
1097 : BUILT_IN_NONE,
1098 20 : true);
1099 10 : record (result);
1100 :
1101 10 : return result;
1102 : }
1103 :
1104 : /* Create a recording::global instance and add it to this context's list
1105 : of mementos.
1106 :
1107 : Implements the post-error-checking part of
1108 : gcc_jit_context_new_global. */
1109 :
1110 : recording::lvalue *
1111 2479 : recording::context::new_global (recording::location *loc,
1112 : enum gcc_jit_global_kind kind,
1113 : recording::type *type,
1114 : const char *name)
1115 : {
1116 2479 : recording::global *result =
1117 2479 : new recording::global (this, loc, kind, type, new_string (name));
1118 2479 : record (result);
1119 2479 : m_globals.safe_push (result);
1120 :
1121 2479 : return result;
1122 : }
1123 :
1124 : void
1125 860 : recording::context::new_global_init_rvalue (lvalue *variable,
1126 : rvalue *init)
1127 : {
1128 860 : recording::global_init_rvalue *obj =
1129 860 : new recording::global_init_rvalue (this, variable, init);
1130 860 : record (obj);
1131 :
1132 860 : global *gbl = (global *) variable;
1133 860 : gbl->set_rvalue_init (init); /* Needed by the global for write dump. */
1134 860 : }
1135 :
1136 : /* Create a recording::memento_of_typeinfo instance and add it
1137 : to this context's list of mementos.
1138 :
1139 : Implements the post-error-checking part of
1140 : gcc_jit_context_new_sizeof. */
1141 :
1142 : recording::rvalue *
1143 15 : recording::context::new_sizeof (recording::type *type)
1144 : {
1145 15 : recording::rvalue *result =
1146 15 : new memento_of_typeinfo (this, NULL, type, TYPE_INFO_SIZE_OF);
1147 15 : record (result);
1148 15 : return result;
1149 : }
1150 :
1151 : /* Create a recording::memento_of_typeinfo instance and add it
1152 : to this context's list of mementos.
1153 :
1154 : Implements the post-error-checking part of
1155 : gcc_jit_context_new_alignof. */
1156 :
1157 : recording::rvalue *
1158 15 : recording::context::new_alignof (recording::type *type)
1159 : {
1160 15 : recording::rvalue *result =
1161 15 : new memento_of_typeinfo (this, NULL, type, TYPE_INFO_ALIGN_OF);
1162 15 : record (result);
1163 15 : return result;
1164 : }
1165 :
1166 : /* Create a recording::memento_of_new_string_literal instance and add it
1167 : to this context's list of mementos.
1168 :
1169 : Implements the post-error-checking part of
1170 : gcc_jit_context_new_string_literal. */
1171 :
1172 : recording::rvalue *
1173 1963 : recording::context::new_string_literal (const char *value)
1174 : {
1175 1963 : recording::rvalue *result =
1176 1963 : new memento_of_new_string_literal (this, NULL, new_string (value));
1177 1963 : record (result);
1178 1963 : return result;
1179 : }
1180 :
1181 : /* Create a recording::memento_of_new_rvalue_from_vector instance and add it
1182 : to this context's list of mementos.
1183 :
1184 : Implements the post-error-checking part of
1185 : gcc_jit_context_new_rvalue_from_vector. */
1186 :
1187 : recording::rvalue *
1188 135 : recording::context::new_rvalue_from_vector (location *loc,
1189 : vector_type *type,
1190 : rvalue **elements)
1191 : {
1192 135 : recording::rvalue *result
1193 135 : = new memento_of_new_rvalue_from_vector (this, loc, type, elements);
1194 135 : record (result);
1195 135 : return result;
1196 : }
1197 :
1198 : recording::rvalue *
1199 15 : recording::context::new_rvalue_vector_perm (location *loc,
1200 : rvalue *elements1,
1201 : rvalue *elements2,
1202 : rvalue *mask)
1203 : {
1204 15 : recording::rvalue *result
1205 : = new memento_of_new_rvalue_vector_perm (this, loc, elements1, elements2,
1206 15 : mask);
1207 15 : record (result);
1208 15 : return result;
1209 : }
1210 :
1211 : recording::rvalue *
1212 680 : recording::context::new_ctor (recording::location *loc,
1213 : recording::type *type,
1214 : size_t num_values,
1215 : field **fields,
1216 : rvalue **values)
1217 : {
1218 680 : recording::ctor *result = new ctor (this, loc, type);
1219 :
1220 : /* Short cut for zero init. */
1221 680 : if (!num_values)
1222 : {
1223 60 : record (result);
1224 60 : return result;
1225 : }
1226 :
1227 620 : bool is_struct_or_union = type->is_struct () || type->is_union ();
1228 :
1229 : /* We need to copy fields and values into result's auto_vec:s.
1230 : Both for structs and unions and only values for arrays. */
1231 620 : if (type->is_array () != NULL)
1232 : {
1233 245 : result->m_values.reserve (num_values, false);
1234 :
1235 1275 : for (size_t i = 0; i < num_values; i++)
1236 785 : result->m_values.quick_push (values[i]);
1237 : }
1238 375 : else if (is_struct_or_union && fields)
1239 : {
1240 : /* ctor values are paired with user specified fields. */
1241 :
1242 225 : result->m_values.reserve (num_values, false);
1243 225 : result->m_fields.reserve (num_values, false);
1244 :
1245 840 : for (size_t i = 0; i < num_values; i++)
1246 : {
1247 390 : result->m_values.quick_push (values[i]);
1248 390 : result->m_fields.quick_push (fields[i]);
1249 : }
1250 : }
1251 150 : else if (is_struct_or_union && !fields)
1252 : {
1253 : /* ctor values are in definition order one by one,
1254 : so take the fields from the type object. */
1255 :
1256 150 : result->m_values.reserve (num_values, false);
1257 150 : result->m_fields.reserve (num_values, false);
1258 :
1259 150 : compound_type *ct = reinterpret_cast<compound_type *>(type);
1260 150 : recording::fields *fields = ct->get_fields ();
1261 :
1262 : /* The entry point checks that num_values is not greater than
1263 : the amount of fields in 'fields'. */
1264 375 : for (size_t i = 0; i < num_values; i++)
1265 : {
1266 225 : result->m_values.quick_push (values[i]);
1267 225 : result->m_fields.quick_push (fields->get_field (i));
1268 : }
1269 : }
1270 : else
1271 0 : gcc_unreachable ();
1272 :
1273 620 : record (result);
1274 620 : return result;
1275 : }
1276 :
1277 : /* Create a recording::unary_op instance and add it to this context's
1278 : list of mementos.
1279 :
1280 : Implements the post-error-checking part of
1281 : gcc_jit_context_new_unary_op. */
1282 :
1283 : recording::rvalue *
1284 128 : recording::context::new_unary_op (recording::location *loc,
1285 : enum gcc_jit_unary_op op,
1286 : recording::type *result_type,
1287 : recording::rvalue *a)
1288 : {
1289 128 : recording::rvalue *result =
1290 128 : new unary_op (this, loc, op, result_type, a);
1291 128 : record (result);
1292 128 : return result;
1293 : }
1294 :
1295 : /* Create a recording::binary_op instance and add it to this context's
1296 : list of mementos.
1297 :
1298 : Implements the post-error-checking part of
1299 : gcc_jit_context_new_binary_op. */
1300 :
1301 : recording::rvalue *
1302 3333 : recording::context::new_binary_op (recording::location *loc,
1303 : enum gcc_jit_binary_op op,
1304 : recording::type *result_type,
1305 : recording::rvalue *a,
1306 : recording::rvalue *b)
1307 : {
1308 3333 : recording::rvalue *result =
1309 3333 : new binary_op (this, loc, op, result_type, a, b);
1310 3333 : record (result);
1311 3333 : return result;
1312 : }
1313 :
1314 : /* Create a recording::comparison instance and add it to this context's
1315 : list of mementos.
1316 :
1317 : Implements the post-error-checking part of
1318 : gcc_jit_context_new_comparison. */
1319 :
1320 : recording::rvalue *
1321 1524 : recording::context::new_comparison (recording::location *loc,
1322 : enum gcc_jit_comparison op,
1323 : recording::rvalue *a,
1324 : recording::rvalue *b)
1325 : {
1326 1524 : recording::rvalue *result = new comparison (this, loc, op, a, b);
1327 1524 : record (result);
1328 1524 : return result;
1329 : }
1330 :
1331 : /* Create a recording::cast instance and add it to this context's list
1332 : of mementos.
1333 :
1334 : Implements the post-error-checking part of
1335 : gcc_jit_context_new_cast. */
1336 :
1337 : recording::rvalue *
1338 394 : recording::context::new_cast (recording::location *loc,
1339 : recording::rvalue *expr,
1340 : recording::type *type_)
1341 : {
1342 394 : recording::rvalue *result = new cast (this, loc, expr, type_);
1343 394 : record (result);
1344 394 : return result;
1345 : }
1346 :
1347 : /* Create a recording::bitcast instance and add it to this context's list
1348 : of mementos.
1349 :
1350 : Implements the post-error-checking part of
1351 : gcc_jit_context_new_bitcast. */
1352 :
1353 : recording::rvalue *
1354 25 : recording::context::new_bitcast (location *loc,
1355 : rvalue *expr,
1356 : type *type_)
1357 : {
1358 25 : recording::rvalue *result = new bitcast (this, loc, expr, type_);
1359 25 : record (result);
1360 25 : return result;
1361 : }
1362 :
1363 : /* Create a recording::call instance and add it to this context's list
1364 : of mementos.
1365 :
1366 : Implements the post-error-checking part of
1367 : gcc_jit_context_new_call. */
1368 :
1369 : recording::rvalue *
1370 805 : recording::context::new_call (recording::location *loc,
1371 : function *func,
1372 : int numargs , recording::rvalue **args)
1373 : {
1374 805 : recording::rvalue *result = new call (this, loc, func, numargs, args);
1375 805 : record (result);
1376 805 : return result;
1377 : }
1378 :
1379 : /* Create a recording::call_through_ptr instance and add it to this
1380 : context's list of mementos.
1381 :
1382 : Implements the post-error-checking part of
1383 : gcc_jit_context_new_call_through_ptr. */
1384 :
1385 : recording::rvalue *
1386 15 : recording::context::new_call_through_ptr (recording::location *loc,
1387 : recording::rvalue *fn_ptr,
1388 : int numargs,
1389 : recording::rvalue **args)
1390 : {
1391 15 : recording::rvalue *result = new call_through_ptr (this, loc, fn_ptr, numargs, args);
1392 15 : record (result);
1393 15 : return result;
1394 : }
1395 :
1396 : /* Create a recording::array_access instance and add it to this context's list
1397 : of mementos.
1398 :
1399 : Implements the post-error-checking part of
1400 : gcc_jit_context_new_array_access. */
1401 :
1402 : recording::lvalue *
1403 534 : recording::context::new_array_access (recording::location *loc,
1404 : recording::rvalue *ptr,
1405 : recording::rvalue *index)
1406 : {
1407 534 : recording::lvalue *result = new array_access (this, loc, ptr, index);
1408 534 : record (result);
1409 534 : return result;
1410 : }
1411 :
1412 : /* Create a recording::convert_vector instance and add it to this context's list
1413 : of mementos.
1414 :
1415 : Implements the post-error-checking part of
1416 : gcc_jit_context_convert_vector. */
1417 :
1418 : recording::rvalue *
1419 15 : recording::context::new_convert_vector (recording::location *loc,
1420 : recording::rvalue *vector,
1421 : recording::type *type)
1422 : {
1423 : // TODO: instead have an "internal function" memento?
1424 15 : recording::rvalue *result = new convert_vector (this, loc, vector, type);
1425 15 : record (result);
1426 15 : return result;
1427 : }
1428 :
1429 : /* Create a recording::vector_access instance and add it to this context's list
1430 : of mementos.
1431 :
1432 : Implements the post-error-checking part of
1433 : gcc_jit_context_new_vector_access. */
1434 :
1435 : recording::lvalue *
1436 15 : recording::context::new_vector_access (recording::location *loc,
1437 : recording::rvalue *vector,
1438 : recording::rvalue *index)
1439 : {
1440 15 : recording::lvalue *result = new vector_access (this, loc, vector, index);
1441 15 : record (result);
1442 15 : return result;
1443 : }
1444 :
1445 : /* Create a recording::case_ instance and add it to this context's list
1446 : of mementos.
1447 :
1448 : Implements the post-error-checking part of
1449 : gcc_jit_context_new_case. */
1450 :
1451 : recording::case_ *
1452 95 : recording::context::new_case (recording::rvalue *min_value,
1453 : recording::rvalue *max_value,
1454 : recording::block *block)
1455 : {
1456 95 : recording::case_ *result = new case_ (this, min_value, max_value, block);
1457 95 : record (result);
1458 95 : return result;
1459 : }
1460 :
1461 : /* Set the given string option for this context, or add an error if
1462 : it's not recognized.
1463 :
1464 : Implements the post-error-checking part of
1465 : gcc_jit_context_set_str_option. */
1466 :
1467 : void
1468 1787 : recording::context::set_str_option (enum gcc_jit_str_option opt,
1469 : const char *value)
1470 : {
1471 1787 : if (opt < 0 || opt >= GCC_JIT_NUM_STR_OPTIONS)
1472 : {
1473 0 : add_error (NULL,
1474 : "unrecognized (enum gcc_jit_str_option) value: %i", opt);
1475 0 : return;
1476 : }
1477 1787 : free (m_str_options[opt]);
1478 1787 : m_str_options[opt] = value ? xstrdup (value) : NULL;
1479 1787 : log_str_option (opt);
1480 : }
1481 :
1482 : const char*
1483 11468 : recording::context::get_str_option (enum gcc_jit_str_option opt)
1484 : {
1485 11468 : if (opt < 0 || opt >= GCC_JIT_NUM_STR_OPTIONS)
1486 : {
1487 0 : add_error (NULL,
1488 : "unrecognized (enum gcc_jit_str_option) value: %i", opt);
1489 0 : return NULL;
1490 : }
1491 11468 : return m_str_options[opt];
1492 : }
1493 :
1494 : void
1495 5 : recording::context::set_output_ident (const char *ident)
1496 : {
1497 5 : recording::output_ident *memento = new output_ident (this, ident);
1498 5 : record (memento);
1499 5 : }
1500 :
1501 : bool
1502 0 : recording::context::get_abort_on_unsupported_target_builtin ()
1503 : {
1504 0 : return m_abort_on_unsupported_target_builtin;
1505 : }
1506 :
1507 : void
1508 0 : recording::context::set_abort_on_unsupported_target_builtin ()
1509 : {
1510 0 : m_abort_on_unsupported_target_builtin = true;
1511 0 : }
1512 :
1513 : /* Set the given integer option for this context, or add an error if
1514 : it's not recognized.
1515 :
1516 : Implements the post-error-checking part of
1517 : gcc_jit_context_set_int_option. */
1518 :
1519 : void
1520 1832 : recording::context::set_int_option (enum gcc_jit_int_option opt,
1521 : int value)
1522 : {
1523 1832 : if (opt < 0 || opt >= GCC_JIT_NUM_INT_OPTIONS)
1524 : {
1525 0 : add_error (NULL,
1526 : "unrecognized (enum gcc_jit_int_option) value: %i", opt);
1527 0 : return;
1528 : }
1529 1832 : m_int_options[opt] = value;
1530 1832 : log_int_option (opt);
1531 : }
1532 :
1533 : /* Set the given boolean option for this context, or add an error if
1534 : it's not recognized.
1535 :
1536 : Implements the post-error-checking part of
1537 : gcc_jit_context_set_bool_option. */
1538 :
1539 : void
1540 7717 : recording::context::set_bool_option (enum gcc_jit_bool_option opt,
1541 : int value)
1542 : {
1543 7717 : if (opt < 0 || opt >= GCC_JIT_NUM_BOOL_OPTIONS)
1544 : {
1545 0 : add_error (NULL,
1546 : "unrecognized (enum gcc_jit_bool_option) value: %i", opt);
1547 0 : return;
1548 : }
1549 7717 : m_bool_options[opt] = value;
1550 7717 : log_bool_option (opt);
1551 : }
1552 :
1553 : void
1554 10 : recording::context::set_inner_bool_option (enum inner_bool_option inner_opt,
1555 : int value)
1556 : {
1557 10 : gcc_assert (inner_opt >= 0 && inner_opt < NUM_INNER_BOOL_OPTIONS);
1558 10 : m_inner_bool_options[inner_opt] = value;
1559 10 : log_inner_bool_option (inner_opt);
1560 10 : }
1561 :
1562 :
1563 : /* Add the given optname to this context's list of extra options.
1564 :
1565 : Implements the post-error-checking part of
1566 : gcc_jit_context_add_command_line_option. */
1567 :
1568 : void
1569 1416 : recording::context::add_command_line_option (const char *optname)
1570 : {
1571 1416 : m_command_line_options.safe_push (xstrdup (optname));
1572 1417 : }
1573 :
1574 : /* Add any user-provided extra options, starting with any from
1575 : parent contexts.
1576 : Called by playback::context::make_fake_args. */
1577 :
1578 : void
1579 1401 : recording::context::append_command_line_options (vec <char *> *argvec)
1580 : {
1581 1401 : if (m_parent_ctxt)
1582 20 : m_parent_ctxt->append_command_line_options (argvec);
1583 :
1584 1401 : int i;
1585 1401 : char *optname;
1586 2293 : FOR_EACH_VEC_ELT (m_command_line_options, i, optname)
1587 892 : argvec->safe_push (xstrdup (optname));
1588 1401 : }
1589 :
1590 : /* Add the given optname to this context's list of extra driver options. */
1591 :
1592 : void
1593 77 : recording::context::add_driver_option (const char *optname)
1594 : {
1595 77 : m_driver_options.safe_push (xstrdup (optname));
1596 77 : }
1597 :
1598 : /* Add any user-provided driver options, starting with any from
1599 : parent contexts.
1600 : Called by playback::context::invoke_driver. */
1601 :
1602 : void
1603 1179 : recording::context::append_driver_options (auto_string_vec *argvec)
1604 : {
1605 1179 : if (m_parent_ctxt)
1606 20 : m_parent_ctxt->append_driver_options (argvec);
1607 :
1608 1179 : int i;
1609 1179 : char *optname;
1610 :
1611 1194 : FOR_EACH_VEC_ELT (m_driver_options, i, optname)
1612 15 : argvec->safe_push (xstrdup (optname));
1613 1179 : }
1614 :
1615 : /* Add the given dumpname/out_ptr pair to this context's list of requested
1616 : dumps.
1617 :
1618 : Implements the post-error-checking part of
1619 : gcc_jit_context_enable_dump. */
1620 :
1621 : void
1622 50 : recording::context::enable_dump (const char *dumpname,
1623 : char **out_ptr)
1624 : {
1625 50 : requested_dump d;
1626 50 : gcc_assert (dumpname);
1627 50 : gcc_assert (out_ptr);
1628 :
1629 50 : d.m_dumpname = dumpname;
1630 50 : d.m_out_ptr = out_ptr;
1631 50 : *out_ptr = NULL;
1632 50 : m_requested_dumps.safe_push (d);
1633 50 : }
1634 :
1635 : /* Validate this context, and if it passes, compile it to memory
1636 : (within a mutex).
1637 :
1638 : Implements the post-error-checking part of
1639 : gcc_jit_context_compile. */
1640 :
1641 : result *
1642 1744 : recording::context::compile ()
1643 : {
1644 1744 : if (m_populated_target_info)
1645 : {
1646 1 : add_error (NULL,
1647 : "cannot compile after calling gcc_jit_context_get_target_info");
1648 1 : return NULL;
1649 : }
1650 :
1651 1743 : JIT_LOG_SCOPE (get_logger ());
1652 :
1653 1743 : log_all_options ();
1654 :
1655 1743 : validate ();
1656 :
1657 1743 : if (errors_occurred ())
1658 : return NULL;
1659 :
1660 : /* Set up a compile_to_memory playback context. */
1661 1193 : ::gcc::jit::playback::compile_to_memory replayer (this);
1662 :
1663 : /* Use it. */
1664 1192 : replayer.compile ();
1665 :
1666 : /* Get the jit::result (or NULL) from the
1667 : compile_to_memory playback context. */
1668 1193 : return replayer.get_result_obj ();
1669 1743 : }
1670 :
1671 : /* Validate this context, and if it passes, compile it to a file
1672 : (within a mutex).
1673 :
1674 : Implements the post-error-checking part of
1675 : gcc_jit_context_compile_to_file. */
1676 :
1677 : void
1678 126 : recording::context::compile_to_file (enum gcc_jit_output_kind output_kind,
1679 : const char *output_path)
1680 : {
1681 126 : if (m_populated_target_info)
1682 : {
1683 0 : add_error (NULL,
1684 : "cannot compile after calling gcc_jit_context_get_target_info");
1685 0 : return;
1686 : }
1687 :
1688 126 : JIT_LOG_SCOPE (get_logger ());
1689 :
1690 126 : log_all_options ();
1691 :
1692 126 : validate ();
1693 :
1694 126 : if (errors_occurred ())
1695 0 : return;
1696 :
1697 : /* Set up a compile_to_file playback context. */
1698 126 : ::gcc::jit::playback::compile_to_file replayer (this,
1699 : output_kind,
1700 126 : output_path);
1701 :
1702 : /* Use it. */
1703 126 : replayer.compile ();
1704 126 : }
1705 :
1706 : void
1707 62 : recording::context::populate_target_info ()
1708 : {
1709 62 : JIT_LOG_SCOPE (get_logger ());
1710 :
1711 62 : log_all_options ();
1712 :
1713 62 : if (errors_occurred ())
1714 0 : return;
1715 :
1716 62 : add_driver_option ("-fsyntax-only");
1717 62 : m_populated_target_info = true;
1718 :
1719 : /* Set up a populate_target_info playback context. */
1720 62 : ::gcc::jit::playback::populate_target_info replayer (this);
1721 :
1722 : /* Use it. */
1723 62 : replayer.compile ();
1724 :
1725 62 : m_target_info = replayer.move_target_info ();
1726 62 : }
1727 :
1728 : /* Format the given error using printf's conventions, print
1729 : it to stderr, and add it to the context. */
1730 :
1731 : void
1732 25 : recording::context::add_diagnostic (location *loc,
1733 : diagnostics::kind diagnostic_kind, const char *fmt, ...)
1734 : {
1735 25 : va_list ap;
1736 25 : va_start (ap, fmt);
1737 25 : add_error_va (loc, diagnostic_kind, fmt, ap);
1738 25 : va_end (ap);
1739 25 : }
1740 :
1741 : void
1742 479 : recording::context::add_error (location *loc, const char *fmt, ...)
1743 : {
1744 479 : va_list ap;
1745 479 : va_start (ap, fmt);
1746 479 : add_error_va (loc, diagnostics::kind::error, fmt, ap);
1747 479 : va_end (ap);
1748 479 : }
1749 :
1750 : /* Format the given error using printf's conventions, print
1751 : it to stderr, and add it to the context. */
1752 :
1753 : void
1754 5413 : recording::context::add_error_va (location *loc,
1755 : diagnostics::kind diagnostic_kind, const char *fmt, va_list ap)
1756 : {
1757 5413 : int len;
1758 5413 : char *malloced_msg;
1759 5413 : const char *errmsg;
1760 5413 : bool has_ownership;
1761 :
1762 5413 : JIT_LOG_SCOPE (get_logger ());
1763 :
1764 5413 : len = vasprintf (&malloced_msg, fmt, ap);
1765 5413 : if (malloced_msg == NULL || len < 0)
1766 : {
1767 : errmsg = "out of memory generating error message";
1768 : has_ownership = false;
1769 : }
1770 : else
1771 : {
1772 5413 : errmsg = malloced_msg;
1773 5413 : has_ownership = true;
1774 : }
1775 5413 : if (get_logger ())
1776 5412 : get_logger ()->log ("%s %i: %s",
1777 : diagnostics::get_text_for_kind (diagnostic_kind),
1778 : m_error_count, errmsg);
1779 :
1780 5413 : const char *ctxt_progname =
1781 5413 : get_str_option (GCC_JIT_STR_OPTION_PROGNAME);
1782 5413 : if (!ctxt_progname)
1783 6 : ctxt_progname = "libgccjit.so";
1784 :
1785 5413 : bool print_errors_to_stderr =
1786 5413 : get_inner_bool_option (INNER_BOOL_OPTION_PRINT_ERRORS_TO_STDERR);
1787 5413 : if (print_errors_to_stderr)
1788 : {
1789 5413 : if (loc)
1790 1763 : fprintf (stderr, "%s: %s: %s: %s\n",
1791 : ctxt_progname,
1792 : loc->get_debug_string (),
1793 : diagnostics::get_text_for_kind (diagnostic_kind),
1794 : errmsg);
1795 : else
1796 3650 : fprintf (stderr, "%s: %s: %s\n",
1797 : ctxt_progname,
1798 : diagnostics::get_text_for_kind (diagnostic_kind),
1799 : errmsg);
1800 : }
1801 :
1802 5413 : if (!m_error_count)
1803 : {
1804 621 : m_first_error_str = const_cast <char *> (errmsg);
1805 621 : m_owns_first_error_str = has_ownership;
1806 : }
1807 :
1808 5413 : if (m_owns_last_error_str)
1809 4797 : if (m_last_error_str != m_first_error_str)
1810 4457 : free (m_last_error_str);
1811 5413 : m_last_error_str = const_cast <char *> (errmsg);
1812 5413 : m_owns_last_error_str = has_ownership;
1813 :
1814 5413 : if (diagnostic_kind == diagnostics::kind::error)
1815 5403 : m_error_count++;
1816 5413 : }
1817 :
1818 : /* Get the message for the first error that occurred on this context, or
1819 : NULL if no errors have occurred on it.
1820 :
1821 : Implements the post-error-checking part of
1822 : gcc_jit_context_get_first_error. */
1823 :
1824 : const char *
1825 540 : recording::context::get_first_error () const
1826 : {
1827 540 : return m_first_error_str;
1828 : }
1829 :
1830 : /* Get the message for the last error that occurred on this context, or
1831 : NULL if no errors have occurred on it.
1832 :
1833 : Implements the post-error-checking part of
1834 : gcc_jit_context_get_last_error. */
1835 :
1836 : const char *
1837 126 : recording::context::get_last_error () const
1838 : {
1839 126 : return m_last_error_str;
1840 : }
1841 :
1842 : /* Lazily generate and record a recording::type representing an opaque
1843 : struct named "FILE".
1844 :
1845 : For use if client code tries to dereference the result of
1846 : get_type (GCC_JIT_TYPE_FILE_PTR). */
1847 :
1848 : recording::type *
1849 19 : recording::context::get_opaque_FILE_type ()
1850 : {
1851 19 : if (!m_FILE_type)
1852 19 : m_FILE_type = new_struct_type (NULL, "FILE");
1853 19 : return m_FILE_type;
1854 : }
1855 :
1856 : /* Dump a C-like representation of the given context to the given path.
1857 : If UPDATE_LOCATIONS is true, update the locations within the
1858 : context's mementos to point to the dumpfile.
1859 :
1860 : Implements the post-error-checking part of
1861 : gcc_jit_context_dump_to_file. */
1862 :
1863 : void
1864 14 : recording::context::dump_to_file (const char *path, bool update_locations)
1865 : {
1866 14 : int i;
1867 14 : dump d (*this, path, update_locations);
1868 :
1869 : /* Forward declaration of structs and unions. */
1870 14 : compound_type *st;
1871 30 : FOR_EACH_VEC_ELT (m_compound_types, i, st)
1872 : {
1873 2 : d.write ("%s;\n\n", st->get_debug_string ());
1874 : }
1875 :
1876 : /* Content of structs, where set. */
1877 16 : FOR_EACH_VEC_ELT (m_compound_types, i, st)
1878 2 : if (st->get_fields ())
1879 : {
1880 2 : st->get_fields ()->write_to_dump (d);
1881 2 : d.write ("\n");
1882 : }
1883 :
1884 : /* Globals. */
1885 : global *g;
1886 14 : FOR_EACH_VEC_ELT (m_globals, i, g)
1887 : {
1888 0 : g->write_to_dump (d);
1889 : }
1890 14 : if (!m_globals.is_empty ())
1891 0 : d.write ("\n");
1892 :
1893 14 : function *fn;
1894 28 : FOR_EACH_VEC_ELT (m_functions, i, fn)
1895 : {
1896 14 : fn->write_to_dump (d);
1897 : }
1898 :
1899 : top_level_asm *tla;
1900 14 : FOR_EACH_VEC_ELT (m_top_level_asms, i, tla)
1901 0 : tla->write_to_dump (d);
1902 14 : }
1903 :
1904 : static const char * const
1905 : str_option_reproducer_strings[GCC_JIT_NUM_STR_OPTIONS] = {
1906 : "GCC_JIT_STR_OPTION_PROGNAME",
1907 : "GCC_JIT_STR_OPTION_SPECIAL_CHARS_IN_FUNC_NAMES",
1908 : };
1909 :
1910 : static const char * const
1911 : int_option_reproducer_strings[GCC_JIT_NUM_INT_OPTIONS] = {
1912 : "GCC_JIT_INT_OPTION_OPTIMIZATION_LEVEL"
1913 : };
1914 :
1915 : static const char * const
1916 : bool_option_reproducer_strings[GCC_JIT_NUM_BOOL_OPTIONS] = {
1917 : "GCC_JIT_BOOL_OPTION_DEBUGINFO",
1918 : "GCC_JIT_BOOL_OPTION_DUMP_INITIAL_TREE",
1919 : "GCC_JIT_BOOL_OPTION_DUMP_INITIAL_GIMPLE",
1920 : "GCC_JIT_BOOL_OPTION_DUMP_GENERATED_CODE",
1921 : "GCC_JIT_BOOL_OPTION_DUMP_SUMMARY",
1922 : "GCC_JIT_BOOL_OPTION_DUMP_EVERYTHING",
1923 : "GCC_JIT_BOOL_OPTION_SELFCHECK_GC",
1924 : "GCC_JIT_BOOL_OPTION_KEEP_INTERMEDIATES",
1925 : };
1926 :
1927 : static const char * const
1928 : inner_bool_option_reproducer_strings[NUM_INNER_BOOL_OPTIONS] = {
1929 : "gcc_jit_context_set_bool_allow_unreachable_blocks",
1930 : "gcc_jit_context_set_bool_use_external_driver",
1931 : "gcc_jit_context_set_bool_print_errors_to_stderr",
1932 : };
1933 :
1934 : /* Write the current value of all options to the log file (if any). */
1935 :
1936 : void
1937 1931 : recording::context::log_all_options () const
1938 : {
1939 1931 : int opt_idx;
1940 :
1941 1931 : if (!get_logger ())
1942 : return;
1943 :
1944 3531 : for (opt_idx = 0; opt_idx < GCC_JIT_NUM_STR_OPTIONS; opt_idx++)
1945 2354 : log_str_option ((enum gcc_jit_str_option)opt_idx);
1946 :
1947 2354 : for (opt_idx = 0; opt_idx < GCC_JIT_NUM_INT_OPTIONS; opt_idx++)
1948 1177 : log_int_option ((enum gcc_jit_int_option)opt_idx);
1949 :
1950 10593 : for (opt_idx = 0; opt_idx < GCC_JIT_NUM_BOOL_OPTIONS; opt_idx++)
1951 9416 : log_bool_option ((enum gcc_jit_bool_option)opt_idx);
1952 4708 : for (opt_idx = 0; opt_idx < NUM_INNER_BOOL_OPTIONS; opt_idx++)
1953 3531 : log_inner_bool_option ((enum inner_bool_option)opt_idx);
1954 : }
1955 :
1956 : /* Write the current value of the given string option to the
1957 : log file (if any). */
1958 :
1959 : void
1960 4141 : recording::context::log_str_option (enum gcc_jit_str_option opt) const
1961 : {
1962 4141 : gcc_assert (opt < GCC_JIT_NUM_STR_OPTIONS);
1963 4141 : if (get_logger ())
1964 : {
1965 3451 : if (m_str_options[opt])
1966 2199 : log ("%s: \"%s\"",
1967 2199 : str_option_reproducer_strings[opt],
1968 : m_str_options[opt]);
1969 : else
1970 1252 : log ("%s: NULL",
1971 1252 : str_option_reproducer_strings[opt]);
1972 : }
1973 4141 : }
1974 :
1975 : /* Write the current value of the given int option to the
1976 : log file (if any). */
1977 :
1978 : void
1979 3009 : recording::context::log_int_option (enum gcc_jit_int_option opt) const
1980 : {
1981 3009 : gcc_assert (opt < GCC_JIT_NUM_INT_OPTIONS);
1982 3009 : if (get_logger ())
1983 2314 : log ("%s: %i",
1984 : int_option_reproducer_strings[opt],
1985 2314 : m_int_options[opt]);
1986 3009 : }
1987 :
1988 : /* Write the current value of the given bool option to the
1989 : log file (if any). */
1990 :
1991 : void
1992 17131 : recording::context::log_bool_option (enum gcc_jit_bool_option opt) const
1993 : {
1994 17131 : gcc_assert (opt < GCC_JIT_NUM_BOOL_OPTIONS);
1995 17131 : if (get_logger ())
1996 29712 : log ("%s: %s",
1997 14856 : bool_option_reproducer_strings[opt],
1998 14856 : m_bool_options[opt] ? "true" : "false");
1999 17131 : }
2000 :
2001 : /* Write the current value of the given "inner" bool option to the
2002 : log file (if any). */
2003 :
2004 : void
2005 3541 : recording::context::log_inner_bool_option (enum inner_bool_option opt) const
2006 : {
2007 3541 : gcc_assert (opt < NUM_INNER_BOOL_OPTIONS);
2008 3541 : if (get_logger ())
2009 7082 : log ("%s: %s",
2010 3541 : inner_bool_option_reproducer_strings[opt],
2011 3541 : m_inner_bool_options[opt] ? "true" : "false");
2012 3541 : }
2013 :
2014 : /* Write C source code to PATH that attempts to replay the API
2015 : calls made to this context (and its parents), for use in
2016 : minimizing test cases for libgccjit.
2017 :
2018 : Implements the post-error-checking part of
2019 : gcc_jit_context_dump_reproducer_to_file. */
2020 :
2021 : void
2022 1163 : recording::context::dump_reproducer_to_file (const char *path)
2023 : {
2024 1163 : JIT_LOG_SCOPE (get_logger ());
2025 1163 : reproducer r (*this, path);
2026 :
2027 : /* Generate the "ancestry" of this context, as a list. */
2028 1163 : auto_vec <context *> ascending_contexts;
2029 2342 : for (context *ctxt = this; ctxt; ctxt = ctxt->m_parent_ctxt)
2030 1179 : ascending_contexts.safe_push (ctxt);
2031 :
2032 : /* Reverse the list, giving a list of contexts from
2033 : top-most parent context down through to youngest child context.
2034 : We will use this list as the parameters of the functions in
2035 : our generated file. */
2036 1163 : unsigned num_ctxts = ascending_contexts.length ();
2037 1163 : auto_vec <context *> contexts (num_ctxts);
2038 3505 : for (unsigned i = 0; i < num_ctxts; i++)
2039 1179 : contexts.safe_push (ascending_contexts[num_ctxts - (i + 1)]);
2040 :
2041 : /* contexts[0] should be the top-level context. */
2042 1163 : gcc_assert (contexts[0]);
2043 1163 : gcc_assert (contexts[0]->m_toplevel_ctxt == contexts[0]);
2044 :
2045 : /* The final element in contexts should be "this". */
2046 1163 : gcc_assert (contexts[contexts.length () - 1] == this);
2047 1163 : gcc_assert (contexts[contexts.length () - 1]->m_toplevel_ctxt
2048 : == contexts[0]);
2049 :
2050 1163 : r.write ("/* This code was autogenerated by"
2051 : " gcc_jit_context_dump_reproducer_to_file.\n\n");
2052 1163 : print_version (r.get_file (), " ", false);
2053 1163 : r.write ("*/\n");
2054 1163 : r.write ("#include <libgccjit.h>\n\n");
2055 1163 : r.write ("#pragma GCC diagnostic ignored \"-Wunused-variable\"\n\n");
2056 1163 : r.write ("static void\nset_options (");
2057 1163 : r.write_params (contexts);
2058 1163 : r.write (");\n\n");
2059 1163 : r.write ("static void\ncreate_code (");
2060 1163 : r.write_params (contexts);
2061 1163 : r.write (");\n\n");
2062 1163 : r.write ("int\nmain (int argc, const char **argv)\n");
2063 1163 : r.write ("{\n");
2064 3505 : for (unsigned i = 0; i < num_ctxts; i++)
2065 2358 : r.write (" gcc_jit_context *%s;\n",
2066 1179 : r.get_identifier (contexts[i]));
2067 1163 : r.write (" gcc_jit_result *result;\n"
2068 : "\n");
2069 :
2070 : /* Create the contexts.
2071 : The top-level context is acquired from a clean slate, the others as
2072 : children of the prior context. */
2073 1163 : r.write (" %s = gcc_jit_context_acquire ();\n",
2074 1163 : r.get_identifier (contexts[0]));
2075 2342 : for (unsigned i = 1; i < num_ctxts; i++)
2076 32 : r.write (" %s = gcc_jit_context_new_child_context (%s);\n",
2077 16 : r.get_identifier (contexts[i]),
2078 16 : r.get_identifier (contexts[i - 1]));
2079 1163 : r.write (" set_options (");
2080 1163 : r.write_args (contexts);
2081 1163 : r.write (");\n");
2082 1163 : r.write (" create_code (");
2083 1163 : r.write_args (contexts);
2084 1163 : r.write (");\n");
2085 :
2086 1163 : r.write (" result = gcc_jit_context_compile (%s);\n",
2087 : r.get_identifier (this));
2088 :
2089 3505 : for (unsigned i = num_ctxts; i > 0; i--)
2090 1179 : r.write (" gcc_jit_context_release (%s);\n",
2091 1179 : r.get_identifier (contexts[i - 1]));
2092 :
2093 1163 : r.write (" gcc_jit_result_release (result);\n"
2094 : " return 0;\n"
2095 : "}\n\n");
2096 :
2097 : /* Define (char *) variables for use in calls to
2098 : gcc_jit_context_enable_dump. */
2099 3505 : for (unsigned ctxt_idx = 0; ctxt_idx < num_ctxts; ctxt_idx++)
2100 : {
2101 1199 : if (m_requested_dumps.length ())
2102 : {
2103 20 : r.write ("/* Requested dumps for %s. */\n",
2104 20 : r.get_identifier (contexts[ctxt_idx]));
2105 75 : for (unsigned i = 0; i < m_requested_dumps.length (); i++)
2106 70 : r.write ("static char *dump_%p;\n",
2107 35 : (void *)&m_requested_dumps[i]);
2108 20 : r.write ("\n");
2109 : }
2110 : }
2111 :
2112 : /* Write out values of options. */
2113 1163 : r.write ("static void\nset_options (");
2114 1163 : r.write_params (contexts);
2115 1163 : r.write (")\n{\n");
2116 3505 : for (unsigned ctxt_idx = 0; ctxt_idx < num_ctxts; ctxt_idx++)
2117 : {
2118 1179 : if (ctxt_idx > 0)
2119 16 : r.write ("\n");
2120 :
2121 2358 : r.write (" /* Set options for %s. */\n",
2122 1179 : r.get_identifier (contexts[ctxt_idx]));
2123 :
2124 1179 : r.write (" /* String options. */\n");
2125 4716 : for (int opt_idx = 0; opt_idx < GCC_JIT_NUM_STR_OPTIONS; opt_idx++)
2126 : {
2127 2358 : r.write (" gcc_jit_context_set_str_option (%s,\n"
2128 : " %s,\n",
2129 2358 : r.get_identifier (contexts[ctxt_idx]),
2130 2358 : str_option_reproducer_strings[opt_idx]);
2131 2358 : if (m_str_options[opt_idx])
2132 1109 : r.write (" \"%s\");\n",
2133 : m_str_options[opt_idx]);
2134 : else
2135 1249 : r.write (" NULL);\n");
2136 : }
2137 1179 : r.write (" /* Int options. */\n");
2138 3537 : for (int opt_idx = 0; opt_idx < GCC_JIT_NUM_INT_OPTIONS; opt_idx++)
2139 1179 : r.write (" gcc_jit_context_set_int_option (%s,\n"
2140 : " %s,\n"
2141 : " %i);\n",
2142 1179 : r.get_identifier (contexts[ctxt_idx]),
2143 1179 : int_option_reproducer_strings[opt_idx],
2144 : m_int_options[opt_idx]);
2145 1179 : r.write (" /* Boolean options. */\n");
2146 11790 : for (int opt_idx = 0; opt_idx < GCC_JIT_NUM_BOOL_OPTIONS; opt_idx++)
2147 9432 : r.write (" gcc_jit_context_set_bool_option (%s,\n"
2148 : " %s,\n"
2149 : " %i);\n",
2150 9432 : r.get_identifier (contexts[ctxt_idx]),
2151 9432 : bool_option_reproducer_strings[opt_idx],
2152 9432 : m_bool_options[opt_idx]);
2153 4716 : for (int opt_idx = 0; opt_idx < NUM_INNER_BOOL_OPTIONS; opt_idx++)
2154 10611 : r.write (" %s (%s, %i);\n",
2155 3537 : inner_bool_option_reproducer_strings[opt_idx],
2156 3537 : r.get_identifier (contexts[ctxt_idx]),
2157 3537 : m_inner_bool_options[opt_idx]);
2158 :
2159 2269 : if (!m_command_line_options.is_empty ())
2160 : {
2161 1090 : int i;
2162 1090 : char *optname;
2163 1090 : r.write (" /* User-provided command-line options. */\n");
2164 3305 : FOR_EACH_VEC_ELT (m_command_line_options, i, optname)
2165 2250 : r.write (" gcc_jit_context_add_command_line_option (%s, \"%s\");\n",
2166 1125 : r.get_identifier (contexts[ctxt_idx]),
2167 : optname);
2168 : }
2169 :
2170 1189 : if (!m_driver_options.is_empty ())
2171 : {
2172 10 : int i;
2173 10 : char *optname;
2174 10 : r.write (" /* User-provided driver options. */\n");
2175 35 : FOR_EACH_VEC_ELT (m_driver_options, i, optname)
2176 30 : r.write (" gcc_jit_context_add_driver_option (%s, \"%s\");\n",
2177 15 : r.get_identifier (contexts[ctxt_idx]),
2178 : optname);
2179 : }
2180 :
2181 1179 : if (m_requested_dumps.length ())
2182 : {
2183 20 : r.write (" /* Requested dumps. */\n");
2184 : /* Dumpfiles that were requested via gcc_jit_context_enable_dump. */
2185 1234 : for (unsigned i = 0; i < m_requested_dumps.length (); i++)
2186 : {
2187 35 : r.write (" gcc_jit_context_enable_dump (%s,\n"
2188 : " \"%s\",\n"
2189 : " &dump_%p);\n",
2190 35 : r.get_identifier (contexts[ctxt_idx]),
2191 35 : m_requested_dumps[i].m_dumpname,
2192 35 : (void *)&m_requested_dumps[i]);
2193 : }
2194 : }
2195 : }
2196 1163 : r.write ("}\n\n");
2197 :
2198 1163 : r.write ("static void\ncreate_code (");
2199 1163 : r.write_params (contexts);
2200 1163 : r.write (")\n"
2201 : "{\n");
2202 3505 : for (unsigned ctxt_idx = 0; ctxt_idx < num_ctxts; ctxt_idx++)
2203 : {
2204 1179 : memento *m;
2205 1179 : int i;
2206 1179 : if (ctxt_idx > 0)
2207 16 : r.write ("\n\n");
2208 :
2209 1179 : r.write (" /* Replay of API calls for %s. */\n",
2210 1179 : r.get_identifier (contexts[ctxt_idx]));
2211 176242 : FOR_EACH_VEC_ELT (contexts[ctxt_idx]->m_mementos, i, m)
2212 172785 : m->write_reproducer (r);
2213 : }
2214 1163 : r.write ("}\n");
2215 1163 : }
2216 :
2217 : /* Copy the requested dumps within this context and all ancestors into
2218 : OUT. */
2219 :
2220 : void
2221 1401 : recording::context::get_all_requested_dumps (vec <recording::requested_dump> *out)
2222 : {
2223 1401 : if (m_parent_ctxt)
2224 20 : m_parent_ctxt->get_all_requested_dumps (out);
2225 :
2226 1436 : out->reserve (m_requested_dumps.length ());
2227 1401 : out->splice (m_requested_dumps);
2228 1401 : }
2229 :
2230 : /* Create a recording::top_level_asm instance and add it to this
2231 : context's list of mementos and to m_top_level_asms.
2232 :
2233 : Implements the post-error-checking part of
2234 : gcc_jit_context_add_top_level_asm. */
2235 :
2236 : void
2237 10 : recording::context::add_top_level_asm (recording::location *loc,
2238 : const char *asm_stmts)
2239 : {
2240 10 : recording::top_level_asm *asm_obj
2241 10 : = new recording::top_level_asm (this, loc, new_string (asm_stmts));
2242 10 : record (asm_obj);
2243 10 : m_top_level_asms.safe_push (asm_obj);
2244 10 : }
2245 :
2246 : /* This is a pre-compilation check for the context (and any parents).
2247 :
2248 : Detect errors within the context, adding errors if any are found. */
2249 :
2250 : void
2251 1889 : recording::context::validate ()
2252 : {
2253 1889 : JIT_LOG_SCOPE (get_logger ());
2254 :
2255 1889 : if (m_parent_ctxt)
2256 20 : m_parent_ctxt->validate ();
2257 :
2258 1888 : int i;
2259 1888 : function *fn;
2260 6351 : FOR_EACH_VEC_ELT (m_functions, i, fn)
2261 4463 : fn->validate ();
2262 1888 : }
2263 :
2264 : /* The implementation of class gcc::jit::recording::memento. */
2265 :
2266 : /* Get a (const char *) debug description of the given memento, by
2267 : calling the pure-virtual make_debug_string hook, caching the
2268 : result.
2269 :
2270 : It is intended that this should only be called in debugging and
2271 : error-handling paths, so this doesn't need to be particularly
2272 : optimized. */
2273 :
2274 : const char *
2275 190872 : recording::memento::get_debug_string ()
2276 : {
2277 190872 : if (!m_debug_string)
2278 93428 : m_debug_string = make_debug_string ();
2279 190872 : return m_debug_string->c_str ();
2280 : }
2281 :
2282 : /* Default implementation of recording::memento::write_to_dump, writing
2283 : an indented form of the memento's debug string to the dump. */
2284 :
2285 : void
2286 156 : recording::memento::write_to_dump (dump &d)
2287 : {
2288 156 : d.write (" %s\n", get_debug_string ());
2289 156 : }
2290 :
2291 : /* The implementation of class gcc::jit::recording::string. */
2292 :
2293 : /* Constructor for gcc::jit::recording::string::string, allocating a
2294 : copy of the given text using new char[]. */
2295 :
2296 140743 : recording::string::string (context *ctxt, const char *text, bool escaped)
2297 : : memento (ctxt),
2298 140743 : m_escaped (escaped)
2299 : {
2300 140740 : m_len = strlen (text);
2301 140740 : m_buffer = new char[m_len + 1];
2302 140743 : strcpy (m_buffer, text);
2303 140743 : }
2304 :
2305 : /* Destructor for gcc::jit::recording::string::string. */
2306 :
2307 279528 : recording::string::~string ()
2308 : {
2309 139764 : delete[] m_buffer;
2310 279528 : }
2311 :
2312 : /* Function for making gcc::jit::recording::string instances on a
2313 : context via printf-style formatting.
2314 :
2315 : It is intended that this should only be called in debugging and
2316 : error-handling paths, so this doesn't need to be particularly
2317 : optimized, hence the double-copy of the string is acceptable. */
2318 :
2319 : recording::string *
2320 30271 : recording::string::from_printf (context *ctxt, const char *fmt, ...)
2321 : {
2322 30271 : int len;
2323 30271 : va_list ap;
2324 30271 : char *buf;
2325 30271 : recording::string *result;
2326 :
2327 30271 : va_start (ap, fmt);
2328 30271 : len = vasprintf (&buf, fmt, ap);
2329 30271 : va_end (ap);
2330 :
2331 30271 : if (buf == NULL || len < 0)
2332 : {
2333 0 : ctxt->add_error (NULL, "malloc failure");
2334 0 : return NULL;
2335 : }
2336 :
2337 30271 : result = ctxt->new_string (buf);
2338 30271 : free (buf);
2339 30271 : return result;
2340 : }
2341 :
2342 : /* Implementation of recording::memento::make_debug_string for strings,
2343 : wrapping the given string in quotes and escaping as necessary. */
2344 :
2345 : recording::string *
2346 34940 : recording::string::make_debug_string ()
2347 : {
2348 : /* Avoid infinite recursion into strings when logging all mementos:
2349 : don't re-escape strings: */
2350 34940 : if (m_escaped)
2351 : return this;
2352 :
2353 : /* Wrap in quotes and do escaping etc */
2354 :
2355 34940 : size_t sz = (1 /* opening quote */
2356 34940 : + (m_len * 2) /* each char might get escaped */
2357 : + 1 /* closing quote */
2358 34940 : + 1); /* nil termintator */
2359 34940 : char *tmp = new char[sz];
2360 34940 : size_t len = 0;
2361 :
2362 : #define APPEND(CH) do { gcc_assert (len < sz); tmp[len++] = (CH); } while (0)
2363 34940 : APPEND('"'); /* opening quote */
2364 2930159 : for (size_t i = 0; i < m_len ; i++)
2365 : {
2366 2895219 : char ch = m_buffer[i];
2367 2895219 : switch (ch)
2368 : {
2369 2894819 : default:
2370 2894819 : APPEND(ch);
2371 2894819 : break;
2372 165 : case '\t':
2373 165 : APPEND('\\');
2374 165 : APPEND('t');
2375 165 : break;
2376 220 : case '\n':
2377 220 : APPEND('\\');
2378 220 : APPEND('n');
2379 220 : break;
2380 15 : case '\\':
2381 15 : case '"':
2382 15 : APPEND('\\');
2383 15 : APPEND(ch);
2384 15 : break;
2385 : }
2386 : }
2387 34940 : APPEND('"'); /* closing quote */
2388 : #undef APPEND
2389 34940 : tmp[len] = '\0'; /* nil termintator */
2390 :
2391 34940 : string *result = m_ctxt->new_string (tmp, true);
2392 :
2393 34940 : delete[] tmp;
2394 : return result;
2395 : }
2396 :
2397 : /* Implementation of recording::memento::write_reproducer for strings. */
2398 :
2399 : void
2400 106413 : recording::string::write_reproducer (reproducer &)
2401 : {
2402 : /* Empty. */
2403 106413 : }
2404 :
2405 : /* The implementation of class gcc::jit::recording::output_ident. */
2406 :
2407 : /* Constructor for gcc::jit::recording::output_ident, allocating a
2408 : copy of the given text using new char[]. */
2409 :
2410 5 : recording::output_ident::output_ident (context *ctxt, const char *ident)
2411 5 : : memento (ctxt)
2412 : {
2413 5 : m_ident = ident ? xstrdup (ident) : NULL;
2414 5 : }
2415 :
2416 : /* Destructor for gcc::jit::recording::output_ident. */
2417 :
2418 10 : recording::output_ident::~output_ident ()
2419 : {
2420 5 : free (m_ident);
2421 10 : }
2422 :
2423 : /* Implementation of pure virtual hook recording::memento::replay_into
2424 : for recording::output_ident. */
2425 :
2426 : void
2427 5 : recording::output_ident::replay_into (replayer *r)
2428 : {
2429 5 : r->set_output_ident (m_ident);
2430 5 : }
2431 :
2432 : /* Implementation of recording::memento::make_debug_string for output_ident. */
2433 :
2434 : recording::string *
2435 0 : recording::output_ident::make_debug_string ()
2436 : {
2437 0 : return m_ctxt->new_string (m_ident);
2438 : }
2439 :
2440 : /* Implementation of recording::memento::write_reproducer for output_ident. */
2441 :
2442 : void
2443 5 : recording::output_ident::write_reproducer (reproducer &r)
2444 : {
2445 5 : r.write (" gcc_jit_context_set_output_ident (%s, \"%s\");",
2446 : r.get_identifier (get_context ()),
2447 : m_ident);
2448 5 : }
2449 :
2450 :
2451 : /* The implementation of class gcc::jit::recording::location. */
2452 :
2453 : /* Implementation of recording::memento::replay_into for locations.
2454 :
2455 : Create a new playback::location and store it into the
2456 : recording::location's m_playback_obj field. */
2457 :
2458 : void
2459 1020 : recording::location::replay_into (replayer *r)
2460 : {
2461 2040 : m_playback_obj = r->new_location (this,
2462 1020 : m_filename->c_str (),
2463 : m_line,
2464 : m_column);
2465 1020 : }
2466 :
2467 : /* Implementation of recording::memento::make_debug_string for locations,
2468 : turning them into the usual form:
2469 : FILENAME:LINE:COLUMN
2470 : like we do when emitting diagnostics. */
2471 :
2472 : recording::string *
2473 9554 : recording::location::make_debug_string ()
2474 : {
2475 9554 : return string::from_printf (m_ctxt,
2476 : "%s:%i:%i",
2477 9554 : m_filename->c_str (), m_line, m_column);
2478 : }
2479 :
2480 : /* Implementation of recording::memento::write_reproducer for locations. */
2481 :
2482 : void
2483 9571 : recording::location::write_reproducer (reproducer &r)
2484 : {
2485 9571 : const char *id = r.make_identifier (this, "loc");
2486 9571 : r.write (" gcc_jit_location *%s =\n"
2487 : " gcc_jit_context_new_location (%s, /* gcc_jit_context *ctxt */\n"
2488 : " %s, /* const char *filename */\n"
2489 : " %i, /* int line */\n"
2490 : " %i);/* int column */\n",
2491 : id,
2492 : r.get_identifier (get_context ()),
2493 9571 : m_filename->get_debug_string (),
2494 : m_line, m_column);
2495 9571 : }
2496 :
2497 : /* The implementation of class gcc::jit::recording::type. */
2498 :
2499 : /* Given a type T, get the type T*.
2500 :
2501 : If this doesn't already exist, generate a new memento_of_get_pointer
2502 : instance and add it to this type's context's list of mementos.
2503 :
2504 : Otherwise, use the cached type.
2505 :
2506 : Implements the post-error-checking part of
2507 : gcc_jit_type_get_pointer. */
2508 :
2509 : recording::type *
2510 49986 : recording::type::get_pointer ()
2511 : {
2512 49986 : if (!m_pointer_to_this_type)
2513 : {
2514 44866 : m_pointer_to_this_type = new memento_of_get_pointer (this);
2515 44866 : m_ctxt->record (m_pointer_to_this_type);
2516 : }
2517 49986 : return m_pointer_to_this_type;
2518 : }
2519 :
2520 : /* Given a type T, get the type const T.
2521 :
2522 : Implements the post-error-checking part of
2523 : gcc_jit_type_get_const. */
2524 :
2525 : recording::type *
2526 816 : recording::type::get_const ()
2527 : {
2528 816 : recording::type *result = new memento_of_get_const (this);
2529 816 : m_ctxt->record (result);
2530 816 : return result;
2531 : }
2532 :
2533 : /* Given a type T, get the type restrict T.
2534 :
2535 : Implements the post-error-checking part of
2536 : gcc_jit_type_get_restrict. */
2537 :
2538 : recording::type *
2539 10 : recording::type::get_restrict ()
2540 : {
2541 10 : recording::type *result = new memento_of_get_restrict (this);
2542 10 : m_ctxt->record (result);
2543 10 : return result;
2544 : }
2545 :
2546 : /* Given a type T, get the type volatile T.
2547 :
2548 : Implements the post-error-checking part of
2549 : gcc_jit_type_get_volatile. */
2550 :
2551 : recording::type *
2552 291 : recording::type::get_volatile ()
2553 : {
2554 291 : recording::type *result = new memento_of_get_volatile (this);
2555 291 : m_ctxt->record (result);
2556 291 : return result;
2557 : }
2558 :
2559 : /* Given a type, get an aligned version of the type.
2560 :
2561 : Implements the post-error-checking part of
2562 : gcc_jit_type_get_aligned. */
2563 :
2564 : recording::type *
2565 414 : recording::type::get_aligned (size_t alignment_in_bytes)
2566 : {
2567 414 : recording::type *result
2568 414 : = new memento_of_get_aligned (this, alignment_in_bytes);
2569 414 : m_ctxt->record (result);
2570 414 : return result;
2571 : }
2572 :
2573 : /* Given a type, get a vector version of the type.
2574 :
2575 : Implements the post-error-checking part of
2576 : gcc_jit_type_get_vector. */
2577 :
2578 : recording::type *
2579 1425533 : recording::type::get_vector (size_t num_units)
2580 : {
2581 1425533 : recording::type *result
2582 1425533 : = new vector_type (this, num_units);
2583 1425533 : m_ctxt->record (result);
2584 1425532 : return result;
2585 : }
2586 :
2587 : const char *
2588 28475 : recording::type::access_as_type (reproducer &r)
2589 : {
2590 28475 : return r.get_identifier (this);
2591 : }
2592 :
2593 : /* Override of default implementation of
2594 : recording::type::get_size.
2595 :
2596 : Return the size in bytes. This is in use for global
2597 : initialization. */
2598 :
2599 : size_t
2600 27053 : recording::memento_of_get_type::get_size ()
2601 : {
2602 27053 : int size;
2603 27053 : machine_mode m;
2604 27053 : switch (m_kind)
2605 : {
2606 : case GCC_JIT_TYPE_VOID:
2607 : return 0;
2608 2511 : case GCC_JIT_TYPE_BOOL:
2609 2511 : case GCC_JIT_TYPE_CHAR:
2610 2511 : case GCC_JIT_TYPE_SIGNED_CHAR:
2611 2511 : case GCC_JIT_TYPE_UNSIGNED_CHAR:
2612 2511 : return 1;
2613 : case GCC_JIT_TYPE_SHORT:
2614 : case GCC_JIT_TYPE_UNSIGNED_SHORT:
2615 : size = SHORT_TYPE_SIZE;
2616 : break;
2617 : case GCC_JIT_TYPE_INT:
2618 : case GCC_JIT_TYPE_UNSIGNED_INT:
2619 18807 : size = INT_TYPE_SIZE;
2620 : break;
2621 720 : case GCC_JIT_TYPE_LONG:
2622 720 : case GCC_JIT_TYPE_UNSIGNED_LONG:
2623 720 : size = LONG_TYPE_SIZE;
2624 : break;
2625 : case GCC_JIT_TYPE_LONG_LONG:
2626 : case GCC_JIT_TYPE_UNSIGNED_LONG_LONG:
2627 1511 : size = LONG_LONG_TYPE_SIZE;
2628 : break;
2629 60 : case GCC_JIT_TYPE_UINT8_T:
2630 60 : case GCC_JIT_TYPE_INT8_T:
2631 60 : size = 8;
2632 60 : break;
2633 : case GCC_JIT_TYPE_UINT16_T:
2634 : case GCC_JIT_TYPE_INT16_T:
2635 : size = 16;
2636 : break;
2637 : case GCC_JIT_TYPE_UINT32_T:
2638 : case GCC_JIT_TYPE_INT32_T:
2639 18807 : size = 32;
2640 : break;
2641 : case GCC_JIT_TYPE_UINT64_T:
2642 : case GCC_JIT_TYPE_INT64_T:
2643 1511 : size = 64;
2644 : break;
2645 : case GCC_JIT_TYPE_UINT128_T:
2646 : case GCC_JIT_TYPE_INT128_T:
2647 105 : size = 128;
2648 : break;
2649 893 : case GCC_JIT_TYPE_FLOAT:
2650 893 : m = targetm.c.mode_for_floating_type (TI_FLOAT_TYPE);
2651 893 : size = GET_MODE_PRECISION (m).to_constant ();
2652 893 : break;
2653 : #ifdef HAVE_BFmode
2654 0 : case GCC_JIT_TYPE_BFLOAT16:
2655 0 : return GET_MODE_UNIT_SIZE (BFmode);
2656 : #endif
2657 2447 : case GCC_JIT_TYPE_DOUBLE:
2658 2447 : m = targetm.c.mode_for_floating_type (TI_DOUBLE_TYPE);
2659 2447 : size = GET_MODE_PRECISION (m).to_constant ();
2660 2447 : break;
2661 76 : case GCC_JIT_TYPE_LONG_DOUBLE:
2662 76 : m = targetm.c.mode_for_floating_type (TI_LONG_DOUBLE_TYPE);
2663 76 : size = GET_MODE_PRECISION (m).to_constant ();
2664 76 : break;
2665 : case GCC_JIT_TYPE_FLOAT16:
2666 : size = 16;
2667 : break;
2668 : case GCC_JIT_TYPE_FLOAT32:
2669 18807 : size = 32;
2670 : break;
2671 : case GCC_JIT_TYPE_FLOAT64:
2672 1511 : size = 64;
2673 : break;
2674 : case GCC_JIT_TYPE_FLOAT128:
2675 105 : size = 128;
2676 : break;
2677 : case GCC_JIT_TYPE_SIZE_T:
2678 1511 : size = MAX_BITS_PER_WORD;
2679 : break;
2680 0 : default:
2681 : /* As this function is called by
2682 : 'gcc_jit_global_set_initializer' and
2683 : 'recording::global::write_reproducer' possible types are only
2684 : integrals and are covered by the previous cases. */
2685 0 : gcc_unreachable ();
2686 : }
2687 :
2688 24542 : return size / BITS_PER_UNIT;
2689 : }
2690 :
2691 : /* Implementation of pure virtual hook recording::type::dereference for
2692 : recording::memento_of_get_type. */
2693 :
2694 : recording::type *
2695 1097 : recording::memento_of_get_type::dereference ()
2696 : {
2697 1097 : switch (m_kind)
2698 : {
2699 0 : default: gcc_unreachable ();
2700 :
2701 : case GCC_JIT_TYPE_VOID:
2702 : return NULL;
2703 :
2704 226 : case GCC_JIT_TYPE_VOID_PTR:
2705 226 : return m_ctxt->get_type (GCC_JIT_TYPE_VOID);
2706 :
2707 : case GCC_JIT_TYPE_BOOL:
2708 : case GCC_JIT_TYPE_CHAR:
2709 : case GCC_JIT_TYPE_SIGNED_CHAR:
2710 : case GCC_JIT_TYPE_UNSIGNED_CHAR:
2711 : case GCC_JIT_TYPE_SHORT:
2712 : case GCC_JIT_TYPE_UNSIGNED_SHORT:
2713 : case GCC_JIT_TYPE_INT:
2714 : case GCC_JIT_TYPE_UNSIGNED_INT:
2715 : case GCC_JIT_TYPE_LONG:
2716 : case GCC_JIT_TYPE_UNSIGNED_LONG:
2717 : case GCC_JIT_TYPE_LONG_LONG:
2718 : case GCC_JIT_TYPE_UNSIGNED_LONG_LONG:
2719 : case GCC_JIT_TYPE_UINT8_T:
2720 : case GCC_JIT_TYPE_UINT16_T:
2721 : case GCC_JIT_TYPE_UINT32_T:
2722 : case GCC_JIT_TYPE_UINT64_T:
2723 : case GCC_JIT_TYPE_UINT128_T:
2724 : case GCC_JIT_TYPE_INT8_T:
2725 : case GCC_JIT_TYPE_INT16_T:
2726 : case GCC_JIT_TYPE_INT32_T:
2727 : case GCC_JIT_TYPE_INT64_T:
2728 : case GCC_JIT_TYPE_INT128_T:
2729 : case GCC_JIT_TYPE_FLOAT:
2730 : case GCC_JIT_TYPE_BFLOAT16:
2731 : case GCC_JIT_TYPE_DOUBLE:
2732 : case GCC_JIT_TYPE_LONG_DOUBLE:
2733 : case GCC_JIT_TYPE_FLOAT16:
2734 : case GCC_JIT_TYPE_FLOAT32:
2735 : case GCC_JIT_TYPE_FLOAT64:
2736 : case GCC_JIT_TYPE_FLOAT128:
2737 : case GCC_JIT_TYPE_COMPLEX_FLOAT:
2738 : case GCC_JIT_TYPE_COMPLEX_DOUBLE:
2739 : case GCC_JIT_TYPE_COMPLEX_LONG_DOUBLE:
2740 : /* Not a pointer: */
2741 : return NULL;
2742 :
2743 120 : case GCC_JIT_TYPE_CONST_CHAR_PTR:
2744 120 : return m_ctxt->get_type (GCC_JIT_TYPE_CHAR)->get_const ();
2745 :
2746 : case GCC_JIT_TYPE_SIZE_T:
2747 : /* Not a pointer: */
2748 : return NULL;
2749 :
2750 19 : case GCC_JIT_TYPE_FILE_PTR:
2751 : /* Give the client code back an opaque "struct FILE". */
2752 19 : return m_ctxt->get_opaque_FILE_type ();
2753 : }
2754 : }
2755 :
2756 : /* Implementation of pure virtual hook recording::type::is_int for
2757 : recording::memento_of_get_type. */
2758 :
2759 : bool
2760 43047 : recording::memento_of_get_type::is_int () const
2761 : {
2762 43047 : switch (m_kind)
2763 : {
2764 0 : default: gcc_unreachable ();
2765 :
2766 : case GCC_JIT_TYPE_VOID:
2767 : return false;
2768 :
2769 : case GCC_JIT_TYPE_VOID_PTR:
2770 : return false;
2771 :
2772 : case GCC_JIT_TYPE_BOOL:
2773 : return false;
2774 :
2775 : case GCC_JIT_TYPE_CHAR:
2776 : case GCC_JIT_TYPE_SIGNED_CHAR:
2777 : case GCC_JIT_TYPE_UNSIGNED_CHAR:
2778 : case GCC_JIT_TYPE_SHORT:
2779 : case GCC_JIT_TYPE_UNSIGNED_SHORT:
2780 : case GCC_JIT_TYPE_INT:
2781 : case GCC_JIT_TYPE_UNSIGNED_INT:
2782 : case GCC_JIT_TYPE_LONG:
2783 : case GCC_JIT_TYPE_UNSIGNED_LONG:
2784 : case GCC_JIT_TYPE_LONG_LONG:
2785 : case GCC_JIT_TYPE_UNSIGNED_LONG_LONG:
2786 : case GCC_JIT_TYPE_UINT8_T:
2787 : case GCC_JIT_TYPE_UINT16_T:
2788 : case GCC_JIT_TYPE_UINT32_T:
2789 : case GCC_JIT_TYPE_UINT64_T:
2790 : case GCC_JIT_TYPE_UINT128_T:
2791 : case GCC_JIT_TYPE_INT8_T:
2792 : case GCC_JIT_TYPE_INT16_T:
2793 : case GCC_JIT_TYPE_INT32_T:
2794 : case GCC_JIT_TYPE_INT64_T:
2795 : case GCC_JIT_TYPE_INT128_T:
2796 : return true;
2797 :
2798 : case GCC_JIT_TYPE_FLOAT:
2799 : case GCC_JIT_TYPE_BFLOAT16:
2800 : case GCC_JIT_TYPE_DOUBLE:
2801 : case GCC_JIT_TYPE_LONG_DOUBLE:
2802 : case GCC_JIT_TYPE_FLOAT16:
2803 : case GCC_JIT_TYPE_FLOAT32:
2804 : case GCC_JIT_TYPE_FLOAT64:
2805 : case GCC_JIT_TYPE_FLOAT128:
2806 : return false;
2807 :
2808 : case GCC_JIT_TYPE_CONST_CHAR_PTR:
2809 : return false;
2810 :
2811 : case GCC_JIT_TYPE_SIZE_T:
2812 : return true;
2813 :
2814 : case GCC_JIT_TYPE_FILE_PTR:
2815 : return false;
2816 :
2817 : case GCC_JIT_TYPE_COMPLEX_FLOAT:
2818 : case GCC_JIT_TYPE_COMPLEX_DOUBLE:
2819 : case GCC_JIT_TYPE_COMPLEX_LONG_DOUBLE:
2820 : return false;
2821 : }
2822 : }
2823 :
2824 : /* Implementation of pure virtual hook recording::type::is_signed for
2825 : recording::memento_of_get_type. */
2826 :
2827 : bool
2828 26140 : recording::memento_of_get_type::is_signed () const
2829 : {
2830 26140 : switch (m_kind)
2831 : {
2832 0 : default: gcc_unreachable ();
2833 :
2834 : case GCC_JIT_TYPE_SIGNED_CHAR:
2835 : case GCC_JIT_TYPE_CHAR:
2836 : case GCC_JIT_TYPE_SHORT:
2837 : case GCC_JIT_TYPE_INT:
2838 : case GCC_JIT_TYPE_LONG:
2839 : case GCC_JIT_TYPE_LONG_LONG:
2840 : case GCC_JIT_TYPE_INT8_T:
2841 : case GCC_JIT_TYPE_INT16_T:
2842 : case GCC_JIT_TYPE_INT32_T:
2843 : case GCC_JIT_TYPE_INT64_T:
2844 : case GCC_JIT_TYPE_INT128_T:
2845 : return true;
2846 :
2847 5933 : case GCC_JIT_TYPE_VOID:
2848 5933 : case GCC_JIT_TYPE_VOID_PTR:
2849 5933 : case GCC_JIT_TYPE_BOOL:
2850 5933 : case GCC_JIT_TYPE_UNSIGNED_CHAR:
2851 5933 : case GCC_JIT_TYPE_UNSIGNED_SHORT:
2852 5933 : case GCC_JIT_TYPE_UNSIGNED_INT:
2853 5933 : case GCC_JIT_TYPE_UNSIGNED_LONG:
2854 5933 : case GCC_JIT_TYPE_UNSIGNED_LONG_LONG:
2855 5933 : case GCC_JIT_TYPE_UINT8_T:
2856 5933 : case GCC_JIT_TYPE_UINT16_T:
2857 5933 : case GCC_JIT_TYPE_UINT32_T:
2858 5933 : case GCC_JIT_TYPE_UINT64_T:
2859 5933 : case GCC_JIT_TYPE_UINT128_T:
2860 :
2861 5933 : case GCC_JIT_TYPE_FLOAT:
2862 5933 : case GCC_JIT_TYPE_BFLOAT16:
2863 5933 : case GCC_JIT_TYPE_DOUBLE:
2864 5933 : case GCC_JIT_TYPE_LONG_DOUBLE:
2865 5933 : case GCC_JIT_TYPE_FLOAT16:
2866 5933 : case GCC_JIT_TYPE_FLOAT32:
2867 5933 : case GCC_JIT_TYPE_FLOAT64:
2868 5933 : case GCC_JIT_TYPE_FLOAT128:
2869 :
2870 5933 : case GCC_JIT_TYPE_CONST_CHAR_PTR:
2871 :
2872 5933 : case GCC_JIT_TYPE_SIZE_T:
2873 :
2874 5933 : case GCC_JIT_TYPE_FILE_PTR:
2875 :
2876 5933 : case GCC_JIT_TYPE_COMPLEX_FLOAT:
2877 5933 : case GCC_JIT_TYPE_COMPLEX_DOUBLE:
2878 5933 : case GCC_JIT_TYPE_COMPLEX_LONG_DOUBLE:
2879 5933 : return false;
2880 : }
2881 : }
2882 :
2883 : /* Implementation of pure virtual hook recording::type::is_float for
2884 : recording::memento_of_get_type. */
2885 :
2886 : bool
2887 9337 : recording::memento_of_get_type::is_float () const
2888 : {
2889 9337 : switch (m_kind)
2890 : {
2891 0 : default: gcc_unreachable ();
2892 :
2893 : case GCC_JIT_TYPE_VOID:
2894 : return false;
2895 :
2896 : case GCC_JIT_TYPE_VOID_PTR:
2897 : return false;
2898 :
2899 : case GCC_JIT_TYPE_BOOL:
2900 : return false;
2901 :
2902 : case GCC_JIT_TYPE_CHAR:
2903 : case GCC_JIT_TYPE_SIGNED_CHAR:
2904 : case GCC_JIT_TYPE_UNSIGNED_CHAR:
2905 : case GCC_JIT_TYPE_SHORT:
2906 : case GCC_JIT_TYPE_UNSIGNED_SHORT:
2907 : case GCC_JIT_TYPE_INT:
2908 : case GCC_JIT_TYPE_UNSIGNED_INT:
2909 : case GCC_JIT_TYPE_LONG:
2910 : case GCC_JIT_TYPE_UNSIGNED_LONG:
2911 : case GCC_JIT_TYPE_LONG_LONG:
2912 : case GCC_JIT_TYPE_UNSIGNED_LONG_LONG:
2913 : case GCC_JIT_TYPE_UINT8_T:
2914 : case GCC_JIT_TYPE_UINT16_T:
2915 : case GCC_JIT_TYPE_UINT32_T:
2916 : case GCC_JIT_TYPE_UINT64_T:
2917 : case GCC_JIT_TYPE_UINT128_T:
2918 : case GCC_JIT_TYPE_INT8_T:
2919 : case GCC_JIT_TYPE_INT16_T:
2920 : case GCC_JIT_TYPE_INT32_T:
2921 : case GCC_JIT_TYPE_INT64_T:
2922 : case GCC_JIT_TYPE_INT128_T:
2923 : return false;
2924 :
2925 : case GCC_JIT_TYPE_FLOAT:
2926 : case GCC_JIT_TYPE_BFLOAT16:
2927 : case GCC_JIT_TYPE_DOUBLE:
2928 : case GCC_JIT_TYPE_LONG_DOUBLE:
2929 : case GCC_JIT_TYPE_FLOAT16:
2930 : case GCC_JIT_TYPE_FLOAT32:
2931 : case GCC_JIT_TYPE_FLOAT64:
2932 : case GCC_JIT_TYPE_FLOAT128:
2933 : return true;
2934 :
2935 : case GCC_JIT_TYPE_CONST_CHAR_PTR:
2936 : return false;
2937 :
2938 : case GCC_JIT_TYPE_SIZE_T:
2939 : return false;
2940 :
2941 : case GCC_JIT_TYPE_FILE_PTR:
2942 : return false;
2943 :
2944 : case GCC_JIT_TYPE_COMPLEX_FLOAT:
2945 : case GCC_JIT_TYPE_COMPLEX_DOUBLE:
2946 : case GCC_JIT_TYPE_COMPLEX_LONG_DOUBLE:
2947 : return true;
2948 : }
2949 : }
2950 :
2951 : /* Implementation of pure virtual hook recording::type::is_bool for
2952 : recording::memento_of_get_type. */
2953 :
2954 : bool
2955 1240 : recording::memento_of_get_type::is_bool () const
2956 : {
2957 1240 : switch (m_kind)
2958 : {
2959 0 : default: gcc_unreachable ();
2960 :
2961 : case GCC_JIT_TYPE_VOID:
2962 : return false;
2963 :
2964 : case GCC_JIT_TYPE_VOID_PTR:
2965 : return false;
2966 :
2967 303 : case GCC_JIT_TYPE_BOOL:
2968 303 : return true;
2969 :
2970 : case GCC_JIT_TYPE_CHAR:
2971 : case GCC_JIT_TYPE_SIGNED_CHAR:
2972 : case GCC_JIT_TYPE_UNSIGNED_CHAR:
2973 : case GCC_JIT_TYPE_SHORT:
2974 : case GCC_JIT_TYPE_UNSIGNED_SHORT:
2975 : case GCC_JIT_TYPE_INT:
2976 : case GCC_JIT_TYPE_UNSIGNED_INT:
2977 : case GCC_JIT_TYPE_LONG:
2978 : case GCC_JIT_TYPE_UNSIGNED_LONG:
2979 : case GCC_JIT_TYPE_LONG_LONG:
2980 : case GCC_JIT_TYPE_UNSIGNED_LONG_LONG:
2981 : case GCC_JIT_TYPE_UINT8_T:
2982 : case GCC_JIT_TYPE_UINT16_T:
2983 : case GCC_JIT_TYPE_UINT32_T:
2984 : case GCC_JIT_TYPE_UINT64_T:
2985 : case GCC_JIT_TYPE_UINT128_T:
2986 : case GCC_JIT_TYPE_INT8_T:
2987 : case GCC_JIT_TYPE_INT16_T:
2988 : case GCC_JIT_TYPE_INT32_T:
2989 : case GCC_JIT_TYPE_INT64_T:
2990 : case GCC_JIT_TYPE_INT128_T:
2991 : return false;
2992 :
2993 : case GCC_JIT_TYPE_FLOAT:
2994 : case GCC_JIT_TYPE_BFLOAT16:
2995 : case GCC_JIT_TYPE_DOUBLE:
2996 : case GCC_JIT_TYPE_LONG_DOUBLE:
2997 : case GCC_JIT_TYPE_FLOAT16:
2998 : case GCC_JIT_TYPE_FLOAT32:
2999 : case GCC_JIT_TYPE_FLOAT64:
3000 : case GCC_JIT_TYPE_FLOAT128:
3001 : return false;
3002 :
3003 : case GCC_JIT_TYPE_CONST_CHAR_PTR:
3004 : return false;
3005 :
3006 : case GCC_JIT_TYPE_SIZE_T:
3007 : return false;
3008 :
3009 : case GCC_JIT_TYPE_FILE_PTR:
3010 : return false;
3011 :
3012 : case GCC_JIT_TYPE_COMPLEX_FLOAT:
3013 : case GCC_JIT_TYPE_COMPLEX_DOUBLE:
3014 : case GCC_JIT_TYPE_COMPLEX_LONG_DOUBLE:
3015 : return false;
3016 : }
3017 : }
3018 :
3019 : /* Implementation of pure virtual hook recording::memento::replay_into
3020 : for recording::memento_of_get_type. */
3021 :
3022 : void
3023 8759 : recording::memento_of_get_type::replay_into (replayer *r)
3024 : {
3025 8759 : set_playback_obj (r->get_type (m_kind));
3026 8759 : }
3027 :
3028 : /* The implementation of class gcc::jit::recording::memento_of_get_type. */
3029 :
3030 : /* Descriptive strings for each of enum gcc_jit_types. */
3031 :
3032 : static const char * const get_type_strings[] = {
3033 : "void", /* GCC_JIT_TYPE_VOID */
3034 : "void *", /* GCC_JIT_TYPE_VOID_PTR */
3035 :
3036 : "bool", /* GCC_JIT_TYPE_BOOL */
3037 :
3038 : "char", /* GCC_JIT_TYPE_CHAR */
3039 : "signed char", /* GCC_JIT_TYPE_SIGNED_CHAR */
3040 : "unsigned char", /* GCC_JIT_TYPE_UNSIGNED_CHAR */
3041 :
3042 : "short", /* GCC_JIT_TYPE_SHORT */
3043 : "unsigned short", /* GCC_JIT_TYPE_UNSIGNED_SHORT */
3044 :
3045 : "int", /* GCC_JIT_TYPE_INT */
3046 : "unsigned int", /* GCC_JIT_TYPE_UNSIGNED_INT */
3047 :
3048 : "long", /* GCC_JIT_TYPE_LONG */
3049 : "unsigned long", /* GCC_JIT_TYPE_UNSIGNED_LONG, */
3050 :
3051 : "long long", /* GCC_JIT_TYPE_LONG_LONG */
3052 : "unsigned long long", /* GCC_JIT_TYPE_UNSIGNED_LONG_LONG */
3053 :
3054 : "float", /* GCC_JIT_TYPE_FLOAT */
3055 : "double", /* GCC_JIT_TYPE_DOUBLE */
3056 : "long double", /* GCC_JIT_TYPE_LONG_DOUBLE */
3057 :
3058 : "const char *", /* GCC_JIT_TYPE_CONST_CHAR_PTR */
3059 :
3060 : "size_t", /* GCC_JIT_TYPE_SIZE_T */
3061 :
3062 : "FILE *", /* GCC_JIT_TYPE_FILE_PTR */
3063 :
3064 : "complex float", /* GCC_JIT_TYPE_COMPLEX_FLOAT */
3065 : "complex double", /* GCC_JIT_TYPE_COMPLEX_DOUBLE */
3066 : "complex long double", /* GCC_JIT_TYPE_COMPLEX_LONG_DOUBLE */
3067 :
3068 : "__uint8_t", /* GCC_JIT_TYPE_UINT8_T */
3069 : "__uint16_t", /* GCC_JIT_TYPE_UINT16_T */
3070 : "__uint32_t", /* GCC_JIT_TYPE_UINT32_T */
3071 : "__uint64_t", /* GCC_JIT_TYPE_UINT64_T */
3072 : "__uint128_t", /* GCC_JIT_TYPE_UINT128_T */
3073 : "__int8_t", /* GCC_JIT_TYPE_INT8_T */
3074 : "__int16_t", /* GCC_JIT_TYPE_INT16_T */
3075 : "__int32_t", /* GCC_JIT_TYPE_INT32_T */
3076 : "__int64_t", /* GCC_JIT_TYPE_INT64_T */
3077 : "__int128_t", /* GCC_JIT_TYPE_INT128_T */
3078 : "bfloat16", /* GCC_JIT_TYPE_BFLOAT16 */
3079 : "_Float16", /* GCC_JIT_TYPE_FLOAT16 */
3080 : "_Float32", /* GCC_JIT_TYPE_FLOAT32 */
3081 : "_Float64", /* GCC_JIT_TYPE_FLOAT64 */
3082 : "__float128", /* GCC_JIT_TYPE_FLOAT128 */
3083 : };
3084 :
3085 : /* Implementation of recording::memento::make_debug_string for
3086 : results of get_type, using a simple table of type names. */
3087 :
3088 : recording::string *
3089 5896 : recording::memento_of_get_type::make_debug_string ()
3090 : {
3091 5896 : return m_ctxt->new_string (get_type_strings[m_kind]);
3092 : }
3093 :
3094 : static const char * const get_type_enum_strings[] = {
3095 : "GCC_JIT_TYPE_VOID",
3096 : "GCC_JIT_TYPE_VOID_PTR",
3097 : "GCC_JIT_TYPE_BOOL",
3098 : "GCC_JIT_TYPE_CHAR",
3099 : "GCC_JIT_TYPE_SIGNED_CHAR",
3100 : "GCC_JIT_TYPE_UNSIGNED_CHAR",
3101 : "GCC_JIT_TYPE_SHORT",
3102 : "GCC_JIT_TYPE_UNSIGNED_SHORT",
3103 : "GCC_JIT_TYPE_INT",
3104 : "GCC_JIT_TYPE_UNSIGNED_INT",
3105 : "GCC_JIT_TYPE_LONG",
3106 : "GCC_JIT_TYPE_UNSIGNED_LONG",
3107 : "GCC_JIT_TYPE_LONG_LONG",
3108 : "GCC_JIT_TYPE_UNSIGNED_LONG_LONG",
3109 : "GCC_JIT_TYPE_FLOAT",
3110 : "GCC_JIT_TYPE_DOUBLE",
3111 : "GCC_JIT_TYPE_LONG_DOUBLE",
3112 : "GCC_JIT_TYPE_CONST_CHAR_PTR",
3113 : "GCC_JIT_TYPE_SIZE_T",
3114 : "GCC_JIT_TYPE_FILE_PTR",
3115 : "GCC_JIT_TYPE_COMPLEX_FLOAT",
3116 : "GCC_JIT_TYPE_COMPLEX_DOUBLE",
3117 : "GCC_JIT_TYPE_COMPLEX_LONG_DOUBLE",
3118 : "GCC_JIT_TYPE_UINT8_T",
3119 : "GCC_JIT_TYPE_UINT16_T",
3120 : "GCC_JIT_TYPE_UINT32_T",
3121 : "GCC_JIT_TYPE_UINT64_T",
3122 : "GCC_JIT_TYPE_UINT128_T",
3123 : "GCC_JIT_TYPE_INT8_T",
3124 : "GCC_JIT_TYPE_INT16_T",
3125 : "GCC_JIT_TYPE_INT32_T",
3126 : "GCC_JIT_TYPE_INT64_T",
3127 : "GCC_JIT_TYPE_INT128_T",
3128 : "GCC_JIT_TYPE_BFLOAT16",
3129 : "GCC_JIT_TYPE_FLOAT16",
3130 : "GCC_JIT_TYPE_FLOAT32",
3131 : "GCC_JIT_TYPE_FLOAT64",
3132 : "GCC_JIT_TYPE_FLOAT128",
3133 : };
3134 :
3135 : void
3136 5878 : recording::memento_of_get_type::write_reproducer (reproducer &r)
3137 : {
3138 5878 : const char *id = r.make_identifier (this, "type");
3139 5878 : r.write (" gcc_jit_type *%s = gcc_jit_context_get_type (%s, %s);\n",
3140 : id,
3141 : r.get_identifier (get_context ()),
3142 5878 : get_type_enum_strings[m_kind]);
3143 5878 : }
3144 :
3145 : /* The implementation of class gcc::jit::recording::memento_of_get_pointer. */
3146 :
3147 : /* Override of default implementation of
3148 : recording::type::get_size for get_pointer. */
3149 :
3150 : size_t
3151 10 : recording::memento_of_get_pointer::get_size ()
3152 : {
3153 10 : return POINTER_SIZE / BITS_PER_UNIT;
3154 : }
3155 :
3156 : /* Override of default implementation of
3157 : recording::type::accepts_writes_from for get_pointer.
3158 :
3159 : Require a pointer type, and allowing writes to
3160 : (const T *) from a (T*), but not the other way around. */
3161 :
3162 : bool
3163 567 : recording::memento_of_get_pointer::accepts_writes_from (type *rtype)
3164 : {
3165 : /* Must be a pointer type: */
3166 567 : type *rtype_points_to = rtype->is_pointer ();
3167 567 : if (!rtype_points_to)
3168 : return false;
3169 :
3170 : /* It's OK to assign to a (const T *) from a (T *). */
3171 502 : if (m_other_type->unqualified ()->accepts_writes_from (rtype_points_to))
3172 : {
3173 : return true;
3174 : }
3175 :
3176 : /* It's OK to assign to a (volatile const T *) from a (volatile const T *). */
3177 46 : return m_other_type->is_same_type_as (rtype_points_to);
3178 : }
3179 :
3180 : /* Implementation of pure virtual hook recording::memento::replay_into
3181 : for recording::memento_of_get_pointer. */
3182 :
3183 : void
3184 1606 : recording::memento_of_get_pointer::replay_into (replayer *)
3185 : {
3186 1606 : set_playback_obj (m_other_type->playback_type ()->get_pointer ());
3187 1606 : }
3188 :
3189 : /* Implementation of recording::memento::make_debug_string for
3190 : results of get_pointer, adding " *" to the underlying type,
3191 : with special-casing to handle function pointer types. */
3192 :
3193 : recording::string *
3194 1208 : recording::memento_of_get_pointer::make_debug_string ()
3195 : {
3196 : /* Special-case function pointer types, to put the "*" in parens between
3197 : the return type and the params (for one level of dereferencing, at
3198 : least). */
3199 1208 : if (function_type *fn_type = m_other_type->dyn_cast_function_type ())
3200 60 : return fn_type->make_debug_string_with_ptr ();
3201 :
3202 1148 : return string::from_printf (m_ctxt,
3203 2296 : "%s *", m_other_type->get_debug_string ());
3204 : }
3205 :
3206 : /* Implementation of recording::memento::write_reproducer for get_pointer. */
3207 :
3208 : void
3209 1205 : recording::memento_of_get_pointer::write_reproducer (reproducer &r)
3210 : {
3211 : /* We need to special-case function pointer types; see the notes in
3212 : recording::function_type::write_deferred_reproducer. */
3213 1205 : if (function_type *fn_type = m_other_type->dyn_cast_function_type ())
3214 : {
3215 55 : fn_type->write_deferred_reproducer (r, this);
3216 55 : return;
3217 : }
3218 :
3219 1150 : const char *id = r.make_identifier (this, "type");
3220 1150 : r.write (" gcc_jit_type *%s =\n"
3221 : " gcc_jit_type_get_pointer (%s);\n",
3222 : id,
3223 : r.get_identifier_as_type (m_other_type));
3224 : }
3225 :
3226 : /* The implementation of class gcc::jit::recording::memento_of_get_const. */
3227 :
3228 : /* Implementation of pure virtual hook recording::memento::replay_into
3229 : for recording::memento_of_get_const. */
3230 :
3231 : void
3232 715 : recording::memento_of_get_const::replay_into (replayer *)
3233 : {
3234 715 : set_playback_obj (m_other_type->playback_type ()->get_const ());
3235 715 : }
3236 :
3237 : /* Implementation of recording::memento::make_debug_string for
3238 : results of get_const, prepending "const ". */
3239 :
3240 : recording::string *
3241 661 : recording::memento_of_get_const::make_debug_string ()
3242 : {
3243 661 : return string::from_printf (m_ctxt,
3244 661 : "const %s", m_other_type->get_debug_string ());
3245 : }
3246 :
3247 : /* Implementation of recording::memento::write_reproducer for const types. */
3248 :
3249 : void
3250 661 : recording::memento_of_get_const::write_reproducer (reproducer &r)
3251 : {
3252 661 : const char *id = r.make_identifier (this, "type");
3253 661 : r.write (" gcc_jit_type *%s =\n"
3254 : " gcc_jit_type_get_const (%s);\n",
3255 : id,
3256 : r.get_identifier_as_type (m_other_type));
3257 661 : }
3258 :
3259 : /* The implementation of class gcc::jit::recording::memento_of_get_volatile. */
3260 :
3261 : /* Implementation of pure virtual hook recording::memento::replay_into
3262 : for recording::memento_of_get_volatile. */
3263 :
3264 : void
3265 150 : recording::memento_of_get_volatile::replay_into (replayer *)
3266 : {
3267 150 : set_playback_obj (m_other_type->playback_type ()->get_volatile ());
3268 150 : }
3269 :
3270 : /* Implementation of recording::memento::make_debug_string for
3271 : results of get_volatile, prepending "volatile ". */
3272 :
3273 : recording::string *
3274 236 : recording::memento_of_get_volatile::make_debug_string ()
3275 : {
3276 236 : return string::from_printf (m_ctxt,
3277 236 : "volatile %s", m_other_type->get_debug_string ());
3278 : }
3279 :
3280 : /* Implementation of recording::memento::write_reproducer for volatile
3281 : types. */
3282 :
3283 : void
3284 236 : recording::memento_of_get_volatile::write_reproducer (reproducer &r)
3285 : {
3286 236 : const char *id = r.make_identifier (this, "type");
3287 236 : r.write (" gcc_jit_type *%s =\n"
3288 : " gcc_jit_type_get_volatile (%s);\n",
3289 : id,
3290 : r.get_identifier_as_type (m_other_type));
3291 236 : }
3292 :
3293 : /* The implementation of class gcc::jit::recording::memento_of_get_restrict. */
3294 :
3295 : /* Implementation of pure virtual hook recording::memento::replay_into
3296 : for recording::memento_of_get_restrict. */
3297 :
3298 : void
3299 10 : recording::memento_of_get_restrict::replay_into (replayer *)
3300 : {
3301 10 : set_playback_obj (m_other_type->playback_type ()->get_restrict ());
3302 10 : }
3303 :
3304 : /* Implementation of recording::memento::make_debug_string for
3305 : results of get_restrict, prepending "restrict ". */
3306 :
3307 : recording::string *
3308 10 : recording::memento_of_get_restrict::make_debug_string ()
3309 : {
3310 10 : return string::from_printf (m_ctxt,
3311 10 : "restrict %s", m_other_type->get_debug_string ());
3312 : }
3313 :
3314 : /* Implementation of recording::memento::write_reproducer for restrict
3315 : types. */
3316 :
3317 : void
3318 10 : recording::memento_of_get_restrict::write_reproducer (reproducer &r)
3319 : {
3320 10 : const char *id = r.make_identifier (this, "type");
3321 10 : r.write (" gcc_jit_type *%s =\n"
3322 : " gcc_jit_type_get_restrict (%s);\n",
3323 : id,
3324 : r.get_identifier_as_type (m_other_type));
3325 10 : }
3326 :
3327 : /* The implementation of class gcc::jit::recording::memento_of_get_aligned. */
3328 :
3329 : /* Implementation of pure virtual hook recording::memento::replay_into
3330 : for recording::memento_of_get_aligned. */
3331 :
3332 : void
3333 240 : recording::memento_of_get_aligned::replay_into (replayer *)
3334 : {
3335 240 : set_playback_obj
3336 240 : (m_other_type->playback_type ()->get_aligned (m_alignment_in_bytes));
3337 240 : }
3338 :
3339 : /* Implementation of recording::memento::make_debug_string for
3340 : results of get_aligned. */
3341 :
3342 : recording::string *
3343 319 : recording::memento_of_get_aligned::make_debug_string ()
3344 : {
3345 319 : return string::from_printf (m_ctxt,
3346 : "%s __attribute__((aligned(%zi)))",
3347 319 : m_other_type->get_debug_string (),
3348 319 : m_alignment_in_bytes);
3349 : }
3350 :
3351 : /* Implementation of recording::memento::write_reproducer for aligned
3352 : types. */
3353 :
3354 : void
3355 319 : recording::memento_of_get_aligned::write_reproducer (reproducer &r)
3356 : {
3357 319 : const char *id = r.make_identifier (this, "type");
3358 319 : r.write (" gcc_jit_type *%s =\n"
3359 : " gcc_jit_type_get_aligned (%s, %zi);\n",
3360 : id,
3361 : r.get_identifier_as_type (m_other_type),
3362 : m_alignment_in_bytes);
3363 319 : }
3364 :
3365 : /* The implementation of class gcc::jit::recording::vector_type. */
3366 :
3367 : /* Implementation of pure virtual hook recording::memento::replay_into
3368 : for recording::vector_type. */
3369 :
3370 : void
3371 525 : recording::vector_type::replay_into (replayer *)
3372 : {
3373 525 : set_playback_obj
3374 525 : (m_other_type->playback_type ()->get_vector (m_num_units));
3375 525 : }
3376 :
3377 : /* Implementation of recording::memento::make_debug_string for
3378 : results of get_vector. */
3379 :
3380 : recording::string *
3381 461 : recording::vector_type::make_debug_string ()
3382 : {
3383 461 : return string::from_printf
3384 461 : (m_ctxt,
3385 : "%s __attribute__((vector_size(sizeof (%s) * %zi)))",
3386 461 : m_other_type->get_debug_string (),
3387 461 : m_other_type->get_debug_string (),
3388 461 : m_num_units);
3389 : }
3390 :
3391 : /* Implementation of recording::memento::write_reproducer for vector types. */
3392 :
3393 : void
3394 461 : recording::vector_type::write_reproducer (reproducer &r)
3395 : {
3396 461 : const char *id = r.make_identifier (this, "type");
3397 461 : r.write (" gcc_jit_type *%s =\n"
3398 : " gcc_jit_type_get_vector (%s, %zi);\n",
3399 : id,
3400 : r.get_identifier_as_type (m_other_type),
3401 : m_num_units);
3402 461 : }
3403 :
3404 : /* The implementation of class gcc::jit::recording::array_type */
3405 :
3406 : /* Implementation of pure virtual hook recording::type::dereference for
3407 : recording::array_type. */
3408 :
3409 : recording::type *
3410 808 : recording::array_type::dereference ()
3411 : {
3412 808 : return m_element_type;
3413 : }
3414 :
3415 : /* Implementation of pure virtual hook recording::memento::replay_into
3416 : for recording::array_type. */
3417 :
3418 : void
3419 445 : recording::array_type::replay_into (replayer *r)
3420 : {
3421 445 : set_playback_obj (r->new_array_type (playback_location (r, m_loc),
3422 445 : m_element_type->playback_type (),
3423 : m_num_elements));
3424 445 : }
3425 :
3426 : /* Implementation of recording::memento::make_debug_string for
3427 : results of new_array_type. */
3428 :
3429 : recording::string *
3430 260 : recording::array_type::make_debug_string ()
3431 : {
3432 260 : return string::from_printf (m_ctxt,
3433 : "%s[%" PRIu64 "]",
3434 260 : m_element_type->get_debug_string (),
3435 260 : m_num_elements);
3436 : }
3437 :
3438 : /* Implementation of recording::memento::write_reproducer for array
3439 : types. */
3440 :
3441 : void
3442 260 : recording::array_type::write_reproducer (reproducer &r)
3443 : {
3444 260 : const char *id = r.make_identifier (this, "array_type");
3445 520 : r.write (" gcc_jit_type *%s =\n"
3446 : " gcc_jit_context_new_array_type_u64 (%s,\n"
3447 : " %s, /* gcc_jit_location *loc */\n"
3448 : " %s, /* gcc_jit_type *element_type */\n"
3449 : " %" PRIu64 "); /* int num_elements */\n",
3450 : id,
3451 : r.get_identifier (get_context ()),
3452 260 : r.get_identifier (m_loc),
3453 : r.get_identifier_as_type (m_element_type),
3454 : m_num_elements);
3455 260 : }
3456 :
3457 : /* The implementation of class gcc::jit::recording::function_type */
3458 :
3459 : /* Constructor for gcc::jit::recording::function_type. */
3460 :
3461 506893 : recording::function_type::function_type (context *ctxt,
3462 : type *return_type,
3463 : int num_params,
3464 : type **param_types,
3465 : int is_variadic,
3466 : bool is_target_builtin)
3467 : : type (ctxt),
3468 506893 : m_return_type (return_type),
3469 506893 : m_param_types (),
3470 506893 : m_is_variadic (is_variadic),
3471 506893 : m_is_target_builtin (is_target_builtin)
3472 : {
3473 2029907 : for (int i = 0; i< num_params; i++)
3474 1523014 : m_param_types.safe_push (param_types[i]);
3475 506893 : }
3476 :
3477 : /* Implementation of pure virtual hook recording::type::dereference for
3478 : recording::function_type. */
3479 :
3480 : recording::type *
3481 0 : recording::function_type::dereference ()
3482 : {
3483 0 : return NULL;
3484 : }
3485 :
3486 : /* Implementation of virtual hook recording::type::is_same_type_as for
3487 : recording::function_type.
3488 :
3489 : We override this to avoid requiring identity of function pointer types,
3490 : so that if client code has obtained the same signature in
3491 : different ways (e.g. via gcc_jit_context_new_function_ptr_type
3492 : vs gcc_jit_function_get_address), the different function_type
3493 : instances are treated as compatible.
3494 :
3495 : We can't use type::accepts_writes_from for this as we need a stronger
3496 : notion of "sameness": if we have a fn_ptr type that has args that are
3497 : themselves fn_ptr types, then those args still need to match exactly.
3498 :
3499 : Alternatively, we could consolidate attempts to create identical
3500 : function_type instances so that pointer equality works, but that runs
3501 : into issues about the lifetimes of the cache (w.r.t. nested contexts). */
3502 :
3503 : bool
3504 45 : recording::function_type::is_same_type_as (type *other)
3505 : {
3506 45 : gcc_assert (other);
3507 :
3508 45 : function_type *other_fn_type = other->dyn_cast_function_type ();
3509 45 : if (!other_fn_type)
3510 : return false;
3511 :
3512 : /* Everything must match. */
3513 :
3514 45 : if (!m_return_type->is_same_type_as (other_fn_type->m_return_type))
3515 : return false;
3516 :
3517 135 : if (m_param_types.length () != other_fn_type->m_param_types.length ())
3518 : return false;
3519 :
3520 : unsigned i;
3521 : type *param_type;
3522 100 : FOR_EACH_VEC_ELT (m_param_types, i, param_type)
3523 65 : if (!param_type->is_same_type_as (other_fn_type->m_param_types[i]))
3524 : return false;
3525 :
3526 35 : if (m_is_variadic != other_fn_type->m_is_variadic)
3527 0 : return false;
3528 :
3529 : /* Passed all tests. */
3530 : return true;
3531 : }
3532 :
3533 : /* Implementation of pure virtual hook recording::memento::replay_into
3534 : for recording::function_type. */
3535 :
3536 : void
3537 5674 : recording::function_type::replay_into (replayer *r)
3538 : {
3539 : /* Convert m_param_types to a vec of playback type. */
3540 5674 : auto_vec <playback::type *> param_types;
3541 5674 : int i;
3542 5674 : recording::type *type;
3543 5674 : param_types.create (m_param_types.length ());
3544 16001 : FOR_EACH_VEC_ELT (m_param_types, i, type)
3545 4653 : param_types.safe_push (type->playback_type ());
3546 :
3547 5674 : set_playback_obj (r->new_function_type (m_return_type->playback_type (),
3548 : ¶m_types,
3549 : m_is_variadic));
3550 5674 : }
3551 :
3552 : /* Special-casing for make_debug_string for get_pointer results for
3553 : handling (one level) of pointers to functions. */
3554 :
3555 : recording::string *
3556 60 : recording::function_type::make_debug_string_with_ptr ()
3557 : {
3558 60 : return make_debug_string_with ("(*) ");
3559 : }
3560 :
3561 : /* Implementation of recording::memento::make_debug_string for
3562 : results of new_function_type. */
3563 :
3564 : recording::string *
3565 55 : recording::function_type::make_debug_string ()
3566 : {
3567 55 : return make_debug_string_with ("");
3568 : }
3569 :
3570 : /* Build a debug string representation of the form:
3571 :
3572 : RESULT_TYPE INSERT (PARAM_TYPES)
3573 :
3574 : for use when handling 0 and 1 level of indirection to this
3575 : function type. */
3576 :
3577 : recording::string *
3578 115 : recording::function_type::make_debug_string_with (const char *insert)
3579 : {
3580 : /* First, build a buffer for the arguments. */
3581 : /* Calculate length of said buffer. */
3582 115 : size_t sz = 1; /* nil terminator */
3583 310 : for (unsigned i = 0; i< m_param_types.length (); i++)
3584 : {
3585 195 : sz += strlen (m_param_types[i]->get_debug_string ());
3586 195 : sz += 2; /* ", " separator */
3587 : }
3588 115 : if (m_is_variadic)
3589 0 : sz += 5; /* ", ..." separator and ellipsis */
3590 :
3591 : /* Now allocate and populate the buffer. */
3592 115 : char *argbuf = new char[sz];
3593 115 : size_t len = 0;
3594 :
3595 425 : for (unsigned i = 0; i< m_param_types.length (); i++)
3596 : {
3597 195 : strcpy (argbuf + len, m_param_types[i]->get_debug_string ());
3598 195 : len += strlen (m_param_types[i]->get_debug_string ());
3599 285 : if (i + 1 < m_param_types.length ())
3600 : {
3601 90 : strcpy (argbuf + len, ", ");
3602 90 : len += 2;
3603 : }
3604 : }
3605 115 : if (m_is_variadic)
3606 : {
3607 0 : if (m_param_types.length ())
3608 : {
3609 0 : strcpy (argbuf + len, ", ");
3610 0 : len += 2;
3611 : }
3612 0 : strcpy (argbuf + len, "...");
3613 0 : len += 3;
3614 : }
3615 115 : argbuf[len] = '\0';
3616 :
3617 : /* ...and use it to get the string for the call as a whole. */
3618 115 : string *result = string::from_printf (m_ctxt,
3619 : "%s %s(%s)",
3620 115 : m_return_type->get_debug_string (),
3621 : insert,
3622 : argbuf);
3623 :
3624 115 : delete[] argbuf;
3625 :
3626 115 : return result;
3627 : }
3628 :
3629 : /* Implementation of recording::memento::write_reproducer for function
3630 : types. */
3631 :
3632 : void
3633 207 : recording::function_type::write_reproducer (reproducer &)
3634 : {
3635 : /* see notes below. */
3636 207 : }
3637 :
3638 : /* There's a get_pointer within context::new_function_ptr_type:
3639 : the type received by client code isn't the memento for the
3640 : function_type, but instead the result of get_pointer on it.
3641 :
3642 : Hence we can't directly write a reproducer that gives function_type.
3643 : Instead we special-case things within get_pointer, detecting this
3644 : case, calling the following function. */
3645 :
3646 : void
3647 55 : recording::function_type::write_deferred_reproducer (reproducer &r,
3648 : memento *ptr_type)
3649 : {
3650 55 : gcc_assert (ptr_type);
3651 55 : r.make_identifier (this, "function_type");
3652 55 : const char *ptr_id = r.make_identifier (ptr_type, "ptr_to");
3653 55 : const char *param_types_id = r.make_tmp_identifier ("params_for", this);
3654 55 : r.write (" gcc_jit_type *%s[%i] = {\n",
3655 : param_types_id,
3656 : m_param_types.length ());
3657 55 : int i;
3658 55 : type *param_type;
3659 200 : FOR_EACH_VEC_ELT (m_param_types, i, param_type)
3660 90 : r.write (" %s,\n", r.get_identifier_as_type (param_type));
3661 55 : r.write (" };\n");
3662 105 : r.write (" gcc_jit_type *%s =\n"
3663 : " gcc_jit_context_new_function_ptr_type (%s, /* gcc_jit_context *ctxt */\n"
3664 : " %s, /* gcc_jit_location *loc */\n"
3665 : " %s, /* gcc_jit_type *return_type */\n"
3666 : " %i, /* int num_params */\n"
3667 : " %s, /* gcc_jit_type **param_types */\n"
3668 : " %i); /* int is_variadic */\n",
3669 : ptr_id,
3670 : r.get_identifier (get_context ()),
3671 : "NULL", /* location is not stored */
3672 : r.get_identifier_as_type (m_return_type),
3673 : m_param_types.length (),
3674 : param_types_id,
3675 : m_is_variadic);
3676 55 : }
3677 :
3678 : /* The implementation of class gcc::jit::recording::field. */
3679 :
3680 : /* Implementation of pure virtual hook recording::memento::replay_into
3681 : for recording::field. */
3682 :
3683 : void
3684 2248 : recording::field::replay_into (replayer *r)
3685 : {
3686 2248 : set_playback_obj (r->new_field (playback_location (r, m_loc),
3687 2248 : m_type->playback_type (),
3688 : playback_string (m_name)));
3689 2248 : }
3690 :
3691 : /* Override the default implementation of
3692 : recording::memento::write_to_dump. Dump each field
3693 : by dumping a line of the form:
3694 : TYPE NAME;
3695 : so that we can build up a struct/union field by field. */
3696 :
3697 : void
3698 8 : recording::field::write_to_dump (dump &d)
3699 : {
3700 8 : d.write (" %s %s;\n",
3701 8 : m_type->get_debug_string (),
3702 8 : m_name->c_str ());
3703 8 : }
3704 :
3705 : /* Implementation of recording::memento::make_debug_string for
3706 : results of new_field. */
3707 :
3708 : recording::string *
3709 4000 : recording::field::make_debug_string ()
3710 : {
3711 4000 : return m_name;
3712 : }
3713 :
3714 : /* Implementation of recording::memento::write_reproducer for fields. */
3715 :
3716 : void
3717 3999 : recording::field::write_reproducer (reproducer &r)
3718 : {
3719 3999 : const char *id = r.make_identifier (this, "field");
3720 7998 : r.write(" gcc_jit_field *%s =\n"
3721 : " gcc_jit_context_new_field (%s,\n"
3722 : " %s, /* gcc_jit_location *loc */\n"
3723 : " %s, /* gcc_jit_type *type, */\n"
3724 : " %s); /* const char *name */\n",
3725 : id,
3726 : r.get_identifier (get_context ()),
3727 3999 : r.get_identifier (m_loc),
3728 : r.get_identifier_as_type (m_type),
3729 3999 : m_name->get_debug_string ());
3730 3999 : }
3731 :
3732 : /* The implementation of class gcc::jit::recording::bitfield. */
3733 :
3734 : /* Implementation of pure virtual hook recording::memento::replay_into
3735 : for recording::bitfield. */
3736 :
3737 : void
3738 120 : recording::bitfield::replay_into (replayer *r)
3739 : {
3740 240 : set_playback_obj (r->new_bitfield (playback_location (r, m_loc),
3741 120 : m_type->playback_type (),
3742 : m_width,
3743 : playback_string (m_name)));
3744 120 : }
3745 :
3746 : /* Override the default implementation of
3747 : recording::memento::write_to_dump. Dump each bit field
3748 : by dumping a line of the form:
3749 : TYPE NAME:WIDTH;
3750 : so that we can build up a struct/union field by field. */
3751 :
3752 : void
3753 0 : recording::bitfield::write_to_dump (dump &d)
3754 : {
3755 0 : d.write (" %s %s:%d;\n",
3756 0 : m_type->get_debug_string (),
3757 0 : m_name->c_str (),
3758 : m_width);
3759 0 : }
3760 :
3761 : /* Implementation of recording::memento::make_debug_string for
3762 : results of new_bitfield. */
3763 :
3764 : recording::string *
3765 85 : recording::bitfield::make_debug_string ()
3766 : {
3767 85 : return string::from_printf (m_ctxt,
3768 : "%s:%d",
3769 85 : m_name->c_str (), m_width);
3770 : }
3771 :
3772 : /* Implementation of recording::memento::write_reproducer for bitfields. */
3773 :
3774 : void
3775 85 : recording::bitfield::write_reproducer (reproducer &r)
3776 : {
3777 85 : const char *id = r.make_identifier (this, "bitfield");
3778 170 : r.write (" gcc_jit_field *%s =\n"
3779 : " gcc_jit_context_new_bitfield (%s,\n"
3780 : " %s, /* gcc_jit_location *loc */\n"
3781 : " %s, /* gcc_jit_type *type, */\n"
3782 : " %d, /* int width, */\n"
3783 : " %s); /* const char *name */\n",
3784 : id,
3785 : r.get_identifier (get_context ()),
3786 85 : r.get_identifier (m_loc),
3787 : r.get_identifier_as_type (m_type),
3788 : m_width,
3789 85 : m_name->get_debug_string ());
3790 85 : }
3791 :
3792 : /* The implementation of class gcc::jit::recording::compound_type */
3793 :
3794 : /* The constructor for gcc::jit::recording::compound_type. */
3795 :
3796 1086 : recording::compound_type::compound_type (context *ctxt,
3797 : location *loc,
3798 : string *name)
3799 : : type (ctxt),
3800 1086 : m_loc (loc),
3801 1086 : m_name (name),
3802 1086 : m_fields (NULL)
3803 : {
3804 1086 : }
3805 :
3806 : /* Set the fields of a compound type.
3807 :
3808 : Implements the post-error-checking part of
3809 : gcc_jit_struct_set_fields, and is also used by
3810 : gcc_jit_context_new_union_type. */
3811 :
3812 : void
3813 1037 : recording::compound_type::set_fields (location *loc,
3814 : int num_fields,
3815 : field **field_array)
3816 : {
3817 1037 : m_loc = loc;
3818 1037 : gcc_assert (m_fields == NULL);
3819 :
3820 1037 : m_fields = new fields (this, num_fields, field_array);
3821 1037 : m_ctxt->record (m_fields);
3822 1037 : }
3823 :
3824 : /* Implementation of pure virtual hook recording::type::dereference for
3825 : recording::compound_type. */
3826 :
3827 : recording::type *
3828 0 : recording::compound_type::dereference ()
3829 : {
3830 0 : return NULL; /* not a pointer */
3831 : }
3832 :
3833 : /* The implementation of class gcc::jit::recording::struct_. */
3834 :
3835 : /* The constructor for gcc::jit::recording::struct_. */
3836 :
3837 1001 : recording::struct_::struct_ (context *ctxt,
3838 : location *loc,
3839 : string *name)
3840 1001 : : compound_type (ctxt, loc, name)
3841 : {
3842 1001 : }
3843 :
3844 : /* Implementation of pure virtual hook recording::memento::replay_into
3845 : for recording::struct_. */
3846 :
3847 : void
3848 677 : recording::struct_::replay_into (replayer *r)
3849 : {
3850 1354 : set_playback_obj (
3851 677 : r->new_compound_type (playback_location (r, get_loc ()),
3852 : get_name ()->c_str (),
3853 : true /* is_struct */));
3854 677 : }
3855 :
3856 : const char *
3857 1728 : recording::struct_::access_as_type (reproducer &r)
3858 : {
3859 1728 : return r.xstrdup_printf ("gcc_jit_struct_as_type (%s)",
3860 1728 : r.get_identifier (this));
3861 : }
3862 :
3863 : /* Implementation of recording::memento::make_debug_string for
3864 : structs. */
3865 :
3866 : recording::string *
3867 841 : recording::struct_::make_debug_string ()
3868 : {
3869 841 : return string::from_printf (m_ctxt,
3870 841 : "struct %s", get_name ()->c_str ());
3871 : }
3872 :
3873 : void
3874 842 : recording::struct_::write_reproducer (reproducer &r)
3875 : {
3876 842 : const char *id = r.make_identifier (this, "struct");
3877 842 : r.write (" gcc_jit_struct *%s =\n"
3878 : " gcc_jit_context_new_opaque_struct (%s,\n"
3879 : " %s, /* gcc_jit_location *loc */\n"
3880 : " %s); /* const char *name */\n",
3881 : id,
3882 : r.get_identifier (get_context ()),
3883 842 : r.get_identifier (get_loc ()),
3884 842 : get_name ()->get_debug_string ());
3885 842 : }
3886 :
3887 : /* The implementation of class gcc::jit::recording::union_. */
3888 :
3889 : /* The constructor for gcc::jit::recording::union_. */
3890 :
3891 85 : recording::union_::union_ (context *ctxt,
3892 : location *loc,
3893 : string *name)
3894 85 : : compound_type (ctxt, loc, name)
3895 : {
3896 85 : }
3897 :
3898 : /* Implementation of pure virtual hook recording::memento::replay_into
3899 : for recording::union_. */
3900 :
3901 : void
3902 100 : recording::union_::replay_into (replayer *r)
3903 : {
3904 200 : set_playback_obj (
3905 100 : r->new_compound_type (playback_location (r, get_loc ()),
3906 : get_name ()->c_str (),
3907 : false /* is_struct */));
3908 100 : }
3909 :
3910 : /* Implementation of recording::memento::make_debug_string for
3911 : unions. */
3912 :
3913 : recording::string *
3914 60 : recording::union_::make_debug_string ()
3915 : {
3916 60 : return string::from_printf (m_ctxt,
3917 60 : "union %s", get_name ()->c_str ());
3918 : }
3919 :
3920 : /* Implementation of recording::memento::write_reproducer for unions. */
3921 :
3922 : void
3923 60 : recording::union_::write_reproducer (reproducer &r)
3924 : {
3925 60 : const char *id = r.make_identifier (this, "union");
3926 :
3927 60 : const char *fields_id = r.make_tmp_identifier ("fields_for", this);
3928 60 : r.write (" gcc_jit_field *%s[%i] = {\n",
3929 : fields_id,
3930 : get_fields ()->length ());
3931 450 : for (int i = 0; i < get_fields ()->length (); i++)
3932 135 : r.write (" %s,\n", r.get_identifier (get_fields ()->get_field (i)));
3933 60 : r.write (" };\n");
3934 :
3935 120 : r.write (" gcc_jit_type *%s =\n"
3936 : " gcc_jit_context_new_union_type (%s,\n"
3937 : " %s, /* gcc_jit_location *loc */\n"
3938 : " %s, /* const char *name */\n"
3939 : " %i, /* int num_fields */\n"
3940 : " %s); /* gcc_jit_field **fields */\n",
3941 : id,
3942 : r.get_identifier (get_context ()),
3943 60 : r.get_identifier (get_loc ()),
3944 60 : get_name ()->get_debug_string (),
3945 : get_fields ()->length (),
3946 : fields_id);
3947 60 : }
3948 :
3949 : /* The implementation of class gcc::jit::recording::fields. */
3950 :
3951 : /* The constructor for gcc::jit::recording::fields. */
3952 :
3953 1037 : recording::fields::fields (compound_type *struct_or_union,
3954 : int num_fields,
3955 : field **fields)
3956 : : memento (struct_or_union->m_ctxt),
3957 1037 : m_struct_or_union (struct_or_union),
3958 1037 : m_fields ()
3959 : {
3960 4696 : for (int i = 0; i < num_fields; i++)
3961 : {
3962 3659 : gcc_assert (fields[i]->get_container () == NULL);
3963 3659 : fields[i]->set_container (m_struct_or_union);
3964 3659 : m_fields.safe_push (fields[i]);
3965 : }
3966 1037 : }
3967 :
3968 : /* Implementation of pure virtual hook recording::memento::replay_into
3969 : for recording::fields. */
3970 :
3971 : void
3972 737 : recording::fields::replay_into (replayer *)
3973 : {
3974 737 : auto_vec<playback::field *> playback_fields;
3975 737 : playback_fields.create (m_fields.length ());
3976 3832 : for (unsigned i = 0; i < m_fields.length (); i++)
3977 2358 : playback_fields.safe_push (m_fields[i]->playback_field ());
3978 737 : m_struct_or_union->playback_compound_type ()->set_fields (&playback_fields);
3979 737 : }
3980 :
3981 : /* Override the default implementation of
3982 : recording::memento::write_to_dump by writing a union/struct
3983 : declaration of this form:
3984 :
3985 : struct/union NAME {
3986 : TYPE_1 NAME_1;
3987 : TYPE_2 NAME_2;
3988 : ....
3989 : TYPE_N NAME_N;
3990 : };
3991 :
3992 : to the dump. */
3993 :
3994 : void
3995 2 : recording::fields::write_to_dump (dump &d)
3996 : {
3997 2 : int i;
3998 2 : field *f;
3999 :
4000 2 : d.write ("%s\n{\n", m_struct_or_union->get_debug_string ());
4001 12 : FOR_EACH_VEC_ELT (m_fields, i, f)
4002 8 : f->write_to_dump (d);
4003 2 : d.write ("};\n");
4004 2 : }
4005 :
4006 : /* Implementation of recording::memento::write_reproducer for the fields
4007 : subclass. */
4008 :
4009 : void
4010 863 : recording::fields::write_reproducer (reproducer &r)
4011 : {
4012 863 : if (m_struct_or_union)
4013 863 : if (m_struct_or_union->dyn_cast_struct () == NULL)
4014 : /* We have a union; the fields have already been written by
4015 : union::write_reproducer. */
4016 863 : return;
4017 :
4018 803 : const char *fields_id = r.make_identifier (this, "fields");
4019 803 : r.write (" gcc_jit_field *%s[%i] = {\n",
4020 : fields_id,
4021 : m_fields.length ());
4022 803 : int i;
4023 803 : field *field;
4024 4584 : FOR_EACH_VEC_ELT (m_fields, i, field)
4025 2978 : r.write (" %s,\n", r.get_identifier (field));
4026 803 : r.write (" };\n");
4027 :
4028 2351 : r.write (" gcc_jit_struct_set_fields (%s, /* gcc_jit_struct *struct_type */\n"
4029 : " %s, /* gcc_jit_location *loc */\n"
4030 : " %i, /* int num_fields */\n"
4031 : " %s); /* gcc_jit_field **fields */\n",
4032 803 : r.get_identifier (m_struct_or_union),
4033 : r.get_identifier ((memento *)NULL),
4034 : m_fields.length (),
4035 : fields_id);
4036 : }
4037 :
4038 : /* Implementation of recording::memento::make_debug_string for
4039 : field tables. */
4040 :
4041 : recording::string *
4042 797 : recording::fields::make_debug_string ()
4043 : {
4044 797 : return string::from_printf (m_ctxt,
4045 797 : "fields");
4046 : }
4047 :
4048 : /* The implementation of class gcc::jit::recording::rvalue. */
4049 :
4050 : /* Create a recording::access_field_rvalue instance and add it to
4051 : the rvalue's context's list of mementos.
4052 :
4053 : Implements the post-error-checking part of
4054 : gcc_jit_rvalue_access_field. */
4055 :
4056 : recording::rvalue *
4057 139 : recording::rvalue::access_field (recording::location *loc,
4058 : field *field)
4059 : {
4060 139 : recording::rvalue *result =
4061 139 : new access_field_rvalue (m_ctxt, loc, this, field);
4062 139 : m_ctxt->record (result);
4063 139 : return result;
4064 : }
4065 :
4066 : /* Create a recording::dereference_field_rvalue instance and add it to
4067 : the rvalue's context's list of mementos.
4068 :
4069 : Implements the post-error-checking part of
4070 : gcc_jit_rvalue_dereference_field. */
4071 :
4072 : recording::lvalue *
4073 1531 : recording::rvalue::dereference_field (recording::location *loc,
4074 : field *field)
4075 : {
4076 1531 : recording::lvalue *result =
4077 1531 : new dereference_field_rvalue (m_ctxt, loc, this, field);
4078 1531 : m_ctxt->record (result);
4079 1531 : return result;
4080 : }
4081 :
4082 : /* Create a recording::dereference_rvalue instance and add it to the
4083 : rvalue's context's list of mementos.
4084 :
4085 : Implements the post-error-checking part of
4086 : gcc_jit_rvalue_dereference. */
4087 :
4088 : recording::lvalue *
4089 684 : recording::rvalue::dereference (recording::location *loc)
4090 : {
4091 684 : recording::lvalue *result =
4092 684 : new dereference_rvalue (m_ctxt, loc, this);
4093 684 : m_ctxt->record (result);
4094 684 : return result;
4095 : }
4096 :
4097 : /* An rvalue visitor, for validating that every rvalue within an expression
4098 : trees within "STMT" has the correct scope (e.g. no access to locals
4099 : of a different function). */
4100 :
4101 17563 : class rvalue_usage_validator : public recording::rvalue_visitor
4102 : {
4103 : public:
4104 : rvalue_usage_validator (const char *api_funcname,
4105 : recording::context *ctxt,
4106 : recording::statement *stmt,
4107 : recording::lvalue *global = NULL);
4108 : void
4109 : visit (recording::rvalue *rvalue) final override;
4110 :
4111 : bool
4112 870 : has_errors () const
4113 : {
4114 870 : return m_has_errors;
4115 : }
4116 :
4117 : private:
4118 : const char *m_api_funcname;
4119 : recording::context *m_ctxt;
4120 : recording::statement *m_stmt;
4121 : recording::lvalue *m_global;
4122 : bool m_has_errors;
4123 : };
4124 :
4125 : /* The trivial constructor for rvalue_usage_validator. */
4126 :
4127 17564 : rvalue_usage_validator::rvalue_usage_validator (const char *api_funcname,
4128 : recording::context *ctxt,
4129 : recording::statement *stmt,
4130 : recording::lvalue *global)
4131 17564 : : m_api_funcname (api_funcname),
4132 17564 : m_ctxt (ctxt),
4133 17564 : m_stmt (stmt),
4134 17564 : m_global (global),
4135 17564 : m_has_errors (false)
4136 : {
4137 17564 : }
4138 :
4139 : /* Verify that the given rvalue is in the correct scope. */
4140 :
4141 : void
4142 34272 : rvalue_usage_validator::visit (recording::rvalue *rvalue)
4143 : {
4144 34272 : if (!m_stmt)
4145 : {
4146 2420 : gcc_assert (m_global);
4147 2420 : if (rvalue->get_scope ())
4148 : {
4149 10 : m_ctxt->add_error
4150 10 : (rvalue->get_loc (),
4151 : "%s:"
4152 : " rvalue %s (type: %s)"
4153 : " has scope limited to function %s"
4154 : " but was used in initializer for global %s",
4155 : m_api_funcname,
4156 : rvalue->get_debug_string (),
4157 10 : rvalue->get_type ()->get_debug_string (),
4158 10 : rvalue->get_scope ()->get_debug_string (),
4159 : m_global->get_debug_string ());
4160 10 : m_has_errors = true;
4161 : }
4162 2410 : else if (rvalue->dyn_cast_param ())
4163 : {
4164 0 : m_ctxt->add_error
4165 0 : (rvalue->get_loc (),
4166 : "%s:"
4167 : " param %s (type: %s)"
4168 : " was used in initializer for global %s"
4169 : " but is not associated with any function",
4170 : m_api_funcname,
4171 : rvalue->get_debug_string (),
4172 0 : rvalue->get_type ()->get_debug_string (),
4173 0 : m_global->get_debug_string ());
4174 0 : m_has_errors = true;
4175 : }
4176 : }
4177 : else
4178 : {
4179 31852 : gcc_assert (m_stmt->get_block ());
4180 31852 : recording::function *stmt_scope = m_stmt->get_block ()->get_function ();
4181 :
4182 : /* Most rvalues don't have a scope (only locals, params, and
4183 : block addresses). */
4184 31852 : if (rvalue->get_scope ())
4185 : {
4186 14750 : if (rvalue->get_scope () != stmt_scope)
4187 : {
4188 15 : m_ctxt->add_error
4189 15 : (rvalue->get_loc (),
4190 : "%s:"
4191 : " rvalue %s (type: %s)"
4192 : " has scope limited to function %s"
4193 : " but was used within function %s"
4194 : " (in statement: %s)",
4195 : m_api_funcname,
4196 : rvalue->get_debug_string (),
4197 15 : rvalue->get_type ()->get_debug_string (),
4198 15 : rvalue->get_scope ()->get_debug_string (),
4199 : stmt_scope->get_debug_string (),
4200 : m_stmt->get_debug_string ());
4201 15 : m_has_errors = true;
4202 : }
4203 : }
4204 : else
4205 : {
4206 17102 : if (rvalue->dyn_cast_param ())
4207 : {
4208 5 : m_ctxt->add_error
4209 5 : (rvalue->get_loc (),
4210 : "%s:"
4211 : " param %s (type: %s)"
4212 : " was used within function %s"
4213 : " (in statement: %s)"
4214 : " but is not associated with any function",
4215 : m_api_funcname,
4216 : rvalue->get_debug_string (),
4217 5 : rvalue->get_type ()->get_debug_string (),
4218 : stmt_scope->get_debug_string (),
4219 5 : m_stmt->get_debug_string ());
4220 5 : m_has_errors = true;
4221 : }
4222 : }
4223 : }
4224 :
4225 34271 : rvalue->visit_children (this);
4226 34268 : }
4227 :
4228 : /* Verify that it's valid to use this rvalue (and all expressions
4229 : in the tree below it) within the given statement.
4230 :
4231 : For example, we must reject attempts to use a local from one
4232 : function within a different function here, or we'll get
4233 : an ICE deep inside toplev::main. */
4234 :
4235 : void
4236 16694 : recording::rvalue::verify_valid_within_stmt (const char *api_funcname, statement *s)
4237 : {
4238 16694 : rvalue_usage_validator v (api_funcname,
4239 : s->get_context (),
4240 16694 : s);
4241 :
4242 : /* Verify that it's OK to use this rvalue within s. */
4243 16694 : v.visit (this);
4244 16693 : }
4245 :
4246 : /* Verify that it's valid to use this rvalue (and all expressions
4247 : in the tree below it) as the initializer for GLOBAL. */
4248 :
4249 : bool
4250 870 : recording::rvalue::verify_valid_within_global_init (const char *api_funcname,
4251 : lvalue *global)
4252 : {
4253 870 : rvalue_usage_validator v (api_funcname,
4254 : global->get_context (),
4255 : NULL,
4256 870 : global);
4257 :
4258 870 : v.visit (this);
4259 870 : return !v.has_errors ();
4260 870 : }
4261 :
4262 : /* Set the scope of this rvalue to be the given function. This can only
4263 : be done once on a given rvalue. */
4264 :
4265 : void
4266 23720 : recording::rvalue::set_scope (function *scope)
4267 : {
4268 23720 : gcc_assert (scope);
4269 23720 : gcc_assert (m_scope == NULL);
4270 23720 : m_scope = scope;
4271 23720 : }
4272 :
4273 :
4274 : /* Implementation of recording::rvalue::access_as_rvalue for rvalues
4275 : themselves.
4276 : Instances of rvalue don't need an upcast call. */
4277 :
4278 : const char *
4279 9408 : recording::rvalue::access_as_rvalue (reproducer &r)
4280 : {
4281 9408 : return r.get_identifier (this);
4282 : }
4283 :
4284 : /* Return a debug string for the given rvalue, wrapping it in parentheses
4285 : if needed to mimic C's precedence rules, i.e. if OUTER_PREC is of
4286 : stronger precedence that this rvalue's precedence.
4287 :
4288 : For example, given:
4289 :
4290 : MULT
4291 : / \
4292 : PLUS MINUS
4293 : / \ / \
4294 : A B C D
4295 :
4296 : we want to emit:
4297 :
4298 : (A + B) * (C - D)
4299 :
4300 : since MULT has strong precedence than PLUS and MINUS, whereas for:
4301 :
4302 : PLUS
4303 : / \
4304 : MULT DIVIDE
4305 : / \ / \
4306 : A B C D
4307 :
4308 : we can simply emit:
4309 :
4310 : A * B + C / D
4311 :
4312 : since PLUS has weaker precedence than MULT and DIVIDE. */
4313 :
4314 : const char *
4315 12369 : recording::rvalue::get_debug_string_parens (enum precedence outer_prec)
4316 : {
4317 12369 : enum precedence this_prec = get_precedence ();
4318 :
4319 : /* If this_prec has stronger precedence than outer_prec, we don't
4320 : need to wrap this in parens within the outer debug string.
4321 : Stronger precedences occur earlier than weaker within the enum,
4322 : so this is a less than test. Equal precedences don't need
4323 : parentheses. */
4324 12369 : if (this_prec <= outer_prec)
4325 11109 : return get_debug_string();
4326 :
4327 : /* Otherwise, we need parentheses. */
4328 :
4329 : /* Lazily-build and cache m_parenthesized_string. */
4330 1260 : if (!m_parenthesized_string)
4331 : {
4332 634 : const char *debug_string = get_debug_string ();
4333 634 : m_parenthesized_string = string::from_printf (get_context (),
4334 : "(%s)",
4335 : debug_string);
4336 : }
4337 1260 : gcc_assert (m_parenthesized_string);
4338 1260 : return m_parenthesized_string->c_str ();
4339 : }
4340 :
4341 :
4342 : /* The implementation of class gcc::jit::recording::lvalue. */
4343 :
4344 : /* Create a recording::new_access_field_of_lvalue instance and add it to
4345 : the lvalue's context's list of mementos.
4346 :
4347 : Implements the post-error-checking part of
4348 : gcc_jit_lvalue_access_field. */
4349 :
4350 : recording::lvalue *
4351 219 : recording::lvalue::access_field (recording::location *loc,
4352 : field *field)
4353 : {
4354 219 : recording::lvalue *result =
4355 219 : new access_field_of_lvalue (m_ctxt, loc, this, field);
4356 219 : m_ctxt->record (result);
4357 219 : return result;
4358 : }
4359 :
4360 : /* Implementation of recording::rvalue::access_as_rvalue for lvalues.
4361 : Instances of lvalue need to be wrapped in a gcc_jit_lvalue_as_rvalue
4362 : upcast call. */
4363 :
4364 : const char *
4365 3464 : recording::lvalue::access_as_rvalue (reproducer &r)
4366 : {
4367 3464 : return r.xstrdup_printf ("gcc_jit_lvalue_as_rvalue (%s)",
4368 3464 : r.get_identifier (this));
4369 : }
4370 :
4371 : /* Implementation of recording::lvalue::access_as_lvalue for lvalues.
4372 : Instances of lvalue don't need to be upcast. */
4373 :
4374 : const char *
4375 2898 : recording::lvalue::access_as_lvalue (reproducer &r)
4376 : {
4377 2898 : return r.get_identifier (this);
4378 : }
4379 :
4380 : /* Create a recording::get_address_of_lvalue instance and add it to
4381 : the lvalue's context's list of mementos.
4382 :
4383 : Implements the post-error-checking part of
4384 : gcc_jit_lvalue_get_address. */
4385 :
4386 : recording::rvalue *
4387 508 : recording::lvalue::get_address (recording::location *loc)
4388 : {
4389 508 : recording::rvalue *result =
4390 508 : new get_address_of_lvalue (m_ctxt, loc, this);
4391 508 : m_ctxt->record (result);
4392 508 : return result;
4393 : }
4394 :
4395 : void
4396 15 : recording::lvalue::set_tls_model (enum gcc_jit_tls_model model)
4397 : {
4398 15 : m_tls_model = model;
4399 15 : }
4400 :
4401 5 : void recording::lvalue::set_link_section (const char *name)
4402 : {
4403 5 : m_link_section = new_string (name);
4404 5 : }
4405 :
4406 20 : void recording::lvalue::set_register_name (const char *reg_name)
4407 : {
4408 20 : m_reg_name = new_string (reg_name);
4409 20 : }
4410 :
4411 10 : void recording::lvalue::set_alignment (unsigned bytes)
4412 : {
4413 10 : m_alignment = bytes;
4414 10 : }
4415 :
4416 5 : void recording::lvalue::add_string_attribute (
4417 : gcc_jit_variable_attribute attribute,
4418 : const char* value)
4419 : {
4420 5 : m_string_attributes.push_back (std::make_pair (attribute, std::string (value)));
4421 5 : }
4422 :
4423 : /* The implementation of class gcc::jit::recording::param. */
4424 :
4425 : /* Implementation of pure virtual hook recording::memento::replay_into
4426 : for recording::param. */
4427 :
4428 : void
4429 18208 : recording::param::replay_into (replayer *r)
4430 : {
4431 18208 : set_playback_obj (r->new_param (playback_location (r, m_loc),
4432 18208 : m_type->playback_type (),
4433 18208 : m_name->c_str ()));
4434 18208 : }
4435 :
4436 : /* Implementation of recording::rvalue::access_as_rvalue for params.
4437 : Instances of param need to be wrapped in a gcc_jit_param_as_rvalue
4438 : upcast call. */
4439 :
4440 : const char *
4441 4284 : recording::param::access_as_rvalue (reproducer &r)
4442 : {
4443 4284 : return r.xstrdup_printf ("gcc_jit_param_as_rvalue (%s)",
4444 4284 : r.get_identifier (this));
4445 : }
4446 :
4447 : /* Implementation of recording::lvalue::access_as_lvalue for params.
4448 : Instances of param need to be wrapped in a gcc_jit_param_as_lvalue
4449 : upcast call. */
4450 :
4451 : const char *
4452 74 : recording::param::access_as_lvalue (reproducer &r)
4453 : {
4454 74 : return r.xstrdup_printf ("gcc_jit_param_as_lvalue (%s)",
4455 74 : r.get_identifier (this));
4456 : }
4457 :
4458 : /* Implementation of recording::memento::write_reproducer for params. */
4459 :
4460 : void
4461 7242 : recording::param::write_reproducer (reproducer &r)
4462 : {
4463 7242 : const char *id = r.make_identifier (this, "param");
4464 14484 : r.write (" gcc_jit_param *%s =\n"
4465 : " gcc_jit_context_new_param (%s,\n"
4466 : " %s, /* gcc_jit_location *loc */\n"
4467 : " %s, /*gcc_jit_type *type */\n"
4468 : " %s); /* const char *name */\n",
4469 : id,
4470 : r.get_identifier (get_context ()),
4471 7242 : r.get_identifier (m_loc),
4472 : r.get_identifier_as_type (m_type),
4473 7242 : m_name->get_debug_string ());
4474 7242 : }
4475 :
4476 : /* The implementation of class gcc::jit::recording::function. */
4477 :
4478 : /* gcc::jit::recording::function's constructor. */
4479 :
4480 18205 : recording::function::function (context *ctxt,
4481 : recording::location *loc,
4482 : enum gcc_jit_function_kind kind,
4483 : type *return_type,
4484 : recording::string *name,
4485 : int num_params,
4486 : recording::param **params,
4487 : int is_variadic,
4488 : enum built_in_function builtin_id,
4489 : bool is_target_builtin)
4490 : : memento (ctxt),
4491 18205 : m_loc (loc),
4492 18205 : m_kind (kind),
4493 18205 : m_return_type (return_type),
4494 18205 : m_name (name),
4495 18205 : m_params (),
4496 18205 : m_is_variadic (is_variadic),
4497 18205 : m_builtin_id (builtin_id),
4498 18205 : m_locals (),
4499 18205 : m_blocks (),
4500 18205 : m_fn_ptr_type (NULL),
4501 18205 : m_attributes (),
4502 18205 : m_string_attributes (),
4503 18205 : m_int_array_attributes (),
4504 18205 : m_is_target_builtin (is_target_builtin)
4505 : {
4506 37880 : for (int i = 0; i< num_params; i++)
4507 : {
4508 19675 : param *param = params[i];
4509 19675 : gcc_assert (param);
4510 :
4511 : /* Associate each param with this function.
4512 :
4513 : Verify that the param doesn't already have a function. */
4514 19675 : if (param->get_scope ())
4515 : {
4516 : /* We've already rejected attempts to reuse a param between
4517 : different functions (within gcc_jit_context_new_function), so
4518 : if the param *does* already have a function, it must be being
4519 : reused within the params array for this function. We must
4520 : produce an error for this reuse (blocking the compile), since
4521 : otherwise we'd have an ICE later on. */
4522 5 : gcc_assert (this == param->get_scope ());
4523 5 : ctxt->add_error
4524 5 : (loc,
4525 : "gcc_jit_context_new_function:"
4526 : " parameter %s (type: %s)"
4527 : " is used more than once when creating function %s",
4528 : param->get_debug_string (),
4529 5 : param->get_type ()->get_debug_string (),
4530 : name->c_str ());
4531 : }
4532 : else
4533 : {
4534 : /* The normal, non-error case: associate this function with the
4535 : param. */
4536 19670 : param->set_scope (this);
4537 : }
4538 :
4539 19675 : m_params.safe_push (param);
4540 : }
4541 18205 : }
4542 :
4543 : /* Implementation of pure virtual hook recording::memento::replay_into
4544 : for recording::function. */
4545 :
4546 : void
4547 18167 : recording::function::replay_into (replayer *r)
4548 : {
4549 : /* Convert m_params to a vec of playback param. */
4550 18167 : auto_vec <playback::param *> params;
4551 18167 : int i;
4552 18167 : recording::param *param;
4553 18167 : params.create (m_params.length ());
4554 54542 : FOR_EACH_VEC_ELT (m_params, i, param)
4555 18208 : params.safe_push (param->playback_param ());
4556 :
4557 18167 : set_playback_obj (r->new_function (playback_location (r, m_loc),
4558 : m_kind,
4559 18167 : m_return_type->playback_type (),
4560 18167 : m_name->c_str (),
4561 : ¶ms,
4562 : m_is_variadic,
4563 : m_builtin_id,
4564 18167 : m_attributes,
4565 18167 : m_string_attributes,
4566 18167 : m_int_array_attributes,
4567 : m_is_target_builtin));
4568 18167 : }
4569 :
4570 : /* Create a recording::local instance and add it to
4571 : the functions's context's list of mementos, and to the function's
4572 : list of locals.
4573 :
4574 : Implements the post-error-checking part of
4575 : gcc_jit_function_new_local. */
4576 :
4577 : recording::lvalue *
4578 3990 : recording::function::new_local (recording::location *loc,
4579 : type *type,
4580 : const char *name)
4581 : {
4582 3990 : local *result = new local (this, loc, type, new_string (name));
4583 3990 : m_ctxt->record (result);
4584 3990 : m_locals.safe_push (result);
4585 3990 : return result;
4586 : }
4587 :
4588 : /* Create a recording::local instance and add it to
4589 : the functions's context's list of mementos, and to the function's
4590 : list of locals.
4591 :
4592 : Implements the post-error-checking part of
4593 : gcc_jit_function_new_temp. */
4594 :
4595 : recording::lvalue *
4596 5 : recording::function::new_temp (recording::location *loc,
4597 : type *type)
4598 : {
4599 5 : local *result = new local (this, loc, type, NULL);
4600 5 : m_ctxt->record (result);
4601 5 : m_locals.safe_push (result);
4602 5 : return result;
4603 : }
4604 :
4605 : /* Create a recording::block instance and add it to
4606 : the functions's context's list of mementos, and to the function's
4607 : list of blocks.
4608 :
4609 : Implements the post-error-checking part of
4610 : gcc_jit_function_new_block. */
4611 :
4612 : recording::block*
4613 6608 : recording::function::new_block (const char *name)
4614 : {
4615 6608 : gcc_assert (m_kind != GCC_JIT_FUNCTION_IMPORTED);
4616 :
4617 6608 : recording::block *result =
4618 9265 : new recording::block (this, m_blocks.length (), new_string (name));
4619 6609 : m_ctxt->record (result);
4620 6609 : m_blocks.safe_push (result);
4621 6609 : return result;
4622 : }
4623 :
4624 : /* Override the default implementation of
4625 : recording::memento::write_to_dump by dumping a C-like
4626 : representation of the function; either like a prototype
4627 : for GCC_JIT_FUNCTION_IMPORTED, or like a full definition for
4628 : all other kinds of function. */
4629 :
4630 : void
4631 14 : recording::function::write_to_dump (dump &d)
4632 : {
4633 14 : for (auto attr: m_attributes)
4634 : {
4635 0 : const char* attribute = fn_attribute_to_string (attr);
4636 0 : if (attribute)
4637 0 : d.write ("__attribute(%s)__\n", attribute);
4638 : }
4639 14 : for (auto attr: m_string_attributes)
4640 : {
4641 0 : gcc_jit_fn_attribute& name = std::get<0>(attr);
4642 0 : std::string& value = std::get<1>(attr);
4643 0 : const char* attribute = fn_attribute_to_string (name);
4644 :
4645 0 : if (attribute)
4646 0 : d.write ("__attribute(%s(\"%s\"))__\n", attribute, value.c_str());
4647 0 : }
4648 14 : for (auto attr: m_int_array_attributes)
4649 : {
4650 0 : gcc_jit_fn_attribute& name = std::get<0>(attr);
4651 0 : std::vector<int>& values = std::get<1>(attr);
4652 0 : const char* attribute = fn_attribute_to_string (name);
4653 0 : if (attribute)
4654 : {
4655 0 : d.write ("__attribute(%s(", attribute);
4656 0 : for (size_t i = 0; i < values.size(); ++i)
4657 : {
4658 0 : if (i > 0)
4659 0 : d.write (", %d", values[i]);
4660 : else
4661 0 : d.write ("%d", values[i]);
4662 : }
4663 0 : d.write ("))__\n");
4664 : }
4665 0 : }
4666 :
4667 14 : switch (m_kind)
4668 : {
4669 0 : default: gcc_unreachable ();
4670 14 : case GCC_JIT_FUNCTION_EXPORTED:
4671 14 : case GCC_JIT_FUNCTION_IMPORTED:
4672 14 : d.write ("extern ");
4673 14 : break;
4674 0 : case GCC_JIT_FUNCTION_INTERNAL:
4675 0 : d.write ("static ");
4676 0 : break;
4677 0 : case GCC_JIT_FUNCTION_ALWAYS_INLINE:
4678 0 : d.write ("static inline ");
4679 0 : break;
4680 : }
4681 14 : d.write ("%s\n", m_return_type->get_debug_string ());
4682 :
4683 14 : if (d.update_locations ())
4684 14 : m_loc = d.make_location ();
4685 :
4686 14 : d.write ("%s (", get_debug_string ());
4687 :
4688 14 : int i;
4689 14 : recording::param *param;
4690 74 : FOR_EACH_VEC_ELT (m_params, i, param)
4691 : {
4692 46 : if (i > 0)
4693 32 : d.write (", ");
4694 46 : d.write ("%s %s",
4695 46 : param->get_type ()->get_debug_string (),
4696 : param->get_debug_string ());
4697 : }
4698 14 : d.write (")");
4699 14 : if (m_kind == GCC_JIT_FUNCTION_IMPORTED)
4700 : {
4701 2 : d.write ("; /* (imported) */\n\n");
4702 : }
4703 : else
4704 : {
4705 12 : int i;
4706 12 : local *var = NULL;
4707 12 : block *b;
4708 12 : d.write ("\n{\n");
4709 :
4710 : /* Write locals: */
4711 40 : FOR_EACH_VEC_ELT (m_locals, i, var)
4712 16 : var->write_to_dump (d);
4713 12 : if (m_locals.length ())
4714 8 : d.write ("\n");
4715 :
4716 : /* Write each block: */
4717 56 : FOR_EACH_VEC_ELT (m_blocks, i, b)
4718 : {
4719 44 : if (i > 0)
4720 32 : d.write ("\n");
4721 44 : b->write_to_dump (d);
4722 : }
4723 :
4724 12 : d.write ("}\n\n");
4725 : }
4726 14 : }
4727 :
4728 : /* Pre-compilation validation of a function, for those things we can't
4729 : check until the context is (supposedly) fully-populated. */
4730 :
4731 : void
4732 4463 : recording::function::validate ()
4733 : {
4734 : /* Complain about empty functions with non-void return type. */
4735 4463 : if (m_kind != GCC_JIT_FUNCTION_IMPORTED
4736 4463 : && m_return_type != m_ctxt->get_type (GCC_JIT_TYPE_VOID))
4737 3272 : if (m_blocks.length () == 0)
4738 5 : m_ctxt->add_error (m_loc,
4739 : "function %s returns non-void (type: %s)"
4740 : " but has no blocks",
4741 : get_debug_string (),
4742 5 : m_return_type->get_debug_string ());
4743 :
4744 : /* Check that all blocks are terminated. */
4745 4463 : int num_invalid_blocks = 0;
4746 4463 : {
4747 4463 : int i;
4748 4463 : block *b;
4749 :
4750 11080 : FOR_EACH_VEC_ELT (m_blocks, i, b)
4751 6617 : if (!b->validate ())
4752 423 : num_invalid_blocks++;
4753 : }
4754 :
4755 : /* Check that all blocks are reachable. */
4756 4463 : if (!m_ctxt->get_inner_bool_option
4757 4463 : (INNER_BOOL_OPTION_ALLOW_UNREACHABLE_BLOCKS)
4758 4463 : && m_blocks.length () > 0 && num_invalid_blocks == 0)
4759 : {
4760 : /* Iteratively walk the graph of blocks, marking their "m_is_reachable"
4761 : flag, starting at the initial block. */
4762 3537 : auto_vec<block *> worklist (m_blocks.length ());
4763 3537 : worklist.safe_push (m_blocks[0]);
4764 22867 : while (worklist.length () > 0)
4765 : {
4766 6129 : block *b = worklist.pop ();
4767 6128 : b->m_is_reachable = true;
4768 :
4769 : /* Add successor blocks that aren't yet marked to the worklist. */
4770 : /* We checked that each block has a terminating statement above . */
4771 6128 : vec <block *> successors = b->get_successor_blocks ();
4772 6128 : int i;
4773 6128 : block *succ;
4774 17592 : FOR_EACH_VEC_ELT (successors, i, succ)
4775 3180 : if (!succ->m_is_reachable)
4776 2591 : worklist.safe_push (succ);
4777 6128 : successors.release ();
4778 : }
4779 :
4780 : /* Now complain about any blocks that haven't been marked. */
4781 : {
4782 : int i;
4783 : block *b;
4784 13200 : FOR_EACH_VEC_ELT (m_blocks, i, b)
4785 6128 : if (!b->m_is_reachable)
4786 5 : m_ctxt->add_error (b->get_loc (),
4787 : "unreachable block: %s",
4788 : b->get_debug_string ());
4789 : }
4790 3536 : }
4791 4462 : }
4792 :
4793 : /* Implements the post-error-checking part of
4794 : gcc_jit_function_dump_to_dot. */
4795 :
4796 : void
4797 0 : recording::function::dump_to_dot (const char *path)
4798 : {
4799 0 : FILE *fp = fopen (path, "w");
4800 0 : if (!fp)
4801 0 : return;
4802 :
4803 0 : pretty_printer the_pp;
4804 0 : the_pp.set_output_stream (fp);
4805 :
4806 0 : pretty_printer *pp = &the_pp;
4807 :
4808 0 : pp_printf (pp, "digraph %s", get_debug_string ());
4809 0 : pp_string (pp, " {\n");
4810 :
4811 : /* Blocks: */
4812 0 : {
4813 0 : int i;
4814 0 : block *b;
4815 0 : FOR_EACH_VEC_ELT (m_blocks, i, b)
4816 0 : b->dump_to_dot (pp);
4817 : }
4818 :
4819 : /* Edges: */
4820 : {
4821 : int i;
4822 : block *b;
4823 0 : FOR_EACH_VEC_ELT (m_blocks, i, b)
4824 0 : b->dump_edges_to_dot (pp);
4825 : }
4826 :
4827 0 : pp_string (pp, "}\n");
4828 0 : pp_flush (pp);
4829 0 : fclose (fp);
4830 0 : }
4831 :
4832 : /* Implements the post-error-checking part of
4833 : gcc_jit_function_get_address. */
4834 :
4835 : recording::rvalue *
4836 15 : recording::function::get_address (recording::location *loc)
4837 : {
4838 : /* Lazily create and cache the function pointer type. */
4839 15 : if (!m_fn_ptr_type)
4840 : {
4841 : /* Make a recording::function_type for this function. */
4842 30 : auto_vec <recording::type *> param_types (m_params.length ());
4843 15 : unsigned i;
4844 15 : recording::param *param;
4845 45 : FOR_EACH_VEC_ELT (m_params, i, param)
4846 15 : param_types.safe_push (param->get_type ());
4847 15 : recording::function_type *fn_type
4848 15 : = m_ctxt->new_function_type (m_return_type,
4849 30 : m_params.length (),
4850 : param_types.address (),
4851 : m_is_variadic,
4852 : m_is_target_builtin);
4853 15 : m_fn_ptr_type = fn_type->get_pointer ();
4854 15 : }
4855 15 : gcc_assert (m_fn_ptr_type);
4856 :
4857 15 : rvalue *result = new function_pointer (get_context (), loc, this, m_fn_ptr_type);
4858 15 : m_ctxt->record (result);
4859 15 : return result;
4860 : }
4861 :
4862 : void
4863 45 : recording::function::add_attribute (gcc_jit_fn_attribute attribute)
4864 : {
4865 45 : m_attributes.push_back (attribute);
4866 45 : }
4867 :
4868 : void
4869 5 : recording::function::add_string_attribute (gcc_jit_fn_attribute attribute,
4870 : const char* value)
4871 : {
4872 5 : m_string_attributes.push_back (
4873 10 : std::make_pair (attribute, std::string (value)));
4874 5 : }
4875 :
4876 : void
4877 5 : recording::function::add_integer_array_attribute (
4878 : gcc_jit_fn_attribute attribute,
4879 : const int* value,
4880 : size_t length)
4881 : {
4882 5 : m_int_array_attributes.push_back (std::make_pair (
4883 : attribute,
4884 5 : std::vector<int> (value, value + length)));
4885 5 : }
4886 :
4887 : /* Implementation of recording::memento::make_debug_string for
4888 : functions. */
4889 :
4890 : recording::string *
4891 3355 : recording::function::make_debug_string ()
4892 : {
4893 3355 : return m_name;
4894 : }
4895 :
4896 : /* A table of enum gcc_jit_function_kind values expressed in string
4897 : form. */
4898 :
4899 : static const char * const names_of_function_kinds[] = {
4900 : "GCC_JIT_FUNCTION_EXPORTED",
4901 : "GCC_JIT_FUNCTION_INTERNAL",
4902 : "GCC_JIT_FUNCTION_IMPORTED",
4903 : "GCC_JIT_FUNCTION_ALWAYS_INLINE"
4904 : };
4905 :
4906 : /* Implementation of recording::memento::write_reproducer for functions. */
4907 :
4908 : static const char * const fn_attribute_reproducer_strings[] =
4909 : {
4910 : "GCC_JIT_FN_ATTRIBUTE_ALIAS",
4911 : "GCC_JIT_FN_ATTRIBUTE_ALWAYS_INLINE",
4912 : "GCC_JIT_FN_ATTRIBUTE_INLINE",
4913 : "GCC_JIT_FN_ATTRIBUTE_NOINLINE",
4914 : "GCC_JIT_FN_ATTRIBUTE_TARGET",
4915 : "GCC_JIT_FN_ATTRIBUTE_USED",
4916 : "GCC_JIT_FN_ATTRIBUTE_VISIBILITY",
4917 : "GCC_JIT_FN_ATTRIBUTE_COLD",
4918 : "GCC_JIT_FN_ATTRIBUTE_RETURNS_TWICE",
4919 : "GCC_JIT_FN_ATTRIBUTE_PURE",
4920 : "GCC_JIT_FN_ATTRIBUTE_CONST",
4921 : "GCC_JIT_FN_ATTRIBUTE_WEAK",
4922 : "GCC_JIT_FN_ATTRIBUTE_NONNULL",
4923 : "GCC_JIT_FN_ATTRIBUTE_ARM_CMSE_NONSECURE_CALL",
4924 : "GCC_JIT_FN_ATTRIBUTE_ARM_CMSE_NONSECURE_ENTRY",
4925 : "GCC_JIT_FN_ATTRIBUTE_ARM_PCS",
4926 : "GCC_JIT_FN_ATTRIBUTE_AVR_INTERRUPT",
4927 : "GCC_JIT_FN_ATTRIBUTE_AVR_NOBLOCK",
4928 : "GCC_JIT_FN_ATTRIBUTE_AVR_SIGNAL",
4929 : "GCC_JIT_FN_ATTRIBUTE_GCN_AMDGPU_HSA_KERNEL",
4930 : "GCC_JIT_FN_ATTRIBUTE_MSP430_INTERRUPT",
4931 : "GCC_JIT_FN_ATTRIBUTE_NVPTX_KERNEL",
4932 : "GCC_JIT_FN_ATTRIBUTE_RISCV_INTERRUPT",
4933 : "GCC_JIT_FN_ATTRIBUTE_X86_FAST_CALL",
4934 : "GCC_JIT_FN_ATTRIBUTE_X86_INTERRUPT",
4935 : "GCC_JIT_FN_ATTRIBUTE_X86_MS_ABI",
4936 : "GCC_JIT_FN_ATTRIBUTE_X86_STDCALL",
4937 : "GCC_JIT_FN_ATTRIBUTE_X86_SYSV_ABI",
4938 : "GCC_JIT_FN_ATTRIBUTE_X86_THIS_CALL",
4939 : };
4940 :
4941 : std::string
4942 5 : get_vector_int_debug (std::vector<int> &values)
4943 : {
4944 5 : std::stringstream s;
4945 :
4946 5 : s << "{";
4947 10 : for(auto it = values.begin(); it != values.end(); ++it)
4948 : {
4949 5 : if (it != values.begin() )
4950 0 : s << ", ";
4951 5 : s << *it;
4952 : }
4953 5 : s << "}";
4954 5 : return s.str();
4955 5 : }
4956 :
4957 : void
4958 3410 : recording::function::write_reproducer (reproducer &r)
4959 : {
4960 3410 : const char *id = r.make_identifier (this, "func");
4961 :
4962 3410 : if (m_builtin_id)
4963 : {
4964 220 : r.write (" gcc_jit_function *%s =\n"
4965 : " gcc_jit_context_get_builtin_function (%s,\n"
4966 : " %s);\n",
4967 : id,
4968 : r.get_identifier (get_context ()),
4969 220 : m_name->get_debug_string ());
4970 220 : return;
4971 : }
4972 3190 : const char *params_id = r.make_tmp_identifier ("params_for", this);
4973 3190 : r.write (" gcc_jit_param *%s[%i] = {\n",
4974 : params_id,
4975 : m_params.length ());
4976 3190 : int i;
4977 3190 : param *param;
4978 12021 : FOR_EACH_VEC_ELT (m_params, i, param)
4979 5641 : r.write (" %s,\n", r.get_identifier (param));
4980 3190 : r.write (" };\n");
4981 9570 : r.write (" gcc_jit_function *%s =\n"
4982 : " gcc_jit_context_new_function (%s, /* gcc_jit_context *ctxt */\n"
4983 : " %s, /* gcc_jit_location *loc */\n"
4984 : " %s, /* enum gcc_jit_function_kind kind */\n"
4985 : " %s, /* gcc_jit_type *return_type */\n"
4986 : " %s, /* const char *name */\n"
4987 : " %i, /* int num_params */\n"
4988 : " %s, /* gcc_jit_param **params */\n"
4989 : " %i); /* int is_variadic */\n",
4990 : id,
4991 : r.get_identifier (get_context ()),
4992 3190 : r.get_identifier (m_loc),
4993 3190 : names_of_function_kinds[m_kind],
4994 : r.get_identifier_as_type (m_return_type),
4995 3190 : m_name->get_debug_string (),
4996 : m_params.length (),
4997 : params_id,
4998 : m_is_variadic);
4999 3235 : for (auto attribute : m_attributes)
5000 45 : r.write(" gcc_jit_function_add_attribute (%s, %s);\n",
5001 : id,
5002 45 : fn_attribute_reproducer_strings[attribute]);
5003 3195 : for (auto attribute : m_string_attributes)
5004 5 : r.write(" gcc_jit_function_add_string_attribute (%s, %s, \"%s\");\n",
5005 : id,
5006 5 : fn_attribute_reproducer_strings[std::get<0>(attribute)],
5007 10 : std::get<1>(attribute).c_str());
5008 3195 : for (auto attribute : m_int_array_attributes) {
5009 5 : r.write(" gcc_jit_function_add_integer_array_attribute (%s,\n"
5010 : " %s,\n"
5011 : " (int[])%s,\n"
5012 : " %lu);\n",
5013 : id,
5014 5 : fn_attribute_reproducer_strings[std::get<0>(attribute)],
5015 10 : get_vector_int_debug (std::get<1>(attribute)).c_str(),
5016 5 : std::get<1>(attribute).size ());
5017 5 : }
5018 : }
5019 :
5020 :
5021 : /* The implementation of class gcc::jit::recording::block. */
5022 :
5023 : /* Create a recording::eval instance and add it to
5024 : the block's context's list of mementos, and to the block's
5025 : list of statements.
5026 :
5027 : Implements the heart of gcc_jit_block_add_eval. */
5028 :
5029 : recording::statement *
5030 2027 : recording::block::add_eval (recording::location *loc,
5031 : recording::rvalue *rvalue)
5032 : {
5033 2027 : statement *result = new eval (this, loc, rvalue);
5034 2027 : m_ctxt->record (result);
5035 2027 : m_statements.safe_push (result);
5036 2027 : return result;
5037 : }
5038 :
5039 : /* Create a recording::assignment instance and add it to
5040 : the block's context's list of mementos, and to the block's
5041 : list of statements.
5042 :
5043 : Implements the heart of gcc_jit_block_add_assignment. */
5044 :
5045 : recording::statement *
5046 4703 : recording::block::add_assignment (recording::location *loc,
5047 : recording::lvalue *lvalue,
5048 : recording::rvalue *rvalue)
5049 : {
5050 4703 : statement *result = new assignment (this, loc, lvalue, rvalue);
5051 4703 : m_ctxt->record (result);
5052 4703 : m_statements.safe_push (result);
5053 4703 : return result;
5054 : }
5055 :
5056 : /* Create a recording::assignment_op instance and add it to
5057 : the block's context's list of mementos, and to the block's
5058 : list of statements.
5059 :
5060 : Implements the heart of gcc_jit_block_add_assignment_op. */
5061 :
5062 : recording::statement *
5063 484 : recording::block::add_assignment_op (recording::location *loc,
5064 : recording::lvalue *lvalue,
5065 : enum gcc_jit_binary_op op,
5066 : recording::rvalue *rvalue)
5067 : {
5068 484 : statement *result = new assignment_op (this, loc, lvalue, op, rvalue);
5069 484 : m_ctxt->record (result);
5070 484 : m_statements.safe_push (result);
5071 484 : return result;
5072 : }
5073 :
5074 : /* Create a recording::comment instance and add it to
5075 : the block's context's list of mementos, and to the block's
5076 : list of statements.
5077 :
5078 : Implements the heart of gcc_jit_block_add_comment. */
5079 :
5080 : recording::statement *
5081 450 : recording::block::add_comment (recording::location *loc,
5082 : const char *text)
5083 : {
5084 450 : statement *result = new comment (this, loc, new_string (text));
5085 450 : m_ctxt->record (result);
5086 450 : m_statements.safe_push (result);
5087 450 : return result;
5088 : }
5089 :
5090 : /* Create a recording::extended_asm_simple instance and add it to
5091 : the block's context's list of mementos, and to the block's
5092 : list of statements.
5093 :
5094 : Implements the heart of gcc_jit_block_add_extended_asm. */
5095 :
5096 : recording::extended_asm *
5097 50 : recording::block::add_extended_asm (location *loc,
5098 : const char *asm_template)
5099 : {
5100 50 : extended_asm *result
5101 50 : = new extended_asm_simple (this, loc, new_string (asm_template));
5102 50 : m_ctxt->record (result);
5103 50 : m_statements.safe_push (result);
5104 50 : return result;
5105 : }
5106 :
5107 : /* Create a recording::end_with_conditional instance and add it to
5108 : the block's context's list of mementos, and to the block's
5109 : list of statements.
5110 :
5111 : Implements the heart of gcc_jit_block_end_with_conditional. */
5112 :
5113 : recording::statement *
5114 894 : recording::block::end_with_conditional (recording::location *loc,
5115 : recording::rvalue *boolval,
5116 : recording::block *on_true,
5117 : recording::block *on_false)
5118 : {
5119 894 : statement *result = new conditional (this, loc, boolval, on_true, on_false);
5120 894 : m_ctxt->record (result);
5121 894 : m_statements.safe_push (result);
5122 894 : m_has_been_terminated = true;
5123 894 : return result;
5124 : }
5125 :
5126 : /* Create a recording::end_with_jump instance and add it to
5127 : the block's context's list of mementos, and to the block's
5128 : list of statements.
5129 :
5130 : Implements the heart of gcc_jit_block_end_with_jump. */
5131 :
5132 : recording::statement *
5133 1202 : recording::block::end_with_jump (recording::location *loc,
5134 : recording::block *target)
5135 : {
5136 1202 : statement *result = new jump (this, loc, target);
5137 1202 : m_ctxt->record (result);
5138 1202 : m_statements.safe_push (result);
5139 1202 : m_has_been_terminated = true;
5140 1202 : return result;
5141 : }
5142 :
5143 : /* Create a recording::end_with_indirect_jump instance and add
5144 : it to the block's context's list of mementos, and to the
5145 : block's list of statements.
5146 :
5147 : Implements the heart of gcc_jit_block_end_with_indirect_jump. */
5148 :
5149 : recording::statement *
5150 20 : recording::block::end_with_indirect_jump (location *loc,
5151 : rvalue *target_address,
5152 : int num_destinations,
5153 : block **destinations)
5154 : {
5155 20 : statement *result = new indirect_jump (this, loc, target_address,
5156 20 : num_destinations, destinations);
5157 20 : m_ctxt->record (result);
5158 20 : m_statements.safe_push (result);
5159 20 : m_has_been_terminated = true;
5160 20 : return result;
5161 : }
5162 :
5163 : /* Create a recording::end_with_return instance and add it to
5164 : the block's context's list of mementos, and to the block's
5165 : list of statements.
5166 :
5167 : Implements the post-error-checking parts of
5168 : gcc_jit_block_end_with_return and
5169 : gcc_jit_block_end_with_void_return. */
5170 :
5171 : recording::statement *
5172 4030 : recording::block::end_with_return (recording::location *loc,
5173 : recording::rvalue *rvalue)
5174 : {
5175 : /* This is used by both gcc_jit_function_add_return and
5176 : gcc_jit_function_add_void_return; rvalue will be non-NULL for
5177 : the former and NULL for the latter. */
5178 4030 : statement *result = new return_ (this, loc, rvalue);
5179 4029 : m_ctxt->record (result);
5180 4030 : m_statements.safe_push (result);
5181 4030 : m_has_been_terminated = true;
5182 4030 : return result;
5183 : }
5184 :
5185 : /* Create a recording::switch_ instance and add it to
5186 : the block's context's list of mementos, and to the block's
5187 : list of statements.
5188 :
5189 : Implements the heart of gcc_jit_block_end_with_switch. */
5190 :
5191 : recording::statement *
5192 20 : recording::block::end_with_switch (recording::location *loc,
5193 : recording::rvalue *expr,
5194 : recording::block *default_block,
5195 : int num_cases,
5196 : recording::case_ **cases)
5197 : {
5198 20 : statement *result = new switch_ (this, loc,
5199 : expr,
5200 : default_block,
5201 : num_cases,
5202 20 : cases);
5203 20 : m_ctxt->record (result);
5204 20 : m_statements.safe_push (result);
5205 20 : m_has_been_terminated = true;
5206 20 : return result;
5207 : }
5208 :
5209 : /* Create a recording::extended_asm_goto instance and add it to
5210 : the block's context's list of mementos, and to the block's
5211 : list of statements.
5212 :
5213 : Implements the heart of gcc_jit_block_end_with_extended_asm_goto. */
5214 :
5215 :
5216 : recording::extended_asm *
5217 20 : recording::block::end_with_extended_asm_goto (location *loc,
5218 : const char *asm_template,
5219 : int num_goto_blocks,
5220 : block **goto_blocks,
5221 : block *fallthrough_block)
5222 : {
5223 20 : extended_asm *result
5224 : = new extended_asm_goto (this, loc, new_string (asm_template),
5225 : num_goto_blocks, goto_blocks,
5226 20 : fallthrough_block);
5227 20 : m_ctxt->record (result);
5228 20 : m_statements.safe_push (result);
5229 20 : m_has_been_terminated = true;
5230 20 : return result;
5231 : }
5232 :
5233 : /* Override the default implementation of
5234 : recording::memento::write_to_dump for blocks by writing
5235 : an unindented block name as a label, followed by the indented
5236 : statements:
5237 :
5238 : BLOCK_NAME:
5239 : STATEMENT_1;
5240 : STATEMENT_2;
5241 : ...
5242 : STATEMENT_N; */
5243 :
5244 : void
5245 44 : recording::block::write_to_dump (dump &d)
5246 : {
5247 44 : d.write ("%s:\n", get_debug_string ());
5248 :
5249 44 : int i;
5250 44 : statement *s;
5251 244 : FOR_EACH_VEC_ELT (m_statements, i, s)
5252 156 : s->write_to_dump (d);
5253 44 : }
5254 :
5255 : /* Validate a block by ensuring that it has been terminated. */
5256 :
5257 : bool
5258 6617 : recording::block::validate ()
5259 : {
5260 : /* Check for termination. */
5261 6617 : if (!has_been_terminated ())
5262 : {
5263 423 : statement *stmt = get_last_statement ();
5264 423 : location *loc = stmt ? stmt->get_loc () : NULL;
5265 423 : m_func->get_context ()->add_error (loc,
5266 : "unterminated block in %s: %s",
5267 423 : m_func->get_debug_string (),
5268 : get_debug_string ());
5269 423 : return false;
5270 : }
5271 :
5272 : return true;
5273 : }
5274 :
5275 : /* Get the source-location of a block by using that of the first
5276 : statement within it, if any. */
5277 :
5278 : recording::location *
5279 5 : recording::block::get_loc () const
5280 : {
5281 5 : recording::statement *stmt = get_first_statement ();
5282 5 : if (stmt)
5283 5 : return stmt->get_loc ();
5284 : else
5285 : return NULL;
5286 : }
5287 :
5288 : /* Create a recording::get_address instance and add it to
5289 : the block's context's list of mementos, and to the block's
5290 : list of statements.
5291 :
5292 : Implements the heart of gcc_jit_block_get_address. */
5293 :
5294 : recording::rvalue *
5295 55 : recording::block::get_address (location *loc)
5296 : {
5297 55 : recording::rvalue *result = new get_block_address (m_ctxt, loc, this);
5298 55 : m_ctxt->record (result);
5299 55 : return result;
5300 : }
5301 :
5302 : /* Get the first statement within a block, if any. */
5303 :
5304 : recording::statement *
5305 5 : recording::block::get_first_statement () const
5306 : {
5307 5 : if (m_statements.length ())
5308 5 : return m_statements[0];
5309 : else
5310 : return NULL;
5311 : }
5312 :
5313 : /* Get the last statement within a block, if any. */
5314 :
5315 : recording::statement *
5316 6556 : recording::block::get_last_statement () const
5317 : {
5318 6556 : if (m_statements.length ())
5319 6461 : return m_statements[m_statements.length () - 1];
5320 : else
5321 : return NULL;
5322 : }
5323 :
5324 : /* Assuming that this block has been terminated, get the successor blocks
5325 : as a vector. Ownership of the vector transfers to the caller, which
5326 : must call its release () method.
5327 :
5328 : Used when validating functions, and when dumping dot representations
5329 : of them. */
5330 :
5331 : vec <recording::block *>
5332 6128 : recording::block::get_successor_blocks () const
5333 : {
5334 6128 : gcc_assert (m_has_been_terminated);
5335 6128 : statement *last_statement = get_last_statement ();
5336 6128 : gcc_assert (last_statement);
5337 6128 : return last_statement->get_successor_blocks ();
5338 : }
5339 :
5340 : /* Implementation of pure virtual hook recording::memento::replay_into
5341 : for recording::block. */
5342 :
5343 : void
5344 7133 : recording::block::replay_into (replayer *)
5345 : {
5346 7133 : set_playback_obj (m_func->playback_function ()
5347 7133 : ->new_block (playback_string (m_name)));
5348 7133 : }
5349 :
5350 : /* Implementation of recording::memento::make_debug_string for
5351 : blocks. */
5352 :
5353 : recording::string *
5354 3758 : recording::block::make_debug_string ()
5355 : {
5356 3758 : if (m_name)
5357 : return m_name;
5358 : else
5359 1213 : return string::from_printf (m_ctxt,
5360 : "<UNNAMED BLOCK %p>",
5361 1213 : (void *)this);
5362 : }
5363 :
5364 : /* Implementation of recording::memento::write_reproducer for blocks. */
5365 :
5366 : void
5367 3762 : recording::block::write_reproducer (reproducer &r)
5368 : {
5369 3762 : const char *id = r.make_identifier (this, "block");
5370 3762 : r.write (" gcc_jit_block *%s =\n"
5371 : " gcc_jit_function_new_block (%s, %s);\n",
5372 : id,
5373 3762 : r.get_identifier (m_func),
5374 3762 : m_name ? m_name->get_debug_string () : "NULL");
5375 3762 : }
5376 :
5377 : /* Disable warnings about missing quoting in GCC diagnostics for
5378 : the pp_printf calls. Their format strings deliberately don't
5379 : follow GCC diagnostic conventions. */
5380 : #if __GNUC__ >= 10
5381 : # pragma GCC diagnostic push
5382 : # pragma GCC diagnostic ignored "-Wformat-diag"
5383 : #endif
5384 :
5385 : /* Dump a block in graphviz form into PP, capturing the block name (if
5386 : any) and the statements. */
5387 :
5388 : void
5389 0 : recording::block::dump_to_dot (pretty_printer *pp)
5390 : {
5391 0 : pp_printf (pp,
5392 : ("\tblock_%d "
5393 : "[shape=record,style=filled,fillcolor=white,label=\"{"),
5394 : m_index);
5395 0 : pp_write_text_to_stream (pp);
5396 0 : if (m_name)
5397 : {
5398 0 : pp_string (pp, m_name->c_str ());
5399 0 : pp_string (pp, ":");
5400 0 : pp_newline (pp);
5401 0 : pp_write_text_as_dot_label_to_stream (pp, true /*for_record*/);
5402 : }
5403 :
5404 0 : int i;
5405 0 : statement *s;
5406 0 : FOR_EACH_VEC_ELT (m_statements, i, s)
5407 : {
5408 0 : pp_string (pp, s->get_debug_string ());
5409 0 : pp_newline (pp);
5410 0 : pp_write_text_as_dot_label_to_stream (pp, true /*for_record*/);
5411 : }
5412 :
5413 0 : pp_string (pp,
5414 : "}\"];\n\n");
5415 0 : pp_flush (pp);
5416 0 : }
5417 :
5418 : /* Dump the out-edges of the block in graphviz form into PP. */
5419 :
5420 : void
5421 0 : recording::block::dump_edges_to_dot (pretty_printer *pp)
5422 : {
5423 0 : vec <block *> successors = get_successor_blocks ();
5424 0 : int i;
5425 0 : block *succ;
5426 0 : FOR_EACH_VEC_ELT (successors, i, succ)
5427 0 : pp_printf (pp,
5428 : "\tblock_%d:s -> block_%d:n;\n",
5429 : m_index, succ->m_index);
5430 0 : successors.release ();
5431 0 : }
5432 :
5433 : #if __GNUC__ >= 10
5434 : # pragma GCC diagnostic pop
5435 : #endif
5436 :
5437 : namespace recording {
5438 : static const enum tls_model tls_models[] = {
5439 : TLS_MODEL_NONE, /* GCC_JIT_TLS_MODEL_NONE */
5440 : TLS_MODEL_GLOBAL_DYNAMIC, /* GCC_JIT_TLS_MODEL_GLOBAL_DYNAMIC */
5441 : TLS_MODEL_LOCAL_DYNAMIC, /* GCC_JIT_TLS_MODEL_LOCAL_DYNAMIC */
5442 : TLS_MODEL_INITIAL_EXEC, /* GCC_JIT_TLS_MODEL_INITIAL_EXEC */
5443 : TLS_MODEL_LOCAL_EXEC, /* GCC_JIT_TLS_MODEL_LOCAL_EXEC */
5444 : };
5445 : } /* namespace recording */
5446 :
5447 : /* The implementation of class gcc::jit::recording::global. */
5448 :
5449 : /* Implementation of pure virtual hook recording::memento::replay_into
5450 : for recording::global. */
5451 :
5452 : void
5453 1376 : recording::global::replay_into (replayer *r)
5454 : {
5455 1376 : playback::lvalue *global = m_initializer
5456 1391 : ? r->new_global_initialized (playback_location (r, m_loc),
5457 : m_kind,
5458 15 : m_type->playback_type (),
5459 15 : m_type->dereference ()->get_size (),
5460 15 : m_initializer_num_bytes
5461 15 : / m_type->dereference ()->get_size (),
5462 15 : m_initializer,
5463 : playback_string (m_name),
5464 : m_flags,
5465 15 : m_string_attributes,
5466 : m_readonly)
5467 1361 : : r->new_global (playback_location (r, m_loc),
5468 : m_kind,
5469 1361 : m_type->playback_type (),
5470 : playback_string (m_name),
5471 : m_flags,
5472 1361 : m_string_attributes,
5473 : m_readonly);
5474 :
5475 1376 : if (m_tls_model != GCC_JIT_TLS_MODEL_NONE)
5476 20 : global->set_tls_model (recording::tls_models[m_tls_model]);
5477 :
5478 1376 : if (m_link_section != NULL)
5479 5 : global->set_link_section (m_link_section->c_str ());
5480 :
5481 1376 : if (m_reg_name != NULL)
5482 15 : global->set_register_name (m_reg_name->c_str ());
5483 :
5484 1376 : if (m_alignment != 0)
5485 5 : global->set_alignment (m_alignment);
5486 :
5487 1376 : set_playback_obj (global);
5488 1376 : }
5489 :
5490 : /* Override the default implementation of
5491 : recording::memento::write_to_dump for globals.
5492 : This will be of the form:
5493 :
5494 : GCC_JIT_GLOBAL_EXPORTED:
5495 : "TYPE NAME;"
5496 : e.g. "int foo;"
5497 :
5498 : GCC_JIT_GLOBAL_INTERNAL:
5499 : "static TYPE NAME;"
5500 : e.g. "static int foo;"
5501 :
5502 : GCC_JIT_GLOBAL_IMPORTED:
5503 : "extern TYPE NAME;"
5504 : e.g. "extern int foo;"
5505 :
5506 : These are written to the top of the dump by
5507 : recording::context::dump_to_file. */
5508 :
5509 : void
5510 0 : recording::global::write_to_dump (dump &d)
5511 : {
5512 0 : if (d.update_locations ())
5513 0 : m_loc = d.make_location ();
5514 :
5515 0 : switch (m_kind)
5516 : {
5517 0 : default:
5518 0 : gcc_unreachable ();
5519 :
5520 : case GCC_JIT_GLOBAL_EXPORTED:
5521 : break;
5522 :
5523 0 : case GCC_JIT_GLOBAL_INTERNAL:
5524 0 : d.write ("static ");
5525 0 : break;
5526 :
5527 0 : case GCC_JIT_GLOBAL_IMPORTED:
5528 0 : d.write ("extern ");
5529 0 : break;
5530 : }
5531 :
5532 0 : for (auto attr: m_string_attributes)
5533 : {
5534 0 : gcc_jit_variable_attribute& name = std::get<0>(attr);
5535 0 : std::string& value = std::get<1>(attr);
5536 0 : const char* attribute = variable_attribute_to_string (name);
5537 :
5538 0 : if (attribute)
5539 0 : d.write ("__attribute(%s(\"%s\"))__\n", attribute, value.c_str());
5540 0 : }
5541 0 : d.write ("%s %s",
5542 0 : m_type->get_debug_string (),
5543 : get_debug_string ());
5544 :
5545 0 : if (!m_initializer && !m_rvalue_init)
5546 : {
5547 0 : d.write (";\n");
5548 : }
5549 0 : else if (m_initializer)
5550 : {
5551 0 : d.write ("=\n { ");
5552 0 : const unsigned char *p = (const unsigned char *)m_initializer;
5553 0 : for (size_t i = 0; i < m_initializer_num_bytes; i++)
5554 : {
5555 0 : d.write ("0x%x, ", p[i]);
5556 0 : if (i && !(i % 64))
5557 0 : d.write ("\n ");
5558 : }
5559 0 : d.write ("};\n");
5560 : }
5561 0 : else if (m_rvalue_init)
5562 : {
5563 0 : d.write (" = ");
5564 0 : d.write ("%s", m_rvalue_init->get_debug_string ());
5565 0 : d.write (";\n");
5566 : }
5567 :
5568 0 : return;
5569 : }
5570 :
5571 : /* A table of enum gcc_jit_global_kind values expressed in string
5572 : form. */
5573 :
5574 : static const char * const global_kind_reproducer_strings[] = {
5575 : "GCC_JIT_GLOBAL_EXPORTED",
5576 : "GCC_JIT_GLOBAL_INTERNAL",
5577 : "GCC_JIT_GLOBAL_IMPORTED"
5578 : };
5579 :
5580 : template <typename T>
5581 : void
5582 15 : recording::global::write_initializer_reproducer (const char *id, reproducer &r)
5583 : {
5584 15 : const char *init_id = r.make_tmp_identifier ("init_for", this);
5585 15 : r.write (" %s %s[] =\n {",
5586 15 : m_type->dereference ()->get_debug_string (),
5587 : init_id);
5588 :
5589 15 : const T *p = (const T *)m_initializer;
5590 20555 : for (size_t i = 0; i < m_initializer_num_bytes / sizeof (T); i++)
5591 : {
5592 20540 : r.write ("%" PRIu64 ", ", (uint64_t)p[i]);
5593 20540 : if (i && !(i % 64))
5594 315 : r.write ("\n ");
5595 : }
5596 15 : r.write ("};\n");
5597 15 : r.write (" gcc_jit_global_set_initializer (%s, %s, sizeof (%s));\n",
5598 : id, init_id, init_id);
5599 15 : }
5600 :
5601 : /* Implementation of recording::memento::write_reproducer for globals. */
5602 :
5603 : static const char * const tls_model_enum_strings[] = {
5604 : "GCC_JIT_TLS_MODEL_NONE",
5605 : "GCC_JIT_TLS_MODEL_GLOBAL_DYNAMIC",
5606 : "GCC_JIT_TLS_MODEL_LOCAL_DYNAMIC",
5607 : "GCC_JIT_TLS_MODEL_INITIAL_EXEC",
5608 : "GCC_JIT_TLS_MODEL_LOCAL_EXEC",
5609 : };
5610 :
5611 : static const char * const gcc_jit_variable_attribute_enum_strings[] = {
5612 : "GCC_JIT_VARIABLE_ATTRIBUTE_VISIBILITY",
5613 : };
5614 :
5615 : void
5616 2163 : recording::global::write_reproducer (reproducer &r)
5617 : {
5618 2163 : const char *id = r.make_identifier (this, "block");
5619 4326 : r.write (" gcc_jit_lvalue *%s =\n"
5620 : " gcc_jit_context_new_global (%s, /* gcc_jit_context *ctxt */\n"
5621 : " %s, /* gcc_jit_location *loc */\n"
5622 : " %s, /* enum gcc_jit_global_kind kind */\n"
5623 : " %s, /* gcc_jit_type *type */\n"
5624 : " %s); /* const char *name */\n",
5625 : id,
5626 : r.get_identifier (get_context ()),
5627 2163 : r.get_identifier (m_loc),
5628 2163 : global_kind_reproducer_strings[m_kind],
5629 : r.get_identifier_as_type (get_type ()),
5630 2163 : m_name->get_debug_string ());
5631 :
5632 2163 : if (m_tls_model != GCC_JIT_TLS_MODEL_NONE)
5633 10 : r.write (" gcc_jit_lvalue_set_tls_model (%s, /* gcc_jit_lvalue *lvalue */\n"
5634 : " %s); /* enum gcc_jit_tls_model model */\n",
5635 : id,
5636 10 : tls_model_enum_strings[m_tls_model]);
5637 :
5638 2163 : if (m_link_section != NULL)
5639 5 : r.write (" gcc_jit_lvalue_set_link_section (%s, /* gcc_jit_lvalue *lvalue */\n"
5640 : " \"%s\"); /* */\n",
5641 : id,
5642 5 : m_link_section->c_str ());
5643 :
5644 2168 : for (auto attribute : m_string_attributes)
5645 5 : r.write(" gcc_jit_lvalue_add_string_attribute (%s, %s, \"%s\");\n",
5646 : id,
5647 5 : gcc_jit_variable_attribute_enum_strings[std::get<0>(attribute)],
5648 10 : std::get<1>(attribute).c_str());
5649 :
5650 2163 : if (m_readonly)
5651 10 : r.write (" gcc_jit_global_set_readonly (%s /* gcc_jit_lvalue *lvalue */);\n",
5652 : id);
5653 :
5654 2163 : if (m_initializer)
5655 15 : switch (m_type->dereference ()->get_size ())
5656 : {
5657 10 : case 1:
5658 10 : write_initializer_reproducer<uint8_t> (id, r);
5659 10 : break;
5660 0 : case 2:
5661 0 : write_initializer_reproducer<uint16_t> (id, r);
5662 0 : break;
5663 5 : case 4:
5664 5 : write_initializer_reproducer<uint32_t> (id, r);
5665 5 : break;
5666 0 : case 8:
5667 0 : write_initializer_reproducer<uint64_t> (id, r);
5668 0 : break;
5669 0 : default:
5670 : /* This function is serving on sizes returned by 'get_size',
5671 : these are all covered by the previous cases. */
5672 0 : gcc_unreachable ();
5673 : }
5674 2163 : }
5675 :
5676 : /* The implementation of the various const-handling classes:
5677 : gcc::jit::recording::memento_of_new_rvalue_from_const <HOST_TYPE>. */
5678 :
5679 : /* Explicit specialization of the various mementos we're interested in. */
5680 : template class recording::memento_of_new_rvalue_from_const <int>;
5681 : template class recording::memento_of_new_rvalue_from_const <long>;
5682 : template class recording::memento_of_new_rvalue_from_const <double>;
5683 : template class recording::memento_of_new_rvalue_from_const <void *>;
5684 :
5685 : /* Implementation of the pure virtual hook recording::memento::replay_into
5686 : for recording::memento_of_new_rvalue_from_const <HOST_TYPE>. */
5687 :
5688 : template <typename HOST_TYPE>
5689 : void
5690 8259 : recording::
5691 : memento_of_new_rvalue_from_const <HOST_TYPE>::replay_into (replayer *r)
5692 : {
5693 : set_playback_obj
5694 8259 : (r->new_rvalue_from_const <HOST_TYPE> (m_type->playback_type (),
5695 : m_value));
5696 8259 : }
5697 :
5698 : /* The make_debug_string and write_reproducer methods vary between the
5699 : various
5700 : memento_of_new_rvalue_from_const <HOST_TYPE>
5701 : classes, so we explicitly write specializations of them.
5702 :
5703 : I (dmalcolm) find the code to be clearer if the "recording" vs "playback"
5704 : namespaces are written out explicitly, which is why most of this file
5705 : doesn't abbreviate things by entering the "recording" namespace.
5706 :
5707 : However, these specializations are required to be in the same namespace
5708 : as the template, hence we now have to enter the gcc::jit::recording
5709 : namespace. */
5710 :
5711 : namespace recording
5712 : {
5713 :
5714 : /* The make_debug_string specialization for <int>, which renders it as
5715 : (TARGET_TYPE)LITERAL
5716 : e.g.
5717 : "(int)42". */
5718 :
5719 : template <>
5720 : string *
5721 4805 : memento_of_new_rvalue_from_const <int>::make_debug_string ()
5722 : {
5723 4805 : return string::from_printf (m_ctxt,
5724 : "(%s)%i",
5725 4805 : m_type->get_debug_string (),
5726 4805 : m_value);
5727 : }
5728 :
5729 : /* The get_wide_int specialization for <int>. */
5730 :
5731 : template <>
5732 : bool
5733 1470 : memento_of_new_rvalue_from_const <int>::get_wide_int (wide_int *out) const
5734 : {
5735 1470 : *out = wi::shwi (m_value, sizeof (m_value) * 8);
5736 1470 : return true;
5737 : }
5738 :
5739 : /* The write_reproducer specialization for <int>. */
5740 :
5741 : template <>
5742 : void
5743 4770 : memento_of_new_rvalue_from_const <int>::write_reproducer (reproducer &r)
5744 : {
5745 4770 : const char *id = r.make_identifier (this, "rvalue");
5746 4770 : r.write (" gcc_jit_rvalue *%s =\n"
5747 : " gcc_jit_context_new_rvalue_from_int (%s, /* gcc_jit_context *ctxt */\n"
5748 : " %s, /* gcc_jit_type *numeric_type */\n"
5749 : " %i); /* int value */\n",
5750 : id,
5751 : r.get_identifier (get_context ()),
5752 : r.get_identifier_as_type (m_type),
5753 : m_value);
5754 4770 : }
5755 :
5756 : /* The make_debug_string specialization for <long>, rendering it as
5757 : (TARGET_TYPE)LITERAL
5758 : e.g.
5759 : "(long)42". */
5760 :
5761 : template <>
5762 : string *
5763 70 : memento_of_new_rvalue_from_const <long>::make_debug_string ()
5764 : {
5765 70 : return string::from_printf (m_ctxt,
5766 : "(%s)%li",
5767 70 : m_type->get_debug_string (),
5768 70 : m_value);
5769 : }
5770 :
5771 : /* The get_wide_int specialization for <long>. */
5772 :
5773 : template <>
5774 : bool
5775 0 : memento_of_new_rvalue_from_const <long>::get_wide_int (wide_int *out) const
5776 : {
5777 0 : *out = wi::shwi (m_value, sizeof (m_value) * 8);
5778 0 : return true;
5779 : }
5780 :
5781 : /* The write_reproducer specialization for <long>. */
5782 :
5783 : template <>
5784 : void
5785 55 : recording::memento_of_new_rvalue_from_const <long>::write_reproducer (reproducer &r)
5786 : {
5787 55 : const char *id = r.make_identifier (this, "rvalue");
5788 :
5789 : /* We have to special-case LONG_MIN, since e.g.
5790 : -9223372036854775808L
5791 : is parsed as
5792 : -(9223372036854775808L)
5793 : and hence we'd get:
5794 : error: integer constant is so large that it is unsigned [-Werror]
5795 : Workaround this by writing (LONG_MIN + 1) - 1. */
5796 55 : if (m_value == LONG_MIN)
5797 : {
5798 10 : r.write (" gcc_jit_rvalue *%s =\n"
5799 : " gcc_jit_context_new_rvalue_from_long (%s, /* gcc_jit_context *ctxt */\n"
5800 : " %s, /* gcc_jit_type *numeric_type */\n"
5801 : " %ldL - 1); /* long value */\n",
5802 : id,
5803 : r.get_identifier (get_context ()),
5804 : r.get_identifier_as_type (m_type),
5805 : m_value + 1);
5806 10 : return;
5807 : }
5808 :
5809 45 : r.write (" gcc_jit_rvalue *%s =\n"
5810 : " gcc_jit_context_new_rvalue_from_long (%s, /* gcc_jit_context *ctxt */\n"
5811 : " %s, /* gcc_jit_type *numeric_type */\n"
5812 : " %ldL); /* long value */\n",
5813 : id,
5814 : r.get_identifier (get_context ()),
5815 : r.get_identifier_as_type (m_type),
5816 : m_value);
5817 : }
5818 :
5819 : /* The make_debug_string specialization for <double>, rendering it as
5820 : (TARGET_TYPE)LITERAL
5821 : e.g.
5822 : "(float)42.0". */
5823 :
5824 : template <>
5825 : string *
5826 185 : memento_of_new_rvalue_from_const <double>::make_debug_string ()
5827 : {
5828 185 : return string::from_printf (m_ctxt,
5829 : "(%s)%f",
5830 185 : m_type->get_debug_string (),
5831 185 : m_value);
5832 : }
5833 :
5834 : /* The get_wide_int specialization for <double>. */
5835 :
5836 : template <>
5837 : bool
5838 0 : memento_of_new_rvalue_from_const <double>::get_wide_int (wide_int *) const
5839 : {
5840 0 : return false;
5841 : }
5842 :
5843 : /* The write_reproducer specialization for <double>. */
5844 :
5845 : template <>
5846 : void
5847 165 : recording::memento_of_new_rvalue_from_const <double>::write_reproducer (reproducer &r)
5848 : {
5849 165 : const char *id = r.make_identifier (this, "rvalue");
5850 165 : r.write (" gcc_jit_rvalue *%s =\n"
5851 : " gcc_jit_context_new_rvalue_from_double (%s, /* gcc_jit_context *ctxt */\n"
5852 : " %s, /* gcc_jit_type *numeric_type */\n"
5853 : " %f); /* double value */\n",
5854 : id,
5855 : r.get_identifier (get_context ()),
5856 : r.get_identifier_as_type (m_type),
5857 : m_value);
5858 165 : }
5859 :
5860 : /* The make_debug_string specialization for <void *>, rendering it as
5861 : (TARGET_TYPE)HEX
5862 : e.g.
5863 : "(int *)0xdeadbeef"
5864 :
5865 : Zero is rendered as NULL e.g.
5866 : "(int *)NULL". */
5867 :
5868 : template <>
5869 : string *
5870 120 : memento_of_new_rvalue_from_const <void *>::make_debug_string ()
5871 : {
5872 120 : if (m_value != NULL)
5873 55 : return string::from_printf (m_ctxt,
5874 : "(%s)%p",
5875 110 : m_type->get_debug_string (), m_value);
5876 : else
5877 65 : return string::from_printf (m_ctxt,
5878 : "(%s)NULL",
5879 130 : m_type->get_debug_string ());
5880 : }
5881 :
5882 : /* The get_wide_int specialization for <void *>. */
5883 :
5884 : template <>
5885 : bool
5886 0 : memento_of_new_rvalue_from_const <void *>::get_wide_int (wide_int *) const
5887 : {
5888 0 : return false;
5889 : }
5890 :
5891 : /* Implementation of recording::memento::write_reproducer for <void *>
5892 : values. */
5893 :
5894 : template <>
5895 : void
5896 110 : memento_of_new_rvalue_from_const <void *>::write_reproducer (reproducer &r)
5897 : {
5898 110 : const char *id = r.make_identifier (this, "rvalue");
5899 110 : if (m_value)
5900 50 : r.write (" gcc_jit_rvalue *%s =\n"
5901 : " gcc_jit_context_new_rvalue_from_ptr (%s, /* gcc_jit_context *ctxt */\n"
5902 : " %s, /* gcc_jit_type *pointer_type */\n"
5903 : " (void *)%p); /* void *value */\n",
5904 : id,
5905 : r.get_identifier (get_context ()),
5906 : r.get_identifier_as_type (m_type),
5907 : m_value);
5908 : else
5909 60 : r.write (" gcc_jit_rvalue *%s =\n"
5910 : " gcc_jit_context_null (%s, /* gcc_jit_context *ctxt */\n"
5911 : " %s); /* gcc_jit_type *pointer_type */\n",
5912 : id,
5913 : r.get_identifier (get_context ()),
5914 : r.get_identifier_as_type (m_type));
5915 110 : }
5916 :
5917 : /* We're done specializing make_debug_string and write_reproducer, so we
5918 : can exit the gcc::jit::recording namespace. */
5919 :
5920 : } // namespace recording
5921 :
5922 : /* The implementation of class gcc::jit::recording::memento_of_typeinfo. */
5923 :
5924 : /* Implementation of pure virtual hook recording::memento::replay_into
5925 : for recording::memento_of_typeinfo. */
5926 :
5927 : void
5928 40 : recording::memento_of_typeinfo::replay_into (replayer *r)
5929 : {
5930 40 : switch (m_info_type)
5931 : {
5932 20 : case TYPE_INFO_ALIGN_OF:
5933 20 : set_playback_obj (r->new_alignof (m_type->playback_type ()));
5934 20 : break;
5935 20 : case TYPE_INFO_SIZE_OF:
5936 20 : set_playback_obj (r->new_sizeof (m_type->playback_type ()));
5937 20 : break;
5938 : }
5939 40 : }
5940 :
5941 : /* Implementation of recording::memento::make_debug_string for
5942 : sizeof expressions. */
5943 :
5944 : recording::string *
5945 20 : recording::memento_of_typeinfo::make_debug_string ()
5946 : {
5947 20 : const char* ident = "";
5948 20 : switch (m_info_type)
5949 : {
5950 10 : case TYPE_INFO_ALIGN_OF:
5951 10 : ident = "_Alignof";
5952 10 : break;
5953 10 : case TYPE_INFO_SIZE_OF:
5954 10 : ident = "sizeof";
5955 10 : break;
5956 : }
5957 20 : return string::from_printf (m_ctxt,
5958 : "%s (%s)",
5959 : ident,
5960 20 : m_type->get_debug_string ());
5961 : }
5962 :
5963 : /* Implementation of recording::memento::write_reproducer for sizeof
5964 : expressions. */
5965 :
5966 : void
5967 20 : recording::memento_of_typeinfo::write_reproducer (reproducer &r)
5968 : {
5969 20 : const char* type = "";
5970 20 : switch (m_info_type)
5971 : {
5972 10 : case TYPE_INFO_ALIGN_OF:
5973 10 : type = "align";
5974 10 : break;
5975 10 : case TYPE_INFO_SIZE_OF:
5976 10 : type = "size";
5977 10 : break;
5978 : }
5979 20 : const char *id = r.make_identifier (this, "rvalue");
5980 20 : r.write (" gcc_jit_rvalue *%s =\n"
5981 : " gcc_jit_context_new_%sof (%s, /* gcc_jit_context *ctxt */\n"
5982 : " (gcc_jit_type *) %s); /* gcc_jit_type *type */\n",
5983 : id,
5984 : type,
5985 : r.get_identifier (get_context ()),
5986 20 : r.get_identifier (m_type));
5987 20 : }
5988 :
5989 : /* The implementation of class gcc::jit::recording::memento_of_new_string_literal. */
5990 :
5991 : /* Implementation of pure virtual hook recording::memento::replay_into
5992 : for recording::memento_of_new_string_literal. */
5993 :
5994 : void
5995 242 : recording::memento_of_new_string_literal::replay_into (replayer *r)
5996 : {
5997 242 : set_playback_obj (r->new_string_literal (m_value->c_str ()));
5998 242 : }
5999 :
6000 : /* Implementation of recording::memento::make_debug_string for
6001 : string literals. */
6002 :
6003 : recording::string *
6004 1931 : recording::memento_of_new_string_literal::make_debug_string ()
6005 : {
6006 1931 : return string::from_printf (m_ctxt,
6007 : "%s",
6008 1931 : m_value->get_debug_string ());
6009 : }
6010 :
6011 : /* Implementation of recording::memento::write_reproducer for string literal
6012 : values. */
6013 :
6014 : void
6015 1906 : recording::memento_of_new_string_literal::write_reproducer (reproducer &r)
6016 : {
6017 1906 : const char *id = r.make_identifier (this, "rvalue");
6018 1906 : r.write (" gcc_jit_rvalue *%s =\n"
6019 : " gcc_jit_context_new_string_literal (%s, /* gcc_jit_context *ctxt */\n"
6020 : " %s); /* const char *value */\n",
6021 : id,
6022 : r.get_identifier (get_context ()),
6023 1906 : m_value->get_debug_string ());
6024 1906 : }
6025 :
6026 : /* The implementation of class
6027 : gcc::jit::recording::memento_of_new_rvalue_from_vector. */
6028 :
6029 : /* The constructor for
6030 : gcc::jit::recording::memento_of_new_rvalue_from_vector. */
6031 :
6032 135 : recording::memento_of_new_rvalue_from_vector::
6033 : memento_of_new_rvalue_from_vector (context *ctxt,
6034 : location *loc,
6035 : vector_type *type,
6036 : rvalue **elements)
6037 : : rvalue (ctxt, loc, type),
6038 135 : m_vector_type (type),
6039 135 : m_elements ()
6040 : {
6041 675 : for (unsigned i = 0; i < type->get_num_units (); i++)
6042 540 : m_elements.safe_push (elements[i]);
6043 135 : }
6044 :
6045 : /* Implementation of pure virtual hook recording::memento::replay_into
6046 : for recording::memento_of_new_rvalue_from_vector. */
6047 :
6048 : void
6049 160 : recording::memento_of_new_rvalue_from_vector::replay_into (replayer *r)
6050 : {
6051 160 : auto_vec<playback::rvalue *> playback_elements;
6052 160 : playback_elements.create (m_elements.length ());
6053 960 : for (unsigned i = 0; i< m_elements.length (); i++)
6054 640 : playback_elements.safe_push (m_elements[i]->playback_rvalue ());
6055 :
6056 160 : set_playback_obj (r->new_rvalue_from_vector (playback_location (r, m_loc),
6057 160 : m_type->playback_type (),
6058 : playback_elements));
6059 160 : }
6060 :
6061 : /* Implementation of pure virtual hook recording::rvalue::visit_children
6062 : for recording::memento_of_new_rvalue_from_vector. */
6063 :
6064 : void
6065 135 : recording::memento_of_new_rvalue_from_vector::visit_children (rvalue_visitor *v)
6066 : {
6067 675 : for (unsigned i = 0; i< m_elements.length (); i++)
6068 540 : v->visit (m_elements[i]);
6069 135 : }
6070 :
6071 : /* Implementation of recording::memento::make_debug_string for
6072 : vectors. */
6073 :
6074 : recording::string *
6075 115 : recording::memento_of_new_rvalue_from_vector::make_debug_string ()
6076 : {
6077 115 : comma_separated_string elements (m_elements, get_precedence ());
6078 :
6079 : /* Now build a string. */
6080 115 : string *result = string::from_printf (m_ctxt,
6081 : "{%s}",
6082 : elements.as_char_ptr ());
6083 :
6084 230 : return result;
6085 :
6086 115 : }
6087 :
6088 : /* Implementation of recording::memento::write_reproducer for
6089 : vectors. */
6090 :
6091 : void
6092 110 : recording::memento_of_new_rvalue_from_vector::write_reproducer (reproducer &r)
6093 : {
6094 110 : const char *id = r.make_identifier (this, "vector");
6095 110 : const char *elements_id = r.make_tmp_identifier ("elements_for_", this);
6096 110 : r.write (" gcc_jit_rvalue *%s[%i] = {\n",
6097 : elements_id,
6098 : m_elements.length ());
6099 660 : for (unsigned i = 0; i< m_elements.length (); i++)
6100 440 : r.write (" %s,\n", r.get_identifier_as_rvalue (m_elements[i]));
6101 110 : r.write (" };\n");
6102 220 : r.write (" gcc_jit_rvalue *%s =\n"
6103 : " gcc_jit_context_new_rvalue_from_vector (%s, /* gcc_jit_context *ctxt */\n"
6104 : " %s, /* gcc_jit_location *loc */\n"
6105 : " %s, /* gcc_jit_type *vec_type */\n"
6106 : " %i, /* size_t num_elements */ \n"
6107 : " %s); /* gcc_jit_rvalue **elements*/\n",
6108 : id,
6109 : r.get_identifier (get_context ()),
6110 110 : r.get_identifier (m_loc),
6111 110 : r.get_identifier (m_vector_type),
6112 : m_elements.length (),
6113 : elements_id);
6114 110 : }
6115 :
6116 : /* The implementation of class
6117 : gcc::jit::recording::memento_of_new_rvalue_vector_perm. */
6118 :
6119 : /* The constructor for
6120 : gcc::jit::recording::memento_of_new_rvalue_vector_perm. */
6121 :
6122 15 : recording::memento_of_new_rvalue_vector_perm::
6123 : memento_of_new_rvalue_vector_perm (context *ctxt,
6124 : location *loc,
6125 : rvalue *elements1,
6126 : rvalue *elements2,
6127 : rvalue *mask)
6128 : : rvalue (ctxt, loc, elements1->get_type ()),
6129 15 : m_elements1 (elements1),
6130 15 : m_elements2 (elements2),
6131 15 : m_mask (mask)
6132 : {
6133 15 : }
6134 :
6135 : /* Implementation of pure virtual hook recording::memento::replay_into
6136 : for recording::memento_of_new_rvalue_vector_perm. */
6137 :
6138 : void
6139 20 : recording::memento_of_new_rvalue_vector_perm::replay_into (replayer *r)
6140 : {
6141 20 : playback::rvalue *playback_elements1 = m_elements1->playback_rvalue ();
6142 20 : playback::rvalue *playback_elements2 = m_elements2->playback_rvalue ();
6143 20 : playback::rvalue *playback_mask = m_mask->playback_rvalue ();
6144 :
6145 20 : set_playback_obj (r->new_rvalue_vector_perm (playback_location (r, m_loc),
6146 : playback_elements1,
6147 : playback_elements2,
6148 : playback_mask));
6149 20 : }
6150 :
6151 : /* Implementation of pure virtual hook recording::rvalue::visit_children
6152 : for recording::memento_of_new_rvalue_from_vector. */
6153 :
6154 : void
6155 15 : recording::memento_of_new_rvalue_vector_perm::visit_children (rvalue_visitor *v)
6156 : {
6157 15 : v->visit (m_elements1);
6158 15 : v->visit (m_elements2);
6159 15 : v->visit (m_mask);
6160 15 : }
6161 :
6162 : /* Implementation of recording::memento::make_debug_string for
6163 : vectors. */
6164 :
6165 : recording::string *
6166 10 : recording::memento_of_new_rvalue_vector_perm::make_debug_string ()
6167 : {
6168 : /* Now build a string. */
6169 10 : string *result = string::from_printf (m_ctxt,
6170 : "shufflevector (%s, %s, %s)",
6171 10 : m_elements1->get_debug_string (),
6172 10 : m_elements2->get_debug_string (),
6173 10 : m_mask->get_debug_string ());
6174 :
6175 10 : return result;
6176 :
6177 : }
6178 :
6179 : /* Implementation of recording::memento::write_reproducer for
6180 : vectors. */
6181 :
6182 : void
6183 10 : recording::memento_of_new_rvalue_vector_perm::write_reproducer (reproducer &r)
6184 : {
6185 10 : const char *id = r.make_identifier (this, "vector");
6186 20 : r.write (" gcc_jit_rvalue *%s =\n"
6187 : " gcc_jit_context_new_rvalue_vector_perm (%s, /* gcc_jit_context *ctxt */\n"
6188 : " %s, /* gcc_jit_location *loc */\n"
6189 : " %s, /* gcc_jit_rvalue **elements1*/\n"
6190 : " %s, /* gcc_jit_rvalue **elements2*/\n"
6191 : " %s); /* gcc_jit_rvalue **mask*/\n",
6192 : id,
6193 : r.get_identifier (get_context ()),
6194 10 : r.get_identifier (m_loc),
6195 : r.get_identifier_as_rvalue (m_elements1),
6196 : r.get_identifier_as_rvalue (m_elements2),
6197 : r.get_identifier_as_rvalue (m_mask));
6198 10 : }
6199 :
6200 : void
6201 685 : recording::ctor::visit_children (rvalue_visitor *v)
6202 : {
6203 2090 : for (unsigned int i = 0; i < m_values.length (); i++)
6204 1405 : if (m_values[i])
6205 1240 : v->visit (m_values[i]);
6206 685 : }
6207 :
6208 : recording::string *
6209 455 : recording::ctor::make_debug_string ()
6210 : {
6211 : //Make a compound literal-ish
6212 455 : pretty_printer pp;
6213 :
6214 455 : pp_string (&pp, "(");
6215 455 : pp_string (&pp, m_type->get_debug_string ());
6216 455 : pp_string (&pp, ") {");
6217 :
6218 455 : size_t field_n = m_fields.length ();
6219 455 : size_t values_n = m_values.length ();
6220 :
6221 455 : if (!field_n && !values_n)
6222 : ;
6223 415 : else if (!field_n && values_n)
6224 : {
6225 690 : for (size_t i = 0; i < values_n; i++)
6226 : {
6227 525 : if (m_values[i])
6228 475 : pp_string (&pp, m_values[i]->get_debug_string ());
6229 : else
6230 50 : pp_string (&pp, "0");
6231 525 : if (i + 1 != values_n)
6232 360 : pp_string (&pp, ", ");
6233 : }
6234 : }
6235 250 : else if (field_n && values_n)
6236 : {
6237 660 : for (size_t i = 0; i < values_n; i++)
6238 : {
6239 410 : pp_string (&pp, ".");
6240 410 : pp_string (&pp, m_fields[i]->get_debug_string ());
6241 410 : pp_string (&pp, "=");
6242 410 : if (m_values[i])
6243 350 : pp_string (&pp, m_values[i]->get_debug_string ());
6244 : else
6245 60 : pp_string (&pp, "0");
6246 410 : if (i + 1 != values_n)
6247 160 : pp_string (&pp, ", ");
6248 : }
6249 : }
6250 : /* m_fields are never populated with m_values empty. */
6251 :
6252 455 : pp_string (&pp, "}");
6253 :
6254 455 : return new_string (pp_formatted_text (&pp));
6255 455 : }
6256 :
6257 : void
6258 455 : recording::ctor::write_reproducer (reproducer &r)
6259 : {
6260 455 : const char *id = r.make_identifier (this, "rvalue");
6261 455 : type *type = get_type ();
6262 :
6263 455 : r.write (" gcc_jit_rvalue *%s;\n", id);
6264 455 : r.write (" {\n"); /* Open scope for locals. */
6265 :
6266 455 : if (type->is_union ())
6267 : {
6268 50 : if (m_values.length () == 0)
6269 10 : r.write (" gcc_jit_rvalue *value = NULL;\n");
6270 : else
6271 40 : r.write (" gcc_jit_rvalue *value = %s;\n",
6272 40 : r.get_identifier_as_rvalue (m_values[0]));
6273 :
6274 50 : if (m_fields.length () == 0)
6275 10 : r.write (" gcc_jit_field *field = NULL;\n");
6276 : else
6277 40 : r.write (" gcc_jit_field *field = %s;\n",
6278 40 : r.get_identifier (m_fields[0]));
6279 : }
6280 : else
6281 : {
6282 : /* Write the array of values. */
6283 405 : if (m_values.length () == 0)
6284 30 : r.write (" gcc_jit_rvalue **values = NULL;\n");
6285 : else
6286 : {
6287 375 : r.write (" gcc_jit_rvalue *values[] = {\n");
6288 1645 : for (size_t i = 0; i < m_values.length (); i++)
6289 895 : r.write (" %s,\n", r.get_identifier_as_rvalue (m_values[i]));
6290 375 : r.write (" };\n");
6291 : }
6292 : /* Write the array of fields. */
6293 405 : if (m_fields.length () == 0)
6294 195 : r.write (" gcc_jit_field **fields = NULL;\n");
6295 : else
6296 : {
6297 210 : r.write (" gcc_jit_field *fields[] = {\n");
6298 790 : for (size_t i = 0; i < m_fields.length (); i++)
6299 370 : r.write (" %s,\n", r.get_identifier (m_fields[i]));
6300 210 : r.write (" };\n");
6301 : }
6302 : }
6303 455 : if (type->is_array ())
6304 515 : r.write (
6305 : " %s =\n"
6306 : " gcc_jit_context_new_array_constructor (%s,\n"
6307 : " %s, /* gcc_jit_location *loc */\n"
6308 : " %s, /* gcc_jit_type *type */\n"
6309 : " %i, /* int num_values */\n"
6310 : " values);\n",
6311 : id,
6312 : r.get_identifier (get_context ()),
6313 175 : r.get_identifier (m_loc),
6314 : r.get_identifier_as_type (get_type ()),
6315 : m_values.length ());
6316 280 : else if (type->is_struct ())
6317 670 : r.write (
6318 : " %s =\n"
6319 : " gcc_jit_context_new_struct_constructor (%s,\n"
6320 : " %s, /* loc */\n"
6321 : " %s, /* gcc_jit_type *type */\n"
6322 : " %i, /* int num_values */\n"
6323 : " fields,\n"
6324 : " values);\n",
6325 : id,
6326 : r.get_identifier (get_context ()),
6327 230 : r.get_identifier (m_loc),
6328 : r.get_identifier_as_type (get_type ()),
6329 : m_values.length ());
6330 50 : else if (type->is_union ())
6331 100 : r.write (
6332 : " %s =\n"
6333 : " gcc_jit_context_new_union_constructor (%s,\n"
6334 : " %s, /* loc */\n"
6335 : " %s, /* gcc_jit_type *type */\n"
6336 : " field,\n"
6337 : " value);\n",
6338 : id,
6339 : r.get_identifier (get_context ()),
6340 50 : r.get_identifier (m_loc),
6341 : r.get_identifier_as_type (get_type ()));
6342 : else
6343 0 : gcc_unreachable ();
6344 :
6345 455 : r.write (" }\n"); /* Close scope for locals. */
6346 455 : }
6347 :
6348 : void
6349 900 : recording::ctor::replay_into (replayer *r)
6350 : {
6351 900 : auto_vec<playback::rvalue *> playback_values;
6352 900 : auto_vec<playback::field *> playback_fields;
6353 :
6354 900 : int n = m_values.length ();
6355 :
6356 900 : type *type = get_type ();
6357 :
6358 : /* Handle arrays, and return. */
6359 900 : if (type->is_array ())
6360 : {
6361 340 : playback_values.reserve (n, false);
6362 :
6363 1720 : for (int i = 0; i < n; i++)
6364 : {
6365 : /* null m_values element indicates zero ctor. */
6366 2080 : playback_values.quick_push (m_values[i] ?
6367 940 : m_values[i]->playback_rvalue () :
6368 1040 : NULL);
6369 : }
6370 340 : set_playback_obj (r->new_ctor (playback_location (r, m_loc),
6371 : get_type ()->playback_type (),
6372 : NULL,
6373 : &playback_values));
6374 340 : return;
6375 : }
6376 : /* ... else handle unions and structs. */
6377 :
6378 560 : playback_values.reserve (n, false);
6379 560 : playback_fields.reserve (n, false);
6380 :
6381 1940 : for (int i = 0; i < n; i++)
6382 : {
6383 : /* null m_values element indicates zero ctor. */
6384 1640 : playback_values.quick_push (m_values[i] ?
6385 700 : m_values[i]->playback_rvalue () :
6386 820 : NULL);
6387 820 : playback_fields.quick_push (m_fields[i]->playback_field ());
6388 : }
6389 :
6390 560 : set_playback_obj (r->new_ctor (playback_location (r, m_loc),
6391 : get_type ()->playback_type (),
6392 : &playback_fields,
6393 : &playback_values));
6394 900 : }
6395 :
6396 : /* The implementation of class gcc::jit::recording::unary_op. */
6397 :
6398 : /* Implementation of pure virtual hook recording::memento::replay_into
6399 : for recording::unary_op. */
6400 :
6401 : void
6402 163 : recording::unary_op::replay_into (replayer *r)
6403 : {
6404 163 : set_playback_obj (r->new_unary_op (playback_location (r, m_loc),
6405 : m_op,
6406 : get_type ()->playback_type (),
6407 163 : m_a->playback_rvalue ()));
6408 163 : }
6409 :
6410 : /* Implementation of pure virtual hook recording::rvalue::visit_children
6411 : for recording::unary_op. */
6412 : void
6413 194 : recording::unary_op::visit_children (rvalue_visitor *v)
6414 : {
6415 194 : v->visit (m_a);
6416 194 : }
6417 :
6418 : /* Implementation of recording::memento::make_debug_string for
6419 : unary ops. */
6420 :
6421 : static const char * const unary_op_strings[] = {
6422 : "-", /* GCC_JIT_UNARY_OP_MINUS */
6423 : "~", /* GCC_JIT_UNARY_OP_BITWISE_NEGATE */
6424 : "!", /* GCC_JIT_UNARY_OP_LOGICAL_NEGATE */
6425 : "abs ", /* GCC_JIT_UNARY_OP_ABS */
6426 : };
6427 :
6428 : recording::string *
6429 113 : recording::unary_op::make_debug_string ()
6430 : {
6431 226 : return string::from_printf (m_ctxt,
6432 : "%s(%s)",
6433 113 : unary_op_strings[m_op],
6434 113 : m_a->get_debug_string ());
6435 : }
6436 :
6437 : const char * const unary_op_reproducer_strings[] = {
6438 : "GCC_JIT_UNARY_OP_MINUS",
6439 : "GCC_JIT_UNARY_OP_BITWISE_NEGATE",
6440 : "GCC_JIT_UNARY_OP_LOGICAL_NEGATE",
6441 : "GCC_JIT_UNARY_OP_ABS"
6442 : };
6443 :
6444 : /* Implementation of recording::memento::write_reproducer for unary ops. */
6445 :
6446 : void
6447 93 : recording::unary_op::write_reproducer (reproducer &r)
6448 : {
6449 93 : const char *id = r.make_identifier (this, "rvalue");
6450 186 : r.write (" gcc_jit_rvalue *%s =\n"
6451 : " gcc_jit_context_new_unary_op (%s,\n"
6452 : " %s, /* gcc_jit_location *loc */\n"
6453 : " %s, /* enum gcc_jit_unary_op op */\n"
6454 : " %s, /* gcc_jit_type *result_type */\n"
6455 : " %s); /* gcc_jit_rvalue *a */\n",
6456 : id,
6457 : r.get_identifier (get_context ()),
6458 93 : r.get_identifier (m_loc),
6459 93 : unary_op_reproducer_strings[m_op],
6460 : r.get_identifier_as_type (get_type ()),
6461 : r.get_identifier_as_rvalue (m_a));
6462 93 : }
6463 :
6464 : /* The implementation of class gcc::jit::recording::binary_op. */
6465 :
6466 : /* Implementation of pure virtual hook recording::memento::replay_into
6467 : for recording::binary_op. */
6468 :
6469 : void
6470 3945 : recording::binary_op::replay_into (replayer *r)
6471 : {
6472 3945 : set_playback_obj (r->new_binary_op (playback_location (r, m_loc),
6473 : m_op,
6474 : get_type ()->playback_type (),
6475 3945 : m_a->playback_rvalue (),
6476 3945 : m_b->playback_rvalue ()));
6477 3945 : }
6478 :
6479 : /* Implementation of pure virtual hook recording::rvalue::visit_children
6480 : for recording::binary_op. */
6481 : void
6482 3129 : recording::binary_op::visit_children (rvalue_visitor *v)
6483 : {
6484 3129 : v->visit (m_a);
6485 3129 : v->visit (m_b);
6486 3129 : }
6487 :
6488 : /* Implementation of recording::memento::make_debug_string for
6489 : binary ops. */
6490 :
6491 : static const char * const binary_op_strings[] = {
6492 : "+", /* GCC_JIT_BINARY_OP_PLUS */
6493 : "-", /* GCC_JIT_BINARY_OP_MINUS */
6494 : "*", /* GCC_JIT_BINARY_OP_MULT */
6495 : "/", /* GCC_JIT_BINARY_OP_DIVIDE */
6496 : "%", /* GCC_JIT_BINARY_OP_MODULO */
6497 : "&", /* GCC_JIT_BINARY_OP_BITWISE_AND */
6498 : "^", /* GCC_JIT_BINARY_OP_BITWISE_XOR */
6499 : "|", /* GCC_JIT_BINARY_OP_BITWISE_OR */
6500 : "&&", /* GCC_JIT_BINARY_OP_LOGICAL_AND */
6501 : "||", /* GCC_JIT_BINARY_OP_LOGICAL_OR */
6502 : "<<", /* GCC_JIT_BINARY_OP_LSHIFT */
6503 : ">>", /* GCC_JIT_BINARY_OP_RSHIFT */
6504 : };
6505 :
6506 : recording::string *
6507 1714 : recording::binary_op::make_debug_string ()
6508 : {
6509 1714 : enum precedence prec = get_precedence ();
6510 1714 : return string::from_printf (m_ctxt,
6511 : "%s %s %s",
6512 1714 : m_a->get_debug_string_parens (prec),
6513 1714 : binary_op_strings[m_op],
6514 1714 : m_b->get_debug_string_parens (prec));
6515 : }
6516 :
6517 : const char * const binary_op_reproducer_strings[] = {
6518 : "GCC_JIT_BINARY_OP_PLUS",
6519 : "GCC_JIT_BINARY_OP_MINUS",
6520 : "GCC_JIT_BINARY_OP_MULT",
6521 : "GCC_JIT_BINARY_OP_DIVIDE",
6522 : "GCC_JIT_BINARY_OP_MODULO",
6523 : "GCC_JIT_BINARY_OP_BITWISE_AND",
6524 : "GCC_JIT_BINARY_OP_BITWISE_XOR",
6525 : "GCC_JIT_BINARY_OP_BITWISE_OR",
6526 : "GCC_JIT_BINARY_OP_LOGICAL_AND",
6527 : "GCC_JIT_BINARY_OP_LOGICAL_OR",
6528 : "GCC_JIT_BINARY_OP_LSHIFT",
6529 : "GCC_JIT_BINARY_OP_RSHIFT"
6530 : };
6531 :
6532 : /* Implementation of recording::memento::write_reproducer for binary ops. */
6533 :
6534 : void
6535 1550 : recording::binary_op::write_reproducer (reproducer &r)
6536 : {
6537 1550 : const char *id = r.make_identifier (this, "rvalue");
6538 3100 : r.write (" gcc_jit_rvalue *%s =\n"
6539 : " gcc_jit_context_new_binary_op (%s,\n"
6540 : " %s, /* gcc_jit_location *loc */\n"
6541 : " %s, /* enum gcc_jit_binary_op op */\n"
6542 : " %s, /* gcc_jit_type *result_type */\n"
6543 : " %s, /* gcc_jit_rvalue *a */\n"
6544 : " %s); /* gcc_jit_rvalue *b */\n",
6545 : id,
6546 : r.get_identifier (get_context ()),
6547 1550 : r.get_identifier (m_loc),
6548 1550 : binary_op_reproducer_strings[m_op],
6549 : r.get_identifier_as_type (get_type ()),
6550 : r.get_identifier_as_rvalue (m_a),
6551 : r.get_identifier_as_rvalue (m_b));
6552 1550 : }
6553 :
6554 : namespace recording {
6555 : static const enum precedence binary_op_precedence[] = {
6556 : PRECEDENCE_ADDITIVE, /* GCC_JIT_BINARY_OP_PLUS */
6557 : PRECEDENCE_ADDITIVE, /* GCC_JIT_BINARY_OP_MINUS */
6558 :
6559 : PRECEDENCE_MULTIPLICATIVE, /* GCC_JIT_BINARY_OP_MULT */
6560 : PRECEDENCE_MULTIPLICATIVE, /* GCC_JIT_BINARY_OP_DIVIDE */
6561 : PRECEDENCE_MULTIPLICATIVE, /* GCC_JIT_BINARY_OP_MODULO */
6562 :
6563 : PRECEDENCE_BITWISE_AND, /* GCC_JIT_BINARY_OP_BITWISE_AND */
6564 : PRECEDENCE_BITWISE_XOR, /* GCC_JIT_BINARY_OP_BITWISE_XOR */
6565 : PRECEDENCE_BITWISE_IOR, /* GCC_JIT_BINARY_OP_BITWISE_OR */
6566 : PRECEDENCE_LOGICAL_AND, /* GCC_JIT_BINARY_OP_LOGICAL_AND */
6567 : PRECEDENCE_LOGICAL_OR, /* GCC_JIT_BINARY_OP_LOGICAL_OR */
6568 : PRECEDENCE_SHIFT, /* GCC_JIT_BINARY_OP_LSHIFT */
6569 : PRECEDENCE_SHIFT, /* GCC_JIT_BINARY_OP_RSHIFT */
6570 : };
6571 : } /* namespace recording */
6572 :
6573 : enum recording::precedence
6574 2706 : recording::binary_op::get_precedence () const
6575 : {
6576 2706 : return binary_op_precedence[m_op];
6577 : }
6578 :
6579 : /* The implementation of class gcc::jit::recording::comparison. */
6580 :
6581 : /* Implementation of recording::memento::make_debug_string for
6582 : comparisons. */
6583 :
6584 : static const char * const comparison_strings[] =
6585 : {
6586 : "==", /* GCC_JIT_COMPARISON_EQ */
6587 : "!=", /* GCC_JIT_COMPARISON_NE */
6588 : "<", /* GCC_JIT_COMPARISON_LT */
6589 : "<=", /* GCC_JIT_COMPARISON_LE */
6590 : ">", /* GCC_JIT_COMPARISON_GT */
6591 : ">=", /* GCC_JIT_COMPARISON_GE */
6592 : };
6593 :
6594 : recording::string *
6595 911 : recording::comparison::make_debug_string ()
6596 : {
6597 911 : enum precedence prec = get_precedence ();
6598 911 : return string::from_printf (m_ctxt,
6599 : "%s %s %s",
6600 911 : m_a->get_debug_string_parens (prec),
6601 911 : comparison_strings[m_op],
6602 911 : m_b->get_debug_string_parens (prec));
6603 : }
6604 :
6605 : /* A table of enum gcc_jit_comparison values expressed in string
6606 : form. */
6607 :
6608 : static const char * const comparison_reproducer_strings[] =
6609 : {
6610 : "GCC_JIT_COMPARISON_EQ",
6611 : "GCC_JIT_COMPARISON_NE",
6612 : "GCC_JIT_COMPARISON_LT",
6613 : "GCC_JIT_COMPARISON_LE",
6614 : "GCC_JIT_COMPARISON_GT",
6615 : "GCC_JIT_COMPARISON_GE"
6616 : };
6617 :
6618 : /* Implementation of recording::memento::write_reproducer for comparisons. */
6619 :
6620 : void
6621 766 : recording::comparison::write_reproducer (reproducer &r)
6622 : {
6623 766 : const char *id = r.make_identifier (this, "rvalue");
6624 1532 : r.write (" gcc_jit_rvalue *%s =\n"
6625 : " gcc_jit_context_new_comparison (%s,\n"
6626 : " %s, /* gcc_jit_location *loc */\n"
6627 : " %s, /* enum gcc_jit_comparison op */\n"
6628 : " %s, /* gcc_jit_rvalue *a */\n"
6629 : " %s); /* gcc_jit_rvalue *b */\n",
6630 : id,
6631 : r.get_identifier (get_context ()),
6632 766 : r.get_identifier (m_loc),
6633 766 : comparison_reproducer_strings[m_op],
6634 : r.get_identifier_as_rvalue (m_a),
6635 : r.get_identifier_as_rvalue (m_b));
6636 766 : }
6637 :
6638 : /* Implementation of pure virtual hook recording::memento::replay_into
6639 : for recording::comparison. */
6640 :
6641 : void
6642 1874 : recording::comparison::replay_into (replayer *r)
6643 : {
6644 1874 : set_playback_obj (r->new_comparison (playback_location (r, m_loc),
6645 : m_op,
6646 1874 : m_a->playback_rvalue (),
6647 1874 : m_b->playback_rvalue (),
6648 1874 : m_type->playback_type ()));
6649 1874 : }
6650 :
6651 : /* Implementation of pure virtual hook recording::rvalue::visit_children
6652 : for recording::comparison. */
6653 :
6654 : void
6655 1359 : recording::comparison::visit_children (rvalue_visitor *v)
6656 : {
6657 1359 : v->visit (m_a);
6658 1359 : v->visit (m_b);
6659 1359 : }
6660 :
6661 : namespace recording {
6662 : static const enum precedence comparison_precedence[] =
6663 : {
6664 : PRECEDENCE_EQUALITY, /* GCC_JIT_COMPARISON_EQ */
6665 : PRECEDENCE_EQUALITY, /* GCC_JIT_COMPARISON_NE */
6666 :
6667 : PRECEDENCE_RELATIONAL, /* GCC_JIT_COMPARISON_LT */
6668 : PRECEDENCE_RELATIONAL, /* GCC_JIT_COMPARISON_LE */
6669 : PRECEDENCE_RELATIONAL, /* GCC_JIT_COMPARISON_GT */
6670 : PRECEDENCE_RELATIONAL, /* GCC_JIT_COMPARISON_GE */
6671 : };
6672 : } /* namespace recording */
6673 :
6674 : enum recording::precedence
6675 1061 : recording::comparison::get_precedence () const
6676 : {
6677 1061 : return comparison_precedence[m_op];
6678 : }
6679 :
6680 : /* Implementation of pure virtual hook recording::memento::replay_into
6681 : for recording::cast. */
6682 :
6683 : void
6684 514 : recording::cast::replay_into (replayer *r)
6685 : {
6686 514 : set_playback_obj (r->new_cast (playback_location (r, m_loc),
6687 514 : m_rvalue->playback_rvalue (),
6688 : get_type ()->playback_type ()));
6689 514 : }
6690 :
6691 : /* Implementation of pure virtual hook recording::rvalue::visit_children
6692 : for recording::cast. */
6693 : void
6694 364 : recording::cast::visit_children (rvalue_visitor *v)
6695 : {
6696 364 : v->visit (m_rvalue);
6697 364 : }
6698 :
6699 : /* Implementation of recording::memento::make_debug_string for
6700 : casts. */
6701 :
6702 : recording::string *
6703 305 : recording::cast::make_debug_string ()
6704 : {
6705 305 : enum precedence prec = get_precedence ();
6706 305 : return string::from_printf (m_ctxt,
6707 : "(%s)%s",
6708 305 : get_type ()->get_debug_string (),
6709 305 : m_rvalue->get_debug_string_parens (prec));
6710 : }
6711 :
6712 : /* Implementation of recording::memento::write_reproducer for casts. */
6713 :
6714 : void
6715 265 : recording::cast::write_reproducer (reproducer &r)
6716 : {
6717 265 : const char *id = r.make_identifier (this, "rvalue");
6718 530 : r.write (" gcc_jit_rvalue *%s =\n"
6719 : " gcc_jit_context_new_cast (%s,\n"
6720 : " %s, /* gcc_jit_location *loc */\n"
6721 : " %s, /* gcc_jit_rvalue *rvalue */\n"
6722 : " %s); /* gcc_jit_type *type */\n",
6723 : id,
6724 : r.get_identifier (get_context ()),
6725 265 : r.get_identifier (m_loc),
6726 : r.get_identifier_as_rvalue (m_rvalue),
6727 : r.get_identifier_as_type (get_type ()));
6728 265 : }
6729 :
6730 : /* Implementation of pure virtual hook recording::memento::replay_into
6731 : for recording::bitcast. */
6732 :
6733 : void
6734 30 : recording::bitcast::replay_into (replayer *r)
6735 : {
6736 30 : set_playback_obj (r->new_bitcast (playback_location (r, m_loc),
6737 30 : m_rvalue->playback_rvalue (),
6738 : get_type ()->playback_type ()));
6739 30 : }
6740 :
6741 : /* Implementation of pure virtual hook recording::rvalue::visit_children
6742 : for recording::bitcast. */
6743 : void
6744 25 : recording::bitcast::visit_children (rvalue_visitor *v)
6745 : {
6746 25 : v->visit (m_rvalue);
6747 25 : }
6748 :
6749 : /* Implementation of recording::memento::make_debug_string for
6750 : casts. */
6751 :
6752 : recording::string *
6753 20 : recording::bitcast::make_debug_string ()
6754 : {
6755 20 : enum precedence prec = get_precedence ();
6756 20 : return string::from_printf (m_ctxt,
6757 : "bitcast(%s, %s)",
6758 20 : m_rvalue->get_debug_string_parens (prec),
6759 20 : get_type ()->get_debug_string ());
6760 : }
6761 :
6762 : /* Implementation of recording::memento::write_reproducer for casts. */
6763 :
6764 : void
6765 20 : recording::bitcast::write_reproducer (reproducer &r)
6766 : {
6767 20 : const char *id = r.make_identifier (this, "rvalue");
6768 40 : r.write (" gcc_jit_rvalue *%s =\n"
6769 : " gcc_jit_context_new_bitcast (%s,\n"
6770 : " %s, /* gcc_jit_location *loc */\n"
6771 : " %s, /* gcc_jit_rvalue *rvalue */\n"
6772 : " %s); /* gcc_jit_type *type */\n",
6773 : id,
6774 : r.get_identifier (get_context ()),
6775 20 : r.get_identifier (m_loc),
6776 : r.get_identifier_as_rvalue (m_rvalue),
6777 : r.get_identifier_as_type (get_type ()));
6778 20 : }
6779 :
6780 : /* The implementation of class gcc::jit::recording::base_call. */
6781 :
6782 : /* The constructor for gcc::jit::recording::base_call. */
6783 :
6784 820 : recording::base_call::base_call (context *ctxt,
6785 : location *loc,
6786 : type *type_,
6787 : int numargs,
6788 : rvalue **args)
6789 : : rvalue (ctxt, loc, type_),
6790 820 : m_args (),
6791 820 : m_require_tail_call (0)
6792 : {
6793 1987 : for (int i = 0; i< numargs; i++)
6794 1167 : m_args.safe_push (args[i]);
6795 820 : }
6796 :
6797 : /* Subroutine for use by call and call_though_ptr's write_reproducer
6798 : methods. */
6799 :
6800 : void
6801 601 : recording::base_call::write_reproducer_tail_call (reproducer &r,
6802 : const char *id)
6803 : {
6804 601 : if (m_require_tail_call)
6805 : {
6806 15 : r.write (" gcc_jit_rvalue_set_bool_require_tail_call (%s, /* gcc_jit_rvalue *call*/\n"
6807 : " %i); /* int require_tail_call*/\n",
6808 : id,
6809 : 1);
6810 : }
6811 601 : }
6812 :
6813 : /* The implementation of class gcc::jit::recording::call. */
6814 :
6815 : /* The constructor for gcc::jit::recording::call. */
6816 :
6817 805 : recording::call::call (recording::context *ctxt,
6818 : recording::location *loc,
6819 : recording::function *func,
6820 : int numargs,
6821 : rvalue **args)
6822 : : base_call (ctxt, loc, func->get_return_type (), numargs, args),
6823 805 : m_func (func)
6824 : {
6825 805 : }
6826 :
6827 : /* Implementation of pure virtual hook recording::memento::replay_into
6828 : for recording::call. */
6829 :
6830 : void
6831 1010 : recording::call::replay_into (replayer *r)
6832 : {
6833 1010 : auto_vec<playback::rvalue *> playback_args;
6834 1010 : playback_args.create (m_args.length ());
6835 3457 : for (unsigned i = 0; i< m_args.length (); i++)
6836 1437 : playback_args.safe_push (m_args[i]->playback_rvalue ());
6837 :
6838 1010 : set_playback_obj (r->new_call (playback_location (r, m_loc),
6839 1010 : m_func->playback_function (),
6840 : &playback_args,
6841 : m_require_tail_call));
6842 1010 : }
6843 :
6844 : /* Implementation of pure virtual hook recording::rvalue::visit_children
6845 : for recording::call. */
6846 :
6847 : void
6848 790 : recording::call::visit_children (rvalue_visitor *v)
6849 : {
6850 1882 : for (unsigned i = 0; i< m_args.length (); i++)
6851 1092 : v->visit (m_args[i]);
6852 790 : }
6853 :
6854 : /* Implementation of recording::memento::make_debug_string for
6855 : function calls. */
6856 :
6857 : recording::string *
6858 606 : recording::call::make_debug_string ()
6859 : {
6860 : /* First, build a buffer for the arguments. */
6861 606 : comma_separated_string args (m_args, get_precedence ());
6862 :
6863 : /* ...and use it to get the string for the call as a whole. */
6864 606 : string *result = string::from_printf (m_ctxt,
6865 : "%s (%s)",
6866 606 : m_func->get_debug_string (),
6867 : args.as_char_ptr ());
6868 :
6869 1212 : return result;
6870 606 : }
6871 :
6872 : void
6873 591 : recording::call::write_reproducer (reproducer &r)
6874 : {
6875 591 : const char *id = r.make_identifier (this, "call");
6876 591 : const char *args_id = r.make_tmp_identifier ("args_for_", this);
6877 591 : r.write (" gcc_jit_rvalue *%s[%i] = {\n",
6878 : args_id,
6879 : m_args.length ());
6880 1978 : for (unsigned i = 0; i< m_args.length (); i++)
6881 796 : r.write (" %s,\n", r.get_identifier_as_rvalue (m_args[i]));
6882 591 : r.write (" };\n");
6883 1182 : r.write (" gcc_jit_rvalue *%s =\n"
6884 : " gcc_jit_context_new_call (%s, /* gcc_jit_context *ctxt */\n"
6885 : " %s, /* gcc_jit_location *loc */\n"
6886 : " %s, /* gcc_jit_function *func */\n"
6887 : " %i, /* int numargs */ \n"
6888 : " %s); /* gcc_jit_rvalue **args*/\n",
6889 : id,
6890 : r.get_identifier (get_context ()),
6891 591 : r.get_identifier (m_loc),
6892 591 : r.get_identifier (m_func),
6893 : m_args.length (),
6894 : args_id);
6895 591 : write_reproducer_tail_call (r, id);
6896 591 : }
6897 :
6898 : /* The implementation of class gcc::jit::recording::call_through_ptr. */
6899 :
6900 : /* The constructor for recording::call_through_ptr. */
6901 :
6902 15 : recording::call_through_ptr::call_through_ptr (recording::context *ctxt,
6903 : recording::location *loc,
6904 : recording::rvalue *fn_ptr,
6905 : int numargs,
6906 : rvalue **args)
6907 : : base_call (ctxt, loc,
6908 15 : fn_ptr->get_type ()->dereference ()
6909 15 : ->as_a_function_type ()->get_return_type (),
6910 : numargs, args),
6911 15 : m_fn_ptr (fn_ptr)
6912 : {
6913 15 : }
6914 :
6915 : /* Implementation of pure virtual hook recording::memento::replay_into
6916 : for recording::call_through_ptr. */
6917 :
6918 : void
6919 20 : recording::call_through_ptr::replay_into (replayer *r)
6920 : {
6921 20 : auto_vec<playback::rvalue *> playback_args;
6922 20 : playback_args.create (m_args.length ());
6923 100 : for (unsigned i = 0; i< m_args.length (); i++)
6924 60 : playback_args.safe_push (m_args[i]->playback_rvalue ());
6925 :
6926 20 : set_playback_obj (r->new_call_through_ptr (playback_location (r, m_loc),
6927 20 : m_fn_ptr->playback_rvalue (),
6928 : &playback_args,
6929 : m_require_tail_call));
6930 20 : }
6931 :
6932 : /* Implementation of pure virtual hook recording::rvalue::visit_children
6933 : for recording::call_through_ptr. */
6934 :
6935 : void
6936 15 : recording::call_through_ptr::visit_children (rvalue_visitor *v)
6937 : {
6938 15 : v->visit (m_fn_ptr);
6939 75 : for (unsigned i = 0; i< m_args.length (); i++)
6940 45 : v->visit (m_args[i]);
6941 15 : }
6942 :
6943 : /* Implementation of recording::memento::make_debug_string for
6944 : calls through function ptrs. */
6945 :
6946 : recording::string *
6947 10 : recording::call_through_ptr::make_debug_string ()
6948 : {
6949 10 : enum precedence prec = get_precedence ();
6950 : /* First, build a buffer for the arguments. */
6951 : /* Calculate length of said buffer. */
6952 10 : size_t sz = 1; /* nil terminator */
6953 40 : for (unsigned i = 0; i< m_args.length (); i++)
6954 : {
6955 30 : sz += strlen (m_args[i]->get_debug_string_parens (prec));
6956 30 : sz += 2; /* ", " separator */
6957 : }
6958 :
6959 : /* Now allocate and populate the buffer. */
6960 10 : char *argbuf = new char[sz];
6961 10 : size_t len = 0;
6962 :
6963 50 : for (unsigned i = 0; i< m_args.length (); i++)
6964 : {
6965 30 : strcpy (argbuf + len, m_args[i]->get_debug_string_parens (prec));
6966 30 : len += strlen (m_args[i]->get_debug_string_parens (prec));
6967 50 : if (i + 1 < m_args.length ())
6968 : {
6969 20 : strcpy (argbuf + len, ", ");
6970 20 : len += 2;
6971 : }
6972 : }
6973 10 : argbuf[len] = '\0';
6974 :
6975 : /* ...and use it to get the string for the call as a whole. */
6976 10 : string *result = string::from_printf (m_ctxt,
6977 : "%s (%s)",
6978 10 : m_fn_ptr->get_debug_string_parens (prec),
6979 : argbuf);
6980 :
6981 10 : delete[] argbuf;
6982 :
6983 10 : return result;
6984 : }
6985 :
6986 : /* Implementation of recording::memento::write_reproducer for
6987 : call_through_ptr. */
6988 :
6989 : void
6990 10 : recording::call_through_ptr::write_reproducer (reproducer &r)
6991 : {
6992 10 : const char *id = r.make_identifier (this, "call");
6993 10 : const char *args_id = r.make_tmp_identifier ("args_for_", this);
6994 10 : r.write (" gcc_jit_rvalue *%s[%i] = {\n",
6995 : args_id,
6996 : m_args.length ());
6997 50 : for (unsigned i = 0; i< m_args.length (); i++)
6998 30 : r.write (" %s,\n", r.get_identifier_as_rvalue (m_args[i]));
6999 10 : r.write (" };\n");
7000 30 : r.write (" gcc_jit_rvalue *%s =\n"
7001 : " gcc_jit_context_new_call_through_ptr (%s, /* gcc_jit_context *ctxt */\n"
7002 : " %s, /* gcc_jit_location *loc */\n"
7003 : " %s, /* gcc_jit_rvalue *fn_ptr */\n"
7004 : " %i, /* int numargs */ \n"
7005 : " %s); /* gcc_jit_rvalue **args*/\n",
7006 : id,
7007 : r.get_identifier (get_context ()),
7008 10 : r.get_identifier (m_loc),
7009 : r.get_identifier_as_rvalue (m_fn_ptr),
7010 : m_args.length (),
7011 : args_id);
7012 10 : write_reproducer_tail_call (r, id);
7013 10 : }
7014 :
7015 : /* The implementation of class gcc::jit::recording::array_access. */
7016 :
7017 : /* Implementation of pure virtual hook recording::memento::replay_into
7018 : for recording::array_access. */
7019 :
7020 : void
7021 634 : recording::array_access::replay_into (replayer *r)
7022 : {
7023 1268 : set_playback_obj (
7024 634 : r->new_array_access (playback_location (r, m_loc),
7025 634 : m_ptr->playback_rvalue (),
7026 634 : m_index->playback_rvalue ()));
7027 634 : }
7028 :
7029 : /* Implementation of pure virtual hook recording::rvalue::visit_children
7030 : for recording::array_access. */
7031 :
7032 : void
7033 534 : recording::array_access::visit_children (rvalue_visitor *v)
7034 : {
7035 534 : v->visit (m_ptr);
7036 534 : v->visit (m_index);
7037 534 : }
7038 :
7039 : /* Implementation of recording::memento::make_debug_string for
7040 : array accesses. */
7041 :
7042 : recording::string *
7043 230 : recording::array_access::make_debug_string ()
7044 : {
7045 230 : enum precedence prec = get_precedence ();
7046 230 : return string::from_printf (m_ctxt,
7047 : "%s[%s]",
7048 230 : m_ptr->get_debug_string_parens (prec),
7049 230 : m_index->get_debug_string_parens (prec));
7050 : }
7051 :
7052 : /* Implementation of recording::memento::write_reproducer for
7053 : array_access. */
7054 :
7055 : void
7056 230 : recording::array_access::write_reproducer (reproducer &r)
7057 : {
7058 230 : const char *id = r.make_identifier (this, "lvalue");
7059 460 : r.write (" gcc_jit_lvalue *%s = \n"
7060 : " gcc_jit_context_new_array_access (%s, /* gcc_jit_context *ctxt */\n"
7061 : " %s, /*gcc_jit_location *loc */\n"
7062 : " %s, /* gcc_jit_rvalue *ptr */\n"
7063 : " %s); /* gcc_jit_rvalue *index */\n",
7064 : id,
7065 : r.get_identifier (get_context ()),
7066 230 : r.get_identifier (m_loc),
7067 : r.get_identifier_as_rvalue (m_ptr),
7068 : r.get_identifier_as_rvalue (m_index));
7069 230 : }
7070 :
7071 : /* The implementation of class gcc::jit::recording::convert_vector. */
7072 :
7073 : /* Implementation of pure virtual hook recording::memento::replay_into
7074 : for recording::convert_vector. */
7075 :
7076 : void
7077 20 : recording::convert_vector::replay_into (replayer *r)
7078 : {
7079 40 : set_playback_obj (
7080 20 : r->convert_vector (playback_location (r, m_loc),
7081 20 : m_vector->playback_rvalue (),
7082 20 : m_type->playback_type ()));
7083 20 : }
7084 :
7085 : /* Implementation of pure virtual hook recording::rvalue::visit_children
7086 : for recording::convert_vector. */
7087 :
7088 : void
7089 15 : recording::convert_vector::visit_children (rvalue_visitor *v)
7090 : {
7091 15 : v->visit (m_vector);
7092 15 : }
7093 :
7094 : /* The implementation of class gcc::jit::recording::vector_access. */
7095 :
7096 : /* Implementation of pure virtual hook recording::memento::replay_into
7097 : for recording::vector_access. */
7098 :
7099 : void
7100 20 : recording::vector_access::replay_into (replayer *r)
7101 : {
7102 40 : set_playback_obj (
7103 20 : r->new_vector_access (playback_location (r, m_loc),
7104 20 : m_vector->playback_rvalue (),
7105 20 : m_index->playback_rvalue ()));
7106 20 : }
7107 :
7108 : /* Implementation of pure virtual hook recording::rvalue::visit_children
7109 : for recording::vector_access. */
7110 :
7111 : void
7112 15 : recording::vector_access::visit_children (rvalue_visitor *v)
7113 : {
7114 15 : v->visit (m_vector);
7115 15 : v->visit (m_index);
7116 15 : }
7117 :
7118 : /* Implementation of recording::memento::make_debug_string for
7119 : array accesses. */
7120 :
7121 : recording::string *
7122 10 : recording::convert_vector::make_debug_string ()
7123 : {
7124 10 : enum precedence prec = get_precedence ();
7125 10 : return string::from_printf (m_ctxt,
7126 : "(%s)%s",
7127 10 : m_type->get_debug_string (),
7128 10 : m_vector->get_debug_string_parens (prec));
7129 : }
7130 :
7131 : /* Implementation of recording::memento::write_reproducer for
7132 : convert_vector. */
7133 :
7134 : void
7135 10 : recording::convert_vector::write_reproducer (reproducer &r)
7136 : {
7137 10 : const char *id = r.make_identifier (this, "lvalue");
7138 20 : r.write (" gcc_jit_rvalue *%s = \n"
7139 : " gcc_jit_context_convert_vector (%s, /* gcc_jit_context *ctxt */\n"
7140 : " %s, /*gcc_jit_location *loc */\n"
7141 : " %s, /* gcc_jit_rvalue *vector */\n"
7142 : " %s); /* gcc_jit_type *type */\n",
7143 : id,
7144 : r.get_identifier (get_context ()),
7145 10 : r.get_identifier (m_loc),
7146 : r.get_identifier_as_rvalue (m_vector),
7147 : r.get_identifier_as_type (m_type));
7148 10 : }
7149 :
7150 : recording::string *
7151 10 : recording::vector_access::make_debug_string ()
7152 : {
7153 10 : enum precedence prec = get_precedence ();
7154 10 : return string::from_printf (m_ctxt,
7155 : "%s[%s]",
7156 10 : m_vector->get_debug_string_parens (prec),
7157 10 : m_index->get_debug_string_parens (prec));
7158 : }
7159 :
7160 : /* Implementation of recording::memento::write_reproducer for
7161 : vector_access. */
7162 :
7163 : void
7164 10 : recording::vector_access::write_reproducer (reproducer &r)
7165 : {
7166 10 : const char *id = r.make_identifier (this, "lvalue");
7167 20 : r.write (" gcc_jit_lvalue *%s = \n"
7168 : " gcc_jit_context_new_vector_access (%s, /* gcc_jit_context *ctxt */\n"
7169 : " %s, /*gcc_jit_location *loc */\n"
7170 : " %s, /* gcc_jit_rvalue *vector */\n"
7171 : " %s); /* gcc_jit_rvalue *index */\n",
7172 : id,
7173 : r.get_identifier (get_context ()),
7174 10 : r.get_identifier (m_loc),
7175 : r.get_identifier_as_rvalue (m_vector),
7176 : r.get_identifier_as_rvalue (m_index));
7177 10 : }
7178 :
7179 : /* The implementation of class gcc::jit::recording::access_field_of_lvalue. */
7180 :
7181 : /* Implementation of pure virtual hook recording::memento::replay_into
7182 : for recording::access_field_of_lvalue. */
7183 :
7184 : void
7185 234 : recording::access_field_of_lvalue::replay_into (replayer *r)
7186 : {
7187 468 : set_playback_obj (
7188 234 : m_lvalue->playback_lvalue ()
7189 234 : ->access_field (playback_location (r, m_loc),
7190 234 : m_field->playback_field ()));
7191 :
7192 234 : }
7193 :
7194 : /* Implementation of pure virtual hook recording::rvalue::visit_children
7195 : for recording::access_field_of_lvalue. */
7196 :
7197 : void
7198 199 : recording::access_field_of_lvalue::visit_children (rvalue_visitor *v)
7199 : {
7200 199 : v->visit (m_lvalue);
7201 199 : }
7202 :
7203 : /* Implementation of recording::memento::make_debug_string for
7204 : accessing a field of an lvalue. */
7205 :
7206 : recording::string *
7207 184 : recording::access_field_of_lvalue::make_debug_string ()
7208 : {
7209 184 : enum precedence prec = get_precedence ();
7210 184 : return string::from_printf (m_ctxt,
7211 : "%s.%s",
7212 184 : m_lvalue->get_debug_string_parens (prec),
7213 184 : m_field->get_debug_string ());
7214 : }
7215 :
7216 : /* Implementation of recording::memento::write_reproducer for
7217 : access_field_of_lvalue. */
7218 :
7219 : void
7220 184 : recording::access_field_of_lvalue::write_reproducer (reproducer &r)
7221 : {
7222 184 : const char *id = r.make_identifier (this, "lvalue");
7223 184 : r.write (" gcc_jit_lvalue *%s = \n"
7224 : " gcc_jit_lvalue_access_field (%s, /*gcc_jit_lvalue *struct_or_union */\n"
7225 : " %s, /*gcc_jit_location *loc */\n"
7226 : " %s);\n",
7227 : id,
7228 : r.get_identifier_as_lvalue (m_lvalue),
7229 184 : r.get_identifier (m_loc),
7230 184 : r.get_identifier (m_field));
7231 184 : }
7232 :
7233 : /* The implementation of class gcc::jit::recording::access_field_rvalue. */
7234 :
7235 : /* Implementation of pure virtual hook recording::memento::replay_into
7236 : for recording::access_field_rvalue. */
7237 :
7238 : void
7239 174 : recording::access_field_rvalue::replay_into (replayer *r)
7240 : {
7241 348 : set_playback_obj (
7242 174 : m_rvalue->playback_rvalue ()
7243 174 : ->access_field (playback_location (r, m_loc),
7244 174 : m_field->playback_field ()));
7245 174 : }
7246 :
7247 : /* Implementation of pure virtual hook recording::rvalue::visit_children
7248 : for recording::access_field_rvalue. */
7249 :
7250 : void
7251 139 : recording::access_field_rvalue::visit_children (rvalue_visitor *v)
7252 : {
7253 139 : v->visit (m_rvalue);
7254 139 : }
7255 :
7256 : /* Implementation of recording::memento::make_debug_string for
7257 : accessing a field of an rvalue. */
7258 :
7259 : recording::string *
7260 104 : recording::access_field_rvalue::make_debug_string ()
7261 : {
7262 104 : enum precedence prec = get_precedence ();
7263 104 : return string::from_printf (m_ctxt,
7264 : "%s.%s",
7265 104 : m_rvalue->get_debug_string_parens (prec),
7266 104 : m_field->get_debug_string ());
7267 : }
7268 :
7269 : /* Implementation of recording::memento::write_reproducer for
7270 : access_field_rvalue. */
7271 :
7272 : void
7273 104 : recording::access_field_rvalue::write_reproducer (reproducer &r)
7274 : {
7275 104 : const char *id = r.make_identifier (this, "rvalue");
7276 104 : r.write (" gcc_jit_rvalue *%s = \n"
7277 : " gcc_jit_rvalue_access_field (%s, /*gcc_jit_rvalue *struct_or_union */\n"
7278 : " %s, /*gcc_jit_location *loc */\n"
7279 : " %s);\n",
7280 : id,
7281 : r.get_identifier_as_rvalue (m_rvalue),
7282 104 : r.get_identifier (m_loc),
7283 104 : r.get_identifier (m_field));
7284 104 : }
7285 :
7286 : /* The implementation of class
7287 : gcc::jit::recording::dereference_field_rvalue. */
7288 :
7289 : /* Implementation of pure virtual hook recording::memento::replay_into
7290 : for recording::dereference_field_rvalue. */
7291 :
7292 : void
7293 2008 : recording::dereference_field_rvalue::replay_into (replayer *r)
7294 : {
7295 4016 : set_playback_obj (
7296 2008 : m_rvalue->playback_rvalue ()->
7297 2008 : dereference_field (playback_location (r, m_loc),
7298 2008 : m_field->playback_field ()));
7299 2008 : }
7300 :
7301 : /* Implementation of pure virtual hook recording::rvalue::visit_children
7302 : for recording::dereference_field_rvalue. */
7303 :
7304 : void
7305 1530 : recording::dereference_field_rvalue::visit_children (rvalue_visitor *v)
7306 : {
7307 1530 : v->visit (m_rvalue);
7308 1530 : }
7309 :
7310 : /* Implementation of recording::memento::make_debug_string for
7311 : dereferencing a field of an rvalue. */
7312 :
7313 : recording::string *
7314 1116 : recording::dereference_field_rvalue::make_debug_string ()
7315 : {
7316 1116 : enum precedence prec = get_precedence ();
7317 1116 : return string::from_printf (m_ctxt,
7318 : "%s->%s",
7319 1116 : m_rvalue->get_debug_string_parens (prec),
7320 1116 : m_field->get_debug_string ());
7321 : }
7322 :
7323 : /* Implementation of recording::memento::write_reproducer for
7324 : dereference_field_rvalue. */
7325 :
7326 : void
7327 1102 : recording::dereference_field_rvalue::write_reproducer (reproducer &r)
7328 : {
7329 1102 : const char *id = r.make_identifier (this, "lvalue");
7330 1102 : r.write (" gcc_jit_lvalue *%s=\n"
7331 : " gcc_jit_rvalue_dereference_field (%s, /* gcc_jit_rvalue *ptr */\n"
7332 : " %s, /* gcc_jit_location *loc */\n"
7333 : " %s); /* gcc_jit_field *field */\n",
7334 : id,
7335 : r.get_identifier_as_rvalue (m_rvalue),
7336 1102 : r.get_identifier (m_loc),
7337 1102 : r.get_identifier (m_field));
7338 1102 : }
7339 :
7340 : /* The implementation of class gcc::jit::recording::dereference_rvalue. */
7341 :
7342 : /* Implementation of pure virtual hook recording::memento::replay_into
7343 : for recording::dereference_rvalue. */
7344 :
7345 : void
7346 769 : recording::dereference_rvalue::replay_into (replayer *r)
7347 : {
7348 1538 : set_playback_obj (
7349 769 : m_rvalue->playback_rvalue ()->
7350 769 : dereference (playback_location (r, m_loc)));
7351 769 : }
7352 :
7353 : /* Implementation of pure virtual hook recording::rvalue::visit_children
7354 : for recording::dereference_rvalue. */
7355 :
7356 : void
7357 684 : recording::dereference_rvalue::visit_children (rvalue_visitor *v)
7358 : {
7359 684 : v->visit (m_rvalue);
7360 684 : }
7361 :
7362 : /* Implementation of recording::memento::make_debug_string for
7363 : dereferencing an rvalue. */
7364 :
7365 : recording::string *
7366 599 : recording::dereference_rvalue::make_debug_string ()
7367 : {
7368 599 : enum precedence prec = get_precedence ();
7369 599 : return string::from_printf (m_ctxt,
7370 : "*%s",
7371 599 : m_rvalue->get_debug_string_parens (prec));
7372 : }
7373 :
7374 : /* Implementation of recording::memento::write_reproducer for
7375 : dereference_rvalue. */
7376 :
7377 : void
7378 599 : recording::dereference_rvalue::write_reproducer (reproducer &r)
7379 : {
7380 599 : const char *id = r.make_identifier (this, "dereference");
7381 599 : r.write (" gcc_jit_lvalue *%s =\n"
7382 : " gcc_jit_rvalue_dereference (%s, /* gcc_jit_rvalue *rvalue */\n"
7383 : " %s); /* gcc_jit_location *loc */\n",
7384 : id,
7385 : r.get_identifier_as_rvalue (m_rvalue),
7386 599 : r.get_identifier (m_loc));
7387 599 : }
7388 :
7389 : /* The implementation of class gcc::jit::recording::get_address_of_lvalue. */
7390 :
7391 : /* Implementation of pure virtual hook recording::memento::replay_into
7392 : for recording::get_address_of_lvalue. */
7393 :
7394 : void
7395 653 : recording::get_address_of_lvalue::replay_into (replayer *r)
7396 : {
7397 1306 : set_playback_obj (
7398 653 : m_lvalue->playback_lvalue ()->
7399 653 : get_address (playback_location (r, m_loc)));
7400 653 : }
7401 :
7402 : /* Implementation of pure virtual hook recording::rvalue::visit_children
7403 : for recording::get_address_of_lvalue. */
7404 :
7405 : void
7406 508 : recording::get_address_of_lvalue::visit_children (rvalue_visitor *v)
7407 : {
7408 508 : v->visit (m_lvalue);
7409 508 : }
7410 :
7411 : /* Implementation of recording::memento::make_debug_string for
7412 : getting the address of an lvalue. */
7413 :
7414 : recording::string *
7415 358 : recording::get_address_of_lvalue::make_debug_string ()
7416 : {
7417 358 : enum precedence prec = get_precedence ();
7418 358 : return string::from_printf (m_ctxt,
7419 : "&%s",
7420 358 : m_lvalue->get_debug_string_parens (prec));
7421 : }
7422 :
7423 : /* Implementation of recording::memento::write_reproducer for
7424 : get_address_of_lvalue. */
7425 :
7426 : void
7427 353 : recording::get_address_of_lvalue::write_reproducer (reproducer &r)
7428 : {
7429 353 : const char *id = r.make_identifier (this, "address_of");
7430 353 : r.write (" gcc_jit_rvalue *%s =\n"
7431 : " gcc_jit_lvalue_get_address (%s, /* gcc_jit_lvalue *lvalue */\n"
7432 : " %s); /* gcc_jit_location *loc */\n",
7433 : id,
7434 : r.get_identifier_as_lvalue (m_lvalue),
7435 353 : r.get_identifier (m_loc));
7436 353 : }
7437 :
7438 : /* The implementation of class gcc::jit::recording::get_block_address. */
7439 :
7440 : /* Implementation of pure virtual hook recording::memento::replay_into
7441 : for recording::get_block_address. */
7442 :
7443 : void
7444 60 : recording::get_block_address::replay_into (replayer *r)
7445 : {
7446 120 : set_playback_obj (
7447 60 : m_block->playback_block ()->
7448 60 : get_address (playback_location (r, m_loc)));
7449 60 : }
7450 :
7451 : /* Implementation of recording::memento::make_debug_string for
7452 : getting the address of a block. */
7453 :
7454 : recording::string *
7455 40 : recording::get_block_address::make_debug_string ()
7456 : {
7457 40 : return string::from_printf (m_ctxt,
7458 : "&&%s",
7459 40 : m_block->get_debug_string ());
7460 : }
7461 :
7462 : /* Implementation of recording::memento::write_reproducer for
7463 : get_block_address. */
7464 :
7465 : void
7466 40 : recording::get_block_address::write_reproducer (reproducer &r)
7467 : {
7468 40 : const char *id = r.make_identifier (this, "address_of");
7469 40 : r.write (" gcc_jit_rvalue *%s =\n"
7470 : " gcc_jit_block_get_address (%s,\n"
7471 : " %s);\n",
7472 : id,
7473 40 : r.get_identifier (m_block),
7474 40 : r.get_identifier (m_loc));
7475 40 : }
7476 :
7477 : /* The implementation of class gcc::jit::recording::function_pointer. */
7478 :
7479 : /* Implementation of pure virtual hook recording::memento::replay_into
7480 : for recording::function_pointer. */
7481 :
7482 : void
7483 20 : recording::function_pointer::replay_into (replayer *r)
7484 : {
7485 40 : set_playback_obj (
7486 20 : m_fn->playback_function ()->
7487 20 : get_address (playback_location (r, m_loc)));
7488 20 : }
7489 :
7490 : void
7491 15 : recording::function_pointer::visit_children (rvalue_visitor *)
7492 : {
7493 : /* Empty. */
7494 15 : }
7495 :
7496 : /* Implementation of recording::memento::make_debug_string for
7497 : getting the address of an lvalue. */
7498 :
7499 : recording::string *
7500 10 : recording::function_pointer::make_debug_string ()
7501 : {
7502 10 : return string::from_printf (m_ctxt,
7503 : "%s",
7504 10 : m_fn->get_debug_string ());
7505 : }
7506 :
7507 : /* Implementation of recording::memento::write_reproducer for
7508 : function_pointer. */
7509 :
7510 : void
7511 10 : recording::function_pointer::write_reproducer (reproducer &r)
7512 : {
7513 10 : const char *id = r.make_identifier (this, "address_of");
7514 10 : r.write (" gcc_jit_rvalue *%s =\n"
7515 : " gcc_jit_function_get_address (%s, /* gcc_jit_function *fn */\n"
7516 : " %s); /* gcc_jit_location *loc */\n",
7517 : id,
7518 10 : r.get_identifier (m_fn),
7519 10 : r.get_identifier (m_loc));
7520 10 : }
7521 :
7522 : /* The implementation of class gcc::jit::recording::local. */
7523 :
7524 : /* Implementation of pure virtual hook recording::memento::replay_into
7525 : for recording::local. */
7526 :
7527 : void
7528 2592 : recording::local::replay_into (replayer *r)
7529 : {
7530 2592 : playback::lvalue *obj = m_func->playback_function ()
7531 2592 : ->new_local (playback_location (r, m_loc),
7532 2592 : m_type->playback_type (),
7533 : playback_string (m_name),
7534 2592 : m_string_attributes);
7535 :
7536 2592 : if (m_reg_name != NULL)
7537 5 : obj->set_register_name (m_reg_name->c_str ());
7538 :
7539 2592 : if (m_alignment != 0)
7540 5 : obj->set_alignment (m_alignment);
7541 :
7542 2592 : set_playback_obj (obj);
7543 2592 : }
7544 :
7545 : /* Override the default implementation of
7546 : recording::memento::write_to_dump for locals by writing
7547 : TYPE NAME;
7548 : for use at the top of the function body as if it were a
7549 : declaration. */
7550 :
7551 : void
7552 16 : recording::local::write_to_dump (dump &d)
7553 : {
7554 16 : if (d.update_locations ())
7555 16 : m_loc = d.make_location ();
7556 32 : d.write (" %s %s;\n",
7557 16 : m_type->get_debug_string (),
7558 : get_debug_string ());
7559 16 : }
7560 :
7561 : void
7562 2789 : recording::local::write_reproducer (reproducer &r)
7563 : {
7564 2789 : const char *id = r.make_identifier (this, "local");
7565 2789 : if (m_name)
7566 5568 : r.write (" gcc_jit_lvalue *%s =\n"
7567 : " gcc_jit_function_new_local (%s, /* gcc_jit_function *func */\n"
7568 : " %s, /* gcc_jit_location *loc */\n"
7569 : " %s, /* gcc_jit_type *type */\n"
7570 : " %s); /* const char *name */\n",
7571 : id,
7572 2784 : r.get_identifier (m_func),
7573 2784 : r.get_identifier (m_loc),
7574 : r.get_identifier_as_type (m_type),
7575 : m_name->get_debug_string ());
7576 : else
7577 10 : r.write (" gcc_jit_lvalue *%s =\n"
7578 : " gcc_jit_function_new_temp (%s, /* gcc_jit_function *func */\n"
7579 : " %s, /* gcc_jit_location *loc */\n"
7580 : " %s); /* gcc_jit_type *type */\n",
7581 : id,
7582 5 : r.get_identifier (m_func),
7583 5 : r.get_identifier (m_loc),
7584 : r.get_identifier_as_type (m_type));
7585 2789 : }
7586 :
7587 : /* The implementation of class gcc::jit::recording::statement. */
7588 :
7589 : /* We poison the default implementation of
7590 : gcc::jit::recording::statement::get_successor_blocks
7591 : since this vfunc must only ever be called on terminator
7592 : statements. */
7593 :
7594 : vec <recording::block *>
7595 0 : recording::statement::get_successor_blocks () const
7596 : {
7597 : /* The base class implementation is for non-terminating statements,
7598 : and thus should never be called. */
7599 0 : gcc_unreachable ();
7600 : vec <block *> result;
7601 : result.create (0);
7602 : return result;
7603 : }
7604 :
7605 : /* Extend the default implementation of
7606 : recording::memento::write_to_dump for statements by (if requested)
7607 : updating the location of the statement to the current location in
7608 : the dumpfile. */
7609 :
7610 : void
7611 156 : recording::statement::write_to_dump (dump &d)
7612 : {
7613 156 : memento::write_to_dump (d);
7614 156 : if (d.update_locations ())
7615 156 : m_loc = d.make_location ();
7616 156 : }
7617 :
7618 : /* The implementation of class gcc::jit::recording::eval. */
7619 :
7620 : /* Implementation of pure virtual hook recording::memento::replay_into
7621 : for recording::eval. */
7622 :
7623 : void
7624 321 : recording::eval::replay_into (replayer *r)
7625 : {
7626 321 : playback_block (get_block ())
7627 321 : ->add_eval (playback_location (r),
7628 321 : m_rvalue->playback_rvalue ());
7629 321 : }
7630 :
7631 : /* Implementation of recording::memento::make_debug_string for
7632 : an eval statement. */
7633 :
7634 : recording::string *
7635 8 : recording::eval::make_debug_string ()
7636 : {
7637 8 : return string::from_printf (m_ctxt,
7638 : "(void)%s;",
7639 8 : m_rvalue->get_debug_string ());
7640 : }
7641 :
7642 : /* Implementation of recording::memento::write_reproducer for
7643 : eval statements. */
7644 :
7645 : void
7646 1959 : recording::eval::write_reproducer (reproducer &r)
7647 : {
7648 1959 : r.write (" gcc_jit_block_add_eval (%s, /*gcc_jit_block *block */\n"
7649 : " %s, /* gcc_jit_location *loc */\n"
7650 : " %s); /* gcc_jit_rvalue *rvalue */\n",
7651 1959 : r.get_identifier (get_block ()),
7652 1959 : r.get_identifier (get_loc ()),
7653 : r.get_identifier_as_rvalue (m_rvalue));
7654 1959 : }
7655 :
7656 : /* The implementation of class gcc::jit::recording::assignment. */
7657 :
7658 : /* Implementation of pure virtual hook recording::memento::replay_into
7659 : for recording::assignment. */
7660 :
7661 : void
7662 5270 : recording::assignment::replay_into (replayer *r)
7663 : {
7664 5270 : playback_block (get_block ())
7665 10540 : ->add_assignment (playback_location (r),
7666 5270 : m_lvalue->playback_lvalue (),
7667 5270 : m_rvalue->playback_rvalue ());
7668 5270 : }
7669 :
7670 : /* Implementation of recording::memento::make_debug_string for
7671 : an assignment statement. */
7672 :
7673 : recording::string *
7674 60 : recording::assignment::make_debug_string ()
7675 : {
7676 60 : return string::from_printf (m_ctxt,
7677 : "%s = %s;",
7678 60 : m_lvalue->get_debug_string (),
7679 60 : m_rvalue->get_debug_string ());
7680 : }
7681 :
7682 : /* Implementation of recording::memento::write_reproducer for
7683 : assignment statements. */
7684 :
7685 : void
7686 2225 : recording::assignment::write_reproducer (reproducer &r)
7687 : {
7688 2225 : r.write (" gcc_jit_block_add_assignment (%s, /*gcc_jit_block *block */\n"
7689 : " %s, /* gcc_jit_location *loc */\n"
7690 : " %s, /* gcc_jit_lvalue *lvalue */\n"
7691 : " %s); /* gcc_jit_rvalue *rvalue */\n",
7692 2225 : r.get_identifier (get_block ()),
7693 2225 : r.get_identifier (get_loc ()),
7694 : r.get_identifier_as_lvalue (m_lvalue),
7695 : r.get_identifier_as_rvalue (m_rvalue));
7696 2225 : }
7697 :
7698 : /* The implementation of class gcc::jit::recording::assignment_op. */
7699 :
7700 : /* Implementation of pure virtual hook recording::memento::replay_into
7701 : for recording::assignment_op. */
7702 :
7703 : void
7704 549 : recording::assignment_op::replay_into (replayer *r)
7705 : {
7706 549 : playback::type *result_type =
7707 549 : m_lvalue->playback_lvalue ()->get_type ();
7708 :
7709 549 : playback::rvalue *binary_op =
7710 549 : r->new_binary_op (playback_location (r),
7711 : m_op,
7712 : result_type,
7713 549 : m_lvalue->playback_rvalue (),
7714 549 : m_rvalue->playback_rvalue ());
7715 :
7716 549 : playback_block (get_block ())
7717 549 : ->add_assignment (playback_location (r),
7718 549 : m_lvalue->playback_lvalue (),
7719 : binary_op);
7720 549 : }
7721 :
7722 : /* Implementation of recording::memento::make_debug_string for
7723 : an assignment_op statement. */
7724 :
7725 : recording::string *
7726 0 : recording::assignment_op::make_debug_string ()
7727 : {
7728 0 : return string::from_printf (m_ctxt,
7729 : "%s %s= %s;",
7730 0 : m_lvalue->get_debug_string (),
7731 0 : binary_op_strings[m_op],
7732 0 : m_rvalue->get_debug_string ());
7733 : }
7734 :
7735 : /* Implementation of recording::memento::write_reproducer for
7736 : assignment_op statements. */
7737 :
7738 : void
7739 210 : recording::assignment_op::write_reproducer (reproducer &r)
7740 : {
7741 420 : r.write (" gcc_jit_block_add_assignment_op (%s, /*gcc_jit_block *block */\n"
7742 : " %s, /* gcc_jit_location *loc */\n"
7743 : " %s, /* gcc_jit_lvalue *lvalue */\n"
7744 : " %s, /* enum gcc_jit_binary_op op */\n"
7745 : " %s); /* gcc_jit_rvalue *rvalue */\n",
7746 210 : r.get_identifier (get_block ()),
7747 210 : r.get_identifier (get_loc ()),
7748 : r.get_identifier_as_lvalue (m_lvalue),
7749 210 : binary_op_reproducer_strings[m_op],
7750 : r.get_identifier_as_rvalue (m_rvalue));
7751 210 : }
7752 :
7753 : /* The implementation of class gcc::jit::recording::comment. */
7754 :
7755 : /* Implementation of pure virtual hook recording::memento::replay_into
7756 : for recording::comment. */
7757 :
7758 : void
7759 513 : recording::comment::replay_into (replayer *r)
7760 : {
7761 513 : playback_block (get_block ())
7762 513 : ->add_comment (playback_location (r),
7763 513 : m_text->c_str ());
7764 513 : }
7765 :
7766 : /* Implementation of recording::memento::make_debug_string for
7767 : a comment "statement". */
7768 :
7769 : recording::string *
7770 44 : recording::comment::make_debug_string ()
7771 : {
7772 44 : return string::from_printf (m_ctxt,
7773 : "/* %s */",
7774 44 : m_text->c_str ());
7775 : }
7776 :
7777 : /* Implementation of recording::memento::write_reproducer for
7778 : comments. */
7779 :
7780 : void
7781 248 : recording::comment::write_reproducer (reproducer &r)
7782 : {
7783 248 : r.write (" gcc_jit_block_add_comment (%s, /*gcc_jit_block *block */\n"
7784 : " %s, /* gcc_jit_location *loc */\n"
7785 : " %s); /* const char *text */\n",
7786 248 : r.get_identifier (get_block ()),
7787 248 : r.get_identifier (get_loc ()),
7788 248 : m_text->get_debug_string ());
7789 248 : }
7790 :
7791 : /* The implementation of class gcc::jit::recording::conditional. */
7792 :
7793 : /* Implementation of pure virtual hook recording::memento::replay_into
7794 : for recording::conditional. */
7795 :
7796 : void
7797 1034 : recording::conditional::replay_into (replayer *r)
7798 : {
7799 1034 : playback_block (get_block ())
7800 1034 : ->add_conditional (playback_location (r),
7801 1034 : m_boolval->playback_rvalue (),
7802 : playback_block (m_on_true),
7803 : playback_block (m_on_false));
7804 1034 : }
7805 :
7806 : /* Override the poisoned default implementation of
7807 : gcc::jit::recording::statement::get_successor_blocks
7808 :
7809 : A conditional jump has 2 successor blocks. */
7810 :
7811 : vec <recording::block *>
7812 894 : recording::conditional::get_successor_blocks () const
7813 : {
7814 894 : vec <block *> result;
7815 894 : result.create (2);
7816 894 : result.quick_push (m_on_true);
7817 894 : result.quick_push (m_on_false);
7818 894 : return result;
7819 : }
7820 :
7821 : /* Implementation of recording::memento::make_debug_string for
7822 : a conditional jump statement. */
7823 :
7824 : recording::string *
7825 16 : recording::conditional::make_debug_string ()
7826 : {
7827 16 : if (m_on_false)
7828 32 : return string::from_printf (m_ctxt,
7829 : "if (%s) goto %s; else goto %s;",
7830 16 : m_boolval->get_debug_string (),
7831 16 : m_on_true->get_debug_string (),
7832 16 : m_on_false->get_debug_string ());
7833 : else
7834 0 : return string::from_printf (m_ctxt,
7835 : "if (%s) goto %s;",
7836 0 : m_boolval->get_debug_string (),
7837 0 : m_on_true->get_debug_string ());
7838 : }
7839 :
7840 : /* Implementation of recording::memento::write_reproducer for
7841 : conditional statements. */
7842 :
7843 : void
7844 346 : recording::conditional::write_reproducer (reproducer &r)
7845 : {
7846 346 : r.write (" gcc_jit_block_end_with_conditional (%s, /*gcc_jit_block *block */\n"
7847 : " %s, /* gcc_jit_location *loc */\n"
7848 : " %s, /* gcc_jit_rvalue *boolval */\n"
7849 : " %s, /* gcc_jit_block *on_true */\n"
7850 : " %s); /* gcc_jit_block *on_false */\n",
7851 346 : r.get_identifier (get_block ()),
7852 346 : r.get_identifier (get_loc ()),
7853 : r.get_identifier_as_rvalue (m_boolval),
7854 346 : r.get_identifier (m_on_true),
7855 346 : r.get_identifier (m_on_false));
7856 346 : }
7857 :
7858 : /* The implementation of class gcc::jit::recording::jump. */
7859 :
7860 : /* Implementation of pure virtual hook recording::memento::replay_into
7861 : for recording::jump. */
7862 :
7863 : void
7864 1307 : recording::jump::replay_into (replayer *r)
7865 : {
7866 1307 : playback_block (get_block ())
7867 1307 : ->add_jump (playback_location (r),
7868 1307 : m_target->playback_block ());
7869 1307 : }
7870 :
7871 : /* Override the poisoned default implementation of
7872 : gcc::jit::recording::statement::get_successor_blocks
7873 :
7874 : An unconditional jump has 1 successor block. */
7875 :
7876 : vec <recording::block *>
7877 1202 : recording::jump::get_successor_blocks () const
7878 : {
7879 1202 : vec <block *> result;
7880 1202 : result.create (1);
7881 1202 : result.quick_push (m_target);
7882 1202 : return result;
7883 : }
7884 :
7885 : /* Implementation of recording::memento::make_debug_string for
7886 : a unconditional jump statement. */
7887 :
7888 : recording::string *
7889 0 : recording::jump::make_debug_string ()
7890 : {
7891 0 : return string::from_printf (m_ctxt,
7892 : "goto %s;",
7893 0 : m_target->get_debug_string ());
7894 : }
7895 :
7896 : /* Implementation of recording::memento::write_reproducer for
7897 : jump statements. */
7898 :
7899 : void
7900 245 : recording::jump::write_reproducer (reproducer &r)
7901 : {
7902 245 : r.write (" gcc_jit_block_end_with_jump (%s, /*gcc_jit_block *block */\n"
7903 : " %s, /* gcc_jit_location *loc */\n"
7904 : " %s); /* gcc_jit_block *target */\n",
7905 245 : r.get_identifier (get_block ()),
7906 245 : r.get_identifier (get_loc ()),
7907 245 : r.get_identifier (m_target));
7908 245 : }
7909 :
7910 : /* The implementation of class gcc::jit::recording::indirect_jump. */
7911 :
7912 : /* gcc::jit::recording::indirect_jump's constructor. */
7913 :
7914 20 : recording::indirect_jump::indirect_jump (block *b,
7915 : location *loc,
7916 : rvalue *target_address,
7917 : int num_destinations,
7918 : block **destinations)
7919 : : statement (b, loc),
7920 20 : m_target_address (target_address)
7921 : {
7922 20 : m_destinations.reserve_exact (num_destinations);
7923 90 : for (int i = 0; i < num_destinations; i++)
7924 50 : m_destinations.quick_push (destinations[i]);
7925 20 : }
7926 :
7927 : /* Implementation of pure virtual hook recording::memento::replay_into
7928 : for recording::indirect_jump. */
7929 :
7930 : void
7931 20 : recording::indirect_jump::replay_into (replayer *r)
7932 : {
7933 20 : auto_vec <playback::block *> pb_dests;
7934 40 : pb_dests.reserve_exact (m_destinations.length ());
7935 100 : for (unsigned i = 0; i < m_destinations.length (); i++)
7936 60 : pb_dests.quick_push (m_destinations[i]->playback_block ());
7937 :
7938 20 : playback_block (get_block ())
7939 20 : ->add_indirect_jump (playback_location (r),
7940 20 : m_target_address->playback_rvalue (),
7941 : &pb_dests);
7942 20 : }
7943 :
7944 : /* Override the poisoned default implementation of
7945 : gcc::jit::recording::statement::get_successor_blocks. */
7946 :
7947 : vec <recording::block *>
7948 20 : recording::indirect_jump::get_successor_blocks () const
7949 : {
7950 20 : vec <block *> result;
7951 20 : result.create (m_destinations.length ());
7952 90 : for (unsigned i = 0; i < m_destinations.length (); i++)
7953 50 : result.quick_push (m_destinations[i]);
7954 20 : return result;
7955 : }
7956 :
7957 : /* Implementation of recording::memento::make_debug_string for
7958 : an indirect jump statement. */
7959 :
7960 : recording::string *
7961 15 : recording::indirect_jump::make_debug_string ()
7962 : {
7963 15 : return string::from_printf (m_ctxt,
7964 : "goto *%s;",
7965 15 : m_target_address->get_debug_string_parens (PRECEDENCE_UNARY));
7966 : }
7967 :
7968 : /* Implementation of recording::memento::write_reproducer for
7969 : indirect_jump statements. */
7970 :
7971 : void
7972 15 : recording::indirect_jump::write_reproducer (reproducer &r)
7973 : {
7974 15 : r.make_identifier (this, "indirect_jump");
7975 15 : const char *dests_id = r.make_tmp_identifier ("destinations_for", this);
7976 15 : r.write (" gcc_jit_block *%s[%i] = {\n",
7977 : dests_id,
7978 : m_destinations.length ());
7979 65 : for (unsigned i = 0; i < m_destinations.length (); i++)
7980 35 : r.write (" %s,\n", r.get_identifier (m_destinations[i]));
7981 15 : r.write (" };\n");
7982 30 : r.write (" gcc_jit_block_end_with_indirect_jump (%s,\n"
7983 : " %s,\n"
7984 : " %s,\n"
7985 : " %i,\n"
7986 : " %s);\n",
7987 15 : r.get_identifier (get_block ()),
7988 15 : r.get_identifier (get_loc ()),
7989 : r.get_identifier_as_rvalue (m_target_address),
7990 : m_destinations.length (),
7991 : dests_id);
7992 15 : }
7993 :
7994 : /* The implementation of class gcc::jit::recording::return_. */
7995 :
7996 : /* Implementation of pure virtual hook recording::memento::replay_into
7997 : for recording::return_. */
7998 :
7999 : void
8000 4717 : recording::return_::replay_into (replayer *r)
8001 : {
8002 4717 : playback_block (get_block ())
8003 9434 : ->add_return (playback_location (r),
8004 4717 : m_rvalue ? m_rvalue->playback_rvalue () : NULL);
8005 4717 : }
8006 :
8007 : /* Override the poisoned default implementation of
8008 : gcc::jit::recording::statement::get_successor_blocks
8009 :
8010 : A return statement has no successor block. */
8011 :
8012 : vec <recording::block *>
8013 3972 : recording::return_::get_successor_blocks () const
8014 : {
8015 3972 : vec <block *> result;
8016 3972 : result.create (0);
8017 3972 : return result;
8018 : }
8019 :
8020 : /* Implementation of recording::memento::make_debug_string for
8021 : a return statement (covers both those with and without rvalues). */
8022 :
8023 : recording::string *
8024 48 : recording::return_::make_debug_string ()
8025 : {
8026 48 : if (m_rvalue)
8027 39 : return string::from_printf (m_ctxt,
8028 : "return %s;",
8029 39 : m_rvalue->get_debug_string ());
8030 : else
8031 9 : return string::from_printf (m_ctxt,
8032 9 : "return;");
8033 : }
8034 :
8035 : /* Implementation of recording::memento::write_reproducer for
8036 : return statements. */
8037 :
8038 : void
8039 2698 : recording::return_::write_reproducer (reproducer &r)
8040 : {
8041 2698 : if (m_rvalue)
8042 2150 : r.write (" gcc_jit_block_end_with_return (%s, /*gcc_jit_block *block */\n"
8043 : " %s, /* gcc_jit_location *loc */\n"
8044 : " %s); /* gcc_jit_rvalue *rvalue */\n",
8045 2150 : r.get_identifier (get_block ()),
8046 2150 : r.get_identifier (get_loc ()),
8047 : r.get_identifier_as_rvalue (m_rvalue));
8048 : else
8049 548 : r.write (" gcc_jit_block_end_with_void_return (%s, /*gcc_jit_block *block */\n"
8050 : " %s); /* gcc_jit_location *loc */\n",
8051 548 : r.get_identifier (get_block ()),
8052 548 : r.get_identifier (get_loc ()));
8053 2698 : }
8054 :
8055 : /* The implementation of class gcc::jit::recording::case_. */
8056 :
8057 : void
8058 75 : recording::case_::write_reproducer (reproducer &r)
8059 : {
8060 75 : const char *id = r.make_identifier (this, "case");
8061 75 : const char *fmt =
8062 : " gcc_jit_case *%s = \n"
8063 : " gcc_jit_context_new_case (%s, /*gcc_jit_context *ctxt */\n"
8064 : " %s, /* gcc_jit_rvalue *min_value */\n"
8065 : " %s, /* gcc_jit_rvalue *max_value */\n"
8066 : " %s); /* gcc_jit_block *dest_block */\n";
8067 75 : r.write (fmt,
8068 : id,
8069 : r.get_identifier (get_context ()),
8070 : r.get_identifier_as_rvalue (m_min_value),
8071 : r.get_identifier_as_rvalue (m_max_value),
8072 75 : r.get_identifier (m_dest_block));
8073 75 : }
8074 :
8075 : recording::string *
8076 75 : recording::case_::make_debug_string ()
8077 : {
8078 75 : return string::from_printf (get_context (),
8079 : "case %s ... %s: goto %s;",
8080 75 : m_min_value->get_debug_string (),
8081 75 : m_max_value->get_debug_string (),
8082 75 : m_dest_block->get_debug_string ());
8083 : }
8084 :
8085 : /* The implementation of class gcc::jit::recording::switch_. */
8086 :
8087 : /* gcc::jit::recording::switch_'s constructor. */
8088 :
8089 20 : recording::switch_::switch_ (block *b,
8090 : location *loc,
8091 : rvalue *expr,
8092 : block *default_block,
8093 : int num_cases,
8094 : case_ **cases)
8095 : : statement (b, loc),
8096 20 : m_expr (expr),
8097 20 : m_default_block (default_block)
8098 : {
8099 20 : m_cases.reserve_exact (num_cases);
8100 120 : for (int i = 0; i< num_cases; i++)
8101 80 : m_cases.quick_push (cases[i]);
8102 20 : }
8103 :
8104 : /* Implementation of pure virtual hook recording::memento::replay_into
8105 : for recording::switch_. */
8106 :
8107 : void
8108 25 : recording::switch_::replay_into (replayer *r)
8109 : {
8110 25 : auto_vec <playback::case_> pcases;
8111 25 : int i;
8112 25 : recording::case_ *rcase;
8113 50 : pcases.reserve_exact (m_cases.length ());
8114 150 : FOR_EACH_VEC_ELT (m_cases, i, rcase)
8115 : {
8116 100 : playback::case_ pcase (rcase->get_min_value ()->playback_rvalue (),
8117 : rcase->get_max_value ()->playback_rvalue (),
8118 100 : rcase->get_dest_block ()->playback_block ());
8119 100 : pcases.safe_push (pcase);
8120 : }
8121 25 : playback_block (get_block ())
8122 50 : ->add_switch (playback_location (r),
8123 25 : m_expr->playback_rvalue (),
8124 25 : m_default_block->playback_block (),
8125 : &pcases);
8126 25 : }
8127 :
8128 : /* Override the poisoned default implementation of
8129 : gcc::jit::recording::statement::get_successor_blocks
8130 :
8131 : A switch statement has (NUM_CASES + 1) successor blocks. */
8132 :
8133 : vec <recording::block *>
8134 20 : recording::switch_::get_successor_blocks () const
8135 : {
8136 20 : vec <block *> result;
8137 40 : result.create (m_cases.length () + 1);
8138 20 : result.quick_push (m_default_block);
8139 20 : int i;
8140 20 : case_ *c;
8141 120 : FOR_EACH_VEC_ELT (m_cases, i, c)
8142 80 : result.quick_push (c->get_dest_block ());
8143 20 : return result;
8144 : }
8145 :
8146 : /* Implementation of recording::memento::make_debug_string for
8147 : a switch statement. */
8148 :
8149 : recording::string *
8150 15 : recording::switch_::make_debug_string ()
8151 : {
8152 15 : auto_vec <char> cases_str;
8153 15 : int i;
8154 15 : case_ *c;
8155 75 : FOR_EACH_VEC_ELT (m_cases, i, c)
8156 : {
8157 60 : size_t len = strlen (c->get_debug_string ());
8158 60 : unsigned idx = cases_str.length ();
8159 60 : cases_str.safe_grow (idx + 1 + len, true);
8160 60 : cases_str[idx] = ' ';
8161 60 : memcpy (&(cases_str[idx + 1]),
8162 60 : c->get_debug_string (),
8163 : len);
8164 : }
8165 15 : cases_str.safe_push ('\0');
8166 :
8167 30 : return string::from_printf (m_ctxt,
8168 : "switch (%s) {default: goto %s;%s}",
8169 15 : m_expr->get_debug_string (),
8170 15 : m_default_block->get_debug_string (),
8171 15 : &cases_str[0]);
8172 15 : }
8173 :
8174 : /* Implementation of recording::memento::write_reproducer for
8175 : switch statements. */
8176 :
8177 : void
8178 15 : recording::switch_::write_reproducer (reproducer &r)
8179 : {
8180 15 : r.make_identifier (this, "switch");
8181 15 : int i;
8182 15 : case_ *c;
8183 15 : const char *cases_id =
8184 15 : r.make_tmp_identifier ("cases_for", this);
8185 15 : r.write (" gcc_jit_case *%s[%i] = {\n",
8186 : cases_id,
8187 : m_cases.length ());
8188 90 : FOR_EACH_VEC_ELT (m_cases, i, c)
8189 60 : r.write (" %s,\n", r.get_identifier (c));
8190 15 : r.write (" };\n");
8191 15 : const char *fmt =
8192 : " gcc_jit_block_end_with_switch (%s, /*gcc_jit_block *block */\n"
8193 : " %s, /* gcc_jit_location *loc */\n"
8194 : " %s, /* gcc_jit_rvalue *expr */\n"
8195 : " %s, /* gcc_jit_block *default_block */\n"
8196 : " %i, /* int num_cases */\n"
8197 : " %s); /* gcc_jit_case **cases */\n";
8198 30 : r.write (fmt,
8199 15 : r.get_identifier (get_block ()),
8200 15 : r.get_identifier (get_loc ()),
8201 : r.get_identifier_as_rvalue (m_expr),
8202 15 : r.get_identifier (m_default_block),
8203 : m_cases.length (),
8204 : cases_id);
8205 15 : }
8206 :
8207 : /* class asm_operand : public memento. */
8208 :
8209 100 : recording::asm_operand::asm_operand (extended_asm *ext_asm,
8210 : string *asm_symbolic_name,
8211 : string *constraint)
8212 : : memento (ext_asm->get_context ()),
8213 100 : m_ext_asm (ext_asm),
8214 100 : m_asm_symbolic_name (asm_symbolic_name),
8215 100 : m_constraint (constraint)
8216 : {
8217 100 : }
8218 :
8219 : void
8220 100 : recording::asm_operand::print (pretty_printer *pp) const
8221 : {
8222 100 : if (m_asm_symbolic_name)
8223 : {
8224 20 : pp_character (pp, '[');
8225 20 : pp_string (pp, m_asm_symbolic_name->c_str ());
8226 20 : pp_character (pp, ']');
8227 20 : pp_space (pp);
8228 : }
8229 100 : pp_string (pp, m_constraint->get_debug_string ());
8230 : /* Subclass will add lvalue/rvalue. */
8231 100 : }
8232 :
8233 : recording::string *
8234 0 : recording::asm_operand::make_debug_string ()
8235 : {
8236 0 : pretty_printer pp;
8237 0 : print (&pp);
8238 0 : return m_ctxt->new_string (pp_formatted_text (&pp), false);
8239 0 : }
8240 :
8241 : /* class output_asm_operand : public asm_operand. */
8242 :
8243 : void
8244 40 : recording::output_asm_operand::write_reproducer (reproducer &r)
8245 : {
8246 40 : const char *fmt =
8247 : " gcc_jit_extended_asm_add_output_operand (%s, /* gcc_jit_extended_asm *ext_asm */\n"
8248 : " %s, /* const char *asm_symbolic_name */\n"
8249 : " %s, /* const char *constraint */\n"
8250 : " %s); /* gcc_jit_lvalue *dest */\n";
8251 40 : r.write (fmt,
8252 40 : r.get_identifier (m_ext_asm),
8253 40 : (m_asm_symbolic_name
8254 10 : ? m_asm_symbolic_name->get_debug_string () : "NULL"),
8255 40 : m_constraint->get_debug_string (),
8256 40 : r.get_identifier (m_dest));
8257 40 : }
8258 :
8259 : void
8260 40 : recording::output_asm_operand::print (pretty_printer *pp) const
8261 : {
8262 40 : asm_operand::print (pp);
8263 40 : pp_string (pp, " (");
8264 40 : pp_string (pp, m_dest->get_debug_string ());
8265 40 : pp_string (pp, ")");
8266 40 : }
8267 :
8268 : /* class input_asm_operand : public asm_operand. */
8269 :
8270 : void
8271 60 : recording::input_asm_operand::write_reproducer (reproducer &r)
8272 : {
8273 60 : const char *fmt =
8274 : " gcc_jit_extended_asm_add_input_operand (%s, /* gcc_jit_extended_asm *ext_asm */\n"
8275 : " %s, /* const char *asm_symbolic_name */\n"
8276 : " %s, /* const char *constraint */\n"
8277 : " %s); /* gcc_jit_rvalue *src */\n";
8278 120 : r.write (fmt,
8279 60 : r.get_identifier (m_ext_asm),
8280 60 : (m_asm_symbolic_name
8281 10 : ? m_asm_symbolic_name->get_debug_string () : "NULL"),
8282 60 : m_constraint->get_debug_string (),
8283 : r.get_identifier_as_rvalue (m_src));
8284 60 : }
8285 :
8286 : void
8287 60 : recording::input_asm_operand::print (pretty_printer *pp) const
8288 : {
8289 60 : asm_operand::print (pp);
8290 60 : pp_string (pp, " (");
8291 60 : pp_string (pp, m_src->get_debug_string ());
8292 60 : pp_string (pp, ")");
8293 60 : }
8294 :
8295 : /* The implementation of class gcc::jit::recording::extended_asm. */
8296 :
8297 : void
8298 40 : recording::extended_asm::add_output_operand (const char *asm_symbolic_name,
8299 : const char *constraint,
8300 : lvalue *dest)
8301 : {
8302 40 : output_asm_operand *op
8303 : = new output_asm_operand (this,
8304 : new_string (asm_symbolic_name),
8305 : new_string (constraint),
8306 40 : dest);
8307 40 : m_ctxt->record (op);
8308 40 : m_output_ops.safe_push (op);
8309 40 : }
8310 :
8311 : void
8312 60 : recording::extended_asm::add_input_operand (const char *asm_symbolic_name,
8313 : const char *constraint,
8314 : rvalue *src)
8315 : {
8316 60 : input_asm_operand *op
8317 : = new input_asm_operand (this,
8318 : new_string (asm_symbolic_name),
8319 : new_string (constraint),
8320 60 : src);
8321 60 : m_ctxt->record (op);
8322 60 : m_input_ops.safe_push (op);
8323 60 : }
8324 :
8325 : void
8326 50 : recording::extended_asm::add_clobber (const char *victim)
8327 : {
8328 50 : m_clobbers.safe_push (new_string (victim));
8329 50 : }
8330 :
8331 : /* Implementation of recording::memento::replay_into
8332 : for recording::extended_asm. */
8333 :
8334 : void
8335 70 : recording::extended_asm::replay_into (replayer *r)
8336 : {
8337 70 : auto_vec<playback::asm_operand> playback_output_ops;
8338 70 : auto_vec<playback::asm_operand> playback_input_ops;
8339 70 : auto_vec<const char *> playback_clobbers;
8340 70 : auto_vec<playback::block *> playback_goto_blocks;
8341 :
8342 : /* Populate outputs. */
8343 70 : {
8344 70 : output_asm_operand *rec_asm_op;
8345 70 : unsigned i;
8346 110 : FOR_EACH_VEC_ELT (m_output_ops, i, rec_asm_op)
8347 : {
8348 40 : playback::asm_operand playback_asm_op
8349 : (rec_asm_op->get_symbolic_name (),
8350 : rec_asm_op->get_constraint (),
8351 50 : rec_asm_op->get_lvalue ()->playback_lvalue ()->as_tree ());
8352 40 : playback_output_ops.safe_push (playback_asm_op);
8353 : }
8354 : }
8355 :
8356 : /* Populate inputs. */
8357 : {
8358 : input_asm_operand *rec_asm_op;
8359 : unsigned i;
8360 130 : FOR_EACH_VEC_ELT (m_input_ops, i, rec_asm_op)
8361 : {
8362 60 : playback::asm_operand playback_asm_op
8363 : (rec_asm_op->get_symbolic_name (),
8364 : rec_asm_op->get_constraint (),
8365 70 : rec_asm_op->get_rvalue ()->playback_rvalue ()->as_tree ());
8366 60 : playback_input_ops.safe_push (playback_asm_op);
8367 : }
8368 : }
8369 :
8370 : /* Populate clobbers. */
8371 : {
8372 : string *rec_clobber;
8373 : unsigned i;
8374 120 : FOR_EACH_VEC_ELT (m_clobbers, i, rec_clobber)
8375 50 : playback_clobbers.safe_push (rec_clobber->c_str ());
8376 : }
8377 :
8378 : /* Populate playback blocks if an "asm goto". */
8379 70 : maybe_populate_playback_blocks (&playback_goto_blocks);
8380 :
8381 70 : playback_block (get_block ())
8382 140 : ->add_extended_asm (playback_location (r),
8383 70 : m_asm_template->c_str (),
8384 : m_is_volatile, m_is_inline,
8385 : &playback_output_ops,
8386 : &playback_input_ops,
8387 : &playback_clobbers,
8388 : &playback_goto_blocks);
8389 70 : }
8390 :
8391 : /* Implementation of recording::memento::make_debug_string for
8392 : an extended_asm "statement". */
8393 :
8394 : recording::string *
8395 70 : recording::extended_asm::make_debug_string ()
8396 : {
8397 70 : pretty_printer pp;
8398 70 : pp_string (&pp, "asm ");
8399 70 : if (m_is_volatile)
8400 20 : pp_string (&pp, "volatile ");
8401 70 : if (m_is_inline)
8402 0 : pp_string (&pp, "inline ");
8403 70 : if (is_goto ())
8404 20 : pp_string (&pp, "goto ");
8405 70 : pp_character (&pp, '(');
8406 70 : pp_string (&pp, m_asm_template->get_debug_string ());
8407 70 : pp_string (&pp, " : ");
8408 70 : unsigned i;
8409 70 : {
8410 70 : output_asm_operand *asm_op;
8411 180 : FOR_EACH_VEC_ELT (m_output_ops, i, asm_op)
8412 : {
8413 40 : if (i > 0)
8414 0 : pp_string (&pp, ", ");
8415 40 : asm_op->print (&pp);
8416 : }
8417 : }
8418 70 : pp_string (&pp, " : ");
8419 70 : {
8420 70 : input_asm_operand *asm_op;
8421 200 : FOR_EACH_VEC_ELT (m_input_ops, i, asm_op)
8422 : {
8423 60 : if (i > 0)
8424 20 : pp_string (&pp, ", ");
8425 60 : asm_op->print (&pp);
8426 : }
8427 : }
8428 70 : pp_string (&pp, " : ");
8429 70 : string *rec_clobber;
8430 190 : FOR_EACH_VEC_ELT (m_clobbers, i, rec_clobber)
8431 : {
8432 50 : if (i > 0)
8433 0 : pp_string (&pp, ", ");
8434 50 : pp_string (&pp, rec_clobber->get_debug_string ());
8435 : }
8436 70 : maybe_print_gotos (&pp);
8437 70 : pp_character (&pp, ')');
8438 70 : return new_string (pp_formatted_text (&pp));
8439 70 : }
8440 :
8441 : void
8442 70 : recording::extended_asm::write_flags (reproducer &r)
8443 : {
8444 70 : if (m_is_volatile)
8445 20 : r.write (" gcc_jit_extended_asm_set_volatile_flag (%s, 1);\n",
8446 : r.get_identifier (this));
8447 70 : if (m_is_inline)
8448 0 : r.write (" gcc_jit_extended_asm_set_inline_flag (%s, 1);\n",
8449 : r.get_identifier (this));
8450 70 : }
8451 :
8452 : void
8453 70 : recording::extended_asm::write_clobbers (reproducer &r)
8454 : {
8455 70 : string *clobber;
8456 70 : unsigned i;
8457 120 : FOR_EACH_VEC_ELT (m_clobbers, i, clobber)
8458 50 : r.write (" gcc_jit_extended_asm_add_clobber (%s, %s);\n",
8459 : r.get_identifier (this),
8460 : clobber->get_debug_string ());
8461 70 : }
8462 :
8463 : /* Implementation of recording::memento::write_reproducer for
8464 : extended_asm_simple. */
8465 :
8466 : void
8467 50 : recording::extended_asm_simple::write_reproducer (reproducer &r)
8468 : {
8469 50 : const char *id = r.make_identifier (this, "extended_asm");
8470 50 : r.write (" gcc_jit_extended_asm *%s =\n"
8471 : " gcc_jit_block_add_extended_asm (%s, /*gcc_jit_block *block */\n"
8472 : " %s, /* gcc_jit_location *loc */\n"
8473 : " %s); /* const char *asm_template */\n",
8474 : id,
8475 50 : r.get_identifier (get_block ()),
8476 50 : r.get_identifier (get_loc ()),
8477 50 : m_asm_template->get_debug_string ());
8478 50 : write_flags (r);
8479 50 : write_clobbers (r);
8480 50 : }
8481 :
8482 : void
8483 0 : recording::extended_asm::
8484 : maybe_populate_playback_blocks (auto_vec <playback::block *> *)
8485 : {
8486 : /* Do nothing; not an "asm goto". */
8487 0 : }
8488 :
8489 : /* The implementation of class gcc::jit::recording::extended_asm_goto. */
8490 :
8491 : /* recording::extended_asm_goto's ctor. */
8492 :
8493 20 : recording::extended_asm_goto::extended_asm_goto (block *b,
8494 : location *loc,
8495 : string *asm_template,
8496 : int num_goto_blocks,
8497 : block **goto_blocks,
8498 : block *fallthrough_block)
8499 : : extended_asm (b, loc, asm_template),
8500 40 : m_goto_blocks (num_goto_blocks),
8501 20 : m_fallthrough_block (fallthrough_block)
8502 : {
8503 40 : for (int i = 0; i < num_goto_blocks; i++)
8504 20 : m_goto_blocks.quick_push (goto_blocks[i]);
8505 20 : }
8506 :
8507 : /* Implementation of recording::memento::replay_into
8508 : for recording::extended_asm_goto. */
8509 :
8510 : void
8511 20 : recording::extended_asm_goto::replay_into (replayer *r)
8512 : {
8513 : /* Chain up to base class impl. */
8514 20 : recording::extended_asm::replay_into (r);
8515 :
8516 : /* ...and potentially add a goto for the fallthrough. */
8517 20 : if (m_fallthrough_block)
8518 20 : playback_block (get_block ())
8519 20 : ->add_jump (playback_location (r),
8520 20 : m_fallthrough_block->playback_block ());
8521 20 : }
8522 :
8523 : /* Implementation of recording::memento::write_reproducer for
8524 : extended_asm_goto. */
8525 :
8526 : void
8527 20 : recording::extended_asm_goto::write_reproducer (reproducer &r)
8528 : {
8529 20 : const char *id = r.make_identifier (this, "extended_asm");
8530 20 : const char *blocks_id = r.make_tmp_identifier ("blocks_for", this);
8531 20 : r.write (" gcc_jit_block *%s[%i] = {\n",
8532 : blocks_id,
8533 : m_goto_blocks.length ());
8534 20 : int i;
8535 20 : block *b;
8536 60 : FOR_EACH_VEC_ELT (m_goto_blocks, i, b)
8537 20 : r.write (" %s,\n", r.get_identifier (b));
8538 20 : r.write (" };\n");
8539 40 : r.write (" gcc_jit_extended_asm *%s =\n"
8540 : " gcc_jit_block_end_with_extended_asm_goto (%s, /*gcc_jit_block *block */\n"
8541 : " %s, /* gcc_jit_location *loc */\n"
8542 : " %s, /* const char *asm_template */\n"
8543 : " %i, /* int num_goto_blocks */\n"
8544 : " %s, /* gcc_jit_block **goto_blocks */\n"
8545 : " %s); /* gcc_jit_block *fallthrough_block */\n",
8546 : id,
8547 20 : r.get_identifier (get_block ()),
8548 20 : r.get_identifier (get_loc ()),
8549 20 : m_asm_template->get_debug_string (),
8550 : m_goto_blocks.length (),
8551 : blocks_id,
8552 20 : (m_fallthrough_block
8553 20 : ? r.get_identifier (m_fallthrough_block)
8554 : : "NULL"));
8555 20 : write_flags (r);
8556 20 : write_clobbers (r);
8557 20 : }
8558 :
8559 : /* Override the poisoned default implementation of
8560 : gcc::jit::recording::statement::get_successor_blocks
8561 :
8562 : An extended_asm_goto can jump to the m_goto_blocks, and to
8563 : the (optional) m_fallthrough_block. */
8564 :
8565 : vec <recording::block *>
8566 20 : recording::extended_asm_goto::get_successor_blocks () const
8567 : {
8568 20 : vec <block *> result;
8569 40 : result.create (m_goto_blocks.length () + 1);
8570 20 : if (m_fallthrough_block)
8571 20 : result.quick_push (m_fallthrough_block);
8572 20 : result.splice (m_goto_blocks);
8573 20 : return result;
8574 : }
8575 :
8576 : /* Vfunc for use by recording::extended_asm::make_debug_string. */
8577 :
8578 : void
8579 20 : recording::extended_asm_goto::maybe_print_gotos (pretty_printer *pp) const
8580 : {
8581 20 : pp_string (pp, " : ");
8582 20 : unsigned i;
8583 20 : block *b;
8584 60 : FOR_EACH_VEC_ELT (m_goto_blocks, i, b)
8585 : {
8586 20 : if (i > 0)
8587 0 : pp_string (pp, ", ");
8588 20 : pp_string (pp, b->get_debug_string ());
8589 : }
8590 : /* Non-C syntax here. */
8591 20 : if (m_fallthrough_block)
8592 20 : pp_printf (pp, " [fallthrough: %s]",
8593 : m_fallthrough_block->get_debug_string ());
8594 20 : }
8595 :
8596 : /* Vfunc for use by recording::extended_asm::replay_into. */
8597 :
8598 : void
8599 20 : recording::extended_asm_goto::
8600 : maybe_populate_playback_blocks (auto_vec <playback::block *> *out)
8601 : {
8602 20 : unsigned i;
8603 20 : block *b;
8604 40 : FOR_EACH_VEC_ELT (m_goto_blocks, i, b)
8605 20 : out->safe_push (b->playback_block ());
8606 20 : }
8607 :
8608 : /* class top_level_asm : public memento. */
8609 :
8610 10 : recording::top_level_asm::top_level_asm (context *ctxt,
8611 : location *loc,
8612 : string *asm_stmts)
8613 : : memento (ctxt),
8614 10 : m_loc (loc),
8615 10 : m_asm_stmts (asm_stmts)
8616 : {
8617 10 : }
8618 :
8619 : /* Implementation of recording::memento::replay_into for top-level asm. */
8620 :
8621 : void
8622 10 : recording::top_level_asm::replay_into (replayer *r)
8623 : {
8624 10 : r->add_top_level_asm (m_asm_stmts->c_str ());
8625 10 : }
8626 :
8627 : /* Implementation of recording::memento::make_debug_string for
8628 : top-level asm. */
8629 :
8630 : recording::string *
8631 0 : recording::top_level_asm::make_debug_string ()
8632 : {
8633 0 : return string::from_printf (m_ctxt, "asm (%s)",
8634 0 : m_asm_stmts->get_debug_string ());
8635 : }
8636 :
8637 : /* Override the default implementation of
8638 : recording::memento::write_to_dump.
8639 : Don't indent the string. */
8640 :
8641 : void
8642 0 : recording::top_level_asm::write_to_dump (dump &d)
8643 : {
8644 0 : d.write ("%s;\n", get_debug_string ());
8645 0 : }
8646 :
8647 : /* Implementation of recording::memento::write_reproducer for top-level asm. */
8648 :
8649 : void
8650 10 : recording::top_level_asm::write_reproducer (reproducer &r)
8651 : {
8652 10 : r.write (" gcc_jit_context_add_top_level_asm (%s, /* gcc_jit_context *ctxt */\n"
8653 : " %s, /* gcc_jit_location *loc */\n"
8654 : " %s); /* const char *asm_stmts */\n",
8655 : r.get_identifier (get_context ()),
8656 10 : r.get_identifier (m_loc),
8657 10 : m_asm_stmts->get_debug_string ());
8658 10 : }
8659 :
8660 : void
8661 1120 : recording::global_init_rvalue::replay_into (replayer *r)
8662 : {
8663 1120 : r->global_set_init_rvalue (m_variable->playback_lvalue (),
8664 1120 : m_init->playback_rvalue ());
8665 1120 : }
8666 :
8667 : void
8668 590 : recording::global_init_rvalue::write_reproducer (reproducer &r)
8669 : {
8670 1180 : r.write (
8671 : " gcc_jit_global_set_initializer_rvalue (%s, /* lvalue *global */\n"
8672 : " %s);/* rvalue *init */\n",
8673 590 : r.get_identifier (m_variable),
8674 : r.get_identifier_as_rvalue (m_init));
8675 590 : }
8676 :
8677 : void
8678 0 : recording::global_init_rvalue::write_to_dump (dump &d)
8679 : {
8680 0 : d.write ("%s;\n", get_debug_string ());
8681 0 : }
8682 :
8683 : recording::string *
8684 0 : recording::global_init_rvalue::make_debug_string ()
8685 : {
8686 0 : return string::from_printf (m_ctxt, "%s = %s",
8687 0 : m_variable->get_debug_string (),
8688 0 : m_init->get_debug_string ());
8689 : }
8690 :
8691 : enum strip_flags {
8692 : STRIP_FLAG_NONE,
8693 : STRIP_FLAG_ARR,
8694 : STRIP_FLAG_VEC
8695 : };
8696 :
8697 : /* Strips type down to array, vector or base type (whichever comes first)
8698 :
8699 : Also saves 'ptr_depth' and sets 'flags' for array or vector types. */
8700 : static
8701 : recording::type *
8702 460 : strip_and_count (recording::type *type_to_strip,
8703 : int &ptr_depth,
8704 : strip_flags &flags)
8705 : {
8706 460 : recording::type *t = type_to_strip;
8707 :
8708 810 : while (true)
8709 : {
8710 810 : if (!t)
8711 0 : gcc_unreachable (); /* Should only happen on corrupt input. */
8712 :
8713 810 : recording::type *pointed_to_type = t->is_pointer ();
8714 810 : if (pointed_to_type != NULL)
8715 : {
8716 90 : ptr_depth++;
8717 90 : t = pointed_to_type;
8718 90 : continue;
8719 : }
8720 :
8721 720 : recording::type *array_el = t->is_array ();
8722 720 : if (array_el != NULL)
8723 : {
8724 30 : flags = STRIP_FLAG_ARR;
8725 30 : break;
8726 : }
8727 :
8728 690 : recording::type *vec = t->dyn_cast_vector_type ();
8729 690 : if (vec != NULL)
8730 : {
8731 0 : flags = STRIP_FLAG_VEC;
8732 0 : break;
8733 : }
8734 :
8735 : /* unqualified () returns 'this' on base types. */
8736 690 : recording::type *next = t->unqualified ();
8737 690 : if (next == t)
8738 : {
8739 : break;
8740 : }
8741 : t = next;
8742 : }
8743 :
8744 460 : return t;
8745 : }
8746 :
8747 : /* Strip qualifiers and count pointer depth, returning true
8748 : if the types' base type and pointer depth are
8749 : the same, otherwise false.
8750 :
8751 : For array and vector types the number of element also
8752 : has to match.
8753 :
8754 : Do not call this directly. Call 'types_kinda_same'. */
8755 : bool
8756 230 : types_kinda_same_internal (recording::type *a, recording::type *b)
8757 : {
8758 230 : int ptr_depth_a = 0;
8759 230 : int ptr_depth_b = 0;
8760 230 : recording::type *base_a;
8761 230 : recording::type *base_b;
8762 :
8763 230 : strip_flags flags_a = STRIP_FLAG_NONE;
8764 230 : strip_flags flags_b = STRIP_FLAG_NONE;
8765 :
8766 230 : base_a = strip_and_count (a, ptr_depth_a, flags_a);
8767 230 : base_b = strip_and_count (b, ptr_depth_b, flags_b);
8768 :
8769 230 : if (ptr_depth_a != ptr_depth_b)
8770 : return false;
8771 :
8772 230 : if (base_a == base_b)
8773 : return true;
8774 :
8775 30 : if (flags_a != flags_b)
8776 : return false;
8777 :
8778 : /* If the "base type" is an array or vector we might need to
8779 : check deeper. */
8780 30 : if (flags_a == STRIP_FLAG_ARR)
8781 : {
8782 15 : recording::array_type *arr_a =
8783 : static_cast<recording::array_type*> (base_a);
8784 15 : recording::array_type *arr_b =
8785 : static_cast<recording::array_type*> (base_b);
8786 :
8787 15 : if (arr_a->num_elements () != arr_b->num_elements ())
8788 : return false;
8789 :
8790 : /* is_array returns element type. */
8791 15 : recording::type *el_a = arr_a->is_array ();
8792 15 : recording::type *el_b = arr_b->is_array ();
8793 :
8794 15 : if (el_a == el_b)
8795 : return true;
8796 :
8797 : return types_kinda_same_internal (el_a, el_b);
8798 : }
8799 15 : if (flags_a == STRIP_FLAG_VEC)
8800 : {
8801 0 : recording::vector_type *arr_a =
8802 : static_cast<recording::vector_type*> (base_a);
8803 0 : recording::vector_type *arr_b =
8804 : static_cast<recording::vector_type*> (base_b);
8805 :
8806 0 : if (arr_a->get_num_units () != arr_b->get_num_units ())
8807 : return false;
8808 :
8809 0 : recording::type *el_a = arr_a->get_element_type ();
8810 0 : recording::type *el_b = arr_b->get_element_type ();
8811 :
8812 0 : if (el_a == el_b)
8813 : return true;
8814 :
8815 : return types_kinda_same_internal (el_a, el_b);
8816 : }
8817 :
8818 : return false;
8819 : }
8820 :
8821 : } // namespace gcc::jit
8822 :
8823 : } // namespace gcc
|