1 .. Copyright (C) 2014 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 zero. 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.
112 .. function:: gccjit::rvalue \
113 gccjit::context::new_unary_op (enum gcc_jit_unary_op, \
114 gccjit::type result_type, \
115 gccjit::rvalue rvalue, \
116 gccjit::location loc)
118 Build a unary operation out of an input rvalue.
120 Parameter ``loc`` is optional.
122 This is a thin wrapper around the C API's
123 :c:func:`gcc_jit_context_new_unary_op` and the available unary
124 operations are documented there.
126 There are shorter ways to spell the various specific kinds of unary
129 .. function:: gccjit::rvalue \
130 gccjit::context::new_minus (gccjit::type result_type, \
132 gccjit::location loc)
134 Negate an arithmetic value; for example:
138 gccjit::rvalue negpi = ctxt.new_minus (t_double, pi);
140 builds the equivalent of this C expression:
146 .. function:: gccjit::rvalue \
147 new_bitwise_negate (gccjit::type result_type, \
149 gccjit::location loc)
151 Bitwise negation of an integer value (one's complement); for example:
155 gccjit::rvalue mask = ctxt.new_bitwise_negate (t_int, a);
157 builds the equivalent of this C expression:
163 .. function:: gccjit::rvalue \
164 new_logical_negate (gccjit::type result_type, \
166 gccjit::location loc)
168 Logical negation of an arithmetic or pointer value; for example:
172 gccjit::rvalue guard = ctxt.new_logical_negate (t_bool, cond);
174 builds the equivalent of this C expression:
181 The most concise way to spell them is with overloaded operators:
183 .. function:: gccjit::rvalue operator- (gccjit::rvalue a)
187 gccjit::rvalue negpi = -pi;
190 .. function:: gccjit::rvalue operator~ (gccjit::rvalue a)
194 gccjit::rvalue mask = ~a;
196 .. function:: gccjit::rvalue operator! (gccjit::rvalue a)
200 gccjit::rvalue guard = !cond;
206 .. function:: gccjit::rvalue\
207 gccjit::context::new_binary_op (enum gcc_jit_binary_op, \
208 gccjit::type result_type, \
211 gccjit::location loc)
213 Build a binary operation out of two constituent rvalues.
215 Parameter ``loc`` is optional.
217 This is a thin wrapper around the C API's
218 :c:func:`gcc_jit_context_new_binary_op` and the available binary
219 operations are documented there.
221 There are shorter ways to spell the various specific kinds of binary
224 .. function:: gccjit::rvalue \
225 gccjit::context::new_plus (gccjit::type result_type, \
226 gccjit::rvalue a, gccjit::rvalue b, \
227 gccjit::location loc)
229 .. function:: gccjit::rvalue \
230 gccjit::context::new_minus (gccjit::type result_type, \
231 gccjit::rvalue a, gccjit::rvalue b, \
232 gccjit::location loc)
234 .. function:: gccjit::rvalue \
235 gccjit::context::new_mult (gccjit::type result_type, \
236 gccjit::rvalue a, gccjit::rvalue b, \
237 gccjit::location loc)
239 .. function:: gccjit::rvalue \
240 gccjit::context::new_divide (gccjit::type result_type, \
241 gccjit::rvalue a, gccjit::rvalue b, \
242 gccjit::location loc)
244 .. function:: gccjit::rvalue \
245 gccjit::context::new_modulo (gccjit::type result_type, \
246 gccjit::rvalue a, gccjit::rvalue b, \
247 gccjit::location loc)
249 .. function:: gccjit::rvalue \
250 gccjit::context::new_bitwise_and (gccjit::type result_type, \
251 gccjit::rvalue a, gccjit::rvalue b, \
252 gccjit::location loc)
254 .. function:: gccjit::rvalue \
255 gccjit::context::new_bitwise_xor (gccjit::type result_type, \
256 gccjit::rvalue a, gccjit::rvalue b, \
257 gccjit::location loc)
259 .. function:: gccjit::rvalue \
260 gccjit::context::new_bitwise_or (gccjit::type result_type, \
261 gccjit::rvalue a, gccjit::rvalue b, \
262 gccjit::location loc)
264 .. function:: gccjit::rvalue \
265 gccjit::context::new_logical_and (gccjit::type result_type, \
266 gccjit::rvalue a, gccjit::rvalue b, \
267 gccjit::location loc)
269 .. function:: gccjit::rvalue \
270 gccjit::context::new_logical_or (gccjit::type result_type, \
271 gccjit::rvalue a, gccjit::rvalue b, \
272 gccjit::location loc)
274 The most concise way to spell them is with overloaded operators:
276 .. function:: gccjit::rvalue operator+ (gccjit::rvalue a, gccjit::rvalue b)
280 gccjit::rvalue sum = a + b;
282 .. function:: gccjit::rvalue operator- (gccjit::rvalue a, gccjit::rvalue b)
286 gccjit::rvalue diff = a - b;
288 .. function:: gccjit::rvalue operator* (gccjit::rvalue a, gccjit::rvalue b)
292 gccjit::rvalue prod = a * b;
294 .. function:: gccjit::rvalue operator/ (gccjit::rvalue a, gccjit::rvalue b)
298 gccjit::rvalue result = a / b;
300 .. function:: gccjit::rvalue operator% (gccjit::rvalue a, gccjit::rvalue b)
304 gccjit::rvalue mod = a % b;
306 .. function:: gccjit::rvalue operator& (gccjit::rvalue a, gccjit::rvalue b)
310 gccjit::rvalue x = a & b;
312 .. function:: gccjit::rvalue operator^ (gccjit::rvalue a, gccjit::rvalue b)
316 gccjit::rvalue x = a ^ b;
318 .. function:: gccjit::rvalue operator| (gccjit::rvalue a, gccjit::rvalue b)
322 gccjit::rvalue x = a | b;
324 .. function:: gccjit::rvalue operator&& (gccjit::rvalue a, gccjit::rvalue b)
328 gccjit::rvalue cond = a && b;
330 .. function:: gccjit::rvalue operator|| (gccjit::rvalue a, gccjit::rvalue b)
334 gccjit::rvalue cond = a || b;
336 These can of course be combined, giving a terse way to build compound
341 gccjit::rvalue discriminant = (b * b) - (four * a * c);
347 .. function:: gccjit::rvalue \
348 gccjit::context::new_comparison (enum gcc_jit_comparison,\
351 gccjit::location loc)
353 Build a boolean rvalue out of the comparison of two other rvalues.
355 Parameter ``loc`` is optional.
357 This is a thin wrapper around the C API's
358 :c:func:`gcc_jit_context_new_comparison` and the available kinds
359 of comparison are documented there.
361 There are shorter ways to spell the various specific kinds of binary
364 .. function:: gccjit::rvalue \
365 gccjit::context::new_eq (gccjit::rvalue a, gccjit::rvalue b, \
366 gccjit::location loc)
368 .. function:: gccjit::rvalue \
369 gccjit::context::new_ne (gccjit::rvalue a, gccjit::rvalue b, \
370 gccjit::location loc)
372 .. function:: gccjit::rvalue \
373 gccjit::context::new_lt (gccjit::rvalue a, gccjit::rvalue b, \
374 gccjit::location loc)
376 .. function:: gccjit::rvalue \
377 gccjit::context::new_le (gccjit::rvalue a, gccjit::rvalue b, \
378 gccjit::location loc)
380 .. function:: gccjit::rvalue \
381 gccjit::context::new_gt (gccjit::rvalue a, gccjit::rvalue b, \
382 gccjit::location loc)
384 .. function:: gccjit::rvalue \
385 gccjit::context::new_ge (gccjit::rvalue a, gccjit::rvalue b, \
386 gccjit::location loc)
388 The most concise way to spell them is with overloaded operators:
390 .. function:: gccjit::rvalue \
391 operator== (gccjit::rvalue a, gccjit::rvalue b)
395 gccjit::rvalue cond = (a == ctxt.zero (t_int));
397 .. function:: gccjit::rvalue \
398 operator!= (gccjit::rvalue a, gccjit::rvalue b)
402 gccjit::rvalue cond = (i != j);
404 .. function:: gccjit::rvalue \
405 operator< (gccjit::rvalue a, gccjit::rvalue b)
409 gccjit::rvalue cond = i < n;
411 .. function:: gccjit::rvalue \
412 operator<= (gccjit::rvalue a, gccjit::rvalue b)
416 gccjit::rvalue cond = i <= n;
418 .. function:: gccjit::rvalue \
419 operator> (gccjit::rvalue a, gccjit::rvalue b)
423 gccjit::rvalue cond = (ch > limit);
425 .. function:: gccjit::rvalue \
426 operator>= (gccjit::rvalue a, gccjit::rvalue b)
430 gccjit::rvalue cond = (score >= ctxt.new_rvalue (t_int, 100));
432 .. TODO: beyond this point
436 .. function:: gcc_jit_rvalue *\
437 gcc_jit_context_new_call (gcc_jit_context *ctxt,\
438 gcc_jit_location *loc,\
439 gcc_jit_function *func,\
440 int numargs , gcc_jit_rvalue **args)
442 Given a function and the given table of argument rvalues, construct a
443 call to the function, with the result as an rvalue.
447 :func:`gccjit::context::new_call` merely builds a
448 :class:`gccjit::rvalue` i.e. an expression that can be evaluated,
449 perhaps as part of a more complicated expression.
450 The call *won't* happen unless you add a statement to a function
451 that evaluates the expression.
453 For example, if you want to call a function and discard the result
454 (or to call a function with ``void`` return type), use
455 :func:`gccjit::block::add_eval`:
459 /* Add "(void)printf (arg0, arg1);". */
460 block.add_eval (ctxt.new_call (printf_func, arg0, arg1));
465 .. function:: gccjit::rvalue \
466 gccjit::context::new_cast (gccjit::rvalue rvalue,\
468 gccjit::location loc)
470 Given an rvalue of T, construct another rvalue of another type.
472 Currently only a limited set of conversions are possible:
476 * P* <-> Q*, for pointer types P and Q
481 .. class:: gccjit::lvalue
483 An lvalue is something that can of the *left*-hand side of an assignment:
484 a storage area (such as a variable). It is a subclass of
485 :class:`gccjit::rvalue`, where the rvalue is computed by reading from the
488 It iss a thin wrapper around :c:type:`gcc_jit_lvalue *` from the C API.
490 .. function:: gccjit::rvalue \
491 gccjit::lvalue::get_address (gccjit::location loc)
493 Take the address of an lvalue; analogous to:
501 Parameter "loc" is optional.
506 .. function:: gccjit::lvalue \
507 gccjit::context::new_global (enum gcc_jit_global_kind,\
510 gccjit::location loc)
512 Add a new global variable of the given type and name to the context.
514 This is a thin wrapper around :c:func:`gcc_jit_context_new_global` from
515 the C API; the "kind" parameter has the same meaning as there.
517 Working with pointers, structs and unions
518 -----------------------------------------
520 .. function:: gccjit::lvalue \
521 gccjit::rvalue::dereference (gccjit::location loc)
523 Given an rvalue of pointer type ``T *``, dereferencing the pointer,
524 getting an lvalue of type ``T``. Analogous to:
532 Parameter "loc" is optional.
534 If you don't need to specify the location, this can also be expressed using
535 an overloaded operator:
537 .. function:: gccjit::lvalue \
538 gccjit::rvalue::operator* ();
542 gccjit::lvalue content = *ptr;
544 Field access is provided separately for both lvalues and rvalues:
546 .. function:: gccjit::lvalue \
547 gccjit::lvalue::access_field (gccjit::field field, \
548 gccjit::location loc)
550 Given an lvalue of struct or union type, access the given field,
551 getting an lvalue of the field's type. Analogous to:
559 .. function:: gccjit::rvalue \
560 gccjit::rvalue::access_field (gccjit::field field, \
561 gccjit::location loc)
563 Given an rvalue of struct or union type, access the given field
564 as an rvalue. Analogous to:
572 .. function:: gccjit::lvalue \
573 gccjit::rvalue::dereference_field (gccjit::field field, \
574 gccjit::location loc)
576 Given an rvalue of pointer type ``T *`` where T is of struct or union
577 type, access the given field as an lvalue. Analogous to:
583 in C, itself equivalent to ``(*EXPR).FIELD``.
585 .. function:: gccjit::lvalue \
586 gccjit::context::new_array_access (gccjit::rvalue ptr, \
587 gccjit::rvalue index, \
588 gccjit::location loc)
590 Given an rvalue of pointer type ``T *``, get at the element `T` at
591 the given index, using standard C array indexing rules i.e. each
592 increment of ``index`` corresponds to ``sizeof(T)`` bytes.
599 in C (or, indeed, to ``PTR + INDEX``).
601 Parameter "loc" is optional.
603 For array accesses where you don't need to specify a :class:`gccjit::location`,
604 two overloaded operators are available:
606 gccjit::lvalue gccjit::rvalue::operator[] (gccjit::rvalue index)
610 gccjit::lvalue element = array[idx];
612 gccjit::lvalue gccjit::rvalue::operator[] (int index)
616 gccjit::lvalue element = array[0];