Debian Codegraph @

gcc/jit/jit-builtins.cc

raw
1 /* jit-builtins.cc -- Handling of builtin functions during JIT-compilation.
2 Copyright (C) 2014-2025 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
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.
10
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.
15
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/>. */
19
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"
26
27 #include "jit-builtins.h"
28
29 namespace gcc {
30
31 namespace jit {
32
33 const char *const prefix = "__builtin_";
34 const size_t prefix_len = strlen (prefix);
35
36 /* Create "builtin_data", a const table of the data within builtins.def. */
37 struct builtin_data
38 {
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;
46
47 const char *get_asm_name () const
48 {
49 if (both_p && fallback_p)
50 return name + prefix_len;
51 else
52 return name;
53 }
54 };
55
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[] =
60 {
61 #include "builtins.def"
62 };
63
64 /* Helper function for find_builtin_by_name. */
65
66 static bool
67 matches_builtin (const char *in_name,
68 const struct builtin_data& bd)
69 {
70 const bool debug = 0;
71
72 /* Ignore entries with a NULL name. */
73 if (!bd.name)
74 return false;
75
76 if (debug)
77 fprintf (stderr, "seen builtin: %s\n", bd.name);
78
79 if (strcmp (bd.name, in_name) == 0)
80 return true;
81
82 if (bd.both_p)
83 {
84 /* Then the macros in builtins.def gave a "__builtin_"
85 prefix to bd.name, but we should also recognize the form
86 without the prefix. */
87 gcc_assert (strncmp (bd.name, prefix, prefix_len) == 0);
88 if (debug)
89 fprintf (stderr, "testing without prefix as: %s\n",
90 bd.name + prefix_len);
91 if (strcmp (bd.name + prefix_len, in_name) == 0)
92 return true;
93 }
94
95 return false;
96 }
97
98 /* Locate the built-in function that matches name IN_NAME,
99 writing the result to OUT_ID and returning true if found,
100 or returning false if not found. */
101
102 static bool
103 find_builtin_by_name (const char *in_name,
104 enum built_in_function *out_id)
105 {
106 /* Locate builtin. This currently works by performing repeated
107 strcmp against every possible candidate, which is likely to
108 inefficient.
109
110 We start at index 1 to skip the initial entry (BUILT_IN_NONE), which
111 has a NULL name. */
112 for (unsigned int i = 1; i < ARRAY_SIZE (builtin_data); i++)
113 {
114 const struct builtin_data& bd = builtin_data[i];
115 if (matches_builtin (in_name, bd))
116 {
117 /* Found a match. */
118 *out_id = static_cast<enum built_in_function> (i);
119 return true;
120 }
121 }
122
123 /* Not found. */
124 return false;
125 }
126
127 // class builtins_manager
128
129 /* Constructor for gcc::jit::builtins_manager. */
130
131 builtins_manager::builtins_manager (recording::context *ctxt)
132 : m_ctxt (ctxt)
133 {
134 memset (m_types, 0, sizeof (m_types));
135 memset (m_builtin_functions, 0, sizeof (m_builtin_functions));
136 memset (m_attributes, 0, sizeof (m_attributes));
137 }
138
139 /* Locate a builtin function by name.
140 Create a recording::function of the appropriate type, reusing them
141 if they've already been seen. */
142
143 recording::function *
144 builtins_manager::get_builtin_function (const char *name)
145 {
146 enum built_in_function builtin_id;
147 if (!find_builtin_by_name (name, &builtin_id))
148 {
149 m_ctxt->add_error (NULL, "builtin \"%s\" not found", name);
150 return NULL;
151 }
152
153 return get_builtin_function_by_id (builtin_id);
154 }
155
156 /* Locate a builtin function by id.
157 Create a recording::function of the appropriate type, reusing them
158 if they've already been seen. */
159
160 recording::function *
161 builtins_manager::get_builtin_function_by_id (enum built_in_function builtin_id)
162 {
163 gcc_assert (builtin_id > BUILT_IN_NONE);
164 gcc_assert (builtin_id < END_BUILTINS);
165
166 /* Lazily build the functions, caching them so that repeated calls for
167 the same id on a context give back the same object. */
168 if (!m_builtin_functions[builtin_id])
169 {
170 recording::function *fn = make_builtin_function (builtin_id);
171 if (fn)
172 {
173 m_builtin_functions[builtin_id] = fn;
174 m_ctxt->record (fn);
175 }
176 }
177
178 return m_builtin_functions[builtin_id];
179 }
180
181 /* Create the recording::function for a given builtin function, by ID. */
182
183 recording::function *
184 builtins_manager::make_builtin_function (enum built_in_function builtin_id)
185 {
186 const struct builtin_data& bd = builtin_data[builtin_id];
187 enum jit_builtin_type type_id = bd.type;
188 recording::type *t = get_type (type_id);
189 if (!t)
190 return NULL;
191 recording::function_type *func_type = t->as_a_function_type ();
192 if (!func_type)
193 return NULL;
194
195 vec<recording::type *> param_types = func_type->get_param_types ();
196 recording::param **params = new recording::param *[param_types.length ()];
197
198 int i;
199 recording::type *param_type;
200 FOR_EACH_VEC_ELT (param_types, i, param_type)
201 {
202 char buf[16];
203 snprintf (buf, 16, "arg%d", i);
204 params[i] = m_ctxt->new_param (NULL,
205 param_type,
206 buf);
207 }
208 const char *asm_name = bd.get_asm_name ();
209 recording::function *result =
210 new recording::function (m_ctxt,
211 NULL,
212 GCC_JIT_FUNCTION_IMPORTED, // FIXME
213 func_type->get_return_type (),
214 m_ctxt->new_string (asm_name),
215 param_types.length (),
216 params,
217 func_type->is_variadic (),
218 builtin_id,
219 false);
220 delete[] params;
221
222 /* PR/64020 - If the client code is using builtin cos or sin,
223 tree-ssa-math-opt.c's execute_cse_sincos_1 may attempt
224 to optimize them to use __builtin_cexpi; for this,
225 BUILT_IN_CEXPI needs to exist.
226
227 Hence query the cache for BUILT_IN_CEXPI to ensure it gets
228 built. */
229 if (builtin_id == BUILT_IN_COS || builtin_id == BUILT_IN_SIN)
230 (void)get_builtin_function_by_id (BUILT_IN_CEXPI);
231
232 /* builtins.cc:expand_builtin_cexpi can optimize the various
233 CEXP builtins to SINCOS builtins, and hence we may require
234 SINCOS builtins latter.
235
236 Ensure the appropriate SINCOS builtin exists. */
237 if (builtin_id == BUILT_IN_CEXPIF)
238 (void)get_builtin_function_by_id (BUILT_IN_SINCOSF);
239 else if (builtin_id == BUILT_IN_CEXPI)
240 (void)get_builtin_function_by_id (BUILT_IN_SINCOS);
241 else if (builtin_id == BUILT_IN_CEXPIL)
242 (void)get_builtin_function_by_id (BUILT_IN_SINCOSL);
243
244 return result;
245 }
246
247 /* Build an array of type names for use by get_string_for_type_id. */
248
249 static const char * const type_names[] = {
250 #define DEF_PRIMITIVE_TYPE(ENUM, VALUE) #ENUM,
251 #define DEF_FUNCTION_TYPE_0(ENUM, RETURN) #ENUM,
252 #define DEF_FUNCTION_TYPE_1(ENUM, RETURN, ARG1) #ENUM,
253 #define DEF_FUNCTION_TYPE_2(ENUM, RETURN, ARG1, ARG2) #ENUM,
254 #define DEF_FUNCTION_TYPE_3(ENUM, RETURN, ARG1, ARG2, ARG3) #ENUM,
255 #define DEF_FUNCTION_TYPE_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) #ENUM,
256 #define DEF_FUNCTION_TYPE_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) #ENUM,
257 #define DEF_FUNCTION_TYPE_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
258 ARG6) \
259 #ENUM,
260 #define DEF_FUNCTION_TYPE_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
261 ARG6, ARG7) \
262 #ENUM,
263 #define DEF_FUNCTION_TYPE_8(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
264 ARG6, ARG7, ARG8) \
265 #ENUM,
266 #define DEF_FUNCTION_TYPE_9(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
267 ARG6, ARG7, ARG8, ARG9) \
268 #ENUM,
269 #define DEF_FUNCTION_TYPE_10(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
270 ARG6, ARG7, ARG8, ARG9, ARG10) \
271 #ENUM,
272 #define DEF_FUNCTION_TYPE_11(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
273 ARG6, ARG7, ARG8, ARG9, ARG10, ARG11) \
274 #ENUM,
275 #define DEF_FUNCTION_TYPE_VAR_0(ENUM, RETURN) #ENUM,
276 #define DEF_FUNCTION_TYPE_VAR_1(ENUM, RETURN, ARG1) #ENUM,
277 #define DEF_FUNCTION_TYPE_VAR_2(ENUM, RETURN, ARG1, ARG2) #ENUM,
278 #define DEF_FUNCTION_TYPE_VAR_3(ENUM, RETURN, ARG1, ARG2, ARG3) #ENUM,
279 #define DEF_FUNCTION_TYPE_VAR_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) #ENUM,
280 #define DEF_FUNCTION_TYPE_VAR_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
281 #ENUM,
282 #define DEF_FUNCTION_TYPE_VAR_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
283 ARG6) \
284 #ENUM,
285 #define DEF_FUNCTION_TYPE_VAR_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
286 ARG6, ARG7) \
287 #ENUM,
288 #define DEF_POINTER_TYPE(ENUM, TYPE) #ENUM,
289
290 #include "builtin-types.def"
291
292 #undef DEF_PRIMITIVE_TYPE
293 #undef DEF_FUNCTION_TYPE_0
294 #undef DEF_FUNCTION_TYPE_1
295 #undef DEF_FUNCTION_TYPE_2
296 #undef DEF_FUNCTION_TYPE_3
297 #undef DEF_FUNCTION_TYPE_4
298 #undef DEF_FUNCTION_TYPE_5
299 #undef DEF_FUNCTION_TYPE_6
300 #undef DEF_FUNCTION_TYPE_7
301 #undef DEF_FUNCTION_TYPE_8
302 #undef DEF_FUNCTION_TYPE_9
303 #undef DEF_FUNCTION_TYPE_10
304 #undef DEF_FUNCTION_TYPE_11
305 #undef DEF_FUNCTION_TYPE_VAR_0
306 #undef DEF_FUNCTION_TYPE_VAR_1
307 #undef DEF_FUNCTION_TYPE_VAR_2
308 #undef DEF_FUNCTION_TYPE_VAR_3
309 #undef DEF_FUNCTION_TYPE_VAR_4
310 #undef DEF_FUNCTION_TYPE_VAR_5
311 #undef DEF_FUNCTION_TYPE_VAR_6
312 #undef DEF_FUNCTION_TYPE_VAR_7
313 #undef DEF_POINTER_TYPE
314 };
315
316 /* Get a string for TYPE_ID suitable for use in logs and error messages
317 (e.g. "BT_PID"). */
318
319 static const char *
320 get_string_for_type_id (enum jit_builtin_type type_id)
321 {
322 gcc_assert (type_id < ARRAY_SIZE (type_names));
323 return type_names[type_id];
324 }
325
326 /* Get the recording::type for a given type of builtin function,
327 by ID, creating it if it doesn't already exist. */
328
329 recording::type *
330 builtins_manager::get_type (enum jit_builtin_type type_id)
331 {
332 if (!m_types[type_id])
333 m_types[type_id] = make_type (type_id);
334 return m_types[type_id];
335 }
336
337 /* Create the recording::type for a given type of builtin function. */
338
339 recording::type *
340 builtins_manager::make_type (enum jit_builtin_type type_id)
341 {
342 /* Use builtin-types.def to construct a switch statement, with each
343 case deferring to one of the methods below:
344 - DEF_PRIMITIVE_TYPE is handled as a call to make_primitive_type.
345 - the various DEF_FUNCTION_TYPE_n are handled by variadic calls
346 to make_fn_type.
347 - similarly for DEF_FUNCTION_TYPE_VAR_n, but setting the
348 "is_variadic" argument.
349 - DEF_POINTER_TYPE is handled by make_ptr_type.
350 That should handle everything, but just in case we also suppy a
351 gcc_unreachable default clause. */
352 switch (type_id)
353 {
354 #define DEF_PRIMITIVE_TYPE(ENUM, VALUE) \
355 case ENUM: return make_primitive_type (ENUM);
356 #define DEF_FUNCTION_TYPE_0(ENUM, RETURN) \
357 case ENUM: return make_fn_type (ENUM, RETURN, 0, 0);
358 #define DEF_FUNCTION_TYPE_1(ENUM, RETURN, ARG1) \
359 case ENUM: return make_fn_type (ENUM, RETURN, 0, 1, ARG1);
360 #define DEF_FUNCTION_TYPE_2(ENUM, RETURN, ARG1, ARG2) \
361 case ENUM: return make_fn_type (ENUM, RETURN, 0, 2, ARG1, ARG2);
362 #define DEF_FUNCTION_TYPE_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
363 case ENUM: return make_fn_type (ENUM, RETURN, 0, 3, ARG1, ARG2, ARG3);
364 #define DEF_FUNCTION_TYPE_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
365 case ENUM: return make_fn_type (ENUM, RETURN, 0, 4, ARG1, ARG2, ARG3, \
366 ARG4);
367 #define DEF_FUNCTION_TYPE_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
368 case ENUM: return make_fn_type (ENUM, RETURN, 0, 5, ARG1, ARG2, ARG3, \
369 ARG4, ARG5);
370 #define DEF_FUNCTION_TYPE_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
371 ARG6) \
372 case ENUM: return make_fn_type (ENUM, RETURN, 0, 6, ARG1, ARG2, ARG3, \
373 ARG4, ARG5, ARG6);
374 #define DEF_FUNCTION_TYPE_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
375 ARG6, ARG7) \
376 case ENUM: return make_fn_type (ENUM, RETURN, 0, 7, ARG1, ARG2, ARG3, \
377 ARG4, ARG5, ARG6, ARG7);
378 #define DEF_FUNCTION_TYPE_8(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
379 ARG6, ARG7, ARG8) \
380 case ENUM: return make_fn_type (ENUM, RETURN, 0, 8, ARG1, ARG2, ARG3, \
381 ARG4, ARG5, ARG6, ARG7, ARG8);
382 #define DEF_FUNCTION_TYPE_9(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
383 ARG6, ARG7, ARG8, ARG9) \
384 case ENUM: return make_fn_type (ENUM, RETURN, 0, 9, ARG1, ARG2, ARG3, \
385 ARG4, ARG5, ARG6, ARG7, ARG8, ARG9);
386 #define DEF_FUNCTION_TYPE_10(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
387 ARG6, ARG7, ARG8, ARG9, ARG10) \
388 case ENUM: return make_fn_type (ENUM, RETURN, 0, 10, ARG1, ARG2, ARG3, \
389 ARG4, ARG5, ARG6, ARG7, ARG8, ARG9, \
390 ARG10);
391 #define DEF_FUNCTION_TYPE_11(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
392 ARG6, ARG7, ARG8, ARG9, ARG10, ARG11) \
393 case ENUM: return make_fn_type (ENUM, RETURN, 0, 11, ARG1, ARG2, ARG3, \
394 ARG4, ARG5, ARG6, ARG7, ARG8, ARG9, \
395 ARG10, ARG11);
396 #define DEF_FUNCTION_TYPE_VAR_0(ENUM, RETURN) \
397 case ENUM: return make_fn_type (ENUM, RETURN, 1, 0);
398 #define DEF_FUNCTION_TYPE_VAR_1(ENUM, RETURN, ARG1) \
399 case ENUM: return make_fn_type (ENUM, RETURN, 1, 1, ARG1);
400 #define DEF_FUNCTION_TYPE_VAR_2(ENUM, RETURN, ARG1, ARG2) \
401 case ENUM: return make_fn_type (ENUM, RETURN, 1, 2, ARG1, ARG2);
402 #define DEF_FUNCTION_TYPE_VAR_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
403 case ENUM: return make_fn_type (ENUM, RETURN, 1, 3, ARG1, ARG2, ARG3);
404 #define DEF_FUNCTION_TYPE_VAR_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
405 case ENUM: return make_fn_type (ENUM, RETURN, 1, 4, ARG1, ARG2, ARG3, \
406 ARG4);
407 #define DEF_FUNCTION_TYPE_VAR_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
408 case ENUM: return make_fn_type (ENUM, RETURN, 1, 5, ARG1, ARG2, ARG3, \
409 ARG4, ARG5);
410 #define DEF_FUNCTION_TYPE_VAR_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
411 ARG6) \
412 case ENUM: return make_fn_type (ENUM, RETURN, 1, 6, ARG1, ARG2, ARG3, \
413 ARG4, ARG5, ARG6);
414 #define DEF_FUNCTION_TYPE_VAR_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
415 ARG6, ARG7) \
416 case ENUM: return make_fn_type (ENUM, RETURN, 1, 7, ARG1, ARG2, ARG3, \
417 ARG4, ARG5, ARG6, ARG7);
418 #define DEF_POINTER_TYPE(ENUM, TYPE) \
419 case ENUM: return make_ptr_type (ENUM, TYPE);
420
421 #include "builtin-types.def"
422
423 #undef DEF_PRIMITIVE_TYPE
424 #undef DEF_FUNCTION_TYPE_0
425 #undef DEF_FUNCTION_TYPE_1
426 #undef DEF_FUNCTION_TYPE_2
427 #undef DEF_FUNCTION_TYPE_3
428 #undef DEF_FUNCTION_TYPE_4
429 #undef DEF_FUNCTION_TYPE_5
430 #undef DEF_FUNCTION_TYPE_6
431 #undef DEF_FUNCTION_TYPE_7
432 #undef DEF_FUNCTION_TYPE_8
433 #undef DEF_FUNCTION_TYPE_9
434 #undef DEF_FUNCTION_TYPE_10
435 #undef DEF_FUNCTION_TYPE_11
436 #undef DEF_FUNCTION_TYPE_VAR_0
437 #undef DEF_FUNCTION_TYPE_VAR_1
438 #undef DEF_FUNCTION_TYPE_VAR_2
439 #undef DEF_FUNCTION_TYPE_VAR_3
440 #undef DEF_FUNCTION_TYPE_VAR_4
441 #undef DEF_FUNCTION_TYPE_VAR_5
442 #undef DEF_FUNCTION_TYPE_VAR_6
443 #undef DEF_FUNCTION_TYPE_VAR_7
444 #undef DEF_POINTER_TYPE
445
446 default:
447 gcc_unreachable ();
448 }
449 }
450
451 /* Create the recording::type for a given primitive type within the
452 builtin system.
453
454 Only some types are currently supported. */
455
456 recording::type*
457 builtins_manager::make_primitive_type (enum jit_builtin_type type_id)
458 {
459 switch (type_id)
460 {
461 default:
462 // only some of these types are implemented so far:
463 m_ctxt->add_error (NULL,
464 "unimplemented primitive type for builtin (type: %s)",
465 get_string_for_type_id (type_id));
466 return NULL;
467
468 case BT_VOID: return m_ctxt->get_type (GCC_JIT_TYPE_VOID);
469 case BT_BOOL: return m_ctxt->get_type (GCC_JIT_TYPE_BOOL);
470 case BT_INT: return m_ctxt->get_type (GCC_JIT_TYPE_INT);
471 case BT_UINT: return m_ctxt->get_type (GCC_JIT_TYPE_UNSIGNED_INT);
472 case BT_LONG: return m_ctxt->get_type (GCC_JIT_TYPE_LONG);
473 case BT_ULONG: return m_ctxt->get_type (GCC_JIT_TYPE_UNSIGNED_LONG);
474 case BT_LONGLONG: return m_ctxt->get_type (GCC_JIT_TYPE_LONG_LONG);
475 case BT_ULONGLONG:
476 return m_ctxt->get_type (GCC_JIT_TYPE_UNSIGNED_LONG_LONG);
477 // case BT_INTMAX:
478 // case BT_UINTMAX:
479 case BT_INT8: return m_ctxt->get_int_type (1, true);
480 case BT_INT16: return m_ctxt->get_int_type (2, true);
481 case BT_UINT8: return m_ctxt->get_int_type (1, false);
482 case BT_UINT16: return m_ctxt->get_int_type (2, false);
483 case BT_UINT32: return m_ctxt->get_int_type (4, false);
484 case BT_UINT64: return m_ctxt->get_int_type (8, false);
485 case BT_UINT128: return m_ctxt->get_int_type (16, false);
486 // case BT_WORD:
487 // case BT_UNWINDWORD:
488 case BT_FLOAT: return m_ctxt->get_type (GCC_JIT_TYPE_FLOAT);
489 case BT_DOUBLE: return m_ctxt->get_type (GCC_JIT_TYPE_DOUBLE);
490 case BT_LONGDOUBLE: return m_ctxt->get_type (GCC_JIT_TYPE_LONG_DOUBLE);
491 // case BT_FLOAT16:
492 // case BT_FLOAT32:
493 // case BT_FLOAT64:
494 // case BT_FLOAT128:
495 // case BT_FLOAT32X:
496 // case BT_FLOAT64X:
497 // case BT_FLOAT128X:
498 case BT_COMPLEX_FLOAT:
499 return m_ctxt->get_type (GCC_JIT_TYPE_COMPLEX_FLOAT);
500 case BT_COMPLEX_DOUBLE:
501 return m_ctxt->get_type (GCC_JIT_TYPE_COMPLEX_DOUBLE);
502 case BT_COMPLEX_LONGDOUBLE:
503 return m_ctxt->get_type (GCC_JIT_TYPE_COMPLEX_LONG_DOUBLE);
504 case BT_PTR: return m_ctxt->get_type (GCC_JIT_TYPE_VOID_PTR);
505 case BT_FILEPTR: return m_ctxt->get_type (GCC_JIT_TYPE_FILE_PTR);
506 // case BT_CONST_TM_PTR:
507 // case BT_FENV_T_PTR:
508 // case BT_CONST_FENV_T_PTR:
509 // case BT_FEXCEPT_T_PTR:
510 // case BT_CONST_FEXCEPT_T_PTR:
511 case BT_CONST_PTR:
512 return m_ctxt->get_type (GCC_JIT_TYPE_VOID)->get_const ()->get_pointer ();
513 case BT_VOLATILE_PTR:
514 return (m_ctxt->get_type (GCC_JIT_TYPE_VOID)->get_volatile ()
515 ->get_pointer ());
516 case BT_CONST_VOLATILE_PTR:
517 return (m_ctxt->get_type (GCC_JIT_TYPE_VOID)->get_const ()
518 ->get_volatile ()->get_pointer ());
519 // case BT_PTRMODE:
520 case BT_INT_PTR:
521 return m_ctxt->get_type (GCC_JIT_TYPE_INT)->get_pointer ();
522 case BT_FLOAT_PTR:
523 return m_ctxt->get_type (GCC_JIT_TYPE_FLOAT)->get_pointer ();
524 case BT_DOUBLE_PTR:
525 return m_ctxt->get_type (GCC_JIT_TYPE_DOUBLE)->get_pointer ();
526 case BT_CONST_DOUBLE_PTR:
527 return (m_ctxt->get_type (GCC_JIT_TYPE_DOUBLE)->get_const ()
528 ->get_pointer ());
529 // case BT_LONGDOUBLE_PTR:
530 // case BT_PID:
531 case BT_SIZE:
532 return m_ctxt->get_type (GCC_JIT_TYPE_SIZE_T);
533 case BT_CONST_SIZE:
534 return m_ctxt->get_type (GCC_JIT_TYPE_SIZE_T)->get_const ();
535 // case BT_SSIZE:
536 // case BT_WINT:
537 // case BT_STRING:
538 case BT_CONST_STRING: return m_ctxt->get_type (GCC_JIT_TYPE_CONST_CHAR_PTR);
539 // case BT_DFLOAT32:
540 // case BT_DFLOAT64:
541 // case BT_DFLOAT128:
542 // case BT_VALIST_REF:
543 // case BT_VALIST_ARG:
544 case BT_I1: return m_ctxt->get_int_type (1, true);
545 case BT_I2: return m_ctxt->get_int_type (2, true);
546 case BT_I4: return m_ctxt->get_int_type (4, true);
547 case BT_I8: return m_ctxt->get_int_type (8, true);
548 case BT_I16: return m_ctxt->get_int_type (16, true);
549 // case BT_PTR_CONST_STRING:
550 }
551 }
552
553 /* Create the recording::function_type for a given function type
554 signature. */
555
556 recording::function_type *
557 builtins_manager::make_fn_type (enum jit_builtin_type,
558 enum jit_builtin_type return_type_id,
559 bool is_variadic,
560 int num_args, ...)
561 {
562 va_list list;
563 int i;
564 recording::type **param_types = new recording::type *[num_args];
565 recording::type *return_type = NULL;
566 recording::function_type *result = NULL;
567
568 va_start (list, num_args);
569 for (i = 0; i < num_args; ++i)
570 {
571 enum jit_builtin_type arg_type_id =
572 (enum jit_builtin_type) va_arg (list, int);
573 param_types[i] = get_type (arg_type_id);
574 if (!param_types[i])
575 goto error;
576 }
577 va_end (list);
578
579 return_type = get_type (return_type_id);
580 if (!return_type)
581 goto error;
582
583 result = m_ctxt->new_function_type (return_type,
584 num_args,
585 param_types,
586 is_variadic,
587 false);
588
589 error:
590 delete[] param_types;
591 return result;
592 }
593
594 /* Handler for DEF_POINTER_TYPE within builtins_manager::make_type. */
595
596 recording::type *
597 builtins_manager::make_ptr_type (enum jit_builtin_type,
598 enum jit_builtin_type other_type_id)
599 {
600 recording::type *base_type = get_type (other_type_id);
601 return base_type->get_pointer ();
602 }
603
604 /* Ensure that builtins that could be needed during optimization
605 get created ahead of time. */
606
607 void
608 builtins_manager::ensure_optimization_builtins_exist ()
609 {
610 /* build_common_builtin_nodes does most of this, but not all.
611 We can't loop through all of the builtin_data array, we don't
612 support all types yet. */
613 (void)get_builtin_function_by_id (BUILT_IN_TRAP);
614 (void)get_builtin_function_by_id (BUILT_IN_POPCOUNT);
615 (void)get_builtin_function_by_id (BUILT_IN_POPCOUNTL);
616 (void)get_builtin_function_by_id (BUILT_IN_POPCOUNTLL);
617 (void)get_builtin_function_by_id (BUILT_IN_CLZ);
618 (void)get_builtin_function_by_id (BUILT_IN_CTZ);
619 (void)get_builtin_function_by_id (BUILT_IN_CLZL);
620 (void)get_builtin_function_by_id (BUILT_IN_CTZL);
621 (void)get_builtin_function_by_id (BUILT_IN_CLZLL);
622 (void)get_builtin_function_by_id (BUILT_IN_CTZLL);
623 }
624
625 /* Playback support. */
626
627 /* A builtins_manager is associated with a recording::context
628 and might be reused for multiple compiles on various
629 playback::contexts, perhaps with different options.
630
631 Purge any playback state. Currently this is just the table of
632 attributes. */
633
634 void
635 builtins_manager::finish_playback (void)
636 {
637 memset (m_attributes, 0, sizeof (m_attributes));
638 }
639
640 /* Get the enum built_in_class for BUILTIN_ID. */
641
642 enum built_in_class
643 builtins_manager::get_class (enum built_in_function builtin_id)
644 {
645 return builtin_data[builtin_id].fnclass;
646 }
647
648 /* Is BUILTIN_ID implicit? */
649
650 bool
651 builtins_manager::implicit_p (enum built_in_function builtin_id)
652 {
653 return builtin_data[builtin_id].implicit_p;
654 }
655
656 /* Get any attributes (in tree form) for the function declaration
657 for BUILTIN_ID.
658
659 These are created on-demand, and cached within the m_attributes
660 array, until finish_playback. */
661
662 tree
663 builtins_manager::get_attrs_tree (enum built_in_function builtin_id)
664 {
665 enum built_in_attribute attr = builtin_data[builtin_id].attr;
666 return get_attrs_tree (attr);
667 }
668
669 /* As above, but for an enum built_in_attribute. */
670
671 tree
672 builtins_manager::get_attrs_tree (enum built_in_attribute attr)
673 {
674 gcc_assert (attr < ATTR_LAST);
675 if (!m_attributes [attr])
676 m_attributes [attr] = make_attrs_tree (attr);
677 return m_attributes [attr];
678 }
679
680 /* Handle a cache-miss within the m_attributes array by
681 generating the attributes for enum built_in_attribute
682 in tree form. */
683
684 tree
685 builtins_manager::make_attrs_tree (enum built_in_attribute attr)
686 {
687 switch (attr)
688 {
689 /* Generate cases from builtin-attrs.def. */
690 #define DEF_ATTR_NULL_TREE(ENUM) \
691 case ENUM: return NULL_TREE;
692 #define DEF_ATTR_INT(ENUM, VALUE) \
693 case ENUM: return build_int_cst (integer_type_node, VALUE);
694 #define DEF_ATTR_STRING(ENUM, VALUE) \
695 case ENUM: return build_string (strlen (VALUE), VALUE);
696 #define DEF_ATTR_IDENT(ENUM, STRING) \
697 case ENUM: return get_identifier (STRING);
698 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) \
699 case ENUM: return tree_cons (get_attrs_tree (PURPOSE), \
700 get_attrs_tree (VALUE), \
701 get_attrs_tree (CHAIN));
702 #include "builtin-attrs.def"
703 #undef DEF_ATTR_NULL_TREE
704 #undef DEF_ATTR_INT
705 #undef DEF_ATTR_IDENT
706 #undef DEF_ATTR_TREE_LIST
707
708 default:
709 /* We somehow got a value not covered by the autogenerated
710 cases. */
711 gcc_unreachable ();
712 return NULL;
713 }
714 }
715
716 } // namespace jit
717 } // namespace gcc