ada: Fix wrong resolution for hidden discriminant in predicate
[official-gcc.git] / gcc / jit / jit-builtins.cc
blobfdd0739789d2cf4b2d0907f5493bb55394db8ba1
1 /* jit-builtins.cc -- Handling of builtin functions during JIT-compilation.
2 Copyright (C) 2014-2023 Free Software Foundation, Inc.
4 This file is part of GCC.
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.
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.
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/>. */
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"
27 #include "jit-builtins.h"
29 namespace gcc {
31 namespace jit {
33 const char *const prefix = "__builtin_";
34 const size_t prefix_len = strlen (prefix);
36 /* Create "builtin_data", a const table of the data within builtins.def. */
37 struct builtin_data
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;
47 const char *get_asm_name () const
49 if (both_p && fallback_p)
50 return name + prefix_len;
51 else
52 return name;
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[] =
61 #include "builtins.def"
64 /* Helper function for find_builtin_by_name. */
66 static bool
67 matches_builtin (const char *in_name,
68 const struct builtin_data& bd)
70 const bool debug = 0;
72 /* Ignore entries with a NULL name. */
73 if (!bd.name)
74 return false;
76 if (debug)
77 fprintf (stderr, "seen builtin: %s\n", bd.name);
79 if (strcmp (bd.name, in_name) == 0)
80 return true;
82 if (bd.both_p)
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 gcc_assert (strncmp (bd.name, prefix, prefix_len) == 0);
88 if (debug)
89 fprintf (stderr, "testing without prefix as: %s\n",
90 bd.name + prefix_len);
91 if (strcmp (bd.name + prefix_len, in_name) == 0)
92 return true;
95 return false;
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. */
102 static bool
103 find_builtin_by_name (const char *in_name,
104 enum built_in_function *out_id)
106 /* Locate builtin. This currently works by performing repeated
107 strcmp against every possible candidate, which is likely to
108 inefficient.
110 We start at index 1 to skip the initial entry (BUILT_IN_NONE), which
111 has a NULL name. */
112 for (unsigned int i = 1; i < ARRAY_SIZE (builtin_data); i++)
114 const struct builtin_data& bd = builtin_data[i];
115 if (matches_builtin (in_name, bd))
117 /* Found a match. */
118 *out_id = static_cast<enum built_in_function> (i);
119 return true;
123 /* Not found. */
124 return false;
127 // class builtins_manager
129 /* Constructor for gcc::jit::builtins_manager. */
131 builtins_manager::builtins_manager (recording::context *ctxt)
132 : m_ctxt (ctxt)
134 memset (m_types, 0, sizeof (m_types));
135 memset (m_builtin_functions, 0, sizeof (m_builtin_functions));
136 memset (m_attributes, 0, sizeof (m_attributes));
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. */
143 recording::function *
144 builtins_manager::get_builtin_function (const char *name)
146 enum built_in_function builtin_id;
147 if (!find_builtin_by_name (name, &builtin_id))
149 m_ctxt->add_error (NULL, "builtin \"%s\" not found", name);
150 return NULL;
153 return get_builtin_function_by_id (builtin_id);
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. */
160 recording::function *
161 builtins_manager::get_builtin_function_by_id (enum built_in_function builtin_id)
163 gcc_assert (builtin_id > BUILT_IN_NONE);
164 gcc_assert (builtin_id < END_BUILTINS);
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 if (!m_builtin_functions[builtin_id])
170 recording::function *fn = make_builtin_function (builtin_id);
171 if (fn)
173 m_builtin_functions[builtin_id] = fn;
174 m_ctxt->record (fn);
178 return m_builtin_functions[builtin_id];
181 /* Create the recording::function for a given builtin function, by ID. */
183 recording::function *
184 builtins_manager::make_builtin_function (enum built_in_function builtin_id)
186 const struct builtin_data& bd = builtin_data[builtin_id];
187 enum jit_builtin_type type_id = bd.type;
188 recording::type *t = get_type (type_id);
189 if (!t)
190 return NULL;
191 recording::function_type *func_type = t->as_a_function_type ();
192 if (!func_type)
193 return NULL;
195 vec<recording::type *> param_types = func_type->get_param_types ();
196 recording::param **params = new recording::param *[param_types.length ()];
198 int i;
199 recording::type *param_type;
200 FOR_EACH_VEC_ELT (param_types, i, param_type)
202 char buf[16];
203 snprintf (buf, 16, "arg%d", i);
204 params[i] = m_ctxt->new_param (NULL,
205 param_type,
206 buf);
208 const char *asm_name = bd.get_asm_name ();
209 recording::function *result =
210 new recording::function (m_ctxt,
211 NULL,
212 GCC_JIT_FUNCTION_IMPORTED, // FIXME
213 func_type->get_return_type (),
214 m_ctxt->new_string (asm_name),
215 param_types.length (),
216 params,
217 func_type->is_variadic (),
218 builtin_id);
219 delete[] params;
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.
226 Hence query the cache for BUILT_IN_CEXPI to ensure it gets
227 built. */
228 if (builtin_id == BUILT_IN_COS || builtin_id == BUILT_IN_SIN)
229 (void)get_builtin_function_by_id (BUILT_IN_CEXPI);
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.
235 Ensure the appropriate SINCOS builtin exists. */
236 if (builtin_id == BUILT_IN_CEXPIF)
237 (void)get_builtin_function_by_id (BUILT_IN_SINCOSF);
238 else if (builtin_id == BUILT_IN_CEXPI)
239 (void)get_builtin_function_by_id (BUILT_IN_SINCOS);
240 else if (builtin_id == BUILT_IN_CEXPIL)
241 (void)get_builtin_function_by_id (BUILT_IN_SINCOSL);
243 return result;
246 /* Build an array of type names for use by get_string_for_type_id. */
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,
289 #include "builtin-types.def"
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
315 /* Get a string for TYPE_ID suitable for use in logs and error messages
316 (e.g. "BT_PID"). */
318 static const char *
319 get_string_for_type_id (enum jit_builtin_type type_id)
321 gcc_assert (type_id < ARRAY_SIZE (type_names));
322 return type_names[type_id];
325 /* Get the recording::type for a given type of builtin function,
326 by ID, creating it if it doesn't already exist. */
328 recording::type *
329 builtins_manager::get_type (enum jit_builtin_type type_id)
331 if (!m_types[type_id])
332 m_types[type_id] = make_type (type_id);
333 return m_types[type_id];
336 /* Create the recording::type for a given type of builtin function. */
338 recording::type *
339 builtins_manager::make_type (enum jit_builtin_type type_id)
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 switch (type_id)
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);
420 #include "builtin-types.def"
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
445 default:
446 gcc_unreachable ();
450 /* Create the recording::type for a given primitive type within the
451 builtin system.
453 Only some types are currently supported. */
455 recording::type*
456 builtins_manager::make_primitive_type (enum jit_builtin_type type_id)
458 switch (type_id)
460 default:
461 // only some of these types are implemented so far:
462 m_ctxt->add_error (NULL,
463 "unimplemented primitive type for builtin (type: %s)",
464 get_string_for_type_id (type_id));
465 return NULL;
467 case BT_VOID: return m_ctxt->get_type (GCC_JIT_TYPE_VOID);
468 case BT_BOOL: return m_ctxt->get_type (GCC_JIT_TYPE_BOOL);
469 case BT_INT: return m_ctxt->get_type (GCC_JIT_TYPE_INT);
470 case BT_UINT: return m_ctxt->get_type (GCC_JIT_TYPE_UNSIGNED_INT);
471 case BT_LONG: return m_ctxt->get_type (GCC_JIT_TYPE_LONG);
472 case BT_ULONG: return m_ctxt->get_type (GCC_JIT_TYPE_UNSIGNED_LONG);
473 case BT_LONGLONG: return m_ctxt->get_type (GCC_JIT_TYPE_LONG_LONG);
474 case BT_ULONGLONG:
475 return m_ctxt->get_type (GCC_JIT_TYPE_UNSIGNED_LONG_LONG);
476 // case BT_INTMAX:
477 // case BT_UINTMAX:
478 case BT_INT8: return m_ctxt->get_int_type (1, true);
479 case BT_INT16: return m_ctxt->get_int_type (2, true);
480 case BT_UINT8: return m_ctxt->get_int_type (1, false);
481 case BT_UINT16: return m_ctxt->get_int_type (2, false);
482 case BT_UINT32: return m_ctxt->get_int_type (4, false);
483 case BT_UINT64: return m_ctxt->get_int_type (8, false);
484 case BT_UINT128: return m_ctxt->get_int_type (16, false);
485 // case BT_WORD:
486 // case BT_UNWINDWORD:
487 case BT_FLOAT: return m_ctxt->get_type (GCC_JIT_TYPE_FLOAT);
488 case BT_DOUBLE: return m_ctxt->get_type (GCC_JIT_TYPE_DOUBLE);
489 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 case BT_COMPLEX_FLOAT:
498 return m_ctxt->get_type (GCC_JIT_TYPE_COMPLEX_FLOAT);
499 case BT_COMPLEX_DOUBLE:
500 return m_ctxt->get_type (GCC_JIT_TYPE_COMPLEX_DOUBLE);
501 case BT_COMPLEX_LONGDOUBLE:
502 return m_ctxt->get_type (GCC_JIT_TYPE_COMPLEX_LONG_DOUBLE);
503 case BT_PTR: return m_ctxt->get_type (GCC_JIT_TYPE_VOID_PTR);
504 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 case BT_CONST_PTR:
511 return m_ctxt->get_type (GCC_JIT_TYPE_VOID)->get_const ()->get_pointer ();
512 case BT_VOLATILE_PTR:
513 return (m_ctxt->get_type (GCC_JIT_TYPE_VOID)->get_volatile ()
514 ->get_pointer ());
515 case BT_CONST_VOLATILE_PTR:
516 return (m_ctxt->get_type (GCC_JIT_TYPE_VOID)->get_const ()
517 ->get_volatile ()->get_pointer ());
518 // case BT_PTRMODE:
519 case BT_INT_PTR:
520 return m_ctxt->get_type (GCC_JIT_TYPE_INT)->get_pointer ();
521 case BT_FLOAT_PTR:
522 return m_ctxt->get_type (GCC_JIT_TYPE_FLOAT)->get_pointer ();
523 case BT_DOUBLE_PTR:
524 return m_ctxt->get_type (GCC_JIT_TYPE_DOUBLE)->get_pointer ();
525 case BT_CONST_DOUBLE_PTR:
526 return (m_ctxt->get_type (GCC_JIT_TYPE_DOUBLE)->get_const ()
527 ->get_pointer ());
528 // case BT_LONGDOUBLE_PTR:
529 // case BT_PID:
530 case BT_SIZE:
531 return m_ctxt->get_type (GCC_JIT_TYPE_SIZE_T);
532 case BT_CONST_SIZE:
533 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 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 case BT_I1: return m_ctxt->get_int_type (1, true);
544 case BT_I2: return m_ctxt->get_int_type (2, true);
545 case BT_I4: return m_ctxt->get_int_type (4, true);
546 case BT_I8: return m_ctxt->get_int_type (8, true);
547 case BT_I16: return m_ctxt->get_int_type (16, true);
548 // case BT_PTR_CONST_STRING:
552 /* Create the recording::function_type for a given function type
553 signature. */
555 recording::function_type *
556 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, ...)
561 va_list list;
562 int i;
563 recording::type **param_types = new recording::type *[num_args];
564 recording::type *return_type = NULL;
565 recording::function_type *result = NULL;
567 va_start (list, num_args);
568 for (i = 0; i < num_args; ++i)
570 enum jit_builtin_type arg_type_id =
571 (enum jit_builtin_type) va_arg (list, int);
572 param_types[i] = get_type (arg_type_id);
573 if (!param_types[i])
574 goto error;
576 va_end (list);
578 return_type = get_type (return_type_id);
579 if (!return_type)
580 goto error;
582 result = m_ctxt->new_function_type (return_type,
583 num_args,
584 param_types,
585 is_variadic);
587 error:
588 delete[] param_types;
589 return result;
592 /* Handler for DEF_POINTER_TYPE within builtins_manager::make_type. */
594 recording::type *
595 builtins_manager::make_ptr_type (enum jit_builtin_type,
596 enum jit_builtin_type other_type_id)
598 recording::type *base_type = get_type (other_type_id);
599 return base_type->get_pointer ();
602 /* Ensure that builtins that could be needed during optimization
603 get created ahead of time. */
605 void
606 builtins_manager::ensure_optimization_builtins_exist ()
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 (void)get_builtin_function_by_id (BUILT_IN_TRAP);
614 /* Playback support. */
616 /* A builtins_manager is associated with a recording::context
617 and might be reused for multiple compiles on various
618 playback::contexts, perhaps with different options.
620 Purge any playback state. Currently this is just the table of
621 attributes. */
623 void
624 builtins_manager::finish_playback (void)
626 memset (m_attributes, 0, sizeof (m_attributes));
629 /* Get the enum built_in_class for BUILTIN_ID. */
631 enum built_in_class
632 builtins_manager::get_class (enum built_in_function builtin_id)
634 return builtin_data[builtin_id].fnclass;
637 /* Is BUILTIN_ID implicit? */
639 bool
640 builtins_manager::implicit_p (enum built_in_function builtin_id)
642 return builtin_data[builtin_id].implicit_p;
645 /* Get any attributes (in tree form) for the function declaration
646 for BUILTIN_ID.
648 These are created on-demand, and cached within the m_attributes
649 array, until finish_playback. */
651 tree
652 builtins_manager::get_attrs_tree (enum built_in_function builtin_id)
654 enum built_in_attribute attr = builtin_data[builtin_id].attr;
655 return get_attrs_tree (attr);
658 /* As above, but for an enum built_in_attribute. */
660 tree
661 builtins_manager::get_attrs_tree (enum built_in_attribute attr)
663 gcc_assert (attr < ATTR_LAST);
664 if (!m_attributes [attr])
665 m_attributes [attr] = make_attrs_tree (attr);
666 return m_attributes [attr];
669 /* Handle a cache-miss within the m_attributes array by
670 generating the attributes for enum built_in_attribute
671 in tree form. */
673 tree
674 builtins_manager::make_attrs_tree (enum built_in_attribute attr)
676 switch (attr)
678 /* Generate cases from builtin-attrs.def. */
679 #define DEF_ATTR_NULL_TREE(ENUM) \
680 case ENUM: return NULL_TREE;
681 #define DEF_ATTR_INT(ENUM, VALUE) \
682 case ENUM: return build_int_cst (integer_type_node, VALUE);
683 #define DEF_ATTR_STRING(ENUM, VALUE) \
684 case ENUM: return build_string (strlen (VALUE), VALUE);
685 #define DEF_ATTR_IDENT(ENUM, STRING) \
686 case ENUM: return get_identifier (STRING);
687 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) \
688 case ENUM: return tree_cons (get_attrs_tree (PURPOSE), \
689 get_attrs_tree (VALUE), \
690 get_attrs_tree (CHAIN));
691 #include "builtin-attrs.def"
692 #undef DEF_ATTR_NULL_TREE
693 #undef DEF_ATTR_INT
694 #undef DEF_ATTR_IDENT
695 #undef DEF_ATTR_TREE_LIST
697 default:
698 /* We somehow got a value not covered by the autogenerated
699 cases. */
700 gcc_unreachable ();
701 return NULL;
705 } // namespace jit
706 } // namespace gcc