Home | History | Annotate | Line # | Download | only in topics
      1  1.7  mrg .. Copyright (C) 2014-2022 Free Software Foundation, Inc.
      2  1.1  mrg    Originally contributed by David Malcolm <dmalcolm (a] redhat.com>
      3  1.1  mrg 
      4  1.1  mrg    This is free software: you can redistribute it and/or modify it
      5  1.1  mrg    under the terms of the GNU General Public License as published by
      6  1.1  mrg    the Free Software Foundation, either version 3 of the License, or
      7  1.1  mrg    (at your option) any later version.
      8  1.1  mrg 
      9  1.1  mrg    This program is distributed in the hope that it will be useful, but
     10  1.1  mrg    WITHOUT ANY WARRANTY; without even the implied warranty of
     11  1.1  mrg    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     12  1.1  mrg    General Public License for more details.
     13  1.1  mrg 
     14  1.1  mrg    You should have received a copy of the GNU General Public License
     15  1.1  mrg    along with this program.  If not, see
     16  1.7  mrg    <https://www.gnu.org/licenses/>.
     17  1.1  mrg 
     18  1.1  mrg .. default-domain:: cpp
     19  1.1  mrg 
     20  1.1  mrg Compilation contexts
     21  1.1  mrg ====================
     22  1.1  mrg 
     23  1.1  mrg .. class:: gccjit::context
     24  1.1  mrg 
     25  1.1  mrg The top-level of the C++ API is the :class:`gccjit::context` type.
     26  1.1  mrg 
     27  1.1  mrg A :class:`gccjit::context` instance encapsulates the state of a
     28  1.1  mrg compilation.
     29  1.1  mrg 
     30  1.1  mrg You can set up options on it, and add types, functions and code.
     31  1.1  mrg Invoking :func:`gccjit::context::compile` on it gives you a
     32  1.1  mrg :c:type:`gcc_jit_result *`.
     33  1.1  mrg 
     34  1.1  mrg It is a thin wrapper around the C API's :c:type:`gcc_jit_context *`.
     35  1.1  mrg 
     36  1.1  mrg Lifetime-management
     37  1.1  mrg -------------------
     38  1.1  mrg Contexts are the unit of lifetime-management within the API: objects
     39  1.1  mrg have their lifetime bounded by the context they are created within, and
     40  1.1  mrg cleanup of such objects is done for you when the context is released.
     41  1.1  mrg 
     42  1.1  mrg .. function:: gccjit::context gccjit::context::acquire ()
     43  1.1  mrg 
     44  1.1  mrg   This function acquires a new :class:`gccjit::context` instance,
     45  1.1  mrg   which is independent of any others that may be present within this
     46  1.1  mrg   process.
     47  1.1  mrg 
     48  1.1  mrg .. function:: void gccjit::context::release ()
     49  1.1  mrg 
     50  1.1  mrg   This function releases all resources associated with the given context.
     51  1.1  mrg   Both the context itself and all of its :c:type:`gccjit::object *`
     52  1.1  mrg   instances are cleaned up.  It should be called exactly once on a given
     53  1.1  mrg   context.
     54  1.1  mrg 
     55  1.1  mrg   It is invalid to use the context or any of its "contextual" objects
     56  1.1  mrg   after calling this.
     57  1.1  mrg 
     58  1.1  mrg   .. code-block:: c++
     59  1.1  mrg 
     60  1.1  mrg     ctxt.release ();
     61  1.1  mrg 
     62  1.1  mrg .. function:: gccjit::context \
     63  1.1  mrg               gccjit::context::new_child_context ()
     64  1.1  mrg 
     65  1.1  mrg    Given an existing JIT context, create a child context.
     66  1.1  mrg 
     67  1.1  mrg    The child inherits a copy of all option-settings from the parent.
     68  1.1  mrg 
     69  1.1  mrg    The child can reference objects created within the parent, but not
     70  1.1  mrg    vice-versa.
     71  1.1  mrg 
     72  1.1  mrg    The lifetime of the child context must be bounded by that of the
     73  1.1  mrg    parent: you should release a child context before releasing the parent
     74  1.1  mrg    context.
     75  1.1  mrg 
     76  1.1  mrg    If you use a function from a parent context within a child context,
     77  1.1  mrg    you have to compile the parent context before you can compile the
     78  1.1  mrg    child context, and the gccjit::result of the parent context must
     79  1.1  mrg    outlive the gccjit::result of the child context.
     80  1.1  mrg 
     81  1.1  mrg    This allows caching of shared initializations.  For example, you could
     82  1.1  mrg    create types and declarations of global functions in a parent context
     83  1.1  mrg    once within a process, and then create child contexts whenever a
     84  1.1  mrg    function or loop becomes hot. Each such child context can be used for
     85  1.1  mrg    JIT-compiling just one function or loop, but can reference types
     86  1.1  mrg    and helper functions created within the parent context.
     87  1.1  mrg 
     88  1.1  mrg    Contexts can be arbitrarily nested, provided the above rules are
     89  1.1  mrg    followed, but it's probably not worth going above 2 or 3 levels, and
     90  1.1  mrg    there will likely be a performance hit for such nesting.
     91  1.1  mrg 
     92  1.1  mrg 
     93  1.1  mrg Thread-safety
     94  1.1  mrg -------------
     95  1.1  mrg Instances of :class:`gccjit::context` created via
     96  1.1  mrg :func:`gccjit::context::acquire` are independent from each other:
     97  1.1  mrg only one thread may use a given context at once, but multiple threads
     98  1.1  mrg could each have their own contexts without needing locks.
     99  1.1  mrg 
    100  1.1  mrg Contexts created via :func:`gccjit::context::new_child_context` are
    101  1.1  mrg related to their parent context.  They can be partitioned by their
    102  1.1  mrg ultimate ancestor into independent "family trees".   Only one thread
    103  1.1  mrg within a process may use a given "family tree" of such contexts at once,
    104  1.1  mrg and if you're using multiple threads you should provide your own locking
    105  1.1  mrg around entire such context partitions.
    106  1.1  mrg 
    107  1.1  mrg 
    108  1.1  mrg Error-handling
    109  1.1  mrg --------------
    110  1.1  mrg .. FIXME: How does error-handling work for C++ API?
    111  1.1  mrg 
    112  1.1  mrg You can only compile and get code from a context if no errors occur.
    113  1.1  mrg 
    114  1.1  mrg In general, if an error occurs when using an API entrypoint, it returns
    115  1.1  mrg NULL.  You don't have to check everywhere for NULL results, since the
    116  1.1  mrg API gracefully handles a NULL being passed in for any argument.
    117  1.1  mrg 
    118  1.1  mrg Errors are printed on stderr and can be queried using
    119  1.1  mrg :func:`gccjit::context::get_first_error`.
    120  1.1  mrg 
    121  1.1  mrg .. function:: const char *\
    122  1.1  mrg               gccjit::context::get_first_error (gccjit::context *ctxt)
    123  1.1  mrg 
    124  1.1  mrg    Returns the first error message that occurred on the context.
    125  1.1  mrg 
    126  1.1  mrg    The returned string is valid for the rest of the lifetime of the
    127  1.1  mrg    context.
    128  1.1  mrg 
    129  1.1  mrg    If no errors occurred, this will be NULL.
    130  1.1  mrg 
    131  1.1  mrg Debugging
    132  1.1  mrg ---------
    133  1.1  mrg 
    134  1.1  mrg .. function:: void\
    135  1.1  mrg               gccjit::context::dump_to_file (const std::string &path, \
    136  1.1  mrg                                              int update_locations)
    137  1.1  mrg 
    138  1.1  mrg    To help with debugging: dump a C-like representation to the given path,
    139  1.1  mrg    describing what's been set up on the context.
    140  1.1  mrg 
    141  1.1  mrg    If "update_locations" is true, then also set up :class:`gccjit::location`
    142  1.1  mrg    information throughout the context, pointing at the dump file as if it
    143  1.1  mrg    were a source file.  This may be of use in conjunction with
    144  1.1  mrg    :c:macro:`GCCJIT::BOOL_OPTION_DEBUGINFO` to allow stepping through the
    145  1.1  mrg    code in a debugger.
    146  1.1  mrg 
    147  1.1  mrg .. function:: void\
    148  1.1  mrg               gccjit::context::dump_reproducer_to_file (gcc_jit_context *ctxt,\
    149  1.1  mrg                                                         const char *path)
    150  1.1  mrg 
    151  1.1  mrg    This is a thin wrapper around the C API
    152  1.1  mrg    :c:func:`gcc_jit_context_dump_reproducer_to_file`, and hence works the
    153  1.1  mrg    same way.
    154  1.1  mrg 
    155  1.1  mrg    Note that the generated source is C code, not C++; this might be of use
    156  1.1  mrg    for seeing what the C++ bindings are doing at the C level.
    157  1.1  mrg 
    158  1.1  mrg Options
    159  1.1  mrg -------
    160  1.1  mrg 
    161  1.1  mrg String Options
    162  1.1  mrg **************
    163  1.1  mrg 
    164  1.1  mrg .. function:: void \
    165  1.1  mrg               gccjit::context::set_str_option (enum gcc_jit_str_option, \
    166  1.1  mrg                                                const char *value)
    167  1.1  mrg 
    168  1.1  mrg    Set a string option of the context.
    169  1.1  mrg 
    170  1.1  mrg    This is a thin wrapper around the C API
    171  1.1  mrg    :c:func:`gcc_jit_context_set_str_option`; the options have the same
    172  1.1  mrg    meaning.
    173  1.1  mrg 
    174  1.1  mrg Boolean options
    175  1.1  mrg ***************
    176  1.1  mrg 
    177  1.1  mrg .. function:: void \
    178  1.1  mrg               gccjit::context::set_bool_option(enum gcc_jit_bool_option, \
    179  1.1  mrg                                                int value)
    180  1.1  mrg 
    181  1.1  mrg   Set a boolean option of the context.
    182  1.1  mrg 
    183  1.1  mrg   This is a thin wrapper around the C API
    184  1.1  mrg   :c:func:`gcc_jit_context_set_bool_option`; the options have the same
    185  1.1  mrg   meaning.
    186  1.1  mrg 
    187  1.1  mrg .. function:: void \
    188  1.1  mrg               gccjit::context::set_bool_allow_unreachable_blocks (int bool_value)
    189  1.1  mrg 
    190  1.1  mrg    By default, libgccjit will issue an error about unreachable blocks
    191  1.1  mrg    within a function.
    192  1.1  mrg 
    193  1.1  mrg    This entrypoint can be used to disable that error; it is a thin wrapper
    194  1.1  mrg    around the C API
    195  1.1  mrg    :c:func:`gcc_jit_context_set_bool_allow_unreachable_blocks`.
    196  1.1  mrg 
    197  1.1  mrg    This entrypoint was added in :ref:`LIBGCCJIT_ABI_2`; you can test for
    198  1.1  mrg    its presence using
    199  1.1  mrg 
    200  1.1  mrg    .. code-block:: c
    201  1.1  mrg 
    202  1.1  mrg       #ifdef LIBGCCJIT_HAVE_gcc_jit_context_set_bool_allow_unreachable_blocks
    203  1.1  mrg 
    204  1.3  mrg .. function:: void \
    205  1.3  mrg               gccjit::context::set_bool_use_external_driver (int bool_value)
    206  1.3  mrg 
    207  1.3  mrg    libgccjit internally generates assembler, and uses "driver" code
    208  1.3  mrg    for converting it to other formats (e.g. shared libraries).
    209  1.3  mrg 
    210  1.3  mrg    By default, libgccjit will use an embedded copy of the driver
    211  1.3  mrg    code.
    212  1.3  mrg 
    213  1.3  mrg    This option can be used to instead invoke an external driver executable
    214  1.3  mrg    as a subprocess; it is a thin wrapper around the C API
    215  1.3  mrg    :c:func:`gcc_jit_context_set_bool_use_external_driver`.
    216  1.3  mrg 
    217  1.3  mrg    This entrypoint was added in :ref:`LIBGCCJIT_ABI_5`; you can test for
    218  1.3  mrg    its presence using
    219  1.3  mrg 
    220  1.3  mrg    .. code-block:: c
    221  1.3  mrg 
    222  1.3  mrg       #ifdef LIBGCCJIT_HAVE_gcc_jit_context_set_bool_use_external_driver
    223  1.3  mrg 
    224  1.1  mrg Integer options
    225  1.1  mrg ***************
    226  1.1  mrg 
    227  1.1  mrg .. function:: void \
    228  1.1  mrg               gccjit::context::set_int_option (enum gcc_jit_int_option, \
    229  1.1  mrg                                                int value)
    230  1.1  mrg 
    231  1.1  mrg   Set an integer option of the context.
    232  1.1  mrg 
    233  1.1  mrg   This is a thin wrapper around the C API
    234  1.1  mrg   :c:func:`gcc_jit_context_set_int_option`; the options have the same
    235  1.1  mrg   meaning.
    236  1.1  mrg 
    237  1.1  mrg Additional command-line options
    238  1.1  mrg *******************************
    239  1.1  mrg 
    240  1.1  mrg .. function:: void \
    241  1.1  mrg               gccjit::context::add_command_line_option (const char *optname)
    242  1.1  mrg 
    243  1.1  mrg    Add an arbitrary gcc command-line option to the context for use
    244  1.1  mrg    when compiling.
    245  1.1  mrg 
    246  1.1  mrg    This is a thin wrapper around the C API
    247  1.1  mrg    :c:func:`gcc_jit_context_add_command_line_option`.
    248  1.1  mrg 
    249  1.1  mrg    This entrypoint was added in :ref:`LIBGCCJIT_ABI_1`; you can test for
    250  1.1  mrg    its presence using
    251  1.1  mrg 
    252  1.1  mrg    .. code-block:: c
    253  1.1  mrg 
    254  1.1  mrg       #ifdef LIBGCCJIT_HAVE_gcc_jit_context_add_command_line_option
    255