hppa: Export main in pr104869.C on hpux
[official-gcc.git] / gcc / jit / jit-recording.h
blob4a8082991fbf3f6d3f2ec4e89c75800cf120b9c0
1 /* Internals of libgccjit: classes for recording calls made to the JIT API.
2 Copyright (C) 2013-2023 Free Software Foundation, Inc.
3 Contributed by David Malcolm <dmalcolm@redhat.com>.
5 This file is part of GCC.
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.
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.
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/>. */
21 #ifndef JIT_RECORDING_H
22 #define JIT_RECORDING_H
24 #include "jit-common.h"
25 #include "jit-logging.h"
27 class timer;
29 namespace gcc {
31 namespace jit {
33 extern const char * const unary_op_reproducer_strings[];
34 extern const char * const binary_op_reproducer_strings[];
36 class result;
37 class dump;
38 class reproducer;
40 /**********************************************************************
41 Recording.
42 **********************************************************************/
44 namespace recording {
46 playback::location *
47 playback_location (replayer *r, location *loc);
49 const char *
50 playback_string (string *str);
52 playback::block *
53 playback_block (block *b);
55 /* A recording of a call to gcc_jit_context_enable_dump. */
56 struct requested_dump
58 const char *m_dumpname;
59 char **m_out_ptr;
62 /* A JIT-compilation context. */
63 class context : public log_user
65 public:
66 context (context *parent_ctxt);
67 ~context ();
69 builtins_manager *
70 get_builtins_manager ();
72 void record (memento *m);
73 void replay_into (replayer *r);
74 void disassociate_from_playback ();
76 string *
77 new_string (const char *text, bool escaped = false);
79 location *
80 new_location (const char *filename,
81 int line,
82 int column,
83 bool created_by_user);
85 type *
86 get_type (enum gcc_jit_types type);
88 type *
89 get_int_type (int num_bytes, int is_signed);
91 type *
92 new_array_type (location *loc,
93 type *element_type,
94 int num_elements);
96 field *
97 new_field (location *loc,
98 type *type,
99 const char *name);
101 field *
102 new_bitfield (location *loc,
103 type *type,
104 int width,
105 const char *name);
107 struct_ *
108 new_struct_type (location *loc,
109 const char *name);
111 union_ *
112 new_union_type (location *loc,
113 const char *name);
115 function_type *
116 new_function_type (type *return_type,
117 int num_params,
118 type **param_types,
119 int is_variadic);
121 type *
122 new_function_ptr_type (location *loc,
123 type *return_type,
124 int num_params,
125 type **param_types,
126 int is_variadic);
128 param *
129 new_param (location *loc,
130 type *type,
131 const char *name);
133 function *
134 new_function (location *loc,
135 enum gcc_jit_function_kind kind,
136 type *return_type,
137 const char *name,
138 int num_params,
139 param **params,
140 int is_variadic,
141 enum built_in_function builtin_id);
143 function *
144 get_builtin_function (const char *name);
146 lvalue *
147 new_global (location *loc,
148 enum gcc_jit_global_kind kind,
149 type *type,
150 const char *name);
152 rvalue *
153 new_ctor (location *loc,
154 type *type,
155 size_t num_values,
156 field **fields,
157 rvalue **values);
159 void
160 new_global_init_rvalue (lvalue *variable,
161 rvalue *init);
163 template <typename HOST_TYPE>
164 rvalue *
165 new_rvalue_from_const (type *type,
166 HOST_TYPE value);
168 rvalue *
169 new_string_literal (const char *value);
171 rvalue *
172 new_rvalue_from_vector (location *loc,
173 vector_type *type,
174 rvalue **elements);
176 rvalue *
177 new_unary_op (location *loc,
178 enum gcc_jit_unary_op op,
179 type *result_type,
180 rvalue *a);
182 rvalue *
183 new_binary_op (location *loc,
184 enum gcc_jit_binary_op op,
185 type *result_type,
186 rvalue *a, rvalue *b);
188 rvalue *
189 new_comparison (location *loc,
190 enum gcc_jit_comparison op,
191 rvalue *a, rvalue *b);
193 rvalue *
194 new_call (location *loc,
195 function *func,
196 int numargs, rvalue **args);
198 rvalue *
199 new_call_through_ptr (location *loc,
200 rvalue *fn_ptr,
201 int numargs, rvalue **args);
203 rvalue *
204 new_cast (location *loc,
205 rvalue *expr,
206 type *type_);
208 rvalue *
209 new_bitcast (location *loc,
210 rvalue *expr,
211 type *type_);
213 lvalue *
214 new_array_access (location *loc,
215 rvalue *ptr,
216 rvalue *index);
218 case_ *
219 new_case (rvalue *min_value,
220 rvalue *max_value,
221 block *block);
223 void
224 set_str_option (enum gcc_jit_str_option opt,
225 const char *value);
227 void
228 set_int_option (enum gcc_jit_int_option opt,
229 int value);
231 void
232 set_bool_option (enum gcc_jit_bool_option opt,
233 int value);
235 void
236 set_inner_bool_option (enum inner_bool_option inner_opt,
237 int value);
239 void
240 add_command_line_option (const char *optname);
242 void
243 append_command_line_options (vec <char *> *argvec);
245 void
246 add_driver_option (const char *optname);
248 void
249 append_driver_options (auto_string_vec *argvec);
251 void
252 enable_dump (const char *dumpname,
253 char **out_ptr);
255 const char *
256 get_str_option (enum gcc_jit_str_option opt) const
258 return m_str_options[opt];
262 get_int_option (enum gcc_jit_int_option opt) const
264 return m_int_options[opt];
268 get_bool_option (enum gcc_jit_bool_option opt) const
270 return m_bool_options[opt];
274 get_inner_bool_option (enum inner_bool_option opt) const
276 return m_inner_bool_options[opt];
279 result *
280 compile ();
282 void
283 compile_to_file (enum gcc_jit_output_kind output_kind,
284 const char *output_path);
286 void
287 add_error (location *loc, const char *fmt, ...)
288 GNU_PRINTF(3, 4);
290 void
291 add_error_va (location *loc, const char *fmt, va_list ap)
292 GNU_PRINTF(3, 0);
294 const char *
295 get_first_error () const;
297 const char *
298 get_last_error () const;
300 bool errors_occurred () const
302 if (m_parent_ctxt)
303 if (m_parent_ctxt->errors_occurred ())
304 return true;
305 return m_error_count;
308 type *get_opaque_FILE_type ();
310 void dump_to_file (const char *path, bool update_locations);
312 void dump_reproducer_to_file (const char *path);
314 void
315 get_all_requested_dumps (vec <recording::requested_dump> *out);
317 void set_timer (timer *t) { m_timer = t; }
318 timer *get_timer () const { return m_timer; }
320 void add_top_level_asm (location *loc, const char *asm_stmts);
322 private:
323 void log_all_options () const;
324 void log_str_option (enum gcc_jit_str_option opt) const;
325 void log_int_option (enum gcc_jit_int_option opt) const;
326 void log_bool_option (enum gcc_jit_bool_option opt) const;
327 void log_inner_bool_option (enum inner_bool_option opt) const;
329 void validate ();
331 private:
332 context *m_parent_ctxt;
334 /* The ultimate ancestor of the contexts within a family tree of
335 contexts. This has itself as its own m_toplevel_ctxt. */
336 context *m_toplevel_ctxt;
338 timer *m_timer;
340 int m_error_count;
342 char *m_first_error_str;
343 bool m_owns_first_error_str;
345 char *m_last_error_str;
346 bool m_owns_last_error_str;
348 char *m_str_options[GCC_JIT_NUM_STR_OPTIONS];
349 int m_int_options[GCC_JIT_NUM_INT_OPTIONS];
350 bool m_bool_options[GCC_JIT_NUM_BOOL_OPTIONS];
351 bool m_inner_bool_options[NUM_INNER_BOOL_OPTIONS];
352 auto_vec <char *> m_command_line_options;
353 auto_vec <char *> m_driver_options;
355 /* Dumpfiles that were requested via gcc_jit_context_enable_dump. */
356 auto_vec<requested_dump> m_requested_dumps;
358 /* Recorded API usage. */
359 auto_vec<memento *> m_mementos;
361 /* Specific recordings, for use by dump_to_file. */
362 auto_vec<compound_type *> m_compound_types;
363 auto_vec<global *> m_globals;
364 auto_vec<function *> m_functions;
365 auto_vec<top_level_asm *> m_top_level_asms;
367 type *m_basic_types[NUM_GCC_JIT_TYPES];
368 type *m_FILE_type;
370 builtins_manager *m_builtins_manager; // lazily created
374 /* An object with lifetime managed by the context i.e.
375 it lives until the context is released, at which
376 point it itself is cleaned up. */
378 class memento
380 public:
381 virtual ~memento () {}
383 /* Hook for replaying this. */
384 virtual void replay_into (replayer *r) = 0;
386 void set_playback_obj (void *obj) { m_playback_obj = obj; }
389 /* Get the context that owns this object.
391 Implements the post-error-checking part of
392 gcc_jit_object_get_context. */
393 context *get_context () { return m_ctxt; }
395 memento *
396 as_object () { return this; }
398 /* Debugging hook, for use in generating error messages etc.
399 Implements the post-error-checking part of
400 gcc_jit_object_get_debug_string. */
401 const char *
402 get_debug_string ();
404 virtual void write_to_dump (dump &d);
405 virtual void write_reproducer (reproducer &r) = 0;
406 virtual location *dyn_cast_location () { return NULL; }
408 memento (const memento&) = delete;
409 memento& operator= (const memento&) = delete;
411 protected:
412 memento (context *ctxt)
413 : m_ctxt (ctxt),
414 m_playback_obj (NULL),
415 m_debug_string (NULL)
417 gcc_assert (ctxt);
420 string *new_string (const char *text) { return m_ctxt->new_string (text); }
422 private:
423 virtual string * make_debug_string () = 0;
425 public:
426 context *m_ctxt;
428 protected:
429 void *m_playback_obj;
431 private:
432 string *m_debug_string;
435 /* or just use std::string? */
436 class string : public memento
438 public:
439 string (context *ctxt, const char *text, bool escaped);
440 ~string ();
442 const char *c_str () const { return m_buffer; }
444 static string * from_printf (context *ctxt, const char *fmt, ...)
445 GNU_PRINTF(2, 3);
447 void replay_into (replayer *) final override {}
449 string (const string&) = delete;
450 string& operator= (const string&) = delete;
452 private:
453 string * make_debug_string () final override;
454 void write_reproducer (reproducer &r) final override;
456 private:
457 size_t m_len;
458 char *m_buffer;
460 /* Flag to track if this string is the result of string::make_debug_string,
461 to avoid infinite recursion when logging all mementos: don't re-escape
462 such strings. */
463 bool m_escaped;
466 class location : public memento
468 public:
469 location (context *ctxt, string *filename, int line, int column,
470 bool created_by_user)
471 : memento (ctxt),
472 m_filename (filename),
473 m_line (line),
474 m_column (column),
475 m_created_by_user (created_by_user)
478 void replay_into (replayer *r) final override;
480 playback::location *
481 playback_location (replayer *r)
483 /* Normally during playback, we can walk forwards through the list of
484 recording objects, playing them back. The ordering of recording
485 ensures that everything that a recording object refers to has
486 already been played back, so we can simply look up the relevant
487 m_playback_obj.
489 Locations are an exception, due to the "write_to_dump" method of
490 recording::statement. This method can set a new location on a
491 statement after the statement is created, and thus the location
492 appears in the context's memento list *after* the statement that
493 refers to it.
495 In such circumstances, the statement is replayed *before* the location,
496 when the latter doesn't yet have a playback object.
498 Hence we need to ensure that locations have playback objects. */
499 if (!m_playback_obj)
501 replay_into (r);
503 gcc_assert (m_playback_obj);
504 return static_cast <playback::location *> (m_playback_obj);
507 location *dyn_cast_location () final override { return this; }
508 bool created_by_user () const { return m_created_by_user; }
510 private:
511 string * make_debug_string () final override;
512 void write_reproducer (reproducer &r) final override;
514 private:
515 string *m_filename;
516 int m_line;
517 int m_column;
518 bool m_created_by_user;
521 class type : public memento
523 public:
524 type *get_pointer ();
525 type *get_const ();
526 type *get_volatile ();
527 type *get_restrict ();
528 type *get_aligned (size_t alignment_in_bytes);
529 type *get_vector (size_t num_units);
531 /* Get the type obtained when dereferencing this type.
533 This will return NULL if it's not valid to dereference this type.
534 The caller is responsible for setting an error. */
535 virtual type *dereference () = 0;
536 /* Get the type size in bytes.
538 This is implemented only for memento_of_get_type and
539 memento_of_get_pointer as it is used for initializing globals of
540 these types. */
541 virtual size_t get_size () { gcc_unreachable (); }
543 /* Dynamic casts. */
544 virtual function_type *dyn_cast_function_type () { return NULL; }
545 virtual function_type *as_a_function_type() { gcc_unreachable (); return NULL; }
546 virtual struct_ *dyn_cast_struct () { return NULL; }
547 virtual vector_type *dyn_cast_vector_type () { return NULL; }
549 /* Is it typesafe to copy to this type from rtype? */
550 virtual bool accepts_writes_from (type *rtype)
552 gcc_assert (rtype);
553 return this->unqualified ()->is_same_type_as (rtype->unqualified ());
556 virtual bool is_same_type_as (type *other)
558 return this == other;
561 /* Strip off "const" etc */
562 virtual type *unqualified ()
564 return this;
567 virtual bool is_int () const = 0;
568 virtual bool is_float () const = 0;
569 virtual bool is_bool () const = 0;
570 virtual type *is_pointer () = 0;
571 virtual type *is_volatile () { return NULL; }
572 virtual type *is_restrict () { return NULL; }
573 virtual type *is_const () { return NULL; }
574 virtual type *is_array () = 0;
575 virtual struct_ *is_struct () { return NULL; }
576 virtual bool is_union () const { return false; }
577 virtual bool is_void () const { return false; }
578 virtual vector_type *is_vector () { return NULL; }
579 virtual bool has_known_size () const { return true; }
580 virtual bool is_signed () const = 0;
582 bool is_numeric () const
584 return is_int () || is_float () || is_bool ();
587 playback::type *
588 playback_type ()
590 return static_cast <playback::type *> (m_playback_obj);
593 virtual const char *access_as_type (reproducer &r);
595 protected:
596 type (context *ctxt)
597 : memento (ctxt),
598 m_pointer_to_this_type (NULL)
601 private:
602 type *m_pointer_to_this_type;
605 /* Result of "gcc_jit_context_get_type". */
606 class memento_of_get_type : public type
608 public:
609 memento_of_get_type (context *ctxt,
610 enum gcc_jit_types kind)
611 : type (ctxt),
612 m_kind (kind) {}
614 type *dereference () final override;
616 size_t get_size () final override;
618 bool accepts_writes_from (type *rtype) final override
620 if (m_kind == GCC_JIT_TYPE_VOID_PTR)
622 if (rtype->is_pointer ())
624 /* LHS (this) is type (void *), and the RHS is a pointer:
625 accept it: */
626 return true;
628 } else if (is_int ()
629 && rtype->is_int ()
630 && get_size () == rtype->get_size ()
631 && is_signed () == rtype->is_signed ())
633 /* LHS (this) is an integer of the same size and sign as rtype. */
634 return true;
637 return type::accepts_writes_from (rtype);
640 bool is_int () const final override;
641 bool is_float () const final override;
642 bool is_bool () const final override;
643 type *is_pointer () final override { return dereference (); }
644 type *is_array () final override { return NULL; }
645 bool is_void () const final override { return m_kind == GCC_JIT_TYPE_VOID; }
646 bool is_signed () const final override;
648 public:
649 void replay_into (replayer *r) final override;
651 private:
652 string * make_debug_string () final override;
653 void write_reproducer (reproducer &r) final override;
655 private:
656 enum gcc_jit_types m_kind;
659 /* Result of "gcc_jit_type_get_pointer". */
660 class memento_of_get_pointer : public type
662 public:
663 memento_of_get_pointer (type *other_type)
664 : type (other_type->m_ctxt),
665 m_other_type (other_type) {}
667 type *dereference () final override { return m_other_type; }
669 size_t get_size () final override;
671 bool accepts_writes_from (type *rtype) final override;
673 void replay_into (replayer *r) final override;
675 bool is_int () const final override { return false; }
676 bool is_float () const final override { return false; }
677 bool is_bool () const final override { return false; }
678 type *is_pointer () final override { return m_other_type; }
679 type *is_array () final override { return NULL; }
680 bool is_signed () const final override { return false; }
682 private:
683 string * make_debug_string () final override;
684 void write_reproducer (reproducer &r) final override;
686 private:
687 type *m_other_type;
690 /* A decorated version of a type, for get_const, get_volatile,
691 get_aligned, get_restrict, and get_vector. */
693 class decorated_type : public type
695 public:
696 decorated_type (type *other_type)
697 : type (other_type->m_ctxt),
698 m_other_type (other_type) {}
700 type *dereference () final override { return m_other_type->dereference (); }
702 size_t get_size () final override { return m_other_type->get_size (); };
704 bool is_int () const final override { return m_other_type->is_int (); }
705 bool is_float () const final override { return m_other_type->is_float (); }
706 bool is_bool () const final override { return m_other_type->is_bool (); }
707 type *is_pointer () final override { return m_other_type->is_pointer (); }
708 type *is_array () final override { return m_other_type->is_array (); }
709 struct_ *is_struct () final override { return m_other_type->is_struct (); }
710 bool is_signed () const final override { return m_other_type->is_signed (); }
712 protected:
713 type *m_other_type;
716 /* Result of "gcc_jit_type_get_const". */
717 class memento_of_get_const : public decorated_type
719 public:
720 memento_of_get_const (type *other_type)
721 : decorated_type (other_type) {}
723 bool accepts_writes_from (type */*rtype*/) final override
725 /* Can't write to a "const". */
726 return false;
729 /* Strip off the "const", giving the underlying type. */
730 type *unqualified () final override { return m_other_type; }
732 bool is_same_type_as (type *other) final override
734 if (!other->is_const ())
735 return false;
736 return m_other_type->is_same_type_as (other->is_const ());
739 type *is_const () final override { return m_other_type; }
741 void replay_into (replayer *) final override;
743 private:
744 string * make_debug_string () final override;
745 void write_reproducer (reproducer &r) final override;
748 /* Result of "gcc_jit_type_get_volatile". */
749 class memento_of_get_volatile : public decorated_type
751 public:
752 memento_of_get_volatile (type *other_type)
753 : decorated_type (other_type) {}
755 bool is_same_type_as (type *other) final override
757 if (!other->is_volatile ())
758 return false;
759 return m_other_type->is_same_type_as (other->is_volatile ());
762 /* Strip off the "volatile", giving the underlying type. */
763 type *unqualified () final override { return m_other_type; }
765 type *is_volatile () final override { return m_other_type; }
767 void replay_into (replayer *) final override;
769 private:
770 string * make_debug_string () final override;
771 void write_reproducer (reproducer &r) final override;
774 /* Result of "gcc_jit_type_get_restrict". */
775 class memento_of_get_restrict : public decorated_type
777 public:
778 memento_of_get_restrict (type *other_type)
779 : decorated_type (other_type) {}
781 bool is_same_type_as (type *other) final override
783 if (!other->is_restrict ())
784 return false;
785 return m_other_type->is_same_type_as (other->is_restrict ());
788 /* Strip off the "restrict", giving the underlying type. */
789 type *unqualified () final override { return m_other_type; }
791 type *is_restrict () final override { return m_other_type; }
793 void replay_into (replayer *) final override;
795 private:
796 string * make_debug_string () final override;
797 void write_reproducer (reproducer &r) final override;
800 /* Result of "gcc_jit_type_get_aligned". */
801 class memento_of_get_aligned : public decorated_type
803 public:
804 memento_of_get_aligned (type *other_type, size_t alignment_in_bytes)
805 : decorated_type (other_type),
806 m_alignment_in_bytes (alignment_in_bytes) {}
808 /* Strip off the alignment, giving the underlying type. */
809 type *unqualified () final override { return m_other_type; }
811 void replay_into (replayer *) final override;
813 private:
814 string * make_debug_string () final override;
815 void write_reproducer (reproducer &r) final override;
817 private:
818 size_t m_alignment_in_bytes;
821 /* Result of "gcc_jit_type_get_vector". */
822 class vector_type : public decorated_type
824 public:
825 vector_type (type *other_type, size_t num_units)
826 : decorated_type (other_type),
827 m_num_units (num_units) {}
829 size_t get_num_units () const { return m_num_units; }
831 vector_type *dyn_cast_vector_type () final override { return this; }
833 type *get_element_type () { return m_other_type; }
835 void replay_into (replayer *) final override;
837 bool is_same_type_as (type *other) final override
839 vector_type *other_vec_type = other->dyn_cast_vector_type ();
840 if (other_vec_type == NULL)
841 return false;
842 return get_num_units () == other_vec_type->get_num_units ()
843 && get_element_type () == other_vec_type->get_element_type ();
846 vector_type *is_vector () final override { return this; }
848 private:
849 string * make_debug_string () final override;
850 void write_reproducer (reproducer &r) final override;
852 private:
853 size_t m_num_units;
856 class array_type : public type
858 public:
859 array_type (context *ctxt,
860 location *loc,
861 type *element_type,
862 int num_elements)
863 : type (ctxt),
864 m_loc (loc),
865 m_element_type (element_type),
866 m_num_elements (num_elements)
869 type *dereference () final override;
871 bool is_int () const final override { return false; }
872 bool is_float () const final override { return false; }
873 bool is_bool () const final override { return false; }
874 type *is_pointer () final override { return NULL; }
875 type *is_array () final override { return m_element_type; }
876 int num_elements () { return m_num_elements; }
877 bool is_signed () const final override { return false; }
879 void replay_into (replayer *) final override;
881 private:
882 string * make_debug_string () final override;
883 void write_reproducer (reproducer &r) final override;
885 private:
886 location *m_loc;
887 type *m_element_type;
888 int m_num_elements;
891 class function_type : public type
893 public:
894 function_type (context *ctxt,
895 type *return_type,
896 int num_params,
897 type **param_types,
898 int is_variadic);
900 type *dereference () final override;
901 function_type *dyn_cast_function_type () final override { return this; }
902 function_type *as_a_function_type () final override { return this; }
904 bool is_same_type_as (type *other) final override;
906 bool is_int () const final override { return false; }
907 bool is_float () const final override { return false; }
908 bool is_bool () const final override { return false; }
909 type *is_pointer () final override { return NULL; }
910 type *is_array () final override { return NULL; }
911 bool is_signed () const final override { return false; }
913 void replay_into (replayer *) final override;
915 type * get_return_type () const { return m_return_type; }
916 const vec<type *> &get_param_types () const { return m_param_types; }
917 int is_variadic () const { return m_is_variadic; }
919 string * make_debug_string_with_ptr ();
921 void
922 write_deferred_reproducer (reproducer &r,
923 memento *ptr_type);
925 private:
926 string * make_debug_string () final override;
927 string * make_debug_string_with (const char *);
928 void write_reproducer (reproducer &r) final override;
930 private:
931 type *m_return_type;
932 auto_vec<type *> m_param_types;
933 int m_is_variadic;
936 class field : public memento
938 public:
939 field (context *ctxt,
940 location *loc,
941 type *type,
942 string *name)
943 : memento (ctxt),
944 m_loc (loc),
945 m_type (type),
946 m_name (name),
947 m_container (NULL)
950 type * get_type () const { return m_type; }
952 compound_type * get_container () const { return m_container; }
953 void set_container (compound_type *c) { m_container = c; }
955 void replay_into (replayer *) override;
957 void write_to_dump (dump &d) override;
959 playback::field *
960 playback_field () const
962 return static_cast <playback::field *> (m_playback_obj);
965 private:
966 string * make_debug_string () override;
967 void write_reproducer (reproducer &r) override;
969 protected:
970 location *m_loc;
971 type *m_type;
972 string *m_name;
973 compound_type *m_container;
977 class bitfield : public field
979 public:
980 bitfield (context *ctxt,
981 location *loc,
982 type *type,
983 int width,
984 string *name)
985 : field (ctxt, loc, type, name),
986 m_width (width)
989 void replay_into (replayer *) final override;
991 void write_to_dump (dump &d) final override;
993 private:
994 string * make_debug_string () final override;
995 void write_reproducer (reproducer &r) final override;
997 private:
998 int m_width;
1001 /* Base class for struct_ and union_ */
1002 class compound_type : public type
1004 public:
1005 compound_type (context *ctxt,
1006 location *loc,
1007 string *name);
1009 string *get_name () const { return m_name; }
1010 location *get_loc () const { return m_loc; }
1011 fields * get_fields () { return m_fields; }
1013 void
1014 set_fields (location *loc,
1015 int num_fields,
1016 field **fields);
1018 type *dereference () final override;
1020 bool is_int () const final override { return false; }
1021 bool is_float () const final override { return false; }
1022 bool is_bool () const final override { return false; }
1023 type *is_pointer () final override { return NULL; }
1024 type *is_array () final override { return NULL; }
1025 bool is_signed () const final override { return false; }
1027 bool has_known_size () const final override { return m_fields != NULL; }
1029 playback::compound_type *
1030 playback_compound_type ()
1032 return static_cast <playback::compound_type *> (m_playback_obj);
1035 private:
1036 location *m_loc;
1037 string *m_name;
1038 fields *m_fields;
1041 class struct_ : public compound_type
1043 public:
1044 struct_ (context *ctxt,
1045 location *loc,
1046 string *name);
1048 struct_ *dyn_cast_struct () final override { return this; }
1050 type *
1051 as_type () { return this; }
1053 void replay_into (replayer *r) final override;
1055 const char *access_as_type (reproducer &r) final override;
1057 struct_ *is_struct () final override { return this; }
1059 private:
1060 string * make_debug_string () final override;
1061 void write_reproducer (reproducer &r) final override;
1064 // memento of struct_::set_fields
1065 class fields : public memento
1067 public:
1068 fields (compound_type *struct_or_union,
1069 int num_fields,
1070 field **fields);
1072 void replay_into (replayer *r) final override;
1074 void write_to_dump (dump &d) final override;
1076 int length () const { return m_fields.length (); }
1077 field *get_field (int i) const { return m_fields[i]; }
1079 private:
1080 string * make_debug_string () final override;
1081 void write_reproducer (reproducer &r) final override;
1083 private:
1084 compound_type *m_struct_or_union;
1085 auto_vec<field *> m_fields;
1088 class union_ : public compound_type
1090 public:
1091 union_ (context *ctxt,
1092 location *loc,
1093 string *name);
1095 void replay_into (replayer *r) final override;
1097 bool is_union () const final override { return true; }
1099 private:
1100 string * make_debug_string () final override;
1101 void write_reproducer (reproducer &r) final override;
1104 /* An abstract base class for operations that visit all rvalues within an
1105 expression tree.
1106 Currently the only implementation is class rvalue_usage_validator within
1107 jit-recording.cc. */
1109 class rvalue_visitor
1111 public:
1112 virtual ~rvalue_visitor () {}
1113 virtual void visit (rvalue *rvalue) = 0;
1116 /* When generating debug strings for rvalues we mimic C, so we need to
1117 mimic C's precedence levels when handling compound expressions.
1118 These are in order from strongest precedence to weakest. */
1119 enum precedence
1121 PRECEDENCE_PRIMARY,
1122 PRECEDENCE_POSTFIX,
1123 PRECEDENCE_UNARY,
1124 PRECEDENCE_CAST,
1125 PRECEDENCE_MULTIPLICATIVE,
1126 PRECEDENCE_ADDITIVE,
1127 PRECEDENCE_SHIFT,
1128 PRECEDENCE_RELATIONAL,
1129 PRECEDENCE_EQUALITY,
1130 PRECEDENCE_BITWISE_AND,
1131 PRECEDENCE_BITWISE_XOR,
1132 PRECEDENCE_BITWISE_IOR,
1133 PRECEDENCE_LOGICAL_AND,
1134 PRECEDENCE_LOGICAL_OR
1137 class rvalue : public memento
1139 public:
1140 rvalue (context *ctxt,
1141 location *loc,
1142 type *type_)
1143 : memento (ctxt),
1144 m_loc (loc),
1145 m_type (type_),
1146 m_scope (NULL),
1147 m_parenthesized_string (NULL)
1149 gcc_assert (type_);
1152 location * get_loc () const { return m_loc; }
1154 /* Get the recording::type of this rvalue.
1156 Implements the post-error-checking part of
1157 gcc_jit_rvalue_get_type. */
1158 type * get_type () const { return m_type; }
1160 playback::rvalue *
1161 playback_rvalue () const
1163 return static_cast <playback::rvalue *> (m_playback_obj);
1165 rvalue *
1166 access_field (location *loc,
1167 field *field);
1169 lvalue *
1170 dereference_field (location *loc,
1171 field *field);
1173 lvalue *
1174 dereference (location *loc);
1176 void
1177 verify_valid_within_stmt (const char *api_funcname, statement *s);
1179 virtual void visit_children (rvalue_visitor *v) = 0;
1181 void set_scope (function *scope);
1182 function *get_scope () const { return m_scope; }
1184 /* Dynamic casts. */
1185 virtual param *dyn_cast_param () { return NULL; }
1186 virtual base_call *dyn_cast_base_call () { return NULL; }
1188 virtual const char *access_as_rvalue (reproducer &r);
1190 /* Get the debug string, wrapped in parentheses. */
1191 const char *
1192 get_debug_string_parens (enum precedence outer_prec);
1194 virtual bool is_constant () const { return false; }
1195 virtual bool get_wide_int (wide_int *) const { return false; }
1197 private:
1198 virtual enum precedence get_precedence () const = 0;
1200 protected:
1201 location *m_loc;
1202 type *m_type;
1204 private:
1205 function *m_scope; /* NULL for globals, non-NULL for locals/params */
1206 string *m_parenthesized_string;
1209 class lvalue : public rvalue
1211 public:
1212 lvalue (context *ctxt,
1213 location *loc,
1214 type *type_)
1215 : rvalue (ctxt, loc, type_),
1216 m_link_section (NULL),
1217 m_reg_name (NULL),
1218 m_tls_model (GCC_JIT_TLS_MODEL_NONE),
1219 m_alignment (0)
1222 playback::lvalue *
1223 playback_lvalue () const
1225 return static_cast <playback::lvalue *> (m_playback_obj);
1228 lvalue *
1229 access_field (location *loc,
1230 field *field);
1232 rvalue *
1233 get_address (location *loc);
1235 rvalue *
1236 as_rvalue () { return this; }
1238 const char *access_as_rvalue (reproducer &r) override;
1239 virtual const char *access_as_lvalue (reproducer &r);
1240 virtual bool is_global () const { return false; }
1241 void set_tls_model (enum gcc_jit_tls_model model);
1242 void set_link_section (const char *name);
1243 void set_register_name (const char *reg_name);
1244 void set_alignment (unsigned bytes);
1245 unsigned get_alignment () const { return m_alignment; }
1247 protected:
1248 string *m_link_section;
1249 string *m_reg_name;
1250 enum gcc_jit_tls_model m_tls_model;
1251 unsigned m_alignment;
1254 class param : public lvalue
1256 public:
1257 param (context *ctxt,
1258 location *loc,
1259 type *type,
1260 string *name)
1261 : lvalue (ctxt, loc, type),
1262 m_name (name) {}
1264 lvalue *
1265 as_lvalue () { return this; }
1267 void replay_into (replayer *r) final override;
1269 void visit_children (rvalue_visitor *) final override {}
1271 playback::param *
1272 playback_param () const
1274 return static_cast <playback::param *> (m_playback_obj);
1277 param *dyn_cast_param () final override { return this; }
1279 const char *access_as_rvalue (reproducer &r) final override;
1280 const char *access_as_lvalue (reproducer &r) final override;
1282 private:
1283 string * make_debug_string () final override { return m_name; }
1284 void write_reproducer (reproducer &r) final override;
1285 enum precedence get_precedence () const final override
1287 return PRECEDENCE_PRIMARY;
1290 private:
1291 string *m_name;
1294 class function : public memento
1296 public:
1297 function (context *ctxt,
1298 location *loc,
1299 enum gcc_jit_function_kind kind,
1300 type *return_type,
1301 string *name,
1302 int num_params,
1303 param **params,
1304 int is_variadic,
1305 enum built_in_function builtin_id);
1307 void replay_into (replayer *r) final override;
1309 playback::function *
1310 playback_function () const
1312 return static_cast <playback::function *> (m_playback_obj);
1315 enum gcc_jit_function_kind get_kind () const { return m_kind; }
1317 lvalue *
1318 new_local (location *loc,
1319 type *type,
1320 const char *name);
1322 block*
1323 new_block (const char *name);
1325 location *get_loc () const { return m_loc; }
1326 type *get_return_type () const { return m_return_type; }
1327 string * get_name () const { return m_name; }
1328 const vec<param *> &get_params () const { return m_params; }
1330 /* Get the given param by index.
1331 Implements the post-error-checking part of
1332 gcc_jit_function_get_param. */
1333 param *get_param (int i) const { return m_params[i]; }
1335 bool is_variadic () const { return m_is_variadic; }
1337 void write_to_dump (dump &d) final override;
1339 void validate ();
1341 void dump_to_dot (const char *path);
1343 rvalue *get_address (location *loc);
1345 private:
1346 string * make_debug_string () final override;
1347 void write_reproducer (reproducer &r) final override;
1349 private:
1350 location *m_loc;
1351 enum gcc_jit_function_kind m_kind;
1352 type *m_return_type;
1353 string *m_name;
1354 auto_vec<param *> m_params;
1355 int m_is_variadic;
1356 enum built_in_function m_builtin_id;
1357 auto_vec<local *> m_locals;
1358 auto_vec<block *> m_blocks;
1359 type *m_fn_ptr_type;
1362 class block : public memento
1364 public:
1365 block (function *func, int index, string *name)
1366 : memento (func->m_ctxt),
1367 m_func (func),
1368 m_index (index),
1369 m_name (name),
1370 m_statements (),
1371 m_has_been_terminated (false),
1372 m_is_reachable (false)
1376 /* Get the recording::function containing this block.
1377 Implements the post-error-checking part of
1378 gcc_jit_block_get_function. */
1379 function *get_function () { return m_func; }
1381 bool has_been_terminated () { return m_has_been_terminated; }
1382 bool is_reachable () { return m_is_reachable; }
1384 statement *
1385 add_eval (location *loc,
1386 rvalue *rvalue);
1388 statement *
1389 add_assignment (location *loc,
1390 lvalue *lvalue,
1391 rvalue *rvalue);
1393 statement *
1394 add_assignment_op (location *loc,
1395 lvalue *lvalue,
1396 enum gcc_jit_binary_op op,
1397 rvalue *rvalue);
1399 statement *
1400 add_comment (location *loc,
1401 const char *text);
1403 extended_asm *
1404 add_extended_asm (location *loc,
1405 const char *asm_template);
1407 statement *
1408 end_with_conditional (location *loc,
1409 rvalue *boolval,
1410 block *on_true,
1411 block *on_false);
1413 statement *
1414 end_with_jump (location *loc,
1415 block *target);
1417 statement *
1418 end_with_return (location *loc,
1419 rvalue *rvalue);
1421 statement *
1422 end_with_switch (location *loc,
1423 rvalue *expr,
1424 block *default_block,
1425 int num_cases,
1426 case_ **cases);
1428 extended_asm *
1429 end_with_extended_asm_goto (location *loc,
1430 const char *asm_template,
1431 int num_goto_blocks,
1432 block **goto_blocks,
1433 block *fallthrough_block);
1435 playback::block *
1436 playback_block () const
1438 return static_cast <playback::block *> (m_playback_obj);
1441 void write_to_dump (dump &d) final override;
1443 bool validate ();
1445 location *get_loc () const;
1447 statement *get_first_statement () const;
1448 statement *get_last_statement () const;
1450 vec <block *> get_successor_blocks () const;
1452 private:
1453 string * make_debug_string () final override;
1454 void write_reproducer (reproducer &r) final override;
1456 void replay_into (replayer *r) final override;
1458 void dump_to_dot (pretty_printer *pp);
1459 void dump_edges_to_dot (pretty_printer *pp);
1461 private:
1462 function *m_func;
1463 int m_index;
1464 string *m_name;
1465 auto_vec<statement *> m_statements;
1466 bool m_has_been_terminated;
1467 bool m_is_reachable;
1469 friend class function;
1472 class global : public lvalue
1474 public:
1475 global (context *ctxt,
1476 location *loc,
1477 enum gcc_jit_global_kind kind,
1478 type *type,
1479 string *name)
1480 : lvalue (ctxt, loc, type),
1481 m_kind (kind),
1482 m_name (name)
1484 m_initializer = NULL;
1485 m_initializer_num_bytes = 0;
1487 ~global ()
1489 free (m_initializer);
1492 void replay_into (replayer *) final override;
1494 void visit_children (rvalue_visitor *) final override {}
1496 void write_to_dump (dump &d) final override;
1498 bool is_global () const final override { return true; }
1500 void
1501 set_initializer (const void *initializer,
1502 size_t num_bytes)
1504 if (m_initializer)
1505 free (m_initializer);
1506 m_initializer = xmalloc (num_bytes);
1507 memcpy (m_initializer, initializer, num_bytes);
1508 m_initializer_num_bytes = num_bytes;
1511 void set_flags (int flag_fields)
1513 m_flags = (enum global_var_flags)(m_flags | flag_fields);
1515 /* Returns true if any of the flags in the argument is set. */
1516 bool test_flags_anyof (int flag_fields) const
1518 return m_flags & flag_fields;
1521 enum gcc_jit_global_kind get_kind () const
1523 return m_kind;
1526 void set_rvalue_init (rvalue *val) { m_rvalue_init = val; }
1528 private:
1529 string * make_debug_string () final override { return m_name; }
1530 template <typename T>
1531 void write_initializer_reproducer (const char *id, reproducer &r);
1532 void write_reproducer (reproducer &r) final override;
1533 enum precedence get_precedence () const final override
1535 return PRECEDENCE_PRIMARY;
1538 private:
1539 enum gcc_jit_global_kind m_kind;
1540 enum global_var_flags m_flags = GLOBAL_VAR_FLAGS_NONE;
1541 string *m_name;
1542 void *m_initializer;
1543 rvalue *m_rvalue_init = nullptr; /* Only needed for write_dump. */
1544 size_t m_initializer_num_bytes;
1547 template <typename HOST_TYPE>
1548 class memento_of_new_rvalue_from_const : public rvalue
1550 public:
1551 memento_of_new_rvalue_from_const (context *ctxt,
1552 location *loc,
1553 type *type,
1554 HOST_TYPE value)
1555 : rvalue (ctxt, loc, type),
1556 m_value (value) {}
1558 void replay_into (replayer *r) final override;
1560 void visit_children (rvalue_visitor *) final override {}
1562 bool is_constant () const final override { return true; }
1564 bool get_wide_int (wide_int *out) const final override;
1566 private:
1567 string * make_debug_string () final override;
1568 void write_reproducer (reproducer &r) final override;
1569 enum precedence get_precedence () const final override
1571 return PRECEDENCE_PRIMARY;
1574 private:
1575 HOST_TYPE m_value;
1578 class memento_of_new_string_literal : public rvalue
1580 public:
1581 memento_of_new_string_literal (context *ctxt,
1582 location *loc,
1583 string *value)
1584 : rvalue (ctxt, loc, ctxt->get_type (GCC_JIT_TYPE_CONST_CHAR_PTR)),
1585 m_value (value) {}
1587 void replay_into (replayer *r) final override;
1589 void visit_children (rvalue_visitor *) final override {}
1591 private:
1592 string * make_debug_string () final override;
1593 void write_reproducer (reproducer &r) final override;
1594 enum precedence get_precedence () const final override
1596 return PRECEDENCE_PRIMARY;
1599 private:
1600 string *m_value;
1603 class memento_of_new_rvalue_from_vector : public rvalue
1605 public:
1606 memento_of_new_rvalue_from_vector (context *ctxt,
1607 location *loc,
1608 vector_type *type,
1609 rvalue **elements);
1611 void replay_into (replayer *r) final override;
1613 void visit_children (rvalue_visitor *) final override;
1615 private:
1616 string * make_debug_string () final override;
1617 void write_reproducer (reproducer &r) final override;
1618 enum precedence get_precedence () const final override
1620 return PRECEDENCE_PRIMARY;
1623 private:
1624 vector_type *m_vector_type;
1625 auto_vec<rvalue *> m_elements;
1628 class ctor : public rvalue
1630 public:
1631 ctor (context *ctxt,
1632 location *loc,
1633 type *type)
1634 : rvalue (ctxt, loc, type)
1637 void replay_into (replayer *r) final override;
1639 void visit_children (rvalue_visitor *) final override;
1641 private:
1642 string * make_debug_string () final override;
1643 void write_reproducer (reproducer &r) final override;
1644 enum precedence get_precedence () const final override
1646 return PRECEDENCE_PRIMARY;
1649 public:
1650 auto_vec<field *> m_fields;
1651 auto_vec<rvalue *> m_values;
1654 class unary_op : public rvalue
1656 public:
1657 unary_op (context *ctxt,
1658 location *loc,
1659 enum gcc_jit_unary_op op,
1660 type *result_type,
1661 rvalue *a)
1662 : rvalue (ctxt, loc, result_type),
1663 m_op (op),
1664 m_a (a)
1667 void replay_into (replayer *r) final override;
1669 void visit_children (rvalue_visitor *v) final override;
1671 private:
1672 string * make_debug_string () final override;
1673 void write_reproducer (reproducer &r) final override;
1674 enum precedence get_precedence () const final override
1676 return PRECEDENCE_UNARY;
1679 private:
1680 enum gcc_jit_unary_op m_op;
1681 rvalue *m_a;
1684 class binary_op : public rvalue
1686 public:
1687 binary_op (context *ctxt,
1688 location *loc,
1689 enum gcc_jit_binary_op op,
1690 type *result_type,
1691 rvalue *a, rvalue *b)
1692 : rvalue (ctxt, loc, result_type),
1693 m_op (op),
1694 m_a (a),
1695 m_b (b) {}
1697 void replay_into (replayer *r) final override;
1699 void visit_children (rvalue_visitor *v) final override;
1701 private:
1702 string * make_debug_string () final override;
1703 void write_reproducer (reproducer &r) final override;
1704 enum precedence get_precedence () const final override;
1706 private:
1707 enum gcc_jit_binary_op m_op;
1708 rvalue *m_a;
1709 rvalue *m_b;
1712 class comparison : public rvalue
1714 public:
1715 comparison (context *ctxt,
1716 location *loc,
1717 enum gcc_jit_comparison op,
1718 rvalue *a, rvalue *b)
1719 : rvalue (ctxt, loc, ctxt->get_type (GCC_JIT_TYPE_BOOL)),
1720 m_op (op),
1721 m_a (a),
1722 m_b (b)
1724 type *a_type = a->get_type ();
1725 vector_type *vec_type = a_type->dyn_cast_vector_type ();
1726 if (vec_type != NULL)
1728 type *element_type = vec_type->get_element_type ();
1729 type *inner_type;
1730 /* Vectors of floating-point values return a vector of integers of the
1731 same size. */
1732 if (element_type->is_float ())
1733 inner_type = ctxt->get_int_type (element_type->get_size (), false);
1734 else
1735 inner_type = element_type;
1736 m_type = new vector_type (inner_type, vec_type->get_num_units ());
1737 ctxt->record (m_type);
1741 void replay_into (replayer *r) final override;
1743 void visit_children (rvalue_visitor *v) final override;
1745 private:
1746 string * make_debug_string () final override;
1747 void write_reproducer (reproducer &r) final override;
1748 enum precedence get_precedence () const final override;
1750 private:
1751 enum gcc_jit_comparison m_op;
1752 rvalue *m_a;
1753 rvalue *m_b;
1756 class cast : public rvalue
1758 public:
1759 cast (context *ctxt,
1760 location *loc,
1761 rvalue *a,
1762 type *type_)
1763 : rvalue (ctxt, loc, type_),
1764 m_rvalue (a)
1767 void replay_into (replayer *r) final override;
1769 void visit_children (rvalue_visitor *v) final override;
1771 private:
1772 string * make_debug_string () final override;
1773 void write_reproducer (reproducer &r) final override;
1774 enum precedence get_precedence () const final override
1776 return PRECEDENCE_CAST;
1779 private:
1780 rvalue *m_rvalue;
1783 class bitcast : public rvalue
1785 public:
1786 bitcast (context *ctxt,
1787 location *loc,
1788 rvalue *a,
1789 type *type_)
1790 : rvalue (ctxt, loc, type_),
1791 m_rvalue (a)
1794 void replay_into (replayer *r) final override;
1796 void visit_children (rvalue_visitor *v) final override;
1798 private:
1799 string * make_debug_string () final override;
1800 void write_reproducer (reproducer &r) final override;
1801 enum precedence get_precedence () const final override
1803 return PRECEDENCE_CAST;
1806 private:
1807 rvalue *m_rvalue;
1810 class base_call : public rvalue
1812 public:
1813 base_call (context *ctxt,
1814 location *loc,
1815 type *type_,
1816 int numargs,
1817 rvalue **args);
1819 enum precedence get_precedence () const final override
1821 return PRECEDENCE_POSTFIX;
1824 base_call *dyn_cast_base_call () final override { return this; }
1826 void set_require_tail_call (bool require_tail_call)
1828 m_require_tail_call = require_tail_call;
1831 protected:
1832 void write_reproducer_tail_call (reproducer &r, const char *id);
1834 protected:
1835 auto_vec<rvalue *> m_args;
1836 bool m_require_tail_call;
1839 class call : public base_call
1841 public:
1842 call (context *ctxt,
1843 location *loc,
1844 function *func,
1845 int numargs,
1846 rvalue **args);
1848 void replay_into (replayer *r) final override;
1850 void visit_children (rvalue_visitor *v) final override;
1852 private:
1853 string * make_debug_string () final override;
1854 void write_reproducer (reproducer &r) final override;
1856 private:
1857 function *m_func;
1860 class call_through_ptr : public base_call
1862 public:
1863 call_through_ptr (context *ctxt,
1864 location *loc,
1865 rvalue *fn_ptr,
1866 int numargs,
1867 rvalue **args);
1869 void replay_into (replayer *r) final override;
1871 void visit_children (rvalue_visitor *v) final override;
1873 private:
1874 string * make_debug_string () final override;
1875 void write_reproducer (reproducer &r) final override;
1877 private:
1878 rvalue *m_fn_ptr;
1881 class array_access : public lvalue
1883 public:
1884 array_access (context *ctxt,
1885 location *loc,
1886 rvalue *ptr,
1887 rvalue *index)
1888 : lvalue (ctxt, loc, ptr->get_type ()->dereference ()),
1889 m_ptr (ptr),
1890 m_index (index)
1893 void replay_into (replayer *r) final override;
1895 void visit_children (rvalue_visitor *v) final override;
1897 private:
1898 string * make_debug_string () final override;
1899 void write_reproducer (reproducer &r) final override;
1900 enum precedence get_precedence () const final override
1902 return PRECEDENCE_POSTFIX;
1905 private:
1906 rvalue *m_ptr;
1907 rvalue *m_index;
1910 class access_field_of_lvalue : public lvalue
1912 public:
1913 access_field_of_lvalue (context *ctxt,
1914 location *loc,
1915 lvalue *val,
1916 field *field)
1917 : lvalue (ctxt, loc, field->get_type ()),
1918 m_lvalue (val),
1919 m_field (field)
1922 void replay_into (replayer *r) final override;
1924 void visit_children (rvalue_visitor *v) final override;
1926 private:
1927 string * make_debug_string () final override;
1928 void write_reproducer (reproducer &r) final override;
1929 enum precedence get_precedence () const final override
1931 return PRECEDENCE_POSTFIX;
1934 private:
1935 lvalue *m_lvalue;
1936 field *m_field;
1939 class access_field_rvalue : public rvalue
1941 public:
1942 access_field_rvalue (context *ctxt,
1943 location *loc,
1944 rvalue *val,
1945 field *field)
1946 : rvalue (ctxt, loc, field->get_type ()),
1947 m_rvalue (val),
1948 m_field (field)
1951 void replay_into (replayer *r) final override;
1953 void visit_children (rvalue_visitor *v) final override;
1955 private:
1956 string * make_debug_string () final override;
1957 void write_reproducer (reproducer &r) final override;
1958 enum precedence get_precedence () const final override
1960 return PRECEDENCE_POSTFIX;
1963 private:
1964 rvalue *m_rvalue;
1965 field *m_field;
1968 class dereference_field_rvalue : public lvalue
1970 public:
1971 dereference_field_rvalue (context *ctxt,
1972 location *loc,
1973 rvalue *val,
1974 field *field)
1975 : lvalue (ctxt, loc, field->get_type ()),
1976 m_rvalue (val),
1977 m_field (field)
1980 void replay_into (replayer *r) final override;
1982 void visit_children (rvalue_visitor *v) final override;
1984 private:
1985 string * make_debug_string () final override;
1986 void write_reproducer (reproducer &r) final override;
1987 enum precedence get_precedence () const final override
1989 return PRECEDENCE_POSTFIX;
1992 private:
1993 rvalue *m_rvalue;
1994 field *m_field;
1997 class dereference_rvalue : public lvalue
1999 public:
2000 dereference_rvalue (context *ctxt,
2001 location *loc,
2002 rvalue *val)
2003 : lvalue (ctxt, loc, val->get_type ()->dereference ()),
2004 m_rvalue (val) {}
2006 void replay_into (replayer *r) final override;
2008 void visit_children (rvalue_visitor *v) final override;
2010 private:
2011 string * make_debug_string () final override;
2012 void write_reproducer (reproducer &r) final override;
2013 enum precedence get_precedence () const final override
2015 return PRECEDENCE_UNARY;
2018 private:
2019 rvalue *m_rvalue;
2022 class get_address_of_lvalue : public rvalue
2024 public:
2025 get_address_of_lvalue (context *ctxt,
2026 location *loc,
2027 lvalue *val)
2028 : rvalue (ctxt, loc, val->get_type ()->get_pointer ()),
2029 m_lvalue (val)
2032 void replay_into (replayer *r) final override;
2034 void visit_children (rvalue_visitor *v) final override;
2036 private:
2037 string * make_debug_string () final override;
2038 void write_reproducer (reproducer &r) final override;
2039 enum precedence get_precedence () const final override
2041 return PRECEDENCE_UNARY;
2044 private:
2045 lvalue *m_lvalue;
2048 class function_pointer : public rvalue
2050 public:
2051 function_pointer (context *ctxt,
2052 location *loc,
2053 function *fn,
2054 type *type)
2055 : rvalue (ctxt, loc, type),
2056 m_fn (fn) {}
2058 void replay_into (replayer *r) final override;
2060 void visit_children (rvalue_visitor *v) final override;
2062 private:
2063 string * make_debug_string () final override;
2064 void write_reproducer (reproducer &r) final override;
2065 enum precedence get_precedence () const final override
2067 return PRECEDENCE_UNARY;
2070 private:
2071 function *m_fn;
2074 class local : public lvalue
2076 public:
2077 local (function *func, location *loc, type *type_, string *name)
2078 : lvalue (func->m_ctxt, loc, type_),
2079 m_func (func),
2080 m_name (name)
2082 set_scope (func);
2085 void replay_into (replayer *r) final override;
2087 void visit_children (rvalue_visitor *) final override {}
2089 void write_to_dump (dump &d) final override;
2091 private:
2092 string * make_debug_string () final override { return m_name; }
2093 void write_reproducer (reproducer &r) final override;
2094 enum precedence get_precedence () const final override
2096 return PRECEDENCE_PRIMARY;
2099 private:
2100 function *m_func;
2101 string *m_name;
2104 class statement : public memento
2106 public:
2107 virtual vec <block *> get_successor_blocks () const;
2109 void write_to_dump (dump &d) final override;
2111 block *get_block () const { return m_block; }
2112 location *get_loc () const { return m_loc; }
2114 protected:
2115 statement (block *b, location *loc)
2116 : memento (b->m_ctxt),
2117 m_block (b),
2118 m_loc (loc) {}
2120 playback::location *
2121 playback_location (replayer *r) const
2123 return ::gcc::jit::recording::playback_location (r, m_loc);
2126 private:
2127 block *m_block;
2128 location *m_loc;
2131 class eval : public statement
2133 public:
2134 eval (block *b,
2135 location *loc,
2136 rvalue *rvalue)
2137 : statement (b, loc),
2138 m_rvalue (rvalue) {}
2140 void replay_into (replayer *r) final override;
2142 private:
2143 string * make_debug_string () final override;
2144 void write_reproducer (reproducer &r) final override;
2146 private:
2147 rvalue *m_rvalue;
2150 class assignment : public statement
2152 public:
2153 assignment (block *b,
2154 location *loc,
2155 lvalue *lvalue,
2156 rvalue *rvalue)
2157 : statement (b, loc),
2158 m_lvalue (lvalue),
2159 m_rvalue (rvalue) {}
2161 void replay_into (replayer *r) final override;
2163 private:
2164 string * make_debug_string () final override;
2165 void write_reproducer (reproducer &r) final override;
2167 private:
2168 lvalue *m_lvalue;
2169 rvalue *m_rvalue;
2172 class assignment_op : public statement
2174 public:
2175 assignment_op (block *b,
2176 location *loc,
2177 lvalue *lvalue,
2178 enum gcc_jit_binary_op op,
2179 rvalue *rvalue)
2180 : statement (b, loc),
2181 m_lvalue (lvalue),
2182 m_op (op),
2183 m_rvalue (rvalue) {}
2185 void replay_into (replayer *r) final override;
2187 private:
2188 string * make_debug_string () final override;
2189 void write_reproducer (reproducer &r) final override;
2191 private:
2192 lvalue *m_lvalue;
2193 enum gcc_jit_binary_op m_op;
2194 rvalue *m_rvalue;
2197 class comment : public statement
2199 public:
2200 comment (block *b,
2201 location *loc,
2202 string *text)
2203 : statement (b, loc),
2204 m_text (text) {}
2206 void replay_into (replayer *r) final override;
2208 private:
2209 string * make_debug_string () final override;
2210 void write_reproducer (reproducer &r) final override;
2212 private:
2213 string *m_text;
2216 class conditional : public statement
2218 public:
2219 conditional (block *b,
2220 location *loc,
2221 rvalue *boolval,
2222 block *on_true,
2223 block *on_false)
2224 : statement (b, loc),
2225 m_boolval (boolval),
2226 m_on_true (on_true),
2227 m_on_false (on_false) {}
2229 void replay_into (replayer *r) final override;
2231 vec <block *> get_successor_blocks () const final override;
2233 private:
2234 string * make_debug_string () final override;
2235 void write_reproducer (reproducer &r) final override;
2237 private:
2238 rvalue *m_boolval;
2239 block *m_on_true;
2240 block *m_on_false;
2243 class jump : public statement
2245 public:
2246 jump (block *b,
2247 location *loc,
2248 block *target)
2249 : statement (b, loc),
2250 m_target (target) {}
2252 void replay_into (replayer *r) final override;
2254 vec <block *> get_successor_blocks () const final override;
2256 private:
2257 string * make_debug_string () final override;
2258 void write_reproducer (reproducer &r) final override;
2260 private:
2261 block *m_target;
2264 class return_ : public statement
2266 public:
2267 return_ (block *b,
2268 location *loc,
2269 rvalue *rvalue)
2270 : statement (b, loc),
2271 m_rvalue (rvalue) {}
2273 void replay_into (replayer *r) final override;
2275 vec <block *> get_successor_blocks () const final override;
2277 private:
2278 string * make_debug_string () final override;
2279 void write_reproducer (reproducer &r) final override;
2281 private:
2282 rvalue *m_rvalue;
2285 class case_ : public memento
2287 public:
2288 case_ (context *ctxt,
2289 rvalue *min_value,
2290 rvalue *max_value,
2291 block *dest_block)
2292 : memento (ctxt),
2293 m_min_value (min_value),
2294 m_max_value (max_value),
2295 m_dest_block (dest_block)
2298 rvalue *get_min_value () const { return m_min_value; }
2299 rvalue *get_max_value () const { return m_max_value; }
2300 block *get_dest_block () const { return m_dest_block; }
2302 void replay_into (replayer *) final override { /* empty */ }
2304 void write_reproducer (reproducer &r) final override;
2306 private:
2307 string * make_debug_string () final override;
2309 private:
2310 rvalue *m_min_value;
2311 rvalue *m_max_value;
2312 block *m_dest_block;
2315 class switch_ : public statement
2317 public:
2318 switch_ (block *b,
2319 location *loc,
2320 rvalue *expr,
2321 block *default_block,
2322 int num_cases,
2323 case_ **cases);
2325 void replay_into (replayer *r) final override;
2327 vec <block *> get_successor_blocks () const final override;
2329 private:
2330 string * make_debug_string () final override;
2331 void write_reproducer (reproducer &r) final override;
2333 private:
2334 rvalue *m_expr;
2335 block *m_default_block;
2336 auto_vec <case_ *> m_cases;
2339 class asm_operand : public memento
2341 public:
2342 asm_operand (extended_asm *ext_asm,
2343 string *asm_symbolic_name,
2344 string *constraint);
2346 const char *get_symbolic_name () const
2348 if (m_asm_symbolic_name)
2349 return m_asm_symbolic_name->c_str ();
2350 else
2351 return NULL;
2354 const char *get_constraint () const
2356 return m_constraint->c_str ();
2359 virtual void print (pretty_printer *pp) const;
2361 private:
2362 string * make_debug_string () final override;
2364 protected:
2365 extended_asm *m_ext_asm;
2366 string *m_asm_symbolic_name;
2367 string *m_constraint;
2370 class output_asm_operand : public asm_operand
2372 public:
2373 output_asm_operand (extended_asm *ext_asm,
2374 string *asm_symbolic_name,
2375 string *constraint,
2376 lvalue *dest)
2377 : asm_operand (ext_asm, asm_symbolic_name, constraint),
2378 m_dest (dest)
2381 lvalue *get_lvalue () const { return m_dest; }
2383 void replay_into (replayer *) final override {}
2385 void print (pretty_printer *pp) const final override;
2387 private:
2388 void write_reproducer (reproducer &r) final override;
2390 private:
2391 lvalue *m_dest;
2394 class input_asm_operand : public asm_operand
2396 public:
2397 input_asm_operand (extended_asm *ext_asm,
2398 string *asm_symbolic_name,
2399 string *constraint,
2400 rvalue *src)
2401 : asm_operand (ext_asm, asm_symbolic_name, constraint),
2402 m_src (src)
2405 rvalue *get_rvalue () const { return m_src; }
2407 void replay_into (replayer *) final override {}
2409 void print (pretty_printer *pp) const final override;
2411 private:
2412 void write_reproducer (reproducer &r) final override;
2414 private:
2415 rvalue *m_src;
2418 /* Abstract base class for extended_asm statements. */
2420 class extended_asm : public statement
2422 public:
2423 extended_asm (block *b,
2424 location *loc,
2425 string *asm_template)
2426 : statement (b, loc),
2427 m_asm_template (asm_template),
2428 m_is_volatile (false),
2429 m_is_inline (false)
2432 void set_volatile_flag (bool flag) { m_is_volatile = flag; }
2433 void set_inline_flag (bool flag) { m_is_inline = flag; }
2435 void add_output_operand (const char *asm_symbolic_name,
2436 const char *constraint,
2437 lvalue *dest);
2438 void add_input_operand (const char *asm_symbolic_name,
2439 const char *constraint,
2440 rvalue *src);
2441 void add_clobber (const char *victim);
2443 void replay_into (replayer *r) override;
2445 string *get_asm_template () const { return m_asm_template; }
2447 virtual bool is_goto () const = 0;
2448 virtual void maybe_print_gotos (pretty_printer *) const = 0;
2450 protected:
2451 void write_flags (reproducer &r);
2452 void write_clobbers (reproducer &r);
2454 private:
2455 string * make_debug_string () final override;
2456 virtual void maybe_populate_playback_blocks
2457 (auto_vec <playback::block *> *out) = 0;
2459 protected:
2460 string *m_asm_template;
2461 bool m_is_volatile;
2462 bool m_is_inline;
2463 auto_vec<output_asm_operand *> m_output_ops;
2464 auto_vec<input_asm_operand *> m_input_ops;
2465 auto_vec<string *> m_clobbers;
2468 /* An extended_asm that's not a goto, as created by
2469 gcc_jit_block_add_extended_asm. */
2471 class extended_asm_simple : public extended_asm
2473 public:
2474 extended_asm_simple (block *b,
2475 location *loc,
2476 string *asm_template)
2477 : extended_asm (b, loc, asm_template)
2480 void write_reproducer (reproducer &r) override;
2481 bool is_goto () const final override { return false; }
2482 void maybe_print_gotos (pretty_printer *) const final override {}
2484 private:
2485 void maybe_populate_playback_blocks
2486 (auto_vec <playback::block *> *) final override
2490 /* An extended_asm that's a asm goto, as created by
2491 gcc_jit_block_end_with_extended_asm_goto. */
2493 class extended_asm_goto : public extended_asm
2495 public:
2496 extended_asm_goto (block *b,
2497 location *loc,
2498 string *asm_template,
2499 int num_goto_blocks,
2500 block **goto_blocks,
2501 block *fallthrough_block);
2503 void replay_into (replayer *r) final override;
2504 void write_reproducer (reproducer &r) override;
2506 vec <block *> get_successor_blocks () const final override;
2508 bool is_goto () const final override { return true; }
2509 void maybe_print_gotos (pretty_printer *) const final override;
2511 private:
2512 void maybe_populate_playback_blocks
2513 (auto_vec <playback::block *> *out) final override;
2515 private:
2516 auto_vec <block *> m_goto_blocks;
2517 block *m_fallthrough_block;
2520 /* A group of top-level asm statements, as created by
2521 gcc_jit_context_add_top_level_asm. */
2523 class top_level_asm : public memento
2525 public:
2526 top_level_asm (context *ctxt, location *loc, string *asm_stmts);
2528 void write_to_dump (dump &d) final override;
2530 private:
2531 void replay_into (replayer *r) final override;
2532 string * make_debug_string () final override;
2533 void write_reproducer (reproducer &r) final override;
2535 private:
2536 location *m_loc;
2537 string *m_asm_stmts;
2540 class global_init_rvalue : public memento
2542 public:
2543 global_init_rvalue (context *ctxt, lvalue *variable, rvalue *init) :
2544 memento (ctxt), m_variable (variable), m_init (init) {};
2546 void write_to_dump (dump &d) final override;
2548 private:
2549 void replay_into (replayer *r) final override;
2550 string * make_debug_string () final override;
2551 void write_reproducer (reproducer &r) final override;
2553 private:
2554 lvalue *m_variable;
2555 rvalue *m_init;
2558 } // namespace gcc::jit::recording
2560 /* Create a recording::memento_of_new_rvalue_from_const instance and add
2561 it to this context's list of mementos.
2563 Implements the post-error-checking part of
2564 gcc_jit_context_new_rvalue_from_{int|long|double|ptr}. */
2566 template <typename HOST_TYPE>
2567 recording::rvalue *
2568 recording::context::new_rvalue_from_const (recording::type *type,
2569 HOST_TYPE value)
2571 recording::rvalue *result =
2572 new memento_of_new_rvalue_from_const <HOST_TYPE> (this, NULL, type, value);
2573 record (result);
2574 return result;
2577 /* Don't call this directly. Call types_kinda_same. */
2578 bool
2579 types_kinda_same_internal (recording::type *a,
2580 recording::type *b);
2582 /* Strip all qualifiers and count pointer depth, returning true
2583 if the types and pointer depth are the same, otherwise false.
2585 For array and vector types the number of element also
2586 has to match, aswell as the element types themself. */
2587 inline bool
2588 types_kinda_same (recording::type *a, recording::type *b)
2590 /* Handle trivial case here, to allow for inlining. */
2591 return a == b || types_kinda_same_internal (a, b);
2594 } // namespace gcc::jit
2596 } // namespace gcc
2598 #endif /* JIT_RECORDING_H */