xref: /netbsd-src/external/gpl3/gcc.old/dist/gcc/jit/docs/topics/function-pointers.rst (revision 4c3eb207d36f67d31994830c0a694161fc1ca39b)
1*4c3eb207Smrg.. Copyright (C) 2017-2020 Free Software Foundation, Inc.
2cef8759bSmrg   Originally contributed by David Malcolm <dmalcolm@redhat.com>
3cef8759bSmrg
4cef8759bSmrg   This is free software: you can redistribute it and/or modify it
5cef8759bSmrg   under the terms of the GNU General Public License as published by
6cef8759bSmrg   the Free Software Foundation, either version 3 of the License, or
7cef8759bSmrg   (at your option) any later version.
8cef8759bSmrg
9cef8759bSmrg   This program is distributed in the hope that it will be useful, but
10cef8759bSmrg   WITHOUT ANY WARRANTY; without even the implied warranty of
11cef8759bSmrg   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12cef8759bSmrg   General Public License for more details.
13cef8759bSmrg
14cef8759bSmrg   You should have received a copy of the GNU General Public License
15cef8759bSmrg   along with this program.  If not, see
16cef8759bSmrg   <http://www.gnu.org/licenses/>.
17cef8759bSmrg
18cef8759bSmrg.. default-domain:: c
19cef8759bSmrg
20cef8759bSmrgFunction pointers
21cef8759bSmrg=================
22cef8759bSmrg
23cef8759bSmrgYou can generate calls that use a function pointer via
24cef8759bSmrg:c:func:`gcc_jit_context_new_call_through_ptr`.
25cef8759bSmrg
26cef8759bSmrgTo do requires a :c:type:`gcc_jit_rvalue` of the correct function pointer type.
27cef8759bSmrg
28cef8759bSmrgFunction pointers for a :c:type:`gcc_jit_function` can be obtained
29cef8759bSmrgvia :c:func:`gcc_jit_function_get_address`.
30cef8759bSmrg
31cef8759bSmrg.. function:: gcc_jit_rvalue *\
32cef8759bSmrg	      gcc_jit_function_get_address (gcc_jit_function *fn,\
33cef8759bSmrg                                            gcc_jit_location *loc)
34cef8759bSmrg
35cef8759bSmrg   Get the address of a function as an rvalue, of function pointer
36cef8759bSmrg   type.
37cef8759bSmrg
38cef8759bSmrg   This entrypoint was added in :ref:`LIBGCCJIT_ABI_9`; you can test
39cef8759bSmrg   for its presence using
40cef8759bSmrg
41cef8759bSmrg   .. code-block:: c
42cef8759bSmrg
43cef8759bSmrg      #ifdef LIBGCCJIT_HAVE_gcc_jit_function_get_address
44cef8759bSmrg
45cef8759bSmrgAlternatively, given an existing function, you can obtain a pointer
46cef8759bSmrgto it in :c:type:`gcc_jit_rvalue` form using
47cef8759bSmrg:c:func:`gcc_jit_context_new_rvalue_from_ptr`, using a function pointer
48cef8759bSmrgtype obtained using :c:func:`gcc_jit_context_new_function_ptr_type`.
49cef8759bSmrg
50cef8759bSmrgHere's an example of creating a function pointer type corresponding to C's
51cef8759bSmrg:c:type:`void (*) (int, int, int)`:
52cef8759bSmrg
53cef8759bSmrg.. code-block:: c
54cef8759bSmrg
55cef8759bSmrg  gcc_jit_type *void_type =
56cef8759bSmrg    gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_VOID);
57cef8759bSmrg  gcc_jit_type *int_type =
58cef8759bSmrg    gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_INT);
59cef8759bSmrg
60cef8759bSmrg  /* Build the function ptr type.  */
61cef8759bSmrg  gcc_jit_type *param_types[3];
62cef8759bSmrg  param_types[0] = int_type;
63cef8759bSmrg  param_types[1] = int_type;
64cef8759bSmrg  param_types[2] = int_type;
65cef8759bSmrg
66cef8759bSmrg  gcc_jit_type *fn_ptr_type =
67cef8759bSmrg    gcc_jit_context_new_function_ptr_type (ctxt, NULL,
68cef8759bSmrg					   void_type,
69cef8759bSmrg					   3, param_types, 0);
70cef8759bSmrg
71cef8759bSmrg.. function:: gcc_jit_type *\
72cef8759bSmrg	      gcc_jit_context_new_function_ptr_type (gcc_jit_context *ctxt,\
73cef8759bSmrg				       gcc_jit_location *loc,\
74cef8759bSmrg				       gcc_jit_type *return_type,\
75cef8759bSmrg				       int num_params,\
76cef8759bSmrg				       gcc_jit_type **param_types,\
77cef8759bSmrg				       int is_variadic)
78cef8759bSmrg
79cef8759bSmrg   Generate a :c:type:`gcc_jit_type` for a function pointer with the
80cef8759bSmrg   given return type and parameters.
81