gcc/
[official-gcc.git] / gcc / jit / jit-builtins.c
blobeabf4e4d04a291153e3e17c3075b54c5b6d1d21f
1 /* jit-builtins.c -- Handling of builtin functions during JIT-compilation.
2 Copyright (C) 2014 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 "opts.h"
24 #include "tree.h"
25 #include "target.h"
26 #include "stringpool.h"
28 #include "jit-common.h"
29 #include "jit-builtins.h"
30 #include "jit-recording.h"
31 #include "jit-playback.h"
33 namespace gcc {
35 namespace jit {
37 const char *const prefix = "__builtin_";
38 const size_t prefix_len = strlen (prefix);
40 /* Create "builtin_data", a const table of the data within builtins.def. */
41 struct builtin_data
43 const char *name;
44 enum built_in_class fnclass;
45 enum jit_builtin_type type;
46 bool both_p;
47 bool fallback_p;
48 enum built_in_attribute attr;
49 bool implicit_p;
51 const char *get_asm_name () const
53 if (both_p && fallback_p)
54 return name + prefix_len;
55 else
56 return name;
60 #define DEF_BUILTIN(X, NAME, CLASS, TYPE, LT, BOTH_P, FALLBACK_P, \
61 NONANSI_P, ATTRS, IMPLICIT, COND) \
62 {NAME, CLASS, TYPE, BOTH_P, FALLBACK_P, ATTRS, IMPLICIT},
63 static const struct builtin_data builtin_data[] =
65 #include "builtins.def"
67 #undef DEF_BUILTIN
69 /* Helper function for find_builtin_by_name. */
71 static bool
72 matches_builtin (const char *in_name,
73 const struct builtin_data& bd)
75 const bool debug = 0;
76 gcc_assert (bd.name);
78 if (debug)
79 fprintf (stderr, "seen builtin: %s\n", bd.name);
81 if (0 == strcmp (bd.name, in_name))
83 return true;
86 if (bd.both_p)
88 /* Then the macros in builtins.def gave a "__builtin_"
89 prefix to bd.name, but we should also recognize the form
90 without the prefix. */
91 gcc_assert (0 == strncmp (bd.name, prefix, prefix_len));
92 if (debug)
93 fprintf (stderr, "testing without prefix as: %s\n",
94 bd.name + prefix_len);
95 if (0 == strcmp (bd.name + prefix_len, in_name))
97 return true;
101 return false;
104 /* Locate the built-in function that matches name IN_NAME,
105 writing the result to OUT_ID and returning true if found,
106 or returning false if not found. */
108 static bool
109 find_builtin_by_name (const char *in_name,
110 enum built_in_function *out_id)
112 /* Locate builtin. This currently works by performing repeated
113 strcmp against every possible candidate, which is likely to
114 inefficient.
116 We start at index 1 to skip the initial entry (BUILT_IN_NONE), which
117 has a NULL name. */
118 for (unsigned int i = 1;
119 i < sizeof (builtin_data) / sizeof (builtin_data[0]);
120 i++)
122 const struct builtin_data& bd = builtin_data[i];
123 if (matches_builtin (in_name, bd))
125 /* Found a match. */
126 *out_id = static_cast<enum built_in_function> (i);
127 return true;
131 /* Not found. */
132 return false;
135 // class builtins_manager
137 /* Constructor for gcc::jit::builtins_manager. */
139 builtins_manager::builtins_manager (recording::context *ctxt)
140 : m_ctxt (ctxt)
142 memset (m_types, 0, sizeof (m_types));
143 memset (m_builtin_functions, 0, sizeof (m_builtin_functions));
144 memset (m_attributes, 0, sizeof (m_attributes));
147 /* Locate a builtin function by name.
148 Create a recording::function of the appropriate type, reusing them
149 if they've already been seen. */
151 recording::function *
152 builtins_manager::get_builtin_function (const char *name)
154 enum built_in_function builtin_id;
155 if (!find_builtin_by_name (name, &builtin_id))
157 m_ctxt->add_error (NULL, "builtin \"%s\" not found", name);
158 return NULL;
161 return get_builtin_function_by_id (builtin_id);
164 /* Locate a builtin function by id.
165 Create a recording::function of the appropriate type, reusing them
166 if they've already been seen. */
168 recording::function *
169 builtins_manager::get_builtin_function_by_id (enum built_in_function builtin_id)
171 gcc_assert (builtin_id >= 0);
172 gcc_assert (builtin_id < END_BUILTINS);
174 /* Lazily build the functions, caching them so that repeated calls for
175 the same id on a context give back the same object. */
176 if (!m_builtin_functions[builtin_id])
178 recording::function *fn = make_builtin_function (builtin_id);
179 if (fn)
181 m_builtin_functions[builtin_id] = fn;
182 m_ctxt->record (fn);
186 return m_builtin_functions[builtin_id];
189 /* Create the recording::function for a given builtin function, by ID. */
191 recording::function *
192 builtins_manager::make_builtin_function (enum built_in_function builtin_id)
194 const struct builtin_data& bd = builtin_data[builtin_id];
195 enum jit_builtin_type type_id = bd.type;
196 recording::type *t = get_type (type_id);
197 if (!t)
198 return NULL;
199 recording::function_type *func_type = t->as_a_function_type ();
200 if (!func_type)
201 return NULL;
203 vec<recording::type *> param_types = func_type->get_param_types ();
204 recording::param **params = new recording::param *[param_types.length ()];
206 int i;
207 recording::type *param_type;
208 FOR_EACH_VEC_ELT (param_types, i, param_type)
210 char buf[16];
211 snprintf (buf, 16, "arg%d", i);
212 params[i] = m_ctxt->new_param (NULL,
213 param_type,
214 buf);
216 const char *asm_name = bd.get_asm_name ();
217 recording::function *result =
218 new recording::function (m_ctxt,
219 NULL,
220 GCC_JIT_FUNCTION_IMPORTED, // FIXME
221 func_type->get_return_type (),
222 m_ctxt->new_string (asm_name),
223 param_types.length (),
224 params,
225 func_type->is_variadic (),
226 builtin_id);
227 delete[] params;
229 /* PR/64020 - If the client code is using builtin cos or sin,
230 tree-ssa-math-opt.c's execute_cse_sincos_1 may attempt
231 to optimize them to use __builtin_cexpi; for this,
232 BUILT_IN_CEXPI needs to exist.
234 Hence query the cache for BUILT_IN_CEXPI to ensure it gets
235 built. */
236 if (builtin_id == BUILT_IN_COS || builtin_id == BUILT_IN_SIN)
237 (void)get_builtin_function_by_id (BUILT_IN_CEXPI);
239 /* builtins.c:expand_builtin_cexpi can optimize the various
240 CEXP builtins to SINCOS builtins, and hence we may require
241 SINCOS builtins latter.
243 Ensure the appropriate SINCOS builtin exists. */
244 if (builtin_id == BUILT_IN_CEXPIF)
245 (void)get_builtin_function_by_id (BUILT_IN_SINCOSF);
246 else if (builtin_id == BUILT_IN_CEXPI)
247 (void)get_builtin_function_by_id (BUILT_IN_SINCOS);
248 else if (builtin_id == BUILT_IN_CEXPIL)
249 (void)get_builtin_function_by_id (BUILT_IN_SINCOSL);
251 return result;
254 /* Get the recording::type for a given type of builtin function,
255 by ID, creating it if it doesn't already exist. */
257 recording::type *
258 builtins_manager::get_type (enum jit_builtin_type type_id)
260 if (!m_types[type_id])
261 m_types[type_id] = make_type (type_id);
262 return m_types[type_id];
265 /* Create the recording::type for a given type of builtin function. */
267 recording::type *
268 builtins_manager::make_type (enum jit_builtin_type type_id)
270 /* Use builtin-types.def to construct a switch statement, with each
271 case deferring to one of the methods below:
272 - DEF_PRIMITIVE_TYPE is handled as a call to make_primitive_type.
273 - the various DEF_FUNCTION_TYPE_n are handled by variadic calls
274 to make_fn_type.
275 - similarly for DEF_FUNCTION_TYPE_VAR_n, but setting the
276 "is_variadic" argument.
277 - DEF_POINTER_TYPE is handled by make_ptr_type.
278 That should handle everything, but just in case we also suppy a
279 gcc_unreachable default clause. */
280 switch (type_id)
282 #define DEF_PRIMITIVE_TYPE(ENUM, VALUE) \
283 case ENUM: return make_primitive_type (ENUM);
284 #define DEF_FUNCTION_TYPE_0(ENUM, RETURN) \
285 case ENUM: return make_fn_type (ENUM, RETURN, 0, 0);
286 #define DEF_FUNCTION_TYPE_1(ENUM, RETURN, ARG1) \
287 case ENUM: return make_fn_type (ENUM, RETURN, 0, 1, ARG1);
288 #define DEF_FUNCTION_TYPE_2(ENUM, RETURN, ARG1, ARG2) \
289 case ENUM: return make_fn_type (ENUM, RETURN, 0, 2, ARG1, ARG2);
290 #define DEF_FUNCTION_TYPE_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
291 case ENUM: return make_fn_type (ENUM, RETURN, 0, 3, ARG1, ARG2, ARG3);
292 #define DEF_FUNCTION_TYPE_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
293 case ENUM: return make_fn_type (ENUM, RETURN, 0, 4, ARG1, ARG2, ARG3, ARG4);
294 #define DEF_FUNCTION_TYPE_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
295 case ENUM: return make_fn_type (ENUM, RETURN, 0, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
296 #define DEF_FUNCTION_TYPE_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
297 ARG6) \
298 case ENUM: return make_fn_type (ENUM, RETURN, 0, 6, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6);
299 #define DEF_FUNCTION_TYPE_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
300 ARG6, ARG7) \
301 case ENUM: return make_fn_type (ENUM, RETURN, 0, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
302 #define DEF_FUNCTION_TYPE_8(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
303 ARG6, ARG7, ARG8) \
304 case ENUM: return make_fn_type (ENUM, RETURN, 0, 8, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
305 ARG7, ARG8);
306 #define DEF_FUNCTION_TYPE_VAR_0(ENUM, RETURN) \
307 case ENUM: return make_fn_type (ENUM, RETURN, 1, 0);
308 #define DEF_FUNCTION_TYPE_VAR_1(ENUM, RETURN, ARG1) \
309 case ENUM: return make_fn_type (ENUM, RETURN, 1, 1, ARG1);
310 #define DEF_FUNCTION_TYPE_VAR_2(ENUM, RETURN, ARG1, ARG2) \
311 case ENUM: return make_fn_type (ENUM, RETURN, 1, 2, ARG1, ARG2);
312 #define DEF_FUNCTION_TYPE_VAR_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
313 case ENUM: return make_fn_type (ENUM, RETURN, 1, 3, ARG1, ARG2, ARG3);
314 #define DEF_FUNCTION_TYPE_VAR_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
315 case ENUM: return make_fn_type (ENUM, RETURN, 1, 4, ARG1, ARG2, ARG3, ARG4);
316 #define DEF_FUNCTION_TYPE_VAR_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
317 case ENUM: return make_fn_type (ENUM, RETURN, 1, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
318 #define DEF_POINTER_TYPE(ENUM, TYPE) \
319 case ENUM: return make_ptr_type (ENUM, TYPE);
321 #include "builtin-types.def"
323 #undef DEF_PRIMITIVE_TYPE
324 #undef DEF_FUNCTION_TYPE_0
325 #undef DEF_FUNCTION_TYPE_1
326 #undef DEF_FUNCTION_TYPE_2
327 #undef DEF_FUNCTION_TYPE_3
328 #undef DEF_FUNCTION_TYPE_4
329 #undef DEF_FUNCTION_TYPE_5
330 #undef DEF_FUNCTION_TYPE_6
331 #undef DEF_FUNCTION_TYPE_7
332 #undef DEF_FUNCTION_TYPE_8
333 #undef DEF_FUNCTION_TYPE_VAR_0
334 #undef DEF_FUNCTION_TYPE_VAR_1
335 #undef DEF_FUNCTION_TYPE_VAR_2
336 #undef DEF_FUNCTION_TYPE_VAR_3
337 #undef DEF_FUNCTION_TYPE_VAR_4
338 #undef DEF_FUNCTION_TYPE_VAR_5
339 #undef DEF_POINTER_TYPE
341 default:
342 gcc_unreachable ();
346 /* Create the recording::type for a given primitive type within the
347 builtin system.
349 Only some types are currently supported. */
351 recording::type*
352 builtins_manager::make_primitive_type (enum jit_builtin_type type_id)
354 switch (type_id)
356 default:
357 // only some of these types are implemented so far:
358 m_ctxt->add_error (NULL,
359 "unimplemented primitive type for builtin: %d", type_id);
360 return NULL;
362 case BT_VOID: return m_ctxt->get_type (GCC_JIT_TYPE_VOID);
363 case BT_BOOL: return m_ctxt->get_type (GCC_JIT_TYPE_BOOL);
364 case BT_INT: return m_ctxt->get_type (GCC_JIT_TYPE_INT);
365 case BT_UINT: return m_ctxt->get_type (GCC_JIT_TYPE_UNSIGNED_INT);
366 case BT_LONG: return m_ctxt->get_type (GCC_JIT_TYPE_LONG);
367 case BT_ULONG: return m_ctxt->get_type (GCC_JIT_TYPE_UNSIGNED_LONG);
368 case BT_LONGLONG: return m_ctxt->get_type (GCC_JIT_TYPE_LONG_LONG);
369 case BT_ULONGLONG:
370 return m_ctxt->get_type (GCC_JIT_TYPE_UNSIGNED_LONG_LONG);
371 // case BT_INT128:
372 // case BT_UINT128:
373 // case BT_INTMAX:
374 // case BT_UINTMAX:
375 case BT_UINT16: return m_ctxt->get_int_type (2, false);
376 case BT_UINT32: return m_ctxt->get_int_type (4, false);
377 case BT_UINT64: return m_ctxt->get_int_type (8, false);
378 // case BT_WORD:
379 // case BT_UNWINDWORD:
380 case BT_FLOAT: return m_ctxt->get_type (GCC_JIT_TYPE_FLOAT);
381 case BT_DOUBLE: return m_ctxt->get_type (GCC_JIT_TYPE_DOUBLE);
382 case BT_LONGDOUBLE: return m_ctxt->get_type (GCC_JIT_TYPE_LONG_DOUBLE);
383 case BT_COMPLEX_FLOAT:
384 return m_ctxt->get_type (GCC_JIT_TYPE_COMPLEX_FLOAT);
385 case BT_COMPLEX_DOUBLE:
386 return m_ctxt->get_type (GCC_JIT_TYPE_COMPLEX_DOUBLE);
387 case BT_COMPLEX_LONGDOUBLE:
388 return m_ctxt->get_type (GCC_JIT_TYPE_COMPLEX_LONG_DOUBLE);
389 case BT_PTR: return m_ctxt->get_type (GCC_JIT_TYPE_VOID_PTR);
390 case BT_FILEPTR: return m_ctxt->get_type (GCC_JIT_TYPE_FILE_PTR);
391 // case BT_CONST:
392 // case BT_VOLATILE_PTR:
393 // case BT_CONST_VOLATILE_PTR:
394 // case BT_PTRMODE:
395 // case BT_INT_PTR:
396 // case BT_FLOAT_PTR:
397 case BT_DOUBLE_PTR:
398 return m_ctxt->get_type (GCC_JIT_TYPE_DOUBLE)->get_pointer ();
399 // case BT_CONST_DOUBLE_PTR:
400 // case BT_LONGDOUBLE_PTR:
401 // case BT_PID:
402 // case BT_SIZE:
403 // case BT_SSIZE:
404 // case BT_WINT:
405 // case BT_STRING:
406 case BT_CONST_STRING: return m_ctxt->get_type (GCC_JIT_TYPE_CONST_CHAR_PTR);
407 // case BT_DFLOAT32:
408 // case BT_DFLOAT64:
409 // case BT_DFLOAT128:
410 // case BT_DFLOAT32_PTR:
411 // case BT_DFLOAT64_PTR:
412 // case BT_DFLOAT128_PTR:
413 // case BT_VALIST_REF:
414 // case BT_VALIST_ARG:
415 // case BT_I1:
416 // case BT_I2:
417 // case BT_I4:
418 // case BT_I8:
419 // case BT_I16:
423 /* Create the recording::function_type for a given function type
424 signature. */
426 recording::function_type *
427 builtins_manager::make_fn_type (enum jit_builtin_type,
428 enum jit_builtin_type return_type_id,
429 bool is_variadic,
430 int num_args, ...)
432 va_list list;
433 int i;
434 recording::type **param_types = new recording::type *[num_args];
435 recording::type *return_type = NULL;
436 recording::function_type *result = NULL;
438 va_start (list, num_args);
439 for (i = 0; i < num_args; ++i)
441 enum jit_builtin_type arg_type_id =
442 (enum jit_builtin_type) va_arg (list, int);
443 param_types[i] = get_type (arg_type_id);
444 if (!param_types[i])
445 goto error;
447 va_end (list);
449 return_type = get_type (return_type_id);
450 if (!return_type)
451 goto error;
453 result = m_ctxt->new_function_type (return_type,
454 num_args,
455 param_types,
456 is_variadic);
458 error:
459 delete[] param_types;
460 return result;
463 /* Handler for DEF_POINTER_TYPE within builtins_manager::make_type. */
465 recording::type *
466 builtins_manager::make_ptr_type (enum jit_builtin_type,
467 enum jit_builtin_type other_type_id)
469 recording::type *base_type = get_type (other_type_id);
470 return base_type->get_pointer ();
473 /* Playback support. */
475 /* A builtins_manager is associated with a recording::context
476 and might be reused for multiple compiles on various
477 playback::contexts, perhaps with different options.
479 Purge any playback state. Currently this is just the table of
480 attributes. */
482 void
483 builtins_manager::finish_playback (void)
485 memset (m_attributes, 0, sizeof (m_attributes));
488 /* Get the enum built_in_class for BUILTIN_ID. */
490 enum built_in_class
491 builtins_manager::get_class (enum built_in_function builtin_id)
493 return builtin_data[builtin_id].fnclass;
496 /* Is BUILTIN_ID implicit? */
498 bool
499 builtins_manager::implicit_p (enum built_in_function builtin_id)
501 return builtin_data[builtin_id].implicit_p;
504 /* Get any attributes (in tree form) for the function declaration
505 for BUILTIN_ID.
507 These are created on-demand, and cached within the m_attributes
508 array, until finish_playback. */
510 tree
511 builtins_manager::get_attrs_tree (enum built_in_function builtin_id)
513 enum built_in_attribute attr = builtin_data[builtin_id].attr;
514 return get_attrs_tree (attr);
517 /* As above, but for an enum built_in_attribute. */
519 tree
520 builtins_manager::get_attrs_tree (enum built_in_attribute attr)
522 gcc_assert (attr < ATTR_LAST);
523 if (!m_attributes [attr])
524 m_attributes [attr] = make_attrs_tree (attr);
525 return m_attributes [attr];
528 /* Handle a cache-miss within the m_attributes array by
529 generating the attributes for enum built_in_attribute
530 in tree form. */
532 tree
533 builtins_manager::make_attrs_tree (enum built_in_attribute attr)
535 switch (attr)
537 /* Generate cases from builtin-attrs.def. */
538 #define DEF_ATTR_NULL_TREE(ENUM) \
539 case ENUM: return NULL_TREE;
540 #define DEF_ATTR_INT(ENUM, VALUE) \
541 case ENUM: return build_int_cst (integer_type_node, VALUE);
542 #define DEF_ATTR_STRING(ENUM, VALUE) \
543 case ENUM: return build_string (strlen (VALUE), VALUE);
544 #define DEF_ATTR_IDENT(ENUM, STRING) \
545 case ENUM: return get_identifier (STRING);
546 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) \
547 case ENUM: return tree_cons (get_attrs_tree (PURPOSE), \
548 get_attrs_tree (VALUE), \
549 get_attrs_tree (CHAIN));
550 #include "builtin-attrs.def"
551 #undef DEF_ATTR_NULL_TREE
552 #undef DEF_ATTR_INT
553 #undef DEF_ATTR_IDENT
554 #undef DEF_ATTR_TREE_LIST
556 default:
557 /* We somehow got a value not covered by the autogenerated
558 cases. */
559 gcc_unreachable ();
560 return NULL;
564 } // namespace jit
565 } // namespace gcc