jit: implement gcc_jit_rvalue_set_bool_require_tail_call
[official-gcc.git] / gcc / jit / docs / topics / expressions.rst
blob261483c78b0e4eb1424f513607721854dd4836aa
1 .. Copyright (C) 2014-2016 Free Software Foundation, Inc.
2    Originally contributed by David Malcolm <dmalcolm@redhat.com>
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.
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.
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/>.
18 .. default-domain:: c
20 Expressions
21 ===========
23 Rvalues
24 -------
25 .. type:: gcc_jit_rvalue
27 A :c:type:`gcc_jit_rvalue *` is an expression that can be computed.
29 It can be simple, e.g.:
31   * an integer value e.g. `0` or `42`
32   * a string literal e.g. `"Hello world"`
33   * a variable e.g. `i`.  These are also lvalues (see below).
35 or compound e.g.:
37   * a unary expression e.g. `!cond`
38   * a binary expression e.g. `(a + b)`
39   * a function call e.g. `get_distance (&player_ship, &target)`
40   * etc.
42 Every rvalue has an associated type, and the API will check to ensure
43 that types match up correctly (otherwise the context will emit an error).
45 .. function:: gcc_jit_type *gcc_jit_rvalue_get_type (gcc_jit_rvalue *rvalue)
47   Get the type of this rvalue.
49 .. function:: gcc_jit_object *gcc_jit_rvalue_as_object (gcc_jit_rvalue *rvalue)
51   Upcast the given rvalue to be an object.
54 Simple expressions
55 ******************
57 .. function:: gcc_jit_rvalue *\
58               gcc_jit_context_new_rvalue_from_int (gcc_jit_context *ctxt, \
59                                                    gcc_jit_type *numeric_type, \
60                                                    int value)
62    Given a numeric type (integer or floating point), build an rvalue for
63    the given constant :c:type:`int` value.
65 .. function:: gcc_jit_rvalue *\
66               gcc_jit_context_new_rvalue_from_long (gcc_jit_context *ctxt, \
67                                                     gcc_jit_type *numeric_type, \
68                                                     long value)
70    Given a numeric type (integer or floating point), build an rvalue for
71    the given constant :c:type:`long` value.
73 .. function::  gcc_jit_rvalue *gcc_jit_context_zero (gcc_jit_context *ctxt, \
74                                                      gcc_jit_type *numeric_type)
76    Given a numeric type (integer or floating point), get the rvalue for
77    zero.  Essentially this is just a shortcut for:
79    .. code-block:: c
81       gcc_jit_context_new_rvalue_from_int (ctxt, numeric_type, 0)
83 .. function::  gcc_jit_rvalue *gcc_jit_context_one (gcc_jit_context *ctxt, \
84                                                     gcc_jit_type *numeric_type)
86    Given a numeric type (integer or floating point), get the rvalue for
87    one.  Essentially this is just a shortcut for:
89    .. code-block:: c
91       gcc_jit_context_new_rvalue_from_int (ctxt, numeric_type, 1)
93 .. function::  gcc_jit_rvalue *\
94                gcc_jit_context_new_rvalue_from_double (gcc_jit_context *ctxt, \
95                                                        gcc_jit_type *numeric_type, \
96                                                        double value)
98    Given a numeric type (integer or floating point), build an rvalue for
99    the given constant :c:type:`double` value.
101 .. function:: gcc_jit_rvalue *\
102               gcc_jit_context_new_rvalue_from_ptr (gcc_jit_context *ctxt, \
103                                                    gcc_jit_type *pointer_type, \
104                                                    void *value)
106    Given a pointer type, build an rvalue for the given address.
108 .. function:: gcc_jit_rvalue *gcc_jit_context_null (gcc_jit_context *ctxt, \
109                                                     gcc_jit_type *pointer_type)
111    Given a pointer type, build an rvalue for ``NULL``.  Essentially this
112    is just a shortcut for:
114    .. code-block:: c
116       gcc_jit_context_new_rvalue_from_ptr (ctxt, pointer_type, NULL)
118 .. function:: gcc_jit_rvalue *\
119               gcc_jit_context_new_string_literal (gcc_jit_context *ctxt, \
120                                                   const char *value)
122    Generate an rvalue for the given NIL-terminated string, of type
123    :c:data:`GCC_JIT_TYPE_CONST_CHAR_PTR`.
125    The parameter ``value`` must be non-NULL.  The call takes a copy of the
126    underlying string, so it is valid to pass in a pointer to an on-stack
127    buffer.
129 Unary Operations
130 ****************
132 .. function:: gcc_jit_rvalue * \
133               gcc_jit_context_new_unary_op (gcc_jit_context *ctxt, \
134                                             gcc_jit_location *loc, \
135                                             enum gcc_jit_unary_op op, \
136                                             gcc_jit_type *result_type, \
137                                             gcc_jit_rvalue *rvalue)
139    Build a unary operation out of an input rvalue.
141 .. type:: enum gcc_jit_unary_op
143 The available unary operations are:
145 ==========================================  ============
146 Unary Operation                             C equivalent
147 ==========================================  ============
148 :c:macro:`GCC_JIT_UNARY_OP_MINUS`           `-(EXPR)`
149 :c:macro:`GCC_JIT_UNARY_OP_BITWISE_NEGATE`  `~(EXPR)`
150 :c:macro:`GCC_JIT_UNARY_OP_LOGICAL_NEGATE`  `!(EXPR)`
151 :c:macro:`GCC_JIT_UNARY_OP_ABS`             `abs (EXPR)`
152 ==========================================  ============
154 .. c:macro:: GCC_JIT_UNARY_OP_MINUS
156     Negate an arithmetic value; analogous to:
158     .. code-block:: c
160        -(EXPR)
162     in C.
164 .. c:macro:: GCC_JIT_UNARY_OP_BITWISE_NEGATE
166     Bitwise negation of an integer value (one's complement); analogous
167     to:
169     .. code-block:: c
171        ~(EXPR)
173     in C.
175 .. c:macro:: GCC_JIT_UNARY_OP_LOGICAL_NEGATE
177     Logical negation of an arithmetic or pointer value; analogous to:
179     .. code-block:: c
181        !(EXPR)
183     in C.
185 .. c:macro:: GCC_JIT_UNARY_OP_ABS
187     Absolute value of an arithmetic expression; analogous to:
189     .. code-block:: c
191         abs (EXPR)
193     in C.
195 Binary Operations
196 *****************
198 .. function:: gcc_jit_rvalue *gcc_jit_context_new_binary_op (gcc_jit_context *ctxt, \
199                                                              gcc_jit_location *loc, \
200                                                              enum gcc_jit_binary_op op, \
201                                                              gcc_jit_type *result_type, \
202                                                              gcc_jit_rvalue *a, gcc_jit_rvalue *b)
204    Build a binary operation out of two constituent rvalues.
206 .. type:: enum gcc_jit_binary_op
208 The available binary operations are:
210 ========================================  ============
211 Binary Operation                          C equivalent
212 ========================================  ============
213 :c:macro:`GCC_JIT_BINARY_OP_PLUS`         `x + y`
214 :c:macro:`GCC_JIT_BINARY_OP_MINUS`        `x - y`
215 :c:macro:`GCC_JIT_BINARY_OP_MULT`         `x * y`
216 :c:macro:`GCC_JIT_BINARY_OP_DIVIDE`       `x / y`
217 :c:macro:`GCC_JIT_BINARY_OP_MODULO`       `x % y`
218 :c:macro:`GCC_JIT_BINARY_OP_BITWISE_AND`  `x & y`
219 :c:macro:`GCC_JIT_BINARY_OP_BITWISE_XOR`  `x ^ y`
220 :c:macro:`GCC_JIT_BINARY_OP_BITWISE_OR`   `x | y`
221 :c:macro:`GCC_JIT_BINARY_OP_LOGICAL_AND`  `x && y`
222 :c:macro:`GCC_JIT_BINARY_OP_LOGICAL_OR`   `x || y`
223 :c:macro:`GCC_JIT_BINARY_OP_LSHIFT`       `x << y`
224 :c:macro:`GCC_JIT_BINARY_OP_RSHIFT`       `x >> y`
225 ========================================  ============
227 .. c:macro:: GCC_JIT_BINARY_OP_PLUS
229    Addition of arithmetic values; analogous to:
231    .. code-block:: c
233      (EXPR_A) + (EXPR_B)
235    in C.
237    For pointer addition, use :c:func:`gcc_jit_context_new_array_access`.
239 .. c:macro:: GCC_JIT_BINARY_OP_MINUS
241    Subtraction of arithmetic values; analogous to:
243    .. code-block:: c
245      (EXPR_A) - (EXPR_B)
247    in C.
249 .. c:macro:: GCC_JIT_BINARY_OP_MULT
251    Multiplication of a pair of arithmetic values; analogous to:
253    .. code-block:: c
255      (EXPR_A) * (EXPR_B)
257    in C.
259 .. c:macro:: GCC_JIT_BINARY_OP_DIVIDE
261    Quotient of division of arithmetic values; analogous to:
263    .. code-block:: c
265      (EXPR_A) / (EXPR_B)
267    in C.
269    The result type affects the kind of division: if the result type is
270    integer-based, then the result is truncated towards zero, whereas
271    a floating-point result type indicates floating-point division.
273 .. c:macro:: GCC_JIT_BINARY_OP_MODULO
275    Remainder of division of arithmetic values; analogous to:
277    .. code-block:: c
279      (EXPR_A) % (EXPR_B)
281    in C.
283 .. c:macro:: GCC_JIT_BINARY_OP_BITWISE_AND
285    Bitwise AND; analogous to:
287    .. code-block:: c
289      (EXPR_A) & (EXPR_B)
291    in C.
293 .. c:macro:: GCC_JIT_BINARY_OP_BITWISE_XOR
295    Bitwise exclusive OR; analogous to:
297    .. code-block:: c
299       (EXPR_A) ^ (EXPR_B)
301    in C.
303 .. c:macro:: GCC_JIT_BINARY_OP_BITWISE_OR
305    Bitwise inclusive OR; analogous to:
307    .. code-block:: c
309      (EXPR_A) | (EXPR_B)
311    in C.
313 .. c:macro:: GCC_JIT_BINARY_OP_LOGICAL_AND
315    Logical AND; analogous to:
317    .. code-block:: c
319      (EXPR_A) && (EXPR_B)
321    in C.
323 .. c:macro:: GCC_JIT_BINARY_OP_LOGICAL_OR
325    Logical OR; analogous to:
327    .. code-block:: c
329      (EXPR_A) || (EXPR_B)
331    in C.
333 .. c:macro:: GCC_JIT_BINARY_OP_LSHIFT
335    Left shift; analogous to:
337    .. code-block:: c
339      (EXPR_A) << (EXPR_B)
341    in C.
343 .. c:macro:: GCC_JIT_BINARY_OP_RSHIFT
345    Right shift; analogous to:
347    .. code-block:: c
349      (EXPR_A) >> (EXPR_B)
351    in C.
353 Comparisons
354 ***********
356 .. function:: gcc_jit_rvalue *\
357               gcc_jit_context_new_comparison (gcc_jit_context *ctxt,\
358                                               gcc_jit_location *loc,\
359                                               enum gcc_jit_comparison op,\
360                                               gcc_jit_rvalue *a, gcc_jit_rvalue *b)
362    Build a boolean rvalue out of the comparison of two other rvalues.
364 .. type:: enum gcc_jit_comparison
366 =======================================  ============
367 Comparison                               C equivalent
368 =======================================  ============
369 :c:macro:`GCC_JIT_COMPARISON_EQ`         `x == y`
370 :c:macro:`GCC_JIT_COMPARISON_NE`         `x != y`
371 :c:macro:`GCC_JIT_COMPARISON_LT`         `x < y`
372 :c:macro:`GCC_JIT_COMPARISON_LE`         `x <= y`
373 :c:macro:`GCC_JIT_COMPARISON_GT`         `x > y`
374 :c:macro:`GCC_JIT_COMPARISON_GE`         `x >= y`
375 =======================================  ============
378 Function calls
379 **************
380 .. function:: gcc_jit_rvalue *\
381               gcc_jit_context_new_call (gcc_jit_context *ctxt,\
382                                         gcc_jit_location *loc,\
383                                         gcc_jit_function *func,\
384                                         int numargs , gcc_jit_rvalue **args)
386    Given a function and the given table of argument rvalues, construct a
387    call to the function, with the result as an rvalue.
389    .. note::
391       :c:func:`gcc_jit_context_new_call` merely builds a
392       :c:type:`gcc_jit_rvalue` i.e. an expression that can be evaluated,
393       perhaps as part of a more complicated expression.
394       The call *won't* happen unless you add a statement to a function
395       that evaluates the expression.
397       For example, if you want to call a function and discard the result
398       (or to call a function with ``void`` return type), use
399       :c:func:`gcc_jit_block_add_eval`:
401       .. code-block:: c
403          /* Add "(void)printf (arg0, arg1);".  */
404          gcc_jit_block_add_eval (
405            block, NULL,
406            gcc_jit_context_new_call (
407              ctxt,
408              NULL,
409              printf_func,
410              2, args));
412 .. function:: gcc_jit_rvalue *\
413               gcc_jit_context_new_call_through_ptr (gcc_jit_context *ctxt,\
414                                                     gcc_jit_location *loc,\
415                                                     gcc_jit_rvalue *fn_ptr,\
416                                                     int numargs, \
417                                                     gcc_jit_rvalue **args)
419    Given an rvalue of function pointer type, and the given table of
420    argument rvalues, construct a call to the function pointer, with the
421    result as an rvalue.
423    .. note::
425       The same caveat as for :c:func:`gcc_jit_context_new_call` applies.
427 .. function:: void\
428               gcc_jit_rvalue_set_bool_require_tail_call (gcc_jit_rvalue *call,\
429                                                          int require_tail_call)
431    Given an :c:type:`gcc_jit_rvalue *` for a call created through
432    :c:func:`gcc_jit_context_new_call` or
433    :c:func:`gcc_jit_context_new_call_through_ptr`, mark/clear the
434    call as needing tail-call optimization.  The optimizer will
435    attempt to optimize the call into a jump instruction; if it is
436    unable to do do, an error will be emitted.
438    This may be useful when implementing functions that use the
439    continuation-passing style (e.g. for functional programming
440    languages), in which every function "returns" by calling a
441    "continuation" function pointer.  This call must be
442    guaranteed to be implemented as a jump, otherwise the program
443    could consume an arbitrary amount of stack space as it executed.
445    This entrypoint was added in :ref:`LIBGCCJIT_ABI_6`; you can test for
446    its presence using
448    .. code-block:: c
450       #ifdef LIBGCCJIT_HAVE_gcc_jit_rvalue_set_bool_require_tail_call
452 Type-coercion
453 *************
455 .. function:: gcc_jit_rvalue *\
456               gcc_jit_context_new_cast (gcc_jit_context *ctxt,\
457                                         gcc_jit_location *loc,\
458                                         gcc_jit_rvalue *rvalue,\
459                                         gcc_jit_type *type)
461    Given an rvalue of T, construct another rvalue of another type.
463    Currently only a limited set of conversions are possible:
465      * int <-> float
466      * int <-> bool
467      * P*  <-> Q*, for pointer types P and Q
469 Lvalues
470 -------
472 .. type:: gcc_jit_lvalue
474 An lvalue is something that can of the *left*-hand side of an assignment:
475 a storage area (such as a variable).  It is also usable as an rvalue,
476 where the rvalue is computed by reading from the storage area.
478 .. function:: gcc_jit_object *\
479               gcc_jit_lvalue_as_object (gcc_jit_lvalue *lvalue)
481    Upcast an lvalue to be an object.
483 .. function:: gcc_jit_rvalue *\
484               gcc_jit_lvalue_as_rvalue (gcc_jit_lvalue *lvalue)
486    Upcast an lvalue to be an rvalue.
488 .. function:: gcc_jit_rvalue *\
489               gcc_jit_lvalue_get_address (gcc_jit_lvalue *lvalue,\
490                                           gcc_jit_location *loc)
492    Take the address of an lvalue; analogous to:
494    .. code-block:: c
496      &(EXPR)
498    in C.
500 Global variables
501 ****************
503 .. function:: gcc_jit_lvalue *\
504               gcc_jit_context_new_global (gcc_jit_context *ctxt,\
505                                           gcc_jit_location *loc,\
506                                           enum gcc_jit_global_kind kind,\
507                                           gcc_jit_type *type,\
508                                           const char *name)
510    Add a new global variable of the given type and name to the context.
512    The parameter ``name`` must be non-NULL.  The call takes a copy of the
513    underlying string, so it is valid to pass in a pointer to an on-stack
514    buffer.
516    The "kind" parameter determines the visibility of the "global" outside
517    of the :c:type:`gcc_jit_result`:
519    .. type:: enum gcc_jit_global_kind
521    .. c:macro:: GCC_JIT_GLOBAL_EXPORTED
523       Global is defined by the client code and is visible
524       by name outside of this JIT context via
525       :c:func:`gcc_jit_result_get_global` (and this value is required for
526       the global to be accessible via that entrypoint).
528    .. c:macro:: GCC_JIT_GLOBAL_INTERNAL
530       Global is defined by the client code, but is invisible
531       outside of it.  Analogous to a "static" global within a .c file.
532       Specifically, the variable will only be visible within this
533       context and within child contexts.
535    .. c:macro:: GCC_JIT_GLOBAL_IMPORTED
537       Global is not defined by the client code; we're merely
538       referring to it.  Analogous to using an "extern" global from a
539       header file.
541 Working with pointers, structs and unions
542 -----------------------------------------
544 .. function:: gcc_jit_lvalue *\
545               gcc_jit_rvalue_dereference (gcc_jit_rvalue *rvalue,\
546                                           gcc_jit_location *loc)
548    Given an rvalue of pointer type ``T *``, dereferencing the pointer,
549    getting an lvalue of type ``T``.  Analogous to:
551    .. code-block:: c
553      *(EXPR)
555    in C.
557 Field access is provided separately for both lvalues and rvalues.
559 .. function:: gcc_jit_lvalue *\
560               gcc_jit_lvalue_access_field (gcc_jit_lvalue *struct_,\
561                                            gcc_jit_location *loc,\
562                                            gcc_jit_field *field)
564    Given an lvalue of struct or union type, access the given field,
565    getting an lvalue of the field's type.  Analogous to:
567    .. code-block:: c
569       (EXPR).field = ...;
571    in C.
573 .. function:: gcc_jit_rvalue *\
574               gcc_jit_rvalue_access_field (gcc_jit_rvalue *struct_,\
575                                            gcc_jit_location *loc,\
576                                            gcc_jit_field *field)
578    Given an rvalue of struct or union type, access the given field
579    as an rvalue.  Analogous to:
581    .. code-block:: c
583       (EXPR).field
585    in C.
587 .. function:: gcc_jit_lvalue *\
588               gcc_jit_rvalue_dereference_field (gcc_jit_rvalue *ptr,\
589                                                 gcc_jit_location *loc,\
590                                                 gcc_jit_field *field)
592    Given an rvalue of pointer type ``T *`` where T is of struct or union
593    type, access the given field as an lvalue.  Analogous to:
595    .. code-block:: c
597       (EXPR)->field
599    in C, itself equivalent to ``(*EXPR).FIELD``.
601 .. function:: gcc_jit_lvalue *\
602               gcc_jit_context_new_array_access (gcc_jit_context *ctxt,\
603                                                 gcc_jit_location *loc,\
604                                                 gcc_jit_rvalue *ptr,\
605                                                 gcc_jit_rvalue *index)
607    Given an rvalue of pointer type ``T *``, get at the element `T` at
608    the given index, using standard C array indexing rules i.e. each
609    increment of ``index`` corresponds to ``sizeof(T)`` bytes.
610    Analogous to:
612    .. code-block:: c
614       PTR[INDEX]
616    in C (or, indeed, to ``PTR + INDEX``).