develooper Front page | perl.perl5.porters | Postings from April 2008

Re: prototypes, parsing and optrees

Thread Previous
From:
Nicholas Clark
Date:
April 29, 2008 16:39
Subject:
Re: prototypes, parsing and optrees
Message ID:
20080429191515.GZ84051@plum.flirble.org
On Tue, Apr 29, 2008 at 08:30:38PM +0200, Aristotle Pagaltzis wrote:
> * Nicholas Clark <nick@ccl4.org> [2008-04-29 19:40]:
> > The version without the prototype has an extra l flag on the
> > refgen. (If I've understood the documentation of B::Concise
> > correctly, that means that it knows that it's in list context).
> > 
> > How come the version with the \@ prototype isn't able to work
> > out that it is in list context?
> 
> But is it in list context?

Well, I wasn't sure sure, but...

> What I am a little surprised about is that in the prototyped sub,
> the `refgen` isn???t in scalar context *either*. OTOH, `refgen` is
> unaffected by context anyway, and while taking a reference is not
> a list context, it???s not really a scalar context either.

yes, me too.

I guess that it should be in scalar context.

> So that observation seems to be an interesting bit of trivia but
> inconsequential to user-visible semantics.

but I would have thought that knowing the context before run time could
give a theoretical microscopic speed boost.

I wonder if there is a way to make this visible.


Also, looking at pp.c I see this:

PP(pp_srefgen)
{
    dVAR; dSP;
    *SP = refto(*SP);
    RETURN;
}

PP(pp_refgen)
{
    dVAR; dSP; dMARK;
    if (GIMME != G_ARRAY) {
	if (++MARK <= SP)
	    *MARK = *SP;
	else
	    *MARK = &PL_sv_undef;
	*MARK = refto(*MARK);
	SP = MARK;
	RETURN;
    }
    EXTEND_MORTAL(SP - MARK);
    while (++MARK <= SP)
	*MARK = refto(*MARK);
    RETURN;
}

yet

$ perl -MO=Concise -e '$a = \@ARGV'
9  <@> leave[1 ref] vKP/REFC ->(end)
1     <0> enter ->2
2     <;> nextstate(main 1 -e:1) v ->3
8     <2> sassign vKS/2 ->9
6        <1> refgen sK/1 ->7
-           <1> ex-list lKRM ->6
3              <0> pushmark sRM ->4
5              <1> rv2av[t1] lKRM/1 ->6
4                 <$> gv(*ARGV) s ->5
-        <1> ex-rv2sv sKRM*/1 ->8
7           <$> gvsv(*a) s ->8
-e syntax OK

in particular, refgen is 's' for scalar context, and the list can only
contain exactly one item, so I wonder if this could be optimised to something
like

5        <1> srefgen sK/1 ->6
-           <1> ex-list lKRM ->5
               <0> ex-pushmark sRM ->3
4              <1> rv2av[t1] lKRM/1 ->5
3                 <$> gv(*ARGV) s ->4


Mmm, a nice B::Generate that actually knew how to lace optrees back together
(or exposing a core API to do so) would make it somewhat easier to
experiment.

Nicholas Clark

Thread Previous


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About