longjmp to the callee

Discussion in 'C Programming' started by Jerald Fijerald, Feb 24, 2005.

  1. Hello.

    I'm trying to find an elegant way to longjmp to the callee
    (aka "generators for C")

    So far I've come to this experimental program (gcc only as it uses
    __builtin_frame_address, although we can do without it):

    ======================================================================
    /* longjmp to the callee */

    #include <setjmp.h>
    #include <stdio.h>

    jmp_buf F;
    jmp_buf F2;

    static inline char* bfr ()
    {
    return __builtin_frame_address (0);
    }

    char *top;

    int foo()
    {
    int i;
    for (i = 0; i < 5; i++) {
    printf (".i=%i\n", i);
    if (i == 2) {
    if (!setjmp (F2)) {
    top = bfr();
    //printf ("Mytop=%p\n", top);
    longjmp (F, 1);
    } else {
    printf ("resumed...\n");
    }
    }
    printf ("i=%i\n", i);
    }
    return 0;
    }

    int bar ()
    {
    return foo ();
    }

    int potatoe ()
    {
    int i, j;
    for (i = 0, j = 10; i < j; i++)
    printf ("%i ", i);
    printf ("\n");
    return 132;
    }

    int main ()
    {
    int i, j;
    char *thistop;
    for (i = 0; i < 3; i++)
    if (!setjmp (F)) {
    printf ("trying...\n");
    bar();
    } else {
    /* preserve the stack */
    j=40;
    char CAP1[j];
    thistop = bfr();
    char GAP2 [top-thistop];

    printf ("caught...\n");

    /* test that stack is not polluted */
    potatoe ();

    /* longjmp to the callee and resume */
    longjmp (F2, 2);
    }
    return 0;
    }
    =============================================================

    Notes:
    longjmp already supports jumping to a callee. The problem
    is that we have to preserve the stack (and thus the function
    potatoe() which is supposed to polute it).
    The solution I found is to use C99 variable-length arrays to
    displace the stacktop beyond the region we want to preserve.

    I'm looking for ideas/enhancements to make this more portable,
    safe, fast, etc. It must have been done before, right?


    jf
     
    Jerald Fijerald, Feb 24, 2005
    #1
    1. Advertising

  2. In article <>,
    Jerald Fijerald <> wrote:
    >Hello.
    >
    >I'm trying to find an elegant way to longjmp to the callee
    >(aka "generators for C")


    You can't. longjmp can only jump into stack[1] frames that are still
    live.

    If this is a "fun" project, you may have found an excuse to go learn
    about functional languages that support continuations (which, at a first
    glance, appears to be exactly what you're looking for).

    If you can't change your tools, you might find something not entirely
    unlike what you're looking for at
    www.chiark.greenend.org.uk/~sgtatham/coroutines.html


    dave

    [1] That's the function-invocation stack, which need not be represented
    with the "usual" block of memory that the hardware treats as a stack.
    Just mentioning it so that the pedants don't jump on me for it.

    --
    Dave Vandervies
    Yes, I'm a middle-aged fogy. Forty years ago I was a young fogy.
    In fifteen years or so, I'll be an old fogy.
    --Mike Andrews in the scary devil monastery
     
    Dave Vandervies, Feb 24, 2005
    #2
    1. Advertising

  3. Jerald Fijerald

    Eric Sosman Guest

    [OT] Re: longjmp to the callee

    Dave Vandervies wrote:
    > In article <>,
    > Jerald Fijerald <> wrote:
    >
    >>Hello.
    >>
    >>I'm trying to find an elegant way to longjmp to the callee
    >>(aka "generators for C")

    >
    >
    > You can't. longjmp can only jump into stack[1] frames that are still
    > live.
    >
    > If this is a "fun" project, you may have found an excuse to go learn
    > about functional languages that support continuations (which, at a first
    > glance, appears to be exactly what you're looking for).
    >
    > If you can't change your tools, you might find something not entirely
    > unlike what you're looking for at
    > www.chiark.greenend.org.uk/~sgtatham/coroutines.html
    >
    >
    > dave
    >
    > [1] That's the function-invocation stack, which need not be represented
    > with the "usual" block of memory that the hardware treats as a stack.
    > Just mentioning it so that the pedants don't jump on me for it.


    Then I guess we'll have to longjmp() on you for it.

    --
     
    Eric Sosman, Feb 24, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. jai hanuman

    caller / callee graph ...

    jai hanuman, Dec 16, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    690
    jai hanuman
    Dec 16, 2003
  2. John Dalberg
    Replies:
    0
    Views:
    767
    John Dalberg
    Jun 21, 2004
  3. Daryl Davis

    Invalid Callee

    Daryl Davis, Jul 2, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    1,523
    Daryl Davis
    Jul 2, 2004
  4. Replies:
    19
    Views:
    462
    Ben Bacarisse
    Feb 22, 2006
  5. Replies:
    8
    Views:
    687
    James Kanze
    May 10, 2007
Loading...

Share This Page