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