Debian Codegraph @

gcc/jit/jit-recording.cc

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