2015-09-30 Matthias Klose <doko@ubuntu.com>
[official-gcc.git] / gcc / jit / jit-builtins.c
blob4ef80edc4528a40a951551c0e05f6e0aa782fb51
1 /* jit-builtins.c -- Handling of builtin functions during JIT-compilation.
2 Copyright (C) 2014-2015 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 "vec.h"
24 #include "target.h"
25 #include "stringpool.h"
27 #include "jit-common.h"
28 #include "jit-builtins.h"
29 #include "jit-recording.h"
30 #include "jit-playback.h"
32 namespace gcc {
34 namespace jit {
36 const char *const prefix = "__builtin_";
37 const size_t prefix_len = strlen (prefix);
39 /* Create "builtin_data", a const table of the data within builtins.def. */
40 struct builtin_data
42 const char *name;
43 enum built_in_class fnclass;
44 enum jit_builtin_type type;
45 bool both_p;
46 bool fallback_p;
47 enum built_in_attribute attr;
48 bool implicit_p;
50 const char *get_asm_name () const
52 if (both_p && fallback_p)
53 return name + prefix_len;
54 else
55 return name;
59 #define DEF_BUILTIN(X, NAME, CLASS, TYPE, LT, BOTH_P, FALLBACK_P, \
60 NONANSI_P, ATTRS, IMPLICIT, COND) \
61 {NAME, CLASS, TYPE, BOTH_P, FALLBACK_P, ATTRS, IMPLICIT},
62 static const struct builtin_data builtin_data[] =
64 #include "builtins.def"
66 #undef DEF_BUILTIN
68 /* Helper function for find_builtin_by_name. */
70 static bool
71 matches_builtin (const char *in_name,
72 const struct builtin_data& bd)
74 const bool debug = 0;
75 gcc_assert (bd.name);
77 if (debug)
78 fprintf (stderr, "seen builtin: %s\n", bd.name);
80 if (0 == strcmp (bd.name, in_name))
82 return true;
85 if (bd.both_p)
87 /* Then the macros in builtins.def gave a "__builtin_"
88 prefix to bd.name, but we should also recognize the form
89 without the prefix. */
90 gcc_assert (0 == strncmp (bd.name, prefix, prefix_len));
91 if (debug)
92 fprintf (stderr, "testing without prefix as: %s\n",
93 bd.name + prefix_len);
94 if (0 == strcmp (bd.name + prefix_len, in_name))
96 return true;
100 return false;
103 /* Locate the built-in function that matches name IN_NAME,
104 writing the result to OUT_ID and returning true if found,
105 or returning false if not found. */
107 static bool
108 find_builtin_by_name (const char *in_name,
109 enum built_in_function *out_id)
111 /* Locate builtin. This currently works by performing repeated
112 strcmp against every possible candidate, which is likely to
113 inefficient.
115 We start at index 1 to skip the initial entry (BUILT_IN_NONE), which
116 has a NULL name. */
117 for (unsigned int i = 1;
118 i < sizeof (builtin_data) / sizeof (builtin_data[0]);
119 i++)
121 const struct builtin_data& bd = builtin_data[i];
122 if (matches_builtin (in_name, bd))
124 /* Found a match. */
125 *out_id = static_cast<enum built_in_function> (i);
126 return true;
130 /* Not found. */
131 return false;
134 // class builtins_manager
136 /* Constructor for gcc::jit::builtins_manager. */
138 builtins_manager::builtins_manager (recording::context *ctxt)
139 : m_ctxt (ctxt)
141 memset (m_types, 0, sizeof (m_types));
142 memset (m_builtin_functions, 0, sizeof (m_builtin_functions));
143 memset (m_attributes, 0, sizeof (m_attributes));
146 /* Locate a builtin function by name.
147 Create a recording::function of the appropriate type, reusing them
148 if they've already been seen. */
150 recording::function *
151 builtins_manager::get_builtin_function (const char *name)
153 enum built_in_function builtin_id;
154 if (!find_builtin_by_name (name, &builtin_id))
156 m_ctxt->add_error (NULL, "builtin \"%s\" not found", name);
157 return NULL;
160 return get_builtin_function_by_id (builtin_id);
163 /* Locate a builtin function by id.
164 Create a recording::function of the appropriate type, reusing them
165 if they've already been seen. */
167 recording::function *
168 builtins_manager::get_builtin_function_by_id (enum built_in_function builtin_id)
170 gcc_assert (builtin_id >= 0);
171 gcc_assert (builtin_id < END_BUILTINS);
173 /* Lazily build the functions, caching them so that repeated calls for
174 the same id on a context give back the same object. */
175 if (!m_builtin_functions[builtin_id])
177 recording::function *fn = make_builtin_function (builtin_id);
178 if (fn)
180 m_builtin_functions[builtin_id] = fn;
181 m_ctxt->record (fn);
185 return m_builtin_functions[builtin_id];
188 /* Create the recording::function for a given builtin function, by ID. */
190 recording::function *
191 builtins_manager::make_builtin_function (enum built_in_function builtin_id)
193 const struct builtin_data& bd = builtin_data[builtin_id];
194 enum jit_builtin_type type_id = bd.type;
195 recording::type *t = get_type (type_id);
196 if (!t)
197 return NULL;
198 recording::function_type *func_type = t->as_a_function_type ();
199 if (!func_type)
200 return NULL;
202 vec<recording::type *> param_types = func_type->get_param_types ();
203 recording::param **params = new recording::param *[param_types.length ()];
205 int i;
206 recording::type *param_type;
207 FOR_EACH_VEC_ELT (param_types, i, param_type)
209 char buf[16];
210 snprintf (buf, 16, "arg%d", i);
211 params[i] = m_ctxt->new_param (NULL,
212 param_type,
213 buf);
215 const char *asm_name = bd.get_asm_name ();
216 recording::function *result =
217 new recording::function (m_ctxt,
218 NULL,
219 GCC_JIT_FUNCTION_IMPORTED, // FIXME
220 func_type->get_return_type (),
221 m_ctxt->new_string (asm_name),
222 param_types.length (),
223 params,
224 func_type->is_variadic (),
225 builtin_id);
226 delete[] params;
228 /* PR/64020 - If the client code is using builtin cos or sin,
229 tree-ssa-math-opt.c's execute_cse_sincos_1 may attempt
230 to optimize them to use __builtin_cexpi; for this,
231 BUILT_IN_CEXPI needs to exist.
233 Hence query the cache for BUILT_IN_CEXPI to ensure it gets
234 built. */
235 if (builtin_id == BUILT_IN_COS || builtin_id == BUILT_IN_SIN)
236 (void)get_builtin_function_by_id (BUILT_IN_CEXPI);
238 /* builtins.c:expand_builtin_cexpi can optimize the various
239 CEXP builtins to SINCOS builtins, and hence we may require
240 SINCOS builtins latter.
242 Ensure the appropriate SINCOS builtin exists. */
243 if (builtin_id == BUILT_IN_CEXPIF)
244 (void)get_builtin_function_by_id (BUILT_IN_SINCOSF);
245 else if (builtin_id == BUILT_IN_CEXPI)
246 (void)get_builtin_function_by_id (BUILT_IN_SINCOS);
247 else if (builtin_id == BUILT_IN_CEXPIL)
248 (void)get_builtin_function_by_id (BUILT_IN_SINCOSL);
250 return result;
253 /* Get the recording::type for a given type of builtin function,
254 by ID, creating it if it doesn't already exist. */
256 recording::type *
257 builtins_manager::get_type (enum jit_builtin_type type_id)
259 if (!m_types[type_id])
260 m_types[type_id] = make_type (type_id);
261 return m_types[type_id];
264 /* Create the recording::type for a given type of builtin function. */
266 recording::type *
267 builtins_manager::make_type (enum jit_builtin_type type_id)
269 /* Use builtin-types.def to construct a switch statement, with each
270 case deferring to one of the methods below:
271 - DEF_PRIMITIVE_TYPE is handled as a call to make_primitive_type.
272 - the various DEF_FUNCTION_TYPE_n are handled by variadic calls
273 to make_fn_type.
274 - similarly for DEF_FUNCTION_TYPE_VAR_n, but setting the
275 "is_variadic" argument.
276 - DEF_POINTER_TYPE is handled by make_ptr_type.
277 That should handle everything, but just in case we also suppy a
278 gcc_unreachable default clause. */
279 switch (type_id)
281 #define DEF_PRIMITIVE_TYPE(ENUM, VALUE) \
282 case ENUM: return make_primitive_type (ENUM);
283 #define DEF_FUNCTION_TYPE_0(ENUM, RETURN) \
284 case ENUM: return make_fn_type (ENUM, RETURN, 0, 0);
285 #define DEF_FUNCTION_TYPE_1(ENUM, RETURN, ARG1) \
286 case ENUM: return make_fn_type (ENUM, RETURN, 0, 1, ARG1);
287 #define DEF_FUNCTION_TYPE_2(ENUM, RETURN, ARG1, ARG2) \
288 case ENUM: return make_fn_type (ENUM, RETURN, 0, 2, ARG1, ARG2);
289 #define DEF_FUNCTION_TYPE_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
290 case ENUM: return make_fn_type (ENUM, RETURN, 0, 3, ARG1, ARG2, ARG3);
291 #define DEF_FUNCTION_TYPE_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
292 case ENUM: return make_fn_type (ENUM, RETURN, 0, 4, ARG1, ARG2, ARG3, \
293 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, \
296 ARG4, ARG5);
297 #define DEF_FUNCTION_TYPE_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
298 ARG6) \
299 case ENUM: return make_fn_type (ENUM, RETURN, 0, 6, ARG1, ARG2, ARG3, \
300 ARG4, ARG5, ARG6);
301 #define DEF_FUNCTION_TYPE_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
302 ARG6, ARG7) \
303 case ENUM: return make_fn_type (ENUM, RETURN, 0, 7, ARG1, ARG2, ARG3, \
304 ARG4, ARG5, ARG6, ARG7);
305 #define DEF_FUNCTION_TYPE_8(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
306 ARG6, ARG7, ARG8) \
307 case ENUM: return make_fn_type (ENUM, RETURN, 0, 8, ARG1, ARG2, ARG3, \
308 ARG4, ARG5, ARG6, ARG7, ARG8);
309 #define DEF_FUNCTION_TYPE_VAR_0(ENUM, RETURN) \
310 case ENUM: return make_fn_type (ENUM, RETURN, 1, 0);
311 #define DEF_FUNCTION_TYPE_VAR_1(ENUM, RETURN, ARG1) \
312 case ENUM: return make_fn_type (ENUM, RETURN, 1, 1, ARG1);
313 #define DEF_FUNCTION_TYPE_VAR_2(ENUM, RETURN, ARG1, ARG2) \
314 case ENUM: return make_fn_type (ENUM, RETURN, 1, 2, ARG1, ARG2);
315 #define DEF_FUNCTION_TYPE_VAR_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
316 case ENUM: return make_fn_type (ENUM, RETURN, 1, 3, ARG1, ARG2, ARG3);
317 #define DEF_FUNCTION_TYPE_VAR_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
318 case ENUM: return make_fn_type (ENUM, RETURN, 1, 4, ARG1, ARG2, ARG3, \
319 ARG4);
320 #define DEF_FUNCTION_TYPE_VAR_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
321 case ENUM: return make_fn_type (ENUM, RETURN, 1, 5, ARG1, ARG2, ARG3, \
322 ARG4, ARG5);
323 #define DEF_FUNCTION_TYPE_VAR_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
324 ARG6) \
325 case ENUM: return make_fn_type (ENUM, RETURN, 1, 6, ARG1, ARG2, ARG3, \
326 ARG4, ARG5, ARG6);
327 #define DEF_FUNCTION_TYPE_VAR_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
328 ARG6, ARG7) \
329 case ENUM: return make_fn_type (ENUM, RETURN, 1, 7, ARG1, ARG2, ARG3, \
330 ARG4, ARG5, ARG6, ARG7);
331 #define DEF_POINTER_TYPE(ENUM, TYPE) \
332 case ENUM: return make_ptr_type (ENUM, TYPE);
334 #include "builtin-types.def"
336 #undef DEF_PRIMITIVE_TYPE
337 #undef DEF_FUNCTION_TYPE_0
338 #undef DEF_FUNCTION_TYPE_1
339 #undef DEF_FUNCTION_TYPE_2
340 #undef DEF_FUNCTION_TYPE_3
341 #undef DEF_FUNCTION_TYPE_4
342 #undef DEF_FUNCTION_TYPE_5
343 #undef DEF_FUNCTION_TYPE_6
344 #undef DEF_FUNCTION_TYPE_7
345 #undef DEF_FUNCTION_TYPE_8
346 #undef DEF_FUNCTION_TYPE_VAR_0
347 #undef DEF_FUNCTION_TYPE_VAR_1
348 #undef DEF_FUNCTION_TYPE_VAR_2
349 #undef DEF_FUNCTION_TYPE_VAR_3
350 #undef DEF_FUNCTION_TYPE_VAR_4
351 #undef DEF_FUNCTION_TYPE_VAR_5
352 #undef DEF_FUNCTION_TYPE_VAR_6
353 #undef DEF_FUNCTION_TYPE_VAR_7
354 #undef DEF_POINTER_TYPE
356 default:
357 gcc_unreachable ();
361 /* Create the recording::type for a given primitive type within the
362 builtin system.
364 Only some types are currently supported. */
366 recording::type*
367 builtins_manager::make_primitive_type (enum jit_builtin_type type_id)
369 switch (type_id)
371 default:
372 // only some of these types are implemented so far:
373 m_ctxt->add_error (NULL,
374 "unimplemented primitive type for builtin: %d", type_id);
375 return NULL;
377 case BT_VOID: return m_ctxt->get_type (GCC_JIT_TYPE_VOID);
378 case BT_BOOL: return m_ctxt->get_type (GCC_JIT_TYPE_BOOL);
379 case BT_INT: return m_ctxt->get_type (GCC_JIT_TYPE_INT);
380 case BT_UINT: return m_ctxt->get_type (GCC_JIT_TYPE_UNSIGNED_INT);
381 case BT_LONG: return m_ctxt->get_type (GCC_JIT_TYPE_LONG);
382 case BT_ULONG: return m_ctxt->get_type (GCC_JIT_TYPE_UNSIGNED_LONG);
383 case BT_LONGLONG: return m_ctxt->get_type (GCC_JIT_TYPE_LONG_LONG);
384 case BT_ULONGLONG:
385 return m_ctxt->get_type (GCC_JIT_TYPE_UNSIGNED_LONG_LONG);
386 // case BT_INT128:
387 // case BT_UINT128:
388 // case BT_INTMAX:
389 // case BT_UINTMAX:
390 case BT_UINT16: return m_ctxt->get_int_type (2, false);
391 case BT_UINT32: return m_ctxt->get_int_type (4, false);
392 case BT_UINT64: return m_ctxt->get_int_type (8, false);
393 // case BT_WORD:
394 // case BT_UNWINDWORD:
395 case BT_FLOAT: return m_ctxt->get_type (GCC_JIT_TYPE_FLOAT);
396 case BT_DOUBLE: return m_ctxt->get_type (GCC_JIT_TYPE_DOUBLE);
397 case BT_LONGDOUBLE: return m_ctxt->get_type (GCC_JIT_TYPE_LONG_DOUBLE);
398 case BT_COMPLEX_FLOAT:
399 return m_ctxt->get_type (GCC_JIT_TYPE_COMPLEX_FLOAT);
400 case BT_COMPLEX_DOUBLE:
401 return m_ctxt->get_type (GCC_JIT_TYPE_COMPLEX_DOUBLE);
402 case BT_COMPLEX_LONGDOUBLE:
403 return m_ctxt->get_type (GCC_JIT_TYPE_COMPLEX_LONG_DOUBLE);
404 case BT_PTR: return m_ctxt->get_type (GCC_JIT_TYPE_VOID_PTR);
405 case BT_FILEPTR: return m_ctxt->get_type (GCC_JIT_TYPE_FILE_PTR);
406 // case BT_CONST:
407 // case BT_VOLATILE_PTR:
408 // case BT_CONST_VOLATILE_PTR:
409 // case BT_PTRMODE:
410 // case BT_INT_PTR:
411 // case BT_FLOAT_PTR:
412 case BT_DOUBLE_PTR:
413 return m_ctxt->get_type (GCC_JIT_TYPE_DOUBLE)->get_pointer ();
414 // case BT_CONST_DOUBLE_PTR:
415 // case BT_LONGDOUBLE_PTR:
416 // case BT_PID:
417 // case BT_SIZE:
418 // case BT_SSIZE:
419 // case BT_WINT:
420 // case BT_STRING:
421 case BT_CONST_STRING: return m_ctxt->get_type (GCC_JIT_TYPE_CONST_CHAR_PTR);
422 // case BT_DFLOAT32:
423 // case BT_DFLOAT64:
424 // case BT_DFLOAT128:
425 // case BT_DFLOAT32_PTR:
426 // case BT_DFLOAT64_PTR:
427 // case BT_DFLOAT128_PTR:
428 // case BT_VALIST_REF:
429 // case BT_VALIST_ARG:
430 // case BT_I1:
431 // case BT_I2:
432 // case BT_I4:
433 // case BT_I8:
434 // case BT_I16:
438 /* Create the recording::function_type for a given function type
439 signature. */
441 recording::function_type *
442 builtins_manager::make_fn_type (enum jit_builtin_type,
443 enum jit_builtin_type return_type_id,
444 bool is_variadic,
445 int num_args, ...)
447 va_list list;
448 int i;
449 recording::type **param_types = new recording::type *[num_args];
450 recording::type *return_type = NULL;
451 recording::function_type *result = NULL;
453 va_start (list, num_args);
454 for (i = 0; i < num_args; ++i)
456 enum jit_builtin_type arg_type_id =
457 (enum jit_builtin_type) va_arg (list, int);
458 param_types[i] = get_type (arg_type_id);
459 if (!param_types[i])
460 goto error;
462 va_end (list);
464 return_type = get_type (return_type_id);
465 if (!return_type)
466 goto error;
468 result = m_ctxt->new_function_type (return_type,
469 num_args,
470 param_types,
471 is_variadic);
473 error:
474 delete[] param_types;
475 return result;
478 /* Handler for DEF_POINTER_TYPE within builtins_manager::make_type. */
480 recording::type *
481 builtins_manager::make_ptr_type (enum jit_builtin_type,
482 enum jit_builtin_type other_type_id)
484 recording::type *base_type = get_type (other_type_id);
485 return base_type->get_pointer ();
488 /* Playback support. */
490 /* A builtins_manager is associated with a recording::context
491 and might be reused for multiple compiles on various
492 playback::contexts, perhaps with different options.
494 Purge any playback state. Currently this is just the table of
495 attributes. */
497 void
498 builtins_manager::finish_playback (void)
500 memset (m_attributes, 0, sizeof (m_attributes));
503 /* Get the enum built_in_class for BUILTIN_ID. */
505 enum built_in_class
506 builtins_manager::get_class (enum built_in_function builtin_id)
508 return builtin_data[builtin_id].fnclass;
511 /* Is BUILTIN_ID implicit? */
513 bool
514 builtins_manager::implicit_p (enum built_in_function builtin_id)
516 return builtin_data[builtin_id].implicit_p;
519 /* Get any attributes (in tree form) for the function declaration
520 for BUILTIN_ID.
522 These are created on-demand, and cached within the m_attributes
523 array, until finish_playback. */
525 tree
526 builtins_manager::get_attrs_tree (enum built_in_function builtin_id)
528 enum built_in_attribute attr = builtin_data[builtin_id].attr;
529 return get_attrs_tree (attr);
532 /* As above, but for an enum built_in_attribute. */
534 tree
535 builtins_manager::get_attrs_tree (enum built_in_attribute attr)
537 gcc_assert (attr < ATTR_LAST);
538 if (!m_attributes [attr])
539 m_attributes [attr] = make_attrs_tree (attr);
540 return m_attributes [attr];
543 /* Handle a cache-miss within the m_attributes array by
544 generating the attributes for enum built_in_attribute
545 in tree form. */
547 tree
548 builtins_manager::make_attrs_tree (enum built_in_attribute attr)
550 switch (attr)
552 /* Generate cases from builtin-attrs.def. */
553 #define DEF_ATTR_NULL_TREE(ENUM) \
554 case ENUM: return NULL_TREE;
555 #define DEF_ATTR_INT(ENUM, VALUE) \
556 case ENUM: return build_int_cst (integer_type_node, VALUE);
557 #define DEF_ATTR_STRING(ENUM, VALUE) \
558 case ENUM: return build_string (strlen (VALUE), VALUE);
559 #define DEF_ATTR_IDENT(ENUM, STRING) \
560 case ENUM: return get_identifier (STRING);
561 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) \
562 case ENUM: return tree_cons (get_attrs_tree (PURPOSE), \
563 get_attrs_tree (VALUE), \
564 get_attrs_tree (CHAIN));
565 #include "builtin-attrs.def"
566 #undef DEF_ATTR_NULL_TREE
567 #undef DEF_ATTR_INT
568 #undef DEF_ATTR_IDENT
569 #undef DEF_ATTR_TREE_LIST
571 default:
572 /* We somehow got a value not covered by the autogenerated
573 cases. */
574 gcc_unreachable ();
575 return NULL;
579 } // namespace jit
580 } // namespace gcc