2015-06-23 Paolo Carlini <paolo.carlini@oracle.com>
[official-gcc.git] / gcc / jit / jit-common.h
blob5a8a56dce4f9c42b96b28bc3feb3e6f216b9ade9
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 "alias.h"
28 #include "flags.h"
29 #include "symtab.h"
30 #include "inchash.h"
31 #include "tree.h"
32 #include "tree-iterator.h"
34 #ifdef GCC_VERSION
35 #if GCC_VERSION >= 4001
36 #define GNU_PRINTF(M, N) __attribute__ ((format (gnu_printf, (M), (N))))
37 #else
38 #define GNU_PRINTF(M, N)
39 #endif
40 #endif
42 const int NUM_GCC_JIT_TYPES = GCC_JIT_TYPE_COMPLEX_LONG_DOUBLE + 1;
44 /* This comment is included by the docs.
46 In order to allow jit objects to be usable outside of a compile
47 whilst working with the existing structure of GCC's code the
48 C API is implemented in terms of a gcc::jit::recording::context,
49 which records the calls made to it.
51 When a gcc_jit_context is compiled, the recording context creates a
52 playback context. The playback context invokes the bulk of the GCC
53 code, and within the "frontend" parsing hook, plays back the recorded
54 API calls, creating GCC tree objects.
56 So there are two parallel families of classes: those relating to
57 recording, and those relating to playback:
59 * Visibility: recording objects are exposed back to client code,
60 whereas playback objects are internal to the library.
62 * Lifetime: recording objects have a lifetime equal to that of the
63 recording context that created them, whereas playback objects only
64 exist within the frontend hook.
66 * Memory allocation: recording objects are allocated by the recording
67 context, and automatically freed by it when the context is released,
68 whereas playback objects are allocated within the GC heap, and
69 garbage-collected; they can own GC-references.
71 * Integration with rest of GCC: recording objects are unrelated to the
72 rest of GCC, whereas playback objects are wrappers around "tree"
73 instances. Hence you can't ask a recording rvalue or lvalue what its
74 type is, whereas you can for a playback rvalue of lvalue (since it
75 can work with the underlying GCC tree nodes).
77 * Instancing: There can be multiple recording contexts "alive" at once
78 (albeit it only one compiling at once), whereas there can only be one
79 playback context alive at one time (since it interacts with the GC).
81 Ultimately if GCC could support multiple GC heaps and contexts, and
82 finer-grained initialization, then this recording vs playback
83 distinction could be eliminated.
85 During a playback, we associate objects from the recording with
86 their counterparts during this playback. For simplicity, we store this
87 within the recording objects, as ``void *m_playback_obj``, casting it to
88 the appropriate playback object subclass. For these casts to make
89 sense, the two class hierarchies need to have the same structure.
91 Note that the playback objects that ``m_playback_obj`` points to are
92 GC-allocated, but the recording objects don't own references:
93 these associations only exist within a part of the code where
94 the GC doesn't collect, and are set back to NULL before the GC can
95 run.
97 End of comment for inclusion in the docs. */
99 namespace gcc {
101 namespace jit {
103 class result;
104 class dump;
105 class logger;
106 class builtins_manager; // declared within jit-builtins.h
107 class tempdir;
109 namespace recording {
111 /* Recording types. */
113 /* Indentation indicates inheritance: */
114 class context;
115 class memento;
116 class string;
117 class location;
118 class type;
119 class function_type;
120 class compound_type;
121 class struct_;
122 class union_;
123 class field;
124 class fields;
125 class function;
126 class block;
127 class rvalue;
128 class lvalue;
129 class local;
130 class global;
131 class param;
132 class statement;
134 /* End of recording types. */
137 namespace playback {
138 /* Playback types. */
140 /* Indentation indicates inheritance: */
141 class context;
142 class wrapper;
143 class type;
144 class compound_type;
145 class field;
146 class function;
147 class block;
148 class rvalue;
149 class lvalue;
150 class param;
151 class source_file;
152 class source_line;
153 class location;
155 /* End of playback types. */
158 typedef playback::context replayer;
160 class dump
162 public:
163 dump (recording::context &ctxt,
164 const char *filename,
165 bool update_locations);
166 ~dump ();
168 recording::context &get_context () { return m_ctxt; }
170 void write (const char *fmt, ...)
171 GNU_PRINTF(2, 3);
173 bool update_locations () const { return m_update_locations; }
175 recording::location *
176 make_location () const;
178 FILE *get_file () const { return m_file; }
180 private:
181 recording::context &m_ctxt;
182 const char *m_filename;
183 bool m_update_locations;
184 int m_line;
185 int m_column;
186 FILE *m_file;
189 } // namespace gcc::jit
191 } // namespace gcc
193 #endif /* JIT_COMMON_H */