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/>.
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).
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)`
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.
57 .. function:: gcc_jit_rvalue *\
58 gcc_jit_context_new_rvalue_from_int (gcc_jit_context *ctxt, \
59 gcc_jit_type *numeric_type, \
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, \
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:
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:
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, \
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, \
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:
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, \
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
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:
164 .. c:macro:: GCC_JIT_UNARY_OP_BITWISE_NEGATE
166 Bitwise negation of an integer value (one's complement); analogous
175 .. c:macro:: GCC_JIT_UNARY_OP_LOGICAL_NEGATE
177 Logical negation of an arithmetic or pointer value; analogous to:
185 .. c:macro:: GCC_JIT_UNARY_OP_ABS
187 Absolute value of an arithmetic expression; analogous to:
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:
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:
249 .. c:macro:: GCC_JIT_BINARY_OP_MULT
251 Multiplication of a pair of arithmetic values; analogous to:
259 .. c:macro:: GCC_JIT_BINARY_OP_DIVIDE
261 Quotient of division of arithmetic values; analogous to:
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:
283 .. c:macro:: GCC_JIT_BINARY_OP_BITWISE_AND
285 Bitwise AND; analogous to:
293 .. c:macro:: GCC_JIT_BINARY_OP_BITWISE_XOR
295 Bitwise exclusive OR; analogous to:
303 .. c:macro:: GCC_JIT_BINARY_OP_BITWISE_OR
305 Bitwise inclusive OR; analogous to:
313 .. c:macro:: GCC_JIT_BINARY_OP_LOGICAL_AND
315 Logical AND; analogous to:
323 .. c:macro:: GCC_JIT_BINARY_OP_LOGICAL_OR
325 Logical OR; analogous to:
333 .. c:macro:: GCC_JIT_BINARY_OP_LSHIFT
335 Left shift; analogous to:
343 .. c:macro:: GCC_JIT_BINARY_OP_RSHIFT
345 Right shift; analogous to:
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 ======================================= ============
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.
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`:
403 /* Add "(void)printf (arg0, arg1);". */
404 gcc_jit_block_add_eval (
406 gcc_jit_context_new_call (
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,\
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
425 The same caveat as for :c:func:`gcc_jit_context_new_call` applies.
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
450 #ifdef LIBGCCJIT_HAVE_gcc_jit_rvalue_set_bool_require_tail_call
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,\
461 Given an rvalue of T, construct another rvalue of another type.
463 Currently only a limited set of conversions are possible:
467 * P* <-> Q*, for pointer types P and Q
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:
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,\
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
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
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:
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:
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:
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:
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.
616 in C (or, indeed, to ``PTR + INDEX``).