Home | History | Annotate | Line # | Download | only in topics
      1  1.1.1.4  mrg .. Copyright (C) 2017-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.1.1.4  mrg    <https://www.gnu.org/licenses/>.
     17      1.1  mrg 
     18      1.1  mrg .. default-domain:: c
     19      1.1  mrg 
     20      1.1  mrg Function pointers
     21      1.1  mrg =================
     22      1.1  mrg 
     23      1.1  mrg You can generate calls that use a function pointer via
     24      1.1  mrg :c:func:`gcc_jit_context_new_call_through_ptr`.
     25      1.1  mrg 
     26      1.1  mrg To do requires a :c:type:`gcc_jit_rvalue` of the correct function pointer type.
     27      1.1  mrg 
     28      1.1  mrg Function pointers for a :c:type:`gcc_jit_function` can be obtained
     29      1.1  mrg via :c:func:`gcc_jit_function_get_address`.
     30      1.1  mrg 
     31      1.1  mrg .. function:: gcc_jit_rvalue *\
     32      1.1  mrg 	      gcc_jit_function_get_address (gcc_jit_function *fn,\
     33      1.1  mrg                                             gcc_jit_location *loc)
     34      1.1  mrg 
     35      1.1  mrg    Get the address of a function as an rvalue, of function pointer
     36      1.1  mrg    type.
     37      1.1  mrg 
     38      1.1  mrg    This entrypoint was added in :ref:`LIBGCCJIT_ABI_9`; you can test
     39      1.1  mrg    for its presence using
     40      1.1  mrg 
     41      1.1  mrg    .. code-block:: c
     42      1.1  mrg 
     43      1.1  mrg       #ifdef LIBGCCJIT_HAVE_gcc_jit_function_get_address
     44      1.1  mrg 
     45      1.1  mrg Alternatively, given an existing function, you can obtain a pointer
     46      1.1  mrg to it in :c:type:`gcc_jit_rvalue` form using
     47      1.1  mrg :c:func:`gcc_jit_context_new_rvalue_from_ptr`, using a function pointer
     48      1.1  mrg type obtained using :c:func:`gcc_jit_context_new_function_ptr_type`.
     49      1.1  mrg 
     50      1.1  mrg Here's an example of creating a function pointer type corresponding to C's
     51      1.1  mrg :c:type:`void (*) (int, int, int)`:
     52      1.1  mrg 
     53      1.1  mrg .. code-block:: c
     54      1.1  mrg 
     55      1.1  mrg   gcc_jit_type *void_type =
     56      1.1  mrg     gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_VOID);
     57      1.1  mrg   gcc_jit_type *int_type =
     58      1.1  mrg     gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_INT);
     59      1.1  mrg 
     60      1.1  mrg   /* Build the function ptr type.  */
     61      1.1  mrg   gcc_jit_type *param_types[3];
     62      1.1  mrg   param_types[0] = int_type;
     63      1.1  mrg   param_types[1] = int_type;
     64      1.1  mrg   param_types[2] = int_type;
     65      1.1  mrg 
     66      1.1  mrg   gcc_jit_type *fn_ptr_type =
     67      1.1  mrg     gcc_jit_context_new_function_ptr_type (ctxt, NULL,
     68      1.1  mrg 					   void_type,
     69      1.1  mrg 					   3, param_types, 0);
     70      1.1  mrg 
     71      1.1  mrg .. function:: gcc_jit_type *\
     72      1.1  mrg 	      gcc_jit_context_new_function_ptr_type (gcc_jit_context *ctxt,\
     73      1.1  mrg 				       gcc_jit_location *loc,\
     74      1.1  mrg 				       gcc_jit_type *return_type,\
     75      1.1  mrg 				       int num_params,\
     76      1.1  mrg 				       gcc_jit_type **param_types,\
     77      1.1  mrg 				       int is_variadic)
     78      1.1  mrg 
     79      1.1  mrg    Generate a :c:type:`gcc_jit_type` for a function pointer with the
     80      1.1  mrg    given return type and parameters.
     81  1.1.1.4  mrg 
     82  1.1.1.4  mrg    Each of `param_types` must be non-`void`; `return_type` may be `void`.
     83