rtl: ICE with thread_local and inline asm [PR104777]
[official-gcc.git] / gcc / jit / libgccjit.h
blob95d60f845db5f5a6fce1b23815bf1760d58c86b0
1 /* A pure C API to enable client code to embed GCC as a JIT-compiler.
2 Copyright (C) 2013-2022 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
9 any later version.
11 GCC is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License 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 #ifndef LIBGCCJIT_H
21 #define LIBGCCJIT_H
23 #include <stdio.h>
25 #ifdef __cplusplus
26 extern "C" {
27 #endif /* __cplusplus */
29 /**********************************************************************
30 Data structures.
31 **********************************************************************/
32 /* All structs within the API are opaque. */
34 /* A gcc_jit_context encapsulates the state of a compilation.
35 You can set up options on it, and add types, functions and code, using
36 the API below.
38 Invoking gcc_jit_context_compile on it gives you a gcc_jit_result *
39 (or NULL), representing in-memory machine code.
41 You can call gcc_jit_context_compile repeatedly on one context, giving
42 multiple independent results.
44 Similarly, you can call gcc_jit_context_compile_to_file on a context
45 to compile to disk.
47 Eventually you can call gcc_jit_context_release to clean up the
48 context; any in-memory results created from it are still usable, and
49 should be cleaned up via gcc_jit_result_release. */
50 typedef struct gcc_jit_context gcc_jit_context;
52 /* A gcc_jit_result encapsulates the result of an in-memory compilation. */
53 typedef struct gcc_jit_result gcc_jit_result;
55 /* An object created within a context. Such objects are automatically
56 cleaned up when the context is released.
58 The class hierarchy looks like this:
60 +- gcc_jit_object
61 +- gcc_jit_location
62 +- gcc_jit_type
63 +- gcc_jit_struct
64 +- gcc_jit_function_type
65 +- gcc_jit_vector_type
66 +- gcc_jit_field
67 +- gcc_jit_function
68 +- gcc_jit_block
69 +- gcc_jit_rvalue
70 +- gcc_jit_lvalue
71 +- gcc_jit_param
72 +- gcc_jit_case
73 +- gcc_jit_extended_asm
75 typedef struct gcc_jit_object gcc_jit_object;
77 /* A gcc_jit_location encapsulates a source code location, so that
78 you can (optionally) associate locations in your language with
79 statements in the JIT-compiled code, allowing the debugger to
80 single-step through your language.
82 Note that to do so, you also need to enable
83 GCC_JIT_BOOL_OPTION_DEBUGINFO
84 on the gcc_jit_context.
86 gcc_jit_location instances are optional; you can always pass
87 NULL. */
88 typedef struct gcc_jit_location gcc_jit_location;
90 /* A gcc_jit_type encapsulates a type e.g. "int" or a "struct foo*". */
91 typedef struct gcc_jit_type gcc_jit_type;
93 /* A gcc_jit_field encapsulates a field within a struct; it is used
94 when creating a struct type (using gcc_jit_context_new_struct_type).
95 Fields cannot be shared between structs. */
96 typedef struct gcc_jit_field gcc_jit_field;
98 /* A gcc_jit_struct encapsulates a struct type, either one that we have
99 the layout for, or an opaque type. */
100 typedef struct gcc_jit_struct gcc_jit_struct;
102 /* A gcc_jit_function_type encapsulates a function type. */
103 typedef struct gcc_jit_function_type gcc_jit_function_type;
105 /* A gcc_jit_vector_type encapsulates a vector type. */
106 typedef struct gcc_jit_vector_type gcc_jit_vector_type;
108 /* A gcc_jit_function encapsulates a function: either one that you're
109 creating yourself, or a reference to one that you're dynamically
110 linking to within the rest of the process. */
111 typedef struct gcc_jit_function gcc_jit_function;
113 /* A gcc_jit_block encapsulates a "basic block" of statements within a
114 function (i.e. with one entry point and one exit point).
116 Every block within a function must be terminated with a conditional,
117 a branch, or a return.
119 The blocks within a function form a directed graph.
121 The entrypoint to the function is the first block created within
124 All of the blocks in a function must be reachable via some path from
125 the first block.
127 It's OK to have more than one "return" from a function (i.e. multiple
128 blocks that terminate by returning). */
129 typedef struct gcc_jit_block gcc_jit_block;
131 /* A gcc_jit_rvalue is an expression within your code, with some type. */
132 typedef struct gcc_jit_rvalue gcc_jit_rvalue;
134 /* A gcc_jit_lvalue is a storage location within your code (e.g. a
135 variable, a parameter, etc). It is also a gcc_jit_rvalue; use
136 gcc_jit_lvalue_as_rvalue to cast. */
137 typedef struct gcc_jit_lvalue gcc_jit_lvalue;
139 /* A gcc_jit_param is a function parameter, used when creating a
140 gcc_jit_function. It is also a gcc_jit_lvalue (and thus also an
141 rvalue); use gcc_jit_param_as_lvalue to convert. */
142 typedef struct gcc_jit_param gcc_jit_param;
144 /* A gcc_jit_case is for use when building multiway branches via
145 gcc_jit_block_end_with_switch and represents a range of integer
146 values (or an individual integer value) together with an associated
147 destination block. */
148 typedef struct gcc_jit_case gcc_jit_case;
150 /* A gcc_jit_extended_asm represents an assembly language statement,
151 analogous to an extended "asm" statement in GCC's C front-end: a series
152 of low-level instructions inside a function that convert inputs to
153 outputs. */
154 typedef struct gcc_jit_extended_asm gcc_jit_extended_asm;
156 /* Acquire a JIT-compilation context. */
157 extern gcc_jit_context *
158 gcc_jit_context_acquire (void);
160 /* Release the context. After this call, it's no longer valid to use
161 the ctxt. */
162 extern void
163 gcc_jit_context_release (gcc_jit_context *ctxt);
165 /* Options present in the initial release of libgccjit.
166 These were handled using enums. */
168 /* Options taking string values. */
169 enum gcc_jit_str_option
171 /* The name of the program, for use as a prefix when printing error
172 messages to stderr. If NULL, or default, "libgccjit.so" is used. */
173 GCC_JIT_STR_OPTION_PROGNAME,
175 GCC_JIT_NUM_STR_OPTIONS
178 /* Options taking int values. */
179 enum gcc_jit_int_option
181 /* How much to optimize the code.
182 Valid values are 0-3, corresponding to GCC's command-line options
183 -O0 through -O3.
185 The default value is 0 (unoptimized). */
186 GCC_JIT_INT_OPTION_OPTIMIZATION_LEVEL,
188 GCC_JIT_NUM_INT_OPTIONS
191 /* Options taking boolean values.
192 These all default to "false". */
193 enum gcc_jit_bool_option
195 /* If true, gcc_jit_context_compile will attempt to do the right
196 thing so that if you attach a debugger to the process, it will
197 be able to inspect variables and step through your code.
199 Note that you can't step through code unless you set up source
200 location information for the code (by creating and passing in
201 gcc_jit_location instances). */
202 GCC_JIT_BOOL_OPTION_DEBUGINFO,
204 /* If true, gcc_jit_context_compile will dump its initial "tree"
205 representation of your code to stderr (before any
206 optimizations). */
207 GCC_JIT_BOOL_OPTION_DUMP_INITIAL_TREE,
209 /* If true, gcc_jit_context_compile will dump the "gimple"
210 representation of your code to stderr, before any optimizations
211 are performed. The dump resembles C code. */
212 GCC_JIT_BOOL_OPTION_DUMP_INITIAL_GIMPLE,
214 /* If true, gcc_jit_context_compile will dump the final
215 generated code to stderr, in the form of assembly language. */
216 GCC_JIT_BOOL_OPTION_DUMP_GENERATED_CODE,
218 /* If true, gcc_jit_context_compile will print information to stderr
219 on the actions it is performing, followed by a profile showing
220 the time taken and memory usage of each phase.
222 GCC_JIT_BOOL_OPTION_DUMP_SUMMARY,
224 /* If true, gcc_jit_context_compile will dump copious
225 amount of information on what it's doing to various
226 files within a temporary directory. Use
227 GCC_JIT_BOOL_OPTION_KEEP_INTERMEDIATES (see below) to
228 see the results. The files are intended to be human-readable,
229 but the exact files and their formats are subject to change.
231 GCC_JIT_BOOL_OPTION_DUMP_EVERYTHING,
233 /* If true, libgccjit will aggressively run its garbage collector, to
234 shake out bugs (greatly slowing down the compile). This is likely
235 to only be of interest to developers *of* the library. It is
236 used when running the selftest suite. */
237 GCC_JIT_BOOL_OPTION_SELFCHECK_GC,
239 /* If true, gcc_jit_context_release will not clean up
240 intermediate files written to the filesystem, and will display
241 their location on stderr. */
242 GCC_JIT_BOOL_OPTION_KEEP_INTERMEDIATES,
244 GCC_JIT_NUM_BOOL_OPTIONS
247 /* Set a string option on the given context.
249 The context takes a copy of the string, so the
250 (const char *) buffer is not needed anymore after the call
251 returns. */
252 extern void
253 gcc_jit_context_set_str_option (gcc_jit_context *ctxt,
254 enum gcc_jit_str_option opt,
255 const char *value);
257 /* Set an int option on the given context. */
258 extern void
259 gcc_jit_context_set_int_option (gcc_jit_context *ctxt,
260 enum gcc_jit_int_option opt,
261 int value);
263 /* Set a boolean option on the given context.
265 Zero is "false" (the default), non-zero is "true". */
266 extern void
267 gcc_jit_context_set_bool_option (gcc_jit_context *ctxt,
268 enum gcc_jit_bool_option opt,
269 int value);
271 /* Options added after the initial release of libgccjit.
272 These are handled by providing an entrypoint per option,
273 rather than by extending the enum gcc_jit_*_option,
274 so that client code that use these new options can be identified
275 from binary metadata. */
277 /* By default, libgccjit will issue an error about unreachable blocks
278 within a function.
280 This option can be used to disable that error.
282 This entrypoint was added in LIBGCCJIT_ABI_2; you can test for
283 its presence using
284 #ifdef LIBGCCJIT_HAVE_gcc_jit_context_set_bool_allow_unreachable_blocks
287 extern void
288 gcc_jit_context_set_bool_allow_unreachable_blocks (gcc_jit_context *ctxt,
289 int bool_value);
291 /* Pre-canned feature macro to indicate the presence of
292 gcc_jit_context_set_bool_allow_unreachable_blocks. This can be
293 tested for with #ifdef. */
294 #define LIBGCCJIT_HAVE_gcc_jit_context_set_bool_allow_unreachable_blocks
296 /* Implementation detail:
297 libgccjit internally generates assembler, and uses "driver" code
298 for converting it to other formats (e.g. shared libraries).
300 By default, libgccjit will use an embedded copy of the driver
301 code.
303 This option can be used to instead invoke an external driver executable
304 as a subprocess.
306 This entrypoint was added in LIBGCCJIT_ABI_5; you can test for
307 its presence using
308 #ifdef LIBGCCJIT_HAVE_gcc_jit_context_set_bool_use_external_driver
311 extern void
312 gcc_jit_context_set_bool_use_external_driver (gcc_jit_context *ctxt,
313 int bool_value);
315 /* Pre-canned feature macro to indicate the presence of
316 gcc_jit_context_set_bool_use_external_driver. This can be
317 tested for with #ifdef. */
318 #define LIBGCCJIT_HAVE_gcc_jit_context_set_bool_use_external_driver
320 /* Add an arbitrary gcc command-line option to the context.
321 The context takes a copy of the string, so the
322 (const char *) optname is not needed anymore after the call
323 returns.
325 Note that only some options are likely to be meaningful; there is no
326 "frontend" within libgccjit, so typically only those affecting
327 optimization and code-generation are likely to be useful.
329 This entrypoint was added in LIBGCCJIT_ABI_1; you can test for
330 its presence using
331 #ifdef LIBGCCJIT_HAVE_gcc_jit_context_add_command_line_option
334 extern void
335 gcc_jit_context_add_command_line_option (gcc_jit_context *ctxt,
336 const char *optname);
338 /* Pre-canned feature-test macro for detecting the presence of
339 gcc_jit_context_add_command_line_option within libgccjit.h. */
341 #define LIBGCCJIT_HAVE_gcc_jit_context_add_command_line_option
343 /* Add an arbitrary gcc driver option to the context.
344 The context takes a copy of the string, so the
345 (const char *) optname is not needed anymore after the call
346 returns.
348 Note that only some options are likely to be meaningful; there is no
349 "frontend" within libgccjit, so typically only those affecting
350 assembler and linker are likely to be useful.
352 This entrypoint was added in LIBGCCJIT_ABI_11; you can test for
353 its presence using
354 #ifdef LIBGCCJIT_HAVE_gcc_jit_context_add_driver_option
356 extern void
357 gcc_jit_context_add_driver_option (gcc_jit_context *ctxt,
358 const char *optname);
360 /* Pre-canned feature-test macro for detecting the presence of
361 gcc_jit_context_add_driver_option within libgccjit.h. */
363 #define LIBGCCJIT_HAVE_gcc_jit_context_add_driver_option
365 /* Compile the context to in-memory machine code.
367 This can be called more that once on a given context,
368 although any errors that occur will block further compilation. */
370 extern gcc_jit_result *
371 gcc_jit_context_compile (gcc_jit_context *ctxt);
373 /* Kinds of ahead-of-time compilation, for use with
374 gcc_jit_context_compile_to_file. */
376 enum gcc_jit_output_kind
378 /* Compile the context to an assembler file. */
379 GCC_JIT_OUTPUT_KIND_ASSEMBLER,
381 /* Compile the context to an object file. */
382 GCC_JIT_OUTPUT_KIND_OBJECT_FILE,
384 /* Compile the context to a dynamic library. */
385 GCC_JIT_OUTPUT_KIND_DYNAMIC_LIBRARY,
387 /* Compile the context to an executable. */
388 GCC_JIT_OUTPUT_KIND_EXECUTABLE
391 /* Compile the context to a file of the given kind.
393 This can be called more that once on a given context,
394 although any errors that occur will block further compilation. */
396 extern void
397 gcc_jit_context_compile_to_file (gcc_jit_context *ctxt,
398 enum gcc_jit_output_kind output_kind,
399 const char *output_path);
401 /* To help with debugging: dump a C-like representation to the given path,
402 describing what's been set up on the context.
404 If "update_locations" is true, then also set up gcc_jit_location
405 information throughout the context, pointing at the dump file as if it
406 were a source file. This may be of use in conjunction with
407 GCC_JIT_BOOL_OPTION_DEBUGINFO to allow stepping through the code in a
408 debugger. */
409 extern void
410 gcc_jit_context_dump_to_file (gcc_jit_context *ctxt,
411 const char *path,
412 int update_locations);
414 /* To help with debugging; enable ongoing logging of the context's
415 activity to the given FILE *.
417 The caller remains responsible for closing "logfile".
419 Params "flags" and "verbosity" are reserved for future use, and
420 must both be 0 for now. */
421 extern void
422 gcc_jit_context_set_logfile (gcc_jit_context *ctxt,
423 FILE *logfile,
424 int flags,
425 int verbosity);
427 /* To be called after any API call, this gives the first error message
428 that occurred on the context.
430 The returned string is valid for the rest of the lifetime of the
431 context.
433 If no errors occurred, this will be NULL. */
434 extern const char *
435 gcc_jit_context_get_first_error (gcc_jit_context *ctxt);
437 /* To be called after any API call, this gives the last error message
438 that occurred on the context.
440 If no errors occurred, this will be NULL.
442 If non-NULL, the returned string is only guaranteed to be valid until
443 the next call to libgccjit relating to this context. */
444 extern const char *
445 gcc_jit_context_get_last_error (gcc_jit_context *ctxt);
447 /* Locate a given function within the built machine code.
448 This will need to be cast to a function pointer of the
449 correct type before it can be called. */
450 extern void *
451 gcc_jit_result_get_code (gcc_jit_result *result,
452 const char *funcname);
454 /* Locate a given global within the built machine code.
455 It must have been created using GCC_JIT_GLOBAL_EXPORTED.
456 This is a ptr to the global, so e.g. for an int this is an int *. */
457 extern void *
458 gcc_jit_result_get_global (gcc_jit_result *result,
459 const char *name);
461 /* Once we're done with the code, this unloads the built .so file.
462 This cleans up the result; after calling this, it's no longer
463 valid to use the result. */
464 extern void
465 gcc_jit_result_release (gcc_jit_result *result);
468 /**********************************************************************
469 Functions for creating "contextual" objects.
471 All objects created by these functions share the lifetime of the context
472 they are created within, and are automatically cleaned up for you when
473 you call gcc_jit_context_release on the context.
475 Note that this means you can't use references to them after you've
476 released their context.
478 All (const char *) string arguments passed to these functions are
479 copied, so you don't need to keep them around.
481 You create code by adding a sequence of statements to blocks.
482 **********************************************************************/
484 /**********************************************************************
485 The base class of "contextual" object.
486 **********************************************************************/
487 /* Which context is "obj" within? */
488 extern gcc_jit_context *
489 gcc_jit_object_get_context (gcc_jit_object *obj);
491 /* Get a human-readable description of this object.
492 The string buffer is created the first time this is called on a given
493 object, and persists until the object's context is released. */
494 extern const char *
495 gcc_jit_object_get_debug_string (gcc_jit_object *obj);
497 /**********************************************************************
498 Debugging information.
499 **********************************************************************/
501 /* Creating source code locations for use by the debugger.
502 Line and column numbers are 1-based. */
503 extern gcc_jit_location *
504 gcc_jit_context_new_location (gcc_jit_context *ctxt,
505 const char *filename,
506 int line,
507 int column);
509 /* Upcasting from location to object. */
510 extern gcc_jit_object *
511 gcc_jit_location_as_object (gcc_jit_location *loc);
514 /**********************************************************************
515 Types.
516 **********************************************************************/
518 /* Upcasting from type to object. */
519 extern gcc_jit_object *
520 gcc_jit_type_as_object (gcc_jit_type *type);
522 /* Access to specific types. */
523 enum gcc_jit_types
525 /* C's "void" type. */
526 GCC_JIT_TYPE_VOID,
528 /* "void *". */
529 GCC_JIT_TYPE_VOID_PTR,
531 /* C++'s bool type; also C99's "_Bool" type, aka "bool" if using
532 stdbool.h. */
533 GCC_JIT_TYPE_BOOL,
535 /* Various integer types. */
537 /* C's "char" (of some signedness) and the variants where the
538 signedness is specified. */
539 GCC_JIT_TYPE_CHAR,
540 GCC_JIT_TYPE_SIGNED_CHAR,
541 GCC_JIT_TYPE_UNSIGNED_CHAR,
543 /* C's "short" and "unsigned short". */
544 GCC_JIT_TYPE_SHORT, /* signed */
545 GCC_JIT_TYPE_UNSIGNED_SHORT,
547 /* C's "int" and "unsigned int". */
548 GCC_JIT_TYPE_INT, /* signed */
549 GCC_JIT_TYPE_UNSIGNED_INT,
551 /* C's "long" and "unsigned long". */
552 GCC_JIT_TYPE_LONG, /* signed */
553 GCC_JIT_TYPE_UNSIGNED_LONG,
555 /* C99's "long long" and "unsigned long long". */
556 GCC_JIT_TYPE_LONG_LONG, /* signed */
557 GCC_JIT_TYPE_UNSIGNED_LONG_LONG,
559 /* Floating-point types */
561 GCC_JIT_TYPE_FLOAT,
562 GCC_JIT_TYPE_DOUBLE,
563 GCC_JIT_TYPE_LONG_DOUBLE,
565 /* C type: (const char *). */
566 GCC_JIT_TYPE_CONST_CHAR_PTR,
568 /* The C "size_t" type. */
569 GCC_JIT_TYPE_SIZE_T,
571 /* C type: (FILE *) */
572 GCC_JIT_TYPE_FILE_PTR,
574 /* Complex numbers. */
575 GCC_JIT_TYPE_COMPLEX_FLOAT,
576 GCC_JIT_TYPE_COMPLEX_DOUBLE,
577 GCC_JIT_TYPE_COMPLEX_LONG_DOUBLE
581 extern gcc_jit_type *
582 gcc_jit_context_get_type (gcc_jit_context *ctxt,
583 enum gcc_jit_types type_);
585 /* Get the integer type of the given size and signedness. */
586 extern gcc_jit_type *
587 gcc_jit_context_get_int_type (gcc_jit_context *ctxt,
588 int num_bytes, int is_signed);
590 /* Constructing new types. */
592 /* Given type "T", get type "T*". */
593 extern gcc_jit_type *
594 gcc_jit_type_get_pointer (gcc_jit_type *type);
596 /* Given type "T", get type "const T". */
597 extern gcc_jit_type *
598 gcc_jit_type_get_const (gcc_jit_type *type);
600 /* Given type "T", get type "volatile T". */
601 extern gcc_jit_type *
602 gcc_jit_type_get_volatile (gcc_jit_type *type);
604 /* Given type "T", get type "T[N]" (for a constant N). */
605 extern gcc_jit_type *
606 gcc_jit_context_new_array_type (gcc_jit_context *ctxt,
607 gcc_jit_location *loc,
608 gcc_jit_type *element_type,
609 int num_elements);
611 /* Struct-handling. */
613 /* Create a field, for use within a struct or union. */
614 extern gcc_jit_field *
615 gcc_jit_context_new_field (gcc_jit_context *ctxt,
616 gcc_jit_location *loc,
617 gcc_jit_type *type,
618 const char *name);
620 #define LIBGCCJIT_HAVE_gcc_jit_context_new_bitfield
622 /* Create a bit field, for use within a struct or union.
624 This API entrypoint was added in LIBGCCJIT_ABI_12; you can test for its
625 presence using
626 #ifdef LIBGCCJIT_HAVE_gcc_jit_context_new_bitfield
628 extern gcc_jit_field *
629 gcc_jit_context_new_bitfield (gcc_jit_context *ctxt,
630 gcc_jit_location *loc,
631 gcc_jit_type *type,
632 int width,
633 const char *name);
635 /* Upcasting from field to object. */
636 extern gcc_jit_object *
637 gcc_jit_field_as_object (gcc_jit_field *field);
639 /* Create a struct type from an array of fields. */
640 extern gcc_jit_struct *
641 gcc_jit_context_new_struct_type (gcc_jit_context *ctxt,
642 gcc_jit_location *loc,
643 const char *name,
644 int num_fields,
645 gcc_jit_field **fields);
647 /* Create an opaque struct type. */
648 extern gcc_jit_struct *
649 gcc_jit_context_new_opaque_struct (gcc_jit_context *ctxt,
650 gcc_jit_location *loc,
651 const char *name);
653 /* Upcast a struct to a type. */
654 extern gcc_jit_type *
655 gcc_jit_struct_as_type (gcc_jit_struct *struct_type);
657 /* Populating the fields of a formerly-opaque struct type.
658 This can only be called once on a given struct type. */
659 extern void
660 gcc_jit_struct_set_fields (gcc_jit_struct *struct_type,
661 gcc_jit_location *loc,
662 int num_fields,
663 gcc_jit_field **fields);
665 /* Get a field by index. */
666 extern gcc_jit_field *
667 gcc_jit_struct_get_field (gcc_jit_struct *struct_type,
668 size_t index);
670 /* Get the number of fields. */
671 extern size_t
672 gcc_jit_struct_get_field_count (gcc_jit_struct *struct_type);
674 /* Unions work similarly to structs. */
675 extern gcc_jit_type *
676 gcc_jit_context_new_union_type (gcc_jit_context *ctxt,
677 gcc_jit_location *loc,
678 const char *name,
679 int num_fields,
680 gcc_jit_field **fields);
682 /* Function pointers. */
684 extern gcc_jit_type *
685 gcc_jit_context_new_function_ptr_type (gcc_jit_context *ctxt,
686 gcc_jit_location *loc,
687 gcc_jit_type *return_type,
688 int num_params,
689 gcc_jit_type **param_types,
690 int is_variadic);
692 /**********************************************************************
693 Constructing functions.
694 **********************************************************************/
695 /* Create a function param. */
696 extern gcc_jit_param *
697 gcc_jit_context_new_param (gcc_jit_context *ctxt,
698 gcc_jit_location *loc,
699 gcc_jit_type *type,
700 const char *name);
702 /* Upcasting from param to object. */
703 extern gcc_jit_object *
704 gcc_jit_param_as_object (gcc_jit_param *param);
706 /* Upcasting from param to lvalue. */
707 extern gcc_jit_lvalue *
708 gcc_jit_param_as_lvalue (gcc_jit_param *param);
710 /* Upcasting from param to rvalue. */
711 extern gcc_jit_rvalue *
712 gcc_jit_param_as_rvalue (gcc_jit_param *param);
714 /* Kinds of function. */
715 enum gcc_jit_function_kind
717 /* Function is defined by the client code and visible
718 by name outside of the JIT. */
719 GCC_JIT_FUNCTION_EXPORTED,
721 /* Function is defined by the client code, but is invisible
722 outside of the JIT. Analogous to a "static" function. */
723 GCC_JIT_FUNCTION_INTERNAL,
725 /* Function is not defined by the client code; we're merely
726 referring to it. Analogous to using an "extern" function from a
727 header file. */
728 GCC_JIT_FUNCTION_IMPORTED,
730 /* Function is only ever inlined into other functions, and is
731 invisible outside of the JIT.
733 Analogous to prefixing with "inline" and adding
734 __attribute__((always_inline)).
736 Inlining will only occur when the optimization level is
737 above 0; when optimization is off, this is essentially the
738 same as GCC_JIT_FUNCTION_INTERNAL. */
739 GCC_JIT_FUNCTION_ALWAYS_INLINE
742 /* Thread local storage model. */
743 enum gcc_jit_tls_model
745 GCC_JIT_TLS_MODEL_NONE,
746 GCC_JIT_TLS_MODEL_GLOBAL_DYNAMIC,
747 GCC_JIT_TLS_MODEL_LOCAL_DYNAMIC,
748 GCC_JIT_TLS_MODEL_INITIAL_EXEC,
749 GCC_JIT_TLS_MODEL_LOCAL_EXEC,
752 /* Create a function. */
753 extern gcc_jit_function *
754 gcc_jit_context_new_function (gcc_jit_context *ctxt,
755 gcc_jit_location *loc,
756 enum gcc_jit_function_kind kind,
757 gcc_jit_type *return_type,
758 const char *name,
759 int num_params,
760 gcc_jit_param **params,
761 int is_variadic);
763 /* Create a reference to a builtin function (sometimes called
764 intrinsic functions). */
765 extern gcc_jit_function *
766 gcc_jit_context_get_builtin_function (gcc_jit_context *ctxt,
767 const char *name);
769 /* Upcasting from function to object. */
770 extern gcc_jit_object *
771 gcc_jit_function_as_object (gcc_jit_function *func);
773 /* Get a specific param of a function by index. */
774 extern gcc_jit_param *
775 gcc_jit_function_get_param (gcc_jit_function *func, int index);
777 /* Emit the function in graphviz format. */
778 extern void
779 gcc_jit_function_dump_to_dot (gcc_jit_function *func,
780 const char *path);
782 /* Create a block.
784 The name can be NULL, or you can give it a meaningful name, which
785 may show up in dumps of the internal representation, and in error
786 messages. */
787 extern gcc_jit_block *
788 gcc_jit_function_new_block (gcc_jit_function *func,
789 const char *name);
791 /* Upcasting from block to object. */
792 extern gcc_jit_object *
793 gcc_jit_block_as_object (gcc_jit_block *block);
795 /* Which function is this block within? */
796 extern gcc_jit_function *
797 gcc_jit_block_get_function (gcc_jit_block *block);
799 /**********************************************************************
800 lvalues, rvalues and expressions.
801 **********************************************************************/
802 enum gcc_jit_global_kind
804 /* Global is defined by the client code and visible
805 by name outside of this JIT context via gcc_jit_result_get_global. */
806 GCC_JIT_GLOBAL_EXPORTED,
808 /* Global is defined by the client code, but is invisible
809 outside of this JIT context. Analogous to a "static" global. */
810 GCC_JIT_GLOBAL_INTERNAL,
812 /* Global is not defined by the client code; we're merely
813 referring to it. Analogous to using an "extern" global from a
814 header file. */
815 GCC_JIT_GLOBAL_IMPORTED
818 extern gcc_jit_lvalue *
819 gcc_jit_context_new_global (gcc_jit_context *ctxt,
820 gcc_jit_location *loc,
821 enum gcc_jit_global_kind kind,
822 gcc_jit_type *type,
823 const char *name);
825 #define LIBGCCJIT_HAVE_CTORS
827 /* Create a constructor for a struct as an rvalue.
829 Returns NULL on error. The two parameter arrays are copied and
830 do not have to outlive the context.
832 `type` specifies what the constructor will build and has to be
833 a struct.
835 `num_values` specifies the number of elements in `values`.
837 `fields` need to have the same length as `values`, or be NULL.
839 If `fields` is null, the values are applied in definition order.
841 Otherwise, each field in `fields` specifies which field in the struct to
842 set to the corresponding value in `values`. `fields` and `values`
843 are paired by index.
845 Each value has to have the same unqualified type as the field
846 it is applied to.
848 A NULL value element in `values` is a shorthand for zero initialization
849 of the corresponding field.
851 The fields in `fields` have to be in definition order, but there
852 can be gaps. Any field in the struct that is not specified in
853 `fields` will be zeroed.
855 The fields in `fields` need to be the same objects that were used
856 to create the struct.
858 If `num_values` is 0, the array parameters will be
859 ignored and zero initialization will be used.
861 The constructor rvalue can be used for assignment to locals.
862 It can be used to initialize global variables with
863 gcc_jit_global_set_initializer_rvalue. It can also be used as a
864 temporary value for function calls and return values.
866 The constructor can contain nested constructors.
868 This entrypoint was added in LIBGCCJIT_ABI_19; you can test for its
869 presence using:
870 #ifdef LIBGCCJIT_HAVE_CTORS
873 extern gcc_jit_rvalue *
874 gcc_jit_context_new_struct_constructor (gcc_jit_context *ctxt,
875 gcc_jit_location *loc,
876 gcc_jit_type *type,
877 size_t num_values,
878 gcc_jit_field **fields,
879 gcc_jit_rvalue **values);
881 /* Create a constructor for a union as an rvalue.
883 Returns NULL on error.
885 `type` specifies what the constructor will build and has to be
886 an union.
888 `field` specifies which field to set. If it is NULL, the first
889 field in the union will be set. `field` need to be the same
890 object that were used to create the union.
892 `value` specifies what value to set the corresponding field to.
893 If `value` is NULL, zero initialization will be used.
895 Each value has to have the same unqualified type as the field
896 it is applied to.
898 `field` need to be the same objects that were used
899 to create the union.
901 The constructor rvalue can be used for assignment to locals.
902 It can be used to initialize global variables with
903 gcc_jit_global_set_initializer_rvalue. It can also be used as a
904 temporary value for function calls and return values.
906 The constructor can contain nested constructors.
908 This entrypoint was added in LIBGCCJIT_ABI_19; you can test for its
909 presence using:
910 #ifdef LIBGCCJIT_HAVE_CTORS
913 extern gcc_jit_rvalue *
914 gcc_jit_context_new_union_constructor (gcc_jit_context *ctxt,
915 gcc_jit_location *loc,
916 gcc_jit_type *type,
917 gcc_jit_field *field,
918 gcc_jit_rvalue *value);
920 /* Create a constructor for an array as an rvalue.
922 Returns NULL on error. `values` are copied and
923 do not have to outlive the context.
925 `type` specifies what the constructor will build and has to be
926 an array.
928 `num_values` specifies the number of elements in `values` and
929 it can't have more elements than the array type.
931 Each value in `values` sets the corresponding value in the array.
932 If the array type itself has more elements than `values`, the
933 left-over elements will be zeroed.
935 Each value in `values` need to be the same unqualified type as the
936 array type's element type.
938 If `num_values` is 0, the `values` parameter will be
939 ignored and zero initialization will be used.
941 Note that a string literal rvalue can't be used to construct a char
942 array. It needs one rvalue for each char.
944 This entrypoint was added in LIBGCCJIT_ABI_19; you can test for its
945 presence using:
946 #ifdef LIBGCCJIT_HAVE_CTORS
949 extern gcc_jit_rvalue *
950 gcc_jit_context_new_array_constructor (gcc_jit_context *ctxt,
951 gcc_jit_location *loc,
952 gcc_jit_type *type,
953 size_t num_values,
954 gcc_jit_rvalue **values);
956 /* Set the initial value of a global of any type with an rvalue.
958 The rvalue needs to be a constant expression, e.g. no function calls.
960 The global can't have the 'kind' GCC_JIT_GLOBAL_IMPORTED.
962 Use together with gcc_jit_context_new_constructor () to
963 initialize structs, unions and arrays.
965 On success, returns the 'global' parameter unchanged. Otherwise, NULL.
967 'values' is copied and does not have to outlive the context.
969 This entrypoint was added in LIBGCCJIT_ABI_19; you can test for its
970 presence using:
971 #ifdef LIBGCCJIT_HAVE_CTORS
974 extern gcc_jit_lvalue *
975 gcc_jit_global_set_initializer_rvalue (gcc_jit_lvalue *global,
976 gcc_jit_rvalue *init_value);
978 #define LIBGCCJIT_HAVE_gcc_jit_global_set_initializer
980 /* Set an initial value for a global, which must be an array of
981 integral type. Return the global itself.
983 This API entrypoint was added in LIBGCCJIT_ABI_14; you can test for its
984 presence using
985 #ifdef LIBGCCJIT_HAVE_gcc_jit_global_set_initializer
988 extern gcc_jit_lvalue *
989 gcc_jit_global_set_initializer (gcc_jit_lvalue *global,
990 const void *blob,
991 size_t num_bytes);
993 /* Upcasting. */
994 extern gcc_jit_object *
995 gcc_jit_lvalue_as_object (gcc_jit_lvalue *lvalue);
997 extern gcc_jit_rvalue *
998 gcc_jit_lvalue_as_rvalue (gcc_jit_lvalue *lvalue);
1000 extern gcc_jit_object *
1001 gcc_jit_rvalue_as_object (gcc_jit_rvalue *rvalue);
1003 extern gcc_jit_type *
1004 gcc_jit_rvalue_get_type (gcc_jit_rvalue *rvalue);
1006 /* Integer constants. */
1007 extern gcc_jit_rvalue *
1008 gcc_jit_context_new_rvalue_from_int (gcc_jit_context *ctxt,
1009 gcc_jit_type *numeric_type,
1010 int value);
1012 extern gcc_jit_rvalue *
1013 gcc_jit_context_new_rvalue_from_long (gcc_jit_context *ctxt,
1014 gcc_jit_type *numeric_type,
1015 long value);
1017 extern gcc_jit_rvalue *
1018 gcc_jit_context_zero (gcc_jit_context *ctxt,
1019 gcc_jit_type *numeric_type);
1021 extern gcc_jit_rvalue *
1022 gcc_jit_context_one (gcc_jit_context *ctxt,
1023 gcc_jit_type *numeric_type);
1025 /* Floating-point constants. */
1026 extern gcc_jit_rvalue *
1027 gcc_jit_context_new_rvalue_from_double (gcc_jit_context *ctxt,
1028 gcc_jit_type *numeric_type,
1029 double value);
1031 /* Pointers. */
1032 extern gcc_jit_rvalue *
1033 gcc_jit_context_new_rvalue_from_ptr (gcc_jit_context *ctxt,
1034 gcc_jit_type *pointer_type,
1035 void *value);
1037 extern gcc_jit_rvalue *
1038 gcc_jit_context_null (gcc_jit_context *ctxt,
1039 gcc_jit_type *pointer_type);
1041 /* String literals. */
1042 extern gcc_jit_rvalue *
1043 gcc_jit_context_new_string_literal (gcc_jit_context *ctxt,
1044 const char *value);
1046 enum gcc_jit_unary_op
1048 /* Negate an arithmetic value; analogous to:
1049 -(EXPR)
1050 in C. */
1051 GCC_JIT_UNARY_OP_MINUS,
1053 /* Bitwise negation of an integer value (one's complement); analogous
1055 ~(EXPR)
1056 in C. */
1057 GCC_JIT_UNARY_OP_BITWISE_NEGATE,
1059 /* Logical negation of an arithmetic or pointer value; analogous to:
1060 !(EXPR)
1061 in C. */
1062 GCC_JIT_UNARY_OP_LOGICAL_NEGATE,
1064 /* Absolute value of an arithmetic expression; analogous to:
1065 abs (EXPR)
1066 in C. */
1067 GCC_JIT_UNARY_OP_ABS
1071 extern gcc_jit_rvalue *
1072 gcc_jit_context_new_unary_op (gcc_jit_context *ctxt,
1073 gcc_jit_location *loc,
1074 enum gcc_jit_unary_op op,
1075 gcc_jit_type *result_type,
1076 gcc_jit_rvalue *rvalue);
1078 enum gcc_jit_binary_op
1080 /* Addition of arithmetic values; analogous to:
1081 (EXPR_A) + (EXPR_B)
1082 in C.
1083 For pointer addition, use gcc_jit_context_new_array_access. */
1084 GCC_JIT_BINARY_OP_PLUS,
1086 /* Subtraction of arithmetic values; analogous to:
1087 (EXPR_A) - (EXPR_B)
1088 in C. */
1089 GCC_JIT_BINARY_OP_MINUS,
1091 /* Multiplication of a pair of arithmetic values; analogous to:
1092 (EXPR_A) * (EXPR_B)
1093 in C. */
1094 GCC_JIT_BINARY_OP_MULT,
1096 /* Quotient of division of arithmetic values; analogous to:
1097 (EXPR_A) / (EXPR_B)
1098 in C.
1099 The result type affects the kind of division: if the result type is
1100 integer-based, then the result is truncated towards zero, whereas
1101 a floating-point result type indicates floating-point division. */
1102 GCC_JIT_BINARY_OP_DIVIDE,
1104 /* Remainder of division of arithmetic values; analogous to:
1105 (EXPR_A) % (EXPR_B)
1106 in C. */
1107 GCC_JIT_BINARY_OP_MODULO,
1109 /* Bitwise AND; analogous to:
1110 (EXPR_A) & (EXPR_B)
1111 in C. */
1112 GCC_JIT_BINARY_OP_BITWISE_AND,
1114 /* Bitwise exclusive OR; analogous to:
1115 (EXPR_A) ^ (EXPR_B)
1116 in C. */
1117 GCC_JIT_BINARY_OP_BITWISE_XOR,
1119 /* Bitwise inclusive OR; analogous to:
1120 (EXPR_A) | (EXPR_B)
1121 in C. */
1122 GCC_JIT_BINARY_OP_BITWISE_OR,
1124 /* Logical AND; analogous to:
1125 (EXPR_A) && (EXPR_B)
1126 in C. */
1127 GCC_JIT_BINARY_OP_LOGICAL_AND,
1129 /* Logical OR; analogous to:
1130 (EXPR_A) || (EXPR_B)
1131 in C. */
1132 GCC_JIT_BINARY_OP_LOGICAL_OR,
1134 /* Left shift; analogous to:
1135 (EXPR_A) << (EXPR_B)
1136 in C. */
1137 GCC_JIT_BINARY_OP_LSHIFT,
1139 /* Right shift; analogous to:
1140 (EXPR_A) >> (EXPR_B)
1141 in C. */
1142 GCC_JIT_BINARY_OP_RSHIFT
1145 extern gcc_jit_rvalue *
1146 gcc_jit_context_new_binary_op (gcc_jit_context *ctxt,
1147 gcc_jit_location *loc,
1148 enum gcc_jit_binary_op op,
1149 gcc_jit_type *result_type,
1150 gcc_jit_rvalue *a, gcc_jit_rvalue *b);
1152 /* (Comparisons are treated as separate from "binary_op" to save
1153 you having to specify the result_type). */
1155 enum gcc_jit_comparison
1157 /* (EXPR_A) == (EXPR_B). */
1158 GCC_JIT_COMPARISON_EQ,
1160 /* (EXPR_A) != (EXPR_B). */
1161 GCC_JIT_COMPARISON_NE,
1163 /* (EXPR_A) < (EXPR_B). */
1164 GCC_JIT_COMPARISON_LT,
1166 /* (EXPR_A) <=(EXPR_B). */
1167 GCC_JIT_COMPARISON_LE,
1169 /* (EXPR_A) > (EXPR_B). */
1170 GCC_JIT_COMPARISON_GT,
1172 /* (EXPR_A) >= (EXPR_B). */
1173 GCC_JIT_COMPARISON_GE
1176 extern gcc_jit_rvalue *
1177 gcc_jit_context_new_comparison (gcc_jit_context *ctxt,
1178 gcc_jit_location *loc,
1179 enum gcc_jit_comparison op,
1180 gcc_jit_rvalue *a, gcc_jit_rvalue *b);
1182 /* Function calls. */
1184 /* Call of a specific function. */
1185 extern gcc_jit_rvalue *
1186 gcc_jit_context_new_call (gcc_jit_context *ctxt,
1187 gcc_jit_location *loc,
1188 gcc_jit_function *func,
1189 int numargs , gcc_jit_rvalue **args);
1191 /* Call through a function pointer. */
1192 extern gcc_jit_rvalue *
1193 gcc_jit_context_new_call_through_ptr (gcc_jit_context *ctxt,
1194 gcc_jit_location *loc,
1195 gcc_jit_rvalue *fn_ptr,
1196 int numargs, gcc_jit_rvalue **args);
1198 /* Type-coercion.
1200 Currently only a limited set of conversions are possible:
1201 int <-> float
1202 int <-> bool */
1203 extern gcc_jit_rvalue *
1204 gcc_jit_context_new_cast (gcc_jit_context *ctxt,
1205 gcc_jit_location *loc,
1206 gcc_jit_rvalue *rvalue,
1207 gcc_jit_type *type);
1209 extern gcc_jit_lvalue *
1210 gcc_jit_context_new_array_access (gcc_jit_context *ctxt,
1211 gcc_jit_location *loc,
1212 gcc_jit_rvalue *ptr,
1213 gcc_jit_rvalue *index);
1215 /* Field access is provided separately for both lvalues and rvalues. */
1217 /* Accessing a field of an lvalue of struct type, analogous to:
1218 (EXPR).field = ...;
1219 in C. */
1220 extern gcc_jit_lvalue *
1221 gcc_jit_lvalue_access_field (gcc_jit_lvalue *struct_or_union,
1222 gcc_jit_location *loc,
1223 gcc_jit_field *field);
1225 /* Accessing a field of an rvalue of struct type, analogous to:
1226 (EXPR).field
1227 in C. */
1228 extern gcc_jit_rvalue *
1229 gcc_jit_rvalue_access_field (gcc_jit_rvalue *struct_or_union,
1230 gcc_jit_location *loc,
1231 gcc_jit_field *field);
1233 /* Accessing a field of an rvalue of pointer type, analogous to:
1234 (EXPR)->field
1235 in C, itself equivalent to (*EXPR).FIELD */
1236 extern gcc_jit_lvalue *
1237 gcc_jit_rvalue_dereference_field (gcc_jit_rvalue *ptr,
1238 gcc_jit_location *loc,
1239 gcc_jit_field *field);
1241 /* Dereferencing a pointer; analogous to:
1242 *(EXPR)
1244 extern gcc_jit_lvalue *
1245 gcc_jit_rvalue_dereference (gcc_jit_rvalue *rvalue,
1246 gcc_jit_location *loc);
1248 /* Taking the address of an lvalue; analogous to:
1249 &(EXPR)
1250 in C. */
1251 extern gcc_jit_rvalue *
1252 gcc_jit_lvalue_get_address (gcc_jit_lvalue *lvalue,
1253 gcc_jit_location *loc);
1255 #define LIBGCCJIT_HAVE_gcc_jit_lvalue_set_tls_model
1257 /* Set the thread-local storage model of a global variable
1259 This API entrypoint was added in LIBGCCJIT_ABI_17; you can test for its
1260 presence using
1261 #ifdef LIBGCCJIT_HAVE_gcc_jit_lvalue_set_tls_model */
1262 extern void
1263 gcc_jit_lvalue_set_tls_model (gcc_jit_lvalue *lvalue,
1264 enum gcc_jit_tls_model model);
1266 #define LIBGCCJIT_HAVE_gcc_jit_lvalue_set_link_section
1268 /* Set the link section of a global variable; analogous to:
1269 __attribute__((section(".section_name")))
1270 in C.
1272 This API entrypoint was added in LIBGCCJIT_ABI_18; you can test for its
1273 presence using
1274 #ifdef LIBGCCJIT_HAVE_gcc_jit_lvalue_set_link_section
1276 extern void
1277 gcc_jit_lvalue_set_link_section (gcc_jit_lvalue *lvalue,
1278 const char *section_name);
1280 extern gcc_jit_lvalue *
1281 gcc_jit_function_new_local (gcc_jit_function *func,
1282 gcc_jit_location *loc,
1283 gcc_jit_type *type,
1284 const char *name);
1286 /**********************************************************************
1287 Statement-creation.
1288 **********************************************************************/
1290 /* Add evaluation of an rvalue, discarding the result
1291 (e.g. a function call that "returns" void).
1293 This is equivalent to this C code:
1295 (void)expression;
1297 extern void
1298 gcc_jit_block_add_eval (gcc_jit_block *block,
1299 gcc_jit_location *loc,
1300 gcc_jit_rvalue *rvalue);
1302 /* Add evaluation of an rvalue, assigning the result to the given
1303 lvalue.
1305 This is roughly equivalent to this C code:
1307 lvalue = rvalue;
1309 extern void
1310 gcc_jit_block_add_assignment (gcc_jit_block *block,
1311 gcc_jit_location *loc,
1312 gcc_jit_lvalue *lvalue,
1313 gcc_jit_rvalue *rvalue);
1315 /* Add evaluation of an rvalue, using the result to modify an
1316 lvalue.
1318 This is analogous to "+=" and friends:
1320 lvalue += rvalue;
1321 lvalue *= rvalue;
1322 lvalue /= rvalue;
1323 etc */
1324 extern void
1325 gcc_jit_block_add_assignment_op (gcc_jit_block *block,
1326 gcc_jit_location *loc,
1327 gcc_jit_lvalue *lvalue,
1328 enum gcc_jit_binary_op op,
1329 gcc_jit_rvalue *rvalue);
1331 /* Add a no-op textual comment to the internal representation of the
1332 code. It will be optimized away, but will be visible in the dumps
1333 seen via
1334 GCC_JIT_BOOL_OPTION_DUMP_INITIAL_TREE
1336 GCC_JIT_BOOL_OPTION_DUMP_INITIAL_GIMPLE,
1337 and thus may be of use when debugging how your project's internal
1338 representation gets converted to the libgccjit IR. */
1339 extern void
1340 gcc_jit_block_add_comment (gcc_jit_block *block,
1341 gcc_jit_location *loc,
1342 const char *text);
1344 /* Terminate a block by adding evaluation of an rvalue, branching on the
1345 result to the appropriate successor block.
1347 This is roughly equivalent to this C code:
1349 if (boolval)
1350 goto on_true;
1351 else
1352 goto on_false;
1354 block, boolval, on_true, and on_false must be non-NULL. */
1355 extern void
1356 gcc_jit_block_end_with_conditional (gcc_jit_block *block,
1357 gcc_jit_location *loc,
1358 gcc_jit_rvalue *boolval,
1359 gcc_jit_block *on_true,
1360 gcc_jit_block *on_false);
1362 /* Terminate a block by adding a jump to the given target block.
1364 This is roughly equivalent to this C code:
1366 goto target;
1368 extern void
1369 gcc_jit_block_end_with_jump (gcc_jit_block *block,
1370 gcc_jit_location *loc,
1371 gcc_jit_block *target);
1373 /* Terminate a block by adding evaluation of an rvalue, returning the value.
1375 This is roughly equivalent to this C code:
1377 return expression;
1379 extern void
1380 gcc_jit_block_end_with_return (gcc_jit_block *block,
1381 gcc_jit_location *loc,
1382 gcc_jit_rvalue *rvalue);
1384 /* Terminate a block by adding a valueless return, for use within a function
1385 with "void" return type.
1387 This is equivalent to this C code:
1389 return;
1391 extern void
1392 gcc_jit_block_end_with_void_return (gcc_jit_block *block,
1393 gcc_jit_location *loc);
1395 /* Create a new gcc_jit_case instance for use in a switch statement.
1396 min_value and max_value must be constants of integer type.
1398 This API entrypoint was added in LIBGCCJIT_ABI_3; you can test for its
1399 presence using
1400 #ifdef LIBGCCJIT_HAVE_SWITCH_STATEMENTS
1402 extern gcc_jit_case *
1403 gcc_jit_context_new_case (gcc_jit_context *ctxt,
1404 gcc_jit_rvalue *min_value,
1405 gcc_jit_rvalue *max_value,
1406 gcc_jit_block *dest_block);
1408 /* Upcasting from case to object.
1410 This API entrypoint was added in LIBGCCJIT_ABI_3; you can test for its
1411 presence using
1412 #ifdef LIBGCCJIT_HAVE_SWITCH_STATEMENTS
1415 extern gcc_jit_object *
1416 gcc_jit_case_as_object (gcc_jit_case *case_);
1418 /* Terminate a block by adding evalation of an rvalue, then performing
1419 a multiway branch.
1421 This is roughly equivalent to this C code:
1423 switch (expr)
1425 default:
1426 goto default_block;
1428 case C0.min_value ... C0.max_value:
1429 goto C0.dest_block;
1431 case C1.min_value ... C1.max_value:
1432 goto C1.dest_block;
1434 ...etc...
1436 case C[N - 1].min_value ... C[N - 1].max_value:
1437 goto C[N - 1].dest_block;
1440 block, expr, default_block and cases must all be non-NULL.
1442 expr must be of the same integer type as all of the min_value
1443 and max_value within the cases.
1445 num_cases must be >= 0.
1447 The ranges of the cases must not overlap (or have duplicate
1448 values).
1450 This API entrypoint was added in LIBGCCJIT_ABI_3; you can test for its
1451 presence using
1452 #ifdef LIBGCCJIT_HAVE_SWITCH_STATEMENTS
1455 extern void
1456 gcc_jit_block_end_with_switch (gcc_jit_block *block,
1457 gcc_jit_location *loc,
1458 gcc_jit_rvalue *expr,
1459 gcc_jit_block *default_block,
1460 int num_cases,
1461 gcc_jit_case **cases);
1463 /* Pre-canned feature macro to indicate the presence of
1464 gcc_jit_block_end_with_switch, gcc_jit_case_as_object, and
1465 gcc_jit_context_new_case.
1467 This can be tested for with #ifdef. */
1468 #define LIBGCCJIT_HAVE_SWITCH_STATEMENTS
1470 /**********************************************************************
1471 Nested contexts.
1472 **********************************************************************/
1474 /* Given an existing JIT context, create a child context.
1476 The child inherits a copy of all option-settings from the parent.
1478 The child can reference objects created within the parent, but not
1479 vice-versa.
1481 The lifetime of the child context must be bounded by that of the
1482 parent: you should release a child context before releasing the parent
1483 context.
1485 If you use a function from a parent context within a child context,
1486 you have to compile the parent context before you can compile the
1487 child context, and the gcc_jit_result of the parent context must
1488 outlive the gcc_jit_result of the child context.
1490 This allows caching of shared initializations. For example, you could
1491 create types and declarations of global functions in a parent context
1492 once within a process, and then create child contexts whenever a
1493 function or loop becomes hot. Each such child context can be used for
1494 JIT-compiling just one function or loop, but can reference types
1495 and helper functions created within the parent context.
1497 Contexts can be arbitrarily nested, provided the above rules are
1498 followed, but it's probably not worth going above 2 or 3 levels, and
1499 there will likely be a performance hit for such nesting. */
1501 extern gcc_jit_context *
1502 gcc_jit_context_new_child_context (gcc_jit_context *parent_ctxt);
1504 /**********************************************************************
1505 Implementation support.
1506 **********************************************************************/
1508 /* Write C source code into "path" that can be compiled into a
1509 self-contained executable (i.e. with libgccjit as the only dependency).
1510 The generated code will attempt to replay the API calls that have been
1511 made into the given context.
1513 This may be useful when debugging the library or client code, for
1514 reducing a complicated recipe for reproducing a bug into a simpler
1515 form.
1517 Typically you need to supply the option "-Wno-unused-variable" when
1518 compiling the generated file (since the result of each API call is
1519 assigned to a unique variable within the generated C source, and not
1520 all are necessarily then used). */
1522 extern void
1523 gcc_jit_context_dump_reproducer_to_file (gcc_jit_context *ctxt,
1524 const char *path);
1526 /* Enable the dumping of a specific set of internal state from the
1527 compilation, capturing the result in-memory as a buffer.
1529 Parameter "dumpname" corresponds to the equivalent gcc command-line
1530 option, without the "-fdump-" prefix.
1531 For example, to get the equivalent of "-fdump-tree-vrp1", supply
1532 "tree-vrp1".
1533 The context directly stores the dumpname as a (const char *), so the
1534 passed string must outlive the context.
1536 gcc_jit_context_compile and gcc_jit_context_to_file
1537 will capture the dump as a dynamically-allocated buffer, writing
1538 it to ``*out_ptr``.
1540 The caller becomes responsible for calling
1541 free (*out_ptr)
1542 each time that gcc_jit_context_compile or gcc_jit_context_to_file
1543 are called. *out_ptr will be written to, either with the address of a
1544 buffer, or with NULL if an error occurred.
1546 This API entrypoint is likely to be less stable than the others.
1547 In particular, both the precise dumpnames, and the format and content
1548 of the dumps are subject to change.
1550 It exists primarily for writing the library's own test suite. */
1552 extern void
1553 gcc_jit_context_enable_dump (gcc_jit_context *ctxt,
1554 const char *dumpname,
1555 char **out_ptr);
1557 /**********************************************************************
1558 Timing support.
1559 **********************************************************************/
1561 /* The timing API was added in LIBGCCJIT_ABI_4; you can test for its
1562 presence using
1563 #ifdef LIBGCCJIT_HAVE_TIMING_API
1565 #define LIBGCCJIT_HAVE_TIMING_API
1567 typedef struct gcc_jit_timer gcc_jit_timer;
1569 /* Create a gcc_jit_timer instance, and start timing.
1571 This API entrypoint was added in LIBGCCJIT_ABI_4; you can test for its
1572 presence using
1573 #ifdef LIBGCCJIT_HAVE_TIMING_API
1575 extern gcc_jit_timer *
1576 gcc_jit_timer_new (void);
1578 /* Release a gcc_jit_timer instance.
1580 This API entrypoint was added in LIBGCCJIT_ABI_4; you can test for its
1581 presence using
1582 #ifdef LIBGCCJIT_HAVE_TIMING_API
1584 extern void
1585 gcc_jit_timer_release (gcc_jit_timer *timer);
1587 /* Associate a gcc_jit_timer instance with a context.
1589 This API entrypoint was added in LIBGCCJIT_ABI_4; you can test for its
1590 presence using
1591 #ifdef LIBGCCJIT_HAVE_TIMING_API
1593 extern void
1594 gcc_jit_context_set_timer (gcc_jit_context *ctxt,
1595 gcc_jit_timer *timer);
1597 /* Get the timer associated with a context (if any).
1599 This API entrypoint was added in LIBGCCJIT_ABI_4; you can test for its
1600 presence using
1601 #ifdef LIBGCCJIT_HAVE_TIMING_API
1604 extern gcc_jit_timer *
1605 gcc_jit_context_get_timer (gcc_jit_context *ctxt);
1607 /* Push the given item onto the timing stack.
1609 This API entrypoint was added in LIBGCCJIT_ABI_4; you can test for its
1610 presence using
1611 #ifdef LIBGCCJIT_HAVE_TIMING_API
1614 extern void
1615 gcc_jit_timer_push (gcc_jit_timer *timer,
1616 const char *item_name);
1618 /* Pop the top item from the timing stack.
1620 This API entrypoint was added in LIBGCCJIT_ABI_4; you can test for its
1621 presence using
1622 #ifdef LIBGCCJIT_HAVE_TIMING_API
1625 extern void
1626 gcc_jit_timer_pop (gcc_jit_timer *timer,
1627 const char *item_name);
1629 /* Print timing information to the given stream about activity since
1630 the timer was started.
1632 This API entrypoint was added in LIBGCCJIT_ABI_4; you can test for its
1633 presence using
1634 #ifdef LIBGCCJIT_HAVE_TIMING_API
1637 extern void
1638 gcc_jit_timer_print (gcc_jit_timer *timer,
1639 FILE *f_out);
1642 #define LIBGCCJIT_HAVE_gcc_jit_rvalue_set_bool_require_tail_call
1644 /* Mark/clear a call as needing tail-call optimization.
1646 This API entrypoint was added in LIBGCCJIT_ABI_6; you can test for its
1647 presence using
1648 #ifdef LIBGCCJIT_HAVE_gcc_jit_rvalue_set_bool_require_tail_call
1650 extern void
1651 gcc_jit_rvalue_set_bool_require_tail_call (gcc_jit_rvalue *call,
1652 int require_tail_call);
1654 #define LIBGCCJIT_HAVE_gcc_jit_type_get_aligned
1656 /* Given type "T", get type:
1658 T __attribute__ ((aligned (ALIGNMENT_IN_BYTES)))
1660 The alignment must be a power of two.
1662 This API entrypoint was added in LIBGCCJIT_ABI_7; you can test for its
1663 presence using
1664 #ifdef LIBGCCJIT_HAVE_gcc_jit_type_get_aligned
1666 extern gcc_jit_type *
1667 gcc_jit_type_get_aligned (gcc_jit_type *type,
1668 size_t alignment_in_bytes);
1670 #define LIBGCCJIT_HAVE_gcc_jit_type_get_vector
1672 /* Given type "T", get type:
1674 T __attribute__ ((vector_size (sizeof(T) * num_units))
1676 T must be integral/floating point; num_units must be a power of two.
1678 This API entrypoint was added in LIBGCCJIT_ABI_8; you can test for its
1679 presence using
1680 #ifdef LIBGCCJIT_HAVE_gcc_jit_type_get_vector
1682 extern gcc_jit_type *
1683 gcc_jit_type_get_vector (gcc_jit_type *type, size_t num_units);
1686 #define LIBGCCJIT_HAVE_gcc_jit_function_get_address
1688 /* Get the address of a function as an rvalue, of function pointer
1689 type.
1691 This API entrypoint was added in LIBGCCJIT_ABI_9; you can test for its
1692 presence using
1693 #ifdef LIBGCCJIT_HAVE_gcc_jit_function_get_address
1695 extern gcc_jit_rvalue *
1696 gcc_jit_function_get_address (gcc_jit_function *fn,
1697 gcc_jit_location *loc);
1700 #define LIBGCCJIT_HAVE_gcc_jit_context_new_rvalue_from_vector
1702 /* Build a vector rvalue from an array of elements.
1704 "vec_type" should be a vector type, created using gcc_jit_type_get_vector.
1706 This API entrypoint was added in LIBGCCJIT_ABI_10; you can test for its
1707 presence using
1708 #ifdef LIBGCCJIT_HAVE_gcc_jit_context_new_rvalue_from_vector
1710 extern gcc_jit_rvalue *
1711 gcc_jit_context_new_rvalue_from_vector (gcc_jit_context *ctxt,
1712 gcc_jit_location *loc,
1713 gcc_jit_type *vec_type,
1714 size_t num_elements,
1715 gcc_jit_rvalue **elements);
1717 #define LIBGCCJIT_HAVE_gcc_jit_version
1719 /* Functions to retrieve libgccjit version.
1720 Analogous to __GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__ in C code.
1722 These API entrypoints were added in LIBGCCJIT_ABI_13; you can test for their
1723 presence using
1724 #ifdef LIBGCCJIT_HAVE_gcc_jit_version
1726 extern int
1727 gcc_jit_version_major (void);
1728 extern int
1729 gcc_jit_version_minor (void);
1730 extern int
1731 gcc_jit_version_patchlevel (void);
1733 /**********************************************************************
1734 Asm support.
1735 **********************************************************************/
1737 /* Functions for adding inline assembler code, analogous to GCC's
1738 "extended asm" syntax.
1740 See https://gcc.gnu.org/onlinedocs/gcc/Using-Assembly-Language-with-C.html
1742 These API entrypoints were added in LIBGCCJIT_ABI_15; you can test for their
1743 presence using
1744 #ifdef LIBGCCJIT_HAVE_ASM_STATEMENTS
1747 #define LIBGCCJIT_HAVE_ASM_STATEMENTS
1749 /* Create a gcc_jit_extended_asm for an extended asm statement
1750 with no control flow (i.e. without the goto qualifier).
1752 The asm_template parameter corresponds to the AssemblerTemplate
1753 within C's extended asm syntax. It must be non-NULL. */
1755 extern gcc_jit_extended_asm *
1756 gcc_jit_block_add_extended_asm (gcc_jit_block *block,
1757 gcc_jit_location *loc,
1758 const char *asm_template);
1760 /* Create a gcc_jit_extended_asm for an extended asm statement
1761 that may perform jumps, and use it to terminate the given block.
1762 This is equivalent to the "goto" qualifier in C's extended asm
1763 syntax. */
1765 extern gcc_jit_extended_asm *
1766 gcc_jit_block_end_with_extended_asm_goto (gcc_jit_block *block,
1767 gcc_jit_location *loc,
1768 const char *asm_template,
1769 int num_goto_blocks,
1770 gcc_jit_block **goto_blocks,
1771 gcc_jit_block *fallthrough_block);
1773 /* Upcasting from extended asm to object. */
1775 extern gcc_jit_object *
1776 gcc_jit_extended_asm_as_object (gcc_jit_extended_asm *ext_asm);
1778 /* Set whether the gcc_jit_extended_asm has side-effects, equivalent to
1779 the "volatile" qualifier in C's extended asm syntax. */
1781 extern void
1782 gcc_jit_extended_asm_set_volatile_flag (gcc_jit_extended_asm *ext_asm,
1783 int flag);
1785 /* Set the equivalent of the "inline" qualifier in C's extended asm
1786 syntax. */
1788 extern void
1789 gcc_jit_extended_asm_set_inline_flag (gcc_jit_extended_asm *ext_asm,
1790 int flag);
1792 /* Add an output operand to the extended asm statement.
1793 "asm_symbolic_name" can be NULL.
1794 "constraint" and "dest" must be non-NULL.
1795 This function can't be called on an "asm goto" as such instructions
1796 can't have outputs */
1798 extern void
1799 gcc_jit_extended_asm_add_output_operand (gcc_jit_extended_asm *ext_asm,
1800 const char *asm_symbolic_name,
1801 const char *constraint,
1802 gcc_jit_lvalue *dest);
1804 /* Add an input operand to the extended asm statement.
1805 "asm_symbolic_name" can be NULL.
1806 "constraint" and "src" must be non-NULL. */
1808 extern void
1809 gcc_jit_extended_asm_add_input_operand (gcc_jit_extended_asm *ext_asm,
1810 const char *asm_symbolic_name,
1811 const char *constraint,
1812 gcc_jit_rvalue *src);
1814 /* Add "victim" to the list of registers clobbered by the extended
1815 asm statement. It must be non-NULL. */
1817 extern void
1818 gcc_jit_extended_asm_add_clobber (gcc_jit_extended_asm *ext_asm,
1819 const char *victim);
1821 /* Add "asm_stmts", a set of top-level asm statements, analogous to
1822 those created by GCC's "basic" asm syntax in C at file scope. */
1824 extern void
1825 gcc_jit_context_add_top_level_asm (gcc_jit_context *ctxt,
1826 gcc_jit_location *loc,
1827 const char *asm_stmts);
1829 #define LIBGCCJIT_HAVE_REFLECTION
1831 /* Reflection functions to get the number of parameters, return type of
1832 a function and whether a type is a bool from the C API.
1834 This API entrypoint was added in LIBGCCJIT_ABI_16; you can test for its
1835 presence using
1836 #ifdef LIBGCCJIT_HAVE_REFLECTION
1838 /* Get the return type of a function. */
1839 extern gcc_jit_type *
1840 gcc_jit_function_get_return_type (gcc_jit_function *func);
1842 /* Get the number of params of a function. */
1843 extern size_t
1844 gcc_jit_function_get_param_count (gcc_jit_function *func);
1846 /* Get the element type of an array type or NULL if it's not an array. */
1847 extern gcc_jit_type *
1848 gcc_jit_type_dyncast_array (gcc_jit_type *type);
1850 /* Return non-zero if the type is a bool. */
1851 extern int
1852 gcc_jit_type_is_bool (gcc_jit_type *type);
1854 /* Return the function type if it is one or NULL. */
1855 extern gcc_jit_function_type *
1856 gcc_jit_type_dyncast_function_ptr_type (gcc_jit_type *type);
1858 /* Given a function type, return its return type. */
1859 extern gcc_jit_type *
1860 gcc_jit_function_type_get_return_type (gcc_jit_function_type *function_type);
1862 /* Given a function type, return its number of parameters. */
1863 extern size_t
1864 gcc_jit_function_type_get_param_count (gcc_jit_function_type *function_type);
1866 /* Given a function type, return the type of the specified parameter. */
1867 extern gcc_jit_type *
1868 gcc_jit_function_type_get_param_type (gcc_jit_function_type *function_type,
1869 size_t index);
1871 /* Return non-zero if the type is an integral. */
1872 extern int
1873 gcc_jit_type_is_integral (gcc_jit_type *type);
1875 /* Return the type pointed by the pointer type or NULL if it's not a
1876 * pointer. */
1877 extern gcc_jit_type *
1878 gcc_jit_type_is_pointer (gcc_jit_type *type);
1880 /* Given a type, return a dynamic cast to a vector type or NULL. */
1881 extern gcc_jit_vector_type *
1882 gcc_jit_type_dyncast_vector (gcc_jit_type *type);
1884 /* Given a type, return a dynamic cast to a struct type or NULL. */
1885 extern gcc_jit_struct *
1886 gcc_jit_type_is_struct (gcc_jit_type *type);
1888 /* Given a vector type, return the number of units it contains. */
1889 extern size_t
1890 gcc_jit_vector_type_get_num_units (gcc_jit_vector_type *vector_type);
1892 /* Given a vector type, return the type of its elements. */
1893 extern gcc_jit_type *
1894 gcc_jit_vector_type_get_element_type (gcc_jit_vector_type *vector_type);
1896 /* Given a type, return the unqualified type, removing "const", "volatile"
1897 * and alignment qualifiers. */
1898 extern gcc_jit_type *
1899 gcc_jit_type_unqualified (gcc_jit_type *type);
1901 #ifdef __cplusplus
1903 #endif /* __cplusplus */
1905 #endif /* LIBGCCJIT_H */