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