2014-12-12 Marc Glisse <marc.glisse@inria.fr>
[official-gcc.git] / gcc / jit / jit-common.h
blob25c2c6f9fcc0e2da25bcf8447e265a6679eb4e3f
1 /* Core of implementation of libgccjit.so
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)
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 "tree.h"
27 #include "tree-iterator.h"
29 #ifdef GCC_VERSION
30 #if GCC_VERSION >= 4001
31 #define GNU_PRINTF(M, N) __attribute__ ((format (gnu_printf, (M), (N))))
32 #else
33 #define GNU_PRINTF(M, N)
34 #endif
35 #endif
37 const int NUM_GCC_JIT_TYPES = GCC_JIT_TYPE_COMPLEX_LONG_DOUBLE + 1;
39 /* This comment is included by the docs.
41 In order to allow jit objects to be usable outside of a compile
42 whilst working with the existing structure of GCC's code the
43 C API is implemented in terms of a gcc::jit::recording::context,
44 which records the calls made to it.
46 When a gcc_jit_context is compiled, the recording context creates a
47 playback context. The playback context invokes the bulk of the GCC
48 code, and within the "frontend" parsing hook, plays back the recorded
49 API calls, creating GCC tree objects.
51 So there are two parallel families of classes: those relating to
52 recording, and those relating to playback:
54 * Visibility: recording objects are exposed back to client code,
55 whereas playback objects are internal to the library.
57 * Lifetime: recording objects have a lifetime equal to that of the
58 recording context that created them, whereas playback objects only
59 exist within the frontend hook.
61 * Memory allocation: recording objects are allocated by the recording
62 context, and automatically freed by it when the context is released,
63 whereas playback objects are allocated within the GC heap, and
64 garbage-collected; they can own GC-references.
66 * Integration with rest of GCC: recording objects are unrelated to the
67 rest of GCC, whereas playback objects are wrappers around "tree"
68 instances. Hence you can't ask a recording rvalue or lvalue what its
69 type is, whereas you can for a playback rvalue of lvalue (since it
70 can work with the underlying GCC tree nodes).
72 * Instancing: There can be multiple recording contexts "alive" at once
73 (albeit it only one compiling at once), whereas there can only be one
74 playback context alive at one time (since it interacts with the GC).
76 Ultimately if GCC could support multiple GC heaps and contexts, and
77 finer-grained initialization, then this recording vs playback
78 distinction could be eliminated.
80 During a playback, we associate objects from the recording with
81 their counterparts during this playback. For simplicity, we store this
82 within the recording objects, as ``void *m_playback_obj``, casting it to
83 the appropriate playback object subclass. For these casts to make
84 sense, the two class hierarchies need to have the same structure.
86 Note that the playback objects that ``m_playback_obj`` points to are
87 GC-allocated, but the recording objects don't own references:
88 these associations only exist within a part of the code where
89 the GC doesn't collect, and are set back to NULL before the GC can
90 run.
92 End of comment for inclusion in the docs. */
94 namespace gcc {
96 namespace jit {
98 class result;
99 class dump;
100 class builtins_manager; // declared within jit-builtins.h
101 class tempdir;
103 namespace recording {
105 /* Recording types. */
107 /* Indentation indicates inheritance: */
108 class context;
109 class memento;
110 class string;
111 class location;
112 class type;
113 class function_type;
114 class compound_type;
115 class struct_;
116 class union_;
117 class field;
118 class fields;
119 class function;
120 class block;
121 class rvalue;
122 class lvalue;
123 class local;
124 class global;
125 class param;
126 class statement;
128 /* End of recording types. */
131 namespace playback {
132 /* Playback types. */
134 /* Indentation indicates inheritance: */
135 class context;
136 class wrapper;
137 class type;
138 class compound_type;
139 class field;
140 class function;
141 class block;
142 class rvalue;
143 class lvalue;
144 class param;
145 class source_file;
146 class source_line;
147 class location;
149 /* End of playback types. */
152 typedef playback::context replayer;
154 class dump
156 public:
157 dump (recording::context &ctxt,
158 const char *filename,
159 bool update_locations);
160 ~dump ();
162 void write (const char *fmt, ...)
163 GNU_PRINTF(2, 3);
165 bool update_locations () const { return m_update_locations; }
167 recording::location *
168 make_location () const;
170 private:
171 recording::context &m_ctxt;
172 const char *m_filename;
173 bool m_update_locations;
174 int m_line;
175 int m_column;
176 FILE *m_file;
179 } // namespace gcc::jit
181 } // namespace gcc
183 #endif /* JIT_COMMON_H */