in reply to Re: Callstack manipulation?
in thread Callstack manipulation?

Well I saw coro, but "cloning the perl interpreter" sounds very expensive ... do you think this is a practical approach?

Theoratically speaking: If it's possible to safely do a 'goto &sub' without leaving the call frame, why shouldn't it be possible to simulater this state of the engine right after the call ... it's just a "return" without moving the process-pointer.

I'm not to fond about XS but maybe it would be possible to add this behaviour by intergrating special C Code manipulating the stack.

Saying this a have only a superficial knowledge of the interpreter, maybe someone can point to serious sideeffects which strictly disallow this approach.

Replies are listed 'Best First'.
Re^3: Callstack manipulation?
by plobsing (Friar) on Nov 02, 2008 at 04:02 UTC

    I'm sorry, I don't really know Coro all that well. Consulting the docs, it says that a coroutine consists of:

    • a callchain
    • a set of lexical variables
    • a C stack
    • @_, $_, $@, and $/

    Everything else is shared. I don't see how you could get around having the first three items kept seperate. The fourth item is likely to be small compared to the others.

    In terms of actually cloning the perl interpreter, a quick test (given bellow) shows that a new callchain is created rather than cloning the current one. So I guess I wouldn't describe the action as cloning

    Some benchmarking would probably be worthwhile to determine how much Coro actually costs in terms of time and space required. From there you can determine whether or not Coro is practical or not for your problem.

    use strict; use warnings; use Carp 'carp'; use Coro; $\ = $/; sub A { my $a = 1; async { print $a++; carp; cede; print $a++; } print $a++; carp; cede; print $a++; } A;
      Hi plobsing,

      thanks a lot for your help! : )

      I should mention that Moritz already implemented gather&take using coro http://search.cpan.org/~moritz/Perl6-GatherTake/ ...but he doesn't give the impression to be very convinced about the outcome.

      Nevertheless I just wanted to give a motivation for a more fundamental question:

      Are there any opcodes allowing to manipulate the call stack?

      Popping and pushing the stack gives an approach to a whole bunch of Assembler techniques (and pitfalls of course ;)

        > Are there any opcodes allowing to manipulate the call stack?

        Ok, at least one can manipulate the stack with XS, I found some examples in Advanced Perl Programming, describing macros to do so!

        8 )

        (over 10 years old but still an interesting read *)

        UPDATES:

      • Concerning coro: AFAIS the coroutines get separated lexical variable spaces, complicating a Gather/Take-Algorithm to access "surrounding" variables.
      • * I was talking about the first edition!