Branch data Line data Source code
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 : 12630 : const char *get_asm_name () const
48 : : {
49 : 105 : if (both_p && fallback_p)
50 : 105 : return name + prefix_len;
51 : : else
52 : 12525 : 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 : 268680 : matches_builtin (const char *in_name,
68 : : const struct builtin_data& bd)
69 : : {
70 : 268680 : const bool debug = 0;
71 : :
72 : : /* Ignore entries with a NULL name. */
73 : 268680 : if (!bd.name)
74 : : return false;
75 : :
76 : 268660 : if (debug)
77 : : fprintf (stderr, "seen builtin: %s\n", bd.name);
78 : :
79 : 268660 : if (strcmp (bd.name, in_name) == 0)
80 : : return true;
81 : :
82 : 268470 : 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 : 97550 : gcc_assert (strncmp (bd.name, prefix, prefix_len) == 0);
88 : 97550 : if (debug)
89 : : fprintf (stderr, "testing without prefix as: %s\n",
90 : : bd.name + prefix_len);
91 : 97550 : if (strcmp (bd.name + prefix_len, in_name) == 0)
92 : 60 : 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 : 255 : 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 : 268685 : for (unsigned int i = 1; i < ARRAY_SIZE (builtin_data); i++)
113 : : {
114 : 268680 : const struct builtin_data& bd = builtin_data[i];
115 : 268680 : if (matches_builtin (in_name, bd))
116 : : {
117 : : /* Found a match. */
118 : 250 : *out_id = static_cast<enum built_in_function> (i);
119 : 250 : 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 : 1249 : builtins_manager::builtins_manager (recording::context *ctxt)
132 : 1249 : : m_ctxt (ctxt)
133 : : {
134 : 1249 : memset (m_types, 0, sizeof (m_types));
135 : 1249 : memset (m_builtin_functions, 0, sizeof (m_builtin_functions));
136 : 1249 : memset (m_attributes, 0, sizeof (m_attributes));
137 : 1249 : }
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 : 255 : builtins_manager::get_builtin_function (const char *name)
145 : : {
146 : 255 : enum built_in_function builtin_id;
147 : 255 : if (!find_builtin_by_name (name, &builtin_id))
148 : : {
149 : 5 : m_ctxt->add_error (NULL, "builtin \"%s\" not found", name);
150 : 5 : return NULL;
151 : : }
152 : :
153 : 250 : 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 : 12805 : builtins_manager::get_builtin_function_by_id (enum built_in_function builtin_id)
162 : : {
163 : 12805 : gcc_assert (builtin_id > BUILT_IN_NONE);
164 : 12805 : 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 : 12805 : if (!m_builtin_functions[builtin_id])
169 : : {
170 : 12635 : recording::function *fn = make_builtin_function (builtin_id);
171 : 12635 : if (fn)
172 : : {
173 : 12630 : m_builtin_functions[builtin_id] = fn;
174 : 12630 : m_ctxt->record (fn);
175 : : }
176 : : }
177 : :
178 : 12805 : 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 : 12635 : builtins_manager::make_builtin_function (enum built_in_function builtin_id)
185 : : {
186 : 12635 : const struct builtin_data& bd = builtin_data[builtin_id];
187 : 12635 : enum jit_builtin_type type_id = bd.type;
188 : 12635 : recording::type *t = get_type (type_id);
189 : 12635 : if (!t)
190 : : return NULL;
191 : 12630 : recording::function_type *func_type = t->as_a_function_type ();
192 : 12630 : if (!func_type)
193 : : return NULL;
194 : :
195 : 12630 : vec<recording::type *> param_types = func_type->get_param_types ();
196 : 12630 : recording::param **params = new recording::param *[param_types.length ()];
197 : :
198 : 12630 : int i;
199 : 12630 : recording::type *param_type;
200 : 24206 : FOR_EACH_VEC_ELT (param_types, i, param_type)
201 : : {
202 : 11576 : char buf[16];
203 : 11576 : snprintf (buf, 16, "arg%d", i);
204 : 11576 : params[i] = m_ctxt->new_param (NULL,
205 : : param_type,
206 : : buf);
207 : : }
208 : 12630 : const char *asm_name = bd.get_asm_name ();
209 : 12630 : recording::function *result =
210 : : new recording::function (m_ctxt,
211 : : NULL,
212 : : GCC_JIT_FUNCTION_IMPORTED, // FIXME
213 : : func_type->get_return_type (),
214 : 12630 : m_ctxt->new_string (asm_name),
215 : 12630 : param_types.length (),
216 : : params,
217 : : func_type->is_variadic (),
218 : : builtin_id,
219 : 23971 : false);
220 : 12630 : 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 : 12630 : if (builtin_id == BUILT_IN_COS || builtin_id == BUILT_IN_SIN)
230 : 40 : (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 : 12630 : if (builtin_id == BUILT_IN_CEXPIF)
238 : 0 : (void)get_builtin_function_by_id (BUILT_IN_SINCOSF);
239 : 12630 : else if (builtin_id == BUILT_IN_CEXPI)
240 : 25 : (void)get_builtin_function_by_id (BUILT_IN_SINCOS);
241 : 12605 : else if (builtin_id == BUILT_IN_CEXPIL)
242 : 0 : (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 : 5 : get_string_for_type_id (enum jit_builtin_type type_id)
321 : : {
322 : 5 : gcc_assert (type_id < ARRAY_SIZE (type_names));
323 : 5 : 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 : 21913 : builtins_manager::get_type (enum jit_builtin_type type_id)
331 : : {
332 : 21913 : if (!m_types[type_id])
333 : 11626 : m_types[type_id] = make_type (type_id);
334 : 21913 : return m_types[type_id];
335 : : }
336 : :
337 : : /* Create the recording::type for a given type of builtin function. */
338 : :
339 : : recording::type *
340 : 11626 : 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 : 11626 : 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 : 0 : default:
447 : 0 : 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 : 6475 : builtins_manager::make_primitive_type (enum jit_builtin_type type_id)
458 : : {
459 : 6475 : switch (type_id)
460 : : {
461 : 5 : default:
462 : : // only some of these types are implemented so far:
463 : 5 : m_ctxt->add_error (NULL,
464 : : "unimplemented primitive type for builtin (type: %s)",
465 : : get_string_for_type_id (type_id));
466 : 5 : return NULL;
467 : :
468 : 1239 : case BT_VOID: return m_ctxt->get_type (GCC_JIT_TYPE_VOID);
469 : 25 : case BT_BOOL: return m_ctxt->get_type (GCC_JIT_TYPE_BOOL);
470 : 1239 : case BT_INT: return m_ctxt->get_type (GCC_JIT_TYPE_INT);
471 : 1239 : case BT_UINT: return m_ctxt->get_type (GCC_JIT_TYPE_UNSIGNED_INT);
472 : 0 : case BT_LONG: return m_ctxt->get_type (GCC_JIT_TYPE_LONG);
473 : 1239 : case BT_ULONG: return m_ctxt->get_type (GCC_JIT_TYPE_UNSIGNED_LONG);
474 : 0 : case BT_LONGLONG: return m_ctxt->get_type (GCC_JIT_TYPE_LONG_LONG);
475 : 1239 : case BT_ULONGLONG:
476 : 1239 : return m_ctxt->get_type (GCC_JIT_TYPE_UNSIGNED_LONG_LONG);
477 : : // case BT_INTMAX:
478 : : // case BT_UINTMAX:
479 : 0 : case BT_INT8: return m_ctxt->get_int_type (1, true);
480 : 0 : case BT_INT16: return m_ctxt->get_int_type (2, true);
481 : 0 : case BT_UINT8: return m_ctxt->get_int_type (1, false);
482 : 0 : case BT_UINT16: return m_ctxt->get_int_type (2, false);
483 : 0 : case BT_UINT32: return m_ctxt->get_int_type (4, false);
484 : 0 : case BT_UINT64: return m_ctxt->get_int_type (8, false);
485 : 0 : case BT_UINT128: return m_ctxt->get_int_type (16, false);
486 : : // case BT_WORD:
487 : : // case BT_UNWINDWORD:
488 : 0 : case BT_FLOAT: return m_ctxt->get_type (GCC_JIT_TYPE_FLOAT);
489 : 25 : case BT_DOUBLE: return m_ctxt->get_type (GCC_JIT_TYPE_DOUBLE);
490 : 0 : 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 : 0 : case BT_COMPLEX_FLOAT:
499 : 0 : return m_ctxt->get_type (GCC_JIT_TYPE_COMPLEX_FLOAT);
500 : 25 : case BT_COMPLEX_DOUBLE:
501 : 25 : return m_ctxt->get_type (GCC_JIT_TYPE_COMPLEX_DOUBLE);
502 : 0 : case BT_COMPLEX_LONGDOUBLE:
503 : 0 : return m_ctxt->get_type (GCC_JIT_TYPE_COMPLEX_LONG_DOUBLE);
504 : 25 : case BT_PTR: return m_ctxt->get_type (GCC_JIT_TYPE_VOID_PTR);
505 : 0 : 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 : 25 : case BT_CONST_PTR:
512 : 25 : return m_ctxt->get_type (GCC_JIT_TYPE_VOID)->get_const ()->get_pointer ();
513 : 15 : case BT_VOLATILE_PTR:
514 : 15 : return (m_ctxt->get_type (GCC_JIT_TYPE_VOID)->get_volatile ()
515 : 15 : ->get_pointer ());
516 : 25 : case BT_CONST_VOLATILE_PTR:
517 : 25 : return (m_ctxt->get_type (GCC_JIT_TYPE_VOID)->get_const ()
518 : 25 : ->get_volatile ()->get_pointer ());
519 : : // case BT_PTRMODE:
520 : 15 : case BT_INT_PTR:
521 : 15 : return m_ctxt->get_type (GCC_JIT_TYPE_INT)->get_pointer ();
522 : 0 : case BT_FLOAT_PTR:
523 : 0 : return m_ctxt->get_type (GCC_JIT_TYPE_FLOAT)->get_pointer ();
524 : 25 : case BT_DOUBLE_PTR:
525 : 25 : return m_ctxt->get_type (GCC_JIT_TYPE_DOUBLE)->get_pointer ();
526 : 0 : case BT_CONST_DOUBLE_PTR:
527 : 0 : return (m_ctxt->get_type (GCC_JIT_TYPE_DOUBLE)->get_const ()
528 : 0 : ->get_pointer ());
529 : : // case BT_LONGDOUBLE_PTR:
530 : : // case BT_PID:
531 : 25 : case BT_SIZE:
532 : 25 : return m_ctxt->get_type (GCC_JIT_TYPE_SIZE_T);
533 : 0 : case BT_CONST_SIZE:
534 : 0 : 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 : 15 : 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 : 0 : case BT_I1: return m_ctxt->get_int_type (1, true);
545 : 0 : case BT_I2: return m_ctxt->get_int_type (2, true);
546 : 15 : case BT_I4: return m_ctxt->get_int_type (4, true);
547 : 15 : case BT_I8: return m_ctxt->get_int_type (8, true);
548 : 0 : 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 : 5151 : 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 : 5151 : va_list list;
563 : 5151 : int i;
564 : 5151 : recording::type **param_types = new recording::type *[num_args];
565 : 5151 : recording::type *return_type = NULL;
566 : 5151 : recording::function_type *result = NULL;
567 : :
568 : 5151 : va_start (list, num_args);
569 : 9278 : for (i = 0; i < num_args; ++i)
570 : : {
571 : 4127 : enum jit_builtin_type arg_type_id =
572 : 4127 : (enum jit_builtin_type) va_arg (list, int);
573 : 4127 : param_types[i] = get_type (arg_type_id);
574 : 4127 : if (!param_types[i])
575 : 0 : goto error;
576 : : }
577 : 5151 : va_end (list);
578 : :
579 : 5151 : return_type = get_type (return_type_id);
580 : 5151 : if (!return_type)
581 : 5 : goto error;
582 : :
583 : 5146 : result = m_ctxt->new_function_type (return_type,
584 : : num_args,
585 : : param_types,
586 : : is_variadic,
587 : : false);
588 : :
589 : 5151 : error:
590 : 5151 : delete[] param_types;
591 : 5151 : return result;
592 : : }
593 : :
594 : : /* Handler for DEF_POINTER_TYPE within builtins_manager::make_type. */
595 : :
596 : : recording::type *
597 : 0 : builtins_manager::make_ptr_type (enum jit_builtin_type,
598 : : enum jit_builtin_type other_type_id)
599 : : {
600 : 0 : recording::type *base_type = get_type (other_type_id);
601 : 0 : 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 : 1249 : 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 : 1249 : (void)get_builtin_function_by_id (BUILT_IN_TRAP);
614 : 1249 : (void)get_builtin_function_by_id (BUILT_IN_POPCOUNT);
615 : 1249 : (void)get_builtin_function_by_id (BUILT_IN_POPCOUNTL);
616 : 1249 : (void)get_builtin_function_by_id (BUILT_IN_POPCOUNTLL);
617 : 1249 : (void)get_builtin_function_by_id (BUILT_IN_CLZ);
618 : 1249 : (void)get_builtin_function_by_id (BUILT_IN_CTZ);
619 : 1249 : (void)get_builtin_function_by_id (BUILT_IN_CLZL);
620 : 1249 : (void)get_builtin_function_by_id (BUILT_IN_CTZL);
621 : 1249 : (void)get_builtin_function_by_id (BUILT_IN_CLZLL);
622 : 1249 : (void)get_builtin_function_by_id (BUILT_IN_CTZLL);
623 : 1249 : }
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 : 1249 : builtins_manager::finish_playback (void)
636 : : {
637 : 1249 : memset (m_attributes, 0, sizeof (m_attributes));
638 : 1249 : }
639 : :
640 : : /* Get the enum built_in_class for BUILTIN_ID. */
641 : :
642 : : enum built_in_class
643 : 12350 : builtins_manager::get_class (enum built_in_function builtin_id)
644 : : {
645 : 12350 : return builtin_data[builtin_id].fnclass;
646 : : }
647 : :
648 : : /* Is BUILTIN_ID implicit? */
649 : :
650 : : bool
651 : 12350 : builtins_manager::implicit_p (enum built_in_function builtin_id)
652 : : {
653 : 12350 : 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 : 12350 : builtins_manager::get_attrs_tree (enum built_in_function builtin_id)
664 : : {
665 : 12350 : enum built_in_attribute attr = builtin_data[builtin_id].attr;
666 : 12350 : return get_attrs_tree (attr);
667 : : }
668 : :
669 : : /* As above, but for an enum built_in_attribute. */
670 : :
671 : : tree
672 : 31205 : builtins_manager::get_attrs_tree (enum built_in_attribute attr)
673 : : {
674 : 31205 : gcc_assert (attr < ATTR_LAST);
675 : 31205 : if (!m_attributes [attr])
676 : 19994 : m_attributes [attr] = make_attrs_tree (attr);
677 : 31205 : 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 : 19994 : builtins_manager::make_attrs_tree (enum built_in_attribute attr)
686 : : {
687 : 19994 : 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 : 0 : default:
709 : : /* We somehow got a value not covered by the autogenerated
710 : : cases. */
711 : 0 : gcc_unreachable ();
712 : : return NULL;
713 : : }
714 : : }
715 : :
716 : : } // namespace jit
717 : : } // namespace gcc
|