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
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).
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)`
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.
55 .. function:: gccjit::rvalue \
56 gccjit::context::new_rvalue (gccjit::type numeric_type, \
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, \
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:
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:
87 ctxt.new_rvalue (numeric_type, 1)
89 .. function:: gccjit::rvalue \
90 gccjit::context::new_rvalue (gccjit::type numeric_type, \
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, \
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.
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
118 The number of elements needs to match that of the vector type.
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
140 .. function:: gccjit::rvalue \
141 gccjit::context::new_minus (gccjit::type result_type, \
143 gccjit::location loc)
145 Negate an arithmetic value; for example:
149 gccjit::rvalue negpi = ctxt.new_minus (t_double, pi);
151 builds the equivalent of this C expression:
157 .. function:: gccjit::rvalue \
158 new_bitwise_negate (gccjit::type result_type, \
160 gccjit::location loc)
162 Bitwise negation of an integer value (one's complement); for example:
166 gccjit::rvalue mask = ctxt.new_bitwise_negate (t_int, a);
168 builds the equivalent of this C expression:
174 .. function:: gccjit::rvalue \
175 new_logical_negate (gccjit::type result_type, \
177 gccjit::location loc)
179 Logical negation of an arithmetic or pointer value; for example:
183 gccjit::rvalue guard = ctxt.new_logical_negate (t_bool, cond);
185 builds the equivalent of this C expression:
192 The most concise way to spell them is with overloaded operators:
194 .. function:: gccjit::rvalue operator- (gccjit::rvalue a)
198 gccjit::rvalue negpi = -pi;
201 .. function:: gccjit::rvalue operator~ (gccjit::rvalue a)
205 gccjit::rvalue mask = ~a;
207 .. function:: gccjit::rvalue operator! (gccjit::rvalue a)
211 gccjit::rvalue guard = !cond;
217 .. function:: gccjit::rvalue\
218 gccjit::context::new_binary_op (enum gcc_jit_binary_op, \
219 gccjit::type result_type, \
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
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)
291 gccjit::rvalue sum = a + b;
293 .. function:: gccjit::rvalue operator- (gccjit::rvalue a, gccjit::rvalue b)
297 gccjit::rvalue diff = a - b;
299 .. function:: gccjit::rvalue operator* (gccjit::rvalue a, gccjit::rvalue b)
303 gccjit::rvalue prod = a * b;
305 .. function:: gccjit::rvalue operator/ (gccjit::rvalue a, gccjit::rvalue b)
309 gccjit::rvalue result = a / b;
311 .. function:: gccjit::rvalue operator% (gccjit::rvalue a, gccjit::rvalue b)
315 gccjit::rvalue mod = a % b;
317 .. function:: gccjit::rvalue operator& (gccjit::rvalue a, gccjit::rvalue b)
321 gccjit::rvalue x = a & b;
323 .. function:: gccjit::rvalue operator^ (gccjit::rvalue a, gccjit::rvalue b)
327 gccjit::rvalue x = a ^ b;
329 .. function:: gccjit::rvalue operator| (gccjit::rvalue a, gccjit::rvalue b)
333 gccjit::rvalue x = a | b;
335 .. function:: gccjit::rvalue operator&& (gccjit::rvalue a, gccjit::rvalue b)
339 gccjit::rvalue cond = a && b;
341 .. function:: gccjit::rvalue operator|| (gccjit::rvalue a, gccjit::rvalue b)
345 gccjit::rvalue cond = a || b;
347 These can of course be combined, giving a terse way to build compound
352 gccjit::rvalue discriminant = (b * b) - (four * a * c);
358 .. function:: gccjit::rvalue \
359 gccjit::context::new_comparison (enum gcc_jit_comparison,\
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
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)
406 gccjit::rvalue cond = (a == ctxt.zero (t_int));
408 .. function:: gccjit::rvalue \
409 operator!= (gccjit::rvalue a, gccjit::rvalue b)
413 gccjit::rvalue cond = (i != j);
415 .. function:: gccjit::rvalue \
416 operator< (gccjit::rvalue a, gccjit::rvalue b)
420 gccjit::rvalue cond = i < n;
422 .. function:: gccjit::rvalue \
423 operator<= (gccjit::rvalue a, gccjit::rvalue b)
427 gccjit::rvalue cond = i <= n;
429 .. function:: gccjit::rvalue \
430 operator> (gccjit::rvalue a, gccjit::rvalue b)
434 gccjit::rvalue cond = (ch > limit);
436 .. function:: gccjit::rvalue \
437 operator>= (gccjit::rvalue a, gccjit::rvalue b)
441 gccjit::rvalue cond = (score >= ctxt.new_rvalue (t_int, 100));
443 .. TODO: beyond this point
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.
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`:
470 /* Add "(void)printf (arg0, arg1);". */
471 block.add_eval (ctxt.new_call (printf_func, arg0, arg1));
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
485 .. function:: gccjit::rvalue \
486 gccjit::context::new_cast (gccjit::rvalue rvalue,\
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:
496 * P* <-> Q*, for pointer types P and Q
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
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:
521 Parameter "loc" is optional.
526 .. function:: gccjit::lvalue \
527 gccjit::context::new_global (enum gcc_jit_global_kind,\
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:
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* ()
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:
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:
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:
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.
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)
630 gccjit::lvalue element = array[idx];
632 gccjit::lvalue gccjit::rvalue::operator[] (int index)
636 gccjit::lvalue element = array[0];