1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510
|
.. Copyright (C) 2014-2022 Free Software Foundation, Inc.
Originally contributed by David Malcolm <dmalcolm@redhat.com>
This is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see
<https://www.gnu.org/licenses/>.
.. default-domain:: c
Types
=====
.. c:type:: gcc_jit_type
gcc_jit_type represents a type within the library.
.. function:: gcc_jit_object *gcc_jit_type_as_object (gcc_jit_type *type)
Upcast a type to an object.
Types can be created in several ways:
* fundamental types can be accessed using
:func:`gcc_jit_context_get_type`:
.. code-block:: c
gcc_jit_type *int_type = gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_INT);
See :func:`gcc_jit_context_get_type` for the available types.
* derived types can be accessed by using functions such as
:func:`gcc_jit_type_get_pointer` and :func:`gcc_jit_type_get_const`:
.. code-block:: c
gcc_jit_type *const_int_star = gcc_jit_type_get_pointer (gcc_jit_type_get_const (int_type));
gcc_jit_type *int_const_star = gcc_jit_type_get_const (gcc_jit_type_get_pointer (int_type));
* by creating structures (see below).
Standard types
--------------
.. function:: gcc_jit_type *gcc_jit_context_get_type (gcc_jit_context *ctxt, \
enum gcc_jit_types type_)
Access a specific type. The available types are:
========================================== ================================
`enum gcc_jit_types` value Meaning
========================================== ================================
:c:data:`GCC_JIT_TYPE_VOID` C's ``void`` type.
:c:data:`GCC_JIT_TYPE_VOID_PTR` C's ``void *``.
:c:data:`GCC_JIT_TYPE_BOOL` C++'s ``bool`` type; also C99's
``_Bool`` type, aka ``bool`` if
using stdbool.h.
:c:data:`GCC_JIT_TYPE_CHAR` C's ``char`` (of some signedness)
:c:data:`GCC_JIT_TYPE_SIGNED_CHAR` C's ``signed char``
:c:data:`GCC_JIT_TYPE_UNSIGNED_CHAR` C's ``unsigned char``
:c:data:`GCC_JIT_TYPE_SHORT` C's ``short`` (signed)
:c:data:`GCC_JIT_TYPE_UNSIGNED_SHORT` C's ``unsigned short``
:c:data:`GCC_JIT_TYPE_INT` C's ``int`` (signed)
:c:data:`GCC_JIT_TYPE_UNSIGNED_INT` C's ``unsigned int``
:c:data:`GCC_JIT_TYPE_LONG` C's ``long`` (signed)
:c:data:`GCC_JIT_TYPE_UNSIGNED_LONG` C's ``unsigned long``
:c:data:`GCC_JIT_TYPE_LONG_LONG` C99's ``long long`` (signed)
:c:data:`GCC_JIT_TYPE_UNSIGNED_LONG_LONG` C99's ``unsigned long long``
:c:data:`GCC_JIT_TYPE_UINT8_T` C99's ``uint8_t``
:c:data:`GCC_JIT_TYPE_UINT16_T` C99's ``uint16_t``
:c:data:`GCC_JIT_TYPE_UINT32_T` C99's ``uint32_t``
:c:data:`GCC_JIT_TYPE_UINT64_T` C99's ``uint64_t``
:c:data:`GCC_JIT_TYPE_UINT128_T` C99's ``__uint128_t``
:c:data:`GCC_JIT_TYPE_INT8_T` C99's ``int8_t``
:c:data:`GCC_JIT_TYPE_INT16_T` C99's ``int16_t``
:c:data:`GCC_JIT_TYPE_INT32_T` C99's ``int32_t``
:c:data:`GCC_JIT_TYPE_INT64_T` C99's ``int64_t``
:c:data:`GCC_JIT_TYPE_INT128_T` C99's ``__int128_t``
:c:data:`GCC_JIT_TYPE_FLOAT`
:c:data:`GCC_JIT_TYPE_DOUBLE`
:c:data:`GCC_JIT_TYPE_LONG_DOUBLE`
:c:data:`GCC_JIT_TYPE_CONST_CHAR_PTR` C type: ``(const char *)``
:c:data:`GCC_JIT_TYPE_SIZE_T` C's ``size_t`` type
:c:data:`GCC_JIT_TYPE_FILE_PTR` C type: ``(FILE *)``
:c:data:`GCC_JIT_TYPE_COMPLEX_FLOAT` C99's ``_Complex float``
:c:data:`GCC_JIT_TYPE_COMPLEX_DOUBLE` C99's ``_Complex double``
:c:data:`GCC_JIT_TYPE_COMPLEX_LONG_DOUBLE` C99's ``_Complex long double``
========================================== ================================
.. function:: gcc_jit_type *\
gcc_jit_context_get_int_type (gcc_jit_context *ctxt, \
int num_bytes, int is_signed)
Access the integer type of the given size.
Pointers, `const`, and `volatile`
---------------------------------
.. function:: gcc_jit_type *gcc_jit_type_get_pointer (gcc_jit_type *type)
Given type "T", get type "T*".
.. function:: gcc_jit_type *gcc_jit_type_get_const (gcc_jit_type *type)
Given type "T", get type "const T".
.. function:: gcc_jit_type *gcc_jit_type_get_volatile (gcc_jit_type *type)
Given type "T", get type "volatile T".
.. function:: gcc_jit_type *\
gcc_jit_context_new_array_type (gcc_jit_context *ctxt, \
gcc_jit_location *loc, \
gcc_jit_type *element_type, \
int num_elements)
Given non-`void` type "T", get type "T[N]" (for a constant N).
.. function:: gcc_jit_type *\
gcc_jit_type_get_aligned (gcc_jit_type *type, \
size_t alignment_in_bytes)
Given non-`void` type "T", get type:
.. code-block:: c
T __attribute__ ((aligned (ALIGNMENT_IN_BYTES)))
The alignment must be a power of two.
This entrypoint was added in :ref:`LIBGCCJIT_ABI_7`; you can test for
its presence using
.. code-block:: c
#ifdef LIBGCCJIT_HAVE_gcc_jit_type_get_aligned
Vector types
------------
.. function:: gcc_jit_type *\
gcc_jit_type_get_vector (gcc_jit_type *type, \
size_t num_units)
Given type "T", get type:
.. code-block:: c
T __attribute__ ((vector_size (sizeof(T) * num_units))
T must be integral or floating point; num_units must be a power of two.
This can be used to construct a vector type in which operations
are applied element-wise. The compiler will automatically
use SIMD instructions where possible. See:
https://gcc.gnu.org/onlinedocs/gcc/Vector-Extensions.html
For example, assuming 4-byte ``ints``, then:
.. code-block:: c
typedef int v4si __attribute__ ((vector_size (16)));
can be obtained using:
.. code-block:: c
gcc_jit_type *int_type = gcc_jit_context_get_type (ctxt,
GCC_JIT_TYPE_INT);
gcc_jit_type *v4si_type = gcc_jit_type_get_vector (int_type, 4);
This API entrypoint was added in :ref:`LIBGCCJIT_ABI_8`; you can test
for its presence using
.. code-block:: c
#ifdef LIBGCCJIT_HAVE_gcc_jit_type_get_vector
Vector rvalues can be generated using
:func:`gcc_jit_context_new_rvalue_from_vector`.
Structures and unions
---------------------
.. c:type:: gcc_jit_struct
A compound type analagous to a C `struct`.
.. c:type:: gcc_jit_field
A field within a :c:type:`gcc_jit_struct`.
You can model C `struct` types by creating :c:type:`gcc_jit_struct` and
:c:type:`gcc_jit_field` instances, in either order:
* by creating the fields, then the structure. For example, to model:
.. code-block:: c
struct coord {double x; double y; };
you could call:
.. code-block:: c
gcc_jit_field *field_x =
gcc_jit_context_new_field (ctxt, NULL, double_type, "x");
gcc_jit_field *field_y =
gcc_jit_context_new_field (ctxt, NULL, double_type, "y");
gcc_jit_field *fields[2] = {field_x, field_y};
gcc_jit_struct *coord =
gcc_jit_context_new_struct_type (ctxt, NULL, "coord", 2, fields);
* by creating the structure, then populating it with fields, typically
to allow modelling self-referential structs such as:
.. code-block:: c
struct node { int m_hash; struct node *m_next; };
like this:
.. code-block:: c
gcc_jit_type *node =
gcc_jit_context_new_opaque_struct (ctxt, NULL, "node");
gcc_jit_type *node_ptr =
gcc_jit_type_get_pointer (node);
gcc_jit_field *field_hash =
gcc_jit_context_new_field (ctxt, NULL, int_type, "m_hash");
gcc_jit_field *field_next =
gcc_jit_context_new_field (ctxt, NULL, node_ptr, "m_next");
gcc_jit_field *fields[2] = {field_hash, field_next};
gcc_jit_struct_set_fields (node, NULL, 2, fields);
.. function:: gcc_jit_field *\
gcc_jit_context_new_field (gcc_jit_context *ctxt,\
gcc_jit_location *loc,\
gcc_jit_type *type,\
const char *name)
Construct a new field, with the given type and name.
The parameter ``type`` must be non-`void`.
The parameter ``name`` must be non-NULL. The call takes a copy of the
underlying string, so it is valid to pass in a pointer to an on-stack
buffer.
.. function:: gcc_jit_field *\
gcc_jit_context_new_bitfield (gcc_jit_context *ctxt,\
gcc_jit_location *loc,\
gcc_jit_type *type,\
int width,\
const char *name)
Construct a new bit field, with the given type width and name.
The parameter ``name`` must be non-NULL. The call takes a copy of the
underlying string, so it is valid to pass in a pointer to an on-stack
buffer.
The parameter ``type`` must be an integer type.
The parameter ``width`` must be a positive integer that does not exceed the
size of ``type``.
This API entrypoint was added in :ref:`LIBGCCJIT_ABI_12`; you can test
for its presence using
.. code-block:: c
#ifdef LIBGCCJIT_HAVE_gcc_jit_context_new_bitfield
.. function:: gcc_jit_object *\
gcc_jit_field_as_object (gcc_jit_field *field)
Upcast from field to object.
.. function:: gcc_jit_struct *\
gcc_jit_context_new_struct_type (gcc_jit_context *ctxt,\
gcc_jit_location *loc,\
const char *name,\
int num_fields,\
gcc_jit_field **fields)
Construct a new struct type, with the given name and fields.
The parameter ``name`` must be non-NULL. The call takes a copy of
the underlying string, so it is valid to pass in a pointer to an
on-stack buffer.
.. function:: gcc_jit_struct *\
gcc_jit_context_new_opaque_struct (gcc_jit_context *ctxt,\
gcc_jit_location *loc,\
const char *name)
Construct a new struct type, with the given name, but without
specifying the fields. The fields can be omitted (in which case the
size of the struct is not known), or later specified using
:c:func:`gcc_jit_struct_set_fields`.
The parameter ``name`` must be non-NULL. The call takes a copy of
the underlying string, so it is valid to pass in a pointer to an
on-stack buffer.
.. function:: gcc_jit_type *\
gcc_jit_struct_as_type (gcc_jit_struct *struct_type)
Upcast from struct to type.
.. function:: void\
gcc_jit_struct_set_fields (gcc_jit_struct *struct_type,\
gcc_jit_location *loc,\
int num_fields,\
gcc_jit_field **fields)
Populate the fields of a formerly-opaque struct type.
This can only be called once on a given struct type.
.. function:: gcc_jit_type *\
gcc_jit_context_new_union_type (gcc_jit_context *ctxt,\
gcc_jit_location *loc,\
const char *name,\
int num_fields,\
gcc_jit_field **fields)
Construct a new union type, with the given name and fields.
The parameter ``name`` must be non-NULL. It is copied, so the input
buffer does not need to outlive the call.
Example of use:
.. literalinclude:: ../../../testsuite/jit.dg/test-accessing-union.c
:start-after: /* Quote from here in docs/topics/types.rst. */
:end-before: /* Quote up to here in docs/topics/types.rst. */
:language: c
Function pointer types
----------------------
Function pointer types can be created using
:c:func:`gcc_jit_context_new_function_ptr_type`.
Reflection API
--------------
.. function:: gcc_jit_type *\
gcc_jit_type_dyncast_array (gcc_jit_type *type)
Get the element type of an array type or NULL if it's not an array.
.. function:: int\
gcc_jit_type_is_bool (gcc_jit_type *type)
Return non-zero if the type is a bool.
.. function:: gcc_jit_function_type *\
gcc_jit_type_dyncast_function_ptr_type (gcc_jit_type *type)
Return the function type if it is one or NULL.
.. function:: gcc_jit_type *\
gcc_jit_function_type_get_return_type (gcc_jit_function_type *function_type)
Given a function type, return its return type.
.. function:: size_t\
gcc_jit_function_type_get_param_count (gcc_jit_function_type *function_type)
Given a function type, return its number of parameters.
.. function:: gcc_jit_type *\
gcc_jit_function_type_get_param_type (gcc_jit_function_type *function_type,\
size_t index)
Given a function type, return the type of the specified parameter.
.. function:: int\
gcc_jit_type_is_integral (gcc_jit_type *type)
Return non-zero if the type is an integral.
.. function:: gcc_jit_type *\
gcc_jit_type_is_pointer (gcc_jit_type *type)
Return the type pointed by the pointer type or NULL if it's not a pointer.
.. function:: gcc_jit_vector_type *\
gcc_jit_type_dyncast_vector (gcc_jit_type *type)
Given a type, return a dynamic cast to a vector type or NULL.
.. function:: gcc_jit_struct *\
gcc_jit_type_is_struct (gcc_jit_type *type)
Given a type, return a dynamic cast to a struct type or NULL.
.. function:: size_t\
gcc_jit_vector_type_get_num_units (gcc_jit_vector_type *vector_type)
Given a vector type, return the number of units it contains.
.. function:: gcc_jit_type *\
gcc_jit_vector_type_get_element_type (gcc_jit_vector_type * vector_type)
Given a vector type, return the type of its elements.
.. function:: gcc_jit_type *\
gcc_jit_type_unqualified (gcc_jit_type *type)
Given a type, return the unqualified type, removing "const", "volatile" and
alignment qualifiers.
.. function:: gcc_jit_field *\
gcc_jit_struct_get_field (gcc_jit_struct *struct_type,\
size_t index)
Get a struct field by index.
.. function:: size_t\
gcc_jit_struct_get_field_count (gcc_jit_struct *struct_type)
Get the number of fields in the struct.
The API entrypoints related to the reflection API:
* :c:func:`gcc_jit_function_type_get_return_type`
* :c:func:`gcc_jit_function_type_get_param_count`
* :c:func:`gcc_jit_function_type_get_param_type`
* :c:func:`gcc_jit_type_unqualified`
* :c:func:`gcc_jit_type_dyncast_array`
* :c:func:`gcc_jit_type_is_bool`
* :c:func:`gcc_jit_type_dyncast_function_ptr_type`
* :c:func:`gcc_jit_type_is_integral`
* :c:func:`gcc_jit_type_is_pointer`
* :c:func:`gcc_jit_type_dyncast_vector`
* :c:func:`gcc_jit_vector_type_get_element_type`
* :c:func:`gcc_jit_vector_type_get_num_units`
* :c:func:`gcc_jit_struct_get_field`
* :c:func:`gcc_jit_type_is_struct`
* :c:func:`gcc_jit_struct_get_field_count`
were added in :ref:`LIBGCCJIT_ABI_16`; you can test for their presence
using
.. code-block:: c
#ifdef LIBGCCJIT_HAVE_REFLECTION
.. type:: gcc_jit_case
.. function:: int\
gcc_jit_compatible_types (gcc_jit_type *ltype,\
gcc_jit_type *rtype)
Return non-zero if the two types are compatible. For instance,
if :c:data:`GCC_JIT_TYPE_UINT64_T` and :c:data:`GCC_JIT_TYPE_UNSIGNED_LONG`
are the same size on the target, this will return non-zero.
The parameters ``ltype`` and ``rtype`` must be non-NULL.
Return 0 on errors.
This entrypoint was added in :ref:`LIBGCCJIT_ABI_20`; you can test for
its presence using
.. code-block:: c
#ifdef LIBGCCJIT_HAVE_SIZED_INTEGERS
.. function:: ssize_t\
gcc_jit_type_get_size (gcc_jit_type *type)
Return the size of a type, in bytes. It only works on integer types for now.
The parameter ``type`` must be non-NULL.
Return -1 on errors.
This entrypoint was added in :ref:`LIBGCCJIT_ABI_20`; you can test for
its presence using
.. code-block:: c
#ifdef LIBGCCJIT_HAVE_SIZED_INTEGERS
|