Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Callstack manipulation?

by plobsing (Friar)
on Nov 01, 2008 at 19:19 UTC ( [id://720883]=note: print w/replies, xml ) Need Help??


in reply to Callstack manipulation?

I'm pretty sure you can build what you're talking about out of Coro.

AFAICT, it allows you to clone perl interpreters and jump between them, having only one active at a time. Since each perl interpreter has it's own callstack, this is, if I am not mistaken, analogous to your idea.

Replies are listed 'Best First'.
Re^2: Callstack manipulation?
by LanX (Saint) on Nov 01, 2008 at 20:01 UTC
    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.

      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 ;)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://720883]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (4)
As of 2024-04-25 06:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found