annotate gcc/jit/jit-builtins.c @ 131:84e7813d76e9

gcc-8.2
author mir3636
date Thu, 25 Oct 2018 07:37:49 +0900
parents 04ced10e8804
children 1830386684a0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
111
kono
parents:
diff changeset
1 /* jit-builtins.c -- Handling of builtin functions during JIT-compilation.
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2 Copyright (C) 2014-2018 Free Software Foundation, Inc.
111
kono
parents:
diff changeset
3
kono
parents:
diff changeset
4 This file is part of GCC.
kono
parents:
diff changeset
5
kono
parents:
diff changeset
6 GCC is free software; you can redistribute it and/or modify it under
kono
parents:
diff changeset
7 the terms of the GNU General Public License as published by the Free
kono
parents:
diff changeset
8 Software Foundation; either version 3, or (at your option) any later
kono
parents:
diff changeset
9 version.
kono
parents:
diff changeset
10
kono
parents:
diff changeset
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
kono
parents:
diff changeset
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
kono
parents:
diff changeset
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
kono
parents:
diff changeset
14 for more details.
kono
parents:
diff changeset
15
kono
parents:
diff changeset
16 You should have received a copy of the GNU General Public License
kono
parents:
diff changeset
17 along with GCC; see the file COPYING3. If not see
kono
parents:
diff changeset
18 <http://www.gnu.org/licenses/>. */
kono
parents:
diff changeset
19
kono
parents:
diff changeset
20 #include "config.h"
kono
parents:
diff changeset
21 #include "system.h"
kono
parents:
diff changeset
22 #include "coretypes.h"
kono
parents:
diff changeset
23 #include "target.h"
kono
parents:
diff changeset
24 #include "jit-playback.h"
kono
parents:
diff changeset
25 #include "stringpool.h"
kono
parents:
diff changeset
26
kono
parents:
diff changeset
27 #include "jit-builtins.h"
kono
parents:
diff changeset
28
kono
parents:
diff changeset
29 namespace gcc {
kono
parents:
diff changeset
30
kono
parents:
diff changeset
31 namespace jit {
kono
parents:
diff changeset
32
kono
parents:
diff changeset
33 const char *const prefix = "__builtin_";
kono
parents:
diff changeset
34 const size_t prefix_len = strlen (prefix);
kono
parents:
diff changeset
35
kono
parents:
diff changeset
36 /* Create "builtin_data", a const table of the data within builtins.def. */
kono
parents:
diff changeset
37 struct builtin_data
kono
parents:
diff changeset
38 {
kono
parents:
diff changeset
39 const char *name;
kono
parents:
diff changeset
40 enum built_in_class fnclass;
kono
parents:
diff changeset
41 enum jit_builtin_type type;
kono
parents:
diff changeset
42 bool both_p;
kono
parents:
diff changeset
43 bool fallback_p;
kono
parents:
diff changeset
44 enum built_in_attribute attr;
kono
parents:
diff changeset
45 bool implicit_p;
kono
parents:
diff changeset
46
kono
parents:
diff changeset
47 const char *get_asm_name () const
kono
parents:
diff changeset
48 {
kono
parents:
diff changeset
49 if (both_p && fallback_p)
kono
parents:
diff changeset
50 return name + prefix_len;
kono
parents:
diff changeset
51 else
kono
parents:
diff changeset
52 return name;
kono
parents:
diff changeset
53 }
kono
parents:
diff changeset
54 };
kono
parents:
diff changeset
55
kono
parents:
diff changeset
56 #define DEF_BUILTIN(X, NAME, CLASS, TYPE, LT, BOTH_P, FALLBACK_P, \
kono
parents:
diff changeset
57 NONANSI_P, ATTRS, IMPLICIT, COND) \
kono
parents:
diff changeset
58 {NAME, CLASS, TYPE, BOTH_P, FALLBACK_P, ATTRS, IMPLICIT},
kono
parents:
diff changeset
59 static const struct builtin_data builtin_data[] =
kono
parents:
diff changeset
60 {
kono
parents:
diff changeset
61 #include "builtins.def"
kono
parents:
diff changeset
62 };
kono
parents:
diff changeset
63
kono
parents:
diff changeset
64 /* Helper function for find_builtin_by_name. */
kono
parents:
diff changeset
65
kono
parents:
diff changeset
66 static bool
kono
parents:
diff changeset
67 matches_builtin (const char *in_name,
kono
parents:
diff changeset
68 const struct builtin_data& bd)
kono
parents:
diff changeset
69 {
kono
parents:
diff changeset
70 const bool debug = 0;
kono
parents:
diff changeset
71
kono
parents:
diff changeset
72 /* Ignore entries with a NULL name. */
kono
parents:
diff changeset
73 if (!bd.name)
kono
parents:
diff changeset
74 return false;
kono
parents:
diff changeset
75
kono
parents:
diff changeset
76 if (debug)
kono
parents:
diff changeset
77 fprintf (stderr, "seen builtin: %s\n", bd.name);
kono
parents:
diff changeset
78
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
79 if (strcmp (bd.name, in_name) == 0)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
80 return true;
111
kono
parents:
diff changeset
81
kono
parents:
diff changeset
82 if (bd.both_p)
kono
parents:
diff changeset
83 {
kono
parents:
diff changeset
84 /* Then the macros in builtins.def gave a "__builtin_"
kono
parents:
diff changeset
85 prefix to bd.name, but we should also recognize the form
kono
parents:
diff changeset
86 without the prefix. */
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
87 gcc_assert (strncmp (bd.name, prefix, prefix_len) == 0);
111
kono
parents:
diff changeset
88 if (debug)
kono
parents:
diff changeset
89 fprintf (stderr, "testing without prefix as: %s\n",
kono
parents:
diff changeset
90 bd.name + prefix_len);
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
91 if (strcmp (bd.name + prefix_len, in_name) == 0)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
92 return true;
111
kono
parents:
diff changeset
93 }
kono
parents:
diff changeset
94
kono
parents:
diff changeset
95 return false;
kono
parents:
diff changeset
96 }
kono
parents:
diff changeset
97
kono
parents:
diff changeset
98 /* Locate the built-in function that matches name IN_NAME,
kono
parents:
diff changeset
99 writing the result to OUT_ID and returning true if found,
kono
parents:
diff changeset
100 or returning false if not found. */
kono
parents:
diff changeset
101
kono
parents:
diff changeset
102 static bool
kono
parents:
diff changeset
103 find_builtin_by_name (const char *in_name,
kono
parents:
diff changeset
104 enum built_in_function *out_id)
kono
parents:
diff changeset
105 {
kono
parents:
diff changeset
106 /* Locate builtin. This currently works by performing repeated
kono
parents:
diff changeset
107 strcmp against every possible candidate, which is likely to
kono
parents:
diff changeset
108 inefficient.
kono
parents:
diff changeset
109
kono
parents:
diff changeset
110 We start at index 1 to skip the initial entry (BUILT_IN_NONE), which
kono
parents:
diff changeset
111 has a NULL name. */
kono
parents:
diff changeset
112 for (unsigned int i = 1;
kono
parents:
diff changeset
113 i < sizeof (builtin_data) / sizeof (builtin_data[0]);
kono
parents:
diff changeset
114 i++)
kono
parents:
diff changeset
115 {
kono
parents:
diff changeset
116 const struct builtin_data& bd = builtin_data[i];
kono
parents:
diff changeset
117 if (matches_builtin (in_name, bd))
kono
parents:
diff changeset
118 {
kono
parents:
diff changeset
119 /* Found a match. */
kono
parents:
diff changeset
120 *out_id = static_cast<enum built_in_function> (i);
kono
parents:
diff changeset
121 return true;
kono
parents:
diff changeset
122 }
kono
parents:
diff changeset
123 }
kono
parents:
diff changeset
124
kono
parents:
diff changeset
125 /* Not found. */
kono
parents:
diff changeset
126 return false;
kono
parents:
diff changeset
127 }
kono
parents:
diff changeset
128
kono
parents:
diff changeset
129 // class builtins_manager
kono
parents:
diff changeset
130
kono
parents:
diff changeset
131 /* Constructor for gcc::jit::builtins_manager. */
kono
parents:
diff changeset
132
kono
parents:
diff changeset
133 builtins_manager::builtins_manager (recording::context *ctxt)
kono
parents:
diff changeset
134 : m_ctxt (ctxt)
kono
parents:
diff changeset
135 {
kono
parents:
diff changeset
136 memset (m_types, 0, sizeof (m_types));
kono
parents:
diff changeset
137 memset (m_builtin_functions, 0, sizeof (m_builtin_functions));
kono
parents:
diff changeset
138 memset (m_attributes, 0, sizeof (m_attributes));
kono
parents:
diff changeset
139 }
kono
parents:
diff changeset
140
kono
parents:
diff changeset
141 /* Locate a builtin function by name.
kono
parents:
diff changeset
142 Create a recording::function of the appropriate type, reusing them
kono
parents:
diff changeset
143 if they've already been seen. */
kono
parents:
diff changeset
144
kono
parents:
diff changeset
145 recording::function *
kono
parents:
diff changeset
146 builtins_manager::get_builtin_function (const char *name)
kono
parents:
diff changeset
147 {
kono
parents:
diff changeset
148 enum built_in_function builtin_id;
kono
parents:
diff changeset
149 if (!find_builtin_by_name (name, &builtin_id))
kono
parents:
diff changeset
150 {
kono
parents:
diff changeset
151 m_ctxt->add_error (NULL, "builtin \"%s\" not found", name);
kono
parents:
diff changeset
152 return NULL;
kono
parents:
diff changeset
153 }
kono
parents:
diff changeset
154
kono
parents:
diff changeset
155 return get_builtin_function_by_id (builtin_id);
kono
parents:
diff changeset
156 }
kono
parents:
diff changeset
157
kono
parents:
diff changeset
158 /* Locate a builtin function by id.
kono
parents:
diff changeset
159 Create a recording::function of the appropriate type, reusing them
kono
parents:
diff changeset
160 if they've already been seen. */
kono
parents:
diff changeset
161
kono
parents:
diff changeset
162 recording::function *
kono
parents:
diff changeset
163 builtins_manager::get_builtin_function_by_id (enum built_in_function builtin_id)
kono
parents:
diff changeset
164 {
kono
parents:
diff changeset
165 gcc_assert (builtin_id >= 0);
kono
parents:
diff changeset
166 gcc_assert (builtin_id < END_BUILTINS);
kono
parents:
diff changeset
167
kono
parents:
diff changeset
168 /* Lazily build the functions, caching them so that repeated calls for
kono
parents:
diff changeset
169 the same id on a context give back the same object. */
kono
parents:
diff changeset
170 if (!m_builtin_functions[builtin_id])
kono
parents:
diff changeset
171 {
kono
parents:
diff changeset
172 recording::function *fn = make_builtin_function (builtin_id);
kono
parents:
diff changeset
173 if (fn)
kono
parents:
diff changeset
174 {
kono
parents:
diff changeset
175 m_builtin_functions[builtin_id] = fn;
kono
parents:
diff changeset
176 m_ctxt->record (fn);
kono
parents:
diff changeset
177 }
kono
parents:
diff changeset
178 }
kono
parents:
diff changeset
179
kono
parents:
diff changeset
180 return m_builtin_functions[builtin_id];
kono
parents:
diff changeset
181 }
kono
parents:
diff changeset
182
kono
parents:
diff changeset
183 /* Create the recording::function for a given builtin function, by ID. */
kono
parents:
diff changeset
184
kono
parents:
diff changeset
185 recording::function *
kono
parents:
diff changeset
186 builtins_manager::make_builtin_function (enum built_in_function builtin_id)
kono
parents:
diff changeset
187 {
kono
parents:
diff changeset
188 const struct builtin_data& bd = builtin_data[builtin_id];
kono
parents:
diff changeset
189 enum jit_builtin_type type_id = bd.type;
kono
parents:
diff changeset
190 recording::type *t = get_type (type_id);
kono
parents:
diff changeset
191 if (!t)
kono
parents:
diff changeset
192 return NULL;
kono
parents:
diff changeset
193 recording::function_type *func_type = t->as_a_function_type ();
kono
parents:
diff changeset
194 if (!func_type)
kono
parents:
diff changeset
195 return NULL;
kono
parents:
diff changeset
196
kono
parents:
diff changeset
197 vec<recording::type *> param_types = func_type->get_param_types ();
kono
parents:
diff changeset
198 recording::param **params = new recording::param *[param_types.length ()];
kono
parents:
diff changeset
199
kono
parents:
diff changeset
200 int i;
kono
parents:
diff changeset
201 recording::type *param_type;
kono
parents:
diff changeset
202 FOR_EACH_VEC_ELT (param_types, i, param_type)
kono
parents:
diff changeset
203 {
kono
parents:
diff changeset
204 char buf[16];
kono
parents:
diff changeset
205 snprintf (buf, 16, "arg%d", i);
kono
parents:
diff changeset
206 params[i] = m_ctxt->new_param (NULL,
kono
parents:
diff changeset
207 param_type,
kono
parents:
diff changeset
208 buf);
kono
parents:
diff changeset
209 }
kono
parents:
diff changeset
210 const char *asm_name = bd.get_asm_name ();
kono
parents:
diff changeset
211 recording::function *result =
kono
parents:
diff changeset
212 new recording::function (m_ctxt,
kono
parents:
diff changeset
213 NULL,
kono
parents:
diff changeset
214 GCC_JIT_FUNCTION_IMPORTED, // FIXME
kono
parents:
diff changeset
215 func_type->get_return_type (),
kono
parents:
diff changeset
216 m_ctxt->new_string (asm_name),
kono
parents:
diff changeset
217 param_types.length (),
kono
parents:
diff changeset
218 params,
kono
parents:
diff changeset
219 func_type->is_variadic (),
kono
parents:
diff changeset
220 builtin_id);
kono
parents:
diff changeset
221 delete[] params;
kono
parents:
diff changeset
222
kono
parents:
diff changeset
223 /* PR/64020 - If the client code is using builtin cos or sin,
kono
parents:
diff changeset
224 tree-ssa-math-opt.c's execute_cse_sincos_1 may attempt
kono
parents:
diff changeset
225 to optimize them to use __builtin_cexpi; for this,
kono
parents:
diff changeset
226 BUILT_IN_CEXPI needs to exist.
kono
parents:
diff changeset
227
kono
parents:
diff changeset
228 Hence query the cache for BUILT_IN_CEXPI to ensure it gets
kono
parents:
diff changeset
229 built. */
kono
parents:
diff changeset
230 if (builtin_id == BUILT_IN_COS || builtin_id == BUILT_IN_SIN)
kono
parents:
diff changeset
231 (void)get_builtin_function_by_id (BUILT_IN_CEXPI);
kono
parents:
diff changeset
232
kono
parents:
diff changeset
233 /* builtins.c:expand_builtin_cexpi can optimize the various
kono
parents:
diff changeset
234 CEXP builtins to SINCOS builtins, and hence we may require
kono
parents:
diff changeset
235 SINCOS builtins latter.
kono
parents:
diff changeset
236
kono
parents:
diff changeset
237 Ensure the appropriate SINCOS builtin exists. */
kono
parents:
diff changeset
238 if (builtin_id == BUILT_IN_CEXPIF)
kono
parents:
diff changeset
239 (void)get_builtin_function_by_id (BUILT_IN_SINCOSF);
kono
parents:
diff changeset
240 else if (builtin_id == BUILT_IN_CEXPI)
kono
parents:
diff changeset
241 (void)get_builtin_function_by_id (BUILT_IN_SINCOS);
kono
parents:
diff changeset
242 else if (builtin_id == BUILT_IN_CEXPIL)
kono
parents:
diff changeset
243 (void)get_builtin_function_by_id (BUILT_IN_SINCOSL);
kono
parents:
diff changeset
244
kono
parents:
diff changeset
245 return result;
kono
parents:
diff changeset
246 }
kono
parents:
diff changeset
247
kono
parents:
diff changeset
248 /* Get the recording::type for a given type of builtin function,
kono
parents:
diff changeset
249 by ID, creating it if it doesn't already exist. */
kono
parents:
diff changeset
250
kono
parents:
diff changeset
251 recording::type *
kono
parents:
diff changeset
252 builtins_manager::get_type (enum jit_builtin_type type_id)
kono
parents:
diff changeset
253 {
kono
parents:
diff changeset
254 if (!m_types[type_id])
kono
parents:
diff changeset
255 m_types[type_id] = make_type (type_id);
kono
parents:
diff changeset
256 return m_types[type_id];
kono
parents:
diff changeset
257 }
kono
parents:
diff changeset
258
kono
parents:
diff changeset
259 /* Create the recording::type for a given type of builtin function. */
kono
parents:
diff changeset
260
kono
parents:
diff changeset
261 recording::type *
kono
parents:
diff changeset
262 builtins_manager::make_type (enum jit_builtin_type type_id)
kono
parents:
diff changeset
263 {
kono
parents:
diff changeset
264 /* Use builtin-types.def to construct a switch statement, with each
kono
parents:
diff changeset
265 case deferring to one of the methods below:
kono
parents:
diff changeset
266 - DEF_PRIMITIVE_TYPE is handled as a call to make_primitive_type.
kono
parents:
diff changeset
267 - the various DEF_FUNCTION_TYPE_n are handled by variadic calls
kono
parents:
diff changeset
268 to make_fn_type.
kono
parents:
diff changeset
269 - similarly for DEF_FUNCTION_TYPE_VAR_n, but setting the
kono
parents:
diff changeset
270 "is_variadic" argument.
kono
parents:
diff changeset
271 - DEF_POINTER_TYPE is handled by make_ptr_type.
kono
parents:
diff changeset
272 That should handle everything, but just in case we also suppy a
kono
parents:
diff changeset
273 gcc_unreachable default clause. */
kono
parents:
diff changeset
274 switch (type_id)
kono
parents:
diff changeset
275 {
kono
parents:
diff changeset
276 #define DEF_PRIMITIVE_TYPE(ENUM, VALUE) \
kono
parents:
diff changeset
277 case ENUM: return make_primitive_type (ENUM);
kono
parents:
diff changeset
278 #define DEF_FUNCTION_TYPE_0(ENUM, RETURN) \
kono
parents:
diff changeset
279 case ENUM: return make_fn_type (ENUM, RETURN, 0, 0);
kono
parents:
diff changeset
280 #define DEF_FUNCTION_TYPE_1(ENUM, RETURN, ARG1) \
kono
parents:
diff changeset
281 case ENUM: return make_fn_type (ENUM, RETURN, 0, 1, ARG1);
kono
parents:
diff changeset
282 #define DEF_FUNCTION_TYPE_2(ENUM, RETURN, ARG1, ARG2) \
kono
parents:
diff changeset
283 case ENUM: return make_fn_type (ENUM, RETURN, 0, 2, ARG1, ARG2);
kono
parents:
diff changeset
284 #define DEF_FUNCTION_TYPE_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
kono
parents:
diff changeset
285 case ENUM: return make_fn_type (ENUM, RETURN, 0, 3, ARG1, ARG2, ARG3);
kono
parents:
diff changeset
286 #define DEF_FUNCTION_TYPE_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
kono
parents:
diff changeset
287 case ENUM: return make_fn_type (ENUM, RETURN, 0, 4, ARG1, ARG2, ARG3, \
kono
parents:
diff changeset
288 ARG4);
kono
parents:
diff changeset
289 #define DEF_FUNCTION_TYPE_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
kono
parents:
diff changeset
290 case ENUM: return make_fn_type (ENUM, RETURN, 0, 5, ARG1, ARG2, ARG3, \
kono
parents:
diff changeset
291 ARG4, ARG5);
kono
parents:
diff changeset
292 #define DEF_FUNCTION_TYPE_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
kono
parents:
diff changeset
293 ARG6) \
kono
parents:
diff changeset
294 case ENUM: return make_fn_type (ENUM, RETURN, 0, 6, ARG1, ARG2, ARG3, \
kono
parents:
diff changeset
295 ARG4, ARG5, ARG6);
kono
parents:
diff changeset
296 #define DEF_FUNCTION_TYPE_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
kono
parents:
diff changeset
297 ARG6, ARG7) \
kono
parents:
diff changeset
298 case ENUM: return make_fn_type (ENUM, RETURN, 0, 7, ARG1, ARG2, ARG3, \
kono
parents:
diff changeset
299 ARG4, ARG5, ARG6, ARG7);
kono
parents:
diff changeset
300 #define DEF_FUNCTION_TYPE_8(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
kono
parents:
diff changeset
301 ARG6, ARG7, ARG8) \
kono
parents:
diff changeset
302 case ENUM: return make_fn_type (ENUM, RETURN, 0, 8, ARG1, ARG2, ARG3, \
kono
parents:
diff changeset
303 ARG4, ARG5, ARG6, ARG7, ARG8);
kono
parents:
diff changeset
304 #define DEF_FUNCTION_TYPE_9(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
kono
parents:
diff changeset
305 ARG6, ARG7, ARG8, ARG9) \
kono
parents:
diff changeset
306 case ENUM: return make_fn_type (ENUM, RETURN, 0, 9, ARG1, ARG2, ARG3, \
kono
parents:
diff changeset
307 ARG4, ARG5, ARG6, ARG7, ARG8, ARG9);
kono
parents:
diff changeset
308 #define DEF_FUNCTION_TYPE_10(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
kono
parents:
diff changeset
309 ARG6, ARG7, ARG8, ARG9, ARG10) \
kono
parents:
diff changeset
310 case ENUM: return make_fn_type (ENUM, RETURN, 0, 10, ARG1, ARG2, ARG3, \
kono
parents:
diff changeset
311 ARG4, ARG5, ARG6, ARG7, ARG8, ARG9, \
kono
parents:
diff changeset
312 ARG10);
kono
parents:
diff changeset
313 #define DEF_FUNCTION_TYPE_11(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
kono
parents:
diff changeset
314 ARG6, ARG7, ARG8, ARG9, ARG10, ARG11) \
kono
parents:
diff changeset
315 case ENUM: return make_fn_type (ENUM, RETURN, 0, 11, ARG1, ARG2, ARG3, \
kono
parents:
diff changeset
316 ARG4, ARG5, ARG6, ARG7, ARG8, ARG9, \
kono
parents:
diff changeset
317 ARG10, ARG11);
kono
parents:
diff changeset
318 #define DEF_FUNCTION_TYPE_VAR_0(ENUM, RETURN) \
kono
parents:
diff changeset
319 case ENUM: return make_fn_type (ENUM, RETURN, 1, 0);
kono
parents:
diff changeset
320 #define DEF_FUNCTION_TYPE_VAR_1(ENUM, RETURN, ARG1) \
kono
parents:
diff changeset
321 case ENUM: return make_fn_type (ENUM, RETURN, 1, 1, ARG1);
kono
parents:
diff changeset
322 #define DEF_FUNCTION_TYPE_VAR_2(ENUM, RETURN, ARG1, ARG2) \
kono
parents:
diff changeset
323 case ENUM: return make_fn_type (ENUM, RETURN, 1, 2, ARG1, ARG2);
kono
parents:
diff changeset
324 #define DEF_FUNCTION_TYPE_VAR_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
kono
parents:
diff changeset
325 case ENUM: return make_fn_type (ENUM, RETURN, 1, 3, ARG1, ARG2, ARG3);
kono
parents:
diff changeset
326 #define DEF_FUNCTION_TYPE_VAR_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
kono
parents:
diff changeset
327 case ENUM: return make_fn_type (ENUM, RETURN, 1, 4, ARG1, ARG2, ARG3, \
kono
parents:
diff changeset
328 ARG4);
kono
parents:
diff changeset
329 #define DEF_FUNCTION_TYPE_VAR_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
kono
parents:
diff changeset
330 case ENUM: return make_fn_type (ENUM, RETURN, 1, 5, ARG1, ARG2, ARG3, \
kono
parents:
diff changeset
331 ARG4, ARG5);
kono
parents:
diff changeset
332 #define DEF_FUNCTION_TYPE_VAR_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
kono
parents:
diff changeset
333 ARG6) \
kono
parents:
diff changeset
334 case ENUM: return make_fn_type (ENUM, RETURN, 1, 6, ARG1, ARG2, ARG3, \
kono
parents:
diff changeset
335 ARG4, ARG5, ARG6);
kono
parents:
diff changeset
336 #define DEF_FUNCTION_TYPE_VAR_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
kono
parents:
diff changeset
337 ARG6, ARG7) \
kono
parents:
diff changeset
338 case ENUM: return make_fn_type (ENUM, RETURN, 1, 7, ARG1, ARG2, ARG3, \
kono
parents:
diff changeset
339 ARG4, ARG5, ARG6, ARG7);
kono
parents:
diff changeset
340 #define DEF_POINTER_TYPE(ENUM, TYPE) \
kono
parents:
diff changeset
341 case ENUM: return make_ptr_type (ENUM, TYPE);
kono
parents:
diff changeset
342
kono
parents:
diff changeset
343 #include "builtin-types.def"
kono
parents:
diff changeset
344
kono
parents:
diff changeset
345 #undef DEF_PRIMITIVE_TYPE
kono
parents:
diff changeset
346 #undef DEF_FUNCTION_TYPE_0
kono
parents:
diff changeset
347 #undef DEF_FUNCTION_TYPE_1
kono
parents:
diff changeset
348 #undef DEF_FUNCTION_TYPE_2
kono
parents:
diff changeset
349 #undef DEF_FUNCTION_TYPE_3
kono
parents:
diff changeset
350 #undef DEF_FUNCTION_TYPE_4
kono
parents:
diff changeset
351 #undef DEF_FUNCTION_TYPE_5
kono
parents:
diff changeset
352 #undef DEF_FUNCTION_TYPE_6
kono
parents:
diff changeset
353 #undef DEF_FUNCTION_TYPE_7
kono
parents:
diff changeset
354 #undef DEF_FUNCTION_TYPE_8
kono
parents:
diff changeset
355 #undef DEF_FUNCTION_TYPE_9
kono
parents:
diff changeset
356 #undef DEF_FUNCTION_TYPE_10
kono
parents:
diff changeset
357 #undef DEF_FUNCTION_TYPE_11
kono
parents:
diff changeset
358 #undef DEF_FUNCTION_TYPE_VAR_0
kono
parents:
diff changeset
359 #undef DEF_FUNCTION_TYPE_VAR_1
kono
parents:
diff changeset
360 #undef DEF_FUNCTION_TYPE_VAR_2
kono
parents:
diff changeset
361 #undef DEF_FUNCTION_TYPE_VAR_3
kono
parents:
diff changeset
362 #undef DEF_FUNCTION_TYPE_VAR_4
kono
parents:
diff changeset
363 #undef DEF_FUNCTION_TYPE_VAR_5
kono
parents:
diff changeset
364 #undef DEF_FUNCTION_TYPE_VAR_6
kono
parents:
diff changeset
365 #undef DEF_FUNCTION_TYPE_VAR_7
kono
parents:
diff changeset
366 #undef DEF_POINTER_TYPE
kono
parents:
diff changeset
367
kono
parents:
diff changeset
368 default:
kono
parents:
diff changeset
369 gcc_unreachable ();
kono
parents:
diff changeset
370 }
kono
parents:
diff changeset
371 }
kono
parents:
diff changeset
372
kono
parents:
diff changeset
373 /* Create the recording::type for a given primitive type within the
kono
parents:
diff changeset
374 builtin system.
kono
parents:
diff changeset
375
kono
parents:
diff changeset
376 Only some types are currently supported. */
kono
parents:
diff changeset
377
kono
parents:
diff changeset
378 recording::type*
kono
parents:
diff changeset
379 builtins_manager::make_primitive_type (enum jit_builtin_type type_id)
kono
parents:
diff changeset
380 {
kono
parents:
diff changeset
381 switch (type_id)
kono
parents:
diff changeset
382 {
kono
parents:
diff changeset
383 default:
kono
parents:
diff changeset
384 // only some of these types are implemented so far:
kono
parents:
diff changeset
385 m_ctxt->add_error (NULL,
kono
parents:
diff changeset
386 "unimplemented primitive type for builtin: %d", type_id);
kono
parents:
diff changeset
387 return NULL;
kono
parents:
diff changeset
388
kono
parents:
diff changeset
389 case BT_VOID: return m_ctxt->get_type (GCC_JIT_TYPE_VOID);
kono
parents:
diff changeset
390 case BT_BOOL: return m_ctxt->get_type (GCC_JIT_TYPE_BOOL);
kono
parents:
diff changeset
391 case BT_INT: return m_ctxt->get_type (GCC_JIT_TYPE_INT);
kono
parents:
diff changeset
392 case BT_UINT: return m_ctxt->get_type (GCC_JIT_TYPE_UNSIGNED_INT);
kono
parents:
diff changeset
393 case BT_LONG: return m_ctxt->get_type (GCC_JIT_TYPE_LONG);
kono
parents:
diff changeset
394 case BT_ULONG: return m_ctxt->get_type (GCC_JIT_TYPE_UNSIGNED_LONG);
kono
parents:
diff changeset
395 case BT_LONGLONG: return m_ctxt->get_type (GCC_JIT_TYPE_LONG_LONG);
kono
parents:
diff changeset
396 case BT_ULONGLONG:
kono
parents:
diff changeset
397 return m_ctxt->get_type (GCC_JIT_TYPE_UNSIGNED_LONG_LONG);
kono
parents:
diff changeset
398 // case BT_INT128:
kono
parents:
diff changeset
399 // case BT_UINT128:
kono
parents:
diff changeset
400 // case BT_INTMAX:
kono
parents:
diff changeset
401 // case BT_UINTMAX:
kono
parents:
diff changeset
402 case BT_UINT16: return m_ctxt->get_int_type (2, false);
kono
parents:
diff changeset
403 case BT_UINT32: return m_ctxt->get_int_type (4, false);
kono
parents:
diff changeset
404 case BT_UINT64: return m_ctxt->get_int_type (8, false);
kono
parents:
diff changeset
405 // case BT_WORD:
kono
parents:
diff changeset
406 // case BT_UNWINDWORD:
kono
parents:
diff changeset
407 case BT_FLOAT: return m_ctxt->get_type (GCC_JIT_TYPE_FLOAT);
kono
parents:
diff changeset
408 case BT_DOUBLE: return m_ctxt->get_type (GCC_JIT_TYPE_DOUBLE);
kono
parents:
diff changeset
409 case BT_LONGDOUBLE: return m_ctxt->get_type (GCC_JIT_TYPE_LONG_DOUBLE);
kono
parents:
diff changeset
410 case BT_COMPLEX_FLOAT:
kono
parents:
diff changeset
411 return m_ctxt->get_type (GCC_JIT_TYPE_COMPLEX_FLOAT);
kono
parents:
diff changeset
412 case BT_COMPLEX_DOUBLE:
kono
parents:
diff changeset
413 return m_ctxt->get_type (GCC_JIT_TYPE_COMPLEX_DOUBLE);
kono
parents:
diff changeset
414 case BT_COMPLEX_LONGDOUBLE:
kono
parents:
diff changeset
415 return m_ctxt->get_type (GCC_JIT_TYPE_COMPLEX_LONG_DOUBLE);
kono
parents:
diff changeset
416 case BT_PTR: return m_ctxt->get_type (GCC_JIT_TYPE_VOID_PTR);
kono
parents:
diff changeset
417 case BT_FILEPTR: return m_ctxt->get_type (GCC_JIT_TYPE_FILE_PTR);
kono
parents:
diff changeset
418 // case BT_CONST:
kono
parents:
diff changeset
419 // case BT_VOLATILE_PTR:
kono
parents:
diff changeset
420 // case BT_CONST_VOLATILE_PTR:
kono
parents:
diff changeset
421 // case BT_PTRMODE:
kono
parents:
diff changeset
422 // case BT_INT_PTR:
kono
parents:
diff changeset
423 // case BT_FLOAT_PTR:
kono
parents:
diff changeset
424 case BT_DOUBLE_PTR:
kono
parents:
diff changeset
425 return m_ctxt->get_type (GCC_JIT_TYPE_DOUBLE)->get_pointer ();
kono
parents:
diff changeset
426 // case BT_CONST_DOUBLE_PTR:
kono
parents:
diff changeset
427 // case BT_LONGDOUBLE_PTR:
kono
parents:
diff changeset
428 // case BT_PID:
kono
parents:
diff changeset
429 // case BT_SIZE:
kono
parents:
diff changeset
430 // case BT_SSIZE:
kono
parents:
diff changeset
431 // case BT_WINT:
kono
parents:
diff changeset
432 // case BT_STRING:
kono
parents:
diff changeset
433 case BT_CONST_STRING: return m_ctxt->get_type (GCC_JIT_TYPE_CONST_CHAR_PTR);
kono
parents:
diff changeset
434 // case BT_DFLOAT32:
kono
parents:
diff changeset
435 // case BT_DFLOAT64:
kono
parents:
diff changeset
436 // case BT_DFLOAT128:
kono
parents:
diff changeset
437 // case BT_DFLOAT32_PTR:
kono
parents:
diff changeset
438 // case BT_DFLOAT64_PTR:
kono
parents:
diff changeset
439 // case BT_DFLOAT128_PTR:
kono
parents:
diff changeset
440 // case BT_VALIST_REF:
kono
parents:
diff changeset
441 // case BT_VALIST_ARG:
kono
parents:
diff changeset
442 // case BT_I1:
kono
parents:
diff changeset
443 // case BT_I2:
kono
parents:
diff changeset
444 // case BT_I4:
kono
parents:
diff changeset
445 // case BT_I8:
kono
parents:
diff changeset
446 // case BT_I16:
kono
parents:
diff changeset
447 }
kono
parents:
diff changeset
448 }
kono
parents:
diff changeset
449
kono
parents:
diff changeset
450 /* Create the recording::function_type for a given function type
kono
parents:
diff changeset
451 signature. */
kono
parents:
diff changeset
452
kono
parents:
diff changeset
453 recording::function_type *
kono
parents:
diff changeset
454 builtins_manager::make_fn_type (enum jit_builtin_type,
kono
parents:
diff changeset
455 enum jit_builtin_type return_type_id,
kono
parents:
diff changeset
456 bool is_variadic,
kono
parents:
diff changeset
457 int num_args, ...)
kono
parents:
diff changeset
458 {
kono
parents:
diff changeset
459 va_list list;
kono
parents:
diff changeset
460 int i;
kono
parents:
diff changeset
461 recording::type **param_types = new recording::type *[num_args];
kono
parents:
diff changeset
462 recording::type *return_type = NULL;
kono
parents:
diff changeset
463 recording::function_type *result = NULL;
kono
parents:
diff changeset
464
kono
parents:
diff changeset
465 va_start (list, num_args);
kono
parents:
diff changeset
466 for (i = 0; i < num_args; ++i)
kono
parents:
diff changeset
467 {
kono
parents:
diff changeset
468 enum jit_builtin_type arg_type_id =
kono
parents:
diff changeset
469 (enum jit_builtin_type) va_arg (list, int);
kono
parents:
diff changeset
470 param_types[i] = get_type (arg_type_id);
kono
parents:
diff changeset
471 if (!param_types[i])
kono
parents:
diff changeset
472 goto error;
kono
parents:
diff changeset
473 }
kono
parents:
diff changeset
474 va_end (list);
kono
parents:
diff changeset
475
kono
parents:
diff changeset
476 return_type = get_type (return_type_id);
kono
parents:
diff changeset
477 if (!return_type)
kono
parents:
diff changeset
478 goto error;
kono
parents:
diff changeset
479
kono
parents:
diff changeset
480 result = m_ctxt->new_function_type (return_type,
kono
parents:
diff changeset
481 num_args,
kono
parents:
diff changeset
482 param_types,
kono
parents:
diff changeset
483 is_variadic);
kono
parents:
diff changeset
484
kono
parents:
diff changeset
485 error:
kono
parents:
diff changeset
486 delete[] param_types;
kono
parents:
diff changeset
487 return result;
kono
parents:
diff changeset
488 }
kono
parents:
diff changeset
489
kono
parents:
diff changeset
490 /* Handler for DEF_POINTER_TYPE within builtins_manager::make_type. */
kono
parents:
diff changeset
491
kono
parents:
diff changeset
492 recording::type *
kono
parents:
diff changeset
493 builtins_manager::make_ptr_type (enum jit_builtin_type,
kono
parents:
diff changeset
494 enum jit_builtin_type other_type_id)
kono
parents:
diff changeset
495 {
kono
parents:
diff changeset
496 recording::type *base_type = get_type (other_type_id);
kono
parents:
diff changeset
497 return base_type->get_pointer ();
kono
parents:
diff changeset
498 }
kono
parents:
diff changeset
499
kono
parents:
diff changeset
500 /* Playback support. */
kono
parents:
diff changeset
501
kono
parents:
diff changeset
502 /* A builtins_manager is associated with a recording::context
kono
parents:
diff changeset
503 and might be reused for multiple compiles on various
kono
parents:
diff changeset
504 playback::contexts, perhaps with different options.
kono
parents:
diff changeset
505
kono
parents:
diff changeset
506 Purge any playback state. Currently this is just the table of
kono
parents:
diff changeset
507 attributes. */
kono
parents:
diff changeset
508
kono
parents:
diff changeset
509 void
kono
parents:
diff changeset
510 builtins_manager::finish_playback (void)
kono
parents:
diff changeset
511 {
kono
parents:
diff changeset
512 memset (m_attributes, 0, sizeof (m_attributes));
kono
parents:
diff changeset
513 }
kono
parents:
diff changeset
514
kono
parents:
diff changeset
515 /* Get the enum built_in_class for BUILTIN_ID. */
kono
parents:
diff changeset
516
kono
parents:
diff changeset
517 enum built_in_class
kono
parents:
diff changeset
518 builtins_manager::get_class (enum built_in_function builtin_id)
kono
parents:
diff changeset
519 {
kono
parents:
diff changeset
520 return builtin_data[builtin_id].fnclass;
kono
parents:
diff changeset
521 }
kono
parents:
diff changeset
522
kono
parents:
diff changeset
523 /* Is BUILTIN_ID implicit? */
kono
parents:
diff changeset
524
kono
parents:
diff changeset
525 bool
kono
parents:
diff changeset
526 builtins_manager::implicit_p (enum built_in_function builtin_id)
kono
parents:
diff changeset
527 {
kono
parents:
diff changeset
528 return builtin_data[builtin_id].implicit_p;
kono
parents:
diff changeset
529 }
kono
parents:
diff changeset
530
kono
parents:
diff changeset
531 /* Get any attributes (in tree form) for the function declaration
kono
parents:
diff changeset
532 for BUILTIN_ID.
kono
parents:
diff changeset
533
kono
parents:
diff changeset
534 These are created on-demand, and cached within the m_attributes
kono
parents:
diff changeset
535 array, until finish_playback. */
kono
parents:
diff changeset
536
kono
parents:
diff changeset
537 tree
kono
parents:
diff changeset
538 builtins_manager::get_attrs_tree (enum built_in_function builtin_id)
kono
parents:
diff changeset
539 {
kono
parents:
diff changeset
540 enum built_in_attribute attr = builtin_data[builtin_id].attr;
kono
parents:
diff changeset
541 return get_attrs_tree (attr);
kono
parents:
diff changeset
542 }
kono
parents:
diff changeset
543
kono
parents:
diff changeset
544 /* As above, but for an enum built_in_attribute. */
kono
parents:
diff changeset
545
kono
parents:
diff changeset
546 tree
kono
parents:
diff changeset
547 builtins_manager::get_attrs_tree (enum built_in_attribute attr)
kono
parents:
diff changeset
548 {
kono
parents:
diff changeset
549 gcc_assert (attr < ATTR_LAST);
kono
parents:
diff changeset
550 if (!m_attributes [attr])
kono
parents:
diff changeset
551 m_attributes [attr] = make_attrs_tree (attr);
kono
parents:
diff changeset
552 return m_attributes [attr];
kono
parents:
diff changeset
553 }
kono
parents:
diff changeset
554
kono
parents:
diff changeset
555 /* Handle a cache-miss within the m_attributes array by
kono
parents:
diff changeset
556 generating the attributes for enum built_in_attribute
kono
parents:
diff changeset
557 in tree form. */
kono
parents:
diff changeset
558
kono
parents:
diff changeset
559 tree
kono
parents:
diff changeset
560 builtins_manager::make_attrs_tree (enum built_in_attribute attr)
kono
parents:
diff changeset
561 {
kono
parents:
diff changeset
562 switch (attr)
kono
parents:
diff changeset
563 {
kono
parents:
diff changeset
564 /* Generate cases from builtin-attrs.def. */
kono
parents:
diff changeset
565 #define DEF_ATTR_NULL_TREE(ENUM) \
kono
parents:
diff changeset
566 case ENUM: return NULL_TREE;
kono
parents:
diff changeset
567 #define DEF_ATTR_INT(ENUM, VALUE) \
kono
parents:
diff changeset
568 case ENUM: return build_int_cst (integer_type_node, VALUE);
kono
parents:
diff changeset
569 #define DEF_ATTR_STRING(ENUM, VALUE) \
kono
parents:
diff changeset
570 case ENUM: return build_string (strlen (VALUE), VALUE);
kono
parents:
diff changeset
571 #define DEF_ATTR_IDENT(ENUM, STRING) \
kono
parents:
diff changeset
572 case ENUM: return get_identifier (STRING);
kono
parents:
diff changeset
573 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) \
kono
parents:
diff changeset
574 case ENUM: return tree_cons (get_attrs_tree (PURPOSE), \
kono
parents:
diff changeset
575 get_attrs_tree (VALUE), \
kono
parents:
diff changeset
576 get_attrs_tree (CHAIN));
kono
parents:
diff changeset
577 #include "builtin-attrs.def"
kono
parents:
diff changeset
578 #undef DEF_ATTR_NULL_TREE
kono
parents:
diff changeset
579 #undef DEF_ATTR_INT
kono
parents:
diff changeset
580 #undef DEF_ATTR_IDENT
kono
parents:
diff changeset
581 #undef DEF_ATTR_TREE_LIST
kono
parents:
diff changeset
582
kono
parents:
diff changeset
583 default:
kono
parents:
diff changeset
584 /* We somehow got a value not covered by the autogenerated
kono
parents:
diff changeset
585 cases. */
kono
parents:
diff changeset
586 gcc_unreachable ();
kono
parents:
diff changeset
587 return NULL;
kono
parents:
diff changeset
588 }
kono
parents:
diff changeset
589 }
kono
parents:
diff changeset
590
kono
parents:
diff changeset
591 } // namespace jit
kono
parents:
diff changeset
592 } // namespace gcc