New jit API entrypoint: gcc_jit_context_set_logfile
[official-gcc.git] / gcc / jit / jit-common.h
blob80e1cbef1f5a41bd8c4168798821802bee294a11
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 "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 logger;
101 class builtins_manager; // declared within jit-builtins.h
102 class tempdir;
104 namespace recording {
106 /* Recording types. */
108 /* Indentation indicates inheritance: */
109 class context;
110 class memento;
111 class string;
112 class location;
113 class type;
114 class function_type;
115 class compound_type;
116 class struct_;
117 class union_;
118 class field;
119 class fields;
120 class function;
121 class block;
122 class rvalue;
123 class lvalue;
124 class local;
125 class global;
126 class param;
127 class statement;
129 /* End of recording types. */
132 namespace playback {
133 /* Playback types. */
135 /* Indentation indicates inheritance: */
136 class context;
137 class wrapper;
138 class type;
139 class compound_type;
140 class field;
141 class function;
142 class block;
143 class rvalue;
144 class lvalue;
145 class param;
146 class source_file;
147 class source_line;
148 class location;
150 /* End of playback types. */
153 typedef playback::context replayer;
155 class dump
157 public:
158 dump (recording::context &ctxt,
159 const char *filename,
160 bool update_locations);
161 ~dump ();
163 void write (const char *fmt, ...)
164 GNU_PRINTF(2, 3);
166 bool update_locations () const { return m_update_locations; }
168 recording::location *
169 make_location () const;
171 private:
172 recording::context &m_ctxt;
173 const char *m_filename;
174 bool m_update_locations;
175 int m_line;
176 int m_column;
177 FILE *m_file;
180 } // namespace gcc::jit
182 } // namespace gcc
184 #endif /* JIT_COMMON_H */