Fix typos in riscv register save/restore.
[official-gcc.git] / gcc / jit / jit-builtins.c
blob35c4db048755c08c1c9ae326515261ddd27632ce
1 /* jit-builtins.c -- Handling of builtin functions during JIT-compilation.
2 Copyright (C) 2014-2017 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 (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, \
292 ARG4);
293 #define DEF_FUNCTION_TYPE_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
294 case ENUM: return make_fn_type (ENUM, RETURN, 0, 5, ARG1, ARG2, ARG3, \
295 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, \
299 ARG4, ARG5, ARG6);
300 #define DEF_FUNCTION_TYPE_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
301 ARG6, ARG7) \
302 case ENUM: return make_fn_type (ENUM, RETURN, 0, 7, ARG1, ARG2, ARG3, \
303 ARG4, ARG5, ARG6, ARG7);
304 #define DEF_FUNCTION_TYPE_8(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
305 ARG6, ARG7, ARG8) \
306 case ENUM: return make_fn_type (ENUM, RETURN, 0, 8, ARG1, ARG2, ARG3, \
307 ARG4, ARG5, ARG6, ARG7, ARG8);
308 #define DEF_FUNCTION_TYPE_9(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
309 ARG6, ARG7, ARG8, ARG9) \
310 case ENUM: return make_fn_type (ENUM, RETURN, 0, 9, ARG1, ARG2, ARG3, \
311 ARG4, ARG5, ARG6, ARG7, ARG8, ARG9);
312 #define DEF_FUNCTION_TYPE_10(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
313 ARG6, ARG7, ARG8, ARG9, ARG10) \
314 case ENUM: return make_fn_type (ENUM, RETURN, 0, 10, ARG1, ARG2, ARG3, \
315 ARG4, ARG5, ARG6, ARG7, ARG8, ARG9, \
316 ARG10);
317 #define DEF_FUNCTION_TYPE_11(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
318 ARG6, ARG7, ARG8, ARG9, ARG10, ARG11) \
319 case ENUM: return make_fn_type (ENUM, RETURN, 0, 11, ARG1, ARG2, ARG3, \
320 ARG4, ARG5, ARG6, ARG7, ARG8, ARG9, \
321 ARG10, ARG11);
322 #define DEF_FUNCTION_TYPE_VAR_0(ENUM, RETURN) \
323 case ENUM: return make_fn_type (ENUM, RETURN, 1, 0);
324 #define DEF_FUNCTION_TYPE_VAR_1(ENUM, RETURN, ARG1) \
325 case ENUM: return make_fn_type (ENUM, RETURN, 1, 1, ARG1);
326 #define DEF_FUNCTION_TYPE_VAR_2(ENUM, RETURN, ARG1, ARG2) \
327 case ENUM: return make_fn_type (ENUM, RETURN, 1, 2, ARG1, ARG2);
328 #define DEF_FUNCTION_TYPE_VAR_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
329 case ENUM: return make_fn_type (ENUM, RETURN, 1, 3, ARG1, ARG2, ARG3);
330 #define DEF_FUNCTION_TYPE_VAR_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
331 case ENUM: return make_fn_type (ENUM, RETURN, 1, 4, ARG1, ARG2, ARG3, \
332 ARG4);
333 #define DEF_FUNCTION_TYPE_VAR_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
334 case ENUM: return make_fn_type (ENUM, RETURN, 1, 5, ARG1, ARG2, ARG3, \
335 ARG4, ARG5);
336 #define DEF_FUNCTION_TYPE_VAR_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
337 ARG6) \
338 case ENUM: return make_fn_type (ENUM, RETURN, 1, 6, ARG1, ARG2, ARG3, \
339 ARG4, ARG5, ARG6);
340 #define DEF_FUNCTION_TYPE_VAR_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
341 ARG6, ARG7) \
342 case ENUM: return make_fn_type (ENUM, RETURN, 1, 7, ARG1, ARG2, ARG3, \
343 ARG4, ARG5, ARG6, ARG7);
344 #define DEF_POINTER_TYPE(ENUM, TYPE) \
345 case ENUM: return make_ptr_type (ENUM, TYPE);
347 #include "builtin-types.def"
349 #undef DEF_PRIMITIVE_TYPE
350 #undef DEF_FUNCTION_TYPE_0
351 #undef DEF_FUNCTION_TYPE_1
352 #undef DEF_FUNCTION_TYPE_2
353 #undef DEF_FUNCTION_TYPE_3
354 #undef DEF_FUNCTION_TYPE_4
355 #undef DEF_FUNCTION_TYPE_5
356 #undef DEF_FUNCTION_TYPE_6
357 #undef DEF_FUNCTION_TYPE_7
358 #undef DEF_FUNCTION_TYPE_8
359 #undef DEF_FUNCTION_TYPE_9
360 #undef DEF_FUNCTION_TYPE_10
361 #undef DEF_FUNCTION_TYPE_11
362 #undef DEF_FUNCTION_TYPE_VAR_0
363 #undef DEF_FUNCTION_TYPE_VAR_1
364 #undef DEF_FUNCTION_TYPE_VAR_2
365 #undef DEF_FUNCTION_TYPE_VAR_3
366 #undef DEF_FUNCTION_TYPE_VAR_4
367 #undef DEF_FUNCTION_TYPE_VAR_5
368 #undef DEF_FUNCTION_TYPE_VAR_6
369 #undef DEF_FUNCTION_TYPE_VAR_7
370 #undef DEF_POINTER_TYPE
372 default:
373 gcc_unreachable ();
377 /* Create the recording::type for a given primitive type within the
378 builtin system.
380 Only some types are currently supported. */
382 recording::type*
383 builtins_manager::make_primitive_type (enum jit_builtin_type type_id)
385 switch (type_id)
387 default:
388 // only some of these types are implemented so far:
389 m_ctxt->add_error (NULL,
390 "unimplemented primitive type for builtin: %d", type_id);
391 return NULL;
393 case BT_VOID: return m_ctxt->get_type (GCC_JIT_TYPE_VOID);
394 case BT_BOOL: return m_ctxt->get_type (GCC_JIT_TYPE_BOOL);
395 case BT_INT: return m_ctxt->get_type (GCC_JIT_TYPE_INT);
396 case BT_UINT: return m_ctxt->get_type (GCC_JIT_TYPE_UNSIGNED_INT);
397 case BT_LONG: return m_ctxt->get_type (GCC_JIT_TYPE_LONG);
398 case BT_ULONG: return m_ctxt->get_type (GCC_JIT_TYPE_UNSIGNED_LONG);
399 case BT_LONGLONG: return m_ctxt->get_type (GCC_JIT_TYPE_LONG_LONG);
400 case BT_ULONGLONG:
401 return m_ctxt->get_type (GCC_JIT_TYPE_UNSIGNED_LONG_LONG);
402 // case BT_INT128:
403 // case BT_UINT128:
404 // case BT_INTMAX:
405 // case BT_UINTMAX:
406 case BT_UINT16: return m_ctxt->get_int_type (2, false);
407 case BT_UINT32: return m_ctxt->get_int_type (4, false);
408 case BT_UINT64: return m_ctxt->get_int_type (8, false);
409 // case BT_WORD:
410 // case BT_UNWINDWORD:
411 case BT_FLOAT: return m_ctxt->get_type (GCC_JIT_TYPE_FLOAT);
412 case BT_DOUBLE: return m_ctxt->get_type (GCC_JIT_TYPE_DOUBLE);
413 case BT_LONGDOUBLE: return m_ctxt->get_type (GCC_JIT_TYPE_LONG_DOUBLE);
414 case BT_COMPLEX_FLOAT:
415 return m_ctxt->get_type (GCC_JIT_TYPE_COMPLEX_FLOAT);
416 case BT_COMPLEX_DOUBLE:
417 return m_ctxt->get_type (GCC_JIT_TYPE_COMPLEX_DOUBLE);
418 case BT_COMPLEX_LONGDOUBLE:
419 return m_ctxt->get_type (GCC_JIT_TYPE_COMPLEX_LONG_DOUBLE);
420 case BT_PTR: return m_ctxt->get_type (GCC_JIT_TYPE_VOID_PTR);
421 case BT_FILEPTR: return m_ctxt->get_type (GCC_JIT_TYPE_FILE_PTR);
422 // case BT_CONST:
423 // case BT_VOLATILE_PTR:
424 // case BT_CONST_VOLATILE_PTR:
425 // case BT_PTRMODE:
426 // case BT_INT_PTR:
427 // case BT_FLOAT_PTR:
428 case BT_DOUBLE_PTR:
429 return m_ctxt->get_type (GCC_JIT_TYPE_DOUBLE)->get_pointer ();
430 // case BT_CONST_DOUBLE_PTR:
431 // case BT_LONGDOUBLE_PTR:
432 // case BT_PID:
433 // case BT_SIZE:
434 // case BT_SSIZE:
435 // case BT_WINT:
436 // case BT_STRING:
437 case BT_CONST_STRING: return m_ctxt->get_type (GCC_JIT_TYPE_CONST_CHAR_PTR);
438 // case BT_DFLOAT32:
439 // case BT_DFLOAT64:
440 // case BT_DFLOAT128:
441 // case BT_DFLOAT32_PTR:
442 // case BT_DFLOAT64_PTR:
443 // case BT_DFLOAT128_PTR:
444 // case BT_VALIST_REF:
445 // case BT_VALIST_ARG:
446 // case BT_I1:
447 // case BT_I2:
448 // case BT_I4:
449 // case BT_I8:
450 // case BT_I16:
454 /* Create the recording::function_type for a given function type
455 signature. */
457 recording::function_type *
458 builtins_manager::make_fn_type (enum jit_builtin_type,
459 enum jit_builtin_type return_type_id,
460 bool is_variadic,
461 int num_args, ...)
463 va_list list;
464 int i;
465 recording::type **param_types = new recording::type *[num_args];
466 recording::type *return_type = NULL;
467 recording::function_type *result = NULL;
469 va_start (list, num_args);
470 for (i = 0; i < num_args; ++i)
472 enum jit_builtin_type arg_type_id =
473 (enum jit_builtin_type) va_arg (list, int);
474 param_types[i] = get_type (arg_type_id);
475 if (!param_types[i])
476 goto error;
478 va_end (list);
480 return_type = get_type (return_type_id);
481 if (!return_type)
482 goto error;
484 result = m_ctxt->new_function_type (return_type,
485 num_args,
486 param_types,
487 is_variadic);
489 error:
490 delete[] param_types;
491 return result;
494 /* Handler for DEF_POINTER_TYPE within builtins_manager::make_type. */
496 recording::type *
497 builtins_manager::make_ptr_type (enum jit_builtin_type,
498 enum jit_builtin_type other_type_id)
500 recording::type *base_type = get_type (other_type_id);
501 return base_type->get_pointer ();
504 /* Playback support. */
506 /* A builtins_manager is associated with a recording::context
507 and might be reused for multiple compiles on various
508 playback::contexts, perhaps with different options.
510 Purge any playback state. Currently this is just the table of
511 attributes. */
513 void
514 builtins_manager::finish_playback (void)
516 memset (m_attributes, 0, sizeof (m_attributes));
519 /* Get the enum built_in_class for BUILTIN_ID. */
521 enum built_in_class
522 builtins_manager::get_class (enum built_in_function builtin_id)
524 return builtin_data[builtin_id].fnclass;
527 /* Is BUILTIN_ID implicit? */
529 bool
530 builtins_manager::implicit_p (enum built_in_function builtin_id)
532 return builtin_data[builtin_id].implicit_p;
535 /* Get any attributes (in tree form) for the function declaration
536 for BUILTIN_ID.
538 These are created on-demand, and cached within the m_attributes
539 array, until finish_playback. */
541 tree
542 builtins_manager::get_attrs_tree (enum built_in_function builtin_id)
544 enum built_in_attribute attr = builtin_data[builtin_id].attr;
545 return get_attrs_tree (attr);
548 /* As above, but for an enum built_in_attribute. */
550 tree
551 builtins_manager::get_attrs_tree (enum built_in_attribute attr)
553 gcc_assert (attr < ATTR_LAST);
554 if (!m_attributes [attr])
555 m_attributes [attr] = make_attrs_tree (attr);
556 return m_attributes [attr];
559 /* Handle a cache-miss within the m_attributes array by
560 generating the attributes for enum built_in_attribute
561 in tree form. */
563 tree
564 builtins_manager::make_attrs_tree (enum built_in_attribute attr)
566 switch (attr)
568 /* Generate cases from builtin-attrs.def. */
569 #define DEF_ATTR_NULL_TREE(ENUM) \
570 case ENUM: return NULL_TREE;
571 #define DEF_ATTR_INT(ENUM, VALUE) \
572 case ENUM: return build_int_cst (integer_type_node, VALUE);
573 #define DEF_ATTR_STRING(ENUM, VALUE) \
574 case ENUM: return build_string (strlen (VALUE), VALUE);
575 #define DEF_ATTR_IDENT(ENUM, STRING) \
576 case ENUM: return get_identifier (STRING);
577 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) \
578 case ENUM: return tree_cons (get_attrs_tree (PURPOSE), \
579 get_attrs_tree (VALUE), \
580 get_attrs_tree (CHAIN));
581 #include "builtin-attrs.def"
582 #undef DEF_ATTR_NULL_TREE
583 #undef DEF_ATTR_INT
584 #undef DEF_ATTR_IDENT
585 #undef DEF_ATTR_TREE_LIST
587 default:
588 /* We somehow got a value not covered by the autogenerated
589 cases. */
590 gcc_unreachable ();
591 return NULL;
595 } // namespace jit
596 } // namespace gcc