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
5This file is part of GCC.
6
7GCC is free software; you can redistribute it and/or modify it
8under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 3, or (at your option)
10any later version.
11
12GCC is distributed in the hope that it will be useful, but
13WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along 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 "hash-set.h"
27#include "input.h"
28#include "vec.h"
29#include "double-int.h"
30#include "alias.h"
31#include "flags.h"
32#include "symtab.h"
33#include "inchash.h"
34#include "tree.h"
35#include "tree-iterator.h"
36
37#ifdef GCC_VERSION
38#if GCC_VERSION >= 4001
39#define GNU_PRINTF(M, N) __attribute__ ((format (gnu_printf, (M), (N))))
40#else
41#define GNU_PRINTF(M, N)
42#endif
43#endif
44
45const int NUM_GCC_JIT_TYPES = GCC_JIT_TYPE_COMPLEX_LONG_DOUBLE + 1;
46
47/* This comment is included by the docs.
48
49   In order to allow jit objects to be usable outside of a compile
50   whilst working with the existing structure of GCC's code the
51   C API is implemented in terms of a gcc::jit::recording::context,
52   which records the calls made to it.
53
54   When a gcc_jit_context is compiled, the recording context creates a
55   playback context.  The playback context invokes the bulk of the GCC
56   code, and within the "frontend" parsing hook, plays back the recorded
57   API calls, creating GCC tree objects.
58
59   So there are two parallel families of classes: those relating to
60   recording, and those relating to playback:
61
62   * Visibility: recording objects are exposed back to client code,
63     whereas playback objects are internal to the library.
64
65   * Lifetime: recording objects have a lifetime equal to that of the
66     recording context that created them, whereas playback objects only
67     exist within the frontend hook.
68
69   * Memory allocation: recording objects are allocated by the recording
70     context, and automatically freed by it when the context is released,
71     whereas playback objects are allocated within the GC heap, and
72     garbage-collected; they can own GC-references.
73
74   * Integration with rest of GCC: recording objects are unrelated to the
75     rest of GCC, whereas playback objects are wrappers around "tree"
76     instances.  Hence you can't ask a recording rvalue or lvalue what its
77     type is, whereas you can for a playback rvalue of lvalue (since it
78     can work with the underlying GCC tree nodes).
79
80   * Instancing: There can be multiple recording contexts "alive" at once
81     (albeit it only one compiling at once), whereas there can only be one
82     playback context alive at one time (since it interacts with the GC).
83
84   Ultimately if GCC could support multiple GC heaps and contexts, and
85   finer-grained initialization, then this recording vs playback
86   distinction could be eliminated.
87
88   During a playback, we associate objects from the recording with
89   their counterparts during this playback.  For simplicity, we store this
90   within the recording objects, as ``void *m_playback_obj``, casting it to
91   the appropriate playback object subclass.  For these casts to make
92   sense, the two class hierarchies need to have the same structure.
93
94   Note that the playback objects that ``m_playback_obj`` points to are
95   GC-allocated, but the recording objects don't own references:
96   these associations only exist within a part of the code where
97   the GC doesn't collect, and are set back to NULL before the GC can
98   run.
99
100   End of comment for inclusion in the docs.  */
101
102namespace gcc {
103
104namespace jit {
105
106class result;
107class dump;
108class logger;
109class builtins_manager; // declared within jit-builtins.h
110class tempdir;
111
112namespace recording {
113
114  /* Recording types.  */
115
116  /* Indentation indicates inheritance: */
117  class context;
118  class memento;
119    class string;
120    class location;
121    class type;
122      class function_type;
123      class compound_type;
124        class struct_;
125	class union_;
126    class field;
127    class fields;
128    class function;
129    class block;
130    class rvalue;
131      class lvalue;
132        class local;
133	class global;
134        class param;
135    class statement;
136    class case_;
137
138  /* End of recording types. */
139}
140
141namespace playback {
142  /* Playback types.  */
143
144  /* Indentation indicates inheritance: */
145  class context;
146  class wrapper;
147    class type;
148      class compound_type;
149    class field;
150    class function;
151    class block;
152    class rvalue;
153      class lvalue;
154        class param;
155    class source_file;
156    class source_line;
157    class location;
158    class case_;
159
160  /* End of playback types. */
161}
162
163typedef playback::context replayer;
164
165class dump
166{
167public:
168  dump (recording::context &ctxt,
169	const char *filename,
170	bool update_locations);
171  ~dump ();
172
173  recording::context &get_context () { return m_ctxt; }
174
175  void write (const char *fmt, ...)
176    GNU_PRINTF(2, 3);
177
178  bool update_locations () const { return m_update_locations; }
179
180  recording::location *
181  make_location () const;
182
183  FILE *get_file () const { return m_file; }
184
185private:
186  recording::context &m_ctxt;
187  const char *m_filename;
188  bool m_update_locations;
189  int m_line;
190  int m_column;
191  FILE *m_file;
192};
193
194/* A hidden enum of boolean options that are only exposed via API
195   entrypoints, rather than via gcc_jit_context_set_bool_option.  */
196
197enum inner_bool_option
198{
199  INNER_BOOL_OPTION_ALLOW_UNREACHABLE_BLOCKS,
200
201  NUM_INNER_BOOL_OPTIONS
202};
203
204} // namespace gcc::jit
205
206} // namespace gcc
207
208#endif /* JIT_COMMON_H */
209