1 /* Internals of libgccjit: classes for playing back recorded API calls.
2 Copyright (C) 2013-2014 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)
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_PLAYBACK_H
22 #define JIT_PLAYBACK_H
24 #include <utility> // for std::pair
26 #include "jit-recording.h"
32 /**********************************************************************
34 **********************************************************************/
41 context (::gcc::jit::recording::context
*ctxt
);
49 new_location (recording::location
*rloc
,
55 get_type (enum gcc_jit_types type
);
58 new_array_type (location
*loc
,
63 new_field (location
*loc
,
68 new_compound_type (location
*loc
,
70 bool is_struct
); /* else is union */
73 new_function_type (type
*return_type
,
74 const auto_vec
<type
*> *param_types
,
78 new_param (location
*loc
,
83 new_function (location
*loc
,
84 enum gcc_jit_function_kind kind
,
87 const auto_vec
<param
*> *params
,
89 enum built_in_function builtin_id
);
92 new_global (location
*loc
,
97 new_rvalue_from_int (type
*type
,
101 new_rvalue_from_double (type
*type
,
105 new_rvalue_from_ptr (type
*type
,
109 new_string_literal (const char *value
);
112 new_unary_op (location
*loc
,
113 enum gcc_jit_unary_op op
,
118 new_binary_op (location
*loc
,
119 enum gcc_jit_binary_op op
,
121 rvalue
*a
, rvalue
*b
);
124 new_comparison (location
*loc
,
125 enum gcc_jit_comparison op
,
126 rvalue
*a
, rvalue
*b
);
129 new_call (location
*loc
,
131 const auto_vec
<rvalue
*> *args
);
134 new_call_through_ptr (location
*loc
,
136 const auto_vec
<rvalue
*> *args
);
139 new_cast (location
*loc
,
144 new_array_access (location
*loc
,
149 set_str_option (enum gcc_jit_str_option opt
,
153 set_int_option (enum gcc_jit_int_option opt
,
157 set_bool_option (enum gcc_jit_bool_option opt
,
161 get_str_option (enum gcc_jit_str_option opt
) const
163 return m_recording_ctxt
->get_str_option (opt
);
167 get_int_option (enum gcc_jit_int_option opt
) const
169 return m_recording_ctxt
->get_int_option (opt
);
173 get_bool_option (enum gcc_jit_bool_option opt
) const
175 return m_recording_ctxt
->get_bool_option (opt
);
182 add_error (location
*loc
, const char *fmt
, ...)
186 add_error_va (location
*loc
, const char *fmt
, va_list ap
)
190 get_first_error () const;
193 set_tree_location (tree t
, location
*loc
);
196 new_field_access (location
*loc
,
201 new_dereference (tree ptr
, location
*loc
);
204 as_truth_value (tree expr
, location
*loc
);
206 bool errors_occurred () const
208 return m_recording_ctxt
->errors_occurred ();
212 void dump_generated_code ();
215 build_call (location
*loc
,
217 const auto_vec
<rvalue
*> *args
);
220 build_cast (location
*loc
,
225 get_source_file (const char *filename
);
227 void handle_locations ();
230 ::gcc::jit::recording::context
*m_recording_ctxt
;
232 /* Allocated using xmalloc (by xstrdup). */
233 char *m_path_template
;
235 /* This either aliases m_path_template, or is NULL. */
236 char *m_path_tempdir
;
238 /* The following are allocated using xmalloc. */
241 char *m_path_so_file
;
243 auto_vec
<function
*> m_functions
;
244 tree m_char_array_type_node
;
245 tree m_const_char_ptr
;
247 /* Source location handling. */
248 auto_vec
<source_file
*> m_source_files
;
250 auto_vec
<std::pair
<tree
, location
*> > m_cached_locations
;
253 /* A temporary wrapper object.
254 These objects are (mostly) only valid during replay.
255 We allocate them on the GC heap, so that they will be cleaned
256 the next time the GC collects.
257 The exception is the "function" class, which is tracked and marked by
258 the jit::context, since it needs to stay alive during post-processing
259 (when the GC could run). */
263 /* Allocate in the GC heap. */
264 void *operator new (size_t sz
);
266 /* Some wrapper subclasses contain vec<> and so need to
267 release them when they are GC-ed. */
268 virtual void finalizer () { }
272 class type
: public wrapper
279 tree
as_tree () const { return m_inner
; }
281 type
*get_pointer () const { return new type (build_pointer_type (m_inner
)); }
283 type
*get_const () const
285 return new type (build_qualified_type (m_inner
, TYPE_QUAL_CONST
));
288 type
*get_volatile () const
290 return new type (build_qualified_type (m_inner
, TYPE_QUAL_VOLATILE
));
297 class compound_type
: public type
300 compound_type (tree inner
)
304 void set_fields (const auto_vec
<field
*> *fields
);
307 class field
: public wrapper
314 tree
as_tree () const { return m_inner
; }
320 class function
: public wrapper
323 function(context
*ctxt
, tree fndecl
, enum gcc_jit_function_kind kind
);
328 tree
get_return_type_as_tree () const;
330 tree
as_fndecl () const { return m_inner_fndecl
; }
332 enum gcc_jit_function_kind
get_kind () const { return m_kind
; }
335 new_local (location
*loc
,
340 new_block (const char *name
);
353 set_tree_location (tree t
, location
*loc
)
355 m_ctxt
->set_tree_location (t
, loc
);
361 tree m_inner_bind_expr
;
362 enum gcc_jit_function_kind m_kind
;
364 tree_stmt_iterator m_stmt_iter
;
365 vec
<block
*> m_blocks
;
368 class block
: public wrapper
371 block (function
*func
,
376 tree
as_label_decl () const { return m_label_decl
; }
379 add_eval (location
*loc
,
383 add_assignment (location
*loc
,
388 add_comment (location
*loc
,
392 add_conditional (location
*loc
,
398 add_block (location
*loc
,
402 add_jump (location
*loc
,
406 add_return (location
*loc
,
411 set_tree_location (tree t
, location
*loc
)
413 m_func
->set_tree_location (t
, loc
);
416 void add_stmt (tree stmt
)
418 /* TODO: use one stmt_list per block. */
419 m_stmts
.safe_push (stmt
);
430 friend class function
;
433 class rvalue
: public wrapper
436 rvalue (context
*ctxt
, tree inner
)
442 as_rvalue () { return this; }
444 tree
as_tree () const { return m_inner
; }
446 context
*get_context () const { return m_ctxt
; }
449 get_type () { return new type (TREE_TYPE (m_inner
)); }
452 access_field (location
*loc
,
456 dereference_field (location
*loc
,
460 dereference (location
*loc
);
467 class lvalue
: public rvalue
470 lvalue (context
*ctxt
, tree inner
)
471 : rvalue(ctxt
, inner
)
475 as_lvalue () { return this; }
478 access_field (location
*loc
,
482 get_address (location
*loc
);
486 class param
: public lvalue
489 param (context
*ctxt
, tree inner
)
490 : lvalue(ctxt
, inner
)
494 /* Dealing with the linemap API.
496 It appears that libcpp requires locations to be created as if by
497 a tokenizer, creating them by filename, in ascending order of
498 line/column, whereas our API doesn't impose any such constraints:
499 we allow client code to create locations in arbitrary orders.
501 To square this circle, we need to cache all location creation,
502 grouping things up by filename/line, and then creating the linemap
503 entries in a post-processing phase. */
505 /* A set of locations, all sharing a filename */
506 class source_file
: public wrapper
509 source_file (tree filename
);
513 get_source_line (int line_num
);
515 tree
filename_as_tree () const { return m_filename
; }
518 get_filename () const { return IDENTIFIER_POINTER (m_filename
); }
520 vec
<source_line
*> m_source_lines
;
526 /* A source line, with one or more locations of interest. */
527 class source_line
: public wrapper
530 source_line (source_file
*file
, int line_num
);
534 get_location (recording::location
*rloc
, int column_num
);
536 int get_line_num () const { return m_line_num
; }
538 vec
<location
*> m_locations
;
541 source_file
*m_source_file
;
545 /* A specific location on a source line. This is what we expose
546 to the client API. */
547 class location
: public wrapper
550 location (recording::location
*loc
, source_line
*line
, int column_num
);
552 int get_column_num () const { return m_column_num
; }
554 recording::location
*get_recording_loc () const { return m_recording_loc
; }
556 source_location m_srcloc
;
559 recording::location
*m_recording_loc
;
564 } // namespace gcc::jit::playback
566 extern playback::context
*active_playback_ctxt
;
568 } // namespace gcc::jit
572 #endif /* JIT_PLAYBACK_H */