]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/jit/docs/topics/expressions.rst
Update copyright years.
[thirdparty/gcc.git] / gcc / jit / docs / topics / expressions.rst
1 .. Copyright (C) 2014-2024 Free Software Foundation, Inc.
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 <https://www.gnu.org/licenses/>.
17
18 .. default-domain:: c
19
20 Expressions
21 ===========
22
23 Rvalues
24 -------
25 .. type:: gcc_jit_rvalue
26
27 A :c:type:`gcc_jit_rvalue` is an expression that can be computed.
28
29 It 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
35 or 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
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).
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
54 Simple 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
63 the given constant :expr:`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 :expr:`long` value.
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
87 one. Essentially this is just a shortcut for:
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
99 the given constant :expr:`double` value.
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
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.
128
129 Constructor expressions
130 ***********************
131
132 The following functions make constructors for array, struct and union
133 types.
134
135 The constructor rvalue can be used for assignment to locals.
136 It can be used to initialize global variables with
137 :func:`gcc_jit_global_set_initializer_rvalue`. It can also be used as a
138 temporary value for function calls and return values, but its address
139 can't be taken.
140
141 Note that arrays in libgccjit do not collapse to pointers like in
142 C. I.e. if an array constructor is used as e.g. a return value, the whole
143 array would be returned by value - array constructors can be assigned to
144 array variables.
145
146 The constructor can contain nested constructors.
147
148 Note that a string literal rvalue can't be used to construct a char array;
149 the latter needs one rvalue for each char.
150
151 These entrypoints were added in :ref:`LIBGCCJIT_ABI_19`; you can test for
152 their presence using:
153
154 .. code-block:: c
155
156 #ifdef LIBGCCJIT_HAVE_CTORS
157
158 .. function:: gcc_jit_rvalue *\
159 gcc_jit_context_new_array_constructor (gcc_jit_context *ctxt,\
160 gcc_jit_location *loc,\
161 gcc_jit_type *type,\
162 size_t num_values,\
163 gcc_jit_rvalue **values)
164
165 Create a constructor for an array as an rvalue.
166
167 Returns NULL on error. ``values`` are copied and
168 do not have to outlive the context.
169
170 ``type`` specifies what the constructor will build and has to be
171 an array.
172
173 ``num_values`` specifies the number of elements in ``values`` and
174 it can't have more elements than the array type.
175
176 Each value in ``values`` sets the corresponding value in the array.
177 If the array type itself has more elements than ``values``, the
178 left-over elements will be zeroed.
179
180 Each value in ``values`` need to be the same unqualified type as the
181 array type's element type.
182
183 If ``num_values`` is 0, the ``values`` parameter will be
184 ignored and zero initialization will be used.
185
186 This entrypoint was added in :ref:`LIBGCCJIT_ABI_19`; you can test for its
187 presence using:
188
189 .. code-block:: c
190
191 #ifdef LIBGCCJIT_HAVE_CTORS
192
193 .. function:: gcc_jit_rvalue *\
194 gcc_jit_context_new_struct_constructor (gcc_jit_context *ctxt,\
195 gcc_jit_location *loc,\
196 gcc_jit_type *type,\
197 size_t num_values,\
198 gcc_jit_field **fields,\
199 gcc_jit_rvalue **values)
200
201
202 Create a constructor for a struct as an rvalue.
203
204 Returns NULL on error. The two parameter arrays are copied and
205 do not have to outlive the context.
206
207 ``type`` specifies what the constructor will build and has to be
208 a struct.
209
210 ``num_values`` specifies the number of elements in ``values``.
211
212 ``fields`` need to have the same length as ``values``, or be NULL.
213
214 If ``fields`` is null, the values are applied in definition order.
215
216 Otherwise, each field in ``fields`` specifies which field in the struct to
217 set to the corresponding value in ``values``. ``fields`` and ``values``
218 are paired by index.
219
220 The fields in ``fields`` have to be in definition order, but there
221 can be gaps. Any field in the struct that is not specified in
222 ``fields`` will be zeroed.
223
224 The fields in ``fields`` need to be the same objects that were used
225 to create the struct.
226
227 Each value has to have have the same unqualified type as the field
228 it is applied to.
229
230 A NULL value element in ``values`` is a shorthand for zero initialization
231 of the corresponding field.
232
233 If ``num_values`` is 0, the array parameters will be
234 ignored and zero initialization will be used.
235
236 This entrypoint was added in :ref:`LIBGCCJIT_ABI_19`; you can test for its
237 presence using:
238
239 .. code-block:: c
240
241 #ifdef LIBGCCJIT_HAVE_CTORS
242
243 .. function:: gcc_jit_rvalue *\
244 gcc_jit_context_new_union_constructor (gcc_jit_context *ctxt,\
245 gcc_jit_location *loc,\
246 gcc_jit_type *type,\
247 gcc_jit_field *field,\
248 gcc_jit_rvalue *value)
249
250 Create a constructor for a union as an rvalue.
251
252 Returns NULL on error.
253
254 ``type`` specifies what the constructor will build and has to be
255 an union.
256
257 ``field`` specifies which field to set. If it is NULL, the first
258 field in the union will be set.``field`` need to be the same object
259 that were used to create the union.
260
261 ``value`` specifies what value to set the corresponding field to.
262 If ``value`` is NULL, zero initialization will be used.
263
264 Each value has to have have the same unqualified type as the field
265 it is applied to.
266
267 This entrypoint was added in :ref:`LIBGCCJIT_ABI_19`; you can test for its
268 presence using:
269
270 .. code-block:: c
271
272 #ifdef LIBGCCJIT_HAVE_CTORS
273
274 Vector expressions
275 ******************
276
277 .. function:: gcc_jit_rvalue * \
278 gcc_jit_context_new_rvalue_from_vector (gcc_jit_context *ctxt, \
279 gcc_jit_location *loc, \
280 gcc_jit_type *vec_type, \
281 size_t num_elements, \
282 gcc_jit_rvalue **elements)
283
284 Build a vector rvalue from an array of elements.
285
286 "vec_type" should be a vector type, created using
287 :func:`gcc_jit_type_get_vector`.
288
289 "num_elements" should match that of the vector type.
290
291 This entrypoint was added in :ref:`LIBGCCJIT_ABI_10`; you can test for
292 its presence using
293
294 .. code-block:: c
295
296 #ifdef LIBGCCJIT_HAVE_gcc_jit_context_new_rvalue_from_vector
297
298 Unary Operations
299 ****************
300
301 .. function:: gcc_jit_rvalue * \
302 gcc_jit_context_new_unary_op (gcc_jit_context *ctxt, \
303 gcc_jit_location *loc, \
304 enum gcc_jit_unary_op op, \
305 gcc_jit_type *result_type, \
306 gcc_jit_rvalue *rvalue)
307
308 Build a unary operation out of an input rvalue.
309
310 The parameter ``result_type`` must be a numeric type.
311
312 .. enum:: gcc_jit_unary_op
313
314 The available unary operations are:
315
316 .. list-table::
317 :header-rows: 1
318
319 * - Unary Operation
320 - C equivalent
321
322 * - :c:macro:`GCC_JIT_UNARY_OP_MINUS`
323 - `-(EXPR)`
324 * - :c:macro:`GCC_JIT_UNARY_OP_BITWISE_NEGATE`
325 - `~(EXPR)`
326 * - :c:macro:`GCC_JIT_UNARY_OP_LOGICAL_NEGATE`
327 - `!(EXPR)`
328 * - :c:macro:`GCC_JIT_UNARY_OP_ABS`
329 - `abs (EXPR)`
330
331 .. c:macro:: GCC_JIT_UNARY_OP_MINUS
332
333 Negate an arithmetic value; analogous to:
334
335 .. code-block:: c
336
337 -(EXPR)
338
339 in C.
340
341 .. c:macro:: GCC_JIT_UNARY_OP_BITWISE_NEGATE
342
343 Bitwise negation of an integer value (one's complement); analogous
344 to:
345
346 .. code-block:: c
347
348 ~(EXPR)
349
350 in C.
351
352 .. c:macro:: GCC_JIT_UNARY_OP_LOGICAL_NEGATE
353
354 Logical negation of an arithmetic or pointer value; analogous to:
355
356 .. code-block:: c
357
358 !(EXPR)
359
360 in C.
361
362 .. c:macro:: GCC_JIT_UNARY_OP_ABS
363
364 Absolute value of an arithmetic expression; analogous to:
365
366 .. code-block:: c
367
368 abs (EXPR)
369
370 in C.
371
372 Binary Operations
373 *****************
374
375 .. function:: gcc_jit_rvalue *gcc_jit_context_new_binary_op (gcc_jit_context *ctxt, \
376 gcc_jit_location *loc, \
377 enum gcc_jit_binary_op op, \
378 gcc_jit_type *result_type, \
379 gcc_jit_rvalue *a, gcc_jit_rvalue *b)
380
381 Build a binary operation out of two constituent rvalues.
382
383 The parameter ``result_type`` must be a numeric type.
384
385 .. enum:: gcc_jit_binary_op
386
387 The available binary operations are:
388
389 .. list-table::
390 :header-rows: 1
391
392 * - Binary Operation
393 - C equivalent
394
395 * - :c:macro:`GCC_JIT_BINARY_OP_PLUS`
396 - `x + y`
397 * - :c:macro:`GCC_JIT_BINARY_OP_MINUS`
398 - `x - y`
399 * - :c:macro:`GCC_JIT_BINARY_OP_MULT`
400 - `x * y`
401 * - :c:macro:`GCC_JIT_BINARY_OP_DIVIDE`
402 - `x / y`
403 * - :c:macro:`GCC_JIT_BINARY_OP_MODULO`
404 - `x % y`
405 * - :c:macro:`GCC_JIT_BINARY_OP_BITWISE_AND`
406 - `x & y`
407 * - :c:macro:`GCC_JIT_BINARY_OP_BITWISE_XOR`
408 - `x ^ y`
409 * - :c:macro:`GCC_JIT_BINARY_OP_BITWISE_OR`
410 - `x | y`
411 * - :c:macro:`GCC_JIT_BINARY_OP_LOGICAL_AND`
412 - `x && y`
413 * - :c:macro:`GCC_JIT_BINARY_OP_LOGICAL_OR`
414 - `x || y`
415 * - :c:macro:`GCC_JIT_BINARY_OP_LSHIFT`
416 - `x << y`
417 * - :c:macro:`GCC_JIT_BINARY_OP_RSHIFT`
418 - `x >> y`
419
420 .. c:macro:: GCC_JIT_BINARY_OP_PLUS
421
422 Addition of arithmetic values; analogous to:
423
424 .. code-block:: c
425
426 (EXPR_A) + (EXPR_B)
427
428 in C.
429
430 For pointer addition, use :c:func:`gcc_jit_context_new_array_access`.
431
432 .. c:macro:: GCC_JIT_BINARY_OP_MINUS
433
434 Subtraction of arithmetic values; analogous to:
435
436 .. code-block:: c
437
438 (EXPR_A) - (EXPR_B)
439
440 in C.
441
442 .. c:macro:: GCC_JIT_BINARY_OP_MULT
443
444 Multiplication of a pair of arithmetic values; analogous to:
445
446 .. code-block:: c
447
448 (EXPR_A) * (EXPR_B)
449
450 in C.
451
452 .. c:macro:: GCC_JIT_BINARY_OP_DIVIDE
453
454 Quotient of division of arithmetic values; analogous to:
455
456 .. code-block:: c
457
458 (EXPR_A) / (EXPR_B)
459
460 in C.
461
462 The result type affects the kind of division: if the result type is
463 integer-based, then the result is truncated towards zero, whereas
464 a floating-point result type indicates floating-point division.
465
466 .. c:macro:: GCC_JIT_BINARY_OP_MODULO
467
468 Remainder of division of arithmetic values; analogous to:
469
470 .. code-block:: c
471
472 (EXPR_A) % (EXPR_B)
473
474 in C.
475
476 .. c:macro:: GCC_JIT_BINARY_OP_BITWISE_AND
477
478 Bitwise AND; analogous to:
479
480 .. code-block:: c
481
482 (EXPR_A) & (EXPR_B)
483
484 in C.
485
486 .. c:macro:: GCC_JIT_BINARY_OP_BITWISE_XOR
487
488 Bitwise exclusive OR; analogous to:
489
490 .. code-block:: c
491
492 (EXPR_A) ^ (EXPR_B)
493
494 in C.
495
496 .. c:macro:: GCC_JIT_BINARY_OP_BITWISE_OR
497
498 Bitwise inclusive OR; analogous to:
499
500 .. code-block:: c
501
502 (EXPR_A) | (EXPR_B)
503
504 in C.
505
506 .. c:macro:: GCC_JIT_BINARY_OP_LOGICAL_AND
507
508 Logical AND; analogous to:
509
510 .. code-block:: c
511
512 (EXPR_A) && (EXPR_B)
513
514 in C.
515
516 .. c:macro:: GCC_JIT_BINARY_OP_LOGICAL_OR
517
518 Logical OR; analogous to:
519
520 .. code-block:: c
521
522 (EXPR_A) || (EXPR_B)
523
524 in C.
525
526 .. c:macro:: GCC_JIT_BINARY_OP_LSHIFT
527
528 Left shift; analogous to:
529
530 .. code-block:: c
531
532 (EXPR_A) << (EXPR_B)
533
534 in C.
535
536 .. c:macro:: GCC_JIT_BINARY_OP_RSHIFT
537
538 Right shift; analogous to:
539
540 .. code-block:: c
541
542 (EXPR_A) >> (EXPR_B)
543
544 in C.
545
546 Comparisons
547 ***********
548
549 .. function:: gcc_jit_rvalue *\
550 gcc_jit_context_new_comparison (gcc_jit_context *ctxt,\
551 gcc_jit_location *loc,\
552 enum gcc_jit_comparison op,\
553 gcc_jit_rvalue *a, gcc_jit_rvalue *b)
554
555 Build a boolean rvalue out of the comparison of two other rvalues.
556
557 .. enum:: gcc_jit_comparison
558
559 .. list-table::
560 :header-rows: 1
561
562 * - Comparison
563 - C equivalent
564
565 * - :c:macro:`GCC_JIT_COMPARISON_EQ`
566 - `x == y`
567 * - :c:macro:`GCC_JIT_COMPARISON_NE`
568 - `x != y`
569 * - :c:macro:`GCC_JIT_COMPARISON_LT`
570 - `x < y`
571 * - :c:macro:`GCC_JIT_COMPARISON_LE`
572 - `x <= y`
573 * - :c:macro:`GCC_JIT_COMPARISON_GT`
574 - `x > y`
575 * - :c:macro:`GCC_JIT_COMPARISON_GE`
576 - `x >= y`
577
578 Function calls
579 **************
580 .. function:: gcc_jit_rvalue *\
581 gcc_jit_context_new_call (gcc_jit_context *ctxt,\
582 gcc_jit_location *loc,\
583 gcc_jit_function *func,\
584 int numargs , gcc_jit_rvalue **args)
585
586 Given a function and the given table of argument rvalues, construct a
587 call to the function, with the result as an rvalue.
588
589 .. note::
590
591 :c:func:`gcc_jit_context_new_call` merely builds a
592 :c:type:`gcc_jit_rvalue` i.e. an expression that can be evaluated,
593 perhaps as part of a more complicated expression.
594 The call *won't* happen unless you add a statement to a function
595 that evaluates the expression.
596
597 For example, if you want to call a function and discard the result
598 (or to call a function with ``void`` return type), use
599 :c:func:`gcc_jit_block_add_eval`:
600
601 .. code-block:: c
602
603 /* Add "(void)printf (arg0, arg1);". */
604 gcc_jit_block_add_eval (
605 block, NULL,
606 gcc_jit_context_new_call (
607 ctxt,
608 NULL,
609 printf_func,
610 2, args));
611
612 .. function:: gcc_jit_rvalue *\
613 gcc_jit_context_new_call_through_ptr (gcc_jit_context *ctxt,\
614 gcc_jit_location *loc,\
615 gcc_jit_rvalue *fn_ptr,\
616 int numargs, \
617 gcc_jit_rvalue **args)
618
619 Given an rvalue of function pointer type (e.g. from
620 :c:func:`gcc_jit_context_new_function_ptr_type`), and the given table of
621 argument rvalues, construct a call to the function pointer, with the
622 result as an rvalue.
623
624 .. note::
625
626 The same caveat as for :c:func:`gcc_jit_context_new_call` applies.
627
628 .. function:: void\
629 gcc_jit_rvalue_set_bool_require_tail_call (gcc_jit_rvalue *call,\
630 int require_tail_call)
631
632 Given an :c:type:`gcc_jit_rvalue` for a call created through
633 :c:func:`gcc_jit_context_new_call` or
634 :c:func:`gcc_jit_context_new_call_through_ptr`, mark/clear the
635 call as needing tail-call optimization. The optimizer will
636 attempt to optimize the call into a jump instruction; if it is
637 unable to do do, an error will be emitted.
638
639 This may be useful when implementing functions that use the
640 continuation-passing style (e.g. for functional programming
641 languages), in which every function "returns" by calling a
642 "continuation" function pointer. This call must be
643 guaranteed to be implemented as a jump, otherwise the program
644 could consume an arbitrary amount of stack space as it executed.
645
646 This entrypoint was added in :ref:`LIBGCCJIT_ABI_6`; you can test for
647 its presence using
648
649 .. code-block:: c
650
651 #ifdef LIBGCCJIT_HAVE_gcc_jit_rvalue_set_bool_require_tail_call
652
653 Function pointers
654 *****************
655
656 Function pointers can be obtained:
657
658 * from a :c:type:`gcc_jit_function` using
659 :c:func:`gcc_jit_function_get_address`, or
660
661 * from an existing function using
662 :c:func:`gcc_jit_context_new_rvalue_from_ptr`,
663 using a function pointer type obtained using
664 :c:func:`gcc_jit_context_new_function_ptr_type`.
665
666 Type-coercion
667 *************
668
669 .. function:: gcc_jit_rvalue *\
670 gcc_jit_context_new_cast (gcc_jit_context *ctxt,\
671 gcc_jit_location *loc,\
672 gcc_jit_rvalue *rvalue,\
673 gcc_jit_type *type)
674
675 Given an rvalue of T, construct another rvalue of another type.
676
677 Currently only a limited set of conversions are possible:
678
679 * int <-> float
680 * int <-> bool
681 * P* <-> Q*, for pointer types P and Q
682
683 .. function:: gcc_jit_rvalue *\
684 gcc_jit_context_new_bitcast (gcc_jit_context *ctxt,\
685 gcc_jit_location *loc,\
686 gcc_jit_rvalue *rvalue,\
687 gcc_jit_type *type)
688
689 Given an rvalue of T, bitcast it to another type, meaning that this will
690 generate a new rvalue by interpreting the bits of ``rvalue`` to the layout
691 of ``type``.
692
693 The type of rvalue must be the same size as the size of ``type``.
694
695 This entrypoint was added in :ref:`LIBGCCJIT_ABI_21`; you can test for
696 its presence using
697
698 .. code-block:: c
699
700 #ifdef LIBGCCJIT_HAVE_gcc_jit_context_new_bitcast
701
702 Lvalues
703 -------
704
705 .. type:: gcc_jit_lvalue
706
707 An lvalue is something that can of the *left*-hand side of an assignment:
708 a storage area (such as a variable). It is also usable as an rvalue,
709 where the rvalue is computed by reading from the storage area.
710
711 .. function:: gcc_jit_object *\
712 gcc_jit_lvalue_as_object (gcc_jit_lvalue *lvalue)
713
714 Upcast an lvalue to be an object.
715
716 .. function:: gcc_jit_rvalue *\
717 gcc_jit_lvalue_as_rvalue (gcc_jit_lvalue *lvalue)
718
719 Upcast an lvalue to be an rvalue.
720
721 .. function:: gcc_jit_rvalue *\
722 gcc_jit_lvalue_get_address (gcc_jit_lvalue *lvalue,\
723 gcc_jit_location *loc)
724
725 Take the address of an lvalue; analogous to:
726
727 .. code-block:: c
728
729 &(EXPR)
730
731 in C.
732
733 .. function:: void\
734 gcc_jit_lvalue_set_tls_model (gcc_jit_lvalue *lvalue,\
735 enum gcc_jit_tls_model model)
736
737 Make a variable a thread-local variable.
738
739 The "model" parameter determines the thread-local storage model of the "lvalue":
740
741 .. enum:: gcc_jit_tls_model
742
743 .. c:macro:: GCC_JIT_TLS_MODEL_NONE
744
745 Don't set the TLS model.
746
747 .. c:macro:: GCC_JIT_TLS_MODEL_GLOBAL_DYNAMIC
748
749 .. c:macro:: GCC_JIT_TLS_MODEL_LOCAL_DYNAMIC
750
751 .. c:macro:: GCC_JIT_TLS_MODEL_INITIAL_EXEC
752
753 .. c:macro:: GCC_JIT_TLS_MODEL_LOCAL_EXEC
754
755 This is analogous to:
756
757 .. code-block:: c
758
759 _Thread_local int foo __attribute__ ((tls_model("MODEL")));
760
761 in C.
762
763 This entrypoint was added in :ref:`LIBGCCJIT_ABI_17`; you can test for
764 its presence using
765
766 .. code-block:: c
767
768 #ifdef LIBGCCJIT_HAVE_gcc_jit_lvalue_set_tls_model
769
770 .. function:: void\
771 gcc_jit_lvalue_set_link_section (gcc_jit_lvalue *lvalue,\
772 const char *section_name)
773
774 Set the link section of a variable.
775 The parameter ``section_name`` must be non-NULL and must contain the
776 leading dot. Analogous to:
777
778 .. code-block:: c
779
780 int variable __attribute__((section(".section")));
781
782 in C.
783
784 This entrypoint was added in :ref:`LIBGCCJIT_ABI_18`; you can test for
785 its presence using
786
787 .. code-block:: c
788
789 #ifdef LIBGCCJIT_HAVE_gcc_jit_lvalue_set_link_section
790
791 .. function:: void\
792 gcc_jit_lvalue_set_register_name (gcc_jit_lvalue *lvalue,\
793 const char *reg_name);
794
795 Set the register name of a variable.
796 The parameter ``reg_name`` must be non-NULL. Analogous to:
797
798 .. code-block:: c
799
800 register int variable asm ("r12");
801
802 in C.
803
804 This entrypoint was added in :ref:`LIBGCCJIT_ABI_22`; you can test for
805 its presence using
806
807 .. code-block:: c
808
809 #ifdef LIBGCCJIT_HAVE_gcc_jit_lvalue_set_register_name
810
811 .. function:: void\
812 gcc_jit_lvalue_set_alignment (gcc_jit_lvalue *lvalue,\
813 unsigned bytes)
814
815 Set the alignment of a variable, in bytes.
816 Analogous to:
817
818 .. code-block:: c
819
820 int variable __attribute__((aligned (16)));
821
822 in C.
823
824 This entrypoint was added in :ref:`LIBGCCJIT_ABI_24`; you can test for
825 its presence using
826
827 .. code-block:: c
828
829 #ifdef LIBGCCJIT_HAVE_ALIGNMENT
830
831 .. function:: unsigned\
832 gcc_jit_lvalue_get_alignment (gcc_jit_lvalue *lvalue)
833
834 Return the alignment of a variable set by ``gcc_jit_lvalue_set_alignment``.
835 Return 0 if the alignment was not set. Analogous to:
836
837 .. code-block:: c
838
839 _Alignof (variable)
840
841 in C.
842
843 This entrypoint was added in :ref:`LIBGCCJIT_ABI_24`; you can test for
844 its presence using
845
846 .. code-block:: c
847
848 #ifdef LIBGCCJIT_HAVE_ALIGNMENT
849
850 Global variables
851 ****************
852
853 .. function:: gcc_jit_lvalue *\
854 gcc_jit_context_new_global (gcc_jit_context *ctxt,\
855 gcc_jit_location *loc,\
856 enum gcc_jit_global_kind kind,\
857 gcc_jit_type *type,\
858 const char *name)
859
860 Add a new global variable of the given type and name to the context.
861
862 The parameter ``type`` must be non-`void`.
863
864 The parameter ``name`` must be non-NULL. The call takes a copy of the
865 underlying string, so it is valid to pass in a pointer to an on-stack
866 buffer.
867
868 The "kind" parameter determines the visibility of the "global" outside
869 of the :c:type:`gcc_jit_result`:
870
871 .. enum:: gcc_jit_global_kind
872
873 .. c:macro:: GCC_JIT_GLOBAL_EXPORTED
874
875 Global is defined by the client code and is visible
876 by name outside of this JIT context via
877 :c:func:`gcc_jit_result_get_global` (and this value is required for
878 the global to be accessible via that entrypoint).
879
880 .. c:macro:: GCC_JIT_GLOBAL_INTERNAL
881
882 Global is defined by the client code, but is invisible
883 outside of it. Analogous to a "static" global within a .c file.
884 Specifically, the variable will only be visible within this
885 context and within child contexts.
886
887 .. c:macro:: GCC_JIT_GLOBAL_IMPORTED
888
889 Global is not defined by the client code; we're merely
890 referring to it. Analogous to using an "extern" global from a
891 header file.
892
893 .. function:: gcc_jit_lvalue *\
894 gcc_jit_global_set_initializer (gcc_jit_lvalue *global,\
895 const void *blob,\
896 size_t num_bytes)
897
898 Set an initializer for ``global`` using the memory content pointed
899 by ``blob`` for ``num_bytes``. ``global`` must be an array of an
900 integral type. Return the global itself.
901
902 The parameter ``blob`` must be non-NULL. The call copies the memory
903 pointed by ``blob`` for ``num_bytes`` bytes, so it is valid to pass
904 in a pointer to an on-stack buffer. The content will be stored in
905 the compilation unit and used as initialization value of the array.
906
907 This entrypoint was added in :ref:`LIBGCCJIT_ABI_14`; you can test for
908 its presence using
909
910 .. code-block:: c
911
912 #ifdef LIBGCCJIT_HAVE_gcc_jit_global_set_initializer
913
914 .. function:: gcc_jit_lvalue *\
915 gcc_jit_global_set_initializer_rvalue (gcc_jit_lvalue *global,\
916 gcc_jit_rvalue *init_value)
917
918 Set the initial value of a global with an rvalue.
919
920 The rvalue needs to be a constant expression, e.g. no function calls.
921
922 The global can't have the ``kind`` :c:macro:`GCC_JIT_GLOBAL_IMPORTED`.
923
924 As a non-comprehensive example it is OK to do the equivalent of:
925
926 .. code-block:: c
927
928 int foo = 3 * 2; /* rvalue from gcc_jit_context_new_binary_op. */
929 int arr[] = {1,2,3,4}; /* rvalue from gcc_jit_context_new_constructor. */
930 int *bar = &arr[2] + 1; /* rvalue from nested "get address" of "array access". */
931 const int baz = 3; /* rvalue from gcc_jit_context_rvalue_from_int. */
932 int boz = baz; /* rvalue from gcc_jit_lvalue_as_rvalue. */
933
934 Use together with :c:func:`gcc_jit_context_new_struct_constructor`,
935 :c:func:`gcc_jit_context_new_union_constructor`, :c:func:`gcc_jit_context_new_array_constructor`
936 to initialize structs, unions and arrays.
937
938 On success, returns the ``global`` parameter unchanged. Otherwise, ``NULL``.
939
940 This entrypoint was added in :ref:`LIBGCCJIT_ABI_19`; you can test for its
941 presence using:
942
943 .. code-block:: c
944
945 #ifdef LIBGCCJIT_HAVE_CTORS
946
947 Working with pointers, structs and unions
948 -----------------------------------------
949
950 .. function:: gcc_jit_lvalue *\
951 gcc_jit_rvalue_dereference (gcc_jit_rvalue *rvalue,\
952 gcc_jit_location *loc)
953
954 Given an rvalue of pointer type ``T *``, dereferencing the pointer,
955 getting an lvalue of type ``T``. Analogous to:
956
957 .. code-block:: c
958
959 *(EXPR)
960
961 in C.
962
963 Field access is provided separately for both lvalues and rvalues.
964
965 .. function:: gcc_jit_lvalue *\
966 gcc_jit_lvalue_access_field (gcc_jit_lvalue *struct_,\
967 gcc_jit_location *loc,\
968 gcc_jit_field *field)
969
970 Given an lvalue of struct or union type, access the given field,
971 getting an lvalue of the field's type. Analogous to:
972
973 .. code-block:: c
974
975 (EXPR).field = ...;
976
977 in C.
978
979 .. function:: gcc_jit_rvalue *\
980 gcc_jit_rvalue_access_field (gcc_jit_rvalue *struct_,\
981 gcc_jit_location *loc,\
982 gcc_jit_field *field)
983
984 Given an rvalue of struct or union type, access the given field
985 as an rvalue. Analogous to:
986
987 .. code-block:: c
988
989 (EXPR).field
990
991 in C.
992
993 .. function:: gcc_jit_lvalue *\
994 gcc_jit_rvalue_dereference_field (gcc_jit_rvalue *ptr,\
995 gcc_jit_location *loc,\
996 gcc_jit_field *field)
997
998 Given an rvalue of pointer type ``T *`` where T is of struct or union
999 type, access the given field as an lvalue. Analogous to:
1000
1001 .. code-block:: c
1002
1003 (EXPR)->field
1004
1005 in C, itself equivalent to ``(*EXPR).FIELD``.
1006
1007 .. function:: gcc_jit_lvalue *\
1008 gcc_jit_context_new_array_access (gcc_jit_context *ctxt,\
1009 gcc_jit_location *loc,\
1010 gcc_jit_rvalue *ptr,\
1011 gcc_jit_rvalue *index)
1012
1013 Given an rvalue of pointer type ``T *``, get at the element `T` at
1014 the given index, using standard C array indexing rules i.e. each
1015 increment of ``index`` corresponds to ``sizeof(T)`` bytes.
1016 Analogous to:
1017
1018 .. code-block:: c
1019
1020 PTR[INDEX]
1021
1022 in C (or, indeed, to ``PTR + INDEX``).