develooper Front page | perl.perl5.porters | Postings from March 2015

Re: OP_SIGNATURE

Thread Previous | Thread Next
From:
Paul "LeoNerd" Evans
Date:
March 5, 2015 18:49
Subject:
Re: OP_SIGNATURE
Message ID:
20150305184852.3bd82a20@shy.leonerd.org.uk
On Thu, 5 Mar 2015 12:59:41 +0000
Dave Mitchell <davem@iabyn.com> wrote:

>     sub f ($a, $b = 1, $c = $a++) {}
> 
> is compiled as:
> 
>     1  <;> nextstate(main 77 p:5) v:%,469762048
>     2  <+> signature($a, $b=1, $c=<expr>) v
>     3  <;> nextstate(main 79 p:5) v:%,469762048
>     4  <0> padsv[$a:77,80] sRM
>     5  <1> postinc[t4] sK/1
>     6  <0> padsv[$c:79,80] sRM*
>     7  <2> sassign vKS/2
>     8  <;> nextstate(main 80 p:5) :%,469762048
>     9  <1> leavesub[1 ref] K/REFC,1

OK.

Lets posit a few more ops, which are generated by the peepholer in the
presence of the given PP equivalent. Every one of these is a PAD-style
op, having the usual TARG or PADIX for threadiness. For briefness I'll
omit the PADIX versions below:

  $lexvar = (constant IV)  ==>  OP_PADASSIGNIV[$lexvar, IV=iv]

  $lexvar = @_>N ? $_[N] : EXPR
                           ==>  OP_PARAM[$lexvar, N=n], first=EXPR

  $lexvar = @_>N ? $_[N] : die "Arity check yada yada"
                           ==>  OP_PARAM[$lexvar, N=n], first=NULL

  @_>=L and @_<=U or die "Arity check yada yada"
                           ==>  OP_ARITY[L, U]

Then your sub becomes

  OP_ARITY[1, 3]
  OP_PARAM[$a, 0]
  OP_PARAM[$b, 1]
      OP_PADASSIGNIV[$b, IV=1]
  OP_PARAM[$c, 2]
      OP_PADSV[$a]
      OP_POSTINC
      OP_PADSV[$c]
      OP_SASSIGN

Which compares fairly nicely with yours; the
padsv/postinc/padsv/sassign are all still present in the same order,
I've just changed a single OP_SIGNATURE into an OP_ARITY and three
OP_PARAMs.

Except all of a sudden, I can start applying OP_PADASSIGN* ops
*anywhere* using the peepholer, not just when implementing 5.20-style
signatures. Maybe with a bit more than the ~5 minutes of thinking I've
put into OP_PARAM, it would also be possible to define an op capable of
replacing a common pattern of existing code in pre-signature code on
CPAN.

Again to reiterate what I've said elsewhere on the thread: lets
optimise *all* the perl code we find, on CPAN or elsewhere, and not
*just* 5.20-style signatures.

-- 
Paul "LeoNerd" Evans

leonerd@leonerd.org.uk
http://www.leonerd.org.uk/  |  https://metacpan.org/author/PEVANS

Thread Previous | Thread Next


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