This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
add OP_ARGELEM, OP_ARGDEFELEM, OP_ARGCHECK ops
authorDavid Mitchell <davem@iabyn.com>
Sat, 9 Jul 2016 09:41:08 +0000 (10:41 +0100)
committerDavid Mitchell <davem@iabyn.com>
Wed, 3 Aug 2016 19:54:40 +0000 (20:54 +0100)
commit4fa06845e75d453a3101cff32e24c5b743f9819e
tree71f5473b348e99044ad80eab8a2416a3c8f9a177
parent6cb4123eb32087e8546f1056ca7b4e761c28d9b7
add OP_ARGELEM, OP_ARGDEFELEM, OP_ARGCHECK ops

Currently subroutine signature parsing emits many small discrete ops
to implement arg handling. This commit replaces them with a couple of ops
per signature element, plus an initial signature check op.

These new ops are added to the OP tree during parsing, so will be visible
to hooks called up to and including peephole optimisation. It is intended
soon that the peephole optimiser will take these per-element ops, and
replace them with a single OP_SIGNATURE op which handles the whole
signature in a single go. So normally these ops wont actually get executed
much. But adding these intermediate-level ops gives three advantages:

1) it allows the parser to efficiently generate subtrees containing
   individual signature elements, which can't be done if only OP_SIGNATURE
   or discrete ops are available;
2) prior to optimisation, it provides a simple and straightforward
   representation of the signature;
3) hooks can mess with the signature OP subtree in ways that make it
   no longer possible to optimise into an OP_SIGNATURE, but which can
   still be executed, deparsed etc (if less efficiently).

This code:

    use feature "signatures";
    sub f($a, $, $b = 1, @c)  {$a}

under 'perl -MO=Concise,f' now gives:

    d  <1> leavesub[1 ref] K/REFC,1 ->(end)
    -     <@> lineseq KP ->d
    1        <;> nextstate(main 84 foo:6) v:%,469762048 ->2
    2        <+> argcheck(3,1,@) v ->3
    3        <;> nextstate(main 81 foo:6) v:%,469762048 ->4
    4        <+> argelem(0)[$a:81,84] v/SV ->5
    5        <;> nextstate(main 82 foo:6) v:%,469762048 ->6
    8        <+> argelem(2)[$b:82,84] vKS/SV ->9
    6           <|> argdefelem(other->7)[2] sK ->8
    7              <$> const(IV 1) s ->8
    9        <;> nextstate(main 83 foo:6) v:%,469762048 ->a
    a        <+> argelem(3)[@c:83,84] v/AV ->b
    -        <;> ex-nextstate(main 84 foo:6) v:%,469762048 ->b
    b        <;> nextstate(main 84 foo:6) v:%,469762048 ->c
    c        <0> padsv[$a:81,84] s ->d

The argcheck(3,1,@) op knows the number of positional params (3), the
number of optional params (1), and whether it has an array / hash slurpy
element at the end. This op is responsible for checking that @_ contains
the right number of args.

A simple argelem(0)[$a] op does the equivalent of 'my $a = $_[0]'.
Similarly, argelem(3)[@c] is equivalent to 'my @c = @_[3..$#_]'.
If it has a child, it gets its arg from the stack rather than using $_[N].
Currently the only used child is the logop argdefelem.

argdefelem(other->7)[2] is equivalent to '@_ > 2 ? $_[2] : other'.

[ These ops currently assume that the lexical var being introduced
is undef/empty and non-magival etc. This is an incorrect assumption and
is fixed in a few commits' time ]
25 files changed:
dump.c
embed.fnc
embed.h
ext/B/B.pm
ext/B/B.xs
ext/B/B/Concise.pm
ext/Opcode/Opcode.pm
lib/B/Deparse.pm
lib/B/Op_private.pm
op.c
opcode.h
opnames.h
perly.act
perly.h
perly.tab
perly.y
pod/perldiag.pod
pp.c
pp_hot.c
pp_proto.h
proto.h
regen/op_private
regen/opcodes
t/op/signatures.t
toke.c