gcc/
[official-gcc.git] / gcc / jit / jit-recording.h
blobd3170fecb7c4c459be68251d287d5b05d71212fa
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 /* When generating debug strings for rvalues we mimic C, so we need to
862 mimic C's precedence levels when handling compound expressions.
863 These are in order from strongest precedence to weakest. */
864 enum precedence
866 PRECEDENCE_PRIMARY,
867 PRECEDENCE_POSTFIX,
868 PRECEDENCE_UNARY,
869 PRECEDENCE_CAST,
870 PRECEDENCE_MULTIPLICATIVE,
871 PRECEDENCE_ADDITIVE,
872 PRECEDENCE_SHIFT,
873 PRECEDENCE_RELATIONAL,
874 PRECEDENCE_EQUALITY,
875 PRECEDENCE_BITWISE_AND,
876 PRECEDENCE_BITWISE_XOR,
877 PRECEDENCE_BITWISE_IOR,
878 PRECEDENCE_LOGICAL_AND,
879 PRECEDENCE_LOGICAL_OR
882 class rvalue : public memento
884 public:
885 rvalue (context *ctxt,
886 location *loc,
887 type *type_)
888 : memento (ctxt),
889 m_loc (loc),
890 m_type (type_),
891 m_scope (NULL),
892 m_parenthesized_string (NULL)
894 gcc_assert (type_);
897 location * get_loc () const { return m_loc; }
899 /* Get the recording::type of this rvalue.
901 Implements the post-error-checking part of
902 gcc_jit_rvalue_get_type. */
903 type * get_type () const { return m_type; }
905 playback::rvalue *
906 playback_rvalue () const
908 return static_cast <playback::rvalue *> (m_playback_obj);
910 rvalue *
911 access_field (location *loc,
912 field *field);
914 lvalue *
915 dereference_field (location *loc,
916 field *field);
918 lvalue *
919 dereference (location *loc);
921 void
922 verify_valid_within_stmt (const char *api_funcname, statement *s);
924 virtual void visit_children (rvalue_visitor *v) = 0;
926 void set_scope (function *scope);
927 function *get_scope () const { return m_scope; }
929 /* Dynamic cast. */
930 virtual param *dyn_cast_param () { return NULL; }
932 virtual const char *access_as_rvalue (reproducer &r);
934 /* Get the debug string, wrapped in parentheses. */
935 const char *
936 get_debug_string_parens (enum precedence outer_prec);
938 private:
939 virtual enum precedence get_precedence () const = 0;
941 protected:
942 location *m_loc;
943 type *m_type;
945 private:
946 function *m_scope; /* NULL for globals, non-NULL for locals/params */
947 string *m_parenthesized_string;
950 class lvalue : public rvalue
952 public:
953 lvalue (context *ctxt,
954 location *loc,
955 type *type_)
956 : rvalue (ctxt, loc, type_)
959 playback::lvalue *
960 playback_lvalue () const
962 return static_cast <playback::lvalue *> (m_playback_obj);
965 lvalue *
966 access_field (location *loc,
967 field *field);
969 rvalue *
970 get_address (location *loc);
972 rvalue *
973 as_rvalue () { return this; }
975 const char *access_as_rvalue (reproducer &r);
976 virtual const char *access_as_lvalue (reproducer &r);
979 class param : public lvalue
981 public:
982 param (context *ctxt,
983 location *loc,
984 type *type,
985 string *name)
986 : lvalue (ctxt, loc, type),
987 m_name (name) {}
989 lvalue *
990 as_lvalue () { return this; }
992 void replay_into (replayer *r);
994 void visit_children (rvalue_visitor *) {}
996 playback::param *
997 playback_param () const
999 return static_cast <playback::param *> (m_playback_obj);
1002 param *dyn_cast_param () { return this; }
1004 const char *access_as_rvalue (reproducer &r);
1005 const char *access_as_lvalue (reproducer &r);
1007 private:
1008 string * make_debug_string () { return m_name; }
1009 void write_reproducer (reproducer &r);
1010 enum precedence get_precedence () const { return PRECEDENCE_PRIMARY; }
1012 private:
1013 string *m_name;
1016 class function : public memento
1018 public:
1019 function (context *ctxt,
1020 location *loc,
1021 enum gcc_jit_function_kind kind,
1022 type *return_type,
1023 string *name,
1024 int num_params,
1025 param **params,
1026 int is_variadic,
1027 enum built_in_function builtin_id);
1029 void replay_into (replayer *r);
1031 playback::function *
1032 playback_function () const
1034 return static_cast <playback::function *> (m_playback_obj);
1037 enum gcc_jit_function_kind get_kind () const { return m_kind; }
1039 lvalue *
1040 new_local (location *loc,
1041 type *type,
1042 const char *name);
1044 block*
1045 new_block (const char *name);
1047 location *get_loc () const { return m_loc; }
1048 type *get_return_type () const { return m_return_type; }
1049 string * get_name () const { return m_name; }
1050 const vec<param *> &get_params () const { return m_params; }
1052 /* Get the given param by index.
1053 Implements the post-error-checking part of
1054 gcc_jit_function_get_param. */
1055 param *get_param (int i) const { return m_params[i]; }
1057 bool is_variadic () const { return m_is_variadic; }
1059 void write_to_dump (dump &d);
1061 void validate ();
1063 void dump_to_dot (const char *path);
1065 private:
1066 string * make_debug_string ();
1067 void write_reproducer (reproducer &r);
1069 private:
1070 location *m_loc;
1071 enum gcc_jit_function_kind m_kind;
1072 type *m_return_type;
1073 string *m_name;
1074 auto_vec<param *> m_params;
1075 int m_is_variadic;
1076 enum built_in_function m_builtin_id;
1077 auto_vec<local *> m_locals;
1078 auto_vec<block *> m_blocks;
1081 class block : public memento
1083 public:
1084 block (function *func, int index, string *name)
1085 : memento (func->m_ctxt),
1086 m_func (func),
1087 m_index (index),
1088 m_name (name),
1089 m_statements (),
1090 m_has_been_terminated (false),
1091 m_is_reachable (false)
1095 /* Get the recording::function containing this block.
1096 Implements the post-error-checking part of
1097 gcc_jit_block_get_function. */
1098 function *get_function () { return m_func; }
1100 bool has_been_terminated () { return m_has_been_terminated; }
1101 bool is_reachable () { return m_is_reachable; }
1103 statement *
1104 add_eval (location *loc,
1105 rvalue *rvalue);
1107 statement *
1108 add_assignment (location *loc,
1109 lvalue *lvalue,
1110 rvalue *rvalue);
1112 statement *
1113 add_assignment_op (location *loc,
1114 lvalue *lvalue,
1115 enum gcc_jit_binary_op op,
1116 rvalue *rvalue);
1118 statement *
1119 add_comment (location *loc,
1120 const char *text);
1122 statement *
1123 end_with_conditional (location *loc,
1124 rvalue *boolval,
1125 block *on_true,
1126 block *on_false);
1128 statement *
1129 end_with_jump (location *loc,
1130 block *target);
1132 statement *
1133 end_with_return (location *loc,
1134 rvalue *rvalue);
1136 playback::block *
1137 playback_block () const
1139 return static_cast <playback::block *> (m_playback_obj);
1142 void write_to_dump (dump &d);
1144 bool validate ();
1146 location *get_loc () const;
1148 statement *get_first_statement () const;
1149 statement *get_last_statement () const;
1151 int get_successor_blocks (block **next1, block **next2) const;
1153 private:
1154 string * make_debug_string ();
1155 void write_reproducer (reproducer &r);
1157 void replay_into (replayer *r);
1159 void dump_to_dot (pretty_printer *pp);
1160 void dump_edges_to_dot (pretty_printer *pp);
1162 private:
1163 function *m_func;
1164 int m_index;
1165 string *m_name;
1166 auto_vec<statement *> m_statements;
1167 bool m_has_been_terminated;
1168 bool m_is_reachable;
1170 friend class function;
1173 class global : public lvalue
1175 public:
1176 global (context *ctxt,
1177 location *loc,
1178 enum gcc_jit_global_kind kind,
1179 type *type,
1180 string *name)
1181 : lvalue (ctxt, loc, type),
1182 m_kind (kind),
1183 m_name (name)
1186 void replay_into (replayer *);
1188 void visit_children (rvalue_visitor *) {}
1190 void write_to_dump (dump &d);
1192 private:
1193 string * make_debug_string () { return m_name; }
1194 void write_reproducer (reproducer &r);
1195 enum precedence get_precedence () const { return PRECEDENCE_PRIMARY; }
1197 private:
1198 enum gcc_jit_global_kind m_kind;
1199 string *m_name;
1202 template <typename HOST_TYPE>
1203 class memento_of_new_rvalue_from_const : public rvalue
1205 public:
1206 memento_of_new_rvalue_from_const (context *ctxt,
1207 location *loc,
1208 type *type,
1209 HOST_TYPE value)
1210 : rvalue (ctxt, loc, type),
1211 m_value (value) {}
1213 void replay_into (replayer *r);
1215 void visit_children (rvalue_visitor *) {}
1217 private:
1218 string * make_debug_string ();
1219 void write_reproducer (reproducer &r);
1220 enum precedence get_precedence () const { return PRECEDENCE_PRIMARY; }
1222 private:
1223 HOST_TYPE m_value;
1226 class memento_of_new_string_literal : public rvalue
1228 public:
1229 memento_of_new_string_literal (context *ctxt,
1230 location *loc,
1231 string *value)
1232 : rvalue (ctxt, loc, ctxt->get_type (GCC_JIT_TYPE_CONST_CHAR_PTR)),
1233 m_value (value) {}
1235 void replay_into (replayer *r);
1237 void visit_children (rvalue_visitor *) {}
1239 private:
1240 string * make_debug_string ();
1241 void write_reproducer (reproducer &r);
1242 enum precedence get_precedence () const { return PRECEDENCE_PRIMARY; }
1244 private:
1245 string *m_value;
1248 class unary_op : public rvalue
1250 public:
1251 unary_op (context *ctxt,
1252 location *loc,
1253 enum gcc_jit_unary_op op,
1254 type *result_type,
1255 rvalue *a)
1256 : rvalue (ctxt, loc, result_type),
1257 m_op (op),
1258 m_a (a)
1261 void replay_into (replayer *r);
1263 void visit_children (rvalue_visitor *v);
1265 private:
1266 string * make_debug_string ();
1267 void write_reproducer (reproducer &r);
1268 enum precedence get_precedence () const {return PRECEDENCE_UNARY;}
1270 private:
1271 enum gcc_jit_unary_op m_op;
1272 rvalue *m_a;
1275 class binary_op : public rvalue
1277 public:
1278 binary_op (context *ctxt,
1279 location *loc,
1280 enum gcc_jit_binary_op op,
1281 type *result_type,
1282 rvalue *a, rvalue *b)
1283 : rvalue (ctxt, loc, result_type),
1284 m_op (op),
1285 m_a (a),
1286 m_b (b) {}
1288 void replay_into (replayer *r);
1290 void visit_children (rvalue_visitor *v);
1292 private:
1293 string * make_debug_string ();
1294 void write_reproducer (reproducer &r);
1295 enum precedence get_precedence () const;
1297 private:
1298 enum gcc_jit_binary_op m_op;
1299 rvalue *m_a;
1300 rvalue *m_b;
1303 class comparison : public rvalue
1305 public:
1306 comparison (context *ctxt,
1307 location *loc,
1308 enum gcc_jit_comparison op,
1309 rvalue *a, rvalue *b)
1310 : rvalue (ctxt, loc, ctxt->get_type (GCC_JIT_TYPE_BOOL)),
1311 m_op (op),
1312 m_a (a),
1313 m_b (b)
1316 void replay_into (replayer *r);
1318 void visit_children (rvalue_visitor *v);
1320 private:
1321 string * make_debug_string ();
1322 void write_reproducer (reproducer &r);
1323 enum precedence get_precedence () const;
1325 private:
1326 enum gcc_jit_comparison m_op;
1327 rvalue *m_a;
1328 rvalue *m_b;
1331 class cast : public rvalue
1333 public:
1334 cast (context *ctxt,
1335 location *loc,
1336 rvalue *a,
1337 type *type_)
1338 : rvalue (ctxt, loc, type_),
1339 m_rvalue (a)
1342 void replay_into (replayer *r);
1344 void visit_children (rvalue_visitor *v);
1346 private:
1347 string * make_debug_string ();
1348 void write_reproducer (reproducer &r);
1349 enum precedence get_precedence () const { return PRECEDENCE_CAST; }
1351 private:
1352 rvalue *m_rvalue;
1355 class call : public rvalue
1357 public:
1358 call (context *ctxt,
1359 location *loc,
1360 function *func,
1361 int numargs,
1362 rvalue **args);
1364 void replay_into (replayer *r);
1366 void visit_children (rvalue_visitor *v);
1368 private:
1369 string * make_debug_string ();
1370 void write_reproducer (reproducer &r);
1371 enum precedence get_precedence () const { return PRECEDENCE_POSTFIX; }
1373 private:
1374 function *m_func;
1375 auto_vec<rvalue *> m_args;
1378 class call_through_ptr : public rvalue
1380 public:
1381 call_through_ptr (context *ctxt,
1382 location *loc,
1383 rvalue *fn_ptr,
1384 int numargs,
1385 rvalue **args);
1387 void replay_into (replayer *r);
1389 void visit_children (rvalue_visitor *v);
1391 private:
1392 string * make_debug_string ();
1393 void write_reproducer (reproducer &r);
1394 enum precedence get_precedence () const { return PRECEDENCE_POSTFIX; }
1396 private:
1397 rvalue *m_fn_ptr;
1398 auto_vec<rvalue *> m_args;
1401 class array_access : public lvalue
1403 public:
1404 array_access (context *ctxt,
1405 location *loc,
1406 rvalue *ptr,
1407 rvalue *index)
1408 : lvalue (ctxt, loc, ptr->get_type ()->dereference ()),
1409 m_ptr (ptr),
1410 m_index (index)
1413 void replay_into (replayer *r);
1415 void visit_children (rvalue_visitor *v);
1417 private:
1418 string * make_debug_string ();
1419 void write_reproducer (reproducer &r);
1420 enum precedence get_precedence () const { return PRECEDENCE_POSTFIX; }
1422 private:
1423 rvalue *m_ptr;
1424 rvalue *m_index;
1427 class access_field_of_lvalue : public lvalue
1429 public:
1430 access_field_of_lvalue (context *ctxt,
1431 location *loc,
1432 lvalue *val,
1433 field *field)
1434 : lvalue (ctxt, loc, field->get_type ()),
1435 m_lvalue (val),
1436 m_field (field)
1439 void replay_into (replayer *r);
1441 void visit_children (rvalue_visitor *v);
1443 private:
1444 string * make_debug_string ();
1445 void write_reproducer (reproducer &r);
1446 enum precedence get_precedence () const { return PRECEDENCE_POSTFIX; }
1448 private:
1449 lvalue *m_lvalue;
1450 field *m_field;
1453 class access_field_rvalue : public rvalue
1455 public:
1456 access_field_rvalue (context *ctxt,
1457 location *loc,
1458 rvalue *val,
1459 field *field)
1460 : rvalue (ctxt, loc, field->get_type ()),
1461 m_rvalue (val),
1462 m_field (field)
1465 void replay_into (replayer *r);
1467 void visit_children (rvalue_visitor *v);
1469 private:
1470 string * make_debug_string ();
1471 void write_reproducer (reproducer &r);
1472 enum precedence get_precedence () const { return PRECEDENCE_POSTFIX; }
1474 private:
1475 rvalue *m_rvalue;
1476 field *m_field;
1479 class dereference_field_rvalue : public lvalue
1481 public:
1482 dereference_field_rvalue (context *ctxt,
1483 location *loc,
1484 rvalue *val,
1485 field *field)
1486 : lvalue (ctxt, loc, field->get_type ()),
1487 m_rvalue (val),
1488 m_field (field)
1491 void replay_into (replayer *r);
1493 void visit_children (rvalue_visitor *v);
1495 private:
1496 string * make_debug_string ();
1497 void write_reproducer (reproducer &r);
1498 enum precedence get_precedence () const { return PRECEDENCE_POSTFIX; }
1500 private:
1501 rvalue *m_rvalue;
1502 field *m_field;
1505 class dereference_rvalue : public lvalue
1507 public:
1508 dereference_rvalue (context *ctxt,
1509 location *loc,
1510 rvalue *val)
1511 : lvalue (ctxt, loc, val->get_type ()->dereference ()),
1512 m_rvalue (val) {}
1514 void replay_into (replayer *r);
1516 void visit_children (rvalue_visitor *v);
1518 private:
1519 string * make_debug_string ();
1520 void write_reproducer (reproducer &r);
1521 enum precedence get_precedence () const { return PRECEDENCE_UNARY; }
1523 private:
1524 rvalue *m_rvalue;
1527 class get_address_of_lvalue : public rvalue
1529 public:
1530 get_address_of_lvalue (context *ctxt,
1531 location *loc,
1532 lvalue *val)
1533 : rvalue (ctxt, loc, val->get_type ()->get_pointer ()),
1534 m_lvalue (val)
1537 void replay_into (replayer *r);
1539 void visit_children (rvalue_visitor *v);
1541 private:
1542 string * make_debug_string ();
1543 void write_reproducer (reproducer &r);
1544 enum precedence get_precedence () const { return PRECEDENCE_UNARY; }
1546 private:
1547 lvalue *m_lvalue;
1550 class local : public lvalue
1552 public:
1553 local (function *func, location *loc, type *type_, string *name)
1554 : lvalue (func->m_ctxt, loc, type_),
1555 m_func (func),
1556 m_name (name)
1558 set_scope (func);
1561 void replay_into (replayer *r);
1563 void visit_children (rvalue_visitor *) {}
1565 void write_to_dump (dump &d);
1567 private:
1568 string * make_debug_string () { return m_name; }
1569 void write_reproducer (reproducer &r);
1570 enum precedence get_precedence () const { return PRECEDENCE_PRIMARY; }
1572 private:
1573 function *m_func;
1574 string *m_name;
1577 class statement : public memento
1579 public:
1580 virtual int get_successor_blocks (block **out_next1,
1581 block **out_next2) const;
1583 void write_to_dump (dump &d);
1585 block *get_block () const { return m_block; }
1586 location *get_loc () const { return m_loc; }
1588 protected:
1589 statement (block *b, location *loc)
1590 : memento (b->m_ctxt),
1591 m_block (b),
1592 m_loc (loc) {}
1594 playback::location *
1595 playback_location (replayer *r) const
1597 return ::gcc::jit::recording::playback_location (r, m_loc);
1600 private:
1601 block *m_block;
1602 location *m_loc;
1605 class eval : public statement
1607 public:
1608 eval (block *b,
1609 location *loc,
1610 rvalue *rvalue)
1611 : statement (b, loc),
1612 m_rvalue (rvalue) {}
1614 void replay_into (replayer *r);
1616 private:
1617 string * make_debug_string ();
1618 void write_reproducer (reproducer &r);
1620 private:
1621 rvalue *m_rvalue;
1624 class assignment : public statement
1626 public:
1627 assignment (block *b,
1628 location *loc,
1629 lvalue *lvalue,
1630 rvalue *rvalue)
1631 : statement (b, loc),
1632 m_lvalue (lvalue),
1633 m_rvalue (rvalue) {}
1635 void replay_into (replayer *r);
1637 private:
1638 string * make_debug_string ();
1639 void write_reproducer (reproducer &r);
1641 private:
1642 lvalue *m_lvalue;
1643 rvalue *m_rvalue;
1646 class assignment_op : public statement
1648 public:
1649 assignment_op (block *b,
1650 location *loc,
1651 lvalue *lvalue,
1652 enum gcc_jit_binary_op op,
1653 rvalue *rvalue)
1654 : statement (b, loc),
1655 m_lvalue (lvalue),
1656 m_op (op),
1657 m_rvalue (rvalue) {}
1659 void replay_into (replayer *r);
1661 private:
1662 string * make_debug_string ();
1663 void write_reproducer (reproducer &r);
1665 private:
1666 lvalue *m_lvalue;
1667 enum gcc_jit_binary_op m_op;
1668 rvalue *m_rvalue;
1671 class comment : public statement
1673 public:
1674 comment (block *b,
1675 location *loc,
1676 string *text)
1677 : statement (b, loc),
1678 m_text (text) {}
1680 void replay_into (replayer *r);
1682 private:
1683 string * make_debug_string ();
1684 void write_reproducer (reproducer &r);
1686 private:
1687 string *m_text;
1690 class conditional : public statement
1692 public:
1693 conditional (block *b,
1694 location *loc,
1695 rvalue *boolval,
1696 block *on_true,
1697 block *on_false)
1698 : statement (b, loc),
1699 m_boolval (boolval),
1700 m_on_true (on_true),
1701 m_on_false (on_false) {}
1703 void replay_into (replayer *r);
1705 int get_successor_blocks (block **out_next1,
1706 block **out_next2) const;
1708 private:
1709 string * make_debug_string ();
1710 void write_reproducer (reproducer &r);
1712 private:
1713 rvalue *m_boolval;
1714 block *m_on_true;
1715 block *m_on_false;
1718 class jump : public statement
1720 public:
1721 jump (block *b,
1722 location *loc,
1723 block *target)
1724 : statement (b, loc),
1725 m_target (target) {}
1727 void replay_into (replayer *r);
1729 int get_successor_blocks (block **out_next1,
1730 block **out_next2) const;
1732 private:
1733 string * make_debug_string ();
1734 void write_reproducer (reproducer &r);
1736 private:
1737 block *m_target;
1740 class return_ : public statement
1742 public:
1743 return_ (block *b,
1744 location *loc,
1745 rvalue *rvalue)
1746 : statement (b, loc),
1747 m_rvalue (rvalue) {}
1749 void replay_into (replayer *r);
1751 int get_successor_blocks (block **out_next1,
1752 block **out_next2) const;
1754 private:
1755 string * make_debug_string ();
1756 void write_reproducer (reproducer &r);
1758 private:
1759 rvalue *m_rvalue;
1762 } // namespace gcc::jit::recording
1764 /* Create a recording::memento_of_new_rvalue_from_const instance and add
1765 it to this context's list of mementos.
1767 Implements the post-error-checking part of
1768 gcc_jit_context_new_rvalue_from_{int|long|double|ptr}. */
1770 template <typename HOST_TYPE>
1771 recording::rvalue *
1772 recording::context::new_rvalue_from_const (recording::type *type,
1773 HOST_TYPE value)
1775 recording::rvalue *result =
1776 new memento_of_new_rvalue_from_const <HOST_TYPE> (this, NULL, type, value);
1777 record (result);
1778 return result;
1781 } // namespace gcc::jit
1783 } // namespace gcc
1785 #endif /* JIT_RECORDING_H */