annotate gcc/jit/docs/topics/function-pointers.rst @ 111:04ced10e8804

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