jit,docs: replace c:type:`int_type` with :expr:`int_type`
[official-gcc.git] / gcc / jit / docs / topics / expressions.rst
blob00e2ec8cfeb3cf2880b37a7a47e2e3e1fe33f9b3
1 .. Copyright (C) 2014-2022 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    <https://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 :expr:`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 :expr:`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 :expr:`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 Constructor expressions
130 ***********************
132    The following functions make constructors for array, struct and union
133    types.
135    The constructor rvalue can be used for assignment to locals.
136    It can be used to initialize global variables with
137    :func:`gcc_jit_global_set_initializer_rvalue`. It can also be used as a
138    temporary value for function calls and return values, but its address
139    can't be taken.
141    Note that arrays in libgccjit do not collapse to pointers like in
142    C. I.e. if an array constructor is used as e.g. a return value, the whole
143    array would be returned by value - array constructors can be assigned to
144    array variables.
146    The constructor can contain nested constructors.
148    Note that a string literal rvalue can't be used to construct a char array;
149    the latter needs one rvalue for each char.
151    These entrypoints were added in :ref:`LIBGCCJIT_ABI_19`; you can test for
152    their presence using:
154    .. code-block:: c
156      #ifdef LIBGCCJIT_HAVE_CTORS
158 .. function:: gcc_jit_rvalue *\
159               gcc_jit_context_new_array_constructor (gcc_jit_context *ctxt,\
160                                                      gcc_jit_location *loc,\
161                                                      gcc_jit_type *type,\
162                                                      size_t num_values,\
163                                                      gcc_jit_rvalue **values)
165    Create a constructor for an array as an rvalue.
167    Returns NULL on error. ``values`` are copied and
168    do not have to outlive the context.
170    ``type`` specifies what the constructor will build and has to be
171    an array.
173    ``num_values`` specifies the number of elements in ``values`` and
174    it can't have more elements than the array type.
176    Each value in ``values`` sets the corresponding value in the array.
177    If the array type itself has more elements than ``values``, the
178    left-over elements will be zeroed.
180    Each value in ``values`` need to be the same unqualified type as the
181    array type's element type.
183    If ``num_values`` is 0, the ``values`` parameter will be
184    ignored and zero initialization will be used.
186    This entrypoint was added in :ref:`LIBGCCJIT_ABI_19`; you can test for its
187    presence using:
189    .. code-block:: c
191      #ifdef LIBGCCJIT_HAVE_CTORS
193 .. function:: gcc_jit_rvalue *\
194               gcc_jit_context_new_struct_constructor (gcc_jit_context *ctxt,\
195                                                       gcc_jit_location *loc,\
196                                                       gcc_jit_type *type,\
197                                                       size_t num_values,\
198                                                       gcc_jit_field **fields,\
199                                                       gcc_jit_rvalue **values)
202    Create a constructor for a struct as an rvalue.
204    Returns NULL on error. The two parameter arrays are copied and
205    do not have to outlive the context.
207    ``type`` specifies what the constructor will build and has to be
208    a struct.
210    ``num_values`` specifies the number of elements in ``values``.
212    ``fields`` need to have the same length as ``values``, or be NULL.
214    If ``fields`` is null, the values are applied in definition order.
216    Otherwise, each field in ``fields`` specifies which field in the struct to
217    set to the corresponding value in ``values``. ``fields`` and ``values``
218    are paired by index.
220    The fields in ``fields`` have to be in definition order, but there
221    can be gaps. Any field in the struct that is not specified in
222    ``fields`` will be zeroed.
224    The fields in ``fields`` need to be the same objects that were used
225    to create the struct.
227    Each value has to have have the same unqualified type as the field
228    it is applied to.
230    A NULL value element  in ``values`` is a shorthand for zero initialization
231    of the corresponding field.
233    If ``num_values`` is 0, the array parameters will be
234    ignored and zero initialization will be used.
236    This entrypoint was added in :ref:`LIBGCCJIT_ABI_19`; you can test for its
237    presence using:
239    .. code-block:: c
241      #ifdef LIBGCCJIT_HAVE_CTORS
243 .. function:: gcc_jit_rvalue *\
244               gcc_jit_context_new_union_constructor (gcc_jit_context *ctxt,\
245                                                      gcc_jit_location *loc,\
246                                                      gcc_jit_type *type,\
247                                                      gcc_jit_field *field,\
248                                                      gcc_jit_rvalue *value)
250    Create a constructor for a union as an rvalue.
252    Returns NULL on error.
254    ``type`` specifies what the constructor will build and has to be
255    an union.
257    ``field`` specifies which field to set. If it is NULL, the first
258    field in the union will be set.``field`` need to be the same object
259    that were used to create the union.
261    ``value`` specifies what value to set the corresponding field to.
262    If ``value`` is NULL, zero initialization will be used.
264    Each value has to have have the same unqualified type as the field
265    it is applied to.
267    This entrypoint was added in :ref:`LIBGCCJIT_ABI_19`; you can test for its
268    presence using:
270    .. code-block:: c
272      #ifdef LIBGCCJIT_HAVE_CTORS
274 Vector expressions
275 ******************
277 .. function:: gcc_jit_rvalue * \
278               gcc_jit_context_new_rvalue_from_vector (gcc_jit_context *ctxt, \
279                                                       gcc_jit_location *loc, \
280                                                       gcc_jit_type *vec_type, \
281                                                       size_t num_elements, \
282                                                       gcc_jit_rvalue **elements)
284    Build a vector rvalue from an array of elements.
286    "vec_type" should be a vector type, created using
287    :func:`gcc_jit_type_get_vector`.
289    "num_elements" should match that of the vector type.
291    This entrypoint was added in :ref:`LIBGCCJIT_ABI_10`; you can test for
292    its presence using
294    .. code-block:: c
296       #ifdef LIBGCCJIT_HAVE_gcc_jit_context_new_rvalue_from_vector
298 Unary Operations
299 ****************
301 .. function:: gcc_jit_rvalue * \
302               gcc_jit_context_new_unary_op (gcc_jit_context *ctxt, \
303                                             gcc_jit_location *loc, \
304                                             enum gcc_jit_unary_op op, \
305                                             gcc_jit_type *result_type, \
306                                             gcc_jit_rvalue *rvalue)
308    Build a unary operation out of an input rvalue.
310    The parameter ``result_type`` must be a numeric type.
312 .. enum:: gcc_jit_unary_op
314 The available unary operations are:
316 ==========================================  ============
317 Unary Operation                             C equivalent
318 ==========================================  ============
319 :c:macro:`GCC_JIT_UNARY_OP_MINUS`           `-(EXPR)`
320 :c:macro:`GCC_JIT_UNARY_OP_BITWISE_NEGATE`  `~(EXPR)`
321 :c:macro:`GCC_JIT_UNARY_OP_LOGICAL_NEGATE`  `!(EXPR)`
322 :c:macro:`GCC_JIT_UNARY_OP_ABS`             `abs (EXPR)`
323 ==========================================  ============
325 .. c:macro:: GCC_JIT_UNARY_OP_MINUS
327     Negate an arithmetic value; analogous to:
329     .. code-block:: c
331        -(EXPR)
333     in C.
335 .. c:macro:: GCC_JIT_UNARY_OP_BITWISE_NEGATE
337     Bitwise negation of an integer value (one's complement); analogous
338     to:
340     .. code-block:: c
342        ~(EXPR)
344     in C.
346 .. c:macro:: GCC_JIT_UNARY_OP_LOGICAL_NEGATE
348     Logical negation of an arithmetic or pointer value; analogous to:
350     .. code-block:: c
352        !(EXPR)
354     in C.
356 .. c:macro:: GCC_JIT_UNARY_OP_ABS
358     Absolute value of an arithmetic expression; analogous to:
360     .. code-block:: c
362         abs (EXPR)
364     in C.
366 Binary Operations
367 *****************
369 .. function:: gcc_jit_rvalue *gcc_jit_context_new_binary_op (gcc_jit_context *ctxt, \
370                                                              gcc_jit_location *loc, \
371                                                              enum gcc_jit_binary_op op, \
372                                                              gcc_jit_type *result_type, \
373                                                              gcc_jit_rvalue *a, gcc_jit_rvalue *b)
375    Build a binary operation out of two constituent rvalues.
377    The parameter ``result_type`` must be a numeric type.
379 .. enum:: gcc_jit_binary_op
381 The available binary operations are:
383 ========================================  ============
384 Binary Operation                          C equivalent
385 ========================================  ============
386 :c:macro:`GCC_JIT_BINARY_OP_PLUS`         `x + y`
387 :c:macro:`GCC_JIT_BINARY_OP_MINUS`        `x - y`
388 :c:macro:`GCC_JIT_BINARY_OP_MULT`         `x * y`
389 :c:macro:`GCC_JIT_BINARY_OP_DIVIDE`       `x / y`
390 :c:macro:`GCC_JIT_BINARY_OP_MODULO`       `x % y`
391 :c:macro:`GCC_JIT_BINARY_OP_BITWISE_AND`  `x & y`
392 :c:macro:`GCC_JIT_BINARY_OP_BITWISE_XOR`  `x ^ y`
393 :c:macro:`GCC_JIT_BINARY_OP_BITWISE_OR`   `x | y`
394 :c:macro:`GCC_JIT_BINARY_OP_LOGICAL_AND`  `x && y`
395 :c:macro:`GCC_JIT_BINARY_OP_LOGICAL_OR`   `x || y`
396 :c:macro:`GCC_JIT_BINARY_OP_LSHIFT`       `x << y`
397 :c:macro:`GCC_JIT_BINARY_OP_RSHIFT`       `x >> y`
398 ========================================  ============
400 .. c:macro:: GCC_JIT_BINARY_OP_PLUS
402    Addition of arithmetic values; analogous to:
404    .. code-block:: c
406      (EXPR_A) + (EXPR_B)
408    in C.
410    For pointer addition, use :c:func:`gcc_jit_context_new_array_access`.
412 .. c:macro:: GCC_JIT_BINARY_OP_MINUS
414    Subtraction of arithmetic values; analogous to:
416    .. code-block:: c
418      (EXPR_A) - (EXPR_B)
420    in C.
422 .. c:macro:: GCC_JIT_BINARY_OP_MULT
424    Multiplication of a pair of arithmetic values; analogous to:
426    .. code-block:: c
428      (EXPR_A) * (EXPR_B)
430    in C.
432 .. c:macro:: GCC_JIT_BINARY_OP_DIVIDE
434    Quotient of division of arithmetic values; analogous to:
436    .. code-block:: c
438      (EXPR_A) / (EXPR_B)
440    in C.
442    The result type affects the kind of division: if the result type is
443    integer-based, then the result is truncated towards zero, whereas
444    a floating-point result type indicates floating-point division.
446 .. c:macro:: GCC_JIT_BINARY_OP_MODULO
448    Remainder of division of arithmetic values; analogous to:
450    .. code-block:: c
452      (EXPR_A) % (EXPR_B)
454    in C.
456 .. c:macro:: GCC_JIT_BINARY_OP_BITWISE_AND
458    Bitwise AND; analogous to:
460    .. code-block:: c
462      (EXPR_A) & (EXPR_B)
464    in C.
466 .. c:macro:: GCC_JIT_BINARY_OP_BITWISE_XOR
468    Bitwise exclusive OR; analogous to:
470    .. code-block:: c
472       (EXPR_A) ^ (EXPR_B)
474    in C.
476 .. c:macro:: GCC_JIT_BINARY_OP_BITWISE_OR
478    Bitwise inclusive OR; analogous to:
480    .. code-block:: c
482      (EXPR_A) | (EXPR_B)
484    in C.
486 .. c:macro:: GCC_JIT_BINARY_OP_LOGICAL_AND
488    Logical AND; analogous to:
490    .. code-block:: c
492      (EXPR_A) && (EXPR_B)
494    in C.
496 .. c:macro:: GCC_JIT_BINARY_OP_LOGICAL_OR
498    Logical OR; analogous to:
500    .. code-block:: c
502      (EXPR_A) || (EXPR_B)
504    in C.
506 .. c:macro:: GCC_JIT_BINARY_OP_LSHIFT
508    Left shift; analogous to:
510    .. code-block:: c
512      (EXPR_A) << (EXPR_B)
514    in C.
516 .. c:macro:: GCC_JIT_BINARY_OP_RSHIFT
518    Right shift; analogous to:
520    .. code-block:: c
522      (EXPR_A) >> (EXPR_B)
524    in C.
526 Comparisons
527 ***********
529 .. function:: gcc_jit_rvalue *\
530               gcc_jit_context_new_comparison (gcc_jit_context *ctxt,\
531                                               gcc_jit_location *loc,\
532                                               enum gcc_jit_comparison op,\
533                                               gcc_jit_rvalue *a, gcc_jit_rvalue *b)
535    Build a boolean rvalue out of the comparison of two other rvalues.
537 .. enum:: gcc_jit_comparison
539 =======================================  ============
540 Comparison                               C equivalent
541 =======================================  ============
542 :c:macro:`GCC_JIT_COMPARISON_EQ`         `x == y`
543 :c:macro:`GCC_JIT_COMPARISON_NE`         `x != y`
544 :c:macro:`GCC_JIT_COMPARISON_LT`         `x < y`
545 :c:macro:`GCC_JIT_COMPARISON_LE`         `x <= y`
546 :c:macro:`GCC_JIT_COMPARISON_GT`         `x > y`
547 :c:macro:`GCC_JIT_COMPARISON_GE`         `x >= y`
548 =======================================  ============
551 Function calls
552 **************
553 .. function:: gcc_jit_rvalue *\
554               gcc_jit_context_new_call (gcc_jit_context *ctxt,\
555                                         gcc_jit_location *loc,\
556                                         gcc_jit_function *func,\
557                                         int numargs , gcc_jit_rvalue **args)
559    Given a function and the given table of argument rvalues, construct a
560    call to the function, with the result as an rvalue.
562    .. note::
564       :c:func:`gcc_jit_context_new_call` merely builds a
565       :c:type:`gcc_jit_rvalue` i.e. an expression that can be evaluated,
566       perhaps as part of a more complicated expression.
567       The call *won't* happen unless you add a statement to a function
568       that evaluates the expression.
570       For example, if you want to call a function and discard the result
571       (or to call a function with ``void`` return type), use
572       :c:func:`gcc_jit_block_add_eval`:
574       .. code-block:: c
576          /* Add "(void)printf (arg0, arg1);".  */
577          gcc_jit_block_add_eval (
578            block, NULL,
579            gcc_jit_context_new_call (
580              ctxt,
581              NULL,
582              printf_func,
583              2, args));
585 .. function:: gcc_jit_rvalue *\
586               gcc_jit_context_new_call_through_ptr (gcc_jit_context *ctxt,\
587                                                     gcc_jit_location *loc,\
588                                                     gcc_jit_rvalue *fn_ptr,\
589                                                     int numargs, \
590                                                     gcc_jit_rvalue **args)
592    Given an rvalue of function pointer type (e.g. from
593    :c:func:`gcc_jit_context_new_function_ptr_type`), and the given table of
594    argument rvalues, construct a call to the function pointer, with the
595    result as an rvalue.
597    .. note::
599       The same caveat as for :c:func:`gcc_jit_context_new_call` applies.
601 .. function:: void\
602               gcc_jit_rvalue_set_bool_require_tail_call (gcc_jit_rvalue *call,\
603                                                          int require_tail_call)
605    Given an :c:type:`gcc_jit_rvalue` for a call created through
606    :c:func:`gcc_jit_context_new_call` or
607    :c:func:`gcc_jit_context_new_call_through_ptr`, mark/clear the
608    call as needing tail-call optimization.  The optimizer will
609    attempt to optimize the call into a jump instruction; if it is
610    unable to do do, an error will be emitted.
612    This may be useful when implementing functions that use the
613    continuation-passing style (e.g. for functional programming
614    languages), in which every function "returns" by calling a
615    "continuation" function pointer.  This call must be
616    guaranteed to be implemented as a jump, otherwise the program
617    could consume an arbitrary amount of stack space as it executed.
619    This entrypoint was added in :ref:`LIBGCCJIT_ABI_6`; you can test for
620    its presence using
622    .. code-block:: c
624       #ifdef LIBGCCJIT_HAVE_gcc_jit_rvalue_set_bool_require_tail_call
626 Function pointers
627 *****************
629 Function pointers can be obtained:
631   * from a :c:type:`gcc_jit_function` using
632     :c:func:`gcc_jit_function_get_address`, or
634   * from an existing function using
635     :c:func:`gcc_jit_context_new_rvalue_from_ptr`,
636     using a function pointer type obtained using
637     :c:func:`gcc_jit_context_new_function_ptr_type`.
639 Type-coercion
640 *************
642 .. function:: gcc_jit_rvalue *\
643               gcc_jit_context_new_cast (gcc_jit_context *ctxt,\
644                                         gcc_jit_location *loc,\
645                                         gcc_jit_rvalue *rvalue,\
646                                         gcc_jit_type *type)
648    Given an rvalue of T, construct another rvalue of another type.
650    Currently only a limited set of conversions are possible:
652      * int <-> float
653      * int <-> bool
654      * P*  <-> Q*, for pointer types P and Q
656 .. function:: gcc_jit_rvalue *\
657               gcc_jit_context_new_bitcast (gcc_jit_context *ctxt,\
658                                            gcc_jit_location *loc,\
659                                            gcc_jit_rvalue *rvalue,\
660                                            gcc_jit_type *type)
662    Given an rvalue of T, bitcast it to another type, meaning that this will
663    generate a new rvalue by interpreting the bits of ``rvalue`` to the layout
664    of ``type``.
666    The type of rvalue must be the same size as the size of ``type``.
668    This entrypoint was added in :ref:`LIBGCCJIT_ABI_21`; you can test for
669    its presence using
671    .. code-block:: c
673       #ifdef LIBGCCJIT_HAVE_gcc_jit_context_new_bitcast
675 Lvalues
676 -------
678 .. type:: gcc_jit_lvalue
680 An lvalue is something that can of the *left*-hand side of an assignment:
681 a storage area (such as a variable).  It is also usable as an rvalue,
682 where the rvalue is computed by reading from the storage area.
684 .. function:: gcc_jit_object *\
685               gcc_jit_lvalue_as_object (gcc_jit_lvalue *lvalue)
687    Upcast an lvalue to be an object.
689 .. function:: gcc_jit_rvalue *\
690               gcc_jit_lvalue_as_rvalue (gcc_jit_lvalue *lvalue)
692    Upcast an lvalue to be an rvalue.
694 .. function:: gcc_jit_rvalue *\
695               gcc_jit_lvalue_get_address (gcc_jit_lvalue *lvalue,\
696                                           gcc_jit_location *loc)
698    Take the address of an lvalue; analogous to:
700    .. code-block:: c
702      &(EXPR)
704    in C.
706 .. function:: void\
707               gcc_jit_lvalue_set_tls_model (gcc_jit_lvalue *lvalue,\
708                                             enum gcc_jit_tls_model model)
710    Make a variable a thread-local variable.
712    The "model" parameter determines the thread-local storage model of the "lvalue":
714    .. enum:: gcc_jit_tls_model
716    .. c:macro:: GCC_JIT_TLS_MODEL_NONE
718       Don't set the TLS model.
720    .. c:macro:: GCC_JIT_TLS_MODEL_GLOBAL_DYNAMIC
722    .. c:macro:: GCC_JIT_TLS_MODEL_LOCAL_DYNAMIC
724    .. c:macro:: GCC_JIT_TLS_MODEL_INITIAL_EXEC
726    .. c:macro:: GCC_JIT_TLS_MODEL_LOCAL_EXEC
728    This is analogous to:
730    .. code-block:: c
732      _Thread_local int foo __attribute__ ((tls_model("MODEL")));
734    in C.
736    This entrypoint was added in :ref:`LIBGCCJIT_ABI_17`; you can test for
737    its presence using
739    .. code-block:: c
741       #ifdef LIBGCCJIT_HAVE_gcc_jit_lvalue_set_tls_model
743 .. function:: void\
744               gcc_jit_lvalue_set_link_section (gcc_jit_lvalue *lvalue,\
745                                                const char *section_name)
747    Set the link section of a variable.
748    The parameter ``section_name`` must be non-NULL and must contain the
749    leading dot. Analogous to:
751    .. code-block:: c
753      int variable __attribute__((section(".section")));
755    in C.
757    This entrypoint was added in :ref:`LIBGCCJIT_ABI_18`; you can test for
758    its presence using
760    .. code-block:: c
762       #ifdef LIBGCCJIT_HAVE_gcc_jit_lvalue_set_link_section
764 .. function:: void\
765               gcc_jit_lvalue_set_register_name (gcc_jit_lvalue *lvalue,\
766                                                 const char *reg_name);
768    Set the register name of a variable.
769    The parameter ``reg_name`` must be non-NULL. Analogous to:
771    .. code-block:: c
773      register int variable asm ("r12");
775    in C.
777    This entrypoint was added in :ref:`LIBGCCJIT_ABI_22`; you can test for
778    its presence using
780    .. code-block:: c
782       #ifdef LIBGCCJIT_HAVE_gcc_jit_lvalue_set_register_name
784 .. function:: void\
785               gcc_jit_lvalue_set_alignment (gcc_jit_lvalue *lvalue,\
786                                             unsigned bytes)
788    Set the alignment of a variable, in bytes.
789    Analogous to:
791    .. code-block:: c
793      int variable __attribute__((aligned (16)));
795    in C.
797    This entrypoint was added in :ref:`LIBGCCJIT_ABI_24`; you can test for
798    its presence using
800    .. code-block:: c
802       #ifdef LIBGCCJIT_HAVE_ALIGNMENT
804 .. function:: unsigned\
805               gcc_jit_lvalue_get_alignment (gcc_jit_lvalue *lvalue)
807    Return the alignment of a variable set by ``gcc_jit_lvalue_set_alignment``.
808    Return 0 if the alignment was not set. Analogous to:
810    .. code-block:: c
812      _Alignof (variable)
814    in C.
816    This entrypoint was added in :ref:`LIBGCCJIT_ABI_24`; you can test for
817    its presence using
819    .. code-block:: c
821       #ifdef LIBGCCJIT_HAVE_ALIGNMENT
823 Global variables
824 ****************
826 .. function:: gcc_jit_lvalue *\
827               gcc_jit_context_new_global (gcc_jit_context *ctxt,\
828                                           gcc_jit_location *loc,\
829                                           enum gcc_jit_global_kind kind,\
830                                           gcc_jit_type *type,\
831                                           const char *name)
833    Add a new global variable of the given type and name to the context.
835    The parameter ``type`` must be non-`void`.
837    The parameter ``name`` must be non-NULL.  The call takes a copy of the
838    underlying string, so it is valid to pass in a pointer to an on-stack
839    buffer.
841    The "kind" parameter determines the visibility of the "global" outside
842    of the :c:type:`gcc_jit_result`:
844    .. enum:: gcc_jit_global_kind
846    .. c:macro:: GCC_JIT_GLOBAL_EXPORTED
848       Global is defined by the client code and is visible
849       by name outside of this JIT context via
850       :c:func:`gcc_jit_result_get_global` (and this value is required for
851       the global to be accessible via that entrypoint).
853    .. c:macro:: GCC_JIT_GLOBAL_INTERNAL
855       Global is defined by the client code, but is invisible
856       outside of it.  Analogous to a "static" global within a .c file.
857       Specifically, the variable will only be visible within this
858       context and within child contexts.
860    .. c:macro:: GCC_JIT_GLOBAL_IMPORTED
862       Global is not defined by the client code; we're merely
863       referring to it.  Analogous to using an "extern" global from a
864       header file.
866 .. function:: gcc_jit_lvalue *\
867               gcc_jit_global_set_initializer (gcc_jit_lvalue *global,\
868                                               const void *blob,\
869                                               size_t num_bytes)
871    Set an initializer for ``global`` using the memory content pointed
872    by ``blob`` for ``num_bytes``.  ``global`` must be an array of an
873    integral type.  Return the global itself.
875    The parameter ``blob`` must be non-NULL. The call copies the memory
876    pointed by ``blob`` for ``num_bytes`` bytes, so it is valid to pass
877    in a pointer to an on-stack buffer.  The content will be stored in
878    the compilation unit and used as initialization value of the array.
880    This entrypoint was added in :ref:`LIBGCCJIT_ABI_14`; you can test for
881    its presence using
883    .. code-block:: c
885       #ifdef LIBGCCJIT_HAVE_gcc_jit_global_set_initializer
887 .. function:: gcc_jit_lvalue *\
888               gcc_jit_global_set_initializer_rvalue (gcc_jit_lvalue *global,\
889                                                      gcc_jit_rvalue *init_value)
891    Set the initial value of a global with an rvalue.
893    The rvalue needs to be a constant expression, e.g. no function calls.
895    The global can't have the ``kind`` :c:macro:`GCC_JIT_GLOBAL_IMPORTED`.
897    As a non-comprehensive example it is OK to do the equivalent of:
899    .. code-block:: c
901        int foo = 3 * 2; /* rvalue from gcc_jit_context_new_binary_op.  */
902        int arr[] = {1,2,3,4}; /* rvalue from gcc_jit_context_new_constructor.  */
903        int *bar = &arr[2] + 1; /* rvalue from nested "get address" of "array access".  */
904        const int baz = 3; /* rvalue from gcc_jit_context_rvalue_from_int.  */
905        int boz = baz; /* rvalue from gcc_jit_lvalue_as_rvalue.  */
907    Use together with :c:func:`gcc_jit_context_new_struct_constructor`,
908    :c:func:`gcc_jit_context_new_union_constructor`, :c:func:`gcc_jit_context_new_array_constructor`
909    to initialize structs, unions and arrays.
911    On success, returns the ``global`` parameter unchanged. Otherwise, ``NULL``.
913    This entrypoint was added in :ref:`LIBGCCJIT_ABI_19`; you can test for its
914    presence using:
916    .. code-block:: c
918       #ifdef LIBGCCJIT_HAVE_CTORS
920 Working with pointers, structs and unions
921 -----------------------------------------
923 .. function:: gcc_jit_lvalue *\
924               gcc_jit_rvalue_dereference (gcc_jit_rvalue *rvalue,\
925                                           gcc_jit_location *loc)
927    Given an rvalue of pointer type ``T *``, dereferencing the pointer,
928    getting an lvalue of type ``T``.  Analogous to:
930    .. code-block:: c
932      *(EXPR)
934    in C.
936 Field access is provided separately for both lvalues and rvalues.
938 .. function:: gcc_jit_lvalue *\
939               gcc_jit_lvalue_access_field (gcc_jit_lvalue *struct_,\
940                                            gcc_jit_location *loc,\
941                                            gcc_jit_field *field)
943    Given an lvalue of struct or union type, access the given field,
944    getting an lvalue of the field's type.  Analogous to:
946    .. code-block:: c
948       (EXPR).field = ...;
950    in C.
952 .. function:: gcc_jit_rvalue *\
953               gcc_jit_rvalue_access_field (gcc_jit_rvalue *struct_,\
954                                            gcc_jit_location *loc,\
955                                            gcc_jit_field *field)
957    Given an rvalue of struct or union type, access the given field
958    as an rvalue.  Analogous to:
960    .. code-block:: c
962       (EXPR).field
964    in C.
966 .. function:: gcc_jit_lvalue *\
967               gcc_jit_rvalue_dereference_field (gcc_jit_rvalue *ptr,\
968                                                 gcc_jit_location *loc,\
969                                                 gcc_jit_field *field)
971    Given an rvalue of pointer type ``T *`` where T is of struct or union
972    type, access the given field as an lvalue.  Analogous to:
974    .. code-block:: c
976       (EXPR)->field
978    in C, itself equivalent to ``(*EXPR).FIELD``.
980 .. function:: gcc_jit_lvalue *\
981               gcc_jit_context_new_array_access (gcc_jit_context *ctxt,\
982                                                 gcc_jit_location *loc,\
983                                                 gcc_jit_rvalue *ptr,\
984                                                 gcc_jit_rvalue *index)
986    Given an rvalue of pointer type ``T *``, get at the element `T` at
987    the given index, using standard C array indexing rules i.e. each
988    increment of ``index`` corresponds to ``sizeof(T)`` bytes.
989    Analogous to:
991    .. code-block:: c
993       PTR[INDEX]
995    in C (or, indeed, to ``PTR + INDEX``).