1 /* Usage example for libgccjit.so
2 Copyright (C) 2014 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
)
29 Simple sum-of-squares, to test conditionals and looping
35 for (i = 0; i < n ; i ++)
41 gcc_jit_type
*the_type
=
42 gcc_jit_context_get_type (ctxt
, GCC_JIT_TYPE_INT
);
43 gcc_jit_type
*return_type
= the_type
;
46 gcc_jit_context_new_param (ctxt
, NULL
, the_type
, "n");
47 gcc_jit_param
*params
[1] = {n
};
48 gcc_jit_function
*func
=
49 gcc_jit_context_new_function (ctxt
, NULL
,
50 GCC_JIT_FUNCTION_EXPORTED
,
57 gcc_jit_function_new_local (func
, NULL
, the_type
, "i");
59 gcc_jit_function_new_local (func
, NULL
, the_type
, "sum");
61 gcc_jit_block
*b_initial
=
62 gcc_jit_function_new_block (func
, "initial");
63 gcc_jit_block
*b_loop_cond
=
64 gcc_jit_function_new_block (func
, "loop_cond");
65 gcc_jit_block
*b_loop_body
=
66 gcc_jit_function_new_block (func
, "loop_body");
67 gcc_jit_block
*b_after_loop
=
68 gcc_jit_function_new_block (func
, "after_loop");
71 gcc_jit_block_add_assignment (
74 gcc_jit_context_zero (ctxt
, the_type
));
77 gcc_jit_block_add_assignment (
80 gcc_jit_context_zero (ctxt
, the_type
));
82 gcc_jit_block_end_with_jump (b_initial
, NULL
, b_loop_cond
);
85 gcc_jit_block_end_with_conditional (
87 gcc_jit_context_new_comparison (
89 GCC_JIT_COMPARISON_GE
,
90 gcc_jit_lvalue_as_rvalue (i
),
91 gcc_jit_param_as_rvalue (n
)),
96 gcc_jit_block_add_assignment_op (
99 GCC_JIT_BINARY_OP_PLUS
,
100 gcc_jit_context_new_binary_op (
102 GCC_JIT_BINARY_OP_MULT
, the_type
,
103 gcc_jit_lvalue_as_rvalue (i
),
104 gcc_jit_lvalue_as_rvalue (i
)));
107 gcc_jit_block_add_assignment_op (
110 GCC_JIT_BINARY_OP_PLUS
,
111 gcc_jit_context_one (ctxt
, the_type
));
113 gcc_jit_block_end_with_jump (b_loop_body
, NULL
, b_loop_cond
);
116 gcc_jit_block_end_with_return (
119 gcc_jit_lvalue_as_rvalue (sum
));
123 main (int argc
, char **argv
)
125 gcc_jit_context
*ctxt
= NULL
;
126 gcc_jit_result
*result
= NULL
;
128 /* Get a "context" object for working with the library. */
129 ctxt
= gcc_jit_context_acquire ();
132 fprintf (stderr
, "NULL ctxt");
136 /* Set some options on the context.
137 Let's see the code being generated, in assembler form. */
138 gcc_jit_context_set_bool_option (
140 GCC_JIT_BOOL_OPTION_DUMP_GENERATED_CODE
,
143 /* Populate the context. */
146 /* Compile the code. */
147 result
= gcc_jit_context_compile (ctxt
);
150 fprintf (stderr
, "NULL result");
154 /* Extract the generated code from "result". */
155 typedef int (*loop_test_fn_type
) (int);
156 loop_test_fn_type loop_test
=
157 (loop_test_fn_type
)gcc_jit_result_get_code (result
, "loop_test");
160 fprintf (stderr
, "NULL loop_test");
164 /* Run the generated code. */
165 int val
= loop_test (10);
166 printf("loop_test returned: %d\n", val
);
169 gcc_jit_context_release (ctxt
);
170 gcc_jit_result_release (result
);