1 /* Smoketest example for libgccjit.so
2 Copyright (C) 2014-2023 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
11 GCC is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 #include <libgccjit.h>
26 create_code (gcc_jit_context
*ctxt
)
28 /* Let's try to inject the equivalent of:
30 greet (const char *name)
32 printf ("hello %s\n", name);
35 gcc_jit_type
*void_type
=
36 gcc_jit_context_get_type (ctxt
, GCC_JIT_TYPE_VOID
);
37 gcc_jit_type
*const_char_ptr_type
=
38 gcc_jit_context_get_type (ctxt
, GCC_JIT_TYPE_CONST_CHAR_PTR
);
39 gcc_jit_param
*param_name
=
40 gcc_jit_context_new_param (ctxt
, NULL
, const_char_ptr_type
, "name");
41 gcc_jit_function
*func
=
42 gcc_jit_context_new_function (ctxt
, NULL
,
43 GCC_JIT_FUNCTION_EXPORTED
,
49 gcc_jit_param
*param_format
=
50 gcc_jit_context_new_param (ctxt
, NULL
, const_char_ptr_type
, "format");
51 gcc_jit_function
*printf_func
=
52 gcc_jit_context_new_function (ctxt
, NULL
,
53 GCC_JIT_FUNCTION_IMPORTED
,
54 gcc_jit_context_get_type (
55 ctxt
, GCC_JIT_TYPE_INT
),
59 gcc_jit_rvalue
*args
[2];
60 args
[0] = gcc_jit_context_new_string_literal (ctxt
, "hello %s\n");
61 args
[1] = gcc_jit_param_as_rvalue (param_name
);
63 gcc_jit_block
*block
= gcc_jit_function_new_block (func
, NULL
);
65 gcc_jit_block_add_eval (
67 gcc_jit_context_new_call (ctxt
,
71 gcc_jit_block_end_with_void_return (block
, NULL
);
75 main (int argc
, char **argv
)
77 gcc_jit_context
*ctxt
;
78 gcc_jit_result
*result
;
80 /* Get a "context" object for working with the library. */
81 ctxt
= gcc_jit_context_acquire ();
84 fprintf (stderr
, "NULL ctxt");
88 /* Set some options on the context.
89 Let's see the code being generated, in assembler form. */
90 gcc_jit_context_set_bool_option (
92 GCC_JIT_BOOL_OPTION_DUMP_GENERATED_CODE
,
95 /* Populate the context. */
98 /* Compile the code. */
99 result
= gcc_jit_context_compile (ctxt
);
102 fprintf (stderr
, "NULL result");
106 /* Extract the generated code from "result". */
107 typedef void (*fn_type
) (const char *);
109 (fn_type
)gcc_jit_result_get_code (result
, "greet");
112 fprintf (stderr
, "NULL greet");
116 /* Now call the generated function: */
120 gcc_jit_context_release (ctxt
);
121 gcc_jit_result_release (result
);