PR c++/65051
[official-gcc.git] / gcc / jit / jit-recording.h
blob439e7ce62d9f729ec30ac2e742d2f237d95a4caa
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 enable_dump (const char *dumpname,
200 char **out_ptr);
202 const char *
203 get_str_option (enum gcc_jit_str_option opt) const
205 return m_str_options[opt];
209 get_int_option (enum gcc_jit_int_option opt) const
211 return m_int_options[opt];
215 get_bool_option (enum gcc_jit_bool_option opt) const
217 return m_bool_options[opt];
220 result *
221 compile ();
223 void
224 compile_to_file (enum gcc_jit_output_kind output_kind,
225 const char *output_path);
227 void
228 add_error (location *loc, const char *fmt, ...)
229 GNU_PRINTF(3, 4);
231 void
232 add_error_va (location *loc, const char *fmt, va_list ap)
233 GNU_PRINTF(3, 0);
235 const char *
236 get_first_error () const;
238 const char *
239 get_last_error () const;
241 bool errors_occurred () const
243 if (m_parent_ctxt)
244 if (m_parent_ctxt->errors_occurred ())
245 return true;
246 return m_error_count;
249 type *get_opaque_FILE_type ();
251 void dump_to_file (const char *path, bool update_locations);
253 void dump_reproducer_to_file (const char *path);
255 void
256 get_all_requested_dumps (vec <recording::requested_dump> *out);
258 private:
259 void log_all_options () const;
260 void log_str_option (enum gcc_jit_str_option opt) const;
261 void log_int_option (enum gcc_jit_int_option opt) const;
262 void log_bool_option (enum gcc_jit_bool_option opt) const;
264 void validate ();
266 private:
267 context *m_parent_ctxt;
269 /* The ultimate ancestor of the contexts within a family tree of
270 contexts. This has itself as its own m_toplevel_ctxt. */
271 context *m_toplevel_ctxt;
273 int m_error_count;
275 char *m_first_error_str;
276 bool m_owns_first_error_str;
278 char *m_last_error_str;
279 bool m_owns_last_error_str;
281 char *m_str_options[GCC_JIT_NUM_STR_OPTIONS];
282 int m_int_options[GCC_JIT_NUM_INT_OPTIONS];
283 bool m_bool_options[GCC_JIT_NUM_BOOL_OPTIONS];
285 /* Dumpfiles that were requested via gcc_jit_context_enable_dump. */
286 auto_vec<requested_dump> m_requested_dumps;
288 /* Recorded API usage. */
289 auto_vec<memento *> m_mementos;
291 /* Specific recordings, for use by dump_to_file. */
292 auto_vec<compound_type *> m_compound_types;
293 auto_vec<global *> m_globals;
294 auto_vec<function *> m_functions;
296 type *m_basic_types[NUM_GCC_JIT_TYPES];
297 type *m_FILE_type;
299 builtins_manager *m_builtins_manager; // lazily created
303 /* An object with lifetime managed by the context i.e.
304 it lives until the context is released, at which
305 point it itself is cleaned up. */
307 class memento
309 public:
310 virtual ~memento () {}
312 /* Hook for replaying this. */
313 virtual void replay_into (replayer *r) = 0;
315 void set_playback_obj (void *obj) { m_playback_obj = obj; }
318 /* Get the context that owns this object.
320 Implements the post-error-checking part of
321 gcc_jit_object_get_context. */
322 context *get_context () { return m_ctxt; }
324 memento *
325 as_object () { return this; }
327 /* Debugging hook, for use in generating error messages etc.
328 Implements the post-error-checking part of
329 gcc_jit_object_get_debug_string. */
330 const char *
331 get_debug_string ();
333 virtual void write_to_dump (dump &d);
334 virtual void write_reproducer (reproducer &r) = 0;
335 virtual location *dyn_cast_location () { return NULL; }
337 protected:
338 memento (context *ctxt)
339 : m_ctxt (ctxt),
340 m_playback_obj (NULL),
341 m_debug_string (NULL)
343 gcc_assert (ctxt);
346 string *new_string (const char *text) { return m_ctxt->new_string (text); }
348 private:
349 virtual string * make_debug_string () = 0;
351 public:
352 context *m_ctxt;
354 protected:
355 void *m_playback_obj;
357 private:
358 string *m_debug_string;
361 /* or just use std::string? */
362 class string : public memento
364 public:
365 string (context *ctxt, const char *text);
366 ~string ();
368 const char *c_str () { return m_buffer; }
370 static string * from_printf (context *ctxt, const char *fmt, ...)
371 GNU_PRINTF(2, 3);
373 void replay_into (replayer *) {}
375 private:
376 string * make_debug_string ();
377 void write_reproducer (reproducer &r);
379 private:
380 size_t m_len;
381 char *m_buffer;
384 class location : public memento
386 public:
387 location (context *ctxt, string *filename, int line, int column,
388 bool created_by_user)
389 : memento (ctxt),
390 m_filename (filename),
391 m_line (line),
392 m_column (column),
393 m_created_by_user (created_by_user)
396 void replay_into (replayer *r);
398 playback::location *
399 playback_location (replayer *r)
401 /* Normally during playback, we can walk forwards through the list of
402 recording objects, playing them back. The ordering of recording
403 ensures that everything that a recording object refers to has
404 already been played back, so we can simply look up the relevant
405 m_playback_obj.
407 Locations are an exception, due to the "write_to_dump" method of
408 recording::statement. This method can set a new location on a
409 statement after the statement is created, and thus the location
410 appears in the context's memento list *after* the statement that
411 refers to it.
413 In such circumstances, the statement is replayed *before* the location,
414 when the latter doesn't yet have a playback object.
416 Hence we need to ensure that locations have playback objects. */
417 if (!m_playback_obj)
419 replay_into (r);
421 gcc_assert (m_playback_obj);
422 return static_cast <playback::location *> (m_playback_obj);
425 location *dyn_cast_location () { return this; }
426 bool created_by_user () const { return m_created_by_user; }
428 private:
429 string * make_debug_string ();
430 void write_reproducer (reproducer &r);
432 private:
433 string *m_filename;
434 int m_line;
435 int m_column;
436 bool m_created_by_user;
439 class type : public memento
441 public:
442 type *get_pointer ();
443 type *get_const ();
444 type *get_volatile ();
446 /* Get the type obtained when dereferencing this type.
448 This will return NULL if it's not valid to dereference this type.
449 The caller is responsible for setting an error. */
450 virtual type *dereference () = 0;
452 /* Dynamic casts. */
453 virtual function_type *dyn_cast_function_type () { return NULL; }
454 virtual function_type *as_a_function_type() { gcc_unreachable (); return NULL; }
455 virtual struct_ *dyn_cast_struct () { return NULL; }
457 /* Is it typesafe to copy to this type from rtype? */
458 virtual bool accepts_writes_from (type *rtype)
460 gcc_assert (rtype);
461 return this == rtype->unqualified ();
464 /* Strip off "const" etc */
465 virtual type *unqualified ()
467 return this;
470 virtual bool is_int () const = 0;
471 virtual bool is_float () const = 0;
472 virtual bool is_bool () const = 0;
473 virtual type *is_pointer () = 0;
474 virtual type *is_array () = 0;
475 virtual bool is_void () const { return false; }
477 bool is_numeric () const
479 return is_int () || is_float () || is_bool ();
482 playback::type *
483 playback_type ()
485 return static_cast <playback::type *> (m_playback_obj);
488 virtual const char *access_as_type (reproducer &r);
490 protected:
491 type (context *ctxt)
492 : memento (ctxt),
493 m_pointer_to_this_type (NULL)
496 private:
497 type *m_pointer_to_this_type;
500 /* Result of "gcc_jit_context_get_type". */
501 class memento_of_get_type : public type
503 public:
504 memento_of_get_type (context *ctxt,
505 enum gcc_jit_types kind)
506 : type (ctxt),
507 m_kind (kind) {}
509 type *dereference ();
511 bool accepts_writes_from (type *rtype)
513 if (m_kind == GCC_JIT_TYPE_VOID_PTR)
514 if (rtype->is_pointer ())
516 /* LHS (this) is type (void *), and the RHS is a pointer:
517 accept it: */
518 return true;
521 return type::accepts_writes_from (rtype);
524 bool is_int () const;
525 bool is_float () const;
526 bool is_bool () const;
527 type *is_pointer () { return dereference (); }
528 type *is_array () { return NULL; }
529 bool is_void () const { return m_kind == GCC_JIT_TYPE_VOID; }
531 public:
532 void replay_into (replayer *r);
534 private:
535 string * make_debug_string ();
536 void write_reproducer (reproducer &r);
538 private:
539 enum gcc_jit_types m_kind;
542 /* Result of "gcc_jit_type_get_pointer". */
543 class memento_of_get_pointer : public type
545 public:
546 memento_of_get_pointer (type *other_type)
547 : type (other_type->m_ctxt),
548 m_other_type (other_type) {}
550 type *dereference () { return m_other_type; }
552 bool accepts_writes_from (type *rtype);
554 void replay_into (replayer *r);
556 bool is_int () const { return false; }
557 bool is_float () const { return false; }
558 bool is_bool () const { return false; }
559 type *is_pointer () { return m_other_type; }
560 type *is_array () { return NULL; }
562 private:
563 string * make_debug_string ();
564 void write_reproducer (reproducer &r);
566 private:
567 type *m_other_type;
570 /* Result of "gcc_jit_type_get_const". */
571 class memento_of_get_const : public type
573 public:
574 memento_of_get_const (type *other_type)
575 : type (other_type->m_ctxt),
576 m_other_type (other_type) {}
578 type *dereference () { return m_other_type->dereference (); }
580 bool accepts_writes_from (type */*rtype*/)
582 /* Can't write to a "const". */
583 return false;
586 /* Strip off the "const", giving the underlying type. */
587 type *unqualified () { return m_other_type; }
589 bool is_int () const { return m_other_type->is_int (); }
590 bool is_float () const { return m_other_type->is_float (); }
591 bool is_bool () const { return m_other_type->is_bool (); }
592 type *is_pointer () { return m_other_type->is_pointer (); }
593 type *is_array () { return m_other_type->is_array (); }
595 void replay_into (replayer *);
597 private:
598 string * make_debug_string ();
599 void write_reproducer (reproducer &r);
601 private:
602 type *m_other_type;
605 /* Result of "gcc_jit_type_get_volatile". */
606 class memento_of_get_volatile : public type
608 public:
609 memento_of_get_volatile (type *other_type)
610 : type (other_type->m_ctxt),
611 m_other_type (other_type) {}
613 type *dereference () { return m_other_type->dereference (); }
615 /* Strip off the "volatile", giving the underlying type. */
616 type *unqualified () { return m_other_type; }
618 bool is_int () const { return m_other_type->is_int (); }
619 bool is_float () const { return m_other_type->is_float (); }
620 bool is_bool () const { return m_other_type->is_bool (); }
621 type *is_pointer () { return m_other_type->is_pointer (); }
622 type *is_array () { return m_other_type->is_array (); }
624 void replay_into (replayer *);
626 private:
627 string * make_debug_string ();
628 void write_reproducer (reproducer &r);
630 private:
631 type *m_other_type;
634 class array_type : public type
636 public:
637 array_type (context *ctxt,
638 location *loc,
639 type *element_type,
640 int num_elements)
641 : type (ctxt),
642 m_loc (loc),
643 m_element_type (element_type),
644 m_num_elements (num_elements)
647 type *dereference ();
649 bool is_int () const { return false; }
650 bool is_float () const { return false; }
651 bool is_bool () const { return false; }
652 type *is_pointer () { return NULL; }
653 type *is_array () { return m_element_type; }
655 void replay_into (replayer *);
657 private:
658 string * make_debug_string ();
659 void write_reproducer (reproducer &r);
661 private:
662 location *m_loc;
663 type *m_element_type;
664 int m_num_elements;
667 class function_type : public type
669 public:
670 function_type (context *ctxt,
671 type *return_type,
672 int num_params,
673 type **param_types,
674 int is_variadic);
676 type *dereference ();
677 function_type *dyn_cast_function_type () { return this; }
678 function_type *as_a_function_type () { return this; }
680 bool is_int () const { return false; }
681 bool is_float () const { return false; }
682 bool is_bool () const { return false; }
683 type *is_pointer () { return NULL; }
684 type *is_array () { return NULL; }
686 void replay_into (replayer *);
688 type * get_return_type () const { return m_return_type; }
689 const vec<type *> &get_param_types () const { return m_param_types; }
690 int is_variadic () const { return m_is_variadic; }
692 string * make_debug_string_with_ptr ();
694 void
695 write_deferred_reproducer (reproducer &r,
696 memento *ptr_type);
698 private:
699 string * make_debug_string ();
700 string * make_debug_string_with (const char *);
701 void write_reproducer (reproducer &r);
703 private:
704 type *m_return_type;
705 auto_vec<type *> m_param_types;
706 int m_is_variadic;
709 class field : public memento
711 public:
712 field (context *ctxt,
713 location *loc,
714 type *type,
715 string *name)
716 : memento (ctxt),
717 m_loc (loc),
718 m_type (type),
719 m_name (name),
720 m_container (NULL)
723 type * get_type () const { return m_type; }
725 compound_type * get_container () const { return m_container; }
726 void set_container (compound_type *c) { m_container = c; }
728 void replay_into (replayer *);
730 void write_to_dump (dump &d);
732 playback::field *
733 playback_field () const
735 return static_cast <playback::field *> (m_playback_obj);
738 private:
739 string * make_debug_string ();
740 void write_reproducer (reproducer &r);
742 private:
743 location *m_loc;
744 type *m_type;
745 string *m_name;
746 compound_type *m_container;
749 /* Base class for struct_ and union_ */
750 class compound_type : public type
752 public:
753 compound_type (context *ctxt,
754 location *loc,
755 string *name);
757 string *get_name () const { return m_name; }
758 location *get_loc () const { return m_loc; }
759 fields * get_fields () { return m_fields; }
761 void
762 set_fields (location *loc,
763 int num_fields,
764 field **fields);
766 type *dereference ();
768 bool is_int () const { return false; }
769 bool is_float () const { return false; }
770 bool is_bool () const { return false; }
771 type *is_pointer () { return NULL; }
772 type *is_array () { return NULL; }
774 playback::compound_type *
775 playback_compound_type ()
777 return static_cast <playback::compound_type *> (m_playback_obj);
780 private:
781 location *m_loc;
782 string *m_name;
783 fields *m_fields;
786 class struct_ : public compound_type
788 public:
789 struct_ (context *ctxt,
790 location *loc,
791 string *name);
793 struct_ *dyn_cast_struct () { return this; }
795 type *
796 as_type () { return this; }
798 void replay_into (replayer *r);
800 const char *access_as_type (reproducer &r);
802 private:
803 string * make_debug_string ();
804 void write_reproducer (reproducer &r);
807 // memento of struct_::set_fields
808 class fields : public memento
810 public:
811 fields (compound_type *struct_or_union,
812 int num_fields,
813 field **fields);
815 void replay_into (replayer *r);
817 void write_to_dump (dump &d);
819 int length () const { return m_fields.length (); }
820 field *get_field (int i) const { return m_fields[i]; }
822 private:
823 string * make_debug_string ();
824 void write_reproducer (reproducer &r);
826 private:
827 compound_type *m_struct_or_union;
828 auto_vec<field *> m_fields;
831 class union_ : public compound_type
833 public:
834 union_ (context *ctxt,
835 location *loc,
836 string *name);
838 void replay_into (replayer *r);
840 private:
841 string * make_debug_string ();
842 void write_reproducer (reproducer &r);
844 private:
845 location *m_loc;
846 string *m_name;
849 /* An abstract base class for operations that visit all rvalues within an
850 expression tree.
851 Currently the only implementation is class rvalue_usage_validator within
852 jit-recording.c. */
854 class rvalue_visitor
856 public:
857 virtual ~rvalue_visitor () {}
858 virtual void visit (rvalue *rvalue) = 0;
861 class rvalue : public memento
863 public:
864 rvalue (context *ctxt,
865 location *loc,
866 type *type_)
867 : memento (ctxt),
868 m_loc (loc),
869 m_type (type_),
870 m_scope (NULL)
872 gcc_assert (type_);
875 location * get_loc () const { return m_loc; }
877 /* Get the recording::type of this rvalue.
879 Implements the post-error-checking part of
880 gcc_jit_rvalue_get_type. */
881 type * get_type () const { return m_type; }
883 playback::rvalue *
884 playback_rvalue () const
886 return static_cast <playback::rvalue *> (m_playback_obj);
888 rvalue *
889 access_field (location *loc,
890 field *field);
892 lvalue *
893 dereference_field (location *loc,
894 field *field);
896 lvalue *
897 dereference (location *loc);
899 void
900 verify_valid_within_stmt (const char *api_funcname, statement *s);
902 virtual void visit_children (rvalue_visitor *v) = 0;
904 void set_scope (function *scope);
905 function *get_scope () const { return m_scope; }
907 /* Dynamic cast. */
908 virtual param *dyn_cast_param () { return NULL; }
910 virtual const char *access_as_rvalue (reproducer &r);
912 protected:
913 location *m_loc;
914 type *m_type;
916 private:
917 function *m_scope; /* NULL for globals, non-NULL for locals/params */
920 class lvalue : public rvalue
922 public:
923 lvalue (context *ctxt,
924 location *loc,
925 type *type_)
926 : rvalue (ctxt, loc, type_)
929 playback::lvalue *
930 playback_lvalue () const
932 return static_cast <playback::lvalue *> (m_playback_obj);
935 lvalue *
936 access_field (location *loc,
937 field *field);
939 rvalue *
940 get_address (location *loc);
942 rvalue *
943 as_rvalue () { return this; }
945 const char *access_as_rvalue (reproducer &r);
946 virtual const char *access_as_lvalue (reproducer &r);
949 class param : public lvalue
951 public:
952 param (context *ctxt,
953 location *loc,
954 type *type,
955 string *name)
956 : lvalue (ctxt, loc, type),
957 m_name (name) {}
959 lvalue *
960 as_lvalue () { return this; }
962 void replay_into (replayer *r);
964 void visit_children (rvalue_visitor *) {}
966 playback::param *
967 playback_param () const
969 return static_cast <playback::param *> (m_playback_obj);
972 param *dyn_cast_param () { return this; }
974 const char *access_as_rvalue (reproducer &r);
975 const char *access_as_lvalue (reproducer &r);
977 private:
978 string * make_debug_string () { return m_name; }
979 void write_reproducer (reproducer &r);
981 private:
982 string *m_name;
985 class function : public memento
987 public:
988 function (context *ctxt,
989 location *loc,
990 enum gcc_jit_function_kind kind,
991 type *return_type,
992 string *name,
993 int num_params,
994 param **params,
995 int is_variadic,
996 enum built_in_function builtin_id);
998 void replay_into (replayer *r);
1000 playback::function *
1001 playback_function () const
1003 return static_cast <playback::function *> (m_playback_obj);
1006 enum gcc_jit_function_kind get_kind () const { return m_kind; }
1008 lvalue *
1009 new_local (location *loc,
1010 type *type,
1011 const char *name);
1013 block*
1014 new_block (const char *name);
1016 location *get_loc () const { return m_loc; }
1017 type *get_return_type () const { return m_return_type; }
1018 string * get_name () const { return m_name; }
1019 const vec<param *> &get_params () const { return m_params; }
1021 /* Get the given param by index.
1022 Implements the post-error-checking part of
1023 gcc_jit_function_get_param. */
1024 param *get_param (int i) const { return m_params[i]; }
1026 bool is_variadic () const { return m_is_variadic; }
1028 void write_to_dump (dump &d);
1030 void validate ();
1032 void dump_to_dot (const char *path);
1034 private:
1035 string * make_debug_string ();
1036 void write_reproducer (reproducer &r);
1038 private:
1039 location *m_loc;
1040 enum gcc_jit_function_kind m_kind;
1041 type *m_return_type;
1042 string *m_name;
1043 auto_vec<param *> m_params;
1044 int m_is_variadic;
1045 enum built_in_function m_builtin_id;
1046 auto_vec<local *> m_locals;
1047 auto_vec<block *> m_blocks;
1050 class block : public memento
1052 public:
1053 block (function *func, int index, string *name)
1054 : memento (func->m_ctxt),
1055 m_func (func),
1056 m_index (index),
1057 m_name (name),
1058 m_statements (),
1059 m_has_been_terminated (false),
1060 m_is_reachable (false)
1064 /* Get the recording::function containing this block.
1065 Implements the post-error-checking part of
1066 gcc_jit_block_get_function. */
1067 function *get_function () { return m_func; }
1069 bool has_been_terminated () { return m_has_been_terminated; }
1070 bool is_reachable () { return m_is_reachable; }
1072 statement *
1073 add_eval (location *loc,
1074 rvalue *rvalue);
1076 statement *
1077 add_assignment (location *loc,
1078 lvalue *lvalue,
1079 rvalue *rvalue);
1081 statement *
1082 add_assignment_op (location *loc,
1083 lvalue *lvalue,
1084 enum gcc_jit_binary_op op,
1085 rvalue *rvalue);
1087 statement *
1088 add_comment (location *loc,
1089 const char *text);
1091 statement *
1092 end_with_conditional (location *loc,
1093 rvalue *boolval,
1094 block *on_true,
1095 block *on_false);
1097 statement *
1098 end_with_jump (location *loc,
1099 block *target);
1101 statement *
1102 end_with_return (location *loc,
1103 rvalue *rvalue);
1105 playback::block *
1106 playback_block () const
1108 return static_cast <playback::block *> (m_playback_obj);
1111 void write_to_dump (dump &d);
1113 bool validate ();
1115 location *get_loc () const;
1117 statement *get_first_statement () const;
1118 statement *get_last_statement () const;
1120 int get_successor_blocks (block **next1, block **next2) const;
1122 private:
1123 string * make_debug_string ();
1124 void write_reproducer (reproducer &r);
1126 void replay_into (replayer *r);
1128 void dump_to_dot (pretty_printer *pp);
1129 void dump_edges_to_dot (pretty_printer *pp);
1131 private:
1132 function *m_func;
1133 int m_index;
1134 string *m_name;
1135 auto_vec<statement *> m_statements;
1136 bool m_has_been_terminated;
1137 bool m_is_reachable;
1139 friend class function;
1142 class global : public lvalue
1144 public:
1145 global (context *ctxt,
1146 location *loc,
1147 enum gcc_jit_global_kind kind,
1148 type *type,
1149 string *name)
1150 : lvalue (ctxt, loc, type),
1151 m_kind (kind),
1152 m_name (name)
1155 void replay_into (replayer *);
1157 void visit_children (rvalue_visitor *) {}
1159 void write_to_dump (dump &d);
1161 private:
1162 string * make_debug_string () { return m_name; }
1163 void write_reproducer (reproducer &r);
1165 private:
1166 enum gcc_jit_global_kind m_kind;
1167 string *m_name;
1170 template <typename HOST_TYPE>
1171 class memento_of_new_rvalue_from_const : public rvalue
1173 public:
1174 memento_of_new_rvalue_from_const (context *ctxt,
1175 location *loc,
1176 type *type,
1177 HOST_TYPE value)
1178 : rvalue (ctxt, loc, type),
1179 m_value (value) {}
1181 void replay_into (replayer *r);
1183 void visit_children (rvalue_visitor *) {}
1185 private:
1186 string * make_debug_string ();
1187 void write_reproducer (reproducer &r);
1189 private:
1190 HOST_TYPE m_value;
1193 class memento_of_new_string_literal : public rvalue
1195 public:
1196 memento_of_new_string_literal (context *ctxt,
1197 location *loc,
1198 string *value)
1199 : rvalue (ctxt, loc, ctxt->get_type (GCC_JIT_TYPE_CONST_CHAR_PTR)),
1200 m_value (value) {}
1202 void replay_into (replayer *r);
1204 void visit_children (rvalue_visitor *) {}
1206 private:
1207 string * make_debug_string ();
1208 void write_reproducer (reproducer &r);
1210 private:
1211 string *m_value;
1214 class unary_op : public rvalue
1216 public:
1217 unary_op (context *ctxt,
1218 location *loc,
1219 enum gcc_jit_unary_op op,
1220 type *result_type,
1221 rvalue *a)
1222 : rvalue (ctxt, loc, result_type),
1223 m_op (op),
1224 m_a (a)
1227 void replay_into (replayer *r);
1229 void visit_children (rvalue_visitor *v);
1231 private:
1232 string * make_debug_string ();
1233 void write_reproducer (reproducer &r);
1235 private:
1236 enum gcc_jit_unary_op m_op;
1237 rvalue *m_a;
1240 class binary_op : public rvalue
1242 public:
1243 binary_op (context *ctxt,
1244 location *loc,
1245 enum gcc_jit_binary_op op,
1246 type *result_type,
1247 rvalue *a, rvalue *b)
1248 : rvalue (ctxt, loc, result_type),
1249 m_op (op),
1250 m_a (a),
1251 m_b (b) {}
1253 void replay_into (replayer *r);
1255 void visit_children (rvalue_visitor *v);
1257 private:
1258 string * make_debug_string ();
1259 void write_reproducer (reproducer &r);
1261 private:
1262 enum gcc_jit_binary_op m_op;
1263 rvalue *m_a;
1264 rvalue *m_b;
1267 class comparison : public rvalue
1269 public:
1270 comparison (context *ctxt,
1271 location *loc,
1272 enum gcc_jit_comparison op,
1273 rvalue *a, rvalue *b)
1274 : rvalue (ctxt, loc, ctxt->get_type (GCC_JIT_TYPE_BOOL)),
1275 m_op (op),
1276 m_a (a),
1277 m_b (b)
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);
1288 private:
1289 enum gcc_jit_comparison m_op;
1290 rvalue *m_a;
1291 rvalue *m_b;
1294 class cast : public rvalue
1296 public:
1297 cast (context *ctxt,
1298 location *loc,
1299 rvalue *a,
1300 type *type_)
1301 : rvalue (ctxt, loc, type_),
1302 m_rvalue (a)
1305 void replay_into (replayer *r);
1307 void visit_children (rvalue_visitor *v);
1309 private:
1310 string * make_debug_string ();
1311 void write_reproducer (reproducer &r);
1313 private:
1314 rvalue *m_rvalue;
1317 class call : public rvalue
1319 public:
1320 call (context *ctxt,
1321 location *loc,
1322 function *func,
1323 int numargs,
1324 rvalue **args);
1326 void replay_into (replayer *r);
1328 void visit_children (rvalue_visitor *v);
1330 private:
1331 string * make_debug_string ();
1332 void write_reproducer (reproducer &r);
1334 private:
1335 function *m_func;
1336 auto_vec<rvalue *> m_args;
1339 class call_through_ptr : public rvalue
1341 public:
1342 call_through_ptr (context *ctxt,
1343 location *loc,
1344 rvalue *fn_ptr,
1345 int numargs,
1346 rvalue **args);
1348 void replay_into (replayer *r);
1350 void visit_children (rvalue_visitor *v);
1352 private:
1353 string * make_debug_string ();
1354 void write_reproducer (reproducer &r);
1356 private:
1357 rvalue *m_fn_ptr;
1358 auto_vec<rvalue *> m_args;
1361 class array_access : public lvalue
1363 public:
1364 array_access (context *ctxt,
1365 location *loc,
1366 rvalue *ptr,
1367 rvalue *index)
1368 : lvalue (ctxt, loc, ptr->get_type ()->dereference ()),
1369 m_ptr (ptr),
1370 m_index (index)
1373 void replay_into (replayer *r);
1375 void visit_children (rvalue_visitor *v);
1377 private:
1378 string * make_debug_string ();
1379 void write_reproducer (reproducer &r);
1381 private:
1382 rvalue *m_ptr;
1383 rvalue *m_index;
1386 class access_field_of_lvalue : public lvalue
1388 public:
1389 access_field_of_lvalue (context *ctxt,
1390 location *loc,
1391 lvalue *val,
1392 field *field)
1393 : lvalue (ctxt, loc, field->get_type ()),
1394 m_lvalue (val),
1395 m_field (field)
1398 void replay_into (replayer *r);
1400 void visit_children (rvalue_visitor *v);
1402 private:
1403 string * make_debug_string ();
1404 void write_reproducer (reproducer &r);
1406 private:
1407 lvalue *m_lvalue;
1408 field *m_field;
1411 class access_field_rvalue : public rvalue
1413 public:
1414 access_field_rvalue (context *ctxt,
1415 location *loc,
1416 rvalue *val,
1417 field *field)
1418 : rvalue (ctxt, loc, field->get_type ()),
1419 m_rvalue (val),
1420 m_field (field)
1423 void replay_into (replayer *r);
1425 void visit_children (rvalue_visitor *v);
1427 private:
1428 string * make_debug_string ();
1429 void write_reproducer (reproducer &r);
1431 private:
1432 rvalue *m_rvalue;
1433 field *m_field;
1436 class dereference_field_rvalue : public lvalue
1438 public:
1439 dereference_field_rvalue (context *ctxt,
1440 location *loc,
1441 rvalue *val,
1442 field *field)
1443 : lvalue (ctxt, loc, field->get_type ()),
1444 m_rvalue (val),
1445 m_field (field)
1448 void replay_into (replayer *r);
1450 void visit_children (rvalue_visitor *v);
1452 private:
1453 string * make_debug_string ();
1454 void write_reproducer (reproducer &r);
1456 private:
1457 rvalue *m_rvalue;
1458 field *m_field;
1461 class dereference_rvalue : public lvalue
1463 public:
1464 dereference_rvalue (context *ctxt,
1465 location *loc,
1466 rvalue *val)
1467 : lvalue (ctxt, loc, val->get_type ()->dereference ()),
1468 m_rvalue (val) {}
1470 void replay_into (replayer *r);
1472 void visit_children (rvalue_visitor *v);
1474 private:
1475 string * make_debug_string ();
1476 void write_reproducer (reproducer &r);
1478 private:
1479 rvalue *m_rvalue;
1482 class get_address_of_lvalue : public rvalue
1484 public:
1485 get_address_of_lvalue (context *ctxt,
1486 location *loc,
1487 lvalue *val)
1488 : rvalue (ctxt, loc, val->get_type ()->get_pointer ()),
1489 m_lvalue (val)
1492 void replay_into (replayer *r);
1494 void visit_children (rvalue_visitor *v);
1496 private:
1497 string * make_debug_string ();
1498 void write_reproducer (reproducer &r);
1500 private:
1501 lvalue *m_lvalue;
1504 class local : public lvalue
1506 public:
1507 local (function *func, location *loc, type *type_, string *name)
1508 : lvalue (func->m_ctxt, loc, type_),
1509 m_func (func),
1510 m_name (name)
1512 set_scope (func);
1515 void replay_into (replayer *r);
1517 void visit_children (rvalue_visitor *) {}
1519 void write_to_dump (dump &d);
1521 private:
1522 string * make_debug_string () { return m_name; }
1523 void write_reproducer (reproducer &r);
1525 private:
1526 function *m_func;
1527 string *m_name;
1530 class statement : public memento
1532 public:
1533 virtual int get_successor_blocks (block **out_next1,
1534 block **out_next2) const;
1536 void write_to_dump (dump &d);
1538 block *get_block () const { return m_block; }
1539 location *get_loc () const { return m_loc; }
1541 protected:
1542 statement (block *b, location *loc)
1543 : memento (b->m_ctxt),
1544 m_block (b),
1545 m_loc (loc) {}
1547 playback::location *
1548 playback_location (replayer *r) const
1550 return ::gcc::jit::recording::playback_location (r, m_loc);
1553 private:
1554 block *m_block;
1555 location *m_loc;
1558 class eval : public statement
1560 public:
1561 eval (block *b,
1562 location *loc,
1563 rvalue *rvalue)
1564 : statement (b, loc),
1565 m_rvalue (rvalue) {}
1567 void replay_into (replayer *r);
1569 private:
1570 string * make_debug_string ();
1571 void write_reproducer (reproducer &r);
1573 private:
1574 rvalue *m_rvalue;
1577 class assignment : public statement
1579 public:
1580 assignment (block *b,
1581 location *loc,
1582 lvalue *lvalue,
1583 rvalue *rvalue)
1584 : statement (b, loc),
1585 m_lvalue (lvalue),
1586 m_rvalue (rvalue) {}
1588 void replay_into (replayer *r);
1590 private:
1591 string * make_debug_string ();
1592 void write_reproducer (reproducer &r);
1594 private:
1595 lvalue *m_lvalue;
1596 rvalue *m_rvalue;
1599 class assignment_op : public statement
1601 public:
1602 assignment_op (block *b,
1603 location *loc,
1604 lvalue *lvalue,
1605 enum gcc_jit_binary_op op,
1606 rvalue *rvalue)
1607 : statement (b, loc),
1608 m_lvalue (lvalue),
1609 m_op (op),
1610 m_rvalue (rvalue) {}
1612 void replay_into (replayer *r);
1614 private:
1615 string * make_debug_string ();
1616 void write_reproducer (reproducer &r);
1618 private:
1619 lvalue *m_lvalue;
1620 enum gcc_jit_binary_op m_op;
1621 rvalue *m_rvalue;
1624 class comment : public statement
1626 public:
1627 comment (block *b,
1628 location *loc,
1629 string *text)
1630 : statement (b, loc),
1631 m_text (text) {}
1633 void replay_into (replayer *r);
1635 private:
1636 string * make_debug_string ();
1637 void write_reproducer (reproducer &r);
1639 private:
1640 string *m_text;
1643 class conditional : public statement
1645 public:
1646 conditional (block *b,
1647 location *loc,
1648 rvalue *boolval,
1649 block *on_true,
1650 block *on_false)
1651 : statement (b, loc),
1652 m_boolval (boolval),
1653 m_on_true (on_true),
1654 m_on_false (on_false) {}
1656 void replay_into (replayer *r);
1658 int get_successor_blocks (block **out_next1,
1659 block **out_next2) const;
1661 private:
1662 string * make_debug_string ();
1663 void write_reproducer (reproducer &r);
1665 private:
1666 rvalue *m_boolval;
1667 block *m_on_true;
1668 block *m_on_false;
1671 class jump : public statement
1673 public:
1674 jump (block *b,
1675 location *loc,
1676 block *target)
1677 : statement (b, loc),
1678 m_target (target) {}
1680 void replay_into (replayer *r);
1682 int get_successor_blocks (block **out_next1,
1683 block **out_next2) const;
1685 private:
1686 string * make_debug_string ();
1687 void write_reproducer (reproducer &r);
1689 private:
1690 block *m_target;
1693 class return_ : public statement
1695 public:
1696 return_ (block *b,
1697 location *loc,
1698 rvalue *rvalue)
1699 : statement (b, loc),
1700 m_rvalue (rvalue) {}
1702 void replay_into (replayer *r);
1704 int get_successor_blocks (block **out_next1,
1705 block **out_next2) const;
1707 private:
1708 string * make_debug_string ();
1709 void write_reproducer (reproducer &r);
1711 private:
1712 rvalue *m_rvalue;
1715 } // namespace gcc::jit::recording
1717 /* Create a recording::memento_of_new_rvalue_from_const instance and add
1718 it to this context's list of mementos.
1720 Implements the post-error-checking part of
1721 gcc_jit_context_new_rvalue_from_{int|long|double|ptr}. */
1723 template <typename HOST_TYPE>
1724 recording::rvalue *
1725 recording::context::new_rvalue_from_const (recording::type *type,
1726 HOST_TYPE value)
1728 recording::rvalue *result =
1729 new memento_of_new_rvalue_from_const <HOST_TYPE> (this, NULL, type, value);
1730 record (result);
1731 return result;
1734 } // namespace gcc::jit
1736 } // namespace gcc
1738 #endif /* JIT_RECORDING_H */