annotate gcc/jit/docs/topics/compilation.rst @ 131:84e7813d76e9

gcc-8.2
author mir3636
date Thu, 25 Oct 2018 07:37:49 +0900
parents 04ced10e8804
children 1830386684a0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1 .. Copyright (C) 2014-2018 Free Software Foundation, Inc.
111
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 Compiling a context
kono
parents:
diff changeset
21 ===================
kono
parents:
diff changeset
22
kono
parents:
diff changeset
23 Once populated, a :c:type:`gcc_jit_context *` can be compiled to
kono
parents:
diff changeset
24 machine code, either in-memory via :c:func:`gcc_jit_context_compile` or
kono
parents:
diff changeset
25 to disk via :c:func:`gcc_jit_context_compile_to_file`.
kono
parents:
diff changeset
26
kono
parents:
diff changeset
27 You can compile a context multiple times (using either form of
kono
parents:
diff changeset
28 compilation), although any errors that occur on the context will
kono
parents:
diff changeset
29 prevent any future compilation of that context.
kono
parents:
diff changeset
30
kono
parents:
diff changeset
31 In-memory compilation
kono
parents:
diff changeset
32 *********************
kono
parents:
diff changeset
33
kono
parents:
diff changeset
34 .. function:: gcc_jit_result *\
kono
parents:
diff changeset
35 gcc_jit_context_compile (gcc_jit_context *ctxt)
kono
parents:
diff changeset
36
kono
parents:
diff changeset
37 This calls into GCC and builds the code, returning a
kono
parents:
diff changeset
38 `gcc_jit_result *`.
kono
parents:
diff changeset
39
kono
parents:
diff changeset
40 If the result is non-NULL, the caller becomes responsible for
kono
parents:
diff changeset
41 calling :func:`gcc_jit_result_release` on it once they're done
kono
parents:
diff changeset
42 with it.
kono
parents:
diff changeset
43
kono
parents:
diff changeset
44 .. type:: gcc_jit_result
kono
parents:
diff changeset
45
kono
parents:
diff changeset
46 A `gcc_jit_result` encapsulates the result of compiling a context
kono
parents:
diff changeset
47 in-memory, and the lifetimes of any machine code functions or globals
kono
parents:
diff changeset
48 that are within the result.
kono
parents:
diff changeset
49
kono
parents:
diff changeset
50 .. function:: void *\
kono
parents:
diff changeset
51 gcc_jit_result_get_code (gcc_jit_result *result,\
kono
parents:
diff changeset
52 const char *funcname)
kono
parents:
diff changeset
53
kono
parents:
diff changeset
54 Locate a given function within the built machine code.
kono
parents:
diff changeset
55
kono
parents:
diff changeset
56 Functions are looked up by name. For this to succeed, a function
kono
parents:
diff changeset
57 with a name matching `funcname` must have been created on
kono
parents:
diff changeset
58 `result`'s context (or a parent context) via a call to
kono
parents:
diff changeset
59 :func:`gcc_jit_context_new_function` with `kind`
kono
parents:
diff changeset
60 :macro:`GCC_JIT_FUNCTION_EXPORTED`:
kono
parents:
diff changeset
61
kono
parents:
diff changeset
62 .. code-block:: c
kono
parents:
diff changeset
63
kono
parents:
diff changeset
64 gcc_jit_context_new_function (ctxt,
kono
parents:
diff changeset
65 any_location, /* or NULL */
kono
parents:
diff changeset
66 /* Required for func to be visible to
kono
parents:
diff changeset
67 gcc_jit_result_get_code: */
kono
parents:
diff changeset
68 GCC_JIT_FUNCTION_EXPORTED,
kono
parents:
diff changeset
69 any_return_type,
kono
parents:
diff changeset
70 /* Must string-compare equal: */
kono
parents:
diff changeset
71 funcname,
kono
parents:
diff changeset
72 /* etc */);
kono
parents:
diff changeset
73
kono
parents:
diff changeset
74 If such a function is not found (or `result` or `funcname` are
kono
parents:
diff changeset
75 ``NULL``), an error message will be emitted on stderr and
kono
parents:
diff changeset
76 ``NULL`` will be returned.
kono
parents:
diff changeset
77
kono
parents:
diff changeset
78 If the function is found, the result will need to be cast to a
kono
parents:
diff changeset
79 function pointer of the correct type before it can be called.
kono
parents:
diff changeset
80
kono
parents:
diff changeset
81 Note that the resulting machine code becomes invalid after
kono
parents:
diff changeset
82 :func:`gcc_jit_result_release` is called on the
kono
parents:
diff changeset
83 :type:`gcc_jit_result *`; attempting to call it after that may lead
kono
parents:
diff changeset
84 to a segmentation fault.
kono
parents:
diff changeset
85
kono
parents:
diff changeset
86 .. function:: void *\
kono
parents:
diff changeset
87 gcc_jit_result_get_global (gcc_jit_result *result,\
kono
parents:
diff changeset
88 const char *name)
kono
parents:
diff changeset
89
kono
parents:
diff changeset
90 Locate a given global within the built machine code.
kono
parents:
diff changeset
91
kono
parents:
diff changeset
92 Globals are looked up by name. For this to succeed, a global
kono
parents:
diff changeset
93 with a name matching `name` must have been created on
kono
parents:
diff changeset
94 `result`'s context (or a parent context) via a call to
kono
parents:
diff changeset
95 :func:`gcc_jit_context_new_global` with `kind`
kono
parents:
diff changeset
96 :macro:`GCC_JIT_GLOBAL_EXPORTED`.
kono
parents:
diff changeset
97
kono
parents:
diff changeset
98 If the global is found, the result will need to be cast to a
kono
parents:
diff changeset
99 pointer of the correct type before it can be called.
kono
parents:
diff changeset
100
kono
parents:
diff changeset
101 This is a *pointer* to the global, so e.g. for an :c:type:`int` this is
kono
parents:
diff changeset
102 an :c:type:`int *`.
kono
parents:
diff changeset
103
kono
parents:
diff changeset
104 For example, given an ``int foo;`` created this way:
kono
parents:
diff changeset
105
kono
parents:
diff changeset
106 .. code-block:: c
kono
parents:
diff changeset
107
kono
parents:
diff changeset
108 gcc_jit_lvalue *exported_global =
kono
parents:
diff changeset
109 gcc_jit_context_new_global (ctxt,
kono
parents:
diff changeset
110 any_location, /* or NULL */
kono
parents:
diff changeset
111 GCC_JIT_GLOBAL_EXPORTED,
kono
parents:
diff changeset
112 int_type,
kono
parents:
diff changeset
113 "foo");
kono
parents:
diff changeset
114
kono
parents:
diff changeset
115 we can access it like this:
kono
parents:
diff changeset
116
kono
parents:
diff changeset
117 .. code-block:: c
kono
parents:
diff changeset
118
kono
parents:
diff changeset
119 int *ptr_to_foo =
kono
parents:
diff changeset
120 (int *)gcc_jit_result_get_global (result, "foo");
kono
parents:
diff changeset
121
kono
parents:
diff changeset
122 If such a global is not found (or `result` or `name` are
kono
parents:
diff changeset
123 ``NULL``), an error message will be emitted on stderr and
kono
parents:
diff changeset
124 ``NULL`` will be returned.
kono
parents:
diff changeset
125
kono
parents:
diff changeset
126 Note that the resulting address becomes invalid after
kono
parents:
diff changeset
127 :func:`gcc_jit_result_release` is called on the
kono
parents:
diff changeset
128 :type:`gcc_jit_result *`; attempting to use it after that may lead
kono
parents:
diff changeset
129 to a segmentation fault.
kono
parents:
diff changeset
130
kono
parents:
diff changeset
131 .. function:: void\
kono
parents:
diff changeset
132 gcc_jit_result_release (gcc_jit_result *result)
kono
parents:
diff changeset
133
kono
parents:
diff changeset
134 Once we're done with the code, this unloads the built .so file.
kono
parents:
diff changeset
135 This cleans up the result; after calling this, it's no longer
kono
parents:
diff changeset
136 valid to use the result, or any code or globals that were obtained
kono
parents:
diff changeset
137 by calling :func:`gcc_jit_result_get_code` or
kono
parents:
diff changeset
138 :func:`gcc_jit_result_get_global` on it.
kono
parents:
diff changeset
139
kono
parents:
diff changeset
140
kono
parents:
diff changeset
141 Ahead-of-time compilation
kono
parents:
diff changeset
142 *************************
kono
parents:
diff changeset
143
kono
parents:
diff changeset
144 Although libgccjit is primarily aimed at just-in-time compilation, it
kono
parents:
diff changeset
145 can also be used for implementing more traditional ahead-of-time
kono
parents:
diff changeset
146 compilers, via the :c:func:`gcc_jit_context_compile_to_file`
kono
parents:
diff changeset
147 API entrypoint.
kono
parents:
diff changeset
148
kono
parents:
diff changeset
149 .. function:: void \
kono
parents:
diff changeset
150 gcc_jit_context_compile_to_file (gcc_jit_context *ctxt, \
kono
parents:
diff changeset
151 enum gcc_jit_output_kind output_kind,\
kono
parents:
diff changeset
152 const char *output_path)
kono
parents:
diff changeset
153
kono
parents:
diff changeset
154 Compile the :c:type:`gcc_jit_context *` to a file of the given
kono
parents:
diff changeset
155 kind.
kono
parents:
diff changeset
156
kono
parents:
diff changeset
157 :c:func:`gcc_jit_context_compile_to_file` ignores the suffix of
kono
parents:
diff changeset
158 ``output_path``, and insteads uses the given
kono
parents:
diff changeset
159 :c:type:`enum gcc_jit_output_kind` to decide what to do.
kono
parents:
diff changeset
160
kono
parents:
diff changeset
161 .. note::
kono
parents:
diff changeset
162
kono
parents:
diff changeset
163 This is different from the ``gcc`` program, which does make use of the
kono
parents:
diff changeset
164 suffix of the output file when determining what to do.
kono
parents:
diff changeset
165
kono
parents:
diff changeset
166 .. type:: enum gcc_jit_output_kind
kono
parents:
diff changeset
167
kono
parents:
diff changeset
168 The available kinds of output are:
kono
parents:
diff changeset
169
kono
parents:
diff changeset
170 ============================================== ==============
kono
parents:
diff changeset
171 Output kind Typical suffix
kono
parents:
diff changeset
172 ============================================== ==============
kono
parents:
diff changeset
173 :c:macro:`GCC_JIT_OUTPUT_KIND_ASSEMBLER` .s
kono
parents:
diff changeset
174 :c:macro:`GCC_JIT_OUTPUT_KIND_OBJECT_FILE` .o
kono
parents:
diff changeset
175 :c:macro:`GCC_JIT_OUTPUT_KIND_DYNAMIC_LIBRARY` .so or .dll
kono
parents:
diff changeset
176 :c:macro:`GCC_JIT_OUTPUT_KIND_EXECUTABLE` None, or .exe
kono
parents:
diff changeset
177 ============================================== ==============
kono
parents:
diff changeset
178
kono
parents:
diff changeset
179 .. c:macro:: GCC_JIT_OUTPUT_KIND_ASSEMBLER
kono
parents:
diff changeset
180
kono
parents:
diff changeset
181 Compile the context to an assembler file.
kono
parents:
diff changeset
182
kono
parents:
diff changeset
183 .. c:macro:: GCC_JIT_OUTPUT_KIND_OBJECT_FILE
kono
parents:
diff changeset
184
kono
parents:
diff changeset
185 Compile the context to an object file.
kono
parents:
diff changeset
186
kono
parents:
diff changeset
187 .. c:macro:: GCC_JIT_OUTPUT_KIND_DYNAMIC_LIBRARY
kono
parents:
diff changeset
188
kono
parents:
diff changeset
189 Compile the context to a dynamic library.
kono
parents:
diff changeset
190
kono
parents:
diff changeset
191 There is currently no support for specifying other libraries to link
kono
parents:
diff changeset
192 against.
kono
parents:
diff changeset
193
kono
parents:
diff changeset
194 .. c:macro:: GCC_JIT_OUTPUT_KIND_EXECUTABLE
kono
parents:
diff changeset
195
kono
parents:
diff changeset
196 Compile the context to an executable.
kono
parents:
diff changeset
197
kono
parents:
diff changeset
198 There is currently no support for specifying libraries to link
kono
parents:
diff changeset
199 against.