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