2015-09-03 Richard Biener <rguenther@suse.de>
[official-gcc.git] / gcc / jit / jit-common.h
blob6400f307d4bd6e57088d3aca9b573085bbfbf00a
1 /* Core of implementation of libgccjit.so
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_COMMON_H
22 #define JIT_COMMON_H
24 #include "libgccjit.h"
26 #include "vec.h"
27 #include "tree.h"
28 #include "inchash.h"
29 #include "tree-iterator.h"
31 #ifdef GCC_VERSION
32 #if GCC_VERSION >= 4001
33 #define GNU_PRINTF(M, N) __attribute__ ((format (gnu_printf, (M), (N))))
34 #else
35 #define GNU_PRINTF(M, N)
36 #endif
37 #endif
39 const int NUM_GCC_JIT_TYPES = GCC_JIT_TYPE_COMPLEX_LONG_DOUBLE + 1;
41 /* This comment is included by the docs.
43 In order to allow jit objects to be usable outside of a compile
44 whilst working with the existing structure of GCC's code the
45 C API is implemented in terms of a gcc::jit::recording::context,
46 which records the calls made to it.
48 When a gcc_jit_context is compiled, the recording context creates a
49 playback context. The playback context invokes the bulk of the GCC
50 code, and within the "frontend" parsing hook, plays back the recorded
51 API calls, creating GCC tree objects.
53 So there are two parallel families of classes: those relating to
54 recording, and those relating to playback:
56 * Visibility: recording objects are exposed back to client code,
57 whereas playback objects are internal to the library.
59 * Lifetime: recording objects have a lifetime equal to that of the
60 recording context that created them, whereas playback objects only
61 exist within the frontend hook.
63 * Memory allocation: recording objects are allocated by the recording
64 context, and automatically freed by it when the context is released,
65 whereas playback objects are allocated within the GC heap, and
66 garbage-collected; they can own GC-references.
68 * Integration with rest of GCC: recording objects are unrelated to the
69 rest of GCC, whereas playback objects are wrappers around "tree"
70 instances. Hence you can't ask a recording rvalue or lvalue what its
71 type is, whereas you can for a playback rvalue of lvalue (since it
72 can work with the underlying GCC tree nodes).
74 * Instancing: There can be multiple recording contexts "alive" at once
75 (albeit it only one compiling at once), whereas there can only be one
76 playback context alive at one time (since it interacts with the GC).
78 Ultimately if GCC could support multiple GC heaps and contexts, and
79 finer-grained initialization, then this recording vs playback
80 distinction could be eliminated.
82 During a playback, we associate objects from the recording with
83 their counterparts during this playback. For simplicity, we store this
84 within the recording objects, as ``void *m_playback_obj``, casting it to
85 the appropriate playback object subclass. For these casts to make
86 sense, the two class hierarchies need to have the same structure.
88 Note that the playback objects that ``m_playback_obj`` points to are
89 GC-allocated, but the recording objects don't own references:
90 these associations only exist within a part of the code where
91 the GC doesn't collect, and are set back to NULL before the GC can
92 run.
94 End of comment for inclusion in the docs. */
96 namespace gcc {
98 namespace jit {
100 class result;
101 class dump;
102 class logger;
103 class builtins_manager; // declared within jit-builtins.h
104 class tempdir;
106 namespace recording {
108 /* Recording types. */
110 /* Indentation indicates inheritance: */
111 class context;
112 class memento;
113 class string;
114 class location;
115 class type;
116 class function_type;
117 class compound_type;
118 class struct_;
119 class union_;
120 class field;
121 class fields;
122 class function;
123 class block;
124 class rvalue;
125 class lvalue;
126 class local;
127 class global;
128 class param;
129 class statement;
130 class case_;
132 /* End of recording types. */
135 namespace playback {
136 /* Playback types. */
138 /* Indentation indicates inheritance: */
139 class context;
140 class wrapper;
141 class type;
142 class compound_type;
143 class field;
144 class function;
145 class block;
146 class rvalue;
147 class lvalue;
148 class param;
149 class source_file;
150 class source_line;
151 class location;
152 class case_;
154 /* End of playback types. */
157 typedef playback::context replayer;
159 class dump
161 public:
162 dump (recording::context &ctxt,
163 const char *filename,
164 bool update_locations);
165 ~dump ();
167 recording::context &get_context () { return m_ctxt; }
169 void write (const char *fmt, ...)
170 GNU_PRINTF(2, 3);
172 bool update_locations () const { return m_update_locations; }
174 recording::location *
175 make_location () const;
177 FILE *get_file () const { return m_file; }
179 private:
180 recording::context &m_ctxt;
181 const char *m_filename;
182 bool m_update_locations;
183 int m_line;
184 int m_column;
185 FILE *m_file;
188 /* A hidden enum of boolean options that are only exposed via API
189 entrypoints, rather than via gcc_jit_context_set_bool_option. */
191 enum inner_bool_option
193 INNER_BOOL_OPTION_ALLOW_UNREACHABLE_BLOCKS,
194 INNER_BOOL_OPTION_USE_EXTERNAL_DRIVER,
196 NUM_INNER_BOOL_OPTIONS
199 } // namespace gcc::jit
201 } // namespace gcc
203 #endif /* JIT_COMMON_H */