Branch data Line data Source code
1 : : /* jit-builtins.cc -- Handling of builtin functions during JIT-compilation.
2 : : Copyright (C) 2014-2024 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 "target.h"
24 : : #include "jit-playback.h"
25 : : #include "stringpool.h"
26 : :
27 : : #include "jit-builtins.h"
28 : :
29 : : namespace gcc {
30 : :
31 : : namespace jit {
32 : :
33 : : const char *const prefix = "__builtin_";
34 : : const size_t prefix_len = strlen (prefix);
35 : :
36 : : /* Create "builtin_data", a const table of the data within builtins.def. */
37 : : struct builtin_data
38 : : {
39 : : const char *name;
40 : : enum built_in_class fnclass;
41 : : enum jit_builtin_type type;
42 : : bool both_p;
43 : : bool fallback_p;
44 : : enum built_in_attribute attr;
45 : : bool implicit_p;
46 : :
47 : 4912 : const char *get_asm_name () const
48 : : {
49 : 105 : if (both_p && fallback_p)
50 : 105 : return name + prefix_len;
51 : : else
52 : 4807 : return name;
53 : : }
54 : : };
55 : :
56 : : #define DEF_BUILTIN(X, NAME, CLASS, TYPE, LT, BOTH_P, FALLBACK_P, \
57 : : NONANSI_P, ATTRS, IMPLICIT, COND) \
58 : : {NAME, CLASS, TYPE, BOTH_P, FALLBACK_P, ATTRS, IMPLICIT},
59 : : static const struct builtin_data builtin_data[] =
60 : : {
61 : : #include "builtins.def"
62 : : };
63 : :
64 : : /* Helper function for find_builtin_by_name. */
65 : :
66 : : static bool
67 : 248272 : matches_builtin (const char *in_name,
68 : : const struct builtin_data& bd)
69 : : {
70 : 248272 : const bool debug = 0;
71 : :
72 : : /* Ignore entries with a NULL name. */
73 : 248272 : if (!bd.name)
74 : : return false;
75 : :
76 : 248252 : if (debug)
77 : : fprintf (stderr, "seen builtin: %s\n", bd.name);
78 : :
79 : 248252 : if (strcmp (bd.name, in_name) == 0)
80 : : return true;
81 : :
82 : 248062 : if (bd.both_p)
83 : : {
84 : : /* Then the macros in builtins.def gave a "__builtin_"
85 : : prefix to bd.name, but we should also recognize the form
86 : : without the prefix. */
87 : 92394 : gcc_assert (strncmp (bd.name, prefix, prefix_len) == 0);
88 : 92394 : if (debug)
89 : : fprintf (stderr, "testing without prefix as: %s\n",
90 : : bd.name + prefix_len);
91 : 92394 : if (strcmp (bd.name + prefix_len, in_name) == 0)
92 : 60 : return true;
93 : : }
94 : :
95 : : return false;
96 : : }
97 : :
98 : : /* Locate the built-in function that matches name IN_NAME,
99 : : writing the result to OUT_ID and returning true if found,
100 : : or returning false if not found. */
101 : :
102 : : static bool
103 : 255 : find_builtin_by_name (const char *in_name,
104 : : enum built_in_function *out_id)
105 : : {
106 : : /* Locate builtin. This currently works by performing repeated
107 : : strcmp against every possible candidate, which is likely to
108 : : inefficient.
109 : :
110 : : We start at index 1 to skip the initial entry (BUILT_IN_NONE), which
111 : : has a NULL name. */
112 : 248237 : for (unsigned int i = 1; i < ARRAY_SIZE (builtin_data); i++)
113 : : {
114 : 248232 : const struct builtin_data& bd = builtin_data[i];
115 : 248232 : if (matches_builtin (in_name, bd))
116 : : {
117 : : /* Found a match. */
118 : 250 : *out_id = static_cast<enum built_in_function> (i);
119 : 250 : return true;
120 : : }
121 : : }
122 : :
123 : : /* Not found. */
124 : : return false;
125 : : }
126 : :
127 : : // class builtins_manager
128 : :
129 : : /* Constructor for gcc::jit::builtins_manager. */
130 : :
131 : 1178 : builtins_manager::builtins_manager (recording::context *ctxt)
132 : 1178 : : m_ctxt (ctxt)
133 : : {
134 : 1178 : memset (m_types, 0, sizeof (m_types));
135 : 1178 : memset (m_builtin_functions, 0, sizeof (m_builtin_functions));
136 : 1178 : memset (m_attributes, 0, sizeof (m_attributes));
137 : 1178 : }
138 : :
139 : : /* Locate a builtin function by name.
140 : : Create a recording::function of the appropriate type, reusing them
141 : : if they've already been seen. */
142 : :
143 : : recording::function *
144 : 255 : builtins_manager::get_builtin_function (const char *name)
145 : : {
146 : 255 : enum built_in_function builtin_id;
147 : 255 : if (!find_builtin_by_name (name, &builtin_id))
148 : : {
149 : 5 : m_ctxt->add_error (NULL, "builtin \"%s\" not found", name);
150 : 5 : return NULL;
151 : : }
152 : :
153 : 250 : return get_builtin_function_by_id (builtin_id);
154 : : }
155 : :
156 : : /* Locate a builtin function by id.
157 : : Create a recording::function of the appropriate type, reusing them
158 : : if they've already been seen. */
159 : :
160 : : recording::function *
161 : 5027 : builtins_manager::get_builtin_function_by_id (enum built_in_function builtin_id)
162 : : {
163 : 5027 : gcc_assert (builtin_id > BUILT_IN_NONE);
164 : 5027 : gcc_assert (builtin_id < END_BUILTINS);
165 : :
166 : : /* Lazily build the functions, caching them so that repeated calls for
167 : : the same id on a context give back the same object. */
168 : 5027 : if (!m_builtin_functions[builtin_id])
169 : : {
170 : 4917 : recording::function *fn = make_builtin_function (builtin_id);
171 : 4917 : if (fn)
172 : : {
173 : 4912 : m_builtin_functions[builtin_id] = fn;
174 : 4912 : m_ctxt->record (fn);
175 : : }
176 : : }
177 : :
178 : 5027 : return m_builtin_functions[builtin_id];
179 : : }
180 : :
181 : : /* Create the recording::function for a given builtin function, by ID. */
182 : :
183 : : recording::function *
184 : 4917 : builtins_manager::make_builtin_function (enum built_in_function builtin_id)
185 : : {
186 : 4917 : const struct builtin_data& bd = builtin_data[builtin_id];
187 : 4917 : enum jit_builtin_type type_id = bd.type;
188 : 4917 : recording::type *t = get_type (type_id);
189 : 4917 : if (!t)
190 : : return NULL;
191 : 4912 : recording::function_type *func_type = t->as_a_function_type ();
192 : 4912 : if (!func_type)
193 : : return NULL;
194 : :
195 : 4912 : vec<recording::type *> param_types = func_type->get_param_types ();
196 : 4912 : recording::param **params = new recording::param *[param_types.length ()];
197 : :
198 : 4912 : int i;
199 : 4912 : recording::type *param_type;
200 : 8841 : FOR_EACH_VEC_ELT (param_types, i, param_type)
201 : : {
202 : 3929 : char buf[16];
203 : 3929 : snprintf (buf, 16, "arg%d", i);
204 : 3929 : params[i] = m_ctxt->new_param (NULL,
205 : : param_type,
206 : : buf);
207 : : }
208 : 4912 : const char *asm_name = bd.get_asm_name ();
209 : 4912 : recording::function *result =
210 : : new recording::function (m_ctxt,
211 : : NULL,
212 : : GCC_JIT_FUNCTION_IMPORTED, // FIXME
213 : : func_type->get_return_type (),
214 : 4912 : m_ctxt->new_string (asm_name),
215 : 4912 : param_types.length (),
216 : : params,
217 : : func_type->is_variadic (),
218 : 8606 : builtin_id);
219 : 4912 : delete[] params;
220 : :
221 : : /* PR/64020 - If the client code is using builtin cos or sin,
222 : : tree-ssa-math-opt.c's execute_cse_sincos_1 may attempt
223 : : to optimize them to use __builtin_cexpi; for this,
224 : : BUILT_IN_CEXPI needs to exist.
225 : :
226 : : Hence query the cache for BUILT_IN_CEXPI to ensure it gets
227 : : built. */
228 : 4912 : if (builtin_id == BUILT_IN_COS || builtin_id == BUILT_IN_SIN)
229 : 40 : (void)get_builtin_function_by_id (BUILT_IN_CEXPI);
230 : :
231 : : /* builtins.cc:expand_builtin_cexpi can optimize the various
232 : : CEXP builtins to SINCOS builtins, and hence we may require
233 : : SINCOS builtins latter.
234 : :
235 : : Ensure the appropriate SINCOS builtin exists. */
236 : 4912 : if (builtin_id == BUILT_IN_CEXPIF)
237 : 0 : (void)get_builtin_function_by_id (BUILT_IN_SINCOSF);
238 : 4912 : else if (builtin_id == BUILT_IN_CEXPI)
239 : 25 : (void)get_builtin_function_by_id (BUILT_IN_SINCOS);
240 : 4887 : else if (builtin_id == BUILT_IN_CEXPIL)
241 : 0 : (void)get_builtin_function_by_id (BUILT_IN_SINCOSL);
242 : :
243 : : return result;
244 : : }
245 : :
246 : : /* Build an array of type names for use by get_string_for_type_id. */
247 : :
248 : : static const char * const type_names[] = {
249 : : #define DEF_PRIMITIVE_TYPE(ENUM, VALUE) #ENUM,
250 : : #define DEF_FUNCTION_TYPE_0(ENUM, RETURN) #ENUM,
251 : : #define DEF_FUNCTION_TYPE_1(ENUM, RETURN, ARG1) #ENUM,
252 : : #define DEF_FUNCTION_TYPE_2(ENUM, RETURN, ARG1, ARG2) #ENUM,
253 : : #define DEF_FUNCTION_TYPE_3(ENUM, RETURN, ARG1, ARG2, ARG3) #ENUM,
254 : : #define DEF_FUNCTION_TYPE_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) #ENUM,
255 : : #define DEF_FUNCTION_TYPE_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) #ENUM,
256 : : #define DEF_FUNCTION_TYPE_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
257 : : ARG6) \
258 : : #ENUM,
259 : : #define DEF_FUNCTION_TYPE_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
260 : : ARG6, ARG7) \
261 : : #ENUM,
262 : : #define DEF_FUNCTION_TYPE_8(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
263 : : ARG6, ARG7, ARG8) \
264 : : #ENUM,
265 : : #define DEF_FUNCTION_TYPE_9(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
266 : : ARG6, ARG7, ARG8, ARG9) \
267 : : #ENUM,
268 : : #define DEF_FUNCTION_TYPE_10(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
269 : : ARG6, ARG7, ARG8, ARG9, ARG10) \
270 : : #ENUM,
271 : : #define DEF_FUNCTION_TYPE_11(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
272 : : ARG6, ARG7, ARG8, ARG9, ARG10, ARG11) \
273 : : #ENUM,
274 : : #define DEF_FUNCTION_TYPE_VAR_0(ENUM, RETURN) #ENUM,
275 : : #define DEF_FUNCTION_TYPE_VAR_1(ENUM, RETURN, ARG1) #ENUM,
276 : : #define DEF_FUNCTION_TYPE_VAR_2(ENUM, RETURN, ARG1, ARG2) #ENUM,
277 : : #define DEF_FUNCTION_TYPE_VAR_3(ENUM, RETURN, ARG1, ARG2, ARG3) #ENUM,
278 : : #define DEF_FUNCTION_TYPE_VAR_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) #ENUM,
279 : : #define DEF_FUNCTION_TYPE_VAR_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
280 : : #ENUM,
281 : : #define DEF_FUNCTION_TYPE_VAR_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
282 : : ARG6) \
283 : : #ENUM,
284 : : #define DEF_FUNCTION_TYPE_VAR_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
285 : : ARG6, ARG7) \
286 : : #ENUM,
287 : : #define DEF_POINTER_TYPE(ENUM, TYPE) #ENUM,
288 : :
289 : : #include "builtin-types.def"
290 : :
291 : : #undef DEF_PRIMITIVE_TYPE
292 : : #undef DEF_FUNCTION_TYPE_0
293 : : #undef DEF_FUNCTION_TYPE_1
294 : : #undef DEF_FUNCTION_TYPE_2
295 : : #undef DEF_FUNCTION_TYPE_3
296 : : #undef DEF_FUNCTION_TYPE_4
297 : : #undef DEF_FUNCTION_TYPE_5
298 : : #undef DEF_FUNCTION_TYPE_6
299 : : #undef DEF_FUNCTION_TYPE_7
300 : : #undef DEF_FUNCTION_TYPE_8
301 : : #undef DEF_FUNCTION_TYPE_9
302 : : #undef DEF_FUNCTION_TYPE_10
303 : : #undef DEF_FUNCTION_TYPE_11
304 : : #undef DEF_FUNCTION_TYPE_VAR_0
305 : : #undef DEF_FUNCTION_TYPE_VAR_1
306 : : #undef DEF_FUNCTION_TYPE_VAR_2
307 : : #undef DEF_FUNCTION_TYPE_VAR_3
308 : : #undef DEF_FUNCTION_TYPE_VAR_4
309 : : #undef DEF_FUNCTION_TYPE_VAR_5
310 : : #undef DEF_FUNCTION_TYPE_VAR_6
311 : : #undef DEF_FUNCTION_TYPE_VAR_7
312 : : #undef DEF_POINTER_TYPE
313 : : };
314 : :
315 : : /* Get a string for TYPE_ID suitable for use in logs and error messages
316 : : (e.g. "BT_PID"). */
317 : :
318 : : static const char *
319 : 5 : get_string_for_type_id (enum jit_builtin_type type_id)
320 : : {
321 : 5 : gcc_assert (type_id < ARRAY_SIZE (type_names));
322 : 5 : return type_names[type_id];
323 : : }
324 : :
325 : : /* Get the recording::type for a given type of builtin function,
326 : : by ID, creating it if it doesn't already exist. */
327 : :
328 : : recording::type *
329 : 13698 : builtins_manager::get_type (enum jit_builtin_type type_id)
330 : : {
331 : 13698 : if (!m_types[type_id])
332 : 10987 : m_types[type_id] = make_type (type_id);
333 : 13698 : return m_types[type_id];
334 : : }
335 : :
336 : : /* Create the recording::type for a given type of builtin function. */
337 : :
338 : : recording::type *
339 : 10987 : builtins_manager::make_type (enum jit_builtin_type type_id)
340 : : {
341 : : /* Use builtin-types.def to construct a switch statement, with each
342 : : case deferring to one of the methods below:
343 : : - DEF_PRIMITIVE_TYPE is handled as a call to make_primitive_type.
344 : : - the various DEF_FUNCTION_TYPE_n are handled by variadic calls
345 : : to make_fn_type.
346 : : - similarly for DEF_FUNCTION_TYPE_VAR_n, but setting the
347 : : "is_variadic" argument.
348 : : - DEF_POINTER_TYPE is handled by make_ptr_type.
349 : : That should handle everything, but just in case we also suppy a
350 : : gcc_unreachable default clause. */
351 : 10987 : switch (type_id)
352 : : {
353 : : #define DEF_PRIMITIVE_TYPE(ENUM, VALUE) \
354 : : case ENUM: return make_primitive_type (ENUM);
355 : : #define DEF_FUNCTION_TYPE_0(ENUM, RETURN) \
356 : : case ENUM: return make_fn_type (ENUM, RETURN, 0, 0);
357 : : #define DEF_FUNCTION_TYPE_1(ENUM, RETURN, ARG1) \
358 : : case ENUM: return make_fn_type (ENUM, RETURN, 0, 1, ARG1);
359 : : #define DEF_FUNCTION_TYPE_2(ENUM, RETURN, ARG1, ARG2) \
360 : : case ENUM: return make_fn_type (ENUM, RETURN, 0, 2, ARG1, ARG2);
361 : : #define DEF_FUNCTION_TYPE_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
362 : : case ENUM: return make_fn_type (ENUM, RETURN, 0, 3, ARG1, ARG2, ARG3);
363 : : #define DEF_FUNCTION_TYPE_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
364 : : case ENUM: return make_fn_type (ENUM, RETURN, 0, 4, ARG1, ARG2, ARG3, \
365 : : ARG4);
366 : : #define DEF_FUNCTION_TYPE_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
367 : : case ENUM: return make_fn_type (ENUM, RETURN, 0, 5, ARG1, ARG2, ARG3, \
368 : : ARG4, ARG5);
369 : : #define DEF_FUNCTION_TYPE_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
370 : : ARG6) \
371 : : case ENUM: return make_fn_type (ENUM, RETURN, 0, 6, ARG1, ARG2, ARG3, \
372 : : ARG4, ARG5, ARG6);
373 : : #define DEF_FUNCTION_TYPE_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
374 : : ARG6, ARG7) \
375 : : case ENUM: return make_fn_type (ENUM, RETURN, 0, 7, ARG1, ARG2, ARG3, \
376 : : ARG4, ARG5, ARG6, ARG7);
377 : : #define DEF_FUNCTION_TYPE_8(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
378 : : ARG6, ARG7, ARG8) \
379 : : case ENUM: return make_fn_type (ENUM, RETURN, 0, 8, ARG1, ARG2, ARG3, \
380 : : ARG4, ARG5, ARG6, ARG7, ARG8);
381 : : #define DEF_FUNCTION_TYPE_9(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
382 : : ARG6, ARG7, ARG8, ARG9) \
383 : : case ENUM: return make_fn_type (ENUM, RETURN, 0, 9, ARG1, ARG2, ARG3, \
384 : : ARG4, ARG5, ARG6, ARG7, ARG8, ARG9);
385 : : #define DEF_FUNCTION_TYPE_10(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
386 : : ARG6, ARG7, ARG8, ARG9, ARG10) \
387 : : case ENUM: return make_fn_type (ENUM, RETURN, 0, 10, ARG1, ARG2, ARG3, \
388 : : ARG4, ARG5, ARG6, ARG7, ARG8, ARG9, \
389 : : ARG10);
390 : : #define DEF_FUNCTION_TYPE_11(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
391 : : ARG6, ARG7, ARG8, ARG9, ARG10, ARG11) \
392 : : case ENUM: return make_fn_type (ENUM, RETURN, 0, 11, ARG1, ARG2, ARG3, \
393 : : ARG4, ARG5, ARG6, ARG7, ARG8, ARG9, \
394 : : ARG10, ARG11);
395 : : #define DEF_FUNCTION_TYPE_VAR_0(ENUM, RETURN) \
396 : : case ENUM: return make_fn_type (ENUM, RETURN, 1, 0);
397 : : #define DEF_FUNCTION_TYPE_VAR_1(ENUM, RETURN, ARG1) \
398 : : case ENUM: return make_fn_type (ENUM, RETURN, 1, 1, ARG1);
399 : : #define DEF_FUNCTION_TYPE_VAR_2(ENUM, RETURN, ARG1, ARG2) \
400 : : case ENUM: return make_fn_type (ENUM, RETURN, 1, 2, ARG1, ARG2);
401 : : #define DEF_FUNCTION_TYPE_VAR_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
402 : : case ENUM: return make_fn_type (ENUM, RETURN, 1, 3, ARG1, ARG2, ARG3);
403 : : #define DEF_FUNCTION_TYPE_VAR_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
404 : : case ENUM: return make_fn_type (ENUM, RETURN, 1, 4, ARG1, ARG2, ARG3, \
405 : : ARG4);
406 : : #define DEF_FUNCTION_TYPE_VAR_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
407 : : case ENUM: return make_fn_type (ENUM, RETURN, 1, 5, ARG1, ARG2, ARG3, \
408 : : ARG4, ARG5);
409 : : #define DEF_FUNCTION_TYPE_VAR_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
410 : : ARG6) \
411 : : case ENUM: return make_fn_type (ENUM, RETURN, 1, 6, ARG1, ARG2, ARG3, \
412 : : ARG4, ARG5, ARG6);
413 : : #define DEF_FUNCTION_TYPE_VAR_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
414 : : ARG6, ARG7) \
415 : : case ENUM: return make_fn_type (ENUM, RETURN, 1, 7, ARG1, ARG2, ARG3, \
416 : : ARG4, ARG5, ARG6, ARG7);
417 : : #define DEF_POINTER_TYPE(ENUM, TYPE) \
418 : : case ENUM: return make_ptr_type (ENUM, TYPE);
419 : :
420 : : #include "builtin-types.def"
421 : :
422 : : #undef DEF_PRIMITIVE_TYPE
423 : : #undef DEF_FUNCTION_TYPE_0
424 : : #undef DEF_FUNCTION_TYPE_1
425 : : #undef DEF_FUNCTION_TYPE_2
426 : : #undef DEF_FUNCTION_TYPE_3
427 : : #undef DEF_FUNCTION_TYPE_4
428 : : #undef DEF_FUNCTION_TYPE_5
429 : : #undef DEF_FUNCTION_TYPE_6
430 : : #undef DEF_FUNCTION_TYPE_7
431 : : #undef DEF_FUNCTION_TYPE_8
432 : : #undef DEF_FUNCTION_TYPE_9
433 : : #undef DEF_FUNCTION_TYPE_10
434 : : #undef DEF_FUNCTION_TYPE_11
435 : : #undef DEF_FUNCTION_TYPE_VAR_0
436 : : #undef DEF_FUNCTION_TYPE_VAR_1
437 : : #undef DEF_FUNCTION_TYPE_VAR_2
438 : : #undef DEF_FUNCTION_TYPE_VAR_3
439 : : #undef DEF_FUNCTION_TYPE_VAR_4
440 : : #undef DEF_FUNCTION_TYPE_VAR_5
441 : : #undef DEF_FUNCTION_TYPE_VAR_6
442 : : #undef DEF_FUNCTION_TYPE_VAR_7
443 : : #undef DEF_POINTER_TYPE
444 : :
445 : 0 : default:
446 : 0 : gcc_unreachable ();
447 : : }
448 : : }
449 : :
450 : : /* Create the recording::type for a given primitive type within the
451 : : builtin system.
452 : :
453 : : Only some types are currently supported. */
454 : :
455 : : recording::type*
456 : 6120 : builtins_manager::make_primitive_type (enum jit_builtin_type type_id)
457 : : {
458 : 6120 : switch (type_id)
459 : : {
460 : 5 : default:
461 : : // only some of these types are implemented so far:
462 : 5 : m_ctxt->add_error (NULL,
463 : : "unimplemented primitive type for builtin (type: %s)",
464 : : get_string_for_type_id (type_id));
465 : 5 : return NULL;
466 : :
467 : 1168 : case BT_VOID: return m_ctxt->get_type (GCC_JIT_TYPE_VOID);
468 : 25 : case BT_BOOL: return m_ctxt->get_type (GCC_JIT_TYPE_BOOL);
469 : 1168 : case BT_INT: return m_ctxt->get_type (GCC_JIT_TYPE_INT);
470 : 1168 : case BT_UINT: return m_ctxt->get_type (GCC_JIT_TYPE_UNSIGNED_INT);
471 : 0 : case BT_LONG: return m_ctxt->get_type (GCC_JIT_TYPE_LONG);
472 : 1168 : case BT_ULONG: return m_ctxt->get_type (GCC_JIT_TYPE_UNSIGNED_LONG);
473 : 0 : case BT_LONGLONG: return m_ctxt->get_type (GCC_JIT_TYPE_LONG_LONG);
474 : 1168 : case BT_ULONGLONG:
475 : 1168 : return m_ctxt->get_type (GCC_JIT_TYPE_UNSIGNED_LONG_LONG);
476 : : // case BT_INTMAX:
477 : : // case BT_UINTMAX:
478 : 0 : case BT_INT8: return m_ctxt->get_int_type (1, true);
479 : 0 : case BT_INT16: return m_ctxt->get_int_type (2, true);
480 : 0 : case BT_UINT8: return m_ctxt->get_int_type (1, false);
481 : 0 : case BT_UINT16: return m_ctxt->get_int_type (2, false);
482 : 0 : case BT_UINT32: return m_ctxt->get_int_type (4, false);
483 : 0 : case BT_UINT64: return m_ctxt->get_int_type (8, false);
484 : 0 : case BT_UINT128: return m_ctxt->get_int_type (16, false);
485 : : // case BT_WORD:
486 : : // case BT_UNWINDWORD:
487 : 0 : case BT_FLOAT: return m_ctxt->get_type (GCC_JIT_TYPE_FLOAT);
488 : 25 : case BT_DOUBLE: return m_ctxt->get_type (GCC_JIT_TYPE_DOUBLE);
489 : 0 : case BT_LONGDOUBLE: return m_ctxt->get_type (GCC_JIT_TYPE_LONG_DOUBLE);
490 : : // case BT_FLOAT16:
491 : : // case BT_FLOAT32:
492 : : // case BT_FLOAT64:
493 : : // case BT_FLOAT128:
494 : : // case BT_FLOAT32X:
495 : : // case BT_FLOAT64X:
496 : : // case BT_FLOAT128X:
497 : 0 : case BT_COMPLEX_FLOAT:
498 : 0 : return m_ctxt->get_type (GCC_JIT_TYPE_COMPLEX_FLOAT);
499 : 25 : case BT_COMPLEX_DOUBLE:
500 : 25 : return m_ctxt->get_type (GCC_JIT_TYPE_COMPLEX_DOUBLE);
501 : 0 : case BT_COMPLEX_LONGDOUBLE:
502 : 0 : return m_ctxt->get_type (GCC_JIT_TYPE_COMPLEX_LONG_DOUBLE);
503 : 25 : case BT_PTR: return m_ctxt->get_type (GCC_JIT_TYPE_VOID_PTR);
504 : 0 : case BT_FILEPTR: return m_ctxt->get_type (GCC_JIT_TYPE_FILE_PTR);
505 : : // case BT_CONST_TM_PTR:
506 : : // case BT_FENV_T_PTR:
507 : : // case BT_CONST_FENV_T_PTR:
508 : : // case BT_FEXCEPT_T_PTR:
509 : : // case BT_CONST_FEXCEPT_T_PTR:
510 : 25 : case BT_CONST_PTR:
511 : 25 : return m_ctxt->get_type (GCC_JIT_TYPE_VOID)->get_const ()->get_pointer ();
512 : 15 : case BT_VOLATILE_PTR:
513 : 15 : return (m_ctxt->get_type (GCC_JIT_TYPE_VOID)->get_volatile ()
514 : 15 : ->get_pointer ());
515 : 25 : case BT_CONST_VOLATILE_PTR:
516 : 25 : return (m_ctxt->get_type (GCC_JIT_TYPE_VOID)->get_const ()
517 : 25 : ->get_volatile ()->get_pointer ());
518 : : // case BT_PTRMODE:
519 : 15 : case BT_INT_PTR:
520 : 15 : return m_ctxt->get_type (GCC_JIT_TYPE_INT)->get_pointer ();
521 : 0 : case BT_FLOAT_PTR:
522 : 0 : return m_ctxt->get_type (GCC_JIT_TYPE_FLOAT)->get_pointer ();
523 : 25 : case BT_DOUBLE_PTR:
524 : 25 : return m_ctxt->get_type (GCC_JIT_TYPE_DOUBLE)->get_pointer ();
525 : 0 : case BT_CONST_DOUBLE_PTR:
526 : 0 : return (m_ctxt->get_type (GCC_JIT_TYPE_DOUBLE)->get_const ()
527 : 0 : ->get_pointer ());
528 : : // case BT_LONGDOUBLE_PTR:
529 : : // case BT_PID:
530 : 25 : case BT_SIZE:
531 : 25 : return m_ctxt->get_type (GCC_JIT_TYPE_SIZE_T);
532 : 0 : case BT_CONST_SIZE:
533 : 0 : return m_ctxt->get_type (GCC_JIT_TYPE_SIZE_T)->get_const ();
534 : : // case BT_SSIZE:
535 : : // case BT_WINT:
536 : : // case BT_STRING:
537 : 15 : case BT_CONST_STRING: return m_ctxt->get_type (GCC_JIT_TYPE_CONST_CHAR_PTR);
538 : : // case BT_DFLOAT32:
539 : : // case BT_DFLOAT64:
540 : : // case BT_DFLOAT128:
541 : : // case BT_VALIST_REF:
542 : : // case BT_VALIST_ARG:
543 : 0 : case BT_I1: return m_ctxt->get_int_type (1, true);
544 : 0 : case BT_I2: return m_ctxt->get_int_type (2, true);
545 : 15 : case BT_I4: return m_ctxt->get_int_type (4, true);
546 : 15 : case BT_I8: return m_ctxt->get_int_type (8, true);
547 : 0 : case BT_I16: return m_ctxt->get_int_type (16, true);
548 : : // case BT_PTR_CONST_STRING:
549 : : }
550 : : }
551 : :
552 : : /* Create the recording::function_type for a given function type
553 : : signature. */
554 : :
555 : : recording::function_type *
556 : 4867 : builtins_manager::make_fn_type (enum jit_builtin_type,
557 : : enum jit_builtin_type return_type_id,
558 : : bool is_variadic,
559 : : int num_args, ...)
560 : : {
561 : 4867 : va_list list;
562 : 4867 : int i;
563 : 4867 : recording::type **param_types = new recording::type *[num_args];
564 : 4867 : recording::type *return_type = NULL;
565 : 4867 : recording::function_type *result = NULL;
566 : :
567 : 4867 : va_start (list, num_args);
568 : 8781 : for (i = 0; i < num_args; ++i)
569 : : {
570 : 3914 : enum jit_builtin_type arg_type_id =
571 : 3914 : (enum jit_builtin_type) va_arg (list, int);
572 : 3914 : param_types[i] = get_type (arg_type_id);
573 : 3914 : if (!param_types[i])
574 : 0 : goto error;
575 : : }
576 : 4867 : va_end (list);
577 : :
578 : 4867 : return_type = get_type (return_type_id);
579 : 4867 : if (!return_type)
580 : 5 : goto error;
581 : :
582 : 4862 : result = m_ctxt->new_function_type (return_type,
583 : : num_args,
584 : : param_types,
585 : : is_variadic);
586 : :
587 : 4867 : error:
588 : 4867 : delete[] param_types;
589 : 4867 : return result;
590 : : }
591 : :
592 : : /* Handler for DEF_POINTER_TYPE within builtins_manager::make_type. */
593 : :
594 : : recording::type *
595 : 0 : builtins_manager::make_ptr_type (enum jit_builtin_type,
596 : : enum jit_builtin_type other_type_id)
597 : : {
598 : 0 : recording::type *base_type = get_type (other_type_id);
599 : 0 : return base_type->get_pointer ();
600 : : }
601 : :
602 : : /* Ensure that builtins that could be needed during optimization
603 : : get created ahead of time. */
604 : :
605 : : void
606 : 1178 : builtins_manager::ensure_optimization_builtins_exist ()
607 : : {
608 : : /* build_common_builtin_nodes does most of this, but not all.
609 : : We can't loop through all of the builtin_data array, we don't
610 : : support all types yet. */
611 : 1178 : (void)get_builtin_function_by_id (BUILT_IN_TRAP);
612 : 1178 : (void)get_builtin_function_by_id (BUILT_IN_POPCOUNT);
613 : 1178 : (void)get_builtin_function_by_id (BUILT_IN_POPCOUNTL);
614 : 1178 : (void)get_builtin_function_by_id (BUILT_IN_POPCOUNTLL);
615 : 1178 : }
616 : :
617 : : /* Playback support. */
618 : :
619 : : /* A builtins_manager is associated with a recording::context
620 : : and might be reused for multiple compiles on various
621 : : playback::contexts, perhaps with different options.
622 : :
623 : : Purge any playback state. Currently this is just the table of
624 : : attributes. */
625 : :
626 : : void
627 : 1178 : builtins_manager::finish_playback (void)
628 : : {
629 : 1178 : memset (m_attributes, 0, sizeof (m_attributes));
630 : 1178 : }
631 : :
632 : : /* Get the enum built_in_class for BUILTIN_ID. */
633 : :
634 : : enum built_in_class
635 : 4782 : builtins_manager::get_class (enum built_in_function builtin_id)
636 : : {
637 : 4782 : return builtin_data[builtin_id].fnclass;
638 : : }
639 : :
640 : : /* Is BUILTIN_ID implicit? */
641 : :
642 : : bool
643 : 4782 : builtins_manager::implicit_p (enum built_in_function builtin_id)
644 : : {
645 : 4782 : return builtin_data[builtin_id].implicit_p;
646 : : }
647 : :
648 : : /* Get any attributes (in tree form) for the function declaration
649 : : for BUILTIN_ID.
650 : :
651 : : These are created on-demand, and cached within the m_attributes
652 : : array, until finish_playback. */
653 : :
654 : : tree
655 : 4782 : builtins_manager::get_attrs_tree (enum built_in_function builtin_id)
656 : : {
657 : 4782 : enum built_in_attribute attr = builtin_data[builtin_id].attr;
658 : 4782 : return get_attrs_tree (attr);
659 : : }
660 : :
661 : : /* As above, but for an enum built_in_attribute. */
662 : :
663 : : tree
664 : 22227 : builtins_manager::get_attrs_tree (enum built_in_attribute attr)
665 : : {
666 : 22227 : gcc_assert (attr < ATTR_LAST);
667 : 22227 : if (!m_attributes [attr])
668 : 18563 : m_attributes [attr] = make_attrs_tree (attr);
669 : 22227 : return m_attributes [attr];
670 : : }
671 : :
672 : : /* Handle a cache-miss within the m_attributes array by
673 : : generating the attributes for enum built_in_attribute
674 : : in tree form. */
675 : :
676 : : tree
677 : 18563 : builtins_manager::make_attrs_tree (enum built_in_attribute attr)
678 : : {
679 : 18563 : switch (attr)
680 : : {
681 : : /* Generate cases from builtin-attrs.def. */
682 : : #define DEF_ATTR_NULL_TREE(ENUM) \
683 : : case ENUM: return NULL_TREE;
684 : : #define DEF_ATTR_INT(ENUM, VALUE) \
685 : : case ENUM: return build_int_cst (integer_type_node, VALUE);
686 : : #define DEF_ATTR_STRING(ENUM, VALUE) \
687 : : case ENUM: return build_string (strlen (VALUE), VALUE);
688 : : #define DEF_ATTR_IDENT(ENUM, STRING) \
689 : : case ENUM: return get_identifier (STRING);
690 : : #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) \
691 : : case ENUM: return tree_cons (get_attrs_tree (PURPOSE), \
692 : : get_attrs_tree (VALUE), \
693 : : get_attrs_tree (CHAIN));
694 : : #include "builtin-attrs.def"
695 : : #undef DEF_ATTR_NULL_TREE
696 : : #undef DEF_ATTR_INT
697 : : #undef DEF_ATTR_IDENT
698 : : #undef DEF_ATTR_TREE_LIST
699 : :
700 : 0 : default:
701 : : /* We somehow got a value not covered by the autogenerated
702 : : cases. */
703 : 0 : gcc_unreachable ();
704 : : return NULL;
705 : : }
706 : : }
707 : :
708 : : } // namespace jit
709 : : } // namespace gcc
|