| 1 |
/* Usage example for libgccjit.so's C++ API |
| 2 |
Copyright (C) 2014-2025 Free Software Foundation, Inc. |
| 3 |
|
| 4 |
This file is part of GCC. |
| 5 |
|
| 6 |
GCC is free software; you can redistribute it and/or modify it |
| 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 |
#include <libgccjit++.h> |
| 21 |
|
| 22 |
#include <stdlib.h> |
| 23 |
#include <stdio.h> |
| 24 |
|
| 25 |
void |
| 26 |
create_code (gccjit::context ctxt) |
| 27 |
{ |
| 28 |
/* Let's try to inject the equivalent of this C code: |
| 29 |
|
| 30 |
int square (int i) |
| 31 |
{ |
| 32 |
return i * i; |
| 33 |
} |
| 34 |
*/ |
| 35 |
gccjit::type int_type = ctxt.get_type (GCC_JIT_TYPE_INT); |
| 36 |
gccjit::param param_i = ctxt.new_param (int_type, "i"); |
| 37 |
std::vector<gccjit::param> params; |
| 38 |
params.push_back (param_i); |
| 39 |
gccjit::function func = ctxt.new_function (GCC_JIT_FUNCTION_EXPORTED, |
| 40 |
int_type, |
| 41 |
"square", |
| 42 |
params, 0); |
| 43 |
|
| 44 |
gccjit::block block = func.new_block (); |
| 45 |
|
| 46 |
gccjit::rvalue expr = |
| 47 |
ctxt.new_binary_op (GCC_JIT_BINARY_OP_MULT, int_type, |
| 48 |
param_i, param_i); |
| 49 |
|
| 50 |
block.end_with_return (expr); |
| 51 |
} |
| 52 |
|
| 53 |
int |
| 54 |
main (int argc, char **argv) |
| 55 |
{ |
| 56 |
/* Get a "context" object for working with the library. */ |
| 57 |
gccjit::context ctxt = gccjit::context::acquire (); |
| 58 |
|
| 59 |
/* Set some options on the context. |
| 60 |
Turn this on to see the code being generated, in assembler form. */ |
| 61 |
ctxt.set_bool_option ( |
| 62 |
GCC_JIT_BOOL_OPTION_DUMP_GENERATED_CODE, |
| 63 |
0); |
| 64 |
|
| 65 |
/* Populate the context. */ |
| 66 |
create_code (ctxt); |
| 67 |
|
| 68 |
/* Compile the code. */ |
| 69 |
gcc_jit_result *result = ctxt.compile (); |
| 70 |
|
| 71 |
/* We're done with the context; we can release it: */ |
| 72 |
ctxt.release (); |
| 73 |
|
| 74 |
if (!result) |
| 75 |
{ |
| 76 |
fprintf (stderr, "NULL result"); |
| 77 |
return 1; |
| 78 |
} |
| 79 |
|
| 80 |
/* Extract the generated code from "result". */ |
| 81 |
void *fn_ptr = gcc_jit_result_get_code (result, "square"); |
| 82 |
if (!fn_ptr) |
| 83 |
{ |
| 84 |
fprintf (stderr, "NULL fn_ptr"); |
| 85 |
gcc_jit_result_release (result); |
| 86 |
return 1; |
| 87 |
} |
| 88 |
|
| 89 |
typedef int (*fn_type) (int); |
| 90 |
fn_type square = (fn_type)fn_ptr; |
| 91 |
printf ("result: %d\n", square (5)); |
| 92 |
|
| 93 |
gcc_jit_result_release (result); |
| 94 |
return 0; |
| 95 |
} |