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