Daily bump.
[official-gcc.git] / gcc / jit / jit-builtins.c
blob56a79b4e249f023a003b412d7706e59ea0ad5f32
1 /* jit-builtins.c -- Handling of builtin functions during JIT-compilation.
2 Copyright (C) 2014-2020 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;
113 i < sizeof (builtin_data) / sizeof (builtin_data[0]);
114 i++)
116 const struct builtin_data& bd = builtin_data[i];
117 if (matches_builtin (in_name, bd))
119 /* Found a match. */
120 *out_id = static_cast<enum built_in_function> (i);
121 return true;
125 /* Not found. */
126 return false;
129 // class builtins_manager
131 /* Constructor for gcc::jit::builtins_manager. */
133 builtins_manager::builtins_manager (recording::context *ctxt)
134 : m_ctxt (ctxt)
136 memset (m_types, 0, sizeof (m_types));
137 memset (m_builtin_functions, 0, sizeof (m_builtin_functions));
138 memset (m_attributes, 0, sizeof (m_attributes));
141 /* Locate a builtin function by name.
142 Create a recording::function of the appropriate type, reusing them
143 if they've already been seen. */
145 recording::function *
146 builtins_manager::get_builtin_function (const char *name)
148 enum built_in_function builtin_id;
149 if (!find_builtin_by_name (name, &builtin_id))
151 m_ctxt->add_error (NULL, "builtin \"%s\" not found", name);
152 return NULL;
155 return get_builtin_function_by_id (builtin_id);
158 /* Locate a builtin function by id.
159 Create a recording::function of the appropriate type, reusing them
160 if they've already been seen. */
162 recording::function *
163 builtins_manager::get_builtin_function_by_id (enum built_in_function builtin_id)
165 gcc_assert (builtin_id >= 0);
166 gcc_assert (builtin_id < END_BUILTINS);
168 /* Lazily build the functions, caching them so that repeated calls for
169 the same id on a context give back the same object. */
170 if (!m_builtin_functions[builtin_id])
172 recording::function *fn = make_builtin_function (builtin_id);
173 if (fn)
175 m_builtin_functions[builtin_id] = fn;
176 m_ctxt->record (fn);
180 return m_builtin_functions[builtin_id];
183 /* Create the recording::function for a given builtin function, by ID. */
185 recording::function *
186 builtins_manager::make_builtin_function (enum built_in_function builtin_id)
188 const struct builtin_data& bd = builtin_data[builtin_id];
189 enum jit_builtin_type type_id = bd.type;
190 recording::type *t = get_type (type_id);
191 if (!t)
192 return NULL;
193 recording::function_type *func_type = t->as_a_function_type ();
194 if (!func_type)
195 return NULL;
197 vec<recording::type *> param_types = func_type->get_param_types ();
198 recording::param **params = new recording::param *[param_types.length ()];
200 int i;
201 recording::type *param_type;
202 FOR_EACH_VEC_ELT (param_types, i, param_type)
204 char buf[16];
205 snprintf (buf, 16, "arg%d", i);
206 params[i] = m_ctxt->new_param (NULL,
207 param_type,
208 buf);
210 const char *asm_name = bd.get_asm_name ();
211 recording::function *result =
212 new recording::function (m_ctxt,
213 NULL,
214 GCC_JIT_FUNCTION_IMPORTED, // FIXME
215 func_type->get_return_type (),
216 m_ctxt->new_string (asm_name),
217 param_types.length (),
218 params,
219 func_type->is_variadic (),
220 builtin_id);
221 delete[] params;
223 /* PR/64020 - If the client code is using builtin cos or sin,
224 tree-ssa-math-opt.c's execute_cse_sincos_1 may attempt
225 to optimize them to use __builtin_cexpi; for this,
226 BUILT_IN_CEXPI needs to exist.
228 Hence query the cache for BUILT_IN_CEXPI to ensure it gets
229 built. */
230 if (builtin_id == BUILT_IN_COS || builtin_id == BUILT_IN_SIN)
231 (void)get_builtin_function_by_id (BUILT_IN_CEXPI);
233 /* builtins.c:expand_builtin_cexpi can optimize the various
234 CEXP builtins to SINCOS builtins, and hence we may require
235 SINCOS builtins latter.
237 Ensure the appropriate SINCOS builtin exists. */
238 if (builtin_id == BUILT_IN_CEXPIF)
239 (void)get_builtin_function_by_id (BUILT_IN_SINCOSF);
240 else if (builtin_id == BUILT_IN_CEXPI)
241 (void)get_builtin_function_by_id (BUILT_IN_SINCOS);
242 else if (builtin_id == BUILT_IN_CEXPIL)
243 (void)get_builtin_function_by_id (BUILT_IN_SINCOSL);
245 return result;
248 /* Build an array of type names for use by get_string_for_type_id. */
250 static const char * const type_names[] = {
251 #define DEF_PRIMITIVE_TYPE(ENUM, VALUE) #ENUM,
252 #define DEF_FUNCTION_TYPE_0(ENUM, RETURN) #ENUM,
253 #define DEF_FUNCTION_TYPE_1(ENUM, RETURN, ARG1) #ENUM,
254 #define DEF_FUNCTION_TYPE_2(ENUM, RETURN, ARG1, ARG2) #ENUM,
255 #define DEF_FUNCTION_TYPE_3(ENUM, RETURN, ARG1, ARG2, ARG3) #ENUM,
256 #define DEF_FUNCTION_TYPE_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) #ENUM,
257 #define DEF_FUNCTION_TYPE_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) #ENUM,
258 #define DEF_FUNCTION_TYPE_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
259 ARG6) \
260 #ENUM,
261 #define DEF_FUNCTION_TYPE_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
262 ARG6, ARG7) \
263 #ENUM,
264 #define DEF_FUNCTION_TYPE_8(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
265 ARG6, ARG7, ARG8) \
266 #ENUM,
267 #define DEF_FUNCTION_TYPE_9(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
268 ARG6, ARG7, ARG8, ARG9) \
269 #ENUM,
270 #define DEF_FUNCTION_TYPE_10(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
271 ARG6, ARG7, ARG8, ARG9, ARG10) \
272 #ENUM,
273 #define DEF_FUNCTION_TYPE_11(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
274 ARG6, ARG7, ARG8, ARG9, ARG10, ARG11) \
275 #ENUM,
276 #define DEF_FUNCTION_TYPE_VAR_0(ENUM, RETURN) #ENUM,
277 #define DEF_FUNCTION_TYPE_VAR_1(ENUM, RETURN, ARG1) #ENUM,
278 #define DEF_FUNCTION_TYPE_VAR_2(ENUM, RETURN, ARG1, ARG2) #ENUM,
279 #define DEF_FUNCTION_TYPE_VAR_3(ENUM, RETURN, ARG1, ARG2, ARG3) #ENUM,
280 #define DEF_FUNCTION_TYPE_VAR_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) #ENUM,
281 #define DEF_FUNCTION_TYPE_VAR_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
282 #ENUM,
283 #define DEF_FUNCTION_TYPE_VAR_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
284 ARG6) \
285 #ENUM,
286 #define DEF_FUNCTION_TYPE_VAR_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
287 ARG6, ARG7) \
288 #ENUM,
289 #define DEF_POINTER_TYPE(ENUM, TYPE) #ENUM,
291 #include "builtin-types.def"
293 #undef DEF_PRIMITIVE_TYPE
294 #undef DEF_FUNCTION_TYPE_0
295 #undef DEF_FUNCTION_TYPE_1
296 #undef DEF_FUNCTION_TYPE_2
297 #undef DEF_FUNCTION_TYPE_3
298 #undef DEF_FUNCTION_TYPE_4
299 #undef DEF_FUNCTION_TYPE_5
300 #undef DEF_FUNCTION_TYPE_6
301 #undef DEF_FUNCTION_TYPE_7
302 #undef DEF_FUNCTION_TYPE_8
303 #undef DEF_FUNCTION_TYPE_9
304 #undef DEF_FUNCTION_TYPE_10
305 #undef DEF_FUNCTION_TYPE_11
306 #undef DEF_FUNCTION_TYPE_VAR_0
307 #undef DEF_FUNCTION_TYPE_VAR_1
308 #undef DEF_FUNCTION_TYPE_VAR_2
309 #undef DEF_FUNCTION_TYPE_VAR_3
310 #undef DEF_FUNCTION_TYPE_VAR_4
311 #undef DEF_FUNCTION_TYPE_VAR_5
312 #undef DEF_FUNCTION_TYPE_VAR_6
313 #undef DEF_FUNCTION_TYPE_VAR_7
314 #undef DEF_POINTER_TYPE
317 /* Get a string for TYPE_ID suitable for use in logs and error messages
318 (e.g. "BT_PID"). */
320 static const char *
321 get_string_for_type_id (enum jit_builtin_type type_id)
323 gcc_assert (type_id < sizeof (type_names)/sizeof(type_names[0]));
324 return type_names[type_id];
327 /* Get the recording::type for a given type of builtin function,
328 by ID, creating it if it doesn't already exist. */
330 recording::type *
331 builtins_manager::get_type (enum jit_builtin_type type_id)
333 if (!m_types[type_id])
334 m_types[type_id] = make_type (type_id);
335 return m_types[type_id];
338 /* Create the recording::type for a given type of builtin function. */
340 recording::type *
341 builtins_manager::make_type (enum jit_builtin_type type_id)
343 /* Use builtin-types.def to construct a switch statement, with each
344 case deferring to one of the methods below:
345 - DEF_PRIMITIVE_TYPE is handled as a call to make_primitive_type.
346 - the various DEF_FUNCTION_TYPE_n are handled by variadic calls
347 to make_fn_type.
348 - similarly for DEF_FUNCTION_TYPE_VAR_n, but setting the
349 "is_variadic" argument.
350 - DEF_POINTER_TYPE is handled by make_ptr_type.
351 That should handle everything, but just in case we also suppy a
352 gcc_unreachable default clause. */
353 switch (type_id)
355 #define DEF_PRIMITIVE_TYPE(ENUM, VALUE) \
356 case ENUM: return make_primitive_type (ENUM);
357 #define DEF_FUNCTION_TYPE_0(ENUM, RETURN) \
358 case ENUM: return make_fn_type (ENUM, RETURN, 0, 0);
359 #define DEF_FUNCTION_TYPE_1(ENUM, RETURN, ARG1) \
360 case ENUM: return make_fn_type (ENUM, RETURN, 0, 1, ARG1);
361 #define DEF_FUNCTION_TYPE_2(ENUM, RETURN, ARG1, ARG2) \
362 case ENUM: return make_fn_type (ENUM, RETURN, 0, 2, ARG1, ARG2);
363 #define DEF_FUNCTION_TYPE_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
364 case ENUM: return make_fn_type (ENUM, RETURN, 0, 3, ARG1, ARG2, ARG3);
365 #define DEF_FUNCTION_TYPE_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
366 case ENUM: return make_fn_type (ENUM, RETURN, 0, 4, ARG1, ARG2, ARG3, \
367 ARG4);
368 #define DEF_FUNCTION_TYPE_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
369 case ENUM: return make_fn_type (ENUM, RETURN, 0, 5, ARG1, ARG2, ARG3, \
370 ARG4, ARG5);
371 #define DEF_FUNCTION_TYPE_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
372 ARG6) \
373 case ENUM: return make_fn_type (ENUM, RETURN, 0, 6, ARG1, ARG2, ARG3, \
374 ARG4, ARG5, ARG6);
375 #define DEF_FUNCTION_TYPE_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
376 ARG6, ARG7) \
377 case ENUM: return make_fn_type (ENUM, RETURN, 0, 7, ARG1, ARG2, ARG3, \
378 ARG4, ARG5, ARG6, ARG7);
379 #define DEF_FUNCTION_TYPE_8(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
380 ARG6, ARG7, ARG8) \
381 case ENUM: return make_fn_type (ENUM, RETURN, 0, 8, ARG1, ARG2, ARG3, \
382 ARG4, ARG5, ARG6, ARG7, ARG8);
383 #define DEF_FUNCTION_TYPE_9(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
384 ARG6, ARG7, ARG8, ARG9) \
385 case ENUM: return make_fn_type (ENUM, RETURN, 0, 9, ARG1, ARG2, ARG3, \
386 ARG4, ARG5, ARG6, ARG7, ARG8, ARG9);
387 #define DEF_FUNCTION_TYPE_10(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
388 ARG6, ARG7, ARG8, ARG9, ARG10) \
389 case ENUM: return make_fn_type (ENUM, RETURN, 0, 10, ARG1, ARG2, ARG3, \
390 ARG4, ARG5, ARG6, ARG7, ARG8, ARG9, \
391 ARG10);
392 #define DEF_FUNCTION_TYPE_11(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
393 ARG6, ARG7, ARG8, ARG9, ARG10, ARG11) \
394 case ENUM: return make_fn_type (ENUM, RETURN, 0, 11, ARG1, ARG2, ARG3, \
395 ARG4, ARG5, ARG6, ARG7, ARG8, ARG9, \
396 ARG10, ARG11);
397 #define DEF_FUNCTION_TYPE_VAR_0(ENUM, RETURN) \
398 case ENUM: return make_fn_type (ENUM, RETURN, 1, 0);
399 #define DEF_FUNCTION_TYPE_VAR_1(ENUM, RETURN, ARG1) \
400 case ENUM: return make_fn_type (ENUM, RETURN, 1, 1, ARG1);
401 #define DEF_FUNCTION_TYPE_VAR_2(ENUM, RETURN, ARG1, ARG2) \
402 case ENUM: return make_fn_type (ENUM, RETURN, 1, 2, ARG1, ARG2);
403 #define DEF_FUNCTION_TYPE_VAR_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
404 case ENUM: return make_fn_type (ENUM, RETURN, 1, 3, ARG1, ARG2, ARG3);
405 #define DEF_FUNCTION_TYPE_VAR_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
406 case ENUM: return make_fn_type (ENUM, RETURN, 1, 4, ARG1, ARG2, ARG3, \
407 ARG4);
408 #define DEF_FUNCTION_TYPE_VAR_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
409 case ENUM: return make_fn_type (ENUM, RETURN, 1, 5, ARG1, ARG2, ARG3, \
410 ARG4, ARG5);
411 #define DEF_FUNCTION_TYPE_VAR_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
412 ARG6) \
413 case ENUM: return make_fn_type (ENUM, RETURN, 1, 6, ARG1, ARG2, ARG3, \
414 ARG4, ARG5, ARG6);
415 #define DEF_FUNCTION_TYPE_VAR_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
416 ARG6, ARG7) \
417 case ENUM: return make_fn_type (ENUM, RETURN, 1, 7, ARG1, ARG2, ARG3, \
418 ARG4, ARG5, ARG6, ARG7);
419 #define DEF_POINTER_TYPE(ENUM, TYPE) \
420 case ENUM: return make_ptr_type (ENUM, TYPE);
422 #include "builtin-types.def"
424 #undef DEF_PRIMITIVE_TYPE
425 #undef DEF_FUNCTION_TYPE_0
426 #undef DEF_FUNCTION_TYPE_1
427 #undef DEF_FUNCTION_TYPE_2
428 #undef DEF_FUNCTION_TYPE_3
429 #undef DEF_FUNCTION_TYPE_4
430 #undef DEF_FUNCTION_TYPE_5
431 #undef DEF_FUNCTION_TYPE_6
432 #undef DEF_FUNCTION_TYPE_7
433 #undef DEF_FUNCTION_TYPE_8
434 #undef DEF_FUNCTION_TYPE_9
435 #undef DEF_FUNCTION_TYPE_10
436 #undef DEF_FUNCTION_TYPE_11
437 #undef DEF_FUNCTION_TYPE_VAR_0
438 #undef DEF_FUNCTION_TYPE_VAR_1
439 #undef DEF_FUNCTION_TYPE_VAR_2
440 #undef DEF_FUNCTION_TYPE_VAR_3
441 #undef DEF_FUNCTION_TYPE_VAR_4
442 #undef DEF_FUNCTION_TYPE_VAR_5
443 #undef DEF_FUNCTION_TYPE_VAR_6
444 #undef DEF_FUNCTION_TYPE_VAR_7
445 #undef DEF_POINTER_TYPE
447 default:
448 gcc_unreachable ();
452 /* Create the recording::type for a given primitive type within the
453 builtin system.
455 Only some types are currently supported. */
457 recording::type*
458 builtins_manager::make_primitive_type (enum jit_builtin_type type_id)
460 switch (type_id)
462 default:
463 // only some of these types are implemented so far:
464 m_ctxt->add_error (NULL,
465 "unimplemented primitive type for builtin (type: %s)",
466 get_string_for_type_id (type_id));
467 return NULL;
469 case BT_VOID: return m_ctxt->get_type (GCC_JIT_TYPE_VOID);
470 case BT_BOOL: return m_ctxt->get_type (GCC_JIT_TYPE_BOOL);
471 case BT_INT: return m_ctxt->get_type (GCC_JIT_TYPE_INT);
472 case BT_UINT: return m_ctxt->get_type (GCC_JIT_TYPE_UNSIGNED_INT);
473 case BT_LONG: return m_ctxt->get_type (GCC_JIT_TYPE_LONG);
474 case BT_ULONG: return m_ctxt->get_type (GCC_JIT_TYPE_UNSIGNED_LONG);
475 case BT_LONGLONG: return m_ctxt->get_type (GCC_JIT_TYPE_LONG_LONG);
476 case BT_ULONGLONG:
477 return m_ctxt->get_type (GCC_JIT_TYPE_UNSIGNED_LONG_LONG);
478 // case BT_INTMAX:
479 // case BT_UINTMAX:
480 case BT_INT8: return m_ctxt->get_int_type (1, true);
481 case BT_INT16: return m_ctxt->get_int_type (2, true);
482 case BT_UINT8: return m_ctxt->get_int_type (1, false);
483 case BT_UINT16: return m_ctxt->get_int_type (2, false);
484 case BT_UINT32: return m_ctxt->get_int_type (4, false);
485 case BT_UINT64: return m_ctxt->get_int_type (8, false);
486 // case BT_WORD:
487 // case BT_UNWINDWORD:
488 case BT_FLOAT: return m_ctxt->get_type (GCC_JIT_TYPE_FLOAT);
489 case BT_DOUBLE: return m_ctxt->get_type (GCC_JIT_TYPE_DOUBLE);
490 case BT_LONGDOUBLE: return m_ctxt->get_type (GCC_JIT_TYPE_LONG_DOUBLE);
491 // case BT_FLOAT16:
492 // case BT_FLOAT32:
493 // case BT_FLOAT64:
494 // case BT_FLOAT128:
495 // case BT_FLOAT32X:
496 // case BT_FLOAT64X:
497 // case BT_FLOAT128X:
498 case BT_COMPLEX_FLOAT:
499 return m_ctxt->get_type (GCC_JIT_TYPE_COMPLEX_FLOAT);
500 case BT_COMPLEX_DOUBLE:
501 return m_ctxt->get_type (GCC_JIT_TYPE_COMPLEX_DOUBLE);
502 case BT_COMPLEX_LONGDOUBLE:
503 return m_ctxt->get_type (GCC_JIT_TYPE_COMPLEX_LONG_DOUBLE);
504 case BT_PTR: return m_ctxt->get_type (GCC_JIT_TYPE_VOID_PTR);
505 case BT_FILEPTR: return m_ctxt->get_type (GCC_JIT_TYPE_FILE_PTR);
506 // case BT_CONST_TM_PTR:
507 // case BT_FENV_T_PTR:
508 // case BT_CONST_FENV_T_PTR:
509 // case BT_FEXCEPT_T_PTR:
510 // case BT_CONST_FEXCEPT_T_PTR:
511 case BT_CONST_PTR:
512 return m_ctxt->get_type (GCC_JIT_TYPE_VOID)->get_const ()->get_pointer ();
513 case BT_VOLATILE_PTR:
514 return (m_ctxt->get_type (GCC_JIT_TYPE_VOID)->get_volatile ()
515 ->get_pointer ());
516 case BT_CONST_VOLATILE_PTR:
517 return (m_ctxt->get_type (GCC_JIT_TYPE_VOID)->get_const ()
518 ->get_volatile ()->get_pointer ());
519 // case BT_PTRMODE:
520 case BT_INT_PTR:
521 return m_ctxt->get_type (GCC_JIT_TYPE_INT)->get_pointer ();
522 case BT_FLOAT_PTR:
523 return m_ctxt->get_type (GCC_JIT_TYPE_FLOAT)->get_pointer ();
524 case BT_DOUBLE_PTR:
525 return m_ctxt->get_type (GCC_JIT_TYPE_DOUBLE)->get_pointer ();
526 case BT_CONST_DOUBLE_PTR:
527 return (m_ctxt->get_type (GCC_JIT_TYPE_DOUBLE)->get_const ()
528 ->get_pointer ());
529 // case BT_LONGDOUBLE_PTR:
530 // case BT_PID:
531 case BT_SIZE:
532 return m_ctxt->get_type (GCC_JIT_TYPE_SIZE_T);
533 case BT_CONST_SIZE:
534 return m_ctxt->get_type (GCC_JIT_TYPE_SIZE_T)->get_const ();
535 // case BT_SSIZE:
536 // case BT_WINT:
537 // case BT_STRING:
538 case BT_CONST_STRING: return m_ctxt->get_type (GCC_JIT_TYPE_CONST_CHAR_PTR);
539 // case BT_DFLOAT32:
540 // case BT_DFLOAT64:
541 // case BT_DFLOAT128:
542 // case BT_VALIST_REF:
543 // case BT_VALIST_ARG:
544 // case BT_I1:
545 // case BT_I2:
546 // case BT_I4:
547 // case BT_I8:
548 // case BT_I16:
549 // case BT_PTR_CONST_STRING:
553 /* Create the recording::function_type for a given function type
554 signature. */
556 recording::function_type *
557 builtins_manager::make_fn_type (enum jit_builtin_type,
558 enum jit_builtin_type return_type_id,
559 bool is_variadic,
560 int num_args, ...)
562 va_list list;
563 int i;
564 recording::type **param_types = new recording::type *[num_args];
565 recording::type *return_type = NULL;
566 recording::function_type *result = NULL;
568 va_start (list, num_args);
569 for (i = 0; i < num_args; ++i)
571 enum jit_builtin_type arg_type_id =
572 (enum jit_builtin_type) va_arg (list, int);
573 param_types[i] = get_type (arg_type_id);
574 if (!param_types[i])
575 goto error;
577 va_end (list);
579 return_type = get_type (return_type_id);
580 if (!return_type)
581 goto error;
583 result = m_ctxt->new_function_type (return_type,
584 num_args,
585 param_types,
586 is_variadic);
588 error:
589 delete[] param_types;
590 return result;
593 /* Handler for DEF_POINTER_TYPE within builtins_manager::make_type. */
595 recording::type *
596 builtins_manager::make_ptr_type (enum jit_builtin_type,
597 enum jit_builtin_type other_type_id)
599 recording::type *base_type = get_type (other_type_id);
600 return base_type->get_pointer ();
603 /* Playback support. */
605 /* A builtins_manager is associated with a recording::context
606 and might be reused for multiple compiles on various
607 playback::contexts, perhaps with different options.
609 Purge any playback state. Currently this is just the table of
610 attributes. */
612 void
613 builtins_manager::finish_playback (void)
615 memset (m_attributes, 0, sizeof (m_attributes));
618 /* Get the enum built_in_class for BUILTIN_ID. */
620 enum built_in_class
621 builtins_manager::get_class (enum built_in_function builtin_id)
623 return builtin_data[builtin_id].fnclass;
626 /* Is BUILTIN_ID implicit? */
628 bool
629 builtins_manager::implicit_p (enum built_in_function builtin_id)
631 return builtin_data[builtin_id].implicit_p;
634 /* Get any attributes (in tree form) for the function declaration
635 for BUILTIN_ID.
637 These are created on-demand, and cached within the m_attributes
638 array, until finish_playback. */
640 tree
641 builtins_manager::get_attrs_tree (enum built_in_function builtin_id)
643 enum built_in_attribute attr = builtin_data[builtin_id].attr;
644 return get_attrs_tree (attr);
647 /* As above, but for an enum built_in_attribute. */
649 tree
650 builtins_manager::get_attrs_tree (enum built_in_attribute attr)
652 gcc_assert (attr < ATTR_LAST);
653 if (!m_attributes [attr])
654 m_attributes [attr] = make_attrs_tree (attr);
655 return m_attributes [attr];
658 /* Handle a cache-miss within the m_attributes array by
659 generating the attributes for enum built_in_attribute
660 in tree form. */
662 tree
663 builtins_manager::make_attrs_tree (enum built_in_attribute attr)
665 switch (attr)
667 /* Generate cases from builtin-attrs.def. */
668 #define DEF_ATTR_NULL_TREE(ENUM) \
669 case ENUM: return NULL_TREE;
670 #define DEF_ATTR_INT(ENUM, VALUE) \
671 case ENUM: return build_int_cst (integer_type_node, VALUE);
672 #define DEF_ATTR_STRING(ENUM, VALUE) \
673 case ENUM: return build_string (strlen (VALUE), VALUE);
674 #define DEF_ATTR_IDENT(ENUM, STRING) \
675 case ENUM: return get_identifier (STRING);
676 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) \
677 case ENUM: return tree_cons (get_attrs_tree (PURPOSE), \
678 get_attrs_tree (VALUE), \
679 get_attrs_tree (CHAIN));
680 #include "builtin-attrs.def"
681 #undef DEF_ATTR_NULL_TREE
682 #undef DEF_ATTR_INT
683 #undef DEF_ATTR_IDENT
684 #undef DEF_ATTR_TREE_LIST
686 default:
687 /* We somehow got a value not covered by the autogenerated
688 cases. */
689 gcc_unreachable ();
690 return NULL;
694 } // namespace jit
695 } // namespace gcc