PR jit/66546: Add gcc_jit_context_set_bool_allow_unreachable_blocks
[official-gcc.git] / gcc / jit / jit-recording.h
bloba9bcbb5d63287a4769ba7ebe219aa687fb36918a
1 /* Internals of libgccjit: classes for recording calls made to the JIT API.
2 Copyright (C) 2013-2015 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 namespace gcc {
29 namespace jit {
31 class result;
32 class dump;
33 class reproducer;
35 /**********************************************************************
36 Recording.
37 **********************************************************************/
39 namespace recording {
41 playback::location *
42 playback_location (replayer *r, location *loc);
44 const char *
45 playback_string (string *str);
47 playback::block *
48 playback_block (block *b);
50 /* A recording of a call to gcc_jit_context_enable_dump. */
51 struct requested_dump
53 const char *m_dumpname;
54 char **m_out_ptr;
57 /* A JIT-compilation context. */
58 class context : public log_user
60 public:
61 context (context *parent_ctxt);
62 ~context ();
64 builtins_manager *
65 get_builtins_manager ();
67 void record (memento *m);
68 void replay_into (replayer *r);
69 void disassociate_from_playback ();
71 string *
72 new_string (const char *text);
74 location *
75 new_location (const char *filename,
76 int line,
77 int column,
78 bool created_by_user);
80 type *
81 get_type (enum gcc_jit_types type);
83 type *
84 get_int_type (int num_bytes, int is_signed);
86 type *
87 new_array_type (location *loc,
88 type *element_type,
89 int num_elements);
91 field *
92 new_field (location *loc,
93 type *type,
94 const char *name);
96 struct_ *
97 new_struct_type (location *loc,
98 const char *name);
100 union_ *
101 new_union_type (location *loc,
102 const char *name);
104 function_type *
105 new_function_type (type *return_type,
106 int num_params,
107 type **param_types,
108 int is_variadic);
110 type *
111 new_function_ptr_type (location *loc,
112 type *return_type,
113 int num_params,
114 type **param_types,
115 int is_variadic);
117 param *
118 new_param (location *loc,
119 type *type,
120 const char *name);
122 function *
123 new_function (location *loc,
124 enum gcc_jit_function_kind kind,
125 type *return_type,
126 const char *name,
127 int num_params,
128 param **params,
129 int is_variadic,
130 enum built_in_function builtin_id);
132 function *
133 get_builtin_function (const char *name);
135 lvalue *
136 new_global (location *loc,
137 enum gcc_jit_global_kind kind,
138 type *type,
139 const char *name);
141 template <typename HOST_TYPE>
142 rvalue *
143 new_rvalue_from_const (type *type,
144 HOST_TYPE value);
146 rvalue *
147 new_string_literal (const char *value);
149 rvalue *
150 new_unary_op (location *loc,
151 enum gcc_jit_unary_op op,
152 type *result_type,
153 rvalue *a);
155 rvalue *
156 new_binary_op (location *loc,
157 enum gcc_jit_binary_op op,
158 type *result_type,
159 rvalue *a, rvalue *b);
161 rvalue *
162 new_comparison (location *loc,
163 enum gcc_jit_comparison op,
164 rvalue *a, rvalue *b);
166 rvalue *
167 new_call (location *loc,
168 function *func,
169 int numargs, rvalue **args);
171 rvalue *
172 new_call_through_ptr (location *loc,
173 rvalue *fn_ptr,
174 int numargs, rvalue **args);
176 rvalue *
177 new_cast (location *loc,
178 rvalue *expr,
179 type *type_);
181 lvalue *
182 new_array_access (location *loc,
183 rvalue *ptr,
184 rvalue *index);
186 void
187 set_str_option (enum gcc_jit_str_option opt,
188 const char *value);
190 void
191 set_int_option (enum gcc_jit_int_option opt,
192 int value);
194 void
195 set_bool_option (enum gcc_jit_bool_option opt,
196 int value);
198 void
199 set_inner_bool_option (enum inner_bool_option inner_opt,
200 int value);
202 void
203 add_command_line_option (const char *optname);
205 void
206 append_command_line_options (vec <char *> *argvec);
208 void
209 enable_dump (const char *dumpname,
210 char **out_ptr);
212 const char *
213 get_str_option (enum gcc_jit_str_option opt) const
215 return m_str_options[opt];
219 get_int_option (enum gcc_jit_int_option opt) const
221 return m_int_options[opt];
225 get_bool_option (enum gcc_jit_bool_option opt) const
227 return m_bool_options[opt];
231 get_inner_bool_option (enum inner_bool_option opt) const
233 return m_inner_bool_options[opt];
236 result *
237 compile ();
239 void
240 compile_to_file (enum gcc_jit_output_kind output_kind,
241 const char *output_path);
243 void
244 add_error (location *loc, const char *fmt, ...)
245 GNU_PRINTF(3, 4);
247 void
248 add_error_va (location *loc, const char *fmt, va_list ap)
249 GNU_PRINTF(3, 0);
251 const char *
252 get_first_error () const;
254 const char *
255 get_last_error () const;
257 bool errors_occurred () const
259 if (m_parent_ctxt)
260 if (m_parent_ctxt->errors_occurred ())
261 return true;
262 return m_error_count;
265 type *get_opaque_FILE_type ();
267 void dump_to_file (const char *path, bool update_locations);
269 void dump_reproducer_to_file (const char *path);
271 void
272 get_all_requested_dumps (vec <recording::requested_dump> *out);
274 private:
275 void log_all_options () const;
276 void log_str_option (enum gcc_jit_str_option opt) const;
277 void log_int_option (enum gcc_jit_int_option opt) const;
278 void log_bool_option (enum gcc_jit_bool_option opt) const;
279 void log_inner_bool_option (enum inner_bool_option opt) const;
281 void validate ();
283 private:
284 context *m_parent_ctxt;
286 /* The ultimate ancestor of the contexts within a family tree of
287 contexts. This has itself as its own m_toplevel_ctxt. */
288 context *m_toplevel_ctxt;
290 int m_error_count;
292 char *m_first_error_str;
293 bool m_owns_first_error_str;
295 char *m_last_error_str;
296 bool m_owns_last_error_str;
298 char *m_str_options[GCC_JIT_NUM_STR_OPTIONS];
299 int m_int_options[GCC_JIT_NUM_INT_OPTIONS];
300 bool m_bool_options[GCC_JIT_NUM_BOOL_OPTIONS];
301 bool m_inner_bool_options[NUM_INNER_BOOL_OPTIONS];
302 auto_vec <char *> m_command_line_options;
304 /* Dumpfiles that were requested via gcc_jit_context_enable_dump. */
305 auto_vec<requested_dump> m_requested_dumps;
307 /* Recorded API usage. */
308 auto_vec<memento *> m_mementos;
310 /* Specific recordings, for use by dump_to_file. */
311 auto_vec<compound_type *> m_compound_types;
312 auto_vec<global *> m_globals;
313 auto_vec<function *> m_functions;
315 type *m_basic_types[NUM_GCC_JIT_TYPES];
316 type *m_FILE_type;
318 builtins_manager *m_builtins_manager; // lazily created
322 /* An object with lifetime managed by the context i.e.
323 it lives until the context is released, at which
324 point it itself is cleaned up. */
326 class memento
328 public:
329 virtual ~memento () {}
331 /* Hook for replaying this. */
332 virtual void replay_into (replayer *r) = 0;
334 void set_playback_obj (void *obj) { m_playback_obj = obj; }
337 /* Get the context that owns this object.
339 Implements the post-error-checking part of
340 gcc_jit_object_get_context. */
341 context *get_context () { return m_ctxt; }
343 memento *
344 as_object () { return this; }
346 /* Debugging hook, for use in generating error messages etc.
347 Implements the post-error-checking part of
348 gcc_jit_object_get_debug_string. */
349 const char *
350 get_debug_string ();
352 virtual void write_to_dump (dump &d);
353 virtual void write_reproducer (reproducer &r) = 0;
354 virtual location *dyn_cast_location () { return NULL; }
356 protected:
357 memento (context *ctxt)
358 : m_ctxt (ctxt),
359 m_playback_obj (NULL),
360 m_debug_string (NULL)
362 gcc_assert (ctxt);
365 string *new_string (const char *text) { return m_ctxt->new_string (text); }
367 private:
368 virtual string * make_debug_string () = 0;
370 public:
371 context *m_ctxt;
373 protected:
374 void *m_playback_obj;
376 private:
377 string *m_debug_string;
380 /* or just use std::string? */
381 class string : public memento
383 public:
384 string (context *ctxt, const char *text);
385 ~string ();
387 const char *c_str () { return m_buffer; }
389 static string * from_printf (context *ctxt, const char *fmt, ...)
390 GNU_PRINTF(2, 3);
392 void replay_into (replayer *) {}
394 private:
395 string * make_debug_string ();
396 void write_reproducer (reproducer &r);
398 private:
399 size_t m_len;
400 char *m_buffer;
403 class location : public memento
405 public:
406 location (context *ctxt, string *filename, int line, int column,
407 bool created_by_user)
408 : memento (ctxt),
409 m_filename (filename),
410 m_line (line),
411 m_column (column),
412 m_created_by_user (created_by_user)
415 void replay_into (replayer *r);
417 playback::location *
418 playback_location (replayer *r)
420 /* Normally during playback, we can walk forwards through the list of
421 recording objects, playing them back. The ordering of recording
422 ensures that everything that a recording object refers to has
423 already been played back, so we can simply look up the relevant
424 m_playback_obj.
426 Locations are an exception, due to the "write_to_dump" method of
427 recording::statement. This method can set a new location on a
428 statement after the statement is created, and thus the location
429 appears in the context's memento list *after* the statement that
430 refers to it.
432 In such circumstances, the statement is replayed *before* the location,
433 when the latter doesn't yet have a playback object.
435 Hence we need to ensure that locations have playback objects. */
436 if (!m_playback_obj)
438 replay_into (r);
440 gcc_assert (m_playback_obj);
441 return static_cast <playback::location *> (m_playback_obj);
444 location *dyn_cast_location () { return this; }
445 bool created_by_user () const { return m_created_by_user; }
447 private:
448 string * make_debug_string ();
449 void write_reproducer (reproducer &r);
451 private:
452 string *m_filename;
453 int m_line;
454 int m_column;
455 bool m_created_by_user;
458 class type : public memento
460 public:
461 type *get_pointer ();
462 type *get_const ();
463 type *get_volatile ();
465 /* Get the type obtained when dereferencing this type.
467 This will return NULL if it's not valid to dereference this type.
468 The caller is responsible for setting an error. */
469 virtual type *dereference () = 0;
471 /* Dynamic casts. */
472 virtual function_type *dyn_cast_function_type () { return NULL; }
473 virtual function_type *as_a_function_type() { gcc_unreachable (); return NULL; }
474 virtual struct_ *dyn_cast_struct () { return NULL; }
476 /* Is it typesafe to copy to this type from rtype? */
477 virtual bool accepts_writes_from (type *rtype)
479 gcc_assert (rtype);
480 return this == rtype->unqualified ();
483 /* Strip off "const" etc */
484 virtual type *unqualified ()
486 return this;
489 virtual bool is_int () const = 0;
490 virtual bool is_float () const = 0;
491 virtual bool is_bool () const = 0;
492 virtual type *is_pointer () = 0;
493 virtual type *is_array () = 0;
494 virtual bool is_void () const { return false; }
496 bool is_numeric () const
498 return is_int () || is_float () || is_bool ();
501 playback::type *
502 playback_type ()
504 return static_cast <playback::type *> (m_playback_obj);
507 virtual const char *access_as_type (reproducer &r);
509 protected:
510 type (context *ctxt)
511 : memento (ctxt),
512 m_pointer_to_this_type (NULL)
515 private:
516 type *m_pointer_to_this_type;
519 /* Result of "gcc_jit_context_get_type". */
520 class memento_of_get_type : public type
522 public:
523 memento_of_get_type (context *ctxt,
524 enum gcc_jit_types kind)
525 : type (ctxt),
526 m_kind (kind) {}
528 type *dereference ();
530 bool accepts_writes_from (type *rtype)
532 if (m_kind == GCC_JIT_TYPE_VOID_PTR)
533 if (rtype->is_pointer ())
535 /* LHS (this) is type (void *), and the RHS is a pointer:
536 accept it: */
537 return true;
540 return type::accepts_writes_from (rtype);
543 bool is_int () const;
544 bool is_float () const;
545 bool is_bool () const;
546 type *is_pointer () { return dereference (); }
547 type *is_array () { return NULL; }
548 bool is_void () const { return m_kind == GCC_JIT_TYPE_VOID; }
550 public:
551 void replay_into (replayer *r);
553 private:
554 string * make_debug_string ();
555 void write_reproducer (reproducer &r);
557 private:
558 enum gcc_jit_types m_kind;
561 /* Result of "gcc_jit_type_get_pointer". */
562 class memento_of_get_pointer : public type
564 public:
565 memento_of_get_pointer (type *other_type)
566 : type (other_type->m_ctxt),
567 m_other_type (other_type) {}
569 type *dereference () { return m_other_type; }
571 bool accepts_writes_from (type *rtype);
573 void replay_into (replayer *r);
575 bool is_int () const { return false; }
576 bool is_float () const { return false; }
577 bool is_bool () const { return false; }
578 type *is_pointer () { return m_other_type; }
579 type *is_array () { return NULL; }
581 private:
582 string * make_debug_string ();
583 void write_reproducer (reproducer &r);
585 private:
586 type *m_other_type;
589 /* Result of "gcc_jit_type_get_const". */
590 class memento_of_get_const : public type
592 public:
593 memento_of_get_const (type *other_type)
594 : type (other_type->m_ctxt),
595 m_other_type (other_type) {}
597 type *dereference () { return m_other_type->dereference (); }
599 bool accepts_writes_from (type */*rtype*/)
601 /* Can't write to a "const". */
602 return false;
605 /* Strip off the "const", giving the underlying type. */
606 type *unqualified () { return m_other_type; }
608 bool is_int () const { return m_other_type->is_int (); }
609 bool is_float () const { return m_other_type->is_float (); }
610 bool is_bool () const { return m_other_type->is_bool (); }
611 type *is_pointer () { return m_other_type->is_pointer (); }
612 type *is_array () { return m_other_type->is_array (); }
614 void replay_into (replayer *);
616 private:
617 string * make_debug_string ();
618 void write_reproducer (reproducer &r);
620 private:
621 type *m_other_type;
624 /* Result of "gcc_jit_type_get_volatile". */
625 class memento_of_get_volatile : public type
627 public:
628 memento_of_get_volatile (type *other_type)
629 : type (other_type->m_ctxt),
630 m_other_type (other_type) {}
632 type *dereference () { return m_other_type->dereference (); }
634 /* Strip off the "volatile", giving the underlying type. */
635 type *unqualified () { return m_other_type; }
637 bool is_int () const { return m_other_type->is_int (); }
638 bool is_float () const { return m_other_type->is_float (); }
639 bool is_bool () const { return m_other_type->is_bool (); }
640 type *is_pointer () { return m_other_type->is_pointer (); }
641 type *is_array () { return m_other_type->is_array (); }
643 void replay_into (replayer *);
645 private:
646 string * make_debug_string ();
647 void write_reproducer (reproducer &r);
649 private:
650 type *m_other_type;
653 class array_type : public type
655 public:
656 array_type (context *ctxt,
657 location *loc,
658 type *element_type,
659 int num_elements)
660 : type (ctxt),
661 m_loc (loc),
662 m_element_type (element_type),
663 m_num_elements (num_elements)
666 type *dereference ();
668 bool is_int () const { return false; }
669 bool is_float () const { return false; }
670 bool is_bool () const { return false; }
671 type *is_pointer () { return NULL; }
672 type *is_array () { return m_element_type; }
674 void replay_into (replayer *);
676 private:
677 string * make_debug_string ();
678 void write_reproducer (reproducer &r);
680 private:
681 location *m_loc;
682 type *m_element_type;
683 int m_num_elements;
686 class function_type : public type
688 public:
689 function_type (context *ctxt,
690 type *return_type,
691 int num_params,
692 type **param_types,
693 int is_variadic);
695 type *dereference ();
696 function_type *dyn_cast_function_type () { return this; }
697 function_type *as_a_function_type () { return this; }
699 bool is_int () const { return false; }
700 bool is_float () const { return false; }
701 bool is_bool () const { return false; }
702 type *is_pointer () { return NULL; }
703 type *is_array () { return NULL; }
705 void replay_into (replayer *);
707 type * get_return_type () const { return m_return_type; }
708 const vec<type *> &get_param_types () const { return m_param_types; }
709 int is_variadic () const { return m_is_variadic; }
711 string * make_debug_string_with_ptr ();
713 void
714 write_deferred_reproducer (reproducer &r,
715 memento *ptr_type);
717 private:
718 string * make_debug_string ();
719 string * make_debug_string_with (const char *);
720 void write_reproducer (reproducer &r);
722 private:
723 type *m_return_type;
724 auto_vec<type *> m_param_types;
725 int m_is_variadic;
728 class field : public memento
730 public:
731 field (context *ctxt,
732 location *loc,
733 type *type,
734 string *name)
735 : memento (ctxt),
736 m_loc (loc),
737 m_type (type),
738 m_name (name),
739 m_container (NULL)
742 type * get_type () const { return m_type; }
744 compound_type * get_container () const { return m_container; }
745 void set_container (compound_type *c) { m_container = c; }
747 void replay_into (replayer *);
749 void write_to_dump (dump &d);
751 playback::field *
752 playback_field () const
754 return static_cast <playback::field *> (m_playback_obj);
757 private:
758 string * make_debug_string ();
759 void write_reproducer (reproducer &r);
761 private:
762 location *m_loc;
763 type *m_type;
764 string *m_name;
765 compound_type *m_container;
768 /* Base class for struct_ and union_ */
769 class compound_type : public type
771 public:
772 compound_type (context *ctxt,
773 location *loc,
774 string *name);
776 string *get_name () const { return m_name; }
777 location *get_loc () const { return m_loc; }
778 fields * get_fields () { return m_fields; }
780 void
781 set_fields (location *loc,
782 int num_fields,
783 field **fields);
785 type *dereference ();
787 bool is_int () const { return false; }
788 bool is_float () const { return false; }
789 bool is_bool () const { return false; }
790 type *is_pointer () { return NULL; }
791 type *is_array () { return NULL; }
793 playback::compound_type *
794 playback_compound_type ()
796 return static_cast <playback::compound_type *> (m_playback_obj);
799 private:
800 location *m_loc;
801 string *m_name;
802 fields *m_fields;
805 class struct_ : public compound_type
807 public:
808 struct_ (context *ctxt,
809 location *loc,
810 string *name);
812 struct_ *dyn_cast_struct () { return this; }
814 type *
815 as_type () { return this; }
817 void replay_into (replayer *r);
819 const char *access_as_type (reproducer &r);
821 private:
822 string * make_debug_string ();
823 void write_reproducer (reproducer &r);
826 // memento of struct_::set_fields
827 class fields : public memento
829 public:
830 fields (compound_type *struct_or_union,
831 int num_fields,
832 field **fields);
834 void replay_into (replayer *r);
836 void write_to_dump (dump &d);
838 int length () const { return m_fields.length (); }
839 field *get_field (int i) const { return m_fields[i]; }
841 private:
842 string * make_debug_string ();
843 void write_reproducer (reproducer &r);
845 private:
846 compound_type *m_struct_or_union;
847 auto_vec<field *> m_fields;
850 class union_ : public compound_type
852 public:
853 union_ (context *ctxt,
854 location *loc,
855 string *name);
857 void replay_into (replayer *r);
859 private:
860 string * make_debug_string ();
861 void write_reproducer (reproducer &r);
863 private:
864 location *m_loc;
865 string *m_name;
868 /* An abstract base class for operations that visit all rvalues within an
869 expression tree.
870 Currently the only implementation is class rvalue_usage_validator within
871 jit-recording.c. */
873 class rvalue_visitor
875 public:
876 virtual ~rvalue_visitor () {}
877 virtual void visit (rvalue *rvalue) = 0;
880 /* When generating debug strings for rvalues we mimic C, so we need to
881 mimic C's precedence levels when handling compound expressions.
882 These are in order from strongest precedence to weakest. */
883 enum precedence
885 PRECEDENCE_PRIMARY,
886 PRECEDENCE_POSTFIX,
887 PRECEDENCE_UNARY,
888 PRECEDENCE_CAST,
889 PRECEDENCE_MULTIPLICATIVE,
890 PRECEDENCE_ADDITIVE,
891 PRECEDENCE_SHIFT,
892 PRECEDENCE_RELATIONAL,
893 PRECEDENCE_EQUALITY,
894 PRECEDENCE_BITWISE_AND,
895 PRECEDENCE_BITWISE_XOR,
896 PRECEDENCE_BITWISE_IOR,
897 PRECEDENCE_LOGICAL_AND,
898 PRECEDENCE_LOGICAL_OR
901 class rvalue : public memento
903 public:
904 rvalue (context *ctxt,
905 location *loc,
906 type *type_)
907 : memento (ctxt),
908 m_loc (loc),
909 m_type (type_),
910 m_scope (NULL),
911 m_parenthesized_string (NULL)
913 gcc_assert (type_);
916 location * get_loc () const { return m_loc; }
918 /* Get the recording::type of this rvalue.
920 Implements the post-error-checking part of
921 gcc_jit_rvalue_get_type. */
922 type * get_type () const { return m_type; }
924 playback::rvalue *
925 playback_rvalue () const
927 return static_cast <playback::rvalue *> (m_playback_obj);
929 rvalue *
930 access_field (location *loc,
931 field *field);
933 lvalue *
934 dereference_field (location *loc,
935 field *field);
937 lvalue *
938 dereference (location *loc);
940 void
941 verify_valid_within_stmt (const char *api_funcname, statement *s);
943 virtual void visit_children (rvalue_visitor *v) = 0;
945 void set_scope (function *scope);
946 function *get_scope () const { return m_scope; }
948 /* Dynamic cast. */
949 virtual param *dyn_cast_param () { return NULL; }
951 virtual const char *access_as_rvalue (reproducer &r);
953 /* Get the debug string, wrapped in parentheses. */
954 const char *
955 get_debug_string_parens (enum precedence outer_prec);
957 private:
958 virtual enum precedence get_precedence () const = 0;
960 protected:
961 location *m_loc;
962 type *m_type;
964 private:
965 function *m_scope; /* NULL for globals, non-NULL for locals/params */
966 string *m_parenthesized_string;
969 class lvalue : public rvalue
971 public:
972 lvalue (context *ctxt,
973 location *loc,
974 type *type_)
975 : rvalue (ctxt, loc, type_)
978 playback::lvalue *
979 playback_lvalue () const
981 return static_cast <playback::lvalue *> (m_playback_obj);
984 lvalue *
985 access_field (location *loc,
986 field *field);
988 rvalue *
989 get_address (location *loc);
991 rvalue *
992 as_rvalue () { return this; }
994 const char *access_as_rvalue (reproducer &r);
995 virtual const char *access_as_lvalue (reproducer &r);
998 class param : public lvalue
1000 public:
1001 param (context *ctxt,
1002 location *loc,
1003 type *type,
1004 string *name)
1005 : lvalue (ctxt, loc, type),
1006 m_name (name) {}
1008 lvalue *
1009 as_lvalue () { return this; }
1011 void replay_into (replayer *r);
1013 void visit_children (rvalue_visitor *) {}
1015 playback::param *
1016 playback_param () const
1018 return static_cast <playback::param *> (m_playback_obj);
1021 param *dyn_cast_param () { return this; }
1023 const char *access_as_rvalue (reproducer &r);
1024 const char *access_as_lvalue (reproducer &r);
1026 private:
1027 string * make_debug_string () { return m_name; }
1028 void write_reproducer (reproducer &r);
1029 enum precedence get_precedence () const { return PRECEDENCE_PRIMARY; }
1031 private:
1032 string *m_name;
1035 class function : public memento
1037 public:
1038 function (context *ctxt,
1039 location *loc,
1040 enum gcc_jit_function_kind kind,
1041 type *return_type,
1042 string *name,
1043 int num_params,
1044 param **params,
1045 int is_variadic,
1046 enum built_in_function builtin_id);
1048 void replay_into (replayer *r);
1050 playback::function *
1051 playback_function () const
1053 return static_cast <playback::function *> (m_playback_obj);
1056 enum gcc_jit_function_kind get_kind () const { return m_kind; }
1058 lvalue *
1059 new_local (location *loc,
1060 type *type,
1061 const char *name);
1063 block*
1064 new_block (const char *name);
1066 location *get_loc () const { return m_loc; }
1067 type *get_return_type () const { return m_return_type; }
1068 string * get_name () const { return m_name; }
1069 const vec<param *> &get_params () const { return m_params; }
1071 /* Get the given param by index.
1072 Implements the post-error-checking part of
1073 gcc_jit_function_get_param. */
1074 param *get_param (int i) const { return m_params[i]; }
1076 bool is_variadic () const { return m_is_variadic; }
1078 void write_to_dump (dump &d);
1080 void validate ();
1082 void dump_to_dot (const char *path);
1084 private:
1085 string * make_debug_string ();
1086 void write_reproducer (reproducer &r);
1088 private:
1089 location *m_loc;
1090 enum gcc_jit_function_kind m_kind;
1091 type *m_return_type;
1092 string *m_name;
1093 auto_vec<param *> m_params;
1094 int m_is_variadic;
1095 enum built_in_function m_builtin_id;
1096 auto_vec<local *> m_locals;
1097 auto_vec<block *> m_blocks;
1100 class block : public memento
1102 public:
1103 block (function *func, int index, string *name)
1104 : memento (func->m_ctxt),
1105 m_func (func),
1106 m_index (index),
1107 m_name (name),
1108 m_statements (),
1109 m_has_been_terminated (false),
1110 m_is_reachable (false)
1114 /* Get the recording::function containing this block.
1115 Implements the post-error-checking part of
1116 gcc_jit_block_get_function. */
1117 function *get_function () { return m_func; }
1119 bool has_been_terminated () { return m_has_been_terminated; }
1120 bool is_reachable () { return m_is_reachable; }
1122 statement *
1123 add_eval (location *loc,
1124 rvalue *rvalue);
1126 statement *
1127 add_assignment (location *loc,
1128 lvalue *lvalue,
1129 rvalue *rvalue);
1131 statement *
1132 add_assignment_op (location *loc,
1133 lvalue *lvalue,
1134 enum gcc_jit_binary_op op,
1135 rvalue *rvalue);
1137 statement *
1138 add_comment (location *loc,
1139 const char *text);
1141 statement *
1142 end_with_conditional (location *loc,
1143 rvalue *boolval,
1144 block *on_true,
1145 block *on_false);
1147 statement *
1148 end_with_jump (location *loc,
1149 block *target);
1151 statement *
1152 end_with_return (location *loc,
1153 rvalue *rvalue);
1155 playback::block *
1156 playback_block () const
1158 return static_cast <playback::block *> (m_playback_obj);
1161 void write_to_dump (dump &d);
1163 bool validate ();
1165 location *get_loc () const;
1167 statement *get_first_statement () const;
1168 statement *get_last_statement () const;
1170 int get_successor_blocks (block **next1, block **next2) const;
1172 private:
1173 string * make_debug_string ();
1174 void write_reproducer (reproducer &r);
1176 void replay_into (replayer *r);
1178 void dump_to_dot (pretty_printer *pp);
1179 void dump_edges_to_dot (pretty_printer *pp);
1181 private:
1182 function *m_func;
1183 int m_index;
1184 string *m_name;
1185 auto_vec<statement *> m_statements;
1186 bool m_has_been_terminated;
1187 bool m_is_reachable;
1189 friend class function;
1192 class global : public lvalue
1194 public:
1195 global (context *ctxt,
1196 location *loc,
1197 enum gcc_jit_global_kind kind,
1198 type *type,
1199 string *name)
1200 : lvalue (ctxt, loc, type),
1201 m_kind (kind),
1202 m_name (name)
1205 void replay_into (replayer *);
1207 void visit_children (rvalue_visitor *) {}
1209 void write_to_dump (dump &d);
1211 private:
1212 string * make_debug_string () { return m_name; }
1213 void write_reproducer (reproducer &r);
1214 enum precedence get_precedence () const { return PRECEDENCE_PRIMARY; }
1216 private:
1217 enum gcc_jit_global_kind m_kind;
1218 string *m_name;
1221 template <typename HOST_TYPE>
1222 class memento_of_new_rvalue_from_const : public rvalue
1224 public:
1225 memento_of_new_rvalue_from_const (context *ctxt,
1226 location *loc,
1227 type *type,
1228 HOST_TYPE value)
1229 : rvalue (ctxt, loc, type),
1230 m_value (value) {}
1232 void replay_into (replayer *r);
1234 void visit_children (rvalue_visitor *) {}
1236 private:
1237 string * make_debug_string ();
1238 void write_reproducer (reproducer &r);
1239 enum precedence get_precedence () const { return PRECEDENCE_PRIMARY; }
1241 private:
1242 HOST_TYPE m_value;
1245 class memento_of_new_string_literal : public rvalue
1247 public:
1248 memento_of_new_string_literal (context *ctxt,
1249 location *loc,
1250 string *value)
1251 : rvalue (ctxt, loc, ctxt->get_type (GCC_JIT_TYPE_CONST_CHAR_PTR)),
1252 m_value (value) {}
1254 void replay_into (replayer *r);
1256 void visit_children (rvalue_visitor *) {}
1258 private:
1259 string * make_debug_string ();
1260 void write_reproducer (reproducer &r);
1261 enum precedence get_precedence () const { return PRECEDENCE_PRIMARY; }
1263 private:
1264 string *m_value;
1267 class unary_op : public rvalue
1269 public:
1270 unary_op (context *ctxt,
1271 location *loc,
1272 enum gcc_jit_unary_op op,
1273 type *result_type,
1274 rvalue *a)
1275 : rvalue (ctxt, loc, result_type),
1276 m_op (op),
1277 m_a (a)
1280 void replay_into (replayer *r);
1282 void visit_children (rvalue_visitor *v);
1284 private:
1285 string * make_debug_string ();
1286 void write_reproducer (reproducer &r);
1287 enum precedence get_precedence () const {return PRECEDENCE_UNARY;}
1289 private:
1290 enum gcc_jit_unary_op m_op;
1291 rvalue *m_a;
1294 class binary_op : public rvalue
1296 public:
1297 binary_op (context *ctxt,
1298 location *loc,
1299 enum gcc_jit_binary_op op,
1300 type *result_type,
1301 rvalue *a, rvalue *b)
1302 : rvalue (ctxt, loc, result_type),
1303 m_op (op),
1304 m_a (a),
1305 m_b (b) {}
1307 void replay_into (replayer *r);
1309 void visit_children (rvalue_visitor *v);
1311 private:
1312 string * make_debug_string ();
1313 void write_reproducer (reproducer &r);
1314 enum precedence get_precedence () const;
1316 private:
1317 enum gcc_jit_binary_op m_op;
1318 rvalue *m_a;
1319 rvalue *m_b;
1322 class comparison : public rvalue
1324 public:
1325 comparison (context *ctxt,
1326 location *loc,
1327 enum gcc_jit_comparison op,
1328 rvalue *a, rvalue *b)
1329 : rvalue (ctxt, loc, ctxt->get_type (GCC_JIT_TYPE_BOOL)),
1330 m_op (op),
1331 m_a (a),
1332 m_b (b)
1335 void replay_into (replayer *r);
1337 void visit_children (rvalue_visitor *v);
1339 private:
1340 string * make_debug_string ();
1341 void write_reproducer (reproducer &r);
1342 enum precedence get_precedence () const;
1344 private:
1345 enum gcc_jit_comparison m_op;
1346 rvalue *m_a;
1347 rvalue *m_b;
1350 class cast : public rvalue
1352 public:
1353 cast (context *ctxt,
1354 location *loc,
1355 rvalue *a,
1356 type *type_)
1357 : rvalue (ctxt, loc, type_),
1358 m_rvalue (a)
1361 void replay_into (replayer *r);
1363 void visit_children (rvalue_visitor *v);
1365 private:
1366 string * make_debug_string ();
1367 void write_reproducer (reproducer &r);
1368 enum precedence get_precedence () const { return PRECEDENCE_CAST; }
1370 private:
1371 rvalue *m_rvalue;
1374 class call : public rvalue
1376 public:
1377 call (context *ctxt,
1378 location *loc,
1379 function *func,
1380 int numargs,
1381 rvalue **args);
1383 void replay_into (replayer *r);
1385 void visit_children (rvalue_visitor *v);
1387 private:
1388 string * make_debug_string ();
1389 void write_reproducer (reproducer &r);
1390 enum precedence get_precedence () const { return PRECEDENCE_POSTFIX; }
1392 private:
1393 function *m_func;
1394 auto_vec<rvalue *> m_args;
1397 class call_through_ptr : public rvalue
1399 public:
1400 call_through_ptr (context *ctxt,
1401 location *loc,
1402 rvalue *fn_ptr,
1403 int numargs,
1404 rvalue **args);
1406 void replay_into (replayer *r);
1408 void visit_children (rvalue_visitor *v);
1410 private:
1411 string * make_debug_string ();
1412 void write_reproducer (reproducer &r);
1413 enum precedence get_precedence () const { return PRECEDENCE_POSTFIX; }
1415 private:
1416 rvalue *m_fn_ptr;
1417 auto_vec<rvalue *> m_args;
1420 class array_access : public lvalue
1422 public:
1423 array_access (context *ctxt,
1424 location *loc,
1425 rvalue *ptr,
1426 rvalue *index)
1427 : lvalue (ctxt, loc, ptr->get_type ()->dereference ()),
1428 m_ptr (ptr),
1429 m_index (index)
1432 void replay_into (replayer *r);
1434 void visit_children (rvalue_visitor *v);
1436 private:
1437 string * make_debug_string ();
1438 void write_reproducer (reproducer &r);
1439 enum precedence get_precedence () const { return PRECEDENCE_POSTFIX; }
1441 private:
1442 rvalue *m_ptr;
1443 rvalue *m_index;
1446 class access_field_of_lvalue : public lvalue
1448 public:
1449 access_field_of_lvalue (context *ctxt,
1450 location *loc,
1451 lvalue *val,
1452 field *field)
1453 : lvalue (ctxt, loc, field->get_type ()),
1454 m_lvalue (val),
1455 m_field (field)
1458 void replay_into (replayer *r);
1460 void visit_children (rvalue_visitor *v);
1462 private:
1463 string * make_debug_string ();
1464 void write_reproducer (reproducer &r);
1465 enum precedence get_precedence () const { return PRECEDENCE_POSTFIX; }
1467 private:
1468 lvalue *m_lvalue;
1469 field *m_field;
1472 class access_field_rvalue : public rvalue
1474 public:
1475 access_field_rvalue (context *ctxt,
1476 location *loc,
1477 rvalue *val,
1478 field *field)
1479 : rvalue (ctxt, loc, field->get_type ()),
1480 m_rvalue (val),
1481 m_field (field)
1484 void replay_into (replayer *r);
1486 void visit_children (rvalue_visitor *v);
1488 private:
1489 string * make_debug_string ();
1490 void write_reproducer (reproducer &r);
1491 enum precedence get_precedence () const { return PRECEDENCE_POSTFIX; }
1493 private:
1494 rvalue *m_rvalue;
1495 field *m_field;
1498 class dereference_field_rvalue : public lvalue
1500 public:
1501 dereference_field_rvalue (context *ctxt,
1502 location *loc,
1503 rvalue *val,
1504 field *field)
1505 : lvalue (ctxt, loc, field->get_type ()),
1506 m_rvalue (val),
1507 m_field (field)
1510 void replay_into (replayer *r);
1512 void visit_children (rvalue_visitor *v);
1514 private:
1515 string * make_debug_string ();
1516 void write_reproducer (reproducer &r);
1517 enum precedence get_precedence () const { return PRECEDENCE_POSTFIX; }
1519 private:
1520 rvalue *m_rvalue;
1521 field *m_field;
1524 class dereference_rvalue : public lvalue
1526 public:
1527 dereference_rvalue (context *ctxt,
1528 location *loc,
1529 rvalue *val)
1530 : lvalue (ctxt, loc, val->get_type ()->dereference ()),
1531 m_rvalue (val) {}
1533 void replay_into (replayer *r);
1535 void visit_children (rvalue_visitor *v);
1537 private:
1538 string * make_debug_string ();
1539 void write_reproducer (reproducer &r);
1540 enum precedence get_precedence () const { return PRECEDENCE_UNARY; }
1542 private:
1543 rvalue *m_rvalue;
1546 class get_address_of_lvalue : public rvalue
1548 public:
1549 get_address_of_lvalue (context *ctxt,
1550 location *loc,
1551 lvalue *val)
1552 : rvalue (ctxt, loc, val->get_type ()->get_pointer ()),
1553 m_lvalue (val)
1556 void replay_into (replayer *r);
1558 void visit_children (rvalue_visitor *v);
1560 private:
1561 string * make_debug_string ();
1562 void write_reproducer (reproducer &r);
1563 enum precedence get_precedence () const { return PRECEDENCE_UNARY; }
1565 private:
1566 lvalue *m_lvalue;
1569 class local : public lvalue
1571 public:
1572 local (function *func, location *loc, type *type_, string *name)
1573 : lvalue (func->m_ctxt, loc, type_),
1574 m_func (func),
1575 m_name (name)
1577 set_scope (func);
1580 void replay_into (replayer *r);
1582 void visit_children (rvalue_visitor *) {}
1584 void write_to_dump (dump &d);
1586 private:
1587 string * make_debug_string () { return m_name; }
1588 void write_reproducer (reproducer &r);
1589 enum precedence get_precedence () const { return PRECEDENCE_PRIMARY; }
1591 private:
1592 function *m_func;
1593 string *m_name;
1596 class statement : public memento
1598 public:
1599 virtual int get_successor_blocks (block **out_next1,
1600 block **out_next2) const;
1602 void write_to_dump (dump &d);
1604 block *get_block () const { return m_block; }
1605 location *get_loc () const { return m_loc; }
1607 protected:
1608 statement (block *b, location *loc)
1609 : memento (b->m_ctxt),
1610 m_block (b),
1611 m_loc (loc) {}
1613 playback::location *
1614 playback_location (replayer *r) const
1616 return ::gcc::jit::recording::playback_location (r, m_loc);
1619 private:
1620 block *m_block;
1621 location *m_loc;
1624 class eval : public statement
1626 public:
1627 eval (block *b,
1628 location *loc,
1629 rvalue *rvalue)
1630 : statement (b, loc),
1631 m_rvalue (rvalue) {}
1633 void replay_into (replayer *r);
1635 private:
1636 string * make_debug_string ();
1637 void write_reproducer (reproducer &r);
1639 private:
1640 rvalue *m_rvalue;
1643 class assignment : public statement
1645 public:
1646 assignment (block *b,
1647 location *loc,
1648 lvalue *lvalue,
1649 rvalue *rvalue)
1650 : statement (b, loc),
1651 m_lvalue (lvalue),
1652 m_rvalue (rvalue) {}
1654 void replay_into (replayer *r);
1656 private:
1657 string * make_debug_string ();
1658 void write_reproducer (reproducer &r);
1660 private:
1661 lvalue *m_lvalue;
1662 rvalue *m_rvalue;
1665 class assignment_op : public statement
1667 public:
1668 assignment_op (block *b,
1669 location *loc,
1670 lvalue *lvalue,
1671 enum gcc_jit_binary_op op,
1672 rvalue *rvalue)
1673 : statement (b, loc),
1674 m_lvalue (lvalue),
1675 m_op (op),
1676 m_rvalue (rvalue) {}
1678 void replay_into (replayer *r);
1680 private:
1681 string * make_debug_string ();
1682 void write_reproducer (reproducer &r);
1684 private:
1685 lvalue *m_lvalue;
1686 enum gcc_jit_binary_op m_op;
1687 rvalue *m_rvalue;
1690 class comment : public statement
1692 public:
1693 comment (block *b,
1694 location *loc,
1695 string *text)
1696 : statement (b, loc),
1697 m_text (text) {}
1699 void replay_into (replayer *r);
1701 private:
1702 string * make_debug_string ();
1703 void write_reproducer (reproducer &r);
1705 private:
1706 string *m_text;
1709 class conditional : public statement
1711 public:
1712 conditional (block *b,
1713 location *loc,
1714 rvalue *boolval,
1715 block *on_true,
1716 block *on_false)
1717 : statement (b, loc),
1718 m_boolval (boolval),
1719 m_on_true (on_true),
1720 m_on_false (on_false) {}
1722 void replay_into (replayer *r);
1724 int get_successor_blocks (block **out_next1,
1725 block **out_next2) const;
1727 private:
1728 string * make_debug_string ();
1729 void write_reproducer (reproducer &r);
1731 private:
1732 rvalue *m_boolval;
1733 block *m_on_true;
1734 block *m_on_false;
1737 class jump : public statement
1739 public:
1740 jump (block *b,
1741 location *loc,
1742 block *target)
1743 : statement (b, loc),
1744 m_target (target) {}
1746 void replay_into (replayer *r);
1748 int get_successor_blocks (block **out_next1,
1749 block **out_next2) const;
1751 private:
1752 string * make_debug_string ();
1753 void write_reproducer (reproducer &r);
1755 private:
1756 block *m_target;
1759 class return_ : public statement
1761 public:
1762 return_ (block *b,
1763 location *loc,
1764 rvalue *rvalue)
1765 : statement (b, loc),
1766 m_rvalue (rvalue) {}
1768 void replay_into (replayer *r);
1770 int get_successor_blocks (block **out_next1,
1771 block **out_next2) const;
1773 private:
1774 string * make_debug_string ();
1775 void write_reproducer (reproducer &r);
1777 private:
1778 rvalue *m_rvalue;
1781 } // namespace gcc::jit::recording
1783 /* Create a recording::memento_of_new_rvalue_from_const instance and add
1784 it to this context's list of mementos.
1786 Implements the post-error-checking part of
1787 gcc_jit_context_new_rvalue_from_{int|long|double|ptr}. */
1789 template <typename HOST_TYPE>
1790 recording::rvalue *
1791 recording::context::new_rvalue_from_const (recording::type *type,
1792 HOST_TYPE value)
1794 recording::rvalue *result =
1795 new memento_of_new_rvalue_from_const <HOST_TYPE> (this, NULL, type, value);
1796 record (result);
1797 return result;
1800 } // namespace gcc::jit
1802 } // namespace gcc
1804 #endif /* JIT_RECORDING_H */