http://www.perlmonks.org?node_id=713229


in reply to Re: Using vec() from XS
in thread Using vec() from XS

There should be a way to create a sequence of OPs (containing the required pp_vec) and run it.

Could you expand on this a little please?


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

Replies are listed 'Best First'.
Re^3: Using vec() from XS
by salva (Canon) on Sep 23, 2008 at 16:13 UTC
    I don't know the details, but it should be something like this:
    1. synthesize an OP o for vec
    2. set up the runtime environment: push the arguments on the stack, save PL_op and make it point to the synthesized OP o, set any other interpreter globals, stacks, etc.
    3. call CALLRUNOPS
    4. retrieve the result from o->op_targ
    5. clean up
    For and example, look at Perl_fold_constants from op.c from the perl source. Try running it inside a C debugger with a simple fold-able expression as print(1+2).

    The C function Perl_op_dump can be used to dump an OP tree once it has been properly build (otherwise it will crash).

      1. synthesize an OP o

      That phrase means absolutely nothing to me! So I perldoc'd; I grepped the sources; I filtered the htmlise perldocs; I googled.

      Only the latter turned up that phrase. And other than your post above, the only references google has to "synthesizing an[d] op", from the >25 billion web pages it indexes, are:

      • synthesize an op-. erating procedure
      • synthesize an o p. tima. supervim
      • synthesize an op-. timal topology
      • synthesize an op-. tical matched spatial filter
      • (synthesize) an op-. erationally larger antenna

      So, could you please explain what you mean by that?

      I did look at Perl_fold_constants:

      and I can relate nothing there to that phrase. So, could you please treat me like the thicko I am, and explain what ... you are suggesting, cos I just do not ... understand!

      A long time ago, before the dawn of time, a young, budding mechanical engineer was taken to task for a suggestion he made, but failed to be able to explain. The upshot was: If you cannot explain it, you do not really understand it, so you should not suggest it.


      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.
        The problem is me, probably, English is not my native tongue!

        An OP is the structure used internally by perl to represent program code. By synthesize an OP I meant to artificially build and OP (in opposition to its natural birth at the perl parser).

        And that means creating some OPs (maybe just one will do, I am not sure) using the newXXXOP functions, building a tree/sequence with them and setting up its internal state as if they had come from the parser.

        But anyway, all this stuff would be far more complicated than just replicating the code for pp_vec.

Re^3: Using vec() from XS
by tsee (Curate) on Sep 24, 2008 at 07:38 UTC

    Most likely, you already know that Perl is compiled to a tree of ops. pp_vec is one such op type -- corresponding to the Perl vec() function.

    Now, you could create op structures from XS and inject them into the running program. B::Generate does stuff like that.

    The one example I'm aware of where this actually works well is List::Util::shuffle. Instead of repeating what's happening there, check for yourself: List::Util XS code.

    I tried to do something similar in order to get caller() in XS, but that experiment mostly failed. I would not suggest going down that route.

    Cheers,
    Steffen