]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/jit/libgccjit.h
PR jit/66628: add gcc_jit_context_add_command_line_option
[thirdparty/gcc.git] / gcc / jit / libgccjit.h
CommitLineData
35485da9 1/* A pure C API to enable client code to embed GCC as a JIT-compiler.
1e3b6a3d 2 Copyright (C) 2013-2015 Free Software Foundation, Inc.
35485da9
DM
3
4This file is part of GCC.
5
6GCC is free software; you can redistribute it and/or modify it
7under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 3, or (at your option)
9any later version.
10
11GCC is distributed in the hope that it will be useful, but
12WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GCC; see the file COPYING3. If not see
18<http://www.gnu.org/licenses/>. */
19
20#ifndef LIBGCCJIT_H
21#define LIBGCCJIT_H
22
eb4c16eb
DM
23#include <stdio.h>
24
35485da9
DM
25#ifdef __cplusplus
26extern "C" {
27#endif /* __cplusplus */
28
29/**********************************************************************
30 Data structures.
31 **********************************************************************/
32/* All structs within the API are opaque. */
33
7c8db13e
DM
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.
35485da9 37
7c8db13e 38 Invoking gcc_jit_context_compile on it gives you a gcc_jit_result *
fdce7209 39 (or NULL), representing in-memory machine code.
35485da9 40
7c8db13e
DM
41 You can call gcc_jit_context_compile repeatedly on one context, giving
42 multiple independent results.
43
fdce7209
DM
44 Similarly, you can call gcc_jit_context_compile_to_file on a context
45 to compile to disk.
46
7c8db13e 47 Eventually you can call gcc_jit_context_release to clean up the
fdce7209
DM
48 context; any in-memory results created from it are still usable, and
49 should be cleaned up via gcc_jit_result_release. */
35485da9
DM
50typedef struct gcc_jit_context gcc_jit_context;
51
fdce7209 52/* A gcc_jit_result encapsulates the result of an in-memory compilation. */
35485da9
DM
53typedef struct gcc_jit_result gcc_jit_result;
54
55/* An object created within a context. Such objects are automatically
56 cleaned up when the context is released.
57
58 The class hierarchy looks like this:
59
60 +- gcc_jit_object
c168eab9
UD
61 +- gcc_jit_location
62 +- gcc_jit_type
35485da9 63 +- gcc_jit_struct
c168eab9
UD
64 +- gcc_jit_field
65 +- gcc_jit_function
66 +- gcc_jit_block
67 +- gcc_jit_rvalue
68 +- gcc_jit_lvalue
69 +- gcc_jit_param
35485da9
DM
70*/
71typedef struct gcc_jit_object gcc_jit_object;
72
73/* A gcc_jit_location encapsulates a source code location, so that
74 you can (optionally) associate locations in your language with
75 statements in the JIT-compiled code, allowing the debugger to
76 single-step through your language.
77
78 Note that to do so, you also need to enable
79 GCC_JIT_BOOL_OPTION_DEBUGINFO
80 on the gcc_jit_context.
81
82 gcc_jit_location instances are optional; you can always pass
83 NULL. */
84typedef struct gcc_jit_location gcc_jit_location;
85
86/* A gcc_jit_type encapsulates a type e.g. "int" or a "struct foo*". */
87typedef struct gcc_jit_type gcc_jit_type;
88
89/* A gcc_jit_field encapsulates a field within a struct; it is used
90 when creating a struct type (using gcc_jit_context_new_struct_type).
91 Fields cannot be shared between structs. */
92typedef struct gcc_jit_field gcc_jit_field;
93
94/* A gcc_jit_struct encapsulates a struct type, either one that we have
95 the layout for, or an opaque type. */
96typedef struct gcc_jit_struct gcc_jit_struct;
97
98/* A gcc_jit_function encapsulates a function: either one that you're
99 creating yourself, or a reference to one that you're dynamically
100 linking to within the rest of the process. */
101typedef struct gcc_jit_function gcc_jit_function;
102
103/* A gcc_jit_block encapsulates a "basic block" of statements within a
104 function (i.e. with one entry point and one exit point).
105
106 Every block within a function must be terminated with a conditional,
107 a branch, or a return.
108
109 The blocks within a function form a directed graph.
110
111 The entrypoint to the function is the first block created within
112 it.
113
114 All of the blocks in a function must be reachable via some path from
115 the first block.
116
117 It's OK to have more than one "return" from a function (i.e. multiple
118 blocks that terminate by returning). */
119typedef struct gcc_jit_block gcc_jit_block;
120
121/* A gcc_jit_rvalue is an expression within your code, with some type. */
122typedef struct gcc_jit_rvalue gcc_jit_rvalue;
123
124/* A gcc_jit_lvalue is a storage location within your code (e.g. a
125 variable, a parameter, etc). It is also a gcc_jit_rvalue; use
126 gcc_jit_lvalue_as_rvalue to cast. */
127typedef struct gcc_jit_lvalue gcc_jit_lvalue;
128
129/* A gcc_jit_param is a function parameter, used when creating a
130 gcc_jit_function. It is also a gcc_jit_lvalue (and thus also an
131 rvalue); use gcc_jit_param_as_lvalue to convert. */
132typedef struct gcc_jit_param gcc_jit_param;
133
134/* Acquire a JIT-compilation context. */
135extern gcc_jit_context *
136gcc_jit_context_acquire (void);
137
138/* Release the context. After this call, it's no longer valid to use
139 the ctxt. */
140extern void
141gcc_jit_context_release (gcc_jit_context *ctxt);
142
143/* Options taking string values. */
144enum gcc_jit_str_option
145{
146 /* The name of the program, for use as a prefix when printing error
147 messages to stderr. If NULL, or default, "libgccjit.so" is used. */
148 GCC_JIT_STR_OPTION_PROGNAME,
149
150 GCC_JIT_NUM_STR_OPTIONS
151};
152
153/* Options taking int values. */
154enum gcc_jit_int_option
155{
156 /* How much to optimize the code.
157 Valid values are 0-3, corresponding to GCC's command-line options
158 -O0 through -O3.
159
160 The default value is 0 (unoptimized). */
161 GCC_JIT_INT_OPTION_OPTIMIZATION_LEVEL,
162
163 GCC_JIT_NUM_INT_OPTIONS
164};
165
166/* Options taking boolean values.
167 These all default to "false". */
168enum gcc_jit_bool_option
169{
170 /* If true, gcc_jit_context_compile will attempt to do the right
171 thing so that if you attach a debugger to the process, it will
172 be able to inspect variables and step through your code.
173
174 Note that you can't step through code unless you set up source
175 location information for the code (by creating and passing in
176 gcc_jit_location instances). */
177 GCC_JIT_BOOL_OPTION_DEBUGINFO,
178
179 /* If true, gcc_jit_context_compile will dump its initial "tree"
180 representation of your code to stderr (before any
181 optimizations). */
182 GCC_JIT_BOOL_OPTION_DUMP_INITIAL_TREE,
183
184 /* If true, gcc_jit_context_compile will dump the "gimple"
185 representation of your code to stderr, before any optimizations
186 are performed. The dump resembles C code. */
187 GCC_JIT_BOOL_OPTION_DUMP_INITIAL_GIMPLE,
188
189 /* If true, gcc_jit_context_compile will dump the final
190 generated code to stderr, in the form of assembly language. */
191 GCC_JIT_BOOL_OPTION_DUMP_GENERATED_CODE,
192
193 /* If true, gcc_jit_context_compile will print information to stderr
194 on the actions it is performing, followed by a profile showing
195 the time taken and memory usage of each phase.
196 */
197 GCC_JIT_BOOL_OPTION_DUMP_SUMMARY,
198
199 /* If true, gcc_jit_context_compile will dump copious
200 amount of information on what it's doing to various
201 files within a temporary directory. Use
202 GCC_JIT_BOOL_OPTION_KEEP_INTERMEDIATES (see below) to
203 see the results. The files are intended to be human-readable,
204 but the exact files and their formats are subject to change.
205 */
206 GCC_JIT_BOOL_OPTION_DUMP_EVERYTHING,
207
208 /* If true, libgccjit will aggressively run its garbage collector, to
209 shake out bugs (greatly slowing down the compile). This is likely
210 to only be of interest to developers *of* the library. It is
211 used when running the selftest suite. */
212 GCC_JIT_BOOL_OPTION_SELFCHECK_GC,
213
214 /* If true, gcc_jit_context_release will not clean up
215 intermediate files written to the filesystem, and will display
216 their location on stderr. */
217 GCC_JIT_BOOL_OPTION_KEEP_INTERMEDIATES,
218
219 GCC_JIT_NUM_BOOL_OPTIONS
220};
221
222/* Set a string option on the given context.
223
c168eab9
UD
224 The context takes a copy of the string, so the
225 (const char *) buffer is not needed anymore after the call
226 returns. */
35485da9
DM
227extern void
228gcc_jit_context_set_str_option (gcc_jit_context *ctxt,
229 enum gcc_jit_str_option opt,
230 const char *value);
231
232/* Set an int option on the given context. */
233extern void
234gcc_jit_context_set_int_option (gcc_jit_context *ctxt,
235 enum gcc_jit_int_option opt,
236 int value);
237
238/* Set a boolean option on the given context.
239
240 Zero is "false" (the default), non-zero is "true". */
241extern void
242gcc_jit_context_set_bool_option (gcc_jit_context *ctxt,
243 enum gcc_jit_bool_option opt,
244 int value);
245
fa22c20d
DM
246/* Add an arbitrary gcc command-line option to the context.
247 The context takes a copy of the string, so the
248 (const char *) optname is not needed anymore after the call
249 returns.
250
251 Note that only some options are likely to be meaningful; there is no
252 "frontend" within libgccjit, so typically only those affecting
253 optimization and code-generation are likely to be useful.
254
255 This entrypoint was added in LIBGCCJIT_ABI_1; you can test for
256 its presence using
257 #ifdef LIBGCCJIT_HAVE_gcc_jit_context_add_command_line_option
258*/
259
260extern void
261gcc_jit_context_add_command_line_option (gcc_jit_context *ctxt,
262 const char *optname);
263
264/* Pre-canned feature-test macro for detecting the presence of
265 gcc_jit_context_add_command_line_option within libgccjit.h. */
266
267#define LIBGCCJIT_HAVE_gcc_jit_context_add_command_line_option
268
fdce7209
DM
269/* Compile the context to in-memory machine code.
270
271 This can be called more that once on a given context,
272 although any errors that occur will block further compilation. */
273
35485da9
DM
274extern gcc_jit_result *
275gcc_jit_context_compile (gcc_jit_context *ctxt);
276
fdce7209
DM
277/* Kinds of ahead-of-time compilation, for use with
278 gcc_jit_context_compile_to_file. */
279
280enum gcc_jit_output_kind
281{
282 /* Compile the context to an assembler file. */
283 GCC_JIT_OUTPUT_KIND_ASSEMBLER,
284
285 /* Compile the context to an object file. */
286 GCC_JIT_OUTPUT_KIND_OBJECT_FILE,
287
288 /* Compile the context to a dynamic library. */
289 GCC_JIT_OUTPUT_KIND_DYNAMIC_LIBRARY,
290
291 /* Compile the context to an executable. */
292 GCC_JIT_OUTPUT_KIND_EXECUTABLE
293};
294
295/* Compile the context to a file of the given kind.
296
297 This can be called more that once on a given context,
298 although any errors that occur will block further compilation. */
299
300extern void
301gcc_jit_context_compile_to_file (gcc_jit_context *ctxt,
302 enum gcc_jit_output_kind output_kind,
303 const char *output_path);
304
35485da9
DM
305/* To help with debugging: dump a C-like representation to the given path,
306 describing what's been set up on the context.
307
308 If "update_locations" is true, then also set up gcc_jit_location
309 information throughout the context, pointing at the dump file as if it
310 were a source file. This may be of use in conjunction with
311 GCC_JIT_BOOL_OPTION_DEBUGINFO to allow stepping through the code in a
312 debugger. */
313extern void
314gcc_jit_context_dump_to_file (gcc_jit_context *ctxt,
315 const char *path,
316 int update_locations);
317
eb4c16eb
DM
318/* To help with debugging; enable ongoing logging of the context's
319 activity to the given FILE *.
320
321 The caller remains responsible for closing "logfile".
322
323 Params "flags" and "verbosity" are reserved for future use, and
324 must both be 0 for now. */
325extern void
326gcc_jit_context_set_logfile (gcc_jit_context *ctxt,
327 FILE *logfile,
328 int flags,
329 int verbosity);
330
dc44ee3a 331/* To be called after any API call, this gives the first error message
35485da9
DM
332 that occurred on the context.
333
334 The returned string is valid for the rest of the lifetime of the
335 context.
336
337 If no errors occurred, this will be NULL. */
338extern const char *
339gcc_jit_context_get_first_error (gcc_jit_context *ctxt);
303e1d56 340
dc44ee3a 341/* To be called after any API call, this gives the last error message
303e1d56
DM
342 that occurred on the context.
343
dc44ee3a 344 If no errors occurred, this will be NULL.
303e1d56 345
dc44ee3a
DM
346 If non-NULL, the returned string is only guaranteed to be valid until
347 the next call to libgccjit relating to this context. */
303e1d56
DM
348extern const char *
349gcc_jit_context_get_last_error (gcc_jit_context *ctxt);
35485da9
DM
350
351/* Locate a given function within the built machine code.
352 This will need to be cast to a function pointer of the
353 correct type before it can be called. */
354extern void *
355gcc_jit_result_get_code (gcc_jit_result *result,
356 const char *funcname);
357
791cfef8
DM
358/* Locate a given global within the built machine code.
359 It must have been created using GCC_JIT_GLOBAL_EXPORTED.
360 This is a ptr to the global, so e.g. for an int this is an int *. */
361extern void *
362gcc_jit_result_get_global (gcc_jit_result *result,
363 const char *name);
364
35485da9
DM
365/* Once we're done with the code, this unloads the built .so file.
366 This cleans up the result; after calling this, it's no longer
367 valid to use the result. */
368extern void
369gcc_jit_result_release (gcc_jit_result *result);
370
371
372/**********************************************************************
373 Functions for creating "contextual" objects.
374
375 All objects created by these functions share the lifetime of the context
376 they are created within, and are automatically cleaned up for you when
377 you call gcc_jit_context_release on the context.
378
379 Note that this means you can't use references to them after you've
380 released their context.
381
382 All (const char *) string arguments passed to these functions are
c168eab9 383 copied, so you don't need to keep them around.
35485da9
DM
384
385 You create code by adding a sequence of statements to blocks.
386**********************************************************************/
387
388/**********************************************************************
389 The base class of "contextual" object.
390 **********************************************************************/
391/* Which context is "obj" within? */
392extern gcc_jit_context *
393gcc_jit_object_get_context (gcc_jit_object *obj);
394
395/* Get a human-readable description of this object.
396 The string buffer is created the first time this is called on a given
397 object, and persists until the object's context is released. */
398extern const char *
399gcc_jit_object_get_debug_string (gcc_jit_object *obj);
400
401/**********************************************************************
402 Debugging information.
403 **********************************************************************/
404
405/* Creating source code locations for use by the debugger.
406 Line and column numbers are 1-based. */
407extern gcc_jit_location *
408gcc_jit_context_new_location (gcc_jit_context *ctxt,
409 const char *filename,
410 int line,
411 int column);
412
413/* Upcasting from location to object. */
414extern gcc_jit_object *
415gcc_jit_location_as_object (gcc_jit_location *loc);
416
417
418/**********************************************************************
419 Types.
420 **********************************************************************/
421
422/* Upcasting from type to object. */
423extern gcc_jit_object *
424gcc_jit_type_as_object (gcc_jit_type *type);
425
426/* Access to specific types. */
427enum gcc_jit_types
428{
429 /* C's "void" type. */
430 GCC_JIT_TYPE_VOID,
431
432 /* "void *". */
433 GCC_JIT_TYPE_VOID_PTR,
434
435 /* C++'s bool type; also C99's "_Bool" type, aka "bool" if using
436 stdbool.h. */
437 GCC_JIT_TYPE_BOOL,
438
439 /* Various integer types. */
440
441 /* C's "char" (of some signedness) and the variants where the
442 signedness is specified. */
443 GCC_JIT_TYPE_CHAR,
444 GCC_JIT_TYPE_SIGNED_CHAR,
445 GCC_JIT_TYPE_UNSIGNED_CHAR,
446
447 /* C's "short" and "unsigned short". */
448 GCC_JIT_TYPE_SHORT, /* signed */
449 GCC_JIT_TYPE_UNSIGNED_SHORT,
450
451 /* C's "int" and "unsigned int". */
452 GCC_JIT_TYPE_INT, /* signed */
453 GCC_JIT_TYPE_UNSIGNED_INT,
454
455 /* C's "long" and "unsigned long". */
456 GCC_JIT_TYPE_LONG, /* signed */
457 GCC_JIT_TYPE_UNSIGNED_LONG,
458
459 /* C99's "long long" and "unsigned long long". */
460 GCC_JIT_TYPE_LONG_LONG, /* signed */
461 GCC_JIT_TYPE_UNSIGNED_LONG_LONG,
462
463 /* Floating-point types */
464
465 GCC_JIT_TYPE_FLOAT,
466 GCC_JIT_TYPE_DOUBLE,
467 GCC_JIT_TYPE_LONG_DOUBLE,
468
469 /* C type: (const char *). */
470 GCC_JIT_TYPE_CONST_CHAR_PTR,
471
472 /* The C "size_t" type. */
473 GCC_JIT_TYPE_SIZE_T,
474
475 /* C type: (FILE *) */
eeafb319
DM
476 GCC_JIT_TYPE_FILE_PTR,
477
478 /* Complex numbers. */
479 GCC_JIT_TYPE_COMPLEX_FLOAT,
480 GCC_JIT_TYPE_COMPLEX_DOUBLE,
481 GCC_JIT_TYPE_COMPLEX_LONG_DOUBLE
482
35485da9
DM
483};
484
485extern gcc_jit_type *
486gcc_jit_context_get_type (gcc_jit_context *ctxt,
487 enum gcc_jit_types type_);
488
489/* Get the integer type of the given size and signedness. */
490extern gcc_jit_type *
491gcc_jit_context_get_int_type (gcc_jit_context *ctxt,
492 int num_bytes, int is_signed);
493
494/* Constructing new types. */
495
496/* Given type "T", get type "T*". */
497extern gcc_jit_type *
498gcc_jit_type_get_pointer (gcc_jit_type *type);
499
500/* Given type "T", get type "const T". */
501extern gcc_jit_type *
502gcc_jit_type_get_const (gcc_jit_type *type);
503
504/* Given type "T", get type "volatile T". */
505extern gcc_jit_type *
506gcc_jit_type_get_volatile (gcc_jit_type *type);
507
508/* Given type "T", get type "T[N]" (for a constant N). */
509extern gcc_jit_type *
510gcc_jit_context_new_array_type (gcc_jit_context *ctxt,
511 gcc_jit_location *loc,
512 gcc_jit_type *element_type,
513 int num_elements);
514
515/* Struct-handling. */
516
517/* Create a field, for use within a struct or union. */
518extern gcc_jit_field *
519gcc_jit_context_new_field (gcc_jit_context *ctxt,
520 gcc_jit_location *loc,
521 gcc_jit_type *type,
522 const char *name);
523
524/* Upcasting from field to object. */
525extern gcc_jit_object *
526gcc_jit_field_as_object (gcc_jit_field *field);
527
528/* Create a struct type from an array of fields. */
529extern gcc_jit_struct *
530gcc_jit_context_new_struct_type (gcc_jit_context *ctxt,
531 gcc_jit_location *loc,
532 const char *name,
533 int num_fields,
534 gcc_jit_field **fields);
535
536/* Create an opaque struct type. */
537extern gcc_jit_struct *
538gcc_jit_context_new_opaque_struct (gcc_jit_context *ctxt,
539 gcc_jit_location *loc,
540 const char *name);
541
542/* Upcast a struct to a type. */
543extern gcc_jit_type *
544gcc_jit_struct_as_type (gcc_jit_struct *struct_type);
545
546/* Populating the fields of a formerly-opaque struct type.
547 This can only be called once on a given struct type. */
548extern void
549gcc_jit_struct_set_fields (gcc_jit_struct *struct_type,
550 gcc_jit_location *loc,
551 int num_fields,
552 gcc_jit_field **fields);
553
554/* Unions work similarly to structs. */
555extern gcc_jit_type *
556gcc_jit_context_new_union_type (gcc_jit_context *ctxt,
557 gcc_jit_location *loc,
558 const char *name,
559 int num_fields,
560 gcc_jit_field **fields);
561
562/* Function pointers. */
563
564extern gcc_jit_type *
565gcc_jit_context_new_function_ptr_type (gcc_jit_context *ctxt,
566 gcc_jit_location *loc,
567 gcc_jit_type *return_type,
568 int num_params,
569 gcc_jit_type **param_types,
570 int is_variadic);
571
572/**********************************************************************
573 Constructing functions.
574 **********************************************************************/
575/* Create a function param. */
576extern gcc_jit_param *
577gcc_jit_context_new_param (gcc_jit_context *ctxt,
578 gcc_jit_location *loc,
579 gcc_jit_type *type,
580 const char *name);
581
582/* Upcasting from param to object. */
583extern gcc_jit_object *
584gcc_jit_param_as_object (gcc_jit_param *param);
585
586/* Upcasting from param to lvalue. */
587extern gcc_jit_lvalue *
588gcc_jit_param_as_lvalue (gcc_jit_param *param);
589
590/* Upcasting from param to rvalue. */
591extern gcc_jit_rvalue *
592gcc_jit_param_as_rvalue (gcc_jit_param *param);
593
594/* Kinds of function. */
595enum gcc_jit_function_kind
596{
597 /* Function is defined by the client code and visible
598 by name outside of the JIT. */
599 GCC_JIT_FUNCTION_EXPORTED,
600
601 /* Function is defined by the client code, but is invisible
602 outside of the JIT. Analogous to a "static" function. */
603 GCC_JIT_FUNCTION_INTERNAL,
604
605 /* Function is not defined by the client code; we're merely
606 referring to it. Analogous to using an "extern" function from a
607 header file. */
608 GCC_JIT_FUNCTION_IMPORTED,
609
610 /* Function is only ever inlined into other functions, and is
611 invisible outside of the JIT.
612
613 Analogous to prefixing with "inline" and adding
614 __attribute__((always_inline)).
615
616 Inlining will only occur when the optimization level is
617 above 0; when optimization is off, this is essentially the
618 same as GCC_JIT_FUNCTION_INTERNAL. */
619 GCC_JIT_FUNCTION_ALWAYS_INLINE
620};
621
622/* Create a function. */
623extern gcc_jit_function *
624gcc_jit_context_new_function (gcc_jit_context *ctxt,
625 gcc_jit_location *loc,
626 enum gcc_jit_function_kind kind,
627 gcc_jit_type *return_type,
628 const char *name,
629 int num_params,
630 gcc_jit_param **params,
631 int is_variadic);
632
633/* Create a reference to a builtin function (sometimes called
634 intrinsic functions). */
635extern gcc_jit_function *
636gcc_jit_context_get_builtin_function (gcc_jit_context *ctxt,
637 const char *name);
638
639/* Upcasting from function to object. */
640extern gcc_jit_object *
641gcc_jit_function_as_object (gcc_jit_function *func);
642
643/* Get a specific param of a function by index. */
644extern gcc_jit_param *
645gcc_jit_function_get_param (gcc_jit_function *func, int index);
646
647/* Emit the function in graphviz format. */
648extern void
649gcc_jit_function_dump_to_dot (gcc_jit_function *func,
650 const char *path);
651
652/* Create a block.
653
654 The name can be NULL, or you can give it a meaningful name, which
655 may show up in dumps of the internal representation, and in error
656 messages. */
657extern gcc_jit_block *
658gcc_jit_function_new_block (gcc_jit_function *func,
659 const char *name);
660
661/* Upcasting from block to object. */
662extern gcc_jit_object *
663gcc_jit_block_as_object (gcc_jit_block *block);
664
665/* Which function is this block within? */
666extern gcc_jit_function *
667gcc_jit_block_get_function (gcc_jit_block *block);
668
669/**********************************************************************
670 lvalues, rvalues and expressions.
671 **********************************************************************/
791cfef8
DM
672enum gcc_jit_global_kind
673{
674 /* Global is defined by the client code and visible
675 by name outside of this JIT context via gcc_jit_result_get_global. */
676 GCC_JIT_GLOBAL_EXPORTED,
677
678 /* Global is defined by the client code, but is invisible
679 outside of this JIT context. Analogous to a "static" global. */
680 GCC_JIT_GLOBAL_INTERNAL,
681
682 /* Global is not defined by the client code; we're merely
683 referring to it. Analogous to using an "extern" global from a
684 header file. */
685 GCC_JIT_GLOBAL_IMPORTED
686};
35485da9
DM
687
688extern gcc_jit_lvalue *
689gcc_jit_context_new_global (gcc_jit_context *ctxt,
690 gcc_jit_location *loc,
791cfef8 691 enum gcc_jit_global_kind kind,
35485da9
DM
692 gcc_jit_type *type,
693 const char *name);
694
695/* Upcasting. */
696extern gcc_jit_object *
697gcc_jit_lvalue_as_object (gcc_jit_lvalue *lvalue);
698
699extern gcc_jit_rvalue *
700gcc_jit_lvalue_as_rvalue (gcc_jit_lvalue *lvalue);
701
702extern gcc_jit_object *
703gcc_jit_rvalue_as_object (gcc_jit_rvalue *rvalue);
704
705extern gcc_jit_type *
706gcc_jit_rvalue_get_type (gcc_jit_rvalue *rvalue);
707
708/* Integer constants. */
709extern gcc_jit_rvalue *
710gcc_jit_context_new_rvalue_from_int (gcc_jit_context *ctxt,
711 gcc_jit_type *numeric_type,
712 int value);
ccce3b2a
DM
713
714extern gcc_jit_rvalue *
715gcc_jit_context_new_rvalue_from_long (gcc_jit_context *ctxt,
716 gcc_jit_type *numeric_type,
717 long value);
35485da9
DM
718
719extern gcc_jit_rvalue *
720gcc_jit_context_zero (gcc_jit_context *ctxt,
721 gcc_jit_type *numeric_type);
722
723extern gcc_jit_rvalue *
724gcc_jit_context_one (gcc_jit_context *ctxt,
725 gcc_jit_type *numeric_type);
726
727/* Floating-point constants. */
728extern gcc_jit_rvalue *
729gcc_jit_context_new_rvalue_from_double (gcc_jit_context *ctxt,
730 gcc_jit_type *numeric_type,
731 double value);
732
733/* Pointers. */
734extern gcc_jit_rvalue *
735gcc_jit_context_new_rvalue_from_ptr (gcc_jit_context *ctxt,
736 gcc_jit_type *pointer_type,
737 void *value);
738
739extern gcc_jit_rvalue *
740gcc_jit_context_null (gcc_jit_context *ctxt,
741 gcc_jit_type *pointer_type);
742
743/* String literals. */
744extern gcc_jit_rvalue *
745gcc_jit_context_new_string_literal (gcc_jit_context *ctxt,
746 const char *value);
747
748enum gcc_jit_unary_op
749{
750 /* Negate an arithmetic value; analogous to:
751 -(EXPR)
752 in C. */
753 GCC_JIT_UNARY_OP_MINUS,
754
755 /* Bitwise negation of an integer value (one's complement); analogous
756 to:
757 ~(EXPR)
758 in C. */
759 GCC_JIT_UNARY_OP_BITWISE_NEGATE,
760
761 /* Logical negation of an arithmetic or pointer value; analogous to:
762 !(EXPR)
763 in C. */
18146f45
DM
764 GCC_JIT_UNARY_OP_LOGICAL_NEGATE,
765
766 /* Absolute value of an arithmetic expression; analogous to:
767 abs (EXPR)
768 in C. */
769 GCC_JIT_UNARY_OP_ABS
770
35485da9
DM
771};
772
773extern gcc_jit_rvalue *
774gcc_jit_context_new_unary_op (gcc_jit_context *ctxt,
775 gcc_jit_location *loc,
776 enum gcc_jit_unary_op op,
777 gcc_jit_type *result_type,
778 gcc_jit_rvalue *rvalue);
779
780enum gcc_jit_binary_op
781{
782 /* Addition of arithmetic values; analogous to:
783 (EXPR_A) + (EXPR_B)
784 in C.
785 For pointer addition, use gcc_jit_context_new_array_access. */
786 GCC_JIT_BINARY_OP_PLUS,
787
788 /* Subtraction of arithmetic values; analogous to:
789 (EXPR_A) - (EXPR_B)
790 in C. */
791 GCC_JIT_BINARY_OP_MINUS,
792
793 /* Multiplication of a pair of arithmetic values; analogous to:
794 (EXPR_A) * (EXPR_B)
795 in C. */
796 GCC_JIT_BINARY_OP_MULT,
797
798 /* Quotient of division of arithmetic values; analogous to:
799 (EXPR_A) / (EXPR_B)
800 in C.
801 The result type affects the kind of division: if the result type is
802 integer-based, then the result is truncated towards zero, whereas
803 a floating-point result type indicates floating-point division. */
804 GCC_JIT_BINARY_OP_DIVIDE,
805
806 /* Remainder of division of arithmetic values; analogous to:
807 (EXPR_A) % (EXPR_B)
808 in C. */
809 GCC_JIT_BINARY_OP_MODULO,
810
811 /* Bitwise AND; analogous to:
812 (EXPR_A) & (EXPR_B)
813 in C. */
814 GCC_JIT_BINARY_OP_BITWISE_AND,
815
816 /* Bitwise exclusive OR; analogous to:
817 (EXPR_A) ^ (EXPR_B)
818 in C. */
819 GCC_JIT_BINARY_OP_BITWISE_XOR,
820
821 /* Bitwise inclusive OR; analogous to:
822 (EXPR_A) | (EXPR_B)
823 in C. */
824 GCC_JIT_BINARY_OP_BITWISE_OR,
825
826 /* Logical AND; analogous to:
827 (EXPR_A) && (EXPR_B)
828 in C. */
829 GCC_JIT_BINARY_OP_LOGICAL_AND,
830
831 /* Logical OR; analogous to:
832 (EXPR_A) || (EXPR_B)
833 in C. */
834 GCC_JIT_BINARY_OP_LOGICAL_OR,
835
836 /* Left shift; analogous to:
837 (EXPR_A) << (EXPR_B)
838 in C. */
839 GCC_JIT_BINARY_OP_LSHIFT,
840
841 /* Right shift; analogous to:
842 (EXPR_A) >> (EXPR_B)
843 in C. */
844 GCC_JIT_BINARY_OP_RSHIFT
845};
846
847extern gcc_jit_rvalue *
848gcc_jit_context_new_binary_op (gcc_jit_context *ctxt,
849 gcc_jit_location *loc,
850 enum gcc_jit_binary_op op,
851 gcc_jit_type *result_type,
852 gcc_jit_rvalue *a, gcc_jit_rvalue *b);
853
854/* (Comparisons are treated as separate from "binary_op" to save
855 you having to specify the result_type). */
856
857enum gcc_jit_comparison
858{
859 /* (EXPR_A) == (EXPR_B). */
860 GCC_JIT_COMPARISON_EQ,
861
862 /* (EXPR_A) != (EXPR_B). */
863 GCC_JIT_COMPARISON_NE,
864
865 /* (EXPR_A) < (EXPR_B). */
866 GCC_JIT_COMPARISON_LT,
867
868 /* (EXPR_A) <=(EXPR_B). */
869 GCC_JIT_COMPARISON_LE,
870
871 /* (EXPR_A) > (EXPR_B). */
872 GCC_JIT_COMPARISON_GT,
873
874 /* (EXPR_A) >= (EXPR_B). */
875 GCC_JIT_COMPARISON_GE
876};
877
878extern gcc_jit_rvalue *
879gcc_jit_context_new_comparison (gcc_jit_context *ctxt,
880 gcc_jit_location *loc,
881 enum gcc_jit_comparison op,
882 gcc_jit_rvalue *a, gcc_jit_rvalue *b);
883
884/* Function calls. */
885
886/* Call of a specific function. */
887extern gcc_jit_rvalue *
888gcc_jit_context_new_call (gcc_jit_context *ctxt,
889 gcc_jit_location *loc,
890 gcc_jit_function *func,
891 int numargs , gcc_jit_rvalue **args);
892
893/* Call through a function pointer. */
894extern gcc_jit_rvalue *
895gcc_jit_context_new_call_through_ptr (gcc_jit_context *ctxt,
896 gcc_jit_location *loc,
897 gcc_jit_rvalue *fn_ptr,
898 int numargs, gcc_jit_rvalue **args);
899
900/* Type-coercion.
901
902 Currently only a limited set of conversions are possible:
903 int <-> float
904 int <-> bool */
905extern gcc_jit_rvalue *
906gcc_jit_context_new_cast (gcc_jit_context *ctxt,
907 gcc_jit_location *loc,
908 gcc_jit_rvalue *rvalue,
909 gcc_jit_type *type);
910
911extern gcc_jit_lvalue *
912gcc_jit_context_new_array_access (gcc_jit_context *ctxt,
913 gcc_jit_location *loc,
914 gcc_jit_rvalue *ptr,
915 gcc_jit_rvalue *index);
916
917/* Field access is provided separately for both lvalues and rvalues. */
918
919/* Accessing a field of an lvalue of struct type, analogous to:
920 (EXPR).field = ...;
921 in C. */
922extern gcc_jit_lvalue *
923gcc_jit_lvalue_access_field (gcc_jit_lvalue *struct_or_union,
924 gcc_jit_location *loc,
925 gcc_jit_field *field);
926
927/* Accessing a field of an rvalue of struct type, analogous to:
928 (EXPR).field
929 in C. */
930extern gcc_jit_rvalue *
931gcc_jit_rvalue_access_field (gcc_jit_rvalue *struct_or_union,
932 gcc_jit_location *loc,
933 gcc_jit_field *field);
934
935/* Accessing a field of an rvalue of pointer type, analogous to:
936 (EXPR)->field
937 in C, itself equivalent to (*EXPR).FIELD */
938extern gcc_jit_lvalue *
939gcc_jit_rvalue_dereference_field (gcc_jit_rvalue *ptr,
940 gcc_jit_location *loc,
941 gcc_jit_field *field);
942
943/* Dereferencing a pointer; analogous to:
944 *(EXPR)
945*/
946extern gcc_jit_lvalue *
947gcc_jit_rvalue_dereference (gcc_jit_rvalue *rvalue,
948 gcc_jit_location *loc);
949
950/* Taking the address of an lvalue; analogous to:
951 &(EXPR)
952 in C. */
953extern gcc_jit_rvalue *
954gcc_jit_lvalue_get_address (gcc_jit_lvalue *lvalue,
955 gcc_jit_location *loc);
956
957extern gcc_jit_lvalue *
958gcc_jit_function_new_local (gcc_jit_function *func,
959 gcc_jit_location *loc,
960 gcc_jit_type *type,
961 const char *name);
962
963/**********************************************************************
964 Statement-creation.
965 **********************************************************************/
966
967/* Add evaluation of an rvalue, discarding the result
968 (e.g. a function call that "returns" void).
969
970 This is equivalent to this C code:
971
972 (void)expression;
973*/
974extern void
975gcc_jit_block_add_eval (gcc_jit_block *block,
976 gcc_jit_location *loc,
977 gcc_jit_rvalue *rvalue);
978
979/* Add evaluation of an rvalue, assigning the result to the given
980 lvalue.
981
982 This is roughly equivalent to this C code:
983
984 lvalue = rvalue;
985*/
986extern void
987gcc_jit_block_add_assignment (gcc_jit_block *block,
988 gcc_jit_location *loc,
989 gcc_jit_lvalue *lvalue,
990 gcc_jit_rvalue *rvalue);
991
992/* Add evaluation of an rvalue, using the result to modify an
993 lvalue.
994
995 This is analogous to "+=" and friends:
996
997 lvalue += rvalue;
998 lvalue *= rvalue;
999 lvalue /= rvalue;
1000 etc */
1001extern void
1002gcc_jit_block_add_assignment_op (gcc_jit_block *block,
1003 gcc_jit_location *loc,
1004 gcc_jit_lvalue *lvalue,
1005 enum gcc_jit_binary_op op,
1006 gcc_jit_rvalue *rvalue);
1007
1008/* Add a no-op textual comment to the internal representation of the
1009 code. It will be optimized away, but will be visible in the dumps
1010 seen via
1011 GCC_JIT_BOOL_OPTION_DUMP_INITIAL_TREE
1012 and
1013 GCC_JIT_BOOL_OPTION_DUMP_INITIAL_GIMPLE,
1014 and thus may be of use when debugging how your project's internal
1015 representation gets converted to the libgccjit IR. */
1016extern void
1017gcc_jit_block_add_comment (gcc_jit_block *block,
1018 gcc_jit_location *loc,
1019 const char *text);
1020
1021/* Terminate a block by adding evaluation of an rvalue, branching on the
1022 result to the appropriate successor block.
1023
1024 This is roughly equivalent to this C code:
1025
1026 if (boolval)
1027 goto on_true;
1028 else
1029 goto on_false;
1030
1031 block, boolval, on_true, and on_false must be non-NULL. */
1032extern void
1033gcc_jit_block_end_with_conditional (gcc_jit_block *block,
1034 gcc_jit_location *loc,
1035 gcc_jit_rvalue *boolval,
1036 gcc_jit_block *on_true,
1037 gcc_jit_block *on_false);
1038
1039/* Terminate a block by adding a jump to the given target block.
1040
1041 This is roughly equivalent to this C code:
1042
1043 goto target;
1044*/
1045extern void
1046gcc_jit_block_end_with_jump (gcc_jit_block *block,
1047 gcc_jit_location *loc,
1048 gcc_jit_block *target);
1049
1050/* Terminate a block by adding evaluation of an rvalue, returning the value.
1051
1052 This is roughly equivalent to this C code:
1053
1054 return expression;
1055*/
1056extern void
1057gcc_jit_block_end_with_return (gcc_jit_block *block,
1058 gcc_jit_location *loc,
1059 gcc_jit_rvalue *rvalue);
1060
1061/* Terminate a block by adding a valueless return, for use within a function
1062 with "void" return type.
1063
1064 This is equivalent to this C code:
1065
1066 return;
1067*/
1068extern void
1069gcc_jit_block_end_with_void_return (gcc_jit_block *block,
1070 gcc_jit_location *loc);
1071
1072/**********************************************************************
1073 Nested contexts.
1074 **********************************************************************/
1075
1076/* Given an existing JIT context, create a child context.
1077
1078 The child inherits a copy of all option-settings from the parent.
1079
1080 The child can reference objects created within the parent, but not
1081 vice-versa.
1082
1083 The lifetime of the child context must be bounded by that of the
1084 parent: you should release a child context before releasing the parent
1085 context.
1086
1087 If you use a function from a parent context within a child context,
1088 you have to compile the parent context before you can compile the
1089 child context, and the gcc_jit_result of the parent context must
1090 outlive the gcc_jit_result of the child context.
1091
1092 This allows caching of shared initializations. For example, you could
1093 create types and declarations of global functions in a parent context
1094 once within a process, and then create child contexts whenever a
1095 function or loop becomes hot. Each such child context can be used for
1096 JIT-compiling just one function or loop, but can reference types
1097 and helper functions created within the parent context.
1098
1099 Contexts can be arbitrarily nested, provided the above rules are
1100 followed, but it's probably not worth going above 2 or 3 levels, and
1101 there will likely be a performance hit for such nesting. */
1102
1103extern gcc_jit_context *
1104gcc_jit_context_new_child_context (gcc_jit_context *parent_ctxt);
1105
463366a0
DM
1106/**********************************************************************
1107 Implementation support.
1108 **********************************************************************/
1109
86d0ac88
DM
1110/* Write C source code into "path" that can be compiled into a
1111 self-contained executable (i.e. with libgccjit as the only dependency).
1112 The generated code will attempt to replay the API calls that have been
1113 made into the given context.
1114
1115 This may be useful when debugging the library or client code, for
1116 reducing a complicated recipe for reproducing a bug into a simpler
1117 form.
1118
1119 Typically you need to supply the option "-Wno-unused-variable" when
1120 compiling the generated file (since the result of each API call is
1121 assigned to a unique variable within the generated C source, and not
1122 all are necessarily then used). */
1123
1124extern void
1125gcc_jit_context_dump_reproducer_to_file (gcc_jit_context *ctxt,
1126 const char *path);
1127
463366a0
DM
1128/* Enable the dumping of a specific set of internal state from the
1129 compilation, capturing the result in-memory as a buffer.
1130
1131 Parameter "dumpname" corresponds to the equivalent gcc command-line
1132 option, without the "-fdump-" prefix.
1133 For example, to get the equivalent of "-fdump-tree-vrp1", supply
1134 "tree-vrp1".
1135 The context directly stores the dumpname as a (const char *), so the
1136 passed string must outlive the context.
1137
fdce7209
DM
1138 gcc_jit_context_compile and gcc_jit_context_to_file
1139 will capture the dump as a dynamically-allocated buffer, writing
1140 it to ``*out_ptr``.
463366a0
DM
1141
1142 The caller becomes responsible for calling
1143 free (*out_ptr)
fdce7209
DM
1144 each time that gcc_jit_context_compile or gcc_jit_context_to_file
1145 are called. *out_ptr will be written to, either with the address of a
1146 buffer, or with NULL if an error occurred.
463366a0
DM
1147
1148 This API entrypoint is likely to be less stable than the others.
1149 In particular, both the precise dumpnames, and the format and content
1150 of the dumps are subject to change.
1151
1152 It exists primarily for writing the library's own test suite. */
1153
1154extern void
1155gcc_jit_context_enable_dump (gcc_jit_context *ctxt,
1156 const char *dumpname,
1157 char **out_ptr);
1158
35485da9
DM
1159#ifdef __cplusplus
1160}
1161#endif /* __cplusplus */
1162
1163#endif /* LIBGCCJIT_H */