Update ChangeLog and version files for release
[official-gcc.git] / gcc / jit / jit-builtins.c
blobb516701ae25a142a24dee36a77e2678553d014ea
1 /* jit-builtins.c -- Handling of builtin functions during JIT-compilation.
2 Copyright (C) 2014-2016 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;
71 gcc_assert (bd.name);
73 if (debug)
74 fprintf (stderr, "seen builtin: %s\n", bd.name);
76 if (0 == strcmp (bd.name, in_name))
78 return true;
81 if (bd.both_p)
83 /* Then the macros in builtins.def gave a "__builtin_"
84 prefix to bd.name, but we should also recognize the form
85 without the prefix. */
86 gcc_assert (0 == strncmp (bd.name, prefix, prefix_len));
87 if (debug)
88 fprintf (stderr, "testing without prefix as: %s\n",
89 bd.name + prefix_len);
90 if (0 == strcmp (bd.name + prefix_len, in_name))
92 return true;
96 return false;
99 /* Locate the built-in function that matches name IN_NAME,
100 writing the result to OUT_ID and returning true if found,
101 or returning false if not found. */
103 static bool
104 find_builtin_by_name (const char *in_name,
105 enum built_in_function *out_id)
107 /* Locate builtin. This currently works by performing repeated
108 strcmp against every possible candidate, which is likely to
109 inefficient.
111 We start at index 1 to skip the initial entry (BUILT_IN_NONE), which
112 has a NULL name. */
113 for (unsigned int i = 1;
114 i < sizeof (builtin_data) / sizeof (builtin_data[0]);
115 i++)
117 const struct builtin_data& bd = builtin_data[i];
118 if (matches_builtin (in_name, bd))
120 /* Found a match. */
121 *out_id = static_cast<enum built_in_function> (i);
122 return true;
126 /* Not found. */
127 return false;
130 // class builtins_manager
132 /* Constructor for gcc::jit::builtins_manager. */
134 builtins_manager::builtins_manager (recording::context *ctxt)
135 : m_ctxt (ctxt)
137 memset (m_types, 0, sizeof (m_types));
138 memset (m_builtin_functions, 0, sizeof (m_builtin_functions));
139 memset (m_attributes, 0, sizeof (m_attributes));
142 /* Locate a builtin function by name.
143 Create a recording::function of the appropriate type, reusing them
144 if they've already been seen. */
146 recording::function *
147 builtins_manager::get_builtin_function (const char *name)
149 enum built_in_function builtin_id;
150 if (!find_builtin_by_name (name, &builtin_id))
152 m_ctxt->add_error (NULL, "builtin \"%s\" not found", name);
153 return NULL;
156 return get_builtin_function_by_id (builtin_id);
159 /* Locate a builtin function by id.
160 Create a recording::function of the appropriate type, reusing them
161 if they've already been seen. */
163 recording::function *
164 builtins_manager::get_builtin_function_by_id (enum built_in_function builtin_id)
166 gcc_assert (builtin_id >= 0);
167 gcc_assert (builtin_id < END_BUILTINS);
169 /* Lazily build the functions, caching them so that repeated calls for
170 the same id on a context give back the same object. */
171 if (!m_builtin_functions[builtin_id])
173 recording::function *fn = make_builtin_function (builtin_id);
174 if (fn)
176 m_builtin_functions[builtin_id] = fn;
177 m_ctxt->record (fn);
181 return m_builtin_functions[builtin_id];
184 /* Create the recording::function for a given builtin function, by ID. */
186 recording::function *
187 builtins_manager::make_builtin_function (enum built_in_function builtin_id)
189 const struct builtin_data& bd = builtin_data[builtin_id];
190 enum jit_builtin_type type_id = bd.type;
191 recording::type *t = get_type (type_id);
192 if (!t)
193 return NULL;
194 recording::function_type *func_type = t->as_a_function_type ();
195 if (!func_type)
196 return NULL;
198 vec<recording::type *> param_types = func_type->get_param_types ();
199 recording::param **params = new recording::param *[param_types.length ()];
201 int i;
202 recording::type *param_type;
203 FOR_EACH_VEC_ELT (param_types, i, param_type)
205 char buf[16];
206 snprintf (buf, 16, "arg%d", i);
207 params[i] = m_ctxt->new_param (NULL,
208 param_type,
209 buf);
211 const char *asm_name = bd.get_asm_name ();
212 recording::function *result =
213 new recording::function (m_ctxt,
214 NULL,
215 GCC_JIT_FUNCTION_IMPORTED, // FIXME
216 func_type->get_return_type (),
217 m_ctxt->new_string (asm_name),
218 param_types.length (),
219 params,
220 func_type->is_variadic (),
221 builtin_id);
222 delete[] params;
224 /* PR/64020 - If the client code is using builtin cos or sin,
225 tree-ssa-math-opt.c's execute_cse_sincos_1 may attempt
226 to optimize them to use __builtin_cexpi; for this,
227 BUILT_IN_CEXPI needs to exist.
229 Hence query the cache for BUILT_IN_CEXPI to ensure it gets
230 built. */
231 if (builtin_id == BUILT_IN_COS || builtin_id == BUILT_IN_SIN)
232 (void)get_builtin_function_by_id (BUILT_IN_CEXPI);
234 /* builtins.c:expand_builtin_cexpi can optimize the various
235 CEXP builtins to SINCOS builtins, and hence we may require
236 SINCOS builtins latter.
238 Ensure the appropriate SINCOS builtin exists. */
239 if (builtin_id == BUILT_IN_CEXPIF)
240 (void)get_builtin_function_by_id (BUILT_IN_SINCOSF);
241 else if (builtin_id == BUILT_IN_CEXPI)
242 (void)get_builtin_function_by_id (BUILT_IN_SINCOS);
243 else if (builtin_id == BUILT_IN_CEXPIL)
244 (void)get_builtin_function_by_id (BUILT_IN_SINCOSL);
246 return result;
249 /* Get the recording::type for a given type of builtin function,
250 by ID, creating it if it doesn't already exist. */
252 recording::type *
253 builtins_manager::get_type (enum jit_builtin_type type_id)
255 if (!m_types[type_id])
256 m_types[type_id] = make_type (type_id);
257 return m_types[type_id];
260 /* Create the recording::type for a given type of builtin function. */
262 recording::type *
263 builtins_manager::make_type (enum jit_builtin_type type_id)
265 /* Use builtin-types.def to construct a switch statement, with each
266 case deferring to one of the methods below:
267 - DEF_PRIMITIVE_TYPE is handled as a call to make_primitive_type.
268 - the various DEF_FUNCTION_TYPE_n are handled by variadic calls
269 to make_fn_type.
270 - similarly for DEF_FUNCTION_TYPE_VAR_n, but setting the
271 "is_variadic" argument.
272 - DEF_POINTER_TYPE is handled by make_ptr_type.
273 That should handle everything, but just in case we also suppy a
274 gcc_unreachable default clause. */
275 switch (type_id)
277 #define DEF_PRIMITIVE_TYPE(ENUM, VALUE) \
278 case ENUM: return make_primitive_type (ENUM);
279 #define DEF_FUNCTION_TYPE_0(ENUM, RETURN) \
280 case ENUM: return make_fn_type (ENUM, RETURN, 0, 0);
281 #define DEF_FUNCTION_TYPE_1(ENUM, RETURN, ARG1) \
282 case ENUM: return make_fn_type (ENUM, RETURN, 0, 1, ARG1);
283 #define DEF_FUNCTION_TYPE_2(ENUM, RETURN, ARG1, ARG2) \
284 case ENUM: return make_fn_type (ENUM, RETURN, 0, 2, ARG1, ARG2);
285 #define DEF_FUNCTION_TYPE_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
286 case ENUM: return make_fn_type (ENUM, RETURN, 0, 3, ARG1, ARG2, ARG3);
287 #define DEF_FUNCTION_TYPE_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
288 case ENUM: return make_fn_type (ENUM, RETURN, 0, 4, ARG1, ARG2, ARG3, \
289 ARG4);
290 #define DEF_FUNCTION_TYPE_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
291 case ENUM: return make_fn_type (ENUM, RETURN, 0, 5, ARG1, ARG2, ARG3, \
292 ARG4, ARG5);
293 #define DEF_FUNCTION_TYPE_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
294 ARG6) \
295 case ENUM: return make_fn_type (ENUM, RETURN, 0, 6, ARG1, ARG2, ARG3, \
296 ARG4, ARG5, ARG6);
297 #define DEF_FUNCTION_TYPE_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
298 ARG6, ARG7) \
299 case ENUM: return make_fn_type (ENUM, RETURN, 0, 7, ARG1, ARG2, ARG3, \
300 ARG4, ARG5, ARG6, ARG7);
301 #define DEF_FUNCTION_TYPE_8(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
302 ARG6, ARG7, ARG8) \
303 case ENUM: return make_fn_type (ENUM, RETURN, 0, 8, ARG1, ARG2, ARG3, \
304 ARG4, ARG5, ARG6, ARG7, ARG8);
305 #define DEF_FUNCTION_TYPE_9(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
306 ARG6, ARG7, ARG8, ARG9) \
307 case ENUM: return make_fn_type (ENUM, RETURN, 0, 9, ARG1, ARG2, ARG3, \
308 ARG4, ARG5, ARG6, ARG7, ARG8, ARG9);
309 #define DEF_FUNCTION_TYPE_10(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
310 ARG6, ARG7, ARG8, ARG9, ARG10) \
311 case ENUM: return make_fn_type (ENUM, RETURN, 0, 10, ARG1, ARG2, ARG3, \
312 ARG4, ARG5, ARG6, ARG7, ARG8, ARG9, \
313 ARG10);
314 #define DEF_FUNCTION_TYPE_11(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
315 ARG6, ARG7, ARG8, ARG9, ARG10, ARG11) \
316 case ENUM: return make_fn_type (ENUM, RETURN, 0, 11, ARG1, ARG2, ARG3, \
317 ARG4, ARG5, ARG6, ARG7, ARG8, ARG9, \
318 ARG10, ARG11);
319 #define DEF_FUNCTION_TYPE_VAR_0(ENUM, RETURN) \
320 case ENUM: return make_fn_type (ENUM, RETURN, 1, 0);
321 #define DEF_FUNCTION_TYPE_VAR_1(ENUM, RETURN, ARG1) \
322 case ENUM: return make_fn_type (ENUM, RETURN, 1, 1, ARG1);
323 #define DEF_FUNCTION_TYPE_VAR_2(ENUM, RETURN, ARG1, ARG2) \
324 case ENUM: return make_fn_type (ENUM, RETURN, 1, 2, ARG1, ARG2);
325 #define DEF_FUNCTION_TYPE_VAR_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
326 case ENUM: return make_fn_type (ENUM, RETURN, 1, 3, ARG1, ARG2, ARG3);
327 #define DEF_FUNCTION_TYPE_VAR_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
328 case ENUM: return make_fn_type (ENUM, RETURN, 1, 4, ARG1, ARG2, ARG3, \
329 ARG4);
330 #define DEF_FUNCTION_TYPE_VAR_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
331 case ENUM: return make_fn_type (ENUM, RETURN, 1, 5, ARG1, ARG2, ARG3, \
332 ARG4, ARG5);
333 #define DEF_FUNCTION_TYPE_VAR_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
334 ARG6) \
335 case ENUM: return make_fn_type (ENUM, RETURN, 1, 6, ARG1, ARG2, ARG3, \
336 ARG4, ARG5, ARG6);
337 #define DEF_FUNCTION_TYPE_VAR_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
338 ARG6, ARG7) \
339 case ENUM: return make_fn_type (ENUM, RETURN, 1, 7, ARG1, ARG2, ARG3, \
340 ARG4, ARG5, ARG6, ARG7);
341 #define DEF_POINTER_TYPE(ENUM, TYPE) \
342 case ENUM: return make_ptr_type (ENUM, TYPE);
344 #include "builtin-types.def"
346 #undef DEF_PRIMITIVE_TYPE
347 #undef DEF_FUNCTION_TYPE_0
348 #undef DEF_FUNCTION_TYPE_1
349 #undef DEF_FUNCTION_TYPE_2
350 #undef DEF_FUNCTION_TYPE_3
351 #undef DEF_FUNCTION_TYPE_4
352 #undef DEF_FUNCTION_TYPE_5
353 #undef DEF_FUNCTION_TYPE_6
354 #undef DEF_FUNCTION_TYPE_7
355 #undef DEF_FUNCTION_TYPE_8
356 #undef DEF_FUNCTION_TYPE_9
357 #undef DEF_FUNCTION_TYPE_10
358 #undef DEF_FUNCTION_TYPE_11
359 #undef DEF_FUNCTION_TYPE_VAR_0
360 #undef DEF_FUNCTION_TYPE_VAR_1
361 #undef DEF_FUNCTION_TYPE_VAR_2
362 #undef DEF_FUNCTION_TYPE_VAR_3
363 #undef DEF_FUNCTION_TYPE_VAR_4
364 #undef DEF_FUNCTION_TYPE_VAR_5
365 #undef DEF_FUNCTION_TYPE_VAR_6
366 #undef DEF_FUNCTION_TYPE_VAR_7
367 #undef DEF_POINTER_TYPE
369 default:
370 gcc_unreachable ();
374 /* Create the recording::type for a given primitive type within the
375 builtin system.
377 Only some types are currently supported. */
379 recording::type*
380 builtins_manager::make_primitive_type (enum jit_builtin_type type_id)
382 switch (type_id)
384 default:
385 // only some of these types are implemented so far:
386 m_ctxt->add_error (NULL,
387 "unimplemented primitive type for builtin: %d", type_id);
388 return NULL;
390 case BT_VOID: return m_ctxt->get_type (GCC_JIT_TYPE_VOID);
391 case BT_BOOL: return m_ctxt->get_type (GCC_JIT_TYPE_BOOL);
392 case BT_INT: return m_ctxt->get_type (GCC_JIT_TYPE_INT);
393 case BT_UINT: return m_ctxt->get_type (GCC_JIT_TYPE_UNSIGNED_INT);
394 case BT_LONG: return m_ctxt->get_type (GCC_JIT_TYPE_LONG);
395 case BT_ULONG: return m_ctxt->get_type (GCC_JIT_TYPE_UNSIGNED_LONG);
396 case BT_LONGLONG: return m_ctxt->get_type (GCC_JIT_TYPE_LONG_LONG);
397 case BT_ULONGLONG:
398 return m_ctxt->get_type (GCC_JIT_TYPE_UNSIGNED_LONG_LONG);
399 // case BT_INT128:
400 // case BT_UINT128:
401 // case BT_INTMAX:
402 // case BT_UINTMAX:
403 case BT_UINT16: return m_ctxt->get_int_type (2, false);
404 case BT_UINT32: return m_ctxt->get_int_type (4, false);
405 case BT_UINT64: return m_ctxt->get_int_type (8, false);
406 // case BT_WORD:
407 // case BT_UNWINDWORD:
408 case BT_FLOAT: return m_ctxt->get_type (GCC_JIT_TYPE_FLOAT);
409 case BT_DOUBLE: return m_ctxt->get_type (GCC_JIT_TYPE_DOUBLE);
410 case BT_LONGDOUBLE: return m_ctxt->get_type (GCC_JIT_TYPE_LONG_DOUBLE);
411 case BT_COMPLEX_FLOAT:
412 return m_ctxt->get_type (GCC_JIT_TYPE_COMPLEX_FLOAT);
413 case BT_COMPLEX_DOUBLE:
414 return m_ctxt->get_type (GCC_JIT_TYPE_COMPLEX_DOUBLE);
415 case BT_COMPLEX_LONGDOUBLE:
416 return m_ctxt->get_type (GCC_JIT_TYPE_COMPLEX_LONG_DOUBLE);
417 case BT_PTR: return m_ctxt->get_type (GCC_JIT_TYPE_VOID_PTR);
418 case BT_FILEPTR: return m_ctxt->get_type (GCC_JIT_TYPE_FILE_PTR);
419 // case BT_CONST:
420 // case BT_VOLATILE_PTR:
421 // case BT_CONST_VOLATILE_PTR:
422 // case BT_PTRMODE:
423 // case BT_INT_PTR:
424 // case BT_FLOAT_PTR:
425 case BT_DOUBLE_PTR:
426 return m_ctxt->get_type (GCC_JIT_TYPE_DOUBLE)->get_pointer ();
427 // case BT_CONST_DOUBLE_PTR:
428 // case BT_LONGDOUBLE_PTR:
429 // case BT_PID:
430 // case BT_SIZE:
431 // case BT_SSIZE:
432 // case BT_WINT:
433 // case BT_STRING:
434 case BT_CONST_STRING: return m_ctxt->get_type (GCC_JIT_TYPE_CONST_CHAR_PTR);
435 // case BT_DFLOAT32:
436 // case BT_DFLOAT64:
437 // case BT_DFLOAT128:
438 // case BT_DFLOAT32_PTR:
439 // case BT_DFLOAT64_PTR:
440 // case BT_DFLOAT128_PTR:
441 // case BT_VALIST_REF:
442 // case BT_VALIST_ARG:
443 // case BT_I1:
444 // case BT_I2:
445 // case BT_I4:
446 // case BT_I8:
447 // case BT_I16:
451 /* Create the recording::function_type for a given function type
452 signature. */
454 recording::function_type *
455 builtins_manager::make_fn_type (enum jit_builtin_type,
456 enum jit_builtin_type return_type_id,
457 bool is_variadic,
458 int num_args, ...)
460 va_list list;
461 int i;
462 recording::type **param_types = new recording::type *[num_args];
463 recording::type *return_type = NULL;
464 recording::function_type *result = NULL;
466 va_start (list, num_args);
467 for (i = 0; i < num_args; ++i)
469 enum jit_builtin_type arg_type_id =
470 (enum jit_builtin_type) va_arg (list, int);
471 param_types[i] = get_type (arg_type_id);
472 if (!param_types[i])
473 goto error;
475 va_end (list);
477 return_type = get_type (return_type_id);
478 if (!return_type)
479 goto error;
481 result = m_ctxt->new_function_type (return_type,
482 num_args,
483 param_types,
484 is_variadic);
486 error:
487 delete[] param_types;
488 return result;
491 /* Handler for DEF_POINTER_TYPE within builtins_manager::make_type. */
493 recording::type *
494 builtins_manager::make_ptr_type (enum jit_builtin_type,
495 enum jit_builtin_type other_type_id)
497 recording::type *base_type = get_type (other_type_id);
498 return base_type->get_pointer ();
501 /* Playback support. */
503 /* A builtins_manager is associated with a recording::context
504 and might be reused for multiple compiles on various
505 playback::contexts, perhaps with different options.
507 Purge any playback state. Currently this is just the table of
508 attributes. */
510 void
511 builtins_manager::finish_playback (void)
513 memset (m_attributes, 0, sizeof (m_attributes));
516 /* Get the enum built_in_class for BUILTIN_ID. */
518 enum built_in_class
519 builtins_manager::get_class (enum built_in_function builtin_id)
521 return builtin_data[builtin_id].fnclass;
524 /* Is BUILTIN_ID implicit? */
526 bool
527 builtins_manager::implicit_p (enum built_in_function builtin_id)
529 return builtin_data[builtin_id].implicit_p;
532 /* Get any attributes (in tree form) for the function declaration
533 for BUILTIN_ID.
535 These are created on-demand, and cached within the m_attributes
536 array, until finish_playback. */
538 tree
539 builtins_manager::get_attrs_tree (enum built_in_function builtin_id)
541 enum built_in_attribute attr = builtin_data[builtin_id].attr;
542 return get_attrs_tree (attr);
545 /* As above, but for an enum built_in_attribute. */
547 tree
548 builtins_manager::get_attrs_tree (enum built_in_attribute attr)
550 gcc_assert (attr < ATTR_LAST);
551 if (!m_attributes [attr])
552 m_attributes [attr] = make_attrs_tree (attr);
553 return m_attributes [attr];
556 /* Handle a cache-miss within the m_attributes array by
557 generating the attributes for enum built_in_attribute
558 in tree form. */
560 tree
561 builtins_manager::make_attrs_tree (enum built_in_attribute attr)
563 switch (attr)
565 /* Generate cases from builtin-attrs.def. */
566 #define DEF_ATTR_NULL_TREE(ENUM) \
567 case ENUM: return NULL_TREE;
568 #define DEF_ATTR_INT(ENUM, VALUE) \
569 case ENUM: return build_int_cst (integer_type_node, VALUE);
570 #define DEF_ATTR_STRING(ENUM, VALUE) \
571 case ENUM: return build_string (strlen (VALUE), VALUE);
572 #define DEF_ATTR_IDENT(ENUM, STRING) \
573 case ENUM: return get_identifier (STRING);
574 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) \
575 case ENUM: return tree_cons (get_attrs_tree (PURPOSE), \
576 get_attrs_tree (VALUE), \
577 get_attrs_tree (CHAIN));
578 #include "builtin-attrs.def"
579 #undef DEF_ATTR_NULL_TREE
580 #undef DEF_ATTR_INT
581 #undef DEF_ATTR_IDENT
582 #undef DEF_ATTR_TREE_LIST
584 default:
585 /* We somehow got a value not covered by the autogenerated
586 cases. */
587 gcc_unreachable ();
588 return NULL;
592 } // namespace jit
593 } // namespace gcc