* gcc.dg/20020919-1.c: Use _ARCH_PPC64 to test for -mpowerpc64.
[official-gcc.git] / gcc / jit / jit-builtins.c
blob47b01985170dff567bd62d7430160491987d681d
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 "target.h"
24 #include "stringpool.h"
26 #include "jit-common.h"
27 #include "jit-builtins.h"
28 #include "jit-recording.h"
29 #include "jit-playback.h"
31 namespace gcc {
33 namespace jit {
35 const char *const prefix = "__builtin_";
36 const size_t prefix_len = strlen (prefix);
38 /* Create "builtin_data", a const table of the data within builtins.def. */
39 struct builtin_data
41 const char *name;
42 enum built_in_class fnclass;
43 enum jit_builtin_type type;
44 bool both_p;
45 bool fallback_p;
46 enum built_in_attribute attr;
47 bool implicit_p;
49 const char *get_asm_name () const
51 if (both_p && fallback_p)
52 return name + prefix_len;
53 else
54 return name;
58 #define DEF_BUILTIN(X, NAME, CLASS, TYPE, LT, BOTH_P, FALLBACK_P, \
59 NONANSI_P, ATTRS, IMPLICIT, COND) \
60 {NAME, CLASS, TYPE, BOTH_P, FALLBACK_P, ATTRS, IMPLICIT},
61 static const struct builtin_data builtin_data[] =
63 #include "builtins.def"
65 #undef DEF_BUILTIN
67 /* Helper function for find_builtin_by_name. */
69 static bool
70 matches_builtin (const char *in_name,
71 const struct builtin_data& bd)
73 const bool debug = 0;
74 gcc_assert (bd.name);
76 if (debug)
77 fprintf (stderr, "seen builtin: %s\n", bd.name);
79 if (0 == strcmp (bd.name, in_name))
81 return true;
84 if (bd.both_p)
86 /* Then the macros in builtins.def gave a "__builtin_"
87 prefix to bd.name, but we should also recognize the form
88 without the prefix. */
89 gcc_assert (0 == strncmp (bd.name, prefix, prefix_len));
90 if (debug)
91 fprintf (stderr, "testing without prefix as: %s\n",
92 bd.name + prefix_len);
93 if (0 == strcmp (bd.name + prefix_len, in_name))
95 return true;
99 return false;
102 /* Locate the built-in function that matches name IN_NAME,
103 writing the result to OUT_ID and returning true if found,
104 or returning false if not found. */
106 static bool
107 find_builtin_by_name (const char *in_name,
108 enum built_in_function *out_id)
110 /* Locate builtin. This currently works by performing repeated
111 strcmp against every possible candidate, which is likely to
112 inefficient.
114 We start at index 1 to skip the initial entry (BUILT_IN_NONE), which
115 has a NULL name. */
116 for (unsigned int i = 1;
117 i < sizeof (builtin_data) / sizeof (builtin_data[0]);
118 i++)
120 const struct builtin_data& bd = builtin_data[i];
121 if (matches_builtin (in_name, bd))
123 /* Found a match. */
124 *out_id = static_cast<enum built_in_function> (i);
125 return true;
129 /* Not found. */
130 return false;
133 // class builtins_manager
135 /* Constructor for gcc::jit::builtins_manager. */
137 builtins_manager::builtins_manager (recording::context *ctxt)
138 : m_ctxt (ctxt)
140 memset (m_types, 0, sizeof (m_types));
141 memset (m_builtin_functions, 0, sizeof (m_builtin_functions));
142 memset (m_attributes, 0, sizeof (m_attributes));
145 /* Locate a builtin function by name.
146 Create a recording::function of the appropriate type, reusing them
147 if they've already been seen. */
149 recording::function *
150 builtins_manager::get_builtin_function (const char *name)
152 enum built_in_function builtin_id;
153 if (!find_builtin_by_name (name, &builtin_id))
155 m_ctxt->add_error (NULL, "builtin \"%s\" not found", name);
156 return NULL;
159 return get_builtin_function_by_id (builtin_id);
162 /* Locate a builtin function by id.
163 Create a recording::function of the appropriate type, reusing them
164 if they've already been seen. */
166 recording::function *
167 builtins_manager::get_builtin_function_by_id (enum built_in_function builtin_id)
169 gcc_assert (builtin_id >= 0);
170 gcc_assert (builtin_id < END_BUILTINS);
172 /* Lazily build the functions, caching them so that repeated calls for
173 the same id on a context give back the same object. */
174 if (!m_builtin_functions[builtin_id])
176 recording::function *fn = make_builtin_function (builtin_id);
177 if (fn)
179 m_builtin_functions[builtin_id] = fn;
180 m_ctxt->record (fn);
184 return m_builtin_functions[builtin_id];
187 /* Create the recording::function for a given builtin function, by ID. */
189 recording::function *
190 builtins_manager::make_builtin_function (enum built_in_function builtin_id)
192 const struct builtin_data& bd = builtin_data[builtin_id];
193 enum jit_builtin_type type_id = bd.type;
194 recording::type *t = get_type (type_id);
195 if (!t)
196 return NULL;
197 recording::function_type *func_type = t->as_a_function_type ();
198 if (!func_type)
199 return NULL;
201 vec<recording::type *> param_types = func_type->get_param_types ();
202 recording::param **params = new recording::param *[param_types.length ()];
204 int i;
205 recording::type *param_type;
206 FOR_EACH_VEC_ELT (param_types, i, param_type)
208 char buf[16];
209 snprintf (buf, 16, "arg%d", i);
210 params[i] = m_ctxt->new_param (NULL,
211 param_type,
212 buf);
214 const char *asm_name = bd.get_asm_name ();
215 recording::function *result =
216 new recording::function (m_ctxt,
217 NULL,
218 GCC_JIT_FUNCTION_IMPORTED, // FIXME
219 func_type->get_return_type (),
220 m_ctxt->new_string (asm_name),
221 param_types.length (),
222 params,
223 func_type->is_variadic (),
224 builtin_id);
225 delete[] params;
227 /* PR/64020 - If the client code is using builtin cos or sin,
228 tree-ssa-math-opt.c's execute_cse_sincos_1 may attempt
229 to optimize them to use __builtin_cexpi; for this,
230 BUILT_IN_CEXPI needs to exist.
232 Hence query the cache for BUILT_IN_CEXPI to ensure it gets
233 built. */
234 if (builtin_id == BUILT_IN_COS || builtin_id == BUILT_IN_SIN)
235 (void)get_builtin_function_by_id (BUILT_IN_CEXPI);
237 /* builtins.c:expand_builtin_cexpi can optimize the various
238 CEXP builtins to SINCOS builtins, and hence we may require
239 SINCOS builtins latter.
241 Ensure the appropriate SINCOS builtin exists. */
242 if (builtin_id == BUILT_IN_CEXPIF)
243 (void)get_builtin_function_by_id (BUILT_IN_SINCOSF);
244 else if (builtin_id == BUILT_IN_CEXPI)
245 (void)get_builtin_function_by_id (BUILT_IN_SINCOS);
246 else if (builtin_id == BUILT_IN_CEXPIL)
247 (void)get_builtin_function_by_id (BUILT_IN_SINCOSL);
249 return result;
252 /* Get the recording::type for a given type of builtin function,
253 by ID, creating it if it doesn't already exist. */
255 recording::type *
256 builtins_manager::get_type (enum jit_builtin_type type_id)
258 if (!m_types[type_id])
259 m_types[type_id] = make_type (type_id);
260 return m_types[type_id];
263 /* Create the recording::type for a given type of builtin function. */
265 recording::type *
266 builtins_manager::make_type (enum jit_builtin_type type_id)
268 /* Use builtin-types.def to construct a switch statement, with each
269 case deferring to one of the methods below:
270 - DEF_PRIMITIVE_TYPE is handled as a call to make_primitive_type.
271 - the various DEF_FUNCTION_TYPE_n are handled by variadic calls
272 to make_fn_type.
273 - similarly for DEF_FUNCTION_TYPE_VAR_n, but setting the
274 "is_variadic" argument.
275 - DEF_POINTER_TYPE is handled by make_ptr_type.
276 That should handle everything, but just in case we also suppy a
277 gcc_unreachable default clause. */
278 switch (type_id)
280 #define DEF_PRIMITIVE_TYPE(ENUM, VALUE) \
281 case ENUM: return make_primitive_type (ENUM);
282 #define DEF_FUNCTION_TYPE_0(ENUM, RETURN) \
283 case ENUM: return make_fn_type (ENUM, RETURN, 0, 0);
284 #define DEF_FUNCTION_TYPE_1(ENUM, RETURN, ARG1) \
285 case ENUM: return make_fn_type (ENUM, RETURN, 0, 1, ARG1);
286 #define DEF_FUNCTION_TYPE_2(ENUM, RETURN, ARG1, ARG2) \
287 case ENUM: return make_fn_type (ENUM, RETURN, 0, 2, ARG1, ARG2);
288 #define DEF_FUNCTION_TYPE_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
289 case ENUM: return make_fn_type (ENUM, RETURN, 0, 3, ARG1, ARG2, ARG3);
290 #define DEF_FUNCTION_TYPE_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
291 case ENUM: return make_fn_type (ENUM, RETURN, 0, 4, ARG1, ARG2, ARG3, ARG4);
292 #define DEF_FUNCTION_TYPE_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
293 case ENUM: return make_fn_type (ENUM, RETURN, 0, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
294 #define DEF_FUNCTION_TYPE_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
295 ARG6) \
296 case ENUM: return make_fn_type (ENUM, RETURN, 0, 6, ARG1, ARG2, ARG3, 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, ARG4, ARG5, ARG6, ARG7);
300 #define DEF_FUNCTION_TYPE_8(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
301 ARG6, ARG7, ARG8) \
302 case ENUM: return make_fn_type (ENUM, RETURN, 0, 8, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
303 ARG7, ARG8);
304 #define DEF_FUNCTION_TYPE_VAR_0(ENUM, RETURN) \
305 case ENUM: return make_fn_type (ENUM, RETURN, 1, 0);
306 #define DEF_FUNCTION_TYPE_VAR_1(ENUM, RETURN, ARG1) \
307 case ENUM: return make_fn_type (ENUM, RETURN, 1, 1, ARG1);
308 #define DEF_FUNCTION_TYPE_VAR_2(ENUM, RETURN, ARG1, ARG2) \
309 case ENUM: return make_fn_type (ENUM, RETURN, 1, 2, ARG1, ARG2);
310 #define DEF_FUNCTION_TYPE_VAR_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
311 case ENUM: return make_fn_type (ENUM, RETURN, 1, 3, ARG1, ARG2, ARG3);
312 #define DEF_FUNCTION_TYPE_VAR_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
313 case ENUM: return make_fn_type (ENUM, RETURN, 1, 4, ARG1, ARG2, ARG3, ARG4);
314 #define DEF_FUNCTION_TYPE_VAR_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
315 case ENUM: return make_fn_type (ENUM, RETURN, 1, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
316 #define DEF_FUNCTION_TYPE_VAR_8(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
317 ARG6, ARG7, ARG8) \
318 case ENUM: return make_fn_type (ENUM, RETURN, 1, 8, ARG1, ARG2, ARG3, \
319 ARG4, ARG5, ARG6, ARG7, ARG8);
320 #define DEF_FUNCTION_TYPE_VAR_12(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
321 ARG6, ARG7, ARG8, ARG9, ARG10, ARG11, ARG12) \
322 case ENUM: return make_fn_type (ENUM, RETURN, 1, 12, ARG1, ARG2, ARG3, \
323 ARG4, ARG5, ARG6, ARG7, ARG8, ARG9, \
324 ARG10, ARG11, ARG12);
325 #define DEF_POINTER_TYPE(ENUM, TYPE) \
326 case ENUM: return make_ptr_type (ENUM, TYPE);
328 #include "builtin-types.def"
330 #undef DEF_PRIMITIVE_TYPE
331 #undef DEF_FUNCTION_TYPE_0
332 #undef DEF_FUNCTION_TYPE_1
333 #undef DEF_FUNCTION_TYPE_2
334 #undef DEF_FUNCTION_TYPE_3
335 #undef DEF_FUNCTION_TYPE_4
336 #undef DEF_FUNCTION_TYPE_5
337 #undef DEF_FUNCTION_TYPE_6
338 #undef DEF_FUNCTION_TYPE_7
339 #undef DEF_FUNCTION_TYPE_8
340 #undef DEF_FUNCTION_TYPE_VAR_0
341 #undef DEF_FUNCTION_TYPE_VAR_1
342 #undef DEF_FUNCTION_TYPE_VAR_2
343 #undef DEF_FUNCTION_TYPE_VAR_3
344 #undef DEF_FUNCTION_TYPE_VAR_4
345 #undef DEF_FUNCTION_TYPE_VAR_5
346 #undef DEF_FUNCTION_TYPE_VAR_8
347 #undef DEF_FUNCTION_TYPE_VAR_12
348 #undef DEF_POINTER_TYPE
350 default:
351 gcc_unreachable ();
355 /* Create the recording::type for a given primitive type within the
356 builtin system.
358 Only some types are currently supported. */
360 recording::type*
361 builtins_manager::make_primitive_type (enum jit_builtin_type type_id)
363 switch (type_id)
365 default:
366 // only some of these types are implemented so far:
367 m_ctxt->add_error (NULL,
368 "unimplemented primitive type for builtin: %d", type_id);
369 return NULL;
371 case BT_VOID: return m_ctxt->get_type (GCC_JIT_TYPE_VOID);
372 case BT_BOOL: return m_ctxt->get_type (GCC_JIT_TYPE_BOOL);
373 case BT_INT: return m_ctxt->get_type (GCC_JIT_TYPE_INT);
374 case BT_UINT: return m_ctxt->get_type (GCC_JIT_TYPE_UNSIGNED_INT);
375 case BT_LONG: return m_ctxt->get_type (GCC_JIT_TYPE_LONG);
376 case BT_ULONG: return m_ctxt->get_type (GCC_JIT_TYPE_UNSIGNED_LONG);
377 case BT_LONGLONG: return m_ctxt->get_type (GCC_JIT_TYPE_LONG_LONG);
378 case BT_ULONGLONG:
379 return m_ctxt->get_type (GCC_JIT_TYPE_UNSIGNED_LONG_LONG);
380 // case BT_INT128:
381 // case BT_UINT128:
382 // case BT_INTMAX:
383 // case BT_UINTMAX:
384 case BT_UINT16: return m_ctxt->get_int_type (2, false);
385 case BT_UINT32: return m_ctxt->get_int_type (4, false);
386 case BT_UINT64: return m_ctxt->get_int_type (8, false);
387 // case BT_WORD:
388 // case BT_UNWINDWORD:
389 case BT_FLOAT: return m_ctxt->get_type (GCC_JIT_TYPE_FLOAT);
390 case BT_DOUBLE: return m_ctxt->get_type (GCC_JIT_TYPE_DOUBLE);
391 case BT_LONGDOUBLE: return m_ctxt->get_type (GCC_JIT_TYPE_LONG_DOUBLE);
392 case BT_COMPLEX_FLOAT:
393 return m_ctxt->get_type (GCC_JIT_TYPE_COMPLEX_FLOAT);
394 case BT_COMPLEX_DOUBLE:
395 return m_ctxt->get_type (GCC_JIT_TYPE_COMPLEX_DOUBLE);
396 case BT_COMPLEX_LONGDOUBLE:
397 return m_ctxt->get_type (GCC_JIT_TYPE_COMPLEX_LONG_DOUBLE);
398 case BT_PTR: return m_ctxt->get_type (GCC_JIT_TYPE_VOID_PTR);
399 case BT_FILEPTR: return m_ctxt->get_type (GCC_JIT_TYPE_FILE_PTR);
400 // case BT_CONST:
401 // case BT_VOLATILE_PTR:
402 // case BT_CONST_VOLATILE_PTR:
403 // case BT_PTRMODE:
404 // case BT_INT_PTR:
405 // case BT_FLOAT_PTR:
406 case BT_DOUBLE_PTR:
407 return m_ctxt->get_type (GCC_JIT_TYPE_DOUBLE)->get_pointer ();
408 // case BT_CONST_DOUBLE_PTR:
409 // case BT_LONGDOUBLE_PTR:
410 // case BT_PID:
411 // case BT_SIZE:
412 // case BT_SSIZE:
413 // case BT_WINT:
414 // case BT_STRING:
415 case BT_CONST_STRING: return m_ctxt->get_type (GCC_JIT_TYPE_CONST_CHAR_PTR);
416 // case BT_DFLOAT32:
417 // case BT_DFLOAT64:
418 // case BT_DFLOAT128:
419 // case BT_DFLOAT32_PTR:
420 // case BT_DFLOAT64_PTR:
421 // case BT_DFLOAT128_PTR:
422 // case BT_VALIST_REF:
423 // case BT_VALIST_ARG:
424 // case BT_I1:
425 // case BT_I2:
426 // case BT_I4:
427 // case BT_I8:
428 // case BT_I16:
432 /* Create the recording::function_type for a given function type
433 signature. */
435 recording::function_type *
436 builtins_manager::make_fn_type (enum jit_builtin_type,
437 enum jit_builtin_type return_type_id,
438 bool is_variadic,
439 int num_args, ...)
441 va_list list;
442 int i;
443 recording::type **param_types = new recording::type *[num_args];
444 recording::type *return_type = NULL;
445 recording::function_type *result = NULL;
447 va_start (list, num_args);
448 for (i = 0; i < num_args; ++i)
450 enum jit_builtin_type arg_type_id =
451 (enum jit_builtin_type) va_arg (list, int);
452 param_types[i] = get_type (arg_type_id);
453 if (!param_types[i])
454 goto error;
456 va_end (list);
458 return_type = get_type (return_type_id);
459 if (!return_type)
460 goto error;
462 result = m_ctxt->new_function_type (return_type,
463 num_args,
464 param_types,
465 is_variadic);
467 error:
468 delete[] param_types;
469 return result;
472 /* Handler for DEF_POINTER_TYPE within builtins_manager::make_type. */
474 recording::type *
475 builtins_manager::make_ptr_type (enum jit_builtin_type,
476 enum jit_builtin_type other_type_id)
478 recording::type *base_type = get_type (other_type_id);
479 return base_type->get_pointer ();
482 /* Playback support. */
484 /* A builtins_manager is associated with a recording::context
485 and might be reused for multiple compiles on various
486 playback::contexts, perhaps with different options.
488 Purge any playback state. Currently this is just the table of
489 attributes. */
491 void
492 builtins_manager::finish_playback (void)
494 memset (m_attributes, 0, sizeof (m_attributes));
497 /* Get the enum built_in_class for BUILTIN_ID. */
499 enum built_in_class
500 builtins_manager::get_class (enum built_in_function builtin_id)
502 return builtin_data[builtin_id].fnclass;
505 /* Is BUILTIN_ID implicit? */
507 bool
508 builtins_manager::implicit_p (enum built_in_function builtin_id)
510 return builtin_data[builtin_id].implicit_p;
513 /* Get any attributes (in tree form) for the function declaration
514 for BUILTIN_ID.
516 These are created on-demand, and cached within the m_attributes
517 array, until finish_playback. */
519 tree
520 builtins_manager::get_attrs_tree (enum built_in_function builtin_id)
522 enum built_in_attribute attr = builtin_data[builtin_id].attr;
523 return get_attrs_tree (attr);
526 /* As above, but for an enum built_in_attribute. */
528 tree
529 builtins_manager::get_attrs_tree (enum built_in_attribute attr)
531 gcc_assert (attr < ATTR_LAST);
532 if (!m_attributes [attr])
533 m_attributes [attr] = make_attrs_tree (attr);
534 return m_attributes [attr];
537 /* Handle a cache-miss within the m_attributes array by
538 generating the attributes for enum built_in_attribute
539 in tree form. */
541 tree
542 builtins_manager::make_attrs_tree (enum built_in_attribute attr)
544 switch (attr)
546 /* Generate cases from builtin-attrs.def. */
547 #define DEF_ATTR_NULL_TREE(ENUM) \
548 case ENUM: return NULL_TREE;
549 #define DEF_ATTR_INT(ENUM, VALUE) \
550 case ENUM: return build_int_cst (integer_type_node, VALUE);
551 #define DEF_ATTR_STRING(ENUM, VALUE) \
552 case ENUM: return build_string (strlen (VALUE), VALUE);
553 #define DEF_ATTR_IDENT(ENUM, STRING) \
554 case ENUM: return get_identifier (STRING);
555 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) \
556 case ENUM: return tree_cons (get_attrs_tree (PURPOSE), \
557 get_attrs_tree (VALUE), \
558 get_attrs_tree (CHAIN));
559 #include "builtin-attrs.def"
560 #undef DEF_ATTR_NULL_TREE
561 #undef DEF_ATTR_INT
562 #undef DEF_ATTR_IDENT
563 #undef DEF_ATTR_TREE_LIST
565 default:
566 /* We somehow got a value not covered by the autogenerated
567 cases. */
568 gcc_unreachable ();
569 return NULL;
573 } // namespace jit
574 } // namespace gcc