]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/jit/docs/topics/functions.rst
PR jit/66546: Add gcc_jit_context_set_bool_allow_unreachable_blocks
[thirdparty/gcc.git] / gcc / jit / docs / topics / functions.rst
CommitLineData
179ed8f5 1.. Copyright (C) 2014-2015 Free Software Foundation, Inc.
35485da9
DM
2 Originally contributed by David Malcolm <dmalcolm@redhat.com>
3
4 This is free software: you can redistribute it and/or modify it
5 under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful, but
10 WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see
16 <http://www.gnu.org/licenses/>.
17
18.. default-domain:: c
19
20Creating and using functions
21============================
22
23Params
24------
25.. type:: gcc_jit_param
26
27 A `gcc_jit_param` represents a parameter to a function.
28
29.. function:: gcc_jit_param *\
30 gcc_jit_context_new_param (gcc_jit_context *ctxt,\
31 gcc_jit_location *loc,\
32 gcc_jit_type *type,\
33 const char *name)
34
35 In preparation for creating a function, create a new parameter of the
36 given type and name.
37
38Parameters are lvalues, and thus are also rvalues (and objects), so the
39following upcasts are available:
40
41.. function:: gcc_jit_lvalue *\
42 gcc_jit_param_as_lvalue (gcc_jit_param *param)
43
44 Upcasting from param to lvalue.
45
46.. function:: gcc_jit_rvalue *\
47 gcc_jit_param_as_rvalue (gcc_jit_param *param)
48
49 Upcasting from param to rvalue.
50
51.. function:: gcc_jit_object *\
52 gcc_jit_param_as_object (gcc_jit_param *param)
53
54 Upcasting from param to object.
55
56
57Functions
58---------
59
60.. type:: gcc_jit_function
61
62 A `gcc_jit_function` represents a function - either one that we're
63 creating ourselves, or one that we're referencing.
64
65.. function:: gcc_jit_function *\
66 gcc_jit_context_new_function (gcc_jit_context *ctxt,\
67 gcc_jit_location *loc,\
68 enum gcc_jit_function_kind kind,\
69 gcc_jit_type *return_type,\
70 const char *name,\
71 int num_params,\
72 gcc_jit_param **params,\
73 int is_variadic)
74
75 Create a gcc_jit_function with the given name and parameters.
76
77 .. type:: enum gcc_jit_function_kind
78
79 This enum controls the kind of function created, and has the following
80 values:
81
82 .. macro:: GCC_JIT_FUNCTION_EXPORTED
83
84 Function is defined by the client code and visible
85 by name outside of the JIT.
86
81ba15f1
DM
87 This value is required if you want to extract machine code
88 for this function from a :type:`gcc_jit_result` via
89 :func:`gcc_jit_result_get_code`.
90
35485da9
DM
91 .. macro:: GCC_JIT_FUNCTION_INTERNAL
92
93 Function is defined by the client code, but is invisible
94 outside of the JIT. Analogous to a "static" function.
95
96 .. macro:: GCC_JIT_FUNCTION_IMPORTED
97
98 Function is not defined by the client code; we're merely
99 referring to it. Analogous to using an "extern" function from a
100 header file.
101
102 .. macro:: GCC_JIT_FUNCTION_ALWAYS_INLINE
103
104 Function is only ever inlined into other functions, and is
105 invisible outside of the JIT.
106
107 Analogous to prefixing with ``inline`` and adding
108 ``__attribute__((always_inline))``
109
110 Inlining will only occur when the optimization level is
111 above 0; when optimization is off, this is essentially the
112 same as GCC_JIT_FUNCTION_INTERNAL.
113
114.. function:: gcc_jit_function *\
115 gcc_jit_context_get_builtin_function (gcc_jit_context *ctxt,\
116 const char *name)
117
118.. function:: gcc_jit_object *\
119 gcc_jit_function_as_object (gcc_jit_function *func)
120
121 Upcasting from function to object.
122
123.. function:: gcc_jit_param *\
124 gcc_jit_function_get_param (gcc_jit_function *func, int index)
125
126 Get the param of the given index (0-based).
127
128.. function:: void \
129 gcc_jit_function_dump_to_dot (gcc_jit_function *func,\
130 const char *path)
131
132 Emit the function in graphviz format to the given path.
133
134.. function:: gcc_jit_lvalue *\
135 gcc_jit_function_new_local (gcc_jit_function *func,\
136 gcc_jit_location *loc,\
137 gcc_jit_type *type,\
138 const char *name)
139
140 Create a new local variable within the function, of the given type and
141 name.
142
143
144Blocks
145------
146.. type:: gcc_jit_block
147
148 A `gcc_jit_block` represents a basic block within a function i.e. a
149 sequence of statements with a single entry point and a single exit
150 point.
151
152 The first basic block that you create within a function will
153 be the entrypoint.
154
155 Each basic block that you create within a function must be
156 terminated, either with a conditional, a jump, or a return.
157
158 It's legal to have multiple basic blocks that return within
159 one function.
160
161.. function:: gcc_jit_block *\
162 gcc_jit_function_new_block (gcc_jit_function *func,\
163 const char *name)
164
165 Create a basic block of the given name. The name may be NULL, but
166 providing meaningful names is often helpful when debugging: it may
167 show up in dumps of the internal representation, and in error
168 messages.
169
170.. function:: gcc_jit_object *\
171 gcc_jit_block_as_object (gcc_jit_block *block)
172
173 Upcast from block to object.
174
175.. function:: gcc_jit_function *\
176 gcc_jit_block_get_function (gcc_jit_block *block)
177
178 Which function is this block within?
179
180
181Statements
182----------
183
184.. function:: void\
185 gcc_jit_block_add_eval (gcc_jit_block *block,\
186 gcc_jit_location *loc,\
187 gcc_jit_rvalue *rvalue)
188
189 Add evaluation of an rvalue, discarding the result
190 (e.g. a function call that "returns" void).
191
192 This is equivalent to this C code:
193
194 .. code-block:: c
195
196 (void)expression;
197
198.. function:: void\
199 gcc_jit_block_add_assignment (gcc_jit_block *block,\
200 gcc_jit_location *loc,\
201 gcc_jit_lvalue *lvalue,\
202 gcc_jit_rvalue *rvalue)
203
204 Add evaluation of an rvalue, assigning the result to the given
205 lvalue.
206
207 This is roughly equivalent to this C code:
208
209 .. code-block:: c
210
211 lvalue = rvalue;
212
213.. function:: void\
214 gcc_jit_block_add_assignment_op (gcc_jit_block *block,\
215 gcc_jit_location *loc,\
216 gcc_jit_lvalue *lvalue,\
217 enum gcc_jit_binary_op op,\
218 gcc_jit_rvalue *rvalue)
219
220 Add evaluation of an rvalue, using the result to modify an
221 lvalue.
222
223 This is analogous to "+=" and friends:
224
225 .. code-block:: c
226
227 lvalue += rvalue;
228 lvalue *= rvalue;
229 lvalue /= rvalue;
230
231 etc. For example:
232
233 .. code-block:: c
234
235 /* "i++" */
236 gcc_jit_block_add_assignment_op (
237 loop_body, NULL,
238 i,
239 GCC_JIT_BINARY_OP_PLUS,
240 gcc_jit_context_one (ctxt, int_type));
241
242.. function:: void\
243 gcc_jit_block_add_comment (gcc_jit_block *block,\
244 gcc_jit_location *loc,\
245 const char *text)
246
247 Add a no-op textual comment to the internal representation of the
248 code. It will be optimized away, but will be visible in the dumps
249 seen via :macro:`GCC_JIT_BOOL_OPTION_DUMP_INITIAL_TREE`
250 and :macro:`GCC_JIT_BOOL_OPTION_DUMP_INITIAL_GIMPLE`,
251 and thus may be of use when debugging how your project's internal
252 representation gets converted to the libgccjit IR.
253
254.. function:: void\
255 gcc_jit_block_end_with_conditional (gcc_jit_block *block,\
256 gcc_jit_location *loc,\
257 gcc_jit_rvalue *boolval,\
258 gcc_jit_block *on_true,\
259 gcc_jit_block *on_false)
260
261 Terminate a block by adding evaluation of an rvalue, branching on the
262 result to the appropriate successor block.
263
264 This is roughly equivalent to this C code:
265
266 .. code-block:: c
267
268 if (boolval)
269 goto on_true;
270 else
271 goto on_false;
272
273 block, boolval, on_true, and on_false must be non-NULL.
274
275.. function:: void\
276 gcc_jit_block_end_with_jump (gcc_jit_block *block,\
277 gcc_jit_location *loc,\
278 gcc_jit_block *target)
279
280
281 Terminate a block by adding a jump to the given target block.
282
283 This is roughly equivalent to this C code:
284
285 .. code-block:: c
286
287 goto target;
288
289.. function:: void\
290 gcc_jit_block_end_with_return (gcc_jit_block *block,\
291 gcc_jit_location *loc,\
292 gcc_jit_rvalue *rvalue)
293
294
295 Terminate a block by adding evaluation of an rvalue, returning the value.
296
297 This is roughly equivalent to this C code:
298
299 .. code-block:: c
300
301 return expression;
302
303.. function:: void\
304 gcc_jit_block_end_with_void_return (gcc_jit_block *block,\
305 gcc_jit_location *loc)
306
307
308 Terminate a block by adding a valueless return, for use within a function
309 with "void" return type.
310
311 This is equivalent to this C code:
312
313 .. code-block:: c
314
315 return;