diff mbox

PATCH: other/59039: Undocumented __builtin_longjmp/__builtin_setjmp

Message ID 20131110162819.GA6525@intel.com
State New
Headers show

Commit Message

H.J. Lu Nov. 10, 2013, 4:28 p.m. UTC
Hi,

This patch documents __builtin_setjmp and __builtin_longjmp.  OK to
install?

Thanks.

H.J.
---
2013-11-10  H.J. Lu  <hongjiu.lu@intel.com>

	PR other/59039
	* doc/extend.texi: Document __builtin_setjmp and
	__builtin_longjmp.

Comments

Mike Stump Nov. 11, 2013, 6:10 p.m. UTC | #1
On Nov 10, 2013, at 8:28 AM, H.J. Lu <hongjiu.lu@intel.com> wrote:
> This patch documents __builtin_setjmp and __builtin_longjmp.  OK to
> install?

I like it.  I think these two should be documented.
Joseph Myers Nov. 11, 2013, 7:07 p.m. UTC | #2
On Sun, 10 Nov 2013, H.J. Lu wrote:

> +@code{__builtin_setjmp} and @code{__builtin_longjmp} may not be used
> +in the same function with the same @code{setjmp} buffer.

Are there any other restrictions?  Do they, for example, guarantee to 
restore all call-saved registers to their values at the time of the 
__builtin_longjmp call, including those for which libc's setjmp and 
longjmp need to test for existence at runtime (but they may have been used 
in an intermediate stack frame, even if not enabled in either the setjmp 
or longjmp frames)?

Either you need to document all the semantics so as to make clear to users 
exactly when they can use these functions safely, or you need to state 
that there are undocumented restrictions that may change from GCC version 
to GCC version and that these functions are only intended for internal use 
as part of GCC's exception handling implementation.
Eric Botcazou Nov. 15, 2013, 12:32 p.m. UTC | #3
> Are there any other restrictions?  Do they, for example, guarantee to
> restore all call-saved registers to their values at the time of the
> __builtin_longjmp call, including those for which libc's setjmp and
> longjmp need to test for existence at runtime (but they may have been used
> in an intermediate stack frame, even if not enabled in either the setjmp
> or longjmp frames)?

__builtin_longjmp/__builtin_setjmp don't save or restore anything themselves.
They cause the compiler to generate code that saves and restores registers 
live across the non-local goto paths, but of course only registers known to 
the compiler and known to be used in the code.

> Either you need to document all the semantics so as to make clear to users
> exactly when they can use these functions safely, or you need to state
> that there are undocumented restrictions that may change from GCC version
> to GCC version and that these functions are only intended for internal use
> as part of GCC's exception handling implementation.

The restrictions have probably not changed since the builtins were invented.
Mike Stump Nov. 15, 2013, 6:06 p.m. UTC | #4
On Nov 15, 2013, at 4:32 AM, Eric Botcazou <ebotcazou@adacore.com> wrote:
>> Either you need to document all the semantics so as to make clear to users
>> exactly when they can use these functions safely, or you need to state
>> that there are undocumented restrictions that may change from GCC version
>> to GCC version and that these functions are only intended for internal use
>> as part of GCC's exception handling implementation.
> 
> The restrictions have probably not changed since the builtins were invented.

:-)  Yeah, don't think I've seen too much change swirling around these two.
diff mbox

Patch

diff --git a/gcc/doc/extend.texi b/gcc/doc/extend.texi
index 0d72819..44b7c59 100644
--- a/gcc/doc/extend.texi
+++ b/gcc/doc/extend.texi
@@ -27,6 +27,7 @@  extensions, accepted by GCC in C90 mode and in C++.
 * Local Labels::        Labels local to a block.
 * Labels as Values::    Getting pointers to labels, and computed gotos.
 * Nested Functions::    As in Algol and Pascal, lexical scoping of functions.
+* Nonlocal Gotos::      Nonlocal gotos
 * Constructing Calls::  Dispatching a call to another function.
 * Typeof::              @code{typeof}: referring to the type of an expression.
 * Conditionals::        Omitting the middle operand of a @samp{?:} expression.
@@ -518,6 +519,36 @@  bar (int *array, int offset, int size)
 @}
 @end smallexample
 
+@node Nonlocal Gotos
+@section Nonlocal Gotos
+@cindex nonlocal gotos
+
+GCC provides builtin functions @code{__builtin_setjmp} and
+@code{__builtin_longjmp} which operate similarly to the C library
+functions of the same name, but are more efficient.
+
+@deftypefn {Built-in Function} {int} __builtin_setjmp (intptr_t *@var{buf})
+This function saves the current stack context in @code{setjmp} buffer,
+@var{buf}.  @code{__builtin_setjmp} returns 0 when returning directly,
+and @var{1} when returning from @code{__builtin_longjmp} using the same
+@code{setjmp} buffer.
+@end deftypefn
+
+@deftypefn {Built-in Function} {void} __builtin_longjmp (intptr_t *@var{buf}, int @var{val})
+This function restores the stack context in @code{setjmp} buffer,
+@var{buf}, saved by the last call of @code{__builtin_setjmp}.  After
+@code{__builtin_longjmp} is finished, program resumes execution as
+if the last @code{__builtin_setjmp} just returns the value @var{val},
+which must be @var{1}.
+@end deftypefn
+
+The @code{setjmp} buffer is an array of five @code{intptr_t}.  The buffer
+will generally contain the frame address, the resume address and the stack
+address.  The other elements may be used in a machine-specific way.
+
+@code{__builtin_setjmp} and @code{__builtin_longjmp} may not be used
+in the same function with the same @code{setjmp} buffer.
+
 @node Constructing Calls
 @section Constructing Function Calls
 @cindex constructing calls