]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/jit/docs/topics/expressions.rst
Update copyright years.
[thirdparty/gcc.git] / gcc / jit / docs / topics / expressions.rst
CommitLineData
99dee823 1.. Copyright (C) 2014-2021 Free Software Foundation, Inc.
35485da9
DM
2 Originally contributed by David Malcolm <dmalcolm@redhat.com>
3
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.
8
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.
13
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/>.
17
18.. default-domain:: c
19
20Expressions
21===========
22
23Rvalues
24-------
25.. type:: gcc_jit_rvalue
26
27A :c:type:`gcc_jit_rvalue *` is an expression that can be computed.
28
29It can be simple, e.g.:
30
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).
34
35or compound e.g.:
36
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.
41
42Every rvalue has an associated type, and the API will check to ensure
43that types match up correctly (otherwise the context will emit an error).
44
45.. function:: gcc_jit_type *gcc_jit_rvalue_get_type (gcc_jit_rvalue *rvalue)
46
47 Get the type of this rvalue.
48
49.. function:: gcc_jit_object *gcc_jit_rvalue_as_object (gcc_jit_rvalue *rvalue)
50
51 Upcast the given rvalue to be an object.
52
53
54Simple expressions
55******************
56
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)
61
62 Given a numeric type (integer or floating point), build an rvalue for
ccce3b2a
DM
63 the given constant :c:type:`int` value.
64
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)
69
70 Given a numeric type (integer or floating point), build an rvalue for
71 the given constant :c:type:`long` value.
35485da9
DM
72
73.. function:: gcc_jit_rvalue *gcc_jit_context_zero (gcc_jit_context *ctxt, \
74 gcc_jit_type *numeric_type)
75
76 Given a numeric type (integer or floating point), get the rvalue for
77 zero. Essentially this is just a shortcut for:
78
79 .. code-block:: c
80
81 gcc_jit_context_new_rvalue_from_int (ctxt, numeric_type, 0)
82
83.. function:: gcc_jit_rvalue *gcc_jit_context_one (gcc_jit_context *ctxt, \
84 gcc_jit_type *numeric_type)
85
86 Given a numeric type (integer or floating point), get the rvalue for
51c5c6b5 87 one. Essentially this is just a shortcut for:
35485da9
DM
88
89 .. code-block:: c
90
91 gcc_jit_context_new_rvalue_from_int (ctxt, numeric_type, 1)
92
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)
97
98 Given a numeric type (integer or floating point), build an rvalue for
ccce3b2a 99 the given constant :c:type:`double` value.
35485da9
DM
100
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)
105
106 Given a pointer type, build an rvalue for the given address.
107
108.. function:: gcc_jit_rvalue *gcc_jit_context_null (gcc_jit_context *ctxt, \
109 gcc_jit_type *pointer_type)
110
111 Given a pointer type, build an rvalue for ``NULL``. Essentially this
112 is just a shortcut for:
113
114 .. code-block:: c
115
116 gcc_jit_context_new_rvalue_from_ptr (ctxt, pointer_type, NULL)
117
118.. function:: gcc_jit_rvalue *\
119 gcc_jit_context_new_string_literal (gcc_jit_context *ctxt, \
120 const char *value)
121
122 Generate an rvalue for the given NIL-terminated string, of type
123 :c:data:`GCC_JIT_TYPE_CONST_CHAR_PTR`.
124
c575221a
DM
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.
35485da9 128
6069fe72
DM
129Vector expressions
130******************
131
132.. function:: gcc_jit_rvalue * \
133 gcc_jit_context_new_rvalue_from_vector (gcc_jit_context *ctxt, \
134 gcc_jit_location *loc, \
135 gcc_jit_type *vec_type, \
136 size_t num_elements, \
137 gcc_jit_rvalue **elements)
138
139 Build a vector rvalue from an array of elements.
140
141 "vec_type" should be a vector type, created using
142 :func:`gcc_jit_type_get_vector`.
143
144 "num_elements" should match that of the vector type.
145
146 This entrypoint was added in :ref:`LIBGCCJIT_ABI_10`; you can test for
147 its presence using
148
149 .. code-block:: c
150
151 #ifdef LIBGCCJIT_HAVE_gcc_jit_context_new_rvalue_from_vector
152
35485da9
DM
153Unary Operations
154****************
155
156.. function:: gcc_jit_rvalue * \
157 gcc_jit_context_new_unary_op (gcc_jit_context *ctxt, \
158 gcc_jit_location *loc, \
159 enum gcc_jit_unary_op op, \
160 gcc_jit_type *result_type, \
161 gcc_jit_rvalue *rvalue)
162
163 Build a unary operation out of an input rvalue.
164
6f7585de
DM
165 The parameter ``result_type`` must be a numeric type.
166
35485da9
DM
167.. type:: enum gcc_jit_unary_op
168
169The available unary operations are:
170
171========================================== ============
172Unary Operation C equivalent
173========================================== ============
174:c:macro:`GCC_JIT_UNARY_OP_MINUS` `-(EXPR)`
175:c:macro:`GCC_JIT_UNARY_OP_BITWISE_NEGATE` `~(EXPR)`
176:c:macro:`GCC_JIT_UNARY_OP_LOGICAL_NEGATE` `!(EXPR)`
18146f45 177:c:macro:`GCC_JIT_UNARY_OP_ABS` `abs (EXPR)`
35485da9
DM
178========================================== ============
179
180.. c:macro:: GCC_JIT_UNARY_OP_MINUS
181
182 Negate an arithmetic value; analogous to:
183
184 .. code-block:: c
185
186 -(EXPR)
187
188 in C.
189
190.. c:macro:: GCC_JIT_UNARY_OP_BITWISE_NEGATE
191
192 Bitwise negation of an integer value (one's complement); analogous
193 to:
194
195 .. code-block:: c
196
197 ~(EXPR)
198
199 in C.
200
201.. c:macro:: GCC_JIT_UNARY_OP_LOGICAL_NEGATE
202
203 Logical negation of an arithmetic or pointer value; analogous to:
204
205 .. code-block:: c
206
207 !(EXPR)
208
209 in C.
210
18146f45
DM
211.. c:macro:: GCC_JIT_UNARY_OP_ABS
212
213 Absolute value of an arithmetic expression; analogous to:
214
215 .. code-block:: c
216
217 abs (EXPR)
218
219 in C.
220
35485da9
DM
221Binary Operations
222*****************
223
224.. function:: gcc_jit_rvalue *gcc_jit_context_new_binary_op (gcc_jit_context *ctxt, \
225 gcc_jit_location *loc, \
226 enum gcc_jit_binary_op op, \
227 gcc_jit_type *result_type, \
228 gcc_jit_rvalue *a, gcc_jit_rvalue *b)
229
230 Build a binary operation out of two constituent rvalues.
231
6f7585de
DM
232 The parameter ``result_type`` must be a numeric type.
233
35485da9
DM
234.. type:: enum gcc_jit_binary_op
235
236The available binary operations are:
237
238======================================== ============
239Binary Operation C equivalent
240======================================== ============
241:c:macro:`GCC_JIT_BINARY_OP_PLUS` `x + y`
242:c:macro:`GCC_JIT_BINARY_OP_MINUS` `x - y`
243:c:macro:`GCC_JIT_BINARY_OP_MULT` `x * y`
244:c:macro:`GCC_JIT_BINARY_OP_DIVIDE` `x / y`
245:c:macro:`GCC_JIT_BINARY_OP_MODULO` `x % y`
246:c:macro:`GCC_JIT_BINARY_OP_BITWISE_AND` `x & y`
247:c:macro:`GCC_JIT_BINARY_OP_BITWISE_XOR` `x ^ y`
248:c:macro:`GCC_JIT_BINARY_OP_BITWISE_OR` `x | y`
249:c:macro:`GCC_JIT_BINARY_OP_LOGICAL_AND` `x && y`
250:c:macro:`GCC_JIT_BINARY_OP_LOGICAL_OR` `x || y`
251:c:macro:`GCC_JIT_BINARY_OP_LSHIFT` `x << y`
252:c:macro:`GCC_JIT_BINARY_OP_RSHIFT` `x >> y`
253======================================== ============
254
255.. c:macro:: GCC_JIT_BINARY_OP_PLUS
256
257 Addition of arithmetic values; analogous to:
258
259 .. code-block:: c
260
261 (EXPR_A) + (EXPR_B)
262
263 in C.
264
265 For pointer addition, use :c:func:`gcc_jit_context_new_array_access`.
266
7ef96183 267.. c:macro:: GCC_JIT_BINARY_OP_MINUS
35485da9
DM
268
269 Subtraction of arithmetic values; analogous to:
270
271 .. code-block:: c
272
273 (EXPR_A) - (EXPR_B)
274
275 in C.
276
277.. c:macro:: GCC_JIT_BINARY_OP_MULT
278
279 Multiplication of a pair of arithmetic values; analogous to:
280
281 .. code-block:: c
282
283 (EXPR_A) * (EXPR_B)
284
285 in C.
286
287.. c:macro:: GCC_JIT_BINARY_OP_DIVIDE
288
289 Quotient of division of arithmetic values; analogous to:
290
291 .. code-block:: c
292
293 (EXPR_A) / (EXPR_B)
294
295 in C.
296
297 The result type affects the kind of division: if the result type is
298 integer-based, then the result is truncated towards zero, whereas
299 a floating-point result type indicates floating-point division.
300
301.. c:macro:: GCC_JIT_BINARY_OP_MODULO
302
303 Remainder of division of arithmetic values; analogous to:
304
305 .. code-block:: c
306
307 (EXPR_A) % (EXPR_B)
308
309 in C.
310
311.. c:macro:: GCC_JIT_BINARY_OP_BITWISE_AND
312
313 Bitwise AND; analogous to:
314
315 .. code-block:: c
316
317 (EXPR_A) & (EXPR_B)
318
319 in C.
320
321.. c:macro:: GCC_JIT_BINARY_OP_BITWISE_XOR
322
323 Bitwise exclusive OR; analogous to:
324
325 .. code-block:: c
326
327 (EXPR_A) ^ (EXPR_B)
328
329 in C.
330
331.. c:macro:: GCC_JIT_BINARY_OP_BITWISE_OR
332
333 Bitwise inclusive OR; analogous to:
334
335 .. code-block:: c
336
337 (EXPR_A) | (EXPR_B)
338
339 in C.
340
341.. c:macro:: GCC_JIT_BINARY_OP_LOGICAL_AND
342
343 Logical AND; analogous to:
344
345 .. code-block:: c
346
347 (EXPR_A) && (EXPR_B)
348
349 in C.
350
351.. c:macro:: GCC_JIT_BINARY_OP_LOGICAL_OR
352
353 Logical OR; analogous to:
354
355 .. code-block:: c
356
357 (EXPR_A) || (EXPR_B)
358
359 in C.
360
361.. c:macro:: GCC_JIT_BINARY_OP_LSHIFT
362
363 Left shift; analogous to:
364
365 .. code-block:: c
366
367 (EXPR_A) << (EXPR_B)
368
369 in C.
370
371.. c:macro:: GCC_JIT_BINARY_OP_RSHIFT
372
373 Right shift; analogous to:
374
375 .. code-block:: c
376
377 (EXPR_A) >> (EXPR_B)
378
379 in C.
380
381Comparisons
382***********
383
384.. function:: gcc_jit_rvalue *\
385 gcc_jit_context_new_comparison (gcc_jit_context *ctxt,\
386 gcc_jit_location *loc,\
387 enum gcc_jit_comparison op,\
388 gcc_jit_rvalue *a, gcc_jit_rvalue *b)
389
390 Build a boolean rvalue out of the comparison of two other rvalues.
391
392.. type:: enum gcc_jit_comparison
393
394======================================= ============
395Comparison C equivalent
396======================================= ============
397:c:macro:`GCC_JIT_COMPARISON_EQ` `x == y`
398:c:macro:`GCC_JIT_COMPARISON_NE` `x != y`
399:c:macro:`GCC_JIT_COMPARISON_LT` `x < y`
400:c:macro:`GCC_JIT_COMPARISON_LE` `x <= y`
401:c:macro:`GCC_JIT_COMPARISON_GT` `x > y`
402:c:macro:`GCC_JIT_COMPARISON_GE` `x >= y`
403======================================= ============
404
405
406Function calls
407**************
408.. function:: gcc_jit_rvalue *\
409 gcc_jit_context_new_call (gcc_jit_context *ctxt,\
410 gcc_jit_location *loc,\
411 gcc_jit_function *func,\
412 int numargs , gcc_jit_rvalue **args)
413
414 Given a function and the given table of argument rvalues, construct a
415 call to the function, with the result as an rvalue.
416
417 .. note::
418
419 :c:func:`gcc_jit_context_new_call` merely builds a
420 :c:type:`gcc_jit_rvalue` i.e. an expression that can be evaluated,
421 perhaps as part of a more complicated expression.
422 The call *won't* happen unless you add a statement to a function
423 that evaluates the expression.
424
425 For example, if you want to call a function and discard the result
426 (or to call a function with ``void`` return type), use
427 :c:func:`gcc_jit_block_add_eval`:
428
429 .. code-block:: c
430
431 /* Add "(void)printf (arg0, arg1);". */
432 gcc_jit_block_add_eval (
433 block, NULL,
434 gcc_jit_context_new_call (
435 ctxt,
436 NULL,
437 printf_func,
438 2, args));
439
f51703a8
DM
440.. function:: gcc_jit_rvalue *\
441 gcc_jit_context_new_call_through_ptr (gcc_jit_context *ctxt,\
442 gcc_jit_location *loc,\
443 gcc_jit_rvalue *fn_ptr,\
444 int numargs, \
445 gcc_jit_rvalue **args)
446
ecd5156d
DM
447 Given an rvalue of function pointer type (e.g. from
448 :c:func:`gcc_jit_context_new_function_ptr_type`), and the given table of
f51703a8
DM
449 argument rvalues, construct a call to the function pointer, with the
450 result as an rvalue.
451
452 .. note::
453
454 The same caveat as for :c:func:`gcc_jit_context_new_call` applies.
455
15c671a7
DM
456.. function:: void\
457 gcc_jit_rvalue_set_bool_require_tail_call (gcc_jit_rvalue *call,\
458 int require_tail_call)
459
460 Given an :c:type:`gcc_jit_rvalue *` for a call created through
461 :c:func:`gcc_jit_context_new_call` or
462 :c:func:`gcc_jit_context_new_call_through_ptr`, mark/clear the
463 call as needing tail-call optimization. The optimizer will
464 attempt to optimize the call into a jump instruction; if it is
465 unable to do do, an error will be emitted.
466
467 This may be useful when implementing functions that use the
468 continuation-passing style (e.g. for functional programming
469 languages), in which every function "returns" by calling a
470 "continuation" function pointer. This call must be
471 guaranteed to be implemented as a jump, otherwise the program
472 could consume an arbitrary amount of stack space as it executed.
473
474 This entrypoint was added in :ref:`LIBGCCJIT_ABI_6`; you can test for
475 its presence using
476
477 .. code-block:: c
478
479 #ifdef LIBGCCJIT_HAVE_gcc_jit_rvalue_set_bool_require_tail_call
f51703a8 480
15a65e63
DM
481Function pointers
482*****************
483
ecd5156d 484Function pointers can be obtained:
15a65e63 485
ecd5156d
DM
486 * from a :c:type:`gcc_jit_function` using
487 :c:func:`gcc_jit_function_get_address`, or
15a65e63 488
ecd5156d
DM
489 * from an existing function using
490 :c:func:`gcc_jit_context_new_rvalue_from_ptr`,
491 using a function pointer type obtained using
492 :c:func:`gcc_jit_context_new_function_ptr_type`.
15a65e63 493
35485da9
DM
494Type-coercion
495*************
496
497.. function:: gcc_jit_rvalue *\
498 gcc_jit_context_new_cast (gcc_jit_context *ctxt,\
499 gcc_jit_location *loc,\
500 gcc_jit_rvalue *rvalue,\
501 gcc_jit_type *type)
502
503 Given an rvalue of T, construct another rvalue of another type.
504
505 Currently only a limited set of conversions are possible:
506
507 * int <-> float
508 * int <-> bool
509 * P* <-> Q*, for pointer types P and Q
510
511Lvalues
512-------
513
514.. type:: gcc_jit_lvalue
515
516An lvalue is something that can of the *left*-hand side of an assignment:
517a storage area (such as a variable). It is also usable as an rvalue,
518where the rvalue is computed by reading from the storage area.
519
520.. function:: gcc_jit_object *\
521 gcc_jit_lvalue_as_object (gcc_jit_lvalue *lvalue)
522
523 Upcast an lvalue to be an object.
524
525.. function:: gcc_jit_rvalue *\
526 gcc_jit_lvalue_as_rvalue (gcc_jit_lvalue *lvalue)
527
528 Upcast an lvalue to be an rvalue.
529
530.. function:: gcc_jit_rvalue *\
531 gcc_jit_lvalue_get_address (gcc_jit_lvalue *lvalue,\
532 gcc_jit_location *loc)
533
534 Take the address of an lvalue; analogous to:
535
536 .. code-block:: c
537
538 &(EXPR)
539
540 in C.
541
542Global variables
543****************
544
545.. function:: gcc_jit_lvalue *\
546 gcc_jit_context_new_global (gcc_jit_context *ctxt,\
547 gcc_jit_location *loc,\
791cfef8 548 enum gcc_jit_global_kind kind,\
35485da9
DM
549 gcc_jit_type *type,\
550 const char *name)
551
552 Add a new global variable of the given type and name to the context.
553
6f7585de
DM
554 The parameter ``type`` must be non-`void`.
555
c575221a
DM
556 The parameter ``name`` must be non-NULL. The call takes a copy of the
557 underlying string, so it is valid to pass in a pointer to an on-stack
558 buffer.
559
791cfef8
DM
560 The "kind" parameter determines the visibility of the "global" outside
561 of the :c:type:`gcc_jit_result`:
562
563 .. type:: enum gcc_jit_global_kind
564
565 .. c:macro:: GCC_JIT_GLOBAL_EXPORTED
566
567 Global is defined by the client code and is visible
568 by name outside of this JIT context via
569 :c:func:`gcc_jit_result_get_global` (and this value is required for
570 the global to be accessible via that entrypoint).
571
572 .. c:macro:: GCC_JIT_GLOBAL_INTERNAL
573
574 Global is defined by the client code, but is invisible
575 outside of it. Analogous to a "static" global within a .c file.
576 Specifically, the variable will only be visible within this
577 context and within child contexts.
578
579 .. c:macro:: GCC_JIT_GLOBAL_IMPORTED
580
581 Global is not defined by the client code; we're merely
582 referring to it. Analogous to using an "extern" global from a
583 header file.
35485da9 584
4ecc0061
AC
585.. function:: gcc_jit_lvalue *\
586 gcc_jit_global_set_initializer (gcc_jit_lvalue *global,\
587 const void *blob,\
588 size_t num_bytes)
589
590 Set an initializer for ``global`` using the memory content pointed
591 by ``blob`` for ``num_bytes``. ``global`` must be an array of an
592 integral type. Return the global itself.
593
594 The parameter ``blob`` must be non-NULL. The call copies the memory
595 pointed by ``blob`` for ``num_bytes`` bytes, so it is valid to pass
596 in a pointer to an on-stack buffer. The content will be stored in
597 the compilation unit and used as initialization value of the array.
598
599 This entrypoint was added in :ref:`LIBGCCJIT_ABI_14`; you can test for
600 its presence using
601
602 .. code-block:: c
603
604 #ifdef LIBGCCJIT_HAVE_gcc_jit_global_set_initializer
605
35485da9
DM
606Working with pointers, structs and unions
607-----------------------------------------
608
609.. function:: gcc_jit_lvalue *\
610 gcc_jit_rvalue_dereference (gcc_jit_rvalue *rvalue,\
611 gcc_jit_location *loc)
612
613 Given an rvalue of pointer type ``T *``, dereferencing the pointer,
614 getting an lvalue of type ``T``. Analogous to:
615
616 .. code-block:: c
617
618 *(EXPR)
619
620 in C.
621
622Field access is provided separately for both lvalues and rvalues.
623
624.. function:: gcc_jit_lvalue *\
625 gcc_jit_lvalue_access_field (gcc_jit_lvalue *struct_,\
626 gcc_jit_location *loc,\
627 gcc_jit_field *field)
628
629 Given an lvalue of struct or union type, access the given field,
630 getting an lvalue of the field's type. Analogous to:
631
632 .. code-block:: c
633
634 (EXPR).field = ...;
635
636 in C.
637
638.. function:: gcc_jit_rvalue *\
639 gcc_jit_rvalue_access_field (gcc_jit_rvalue *struct_,\
640 gcc_jit_location *loc,\
641 gcc_jit_field *field)
642
643 Given an rvalue of struct or union type, access the given field
644 as an rvalue. Analogous to:
645
646 .. code-block:: c
647
648 (EXPR).field
649
650 in C.
651
652.. function:: gcc_jit_lvalue *\
653 gcc_jit_rvalue_dereference_field (gcc_jit_rvalue *ptr,\
654 gcc_jit_location *loc,\
655 gcc_jit_field *field)
656
657 Given an rvalue of pointer type ``T *`` where T is of struct or union
658 type, access the given field as an lvalue. Analogous to:
659
660 .. code-block:: c
661
662 (EXPR)->field
663
664 in C, itself equivalent to ``(*EXPR).FIELD``.
665
666.. function:: gcc_jit_lvalue *\
667 gcc_jit_context_new_array_access (gcc_jit_context *ctxt,\
668 gcc_jit_location *loc,\
669 gcc_jit_rvalue *ptr,\
670 gcc_jit_rvalue *index)
671
672 Given an rvalue of pointer type ``T *``, get at the element `T` at
673 the given index, using standard C array indexing rules i.e. each
674 increment of ``index`` corresponds to ``sizeof(T)`` bytes.
675 Analogous to:
676
677 .. code-block:: c
678
679 PTR[INDEX]
680
681 in C (or, indeed, to ``PTR + INDEX``).