PR rtl-optimization/82913
[official-gcc.git] / gcc / jit / docs / cp / topics / expressions.rst
blobb0081f68fc176e7b1f952b4cfecadebb0358252d
1 .. Copyright (C) 2014-2017 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:: cpp
20 Expressions
21 ===========
23 Rvalues
24 -------
25 .. class:: gccjit::rvalue
27 A :class:`gccjit::rvalue` is an expression that can be computed.  It is a
28 subclass of :class:`gccjit::object`, and is a thin wrapper around
29 :c:type:`gcc_jit_rvalue *` from the C API.
31 It can be simple, e.g.:
33   * an integer value e.g. `0` or `42`
34   * a string literal e.g. `"Hello world"`
35   * a variable e.g. `i`.  These are also lvalues (see below).
37 or compound e.g.:
39   * a unary expression e.g. `!cond`
40   * a binary expression e.g. `(a + b)`
41   * a function call e.g. `get_distance (&player_ship, &target)`
42   * etc.
44 Every rvalue has an associated type, and the API will check to ensure
45 that types match up correctly (otherwise the context will emit an error).
47 .. function:: gccjit::type gccjit::rvalue::get_type ()
49   Get the type of this rvalue.
52 Simple expressions
53 ******************
55 .. function:: gccjit::rvalue \
56               gccjit::context::new_rvalue (gccjit::type numeric_type, \
57                                            int value) const
59    Given a numeric type (integer or floating point), build an rvalue for
60    the given constant :c:type:`int` value.
62 .. function:: gccjit::rvalue \
63               gccjit::context::new_rvalue (gccjit::type numeric_type, \
64                                            long value) const
66    Given a numeric type (integer or floating point), build an rvalue for
67    the given constant :c:type:`long` value.
69 .. function::  gccjit::rvalue \
70                gccjit::context::zero (gccjit::type numeric_type) const
72    Given a numeric type (integer or floating point), get the rvalue for
73    zero.  Essentially this is just a shortcut for:
75    .. code-block:: c++
77       ctxt.new_rvalue (numeric_type, 0)
79 .. function::  gccjit::rvalue \
80                gccjit::context::one (gccjit::type numeric_type) const
82    Given a numeric type (integer or floating point), get the rvalue for
83    one.  Essentially this is just a shortcut for:
85    .. code-block:: c++
87       ctxt.new_rvalue (numeric_type, 1)
89 .. function::  gccjit::rvalue \
90                gccjit::context::new_rvalue (gccjit::type numeric_type, \
91                                             double value) const
93    Given a numeric type (integer or floating point), build an rvalue for
94    the given constant :c:type:`double` value.
96 .. function:: gccjit::rvalue \
97               gccjit::context::new_rvalue (gccjit::type pointer_type, \
98                                            void *value) const
100    Given a pointer type, build an rvalue for the given address.
102 .. function:: gccjit::rvalue \
103               gccjit::context::new_rvalue (const std::string &value) const
105    Generate an rvalue of type :c:data:`GCC_JIT_TYPE_CONST_CHAR_PTR` for
106    the given string.  This is akin to a string literal.
108 Vector expressions
109 ******************
111 .. function:: gccjit::rvalue \
112               gccjit::context::new_rvalue (gccjit::type vector_type, \
113                                            std::vector<gccjit::rvalue> elements) const
115    Given a vector type, and a vector of scalar rvalue elements, generate a
116    vector rvalue.
118    The number of elements needs to match that of the vector type.
120 Unary Operations
121 ****************
123 .. function:: gccjit::rvalue  \
124               gccjit::context::new_unary_op (enum gcc_jit_unary_op, \
125                                              gccjit::type result_type, \
126                                              gccjit::rvalue rvalue, \
127                                              gccjit::location loc)
129    Build a unary operation out of an input rvalue.
131    Parameter ``loc`` is optional.
133    This is a thin wrapper around the C API's
134    :c:func:`gcc_jit_context_new_unary_op` and the available unary
135    operations are documented there.
137 There are shorter ways to spell the various specific kinds of unary
138 operation:
140 .. function:: gccjit::rvalue \
141               gccjit::context::new_minus (gccjit::type result_type, \
142                                           gccjit::rvalue a, \
143                                           gccjit::location loc)
145    Negate an arithmetic value; for example:
147    .. code-block:: c++
149       gccjit::rvalue negpi = ctxt.new_minus (t_double, pi);
151    builds the equivalent of this C expression:
153    .. code-block:: c
155       -pi
157 .. function:: gccjit::rvalue \
158               new_bitwise_negate (gccjit::type result_type, \
159                                   gccjit::rvalue a, \
160                                   gccjit::location loc)
162    Bitwise negation of an integer value (one's complement); for example:
164    .. code-block:: c++
166       gccjit::rvalue mask = ctxt.new_bitwise_negate (t_int, a);
168    builds the equivalent of this C expression:
170    .. code-block:: c
172       ~a
174 .. function:: gccjit::rvalue \
175               new_logical_negate (gccjit::type result_type, \
176                                   gccjit::rvalue a, \
177                                   gccjit::location loc)
179    Logical negation of an arithmetic or pointer value; for example:
181    .. code-block:: c++
183       gccjit::rvalue guard = ctxt.new_logical_negate (t_bool, cond);
185    builds the equivalent of this C expression:
187    .. code-block:: c
189       !cond
192 The most concise way to spell them is with overloaded operators:
194 .. function:: gccjit::rvalue operator- (gccjit::rvalue a)
196    .. code-block:: c++
198      gccjit::rvalue negpi = -pi;
201 .. function:: gccjit::rvalue operator~ (gccjit::rvalue a)
203    .. code-block:: c++
205       gccjit::rvalue mask = ~a;
207 .. function:: gccjit::rvalue operator! (gccjit::rvalue a)
209    .. code-block:: c++
211       gccjit::rvalue guard = !cond;
214 Binary Operations
215 *****************
217 .. function:: gccjit::rvalue\
218               gccjit::context::new_binary_op (enum gcc_jit_binary_op, \
219                                               gccjit::type result_type, \
220                                               gccjit::rvalue a, \
221                                               gccjit::rvalue b, \
222                                               gccjit::location loc)
224    Build a binary operation out of two constituent rvalues.
226    Parameter ``loc`` is optional.
228    This is a thin wrapper around the C API's
229    :c:func:`gcc_jit_context_new_binary_op` and the available binary
230    operations are documented there.
232 There are shorter ways to spell the various specific kinds of binary
233 operation:
235 .. function:: gccjit::rvalue \
236               gccjit::context::new_plus (gccjit::type result_type, \
237                                          gccjit::rvalue a, gccjit::rvalue b, \
238                                          gccjit::location loc)
240 .. function:: gccjit::rvalue \
241               gccjit::context::new_minus (gccjit::type result_type, \
242                                           gccjit::rvalue a, gccjit::rvalue b, \
243                                           gccjit::location loc)
245 .. function:: gccjit::rvalue \
246               gccjit::context::new_mult (gccjit::type result_type, \
247                                          gccjit::rvalue a, gccjit::rvalue b, \
248                                          gccjit::location loc)
250 .. function:: gccjit::rvalue \
251               gccjit::context::new_divide (gccjit::type result_type, \
252                                            gccjit::rvalue a, gccjit::rvalue b, \
253                                            gccjit::location loc)
255 .. function:: gccjit::rvalue \
256               gccjit::context::new_modulo (gccjit::type result_type, \
257                                            gccjit::rvalue a, gccjit::rvalue b, \
258                                            gccjit::location loc)
260 .. function:: gccjit::rvalue \
261               gccjit::context::new_bitwise_and (gccjit::type result_type, \
262                                                 gccjit::rvalue a, gccjit::rvalue b, \
263                                                 gccjit::location loc)
265 .. function:: gccjit::rvalue \
266               gccjit::context::new_bitwise_xor (gccjit::type result_type, \
267                                                 gccjit::rvalue a, gccjit::rvalue b, \
268                                                 gccjit::location loc)
270 .. function:: gccjit::rvalue \
271               gccjit::context::new_bitwise_or (gccjit::type result_type, \
272                                                gccjit::rvalue a, gccjit::rvalue b, \
273                                                gccjit::location loc)
275 .. function:: gccjit::rvalue \
276               gccjit::context::new_logical_and (gccjit::type result_type, \
277                                                 gccjit::rvalue a, gccjit::rvalue b, \
278                                                 gccjit::location loc)
280 .. function:: gccjit::rvalue \
281               gccjit::context::new_logical_or (gccjit::type result_type, \
282                                                gccjit::rvalue a, gccjit::rvalue b, \
283                                                gccjit::location loc)
285 The most concise way to spell them is with overloaded operators:
287 .. function:: gccjit::rvalue operator+ (gccjit::rvalue a, gccjit::rvalue b)
289    .. code-block:: c++
291       gccjit::rvalue sum = a + b;
293 .. function:: gccjit::rvalue operator- (gccjit::rvalue a, gccjit::rvalue b)
295    .. code-block:: c++
297       gccjit::rvalue diff = a - b;
299 .. function:: gccjit::rvalue operator* (gccjit::rvalue a, gccjit::rvalue b)
301    .. code-block:: c++
303       gccjit::rvalue prod = a * b;
305 .. function:: gccjit::rvalue operator/ (gccjit::rvalue a, gccjit::rvalue b)
307    .. code-block:: c++
309       gccjit::rvalue result = a / b;
311 .. function:: gccjit::rvalue operator% (gccjit::rvalue a, gccjit::rvalue b)
313    .. code-block:: c++
315       gccjit::rvalue mod = a % b;
317 .. function:: gccjit::rvalue operator& (gccjit::rvalue a, gccjit::rvalue b)
319    .. code-block:: c++
321       gccjit::rvalue x = a & b;
323 .. function:: gccjit::rvalue operator^ (gccjit::rvalue a, gccjit::rvalue b)
325    .. code-block:: c++
327       gccjit::rvalue x = a ^ b;
329 .. function:: gccjit::rvalue operator| (gccjit::rvalue a, gccjit::rvalue b)
331    .. code-block:: c++
333       gccjit::rvalue x = a | b;
335 .. function:: gccjit::rvalue operator&& (gccjit::rvalue a, gccjit::rvalue b)
337    .. code-block:: c++
339       gccjit::rvalue cond = a && b;
341 .. function:: gccjit::rvalue operator|| (gccjit::rvalue a, gccjit::rvalue b)
343    .. code-block:: c++
345       gccjit::rvalue cond = a || b;
347 These can of course be combined, giving a terse way to build compound
348 expressions:
350    .. code-block:: c++
352       gccjit::rvalue discriminant = (b * b) - (four * a * c);
355 Comparisons
356 ***********
358 .. function:: gccjit::rvalue \
359               gccjit::context::new_comparison (enum gcc_jit_comparison,\
360                                                gccjit::rvalue a, \
361                                                gccjit::rvalue b, \
362                                                gccjit::location loc)
364    Build a boolean rvalue out of the comparison of two other rvalues.
366    Parameter ``loc`` is optional.
368    This is a thin wrapper around the C API's
369    :c:func:`gcc_jit_context_new_comparison` and the available kinds
370    of comparison are documented there.
372 There are shorter ways to spell the various specific kinds of binary
373 operation:
375 .. function:: gccjit::rvalue \
376               gccjit::context::new_eq (gccjit::rvalue a, gccjit::rvalue b, \
377                                        gccjit::location loc)
379 .. function:: gccjit::rvalue \
380               gccjit::context::new_ne (gccjit::rvalue a, gccjit::rvalue b, \
381                                        gccjit::location loc)
383 .. function:: gccjit::rvalue \
384               gccjit::context::new_lt (gccjit::rvalue a, gccjit::rvalue b, \
385                                        gccjit::location loc)
387 .. function:: gccjit::rvalue \
388               gccjit::context::new_le (gccjit::rvalue a, gccjit::rvalue b, \
389                                        gccjit::location loc)
391 .. function:: gccjit::rvalue \
392               gccjit::context::new_gt (gccjit::rvalue a, gccjit::rvalue b, \
393                                        gccjit::location loc)
395 .. function:: gccjit::rvalue \
396               gccjit::context::new_ge (gccjit::rvalue a, gccjit::rvalue b, \
397                                        gccjit::location loc)
399 The most concise way to spell them is with overloaded operators:
401 .. function:: gccjit::rvalue \
402               operator== (gccjit::rvalue a, gccjit::rvalue b)
404    .. code-block:: c++
406       gccjit::rvalue cond = (a == ctxt.zero (t_int));
408 .. function:: gccjit::rvalue \
409               operator!= (gccjit::rvalue a, gccjit::rvalue b)
411    .. code-block:: c++
413       gccjit::rvalue cond = (i != j);
415 .. function:: gccjit::rvalue \
416               operator< (gccjit::rvalue a, gccjit::rvalue b)
418    .. code-block:: c++
420       gccjit::rvalue cond = i < n;
422 .. function:: gccjit::rvalue \
423               operator<= (gccjit::rvalue a, gccjit::rvalue b)
425    .. code-block:: c++
427       gccjit::rvalue cond = i <= n;
429 .. function:: gccjit::rvalue \
430               operator> (gccjit::rvalue a, gccjit::rvalue b)
432    .. code-block:: c++
434       gccjit::rvalue cond = (ch > limit);
436 .. function:: gccjit::rvalue \
437               operator>= (gccjit::rvalue a, gccjit::rvalue b)
439    .. code-block:: c++
441       gccjit::rvalue cond = (score >= ctxt.new_rvalue (t_int, 100));
443 .. TODO: beyond this point
445 Function calls
446 **************
447 .. function:: gcc_jit_rvalue *\
448               gcc_jit_context_new_call (gcc_jit_context *ctxt,\
449                                         gcc_jit_location *loc,\
450                                         gcc_jit_function *func,\
451                                         int numargs , gcc_jit_rvalue **args)
453    Given a function and the given table of argument rvalues, construct a
454    call to the function, with the result as an rvalue.
456    .. note::
458       :func:`gccjit::context::new_call` merely builds a
459       :class:`gccjit::rvalue` i.e. an expression that can be evaluated,
460       perhaps as part of a more complicated expression.
461       The call *won't* happen unless you add a statement to a function
462       that evaluates the expression.
464       For example, if you want to call a function and discard the result
465       (or to call a function with ``void`` return type), use
466       :func:`gccjit::block::add_eval`:
468       .. code-block:: c++
470          /* Add "(void)printf (arg0, arg1);".  */
471          block.add_eval (ctxt.new_call (printf_func, arg0, arg1));
473 Function pointers
474 *****************
476 .. function:: gccjit::rvalue \
477               gccjit::function::get_address (gccjit::location loc)
479    Get the address of a function as an rvalue, of function pointer
480    type.
482 Type-coercion
483 *************
485 .. function:: gccjit::rvalue \
486               gccjit::context::new_cast (gccjit::rvalue rvalue,\
487                                          gccjit::type type, \
488                                          gccjit::location loc)
490    Given an rvalue of T, construct another rvalue of another type.
492    Currently only a limited set of conversions are possible:
494      * int <-> float
495      * int <-> bool
496      * P*  <-> Q*, for pointer types P and Q
498 Lvalues
499 -------
501 .. class:: gccjit::lvalue
503 An lvalue is something that can of the *left*-hand side of an assignment:
504 a storage area (such as a variable).  It is a subclass of
505 :class:`gccjit::rvalue`, where the rvalue is computed by reading from the
506 storage area.
508 It iss a thin wrapper around :c:type:`gcc_jit_lvalue *` from the C API.
510 .. function:: gccjit::rvalue \
511               gccjit::lvalue::get_address (gccjit::location loc)
513    Take the address of an lvalue; analogous to:
515    .. code-block:: c
517      &(EXPR)
519    in C.
521    Parameter "loc" is optional.
523 Global variables
524 ****************
526 .. function:: gccjit::lvalue \
527               gccjit::context::new_global (enum gcc_jit_global_kind,\
528                                            gccjit::type type, \
529                                            const char *name, \
530                                            gccjit::location loc)
532    Add a new global variable of the given type and name to the context.
534    This is a thin wrapper around :c:func:`gcc_jit_context_new_global` from
535    the C API; the "kind" parameter has the same meaning as there.
537 Working with pointers, structs and unions
538 -----------------------------------------
540 .. function:: gccjit::lvalue \
541               gccjit::rvalue::dereference (gccjit::location loc)
543    Given an rvalue of pointer type ``T *``, dereferencing the pointer,
544    getting an lvalue of type ``T``.  Analogous to:
546    .. code-block:: c++
548      *(EXPR)
550    in C.
552    Parameter "loc" is optional.
554 If you don't need to specify the location, this can also be expressed using
555 an overloaded operator:
557 .. function:: gccjit::lvalue \
558               gccjit::rvalue::operator* ()
560    .. code-block:: c++
562       gccjit::lvalue content = *ptr;
564 Field access is provided separately for both lvalues and rvalues:
566 .. function:: gccjit::lvalue \
567               gccjit::lvalue::access_field (gccjit::field field, \
568                                             gccjit::location loc)
570    Given an lvalue of struct or union type, access the given field,
571    getting an lvalue of the field's type.  Analogous to:
573    .. code-block:: c++
575       (EXPR).field = ...;
577    in C.
579 .. function:: gccjit::rvalue \
580               gccjit::rvalue::access_field (gccjit::field field, \
581                                             gccjit::location loc)
583    Given an rvalue of struct or union type, access the given field
584    as an rvalue.  Analogous to:
586    .. code-block:: c++
588       (EXPR).field
590    in C.
592 .. function:: gccjit::lvalue \
593               gccjit::rvalue::dereference_field (gccjit::field field, \
594                                                  gccjit::location loc)
596    Given an rvalue of pointer type ``T *`` where T is of struct or union
597    type, access the given field as an lvalue.  Analogous to:
599    .. code-block:: c++
601       (EXPR)->field
603    in C, itself equivalent to ``(*EXPR).FIELD``.
605 .. function:: gccjit::lvalue \
606               gccjit::context::new_array_access (gccjit::rvalue ptr, \
607                                                  gccjit::rvalue index, \
608                                                  gccjit::location loc)
610    Given an rvalue of pointer type ``T *``, get at the element `T` at
611    the given index, using standard C array indexing rules i.e. each
612    increment of ``index`` corresponds to ``sizeof(T)`` bytes.
613    Analogous to:
615    .. code-block:: c++
617       PTR[INDEX]
619    in C (or, indeed, to ``PTR + INDEX``).
621    Parameter "loc" is optional.
623 For array accesses where you don't need to specify a :class:`gccjit::location`,
624 two overloaded operators are available:
626     gccjit::lvalue gccjit::rvalue::operator[] (gccjit::rvalue index)
628     .. code-block:: c++
630        gccjit::lvalue element = array[idx];
632     gccjit::lvalue gccjit::rvalue::operator[] (int index)
634     .. code-block:: c++
636        gccjit::lvalue element = array[0];