1 /* jit.c -- Dummy "frontend" for use during JIT-compilation.
2 Copyright (C) 2013-2020 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
10
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
19
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "jit-playback.h"
24 #include "stor-layout.h"
25 #include "debug.h"
26 #include "langhooks.h"
27 #include "langhooks-def.h"
28 #include "diagnostic.h"
29
30
31 #include <mpfr.h>
32
33 /* Language-dependent contents of a type. */
34
35 struct GTY(()) lang_type
36 {
37 char dummy;
38 };
39
40 /* Language-dependent contents of a decl. */
41
42 struct GTY((variable_size)) lang_decl
43 {
44 char dummy;
45 };
46
47 /* Language-dependent contents of an identifier. This must include a
48 tree_identifier. */
49
50 struct GTY(()) lang_identifier
51 {
52 struct tree_identifier common;
53 };
54
55 /* The resulting tree type. */
56
57 union GTY((desc ("TREE_CODE (&%h.generic) == IDENTIFIER_NODE"),
58 chain_next ("CODE_CONTAINS_STRUCT (TREE_CODE (&%h.generic), TS_COMMON) ? ((union lang_tree_node *) TREE_CHAIN (&%h.generic)) : NULL")))
59 lang_tree_node
60 {
61 union tree_node GTY((tag ("0"),
62 desc ("tree_node_structure (&%h)"))) generic;
63 struct lang_identifier GTY((tag ("1"))) identifier;
64 };
65
66 /* We don't use language_function. */
67
68 struct GTY(()) language_function
69 {
70 int dummy;
71 };
72
73 /* GC-marking callback for use from jit_root_tab.
74
75 If there's an active playback context, call its marking method
76 so that it can mark any pointers it references. */
77
my_ggc_walker(void *)78 static void my_ggc_walker (void *)
79 {
80 if (gcc::jit::active_playback_ctxt)
81 gcc::jit::active_playback_ctxt->gt_ggc_mx ();
82 }
83
84 const char *dummy;
85
86 struct ggc_root_tab jit_root_tab[] =
87 {
88 {
89 &dummy, 1, 0, my_ggc_walker, NULL
90 },
91 LAST_GGC_ROOT_TAB
92 };
93
94 /* JIT-specific implementation of diagnostic callbacks. */
95
96 /* Implementation of "begin_diagnostic". */
97
98 static void
jit_begin_diagnostic(diagnostic_context *,diagnostic_info *)99 jit_begin_diagnostic (diagnostic_context */*context*/,
100 diagnostic_info */*diagnostic*/)
101 {
102 gcc_assert (gcc::jit::active_playback_ctxt);
103 JIT_LOG_SCOPE (gcc::jit::active_playback_ctxt->get_logger ());
104
105 /* No-op (apart from logging); the real error-handling is done in the
106 "end_diagnostic" hook. */
107 }
108
109 /* Implementation of "end_diagnostic". */
110
111 static void
jit_end_diagnostic(diagnostic_context * context,diagnostic_info * diagnostic,diagnostic_t)112 jit_end_diagnostic (diagnostic_context *context,
113 diagnostic_info *diagnostic,
114 diagnostic_t)
115 {
116 gcc_assert (gcc::jit::active_playback_ctxt);
117 JIT_LOG_SCOPE (gcc::jit::active_playback_ctxt->get_logger ());
118
119 /* Delegate to the playback context (and thence to the
120 recording context). */
121 gcc::jit::active_playback_ctxt->add_diagnostic (context, diagnostic);
122 }
123
124 /* Language hooks. */
125
126 static bool
jit_langhook_init(void)127 jit_langhook_init (void)
128 {
129 gcc_assert (gcc::jit::active_playback_ctxt);
130 JIT_LOG_SCOPE (gcc::jit::active_playback_ctxt->get_logger ());
131
132 static bool registered_root_tab = false;
133 if (!registered_root_tab)
134 {
135 ggc_register_root_tab (jit_root_tab);
136 registered_root_tab = true;
137 }
138
139 gcc_assert (global_dc);
140 global_dc->begin_diagnostic = jit_begin_diagnostic;
141 global_dc->end_diagnostic = jit_end_diagnostic;
142
143 build_common_tree_nodes (false);
144
145 /* I don't know why this has to be done explicitly. */
146 void_list_node = build_tree_list (NULL_TREE, void_type_node);
147
148 build_common_builtin_nodes ();
149
150 /* The default precision for floating point numbers. This is used
151 for floating point constants with abstract type. This may
152 eventually be controllable by a command line option. */
153 mpfr_set_default_prec (256);
154
155 return true;
156 }
157
158 static void
jit_langhook_parse_file(void)159 jit_langhook_parse_file (void)
160 {
161 /* Replay the activity by the client, recorded on the context. */
162 gcc_assert (gcc::jit::active_playback_ctxt);
163 gcc::jit::active_playback_ctxt->replay ();
164 }
165
166 static tree
jit_langhook_type_for_mode(machine_mode mode,int unsignedp)167 jit_langhook_type_for_mode (machine_mode mode, int unsignedp)
168 {
169 /* Build any vector types here (see PR 46805). */
170 if (VECTOR_MODE_P (mode))
171 {
172 tree inner;
173
174 inner = jit_langhook_type_for_mode (GET_MODE_INNER (mode), unsignedp);
175 if (inner != NULL_TREE)
176 return build_vector_type_for_mode (inner, mode);
177 return NULL_TREE;
178 }
179
180 if (mode == TYPE_MODE (float_type_node))
181 return float_type_node;
182
183 if (mode == TYPE_MODE (double_type_node))
184 return double_type_node;
185
186 if (mode == TYPE_MODE (intQI_type_node))
187 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
188 if (mode == TYPE_MODE (intHI_type_node))
189 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
190 if (mode == TYPE_MODE (intSI_type_node))
191 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
192 if (mode == TYPE_MODE (intDI_type_node))
193 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
194 if (mode == TYPE_MODE (intTI_type_node))
195 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
196
197 if (mode == TYPE_MODE (integer_type_node))
198 return unsignedp ? unsigned_type_node : integer_type_node;
199
200 if (mode == TYPE_MODE (long_integer_type_node))
201 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
202
203 if (mode == TYPE_MODE (long_long_integer_type_node))
204 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
205
206 if (COMPLEX_MODE_P (mode))
207 {
208 if (mode == TYPE_MODE (complex_float_type_node))
209 return complex_float_type_node;
210 if (mode == TYPE_MODE (complex_double_type_node))
211 return complex_double_type_node;
212 if (mode == TYPE_MODE (complex_long_double_type_node))
213 return complex_long_double_type_node;
214 if (mode == TYPE_MODE (complex_integer_type_node) && !unsignedp)
215 return complex_integer_type_node;
216 }
217
218 /* gcc_unreachable */
219 return NULL;
220 }
221
222 /* Record a builtin function. We just ignore builtin functions. */
223
224 static tree
jit_langhook_builtin_function(tree decl)225 jit_langhook_builtin_function (tree decl)
226 {
227 return decl;
228 }
229
230 static bool
jit_langhook_global_bindings_p(void)231 jit_langhook_global_bindings_p (void)
232 {
233 gcc_unreachable ();
234 return true;
235 }
236
237 static tree
jit_langhook_pushdecl(tree decl ATTRIBUTE_UNUSED)238 jit_langhook_pushdecl (tree decl ATTRIBUTE_UNUSED)
239 {
240 gcc_unreachable ();
241 }
242
243 static tree
jit_langhook_getdecls(void)244 jit_langhook_getdecls (void)
245 {
246 return NULL;
247 }
248
249 #undef LANG_HOOKS_NAME
250 #define LANG_HOOKS_NAME "libgccjit"
251
252 #undef LANG_HOOKS_INIT
253 #define LANG_HOOKS_INIT jit_langhook_init
254
255 #undef LANG_HOOKS_PARSE_FILE
256 #define LANG_HOOKS_PARSE_FILE jit_langhook_parse_file
257
258 #undef LANG_HOOKS_TYPE_FOR_MODE
259 #define LANG_HOOKS_TYPE_FOR_MODE jit_langhook_type_for_mode
260
261 #undef LANG_HOOKS_BUILTIN_FUNCTION
262 #define LANG_HOOKS_BUILTIN_FUNCTION jit_langhook_builtin_function
263
264 #undef LANG_HOOKS_GLOBAL_BINDINGS_P
265 #define LANG_HOOKS_GLOBAL_BINDINGS_P jit_langhook_global_bindings_p
266
267 #undef LANG_HOOKS_PUSHDECL
268 #define LANG_HOOKS_PUSHDECL jit_langhook_pushdecl
269
270 #undef LANG_HOOKS_GETDECLS
271 #define LANG_HOOKS_GETDECLS jit_langhook_getdecls
272
273 struct lang_hooks lang_hooks = LANG_HOOKS_INITIALIZER;
274
275 #include "gt-jit-dummy-frontend.h"
276 #include "gtype-jit.h"
277