Commit 81ba15f1 by David Malcolm Committed by David Malcolm

Improvements to documentation of gcc_jit_context_release

gcc/jit/ChangeLog:
	* docs/examples/tut02-square.c (main): Release the context
	earlier, to show that this is possible.  Update error-handling
	to avoid a double-release of the context, and to avoid
	releasing a NULL result.
	* docs/intro/tutorial02.rst: Discuss gcc_jit_context_release.
	* docs/topics/functions.rst (GCC_JIT_FUNCTION_EXPORTED): Emphasize
	* docs/topics/results.rst (gcc_jit_result): Mention that this
	controls the lifetimes of machine code functions.
	(gcc_jit_result_get_code): Spell out the requirements for this
	to succeed, and the lifetime of the result.
	(gcc_jit_result_release): Mention that this invalidates any code
	that was obtained from the result.
	* docs/_build/texinfo/libgccjit.texi: Regenerate.

From-SVN: r218245
parent ad1f2178
2014-12-01 David Malcolm <dmalcolm@redhat.com> 2014-12-01 David Malcolm <dmalcolm@redhat.com>
* docs/examples/tut02-square.c (main): Release the context
earlier, to show that this is possible. Update error-handling
to avoid a double-release of the context, and to avoid
releasing a NULL result.
* docs/intro/tutorial02.rst: Discuss gcc_jit_context_release.
* docs/topics/functions.rst (GCC_JIT_FUNCTION_EXPORTED): Emphasize
* docs/topics/results.rst (gcc_jit_result): Mention that this
controls the lifetimes of machine code functions.
(gcc_jit_result_get_code): Spell out the requirements for this
to succeed, and the lifetime of the result.
(gcc_jit_result_release): Mention that this invalidates any code
that was obtained from the result.
* docs/_build/texinfo/libgccjit.texi: Regenerate.
2014-12-01 David Malcolm <dmalcolm@redhat.com>
PR jit/64018 PR jit/64018
* docs/intro/tutorial02.rst: Spell out lifetime of generated code. * docs/intro/tutorial02.rst: Spell out lifetime of generated code.
Add description of error-handling, taken in part from... Add description of error-handling, taken in part from...
......
...@@ -630,6 +630,14 @@ result = gcc_jit_context_compile (ctxt); ...@@ -630,6 +630,14 @@ result = gcc_jit_context_compile (ctxt);
and get a @pxref{16,,gcc_jit_result *}. and get a @pxref{16,,gcc_jit_result *}.
At this point we're done with the context; we can release it:
@example
gcc_jit_context_release (ctxt);
@end example
@noindent
We can now use @pxref{17,,gcc_jit_result_get_code()} to look up a specific We can now use @pxref{17,,gcc_jit_result_get_code()} to look up a specific
machine code routine within the result, in this case, the function we machine code routine within the result, in this case, the function we
created above. created above.
...@@ -935,6 +943,10 @@ main (int argc, char **argv) ...@@ -935,6 +943,10 @@ main (int argc, char **argv)
goto error; goto error;
@} @}
/* We're done with the context; we can release it: */
gcc_jit_context_release (ctxt);
ctxt = NULL;
/* Extract the generated code from "result". */ /* Extract the generated code from "result". */
void *fn_ptr = gcc_jit_result_get_code (result, "square"); void *fn_ptr = gcc_jit_result_get_code (result, "square");
if (!fn_ptr) if (!fn_ptr)
...@@ -948,8 +960,10 @@ main (int argc, char **argv) ...@@ -948,8 +960,10 @@ main (int argc, char **argv)
printf ("result: %d", square (5)); printf ("result: %d", square (5));
error: error:
gcc_jit_context_release (ctxt); if (ctxt)
gcc_jit_result_release (result); gcc_jit_context_release (ctxt);
if (result)
gcc_jit_result_release (result);
return 0; return 0;
@} @}
...@@ -5877,6 +5891,10 @@ values: ...@@ -5877,6 +5891,10 @@ values:
Function is defined by the client code and visible Function is defined by the client code and visible
by name outside of the JIT. by name outside of the JIT.
This value is required if you want to extract machine code
for this function from a @pxref{16,,gcc_jit_result} via
@pxref{17,,gcc_jit_result_get_code()}.
@end deffn @end deffn
@geindex GCC_JIT_FUNCTION_INTERNAL (C macro) @geindex GCC_JIT_FUNCTION_INTERNAL (C macro)
...@@ -6251,7 +6269,9 @@ file, giving you @emph{something} you can step through in the debugger. ...@@ -6251,7 +6269,9 @@ file, giving you @emph{something} you can step through in the debugger.
@anchor{topics/results gcc_jit_result}@anchor{16} @anchor{topics/results gcc_jit_result}@anchor{16}
@deffn {C Type} gcc_jit_result @deffn {C Type} gcc_jit_result
A @cite{gcc_jit_result} encapsulates the result of compiling a context. A @cite{gcc_jit_result} encapsulates the result of compiling a context,
and the lifetimes of any machine code functions that are
returned.
@end deffn @end deffn
@geindex gcc_jit_context_compile (C function) @geindex gcc_jit_context_compile (C function)
...@@ -6267,8 +6287,38 @@ This calls into GCC and builds the code, returning a ...@@ -6267,8 +6287,38 @@ This calls into GCC and builds the code, returning a
@deffn {C Function} void * gcc_jit_result_get_code (gcc_jit_result@w{ }*result, const char@w{ }*funcname) @deffn {C Function} void * gcc_jit_result_get_code (gcc_jit_result@w{ }*result, const char@w{ }*funcname)
Locate a given function within the built machine code. Locate a given function within the built machine code.
This will need to be cast to a function pointer of the
correct type before it can be called. Functions are looked up by name. For this to succeed, a function
with a name matching @cite{funcname} must have been created on
@cite{result}'s context (or a parent context) via a call to
@pxref{11,,gcc_jit_context_new_function()} with @cite{kind}
@pxref{ac,,GCC_JIT_FUNCTION_EXPORTED}:
@example
gcc_jit_context_new_function (ctxt,
any_location, /* or NULL */
/* Required for func to be visible to
gcc_jit_result_get_code: */
GCC_JIT_FUNCTION_EXPORTED,
any_return_type,
/* Must string-compare equal: */
funcname,
/* etc */);
@end example
@noindent
If such a function is not found (or @cite{result} or @cite{funcname} are
@code{NULL}), an error message will be emitted on stderr and
@code{NULL} will be returned.
If the function is found, the result will need to be cast to a
function pointer of the correct type before it can be called.
Note that the resulting machine code becomes invalid after
@pxref{39,,gcc_jit_result_release()} is called on the
@cite{gcc_jit_result *}; attempting to call it after that may lead
to a segmentation fault.
@end deffn @end deffn
@geindex gcc_jit_result_release (C function) @geindex gcc_jit_result_release (C function)
...@@ -6277,7 +6327,8 @@ correct type before it can be called. ...@@ -6277,7 +6327,8 @@ correct type before it can be called.
Once we're done with the code, this unloads the built .so file. Once we're done with the code, this unloads the built .so file.
This cleans up the result; after calling this, it's no longer This cleans up the result; after calling this, it's no longer
valid to use the result. valid to use the result, or any code that was obtained by calling
@pxref{17,,gcc_jit_result_get_code()} on it.
@end deffn @end deffn
@c Copyright (C) 2014 Free Software Foundation, Inc. @c Copyright (C) 2014 Free Software Foundation, Inc.
......
...@@ -88,6 +88,10 @@ main (int argc, char **argv) ...@@ -88,6 +88,10 @@ main (int argc, char **argv)
goto error; goto error;
} }
/* We're done with the context; we can release it: */
gcc_jit_context_release (ctxt);
ctxt = NULL;
/* Extract the generated code from "result". */ /* Extract the generated code from "result". */
void *fn_ptr = gcc_jit_result_get_code (result, "square"); void *fn_ptr = gcc_jit_result_get_code (result, "square");
if (!fn_ptr) if (!fn_ptr)
...@@ -101,7 +105,9 @@ main (int argc, char **argv) ...@@ -101,7 +105,9 @@ main (int argc, char **argv)
printf ("result: %d", square (5)); printf ("result: %d", square (5));
error: error:
gcc_jit_context_release (ctxt); if (ctxt)
gcc_jit_result_release (result); gcc_jit_context_release (ctxt);
if (result)
gcc_jit_result_release (result);
return 0; return 0;
} }
...@@ -192,6 +192,12 @@ OK, we've populated the context. We can now compile it using ...@@ -192,6 +192,12 @@ OK, we've populated the context. We can now compile it using
and get a :c:type:`gcc_jit_result *`. and get a :c:type:`gcc_jit_result *`.
At this point we're done with the context; we can release it:
.. code-block:: c
gcc_jit_context_release (ctxt);
We can now use :c:func:`gcc_jit_result_get_code` to look up a specific We can now use :c:func:`gcc_jit_result_get_code` to look up a specific
machine code routine within the result, in this case, the function we machine code routine within the result, in this case, the function we
created above. created above.
......
...@@ -84,6 +84,10 @@ Functions ...@@ -84,6 +84,10 @@ Functions
Function is defined by the client code and visible Function is defined by the client code and visible
by name outside of the JIT. by name outside of the JIT.
This value is required if you want to extract machine code
for this function from a :type:`gcc_jit_result` via
:func:`gcc_jit_result_get_code`.
.. macro:: GCC_JIT_FUNCTION_INTERNAL .. macro:: GCC_JIT_FUNCTION_INTERNAL
Function is defined by the client code, but is invisible Function is defined by the client code, but is invisible
......
...@@ -22,7 +22,9 @@ Compilation results ...@@ -22,7 +22,9 @@ Compilation results
.. type:: gcc_jit_result .. type:: gcc_jit_result
A `gcc_jit_result` encapsulates the result of compiling a context. A `gcc_jit_result` encapsulates the result of compiling a context,
and the lifetimes of any machine code functions that are
returned.
.. function:: gcc_jit_result *\ .. function:: gcc_jit_result *\
gcc_jit_context_compile (gcc_jit_context *ctxt) gcc_jit_context_compile (gcc_jit_context *ctxt)
...@@ -36,8 +38,36 @@ Compilation results ...@@ -36,8 +38,36 @@ Compilation results
const char *funcname) const char *funcname)
Locate a given function within the built machine code. Locate a given function within the built machine code.
This will need to be cast to a function pointer of the
correct type before it can be called. Functions are looked up by name. For this to succeed, a function
with a name matching `funcname` must have been created on
`result`'s context (or a parent context) via a call to
:func:`gcc_jit_context_new_function` with `kind`
:macro:`GCC_JIT_FUNCTION_EXPORTED`:
.. code-block:: c
gcc_jit_context_new_function (ctxt,
any_location, /* or NULL */
/* Required for func to be visible to
gcc_jit_result_get_code: */
GCC_JIT_FUNCTION_EXPORTED,
any_return_type,
/* Must string-compare equal: */
funcname,
/* etc */);
If such a function is not found (or `result` or `funcname` are
``NULL``), an error message will be emitted on stderr and
``NULL`` will be returned.
If the function is found, the result will need to be cast to a
function pointer of the correct type before it can be called.
Note that the resulting machine code becomes invalid after
:func:`gcc_jit_result_release` is called on the
`gcc_jit_result *`; attempting to call it after that may lead
to a segmentation fault.
.. function:: void\ .. function:: void\
...@@ -45,4 +75,5 @@ Compilation results ...@@ -45,4 +75,5 @@ Compilation results
Once we're done with the code, this unloads the built .so file. Once we're done with the code, this unloads the built .so file.
This cleans up the result; after calling this, it's no longer This cleans up the result; after calling this, it's no longer
valid to use the result. valid to use the result, or any code that was obtained by calling
:func:`gcc_jit_result_get_code` on it.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment