summaryrefslogtreecommitdiff
path: root/gcc/jit/docs/cp/topics/functions.rst
blob: a31b2787f7db14ea686ff6db8b6bbc428160c2eb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
.. Copyright (C) 2014-2015 Free Software Foundation, Inc.
   Originally contributed by David Malcolm <dmalcolm@redhat.com>

   This is free software: you can redistribute it and/or modify it
   under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see
   <http://www.gnu.org/licenses/>.

.. default-domain:: cpp

Creating and using functions
============================

Params
------
.. class:: gccjit::param

   A `gccjit::param` represents a parameter to a function.

.. function:: gccjit::param \
              gccjit::context::new_param (gccjit::type type,\
                                          const char *name, \
                                          gccjit::location loc)

   In preparation for creating a function, create a new parameter of the
   given type and name.

:class:`gccjit::param` is a subclass of :class:`gccjit::lvalue` (and thus
of :class:`gccjit::rvalue` and :class:`gccjit::object`).  It is a thin
wrapper around the C API's :c:type:`gcc_jit_param *`.

Functions
---------

.. class:: gccjit::function

   A `gccjit::function` represents a function - either one that we're
   creating ourselves, or one that we're referencing.

.. function::  gccjit::function \
               gccjit::context::new_function (enum gcc_jit_function_kind,\
                                              gccjit::type return_type, \
                                              const char *name, \
                                              std::vector<param> &params, \
                                              int is_variadic, \
                                              gccjit::location loc) \

   Create a gcc_jit_function with the given name and parameters.

   Parameters "is_variadic" and "loc" are optional.

   This is a wrapper around the C API's :c:func:`gcc_jit_context_new_function`.

.. function::  gccjit::function \
               gccjit::context::get_builtin_function (const char *name)

   This is a wrapper around the C API's
   :c:func:`gcc_jit_context_get_builtin_function`.

.. function::  gccjit::param \
               gccjit::function::get_param (int index) const

   Get the param of the given index (0-based).

.. function::  void \
               gccjit::function::dump_to_dot (const char *path)

   Emit the function in graphviz format to the given path.

.. function:: gccjit::lvalue \
              gccjit::function::new_local (gccjit::type type,\
                                           const char *name, \
                                           gccjit::location loc)

   Create a new local variable within the function, of the given type and
   name.

Blocks
------
.. class:: gccjit::block

   A `gccjit::block` represents a basic block within a function  i.e. a
   sequence of statements with a single entry point and a single exit
   point.

   :class:`gccjit::block` is a subclass of :class:`gccjit::object`.

   The first basic block that you create within a function will
   be the entrypoint.

   Each basic block that you create within a function must be
   terminated, either with a conditional, a jump, or a return.

   It's legal to have multiple basic blocks that return within
   one function.

.. function::  gccjit::block \
               gccjit::function::new_block (const char *name)

   Create a basic block of the given name.  The name may be NULL, but
   providing meaningful names is often helpful when debugging: it may
   show up in dumps of the internal representation, and in error
   messages.

Statements
----------

.. function:: void\
              gccjit::block::add_eval (gccjit::rvalue rvalue, \
                                       gccjit::location loc)

   Add evaluation of an rvalue, discarding the result
   (e.g. a function call that "returns" void).

   This is equivalent to this C code:

   .. code-block:: c

     (void)expression;

.. function:: void\
              gccjit::block::add_assignment (gccjit::lvalue lvalue, \
                                             gccjit::rvalue rvalue, \
                                             gccjit::location loc)

   Add evaluation of an rvalue, assigning the result to the given
   lvalue.

   This is roughly equivalent to this C code:

   .. code-block:: c

     lvalue = rvalue;

.. function:: void\
              gccjit::block::add_assignment_op (gccjit::lvalue lvalue, \
                                                enum gcc_jit_binary_op, \
                                                gccjit::rvalue rvalue, \
                                                gccjit::location loc)

   Add evaluation of an rvalue, using the result to modify an
   lvalue.

   This is analogous to "+=" and friends:

   .. code-block:: c

     lvalue += rvalue;
     lvalue *= rvalue;
     lvalue /= rvalue;

   etc.  For example:

   .. code-block:: c

     /* "i++" */
     loop_body.add_assignment_op (
       i,
       GCC_JIT_BINARY_OP_PLUS,
       ctxt.one (int_type));

.. function:: void\
              gccjit::block::add_comment (const char *text, \
	                                  gccjit::location loc)

   Add a no-op textual comment to the internal representation of the
   code.  It will be optimized away, but will be visible in the dumps
   seen via :c:macro:`GCC_JIT_BOOL_OPTION_DUMP_INITIAL_TREE`
   and :c:macro:`GCC_JIT_BOOL_OPTION_DUMP_INITIAL_GIMPLE`,
   and thus may be of use when debugging how your project's internal
   representation gets converted to the libgccjit IR.

   Parameter "loc" is optional.

.. function:: void\
              gccjit::block::end_with_conditional (gccjit::rvalue boolval,\
                                                   gccjit::block on_true,\
                                                   gccjit::block on_false, \
                                                   gccjit::location loc)

   Terminate a block by adding evaluation of an rvalue, branching on the
   result to the appropriate successor block.

   This is roughly equivalent to this C code:

   .. code-block:: c

     if (boolval)
       goto on_true;
     else
       goto on_false;

   block, boolval, on_true, and on_false must be non-NULL.

.. function:: void\
              gccjit::block::end_with_jump (gccjit::block target, \
                                            gccjit::location loc)

   Terminate a block by adding a jump to the given target block.

   This is roughly equivalent to this C code:

   .. code-block:: c

      goto target;

.. function:: void\
              gccjit::block::end_with_return (gccjit::rvalue rvalue, \
                                              gccjit::location loc)

   Terminate a block.

   Both params are optional.

   An rvalue must be provided for a function returning non-void, and
   must not be provided by a function "returning" `void`.

   If an rvalue is provided, the block is terminated by evaluating the
   rvalue and returning the value.

   This is roughly equivalent to this C code:

   .. code-block:: c

      return expression;

   If an rvalue is not provided, the block is terminated by adding a
   valueless return, for use within a function with "void" return type.

   This is equivalent to this C code:

   .. code-block:: c

      return;